Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pentagon;
- import java.math.BigInteger;
- import java.util.Random;
- public class EllipticCurve {
- public static final BigInteger TWO = BigInteger.valueOf(2);
- public static final BigInteger THREE = BigInteger.valueOf(3);
- public static final BigInteger FOUR = BigInteger.valueOf(4);
- public static final BigInteger EIGHT = BigInteger.valueOf(8);
- public static final BigInteger SIXTEEN = BigInteger.valueOf(16);
- private final BigInteger x;
- private final BigInteger y;
- private final BigInteger a;
- private final BigInteger b;
- private final BigInteger n;
- private EllipticCurve(BigInteger x, BigInteger y, BigInteger a, BigInteger b, BigInteger n) {
- super();
- this.x = x;
- this.y = y;
- this.a = a;
- this.b = b;
- this.n = n;
- }
- public static EllipticCurve generateRandomCurve(BigInteger a, BigInteger n) {
- // Random x
- BigInteger x = BigInteger.probablePrime(n.bitLength(), new Random()).mod(n);
- // Random y
- BigInteger y = BigInteger.probablePrime(n.bitLength(), new Random()).mod(n);
- // b=y^2-x^3-a*x should fit
- BigInteger b = (y.modPow(TWO, n).subtract(x.modPow(THREE, n)).subtract(a.multiply(x))).mod(n);
- // Computing -16(4a^3+27b^2)
- if ((SIXTEEN.negate()
- .multiply(FOUR.multiply(a.modPow(THREE, n)).add(BigInteger.valueOf(27).multiply(b.modPow(TWO, n))))
- .mod(n).equals(BigInteger.ZERO))) {
- return generateRandomCurve(a, n);
- } else {
- return new EllipticCurve(x, y, a, b, n);
- }
- }
- public EllipticCurve add(EllipticCurve ec) {
- if (ec.equals(this)) {
- return doublePoint();
- }
- // s=(y1-y2)/(x1-x2)
- BigInteger s = y.subtract(ec.getY().mod(n)).multiply(x.subtract(ec.getX()).modInverse(n)).mod(n);
- // x3=s^2-x1-x2
- BigInteger x3 = s.modPow(TWO, n).subtract(x).subtract(ec.getX()).mod(n);
- // y3= s(x1-x3)-y1
- BigInteger y3 = s.multiply(x.subtract(x3)).mod(n).subtract(y).mod(n);
- return new EllipticCurve(x3, y3, a, b, n);
- }
- public EllipticCurve doublePoint() {
- // s=(3x^2+a)/2y
- BigInteger s = THREE.multiply(x.modPow(TWO, n)).mod(n).add(a).multiply(TWO.multiply(y).modInverse(n)).mod(n);
- // x3=s^2-x1-x1
- BigInteger x3 = s.modPow(TWO, n).subtract(x).subtract(x).mod(n);
- // y3= s(x1-x3)-y1
- BigInteger y3 = s.multiply(x.subtract(x3)).mod(n).subtract(y).mod(n);
- return new EllipticCurve(x3, y3, a, b, n);
- }
- public BigInteger getX() {
- return x;
- }
- public BigInteger getY() {
- return y;
- }
- public BigInteger getA() {
- return a;
- }
- public BigInteger getB() {
- return b;
- }
- public BigInteger getN() {
- return n;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((a == null) ? 0 : a.hashCode());
- result = prime * result + ((b == null) ? 0 : b.hashCode());
- result = prime * result + ((n == null) ? 0 : n.hashCode());
- result = prime * result + ((x == null) ? 0 : x.hashCode());
- result = prime * result + ((y == null) ? 0 : y.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- EllipticCurve other = (EllipticCurve) obj;
- if (a == null) {
- if (other.a != null)
- return false;
- } else if (!a.equals(other.a))
- return false;
- if (b == null) {
- if (other.b != null)
- return false;
- } else if (!b.equals(other.b))
- return false;
- if (n == null) {
- if (other.n != null)
- return false;
- } else if (!n.equals(other.n))
- return false;
- if (x == null) {
- if (other.x != null)
- return false;
- } else if (!x.equals(other.x))
- return false;
- if (y == null) {
- if (other.y != null)
- return false;
- } else if (!y.equals(other.y))
- return false;
- return true;
- }
- public static void main(String[] args) {
- // Random module
- BigInteger n = BigInteger.probablePrime(32, new Random());
- // Random a
- BigInteger a = BigInteger.probablePrime(n.bitLength() - 1, new Random());
- EllipticCurve c1 = EllipticCurve.generateRandomCurve(a, n);
- // c1+c1+c1
- EllipticCurve result1=c1.add(c1).add(c1);
- // c1+(c1+c1)
- EllipticCurve result2=c1.add(c1.add(c1).add(c1));
- System.out.println(result1.equals(result2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement