# elliptic-lite gem - elliptic curve cryptography from scratch / zero - start with finite fields, add elliptic curve points and point addition and scalar multiplications, add the elliptic curve digital signature algorithm (ECDSA) using the secp256k1 curve /

Hello,

I've put together elliptic-lite [1] - a little gem for programming
elliptic curve cryptography from scratch / zero.
and scalar multiplications,
add the elliptic curve digital signature algorithm (ECDSA) using the
secp256k1 curve / group
to sign and verify messages and more.

Happy secure programming. Cheers. Prost.

`F₁₃ = [0,1,2,3,4,5,6,7,8,9,10,11,12]` where the mod(ulus) is always
a prime number - and the prime number is 13 in this case:

``````require 'elliptic-lite'

class F₁₃ < FiniteField::Element
def self.prime() 13; end
end

F₁₃.prime             #=> 13

F₁₃.include?( 0 )     #=> true
F₁₃.include?( 12 )    #=> true
F₁₃.include?( 13 )    #=> false
``````

Let's try addition, subtraction, multiplication, exponentiation
(power), and division
with finite fields

``````F₁₃.add( 7, 12 )   #=> 6
F₁₃.sub( 7, 12 )   #=> 8
F₁₃.mul( 3, 12 )   #=> 10
F₁₃.pow( 3, 3 )    #=> 1
``````

Let's try a finite field (mod 19):

``````F₁₉ = FiniteField.new(19)

F₁₉.div( 7, 5 )    #=> 9
``````

And optional in a more object-oriented way with

``````a = F₁₃[7]
b = F₁₃[12]
c = F₁₃[6]
a+b == c       #=> true

c = F₁₃[8]
a-b == c       #=> true

a = F₁₃[3]
b = F₁₃[12]
c = F₁₃[10]
a*b == c       #=> true

a = F₁₃[3]
b = F₁₃[1]
a**3  == b     #=> true
a*a*a == b     #=> true
a*a*a == a**3  #=> true

a = F₁₉[2]
b = F₁₉[7]
c = F₁₉[3]
a/b == c       #=> true

# -or-
F₁₃[7] + F₁₃[12] == F₁₃[6]
F₁₃[7] - F₁₃[12] == F₁₃[8]
F₁₃[3] * F₁₃[12] == F₁₃[10]
F₁₃[3] ** 3               == F₁₃[1]
F₁₃[3] * F₁₃[3] * F₁₃[3]  == F₁₃[1]
F₁₃[3] ** 3               == F₁₃[3] * F₁₃[3] * F₁₃[3]

F₁₉[2] / F₁₉[7] == F₁₉[3]
``````

Elliptic Curves & Elliptic Curve Points (Over Integer Numbers)

Let's define an elliptic curve - `y³ = x² + ax + b` where a is 5 and b is 7:

``````CURVE_5_7 = Curve.new( a: 5, b: 7 )
``````

And let's define a point class - a point being a pair of
x/y-coordinates - for the elliptic curve `y³ = x² + 5x + 7` (with
`a=5` and `b=7`):

``````class Point_5_7 < Point
def self.curve() CURVE_5_7; end
end

p1 = Point_5_7.new( -1, -1 )   # point with x/y coords: -1/-1
p2 = Point_5_7.new( -1, -2 )   # raise ArgumentError!! point NOT on curve

Point_5_7.on_curve?( -1, -1 )  #=> true
Point_5_7.on_curve?( -1, -2 )  #=> false

#-or-
p1 = Point_5_7[ -1, -1 ]
p2 = Point_5_7[ -1, -2 ]

# and the infinity point
inf = Point_5_7[ :infinity ]
inf.infinity?                 #=> true
``````

Let's try point addition on the `y³ = x² + 5x + 7` elliptic curve
(with `a=5` and `b=7`):

``````p1  = Point_5_7[-1, -1]
p2  = Point_5_7[-1,  1]
inf = Point_5_7[ :infinity ]

p1  + inf     #=> Point_5_7[-1,-1]
inf + p2      #=> Point_5_7[-1,1]
p1  + p2      #=> Point_5_7[:infinity]

p1 = Point_5_7[ 2, 5]
p2 = Point_5_7[-1,-1]
p1 + p2       #=> Point_5_7[3,-7]

p1 = Point_5_7[-1,-1]
p1 + p1       #=> Point_5_7[18,77]
``````

and so on and so forth.