Guest User

Point.java

a guest
Jul 8th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1.     public Point copy(){
  2.         Point point = new Point(this.x,this.y,this.a,this.p);
  3.         return point;
  4.     }
  5.  
  6.     public Point inverse(){
  7.         Point point = new Point(this.x,this.y.negate().mod(p),this.a,this.p);
  8.         return point;
  9.     }
  10.     /*Point Operation*/
  11.     //Addition (P + Q)
  12.     public Point addition(Point q){
  13.         if(this.isEqual(O)){
  14.             return q;
  15.         }else if(q.isEqual(O)){
  16.             return this.copy();
  17.         }else if(this.inverse().isEqual(q)){
  18.             return O;
  19.         }else if(this.isEqual(q)){
  20.             return this.doubling();
  21.         }else if(this.x.compareTo(q.getX()) == 0){
  22.             return O;
  23.         }else{
  24.             BigInteger m = ((this.y.subtract(q.getY())).multiply((this.x.subtract(q.getX())).modInverse(p))).mod(p);  //Calculate the slope of line
  25.             BigInteger x3 = (m.pow(2).subtract(this.x)).subtract(q.getX());
  26.             BigInteger y3 = (m.multiply(this.x.subtract(x3))).subtract(this.y);
  27.             Point rPoint = new Point(x3.mod(p),y3.mod(p),this.a,this.p);
  28.             return rPoint;
  29.         }
  30.     }
  31.  
  32.     //Subtraction (P + (-Q))
  33.     public Point substraction(Point q){
  34.         Point rPoint = new Point();
  35.         rPoint = this.addition(q.inverse());
  36.         return rPoint;
  37.     }
  38.  
  39.     //Doubling
  40.     public Point doubling(){
  41.         if(this.y.compareTo(BigInteger.ZERO) == 0){
  42.             return O;
  43.         }else{
  44.             BigInteger m = ((this.x.pow(2).multiply(BigInteger.valueOf(3))).add(this.a)).multiply((this.y.multiply(BigInteger.valueOf(2))).modInverse(p));
  45.             BigInteger x3 = (m.pow(2)).subtract(this.x.multiply(BigInteger.valueOf(2)));
  46.             BigInteger y3 = (m.multiply(this.x.subtract(x3))).subtract(this.y);
  47.             Point rPoint = new Point(x3.mod(p),y3.mod(p),this.a,this.p);
  48.             return rPoint;
  49.         }
  50.     }
  51.  
  52.     //k multiplication
  53.     public Point multiplication(BigInteger k){
  54.         Point rPoint = new Point();
  55.         if(k.compareTo(BigInteger.ZERO) == 0){
  56.             return O;
  57.         }else if(k.compareTo(BigInteger.ONE) == 0){
  58.             return this.copy();
  59.         }else if(k.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE) == 0){
  60.             rPoint = this.addition(this.multiplication(k.subtract(BigInteger.ONE)));
  61.             return rPoint;
  62.         }else{
  63.             Point temp = this.doubling();
  64.             rPoint = temp.multiplication(k.divide(BigInteger.valueOf(2)));
  65.             return rPoint;
  66.         }
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment