isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Elliptic Curve Addition and multiplication #236

Closed DonaldTsang closed 7 years ago

DonaldTsang commented 7 years ago

Possible shortcut in https://codegolf.stackexchange.com/a/76113

DonaldTsang commented 7 years ago

Also an example:

def ed_add(x,y,d,p):
    tmp = d*x[0]*y[0]*x[1]*y[1]%p
    return [(x[0]*y[1]+x[1]*y[0])*inv(1+tmp,p)%p,
        (x[1]*y[1]-x[0]*y[0])*inv(1-tmp,p)%p]
def ed_doub(x,d,p): return ed_add(x,x,d,p)
def ed_inverse(x,p): return [-x[0]%p,x[1]]

def mont_add(x,y,a,p):
    tmp, tmp2 = (y[1]-x[1])*inv(y[0]-x[0],p)%p, (x[0]+y[0]+a)%p
    return [(tmp**2-tmp2)%p, ((x[0]+tmp2)*tmp-tmp**3-x[1])%p]
def mont_doub(x,a,p):
    tmp, tmp2 = (3*x[0]*x[0]+2*a*x[0]+1)*inv(2*x[1],p)%p, (x[0]+x[0]+a)%p
    return [(tmp**2-tmp2)%p, ((x[0]+tmp2)*tmp-tmp**3-x[1])%p]
def mont_inverse(x,p): return [-x[0]%p,x[1]]

Defaults: https://eprint.iacr.org/2013/647.pdf

isaacg1 commented 7 years ago

This is rare/obscure enough that I'm not adding it.