Created
March 1, 2012 14:14
-
-
Save siddMahen/1950081 to your computer and use it in GitHub Desktop.
ECC primitives for Julia
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
# Eliptic Curve Cryptography w/ Julia! | |
abstract EC | |
## The point O found at every horizontal line | |
type ECInf <: EC | |
end | |
## Points on the curve | |
type ECPoint <: EC | |
x :: Int | |
y :: Int | |
end | |
# Extended Euclidean Aglorithm | |
function extended_gcd(a, b) | |
if b == 0 | |
return (1, 0) | |
else | |
q = div(a, b) | |
r = a - b * q | |
s, t = extended_gcd(b, r) | |
return (t, s - (q * t)) | |
end | |
end | |
# Multiplicative Inverses over the Field Fp | |
function mod_inv(a :: Int, b :: Int, m :: Int) | |
inver, dis = extended_gcd(b, m); | |
x = (a - m*dis*a)/b | |
return int(x % m) | |
end | |
## Eliptic Curve Addition over the Field Fp ## | |
addMod(a :: ECPoint, b :: ECInf, c :: Int) = a | |
addMod(a :: ECInf, b :: ECPoint, c :: Int) = b | |
addMod(a :: ECPoint, b :: ECPoint, c :: Int) = begin | |
x1 = a.x | |
x2 = b.x | |
y1 = a.y | |
y2 = b.y | |
if x1 == x2 && y1 == -y2 | |
return ECInf() | |
end | |
if x1 == x2 && y1 == y2 | |
lambda = mod_inv(((3*(x1^2)) + A), 2*y1, c) | |
else | |
lambda = mod_inv((y2 - y1), (x2 - x1), c) | |
end | |
x3 = ((lambda)^2 - x1 - x2) % c | |
y3 = (lambda*(x1 - x3) - y1) % c | |
return ECPoint(x3, y3) | |
end | |
# the returned point R satisfied R = nP | |
double_add(p :: ECPoint, n :: Int, Fp :: Int) = begin | |
q = p | |
r = ECInf() | |
while n > 0 | |
if n % 2 == 1 | |
r = addMod(r, q, Fp) | |
end | |
q = addMod(q, q, Fp) | |
n = fld(n, 2) | |
end | |
return r | |
end | |
# Perform the computation and return R | |
A = 14 | |
P = ECPoint(6, 730) | |
Fp = 3623 | |
n = 947 | |
R = double_add(P, n, Fp); | |
println(R); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment