Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package pentagon;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Random;
  5.  
  6. public class EllipticCurve {
  7.  
  8.     public static final BigInteger TWO = BigInteger.valueOf(2);
  9.  
  10.     public static final BigInteger THREE = BigInteger.valueOf(3);
  11.  
  12.     public static final BigInteger FOUR = BigInteger.valueOf(4);
  13.  
  14.     public static final BigInteger EIGHT = BigInteger.valueOf(8);
  15.  
  16.     public static final BigInteger SIXTEEN = BigInteger.valueOf(16);
  17.  
  18.     private final BigInteger x;
  19.  
  20.     private final BigInteger y;
  21.  
  22.     private final BigInteger a;
  23.  
  24.     private final BigInteger b;
  25.  
  26.     private final BigInteger n;
  27.  
  28.     private EllipticCurve(BigInteger x, BigInteger y, BigInteger a, BigInteger b, BigInteger n) {
  29.         super();
  30.         this.x = x;
  31.         this.y = y;
  32.         this.a = a;
  33.         this.b = b;
  34.         this.n = n;
  35.     }
  36.  
  37.     public static EllipticCurve generateRandomCurve(BigInteger a, BigInteger n) {
  38.         // Random x
  39.         BigInteger x = BigInteger.probablePrime(n.bitLength(), new Random()).mod(n);
  40.         // Random y
  41.         BigInteger y = BigInteger.probablePrime(n.bitLength(), new Random()).mod(n);
  42.  
  43.         // b=y^2-x^3-a*x should fit
  44.         BigInteger b = (y.modPow(TWO, n).subtract(x.modPow(THREE, n)).subtract(a.multiply(x))).mod(n);
  45.  
  46.         // Computing -16(4a^3+27b^2)
  47.         if ((SIXTEEN.negate()
  48.                 .multiply(FOUR.multiply(a.modPow(THREE, n)).add(BigInteger.valueOf(27).multiply(b.modPow(TWO, n))))
  49.                 .mod(n).equals(BigInteger.ZERO))) {
  50.             return generateRandomCurve(a, n);
  51.  
  52.         } else {
  53.             return new EllipticCurve(x, y, a, b, n);
  54.         }
  55.     }
  56.  
  57.     public EllipticCurve add(EllipticCurve ec) {
  58.         if (ec.equals(this)) {
  59.             return doublePoint();
  60.         }
  61.         // s=(y1-y2)/(x1-x2)
  62.         BigInteger s = y.subtract(ec.getY().mod(n)).multiply(x.subtract(ec.getX()).modInverse(n)).mod(n);
  63.         // x3=s^2-x1-x2
  64.         BigInteger x3 = s.modPow(TWO, n).subtract(x).subtract(ec.getX()).mod(n);
  65.  
  66.         // y3= s(x1-x3)-y1
  67.         BigInteger y3 = s.multiply(x.subtract(x3)).mod(n).subtract(y).mod(n);
  68.         return new EllipticCurve(x3, y3, a, b, n);
  69.     }
  70.  
  71.     public EllipticCurve doublePoint() {
  72.         // s=(3x^2+a)/2y
  73.         BigInteger s = THREE.multiply(x.modPow(TWO, n)).mod(n).add(a).multiply(TWO.multiply(y).modInverse(n)).mod(n);
  74.         // x3=s^2-x1-x1
  75.         BigInteger x3 = s.modPow(TWO, n).subtract(x).subtract(x).mod(n);
  76.  
  77.         // y3= s(x1-x3)-y1
  78.         BigInteger y3 = s.multiply(x.subtract(x3)).mod(n).subtract(y).mod(n);
  79.         return new EllipticCurve(x3, y3, a, b, n);
  80.     }
  81.  
  82.     public BigInteger getX() {
  83.         return x;
  84.     }
  85.  
  86.     public BigInteger getY() {
  87.         return y;
  88.     }
  89.  
  90.     public BigInteger getA() {
  91.         return a;
  92.     }
  93.  
  94.     public BigInteger getB() {
  95.         return b;
  96.     }
  97.  
  98.     public BigInteger getN() {
  99.         return n;
  100.     }
  101.  
  102.     @Override
  103.     public int hashCode() {
  104.         final int prime = 31;
  105.         int result = 1;
  106.         result = prime * result + ((a == null) ? 0 : a.hashCode());
  107.         result = prime * result + ((b == null) ? 0 : b.hashCode());
  108.         result = prime * result + ((n == null) ? 0 : n.hashCode());
  109.         result = prime * result + ((x == null) ? 0 : x.hashCode());
  110.         result = prime * result + ((y == null) ? 0 : y.hashCode());
  111.         return result;
  112.     }
  113.  
  114.     @Override
  115.     public boolean equals(Object obj) {
  116.         if (this == obj)
  117.             return true;
  118.         if (obj == null)
  119.             return false;
  120.         if (getClass() != obj.getClass())
  121.             return false;
  122.         EllipticCurve other = (EllipticCurve) obj;
  123.         if (a == null) {
  124.             if (other.a != null)
  125.                 return false;
  126.         } else if (!a.equals(other.a))
  127.             return false;
  128.         if (b == null) {
  129.             if (other.b != null)
  130.                 return false;
  131.         } else if (!b.equals(other.b))
  132.             return false;
  133.         if (n == null) {
  134.             if (other.n != null)
  135.                 return false;
  136.         } else if (!n.equals(other.n))
  137.             return false;
  138.         if (x == null) {
  139.             if (other.x != null)
  140.                 return false;
  141.         } else if (!x.equals(other.x))
  142.             return false;
  143.         if (y == null) {
  144.             if (other.y != null)
  145.                 return false;
  146.         } else if (!y.equals(other.y))
  147.             return false;
  148.         return true;
  149.     }
  150.  
  151.     public static void main(String[] args) {
  152.  
  153.         // Random module
  154.         BigInteger n = BigInteger.probablePrime(32, new Random());
  155.         // Random a
  156.         BigInteger a = BigInteger.probablePrime(n.bitLength() - 1, new Random());
  157.  
  158.         EllipticCurve c1 = EllipticCurve.generateRandomCurve(a, n);
  159.  
  160.         // c1+c1+c1
  161.         EllipticCurve result1=c1.add(c1).add(c1);
  162.        
  163.         // c1+(c1+c1)
  164.         EllipticCurve result2=c1.add(c1.add(c1).add(c1));
  165.         System.out.println(result1.equals(result2));
  166.     }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement