Guest User

Gwinnett 0.0.3

a guest
Apr 17th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 KB | None | 0 0
  1. package rsa.release;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Scanner;
  5.  
  6. import static rsa.release.Lib.*;
  7.  
  8. public class Gwinnett {
  9.     private static VQC vqc = new VQC();
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner s = new Scanner(System.in);
  13.  
  14.         for (;;) {
  15.             factor(s.nextBigInteger());
  16.             System.out.println();
  17.         }
  18.     }
  19.  
  20.     public static void factor(BigInteger c) {
  21.         /* Calculate E0, starting estimate */
  22.         VQCElement c1 = vqc.getEntryElement(c);
  23.         System.out.println(c1);
  24.         VQCElement D = vqc.getElementBelowRoot(c1.e, one, c1.d);
  25.         VQCElement D2 = vqc.getElement(c1.e, one, D.t.add(one));
  26.  
  27.         BigInteger i0 = D2.i;
  28.         BigInteger j0 = D.d;
  29.  
  30.         int amount_of_tests = 24;
  31.         for (int i = 0; i < amount_of_tests; i++) {
  32.  
  33.             /** Use in-class method to make d fixed to d from c */
  34.             VQCElement E = getElementFromSquares(i0, j0, c1.d);
  35.  
  36.             /* if d is fixed, (c - c0) = (e - e0) */
  37.             BigInteger diff = c.subtract(E.c);
  38.             BigInteger root_diff = sqrt(diff.abs());
  39.  
  40.             VQCElement temp = vqc.getElementBelowRoot(E.e, E.n, c1.d);
  41.             VQCElement E2 = vqc.getElement(E.e, E.n, temp.t.add(one));
  42.  
  43.             System.out.print(E);
  44.             System.out.println();
  45.  
  46.             /* Evaluating estimate */
  47.             if (isCorrectRoot(E.i, c)) {
  48.                 E = vqc.getFactorizationElement(c1, E.i.subtract(c1.d));
  49.  
  50.                 System.out.println(c + " = " + E.a + " * " + E.b);
  51.                 return;
  52.             }
  53.  
  54.             /* Adjusting */
  55.  
  56.             /* if c0 is less than c, move squares apart */
  57.             if (lt(E.c, c)) {
  58.                 i0 = i0.add(root_diff);
  59.                 j0 = sqrt(i0.pow(2).subtract(c));
  60.  
  61.                 /* Move squares closer */
  62.             } else {
  63.                 j0 = j0.add(diff);
  64.                 i0 = sqrt(j0.pow(2).add(c));
  65.             }
  66.         }
  67.     }
  68.  
  69.     private static VQCElement getElementFromSquares(BigInteger i, BigInteger j, BigInteger d) {
  70.         BigInteger c = i.pow(2).subtract(j.pow(2));
  71.         BigInteger e = c.subtract(d.pow(2));
  72.         BigInteger n = i.subtract(d);
  73.         BigInteger a = i.subtract(j);
  74.         BigInteger t = vqc.getT(e, (d.subtract(a)));
  75.  
  76.         return vqc.getElement(e, n, t);
  77.     }
  78. }
  79.  
  80. /** Library stuff follows, main code is above */
  81.  
  82. class Lib {
  83.     public static BigInteger zero = BigInteger.ZERO;
  84.     public static BigInteger one = BigInteger.ONE;
  85.     public static BigInteger two = BigInteger.valueOf(2);
  86.  
  87.     public static boolean gt(BigInteger i, BigInteger i2) { return i.compareTo(i2) > 0; }
  88.     public static boolean gteq(BigInteger i, BigInteger i2) { return i.compareTo(i2) >= 0; }
  89.     public static boolean lt(BigInteger i, BigInteger i2) { return i.compareTo(i2) < 0; }
  90.  
  91.     public static boolean isCorrectRoot(BigInteger i, BigInteger c) {
  92.         BigInteger i2 = i.multiply(i);
  93.         BigInteger j2 = i2.subtract(c);
  94.  
  95.         if (!lt(j2, one)) {
  96.             return isSquare(j2);
  97.         } else {
  98.             return false;
  99.         }
  100.     }
  101.  
  102.     public static boolean isEven(BigInteger e) { return (e.and(one)).equals(zero); }
  103.     public static boolean isSquare(BigInteger value) { return sqrt(value).pow(2).equals(value); }
  104.  
  105.     /** Efficient Newton square root method */
  106.     public static BigInteger sqrt(BigInteger n) {
  107.         if (n.equals(zero)) return zero;
  108.  
  109.         n = n.abs();
  110.  
  111.         if (gt(n, zero)) {
  112.             int bitLength = n.bitLength(); //ceil(log(n, 2))
  113.             BigInteger root = one.shiftLeft(bitLength >> 1); //right-shifting by one equals dividing by two
  114.  
  115.             while (!sqrtImpl(n, root)) {
  116.                 root = root.add(n.divide(root));
  117.                 root = root.shiftRight(1);
  118.             }
  119.  
  120.             return root;
  121.         }
  122.  
  123.         throw new ArithmeticException("Complex result: sqrt(" + n + ")");
  124.     }
  125.  
  126.     public static boolean sqrtImpl(BigInteger n, BigInteger root) {
  127.         BigInteger lowerBound = root.multiply(root);
  128.         BigInteger upperBound = root.add(one).multiply(root.add(one));
  129.  
  130.         return gteq(n, lowerBound) && lt(n, upperBound);
  131.     }
  132.  
  133. }
  134.  
  135. class VQC {
  136.     public VQC() {}
  137.  
  138.     public VQCElement getElement(BigInteger e, BigInteger n, BigInteger t) {
  139.         VQCElement E = new VQCElement();
  140.         E.e = e;
  141.         E.n = n;
  142.         E.t = t;
  143.  
  144.         if (e.equals(zero) || n.equals(zero)) {
  145.             return getZeroIndexElement(E);
  146.         }
  147.  
  148.         E.x = getX(e, t);
  149.  
  150.         /* define a(x) = ((x^2 + e) / (2*n)) // 1 */
  151.         E.a = (E.x.pow(2)).add(e).divide(two.multiply(E.n));
  152.  
  153.         /* define define d(x) = ((x^2 + e) / (2*n) + x) // 1 */
  154.         E.d = E.a.add(E.x);
  155.         E.f = two.multiply(E.d).add(one).subtract(E.e);
  156.         E._f = E.f.negate();
  157.         E.b = E.a.add(two.multiply(E.x)).add(two.multiply(E.n));
  158.         E.c = E.a.multiply(E.b);
  159.         E.i = E.d.add(E.n);
  160.         E.j = E.x.add(E.n);
  161.  
  162.         return E;
  163.     }
  164.  
  165.     /** Get closest element with d value below input d in (e, n)
  166.      * The element above it can be calculated to give the two elements whose d values d is between */
  167.     public VQCElement getElementBelowRoot(BigInteger e, BigInteger n, BigInteger d) {
  168.         BigInteger c = d.multiply(d).add(e);
  169.  
  170.         BigInteger f = (d.add(n)).pow(2).subtract(c);
  171.  
  172.         BigInteger xpn = sqrt(f);
  173.         BigInteger x = xpn.subtract(n);
  174.  
  175.         if (!(isEven(e) == isEven(x))) {
  176.             x = x.subtract(one);
  177.         }
  178.  
  179.         return getElement(e, n, getT(e, x));
  180.     }
  181.  
  182.     public VQCElement getEntryElement(BigInteger c) {
  183.         BigInteger d = sqrt(c);
  184.         BigInteger e = c.subtract(d.multiply(d));
  185.         BigInteger N = c.add(one).divide(two).subtract(d);
  186.         BigInteger X = d.subtract(one);
  187.         BigInteger T = getT(e, X);
  188.  
  189.         return getElement(e, N, T);
  190.     }
  191.  
  192.     public VQCElement getFactorizationElement(VQCElement c1, BigInteger n) {
  193.         VQCElement ab = c1.clone();
  194.  
  195.         ab.n = n;
  196.         ab.i = ab.d.add(ab.n);
  197.  
  198.         BigInteger i2 = ab.i.pow(2);
  199.         BigInteger j2 = i2.subtract(ab.c);
  200.  
  201.         ab.j = sqrt(j2);
  202.         ab.x = ab.j.subtract(n);
  203.         ab.a = ab.d.subtract(ab.x);
  204.         ab.b = ab.c.divide(ab.a);
  205.         ab.t = getT(ab.e, ab.x);
  206.  
  207.         return ab;
  208.     }
  209.  
  210.     /* Calculate the last row where d[D] = d */
  211.  
  212.     public BigInteger getT(BigInteger e, BigInteger x) {
  213.         BigInteger addend = one;
  214.  
  215.         if (isEven(e)) {
  216.             addend = two;
  217.         }
  218.  
  219.         return x.add(addend).divide(two);
  220.     }
  221.  
  222.     public BigInteger getX(BigInteger e, BigInteger t) {
  223.         BigInteger subtrahend = one;
  224.  
  225.         if (isEven(e)) {
  226.             subtrahend = two;
  227.         }
  228.  
  229.         return two.multiply(t).subtract(subtrahend);
  230.     }
  231.  
  232.     public VQCElement getZeroIndexElement(VQCElement E) {
  233.         E.x  = zero;
  234.         E.a  = E.t;
  235.         E.d  = E.a;
  236.         E.b  = E.a;
  237.         E.c  = E.a.multiply(E.b);
  238.         E.f  = two.multiply(E.d).add(one).subtract(E.e);
  239.         E._f = E.f.negate();
  240.         E.i  = E.d;
  241.         E.j  = zero;
  242.  
  243.         return E;
  244.     }
  245. }
  246.  
  247. class VQCElement {
  248.     public BigInteger a;
  249.     public BigInteger b;
  250.     public BigInteger c;
  251.     public BigInteger d;
  252.     public BigInteger e;
  253.     public BigInteger f;
  254.     public BigInteger _f; //-f
  255.     public BigInteger i;
  256.     public BigInteger j;
  257.     public BigInteger n;
  258.     public BigInteger x;
  259.     public BigInteger t;
  260.  
  261.     public VQCElement clone() {
  262.         VQCElement c = new VQCElement();
  263.         c.a = a;
  264.         c.b = b;
  265.         c.c = this.c;
  266.         c.d = d;
  267.         c.e = e;
  268.         c.f = f;
  269.         c.i = i;
  270.         c.j = j;
  271.         c.n = n;
  272.         c.x = x;
  273.         c.t = t;
  274.  
  275.         return c;
  276.     }
  277.  
  278.     public String getCoordinates() {
  279.         return "(" + e + ", " + n + ", " + t + ")";
  280.     }
  281.  
  282.     /**
  283.      * without the coordinates
  284.      */
  285.     public String toElementString() {
  286.         return "{" + e + ":" + n + ":" + d + ":" + x + ":" + a + ":" + b + "} ";
  287.     }
  288.  
  289.     public String toString() {
  290.         return toElementString() + getCoordinates();
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment