Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Point copy(){
- Point point = new Point(this.x,this.y,this.a,this.p);
- return point;
- }
- public Point inverse(){
- Point point = new Point(this.x,this.y.negate().mod(p),this.a,this.p);
- return point;
- }
- /*Point Operation*/
- //Addition (P + Q)
- public Point addition(Point q){
- if(this.isEqual(O)){
- return q;
- }else if(q.isEqual(O)){
- return this.copy();
- }else if(this.inverse().isEqual(q)){
- return O;
- }else if(this.isEqual(q)){
- return this.doubling();
- }else if(this.x.compareTo(q.getX()) == 0){
- return O;
- }else{
- BigInteger m = ((this.y.subtract(q.getY())).multiply((this.x.subtract(q.getX())).modInverse(p))).mod(p); //Calculate the slope of line
- BigInteger x3 = (m.pow(2).subtract(this.x)).subtract(q.getX());
- BigInteger y3 = (m.multiply(this.x.subtract(x3))).subtract(this.y);
- Point rPoint = new Point(x3.mod(p),y3.mod(p),this.a,this.p);
- return rPoint;
- }
- }
- //Subtraction (P + (-Q))
- public Point substraction(Point q){
- Point rPoint = new Point();
- rPoint = this.addition(q.inverse());
- return rPoint;
- }
- //Doubling
- public Point doubling(){
- if(this.y.compareTo(BigInteger.ZERO) == 0){
- return O;
- }else{
- BigInteger m = ((this.x.pow(2).multiply(BigInteger.valueOf(3))).add(this.a)).multiply((this.y.multiply(BigInteger.valueOf(2))).modInverse(p));
- BigInteger x3 = (m.pow(2)).subtract(this.x.multiply(BigInteger.valueOf(2)));
- BigInteger y3 = (m.multiply(this.x.subtract(x3))).subtract(this.y);
- Point rPoint = new Point(x3.mod(p),y3.mod(p),this.a,this.p);
- return rPoint;
- }
- }
- //k multiplication
- public Point multiplication(BigInteger k){
- Point rPoint = new Point();
- if(k.compareTo(BigInteger.ZERO) == 0){
- return O;
- }else if(k.compareTo(BigInteger.ONE) == 0){
- return this.copy();
- }else if(k.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE) == 0){
- rPoint = this.addition(this.multiplication(k.subtract(BigInteger.ONE)));
- return rPoint;
- }else{
- Point temp = this.doubling();
- rPoint = temp.multiplication(k.divide(BigInteger.valueOf(2)));
- return rPoint;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment