Created
September 22, 2018 20:16
-
-
Save SaifRehman/94e1d09c1426cc08b971604e331623df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def modInverse (var,n) | |
lm = 1 | |
hm = 0 | |
low = var % n | |
high = n | |
while low > 1 do | |
ratio = high/low | |
nm, enew = hm-lm*ratio, high-low*ratio | |
lm, low, hm, high = nm, enew, lm, low | |
end | |
return nm % n | |
end | |
def ECadd(a,b) | |
lamAdd = ((b[1]-a[1]) * modInverse(b[0]-a[0],$provePrimeNumber)) % $provePrimeNumber | |
x = (lamAdd*lamAdd-a[0]-b[0]) % $provePrimeNumber | |
y = (lamAdd*(a[0]-x)-a[1]) % $provePrimeNumber | |
return [x,y] | |
end | |
def ECdouble(a) | |
lam = ((3*a[0]*a[0] * modInverse((2*a[1]),$provePrimeNumber)) % $provePrimeNumber | |
x = (lam*lam-2*a[0]) % $provePrimeNumber | |
y = (lam*(a[0]-x)-a[1]) % $provePrimeNumber | |
return [x,y] | |
end | |
def EccMultiply(genPoint,scalarHex) | |
scalarBin = dectobin(scalarHex) | |
q=genPoint | |
for i in 1..scalarBin.length-1 | |
q=ECdouble(q) | |
puts "DUB", q[0] | |
if scalarBin[i] == '1' | |
q=ECadd(q,genPoint) | |
puts "ADD", q[0] | |
end | |
end | |
return [q] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment