Bitcoin Forum
May 21, 2024, 06:22:09 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How i can subtract a point on an elliptic curve?  (Read 127 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
January 09, 2022, 05:34:32 PM
Last edit: January 09, 2022, 06:11:15 PM by Sanka555
 #1

are there any Java libraries that allow you to subtract a point on an elliptic curve

I found python, but maybe there is a ready-made Java library so as not to rebuild the bike?)
Thank you very much in advance

Quote
# -*- coding: utf-8 -*-

def OnCurve(x,y): # Check if the point is on the curve
    A = (y*y)%P
    B = (x*x*x)%P
    C = False
    if A == (B + 7):
        C = True
    return C

def ECadd(xp,yp,xq,yq): # EC point addition
    m = ((yq-yp) * modinv(xq-xp,P))%P
    xr = (m*m-xp-xq)%P
    yr = (m*(xp-xr)-yp)%P
    return (xr,yr)

def legendre_symbol(a,p):
    ls = pow(a, (p - 1) / 2, p)
    return -1 if ls == p - 1 else ls

def modsqrt(a,p):
    if legendre_symbol(a, p) != 1:
        return 0
    elif a == 0:
        return 0
    elif p == 2:
        return p
    elif p % 4 == 3:
        return pow(a, (p + 1) / 4, p)
    s = p - 1
    e = 0
    while s % 2 == 0:
        s /= 2
        e += 1
    n = 2
    while legendre_symbol(n, p) != -1:
        n += 1
    x = pow(a, (s + 1) / 2, p)
    b = pow(a, s, p)
    g = pow(n, s, p)
    r = e
    while True:
        t = b
        m = 0
        for m in xrange(r):
            if t == 1:
                break
            t = pow(t, 2, p)
        if m == 0:
            return x
        gs = pow(g, 2 ** (r - m - 1), p)
        g = (gs * gs) % p
        x = (x * gs) % p
        b = (b * g) % p
        r = m

def modinv(a,n): # Extended Euclidean Algorithm in elliptic curves
    lm, hm = 1,0
    low, high = a%n,n
    while low > 1:
        ratio = high/low
        nm = hm - lm * ratio
        new = high - low * ratio
        hm = lm
        high = low
        lm = nm
        low = new
    return lm % n

def ECadd(xp,yp,xq,yq): # EC point addition
    m = ((yq-yp) * modinv(xq-xp,P))%P
    xr = (m*m-xp-xq)%P
    yr = (m*(xp-xr)-yp)%P
    return (xr,yr)

def ECsub(xp,yp,xq,yq): # EC point subtraction
    X = (((yp+yq)*modinv(xq-xp,P))**2-xp-xq)%P
    A = (xp + X + xq)%P
    B = modsqrt(A,P)
    B1 = P - B
    Y = yq - (xq - X) * B
    X = X % P
    Y = Y % P
    if not OnCurve(X,Y):
        Y = yq - (xq - X) * B1
    Y = Y % P
    return X,Y

P = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1

Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424

print Gx,Gy

Ax,Ay = ECadd(Gx,Gy,Gx,Gy)
print Ax,Ay

Bx,By = ECsub(Ax,Ay,Gx,Gy)
print Bx,By
This code is from here.

It works perfect in Python 2.7.13.
MixMAx123
Full Member
***
Offline Offline

Activity: 161
Merit: 168


View Profile
January 09, 2022, 06:28:44 PM
Merited by hugeblack (4), vapourminer (2), ABCbits (2)
 #2

Yes:
https://github.com/MrMaxweII/Secp256k1

Code:
import java.math.BigInteger;
import ECDSA.Secp256k1;

public class Test
{
public static void main(String[] args)
{
BigInteger[] p1 = new BigInteger[2];
BigInteger[] p2 = new BigInteger[2];
p1[0] = new BigInteger("af7ea2a1270f6630251eed5a3e2dd036f97c340b0a44589a8c4da9bb1386716",16);
p1[1] = new BigInteger("5ed9fd31f92fec4b511d1667a6174c851bf8dc536538aeb8c1055b8be7b2add7",16);
p2[0] = new BigInteger("c57e3b2c8edb3032a634700069220a4be1535668978844d271349298310dc2a1",16);
p2[1] = new BigInteger("a81df44f0d3cea24b7b021b0708a9fdcb1fce6e4e5c98348efe84624364e37d6",16);
BigInteger[] p3 = Secp256k1.subtraktion(p1, p2);
System.out.println(p3[0].toString(16));
System.out.println(p3[1].toString(16));
}
}

Output:
Code:
7eb33c2cdaa4868b6187b467d09e35e7a33db8ab6c74f1fc54ba22cb8ea0834d
e58ea47bb4be30c6a3696e8d74b82187e0baa5649b2a3303e30a900c855a6940
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
January 09, 2022, 07:35:29 PM
 #3

Thank you very much this is exactly what I need  Smiley
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!