Added eel and eapml in EWF libraries.
Removed them from gitmodule
This commit is contained in:
14
library/crypto/eel/ec/ec_constants.e
Normal file
14
library/crypto/eel/ec/ec_constants.e
Normal file
@@ -0,0 +1,14 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "The natural progress of things is for liberty to yield and government to gain ground. - Thomas Jefferson"
|
||||
|
||||
deferred class
|
||||
EC_CONSTANTS
|
||||
|
||||
inherit
|
||||
CONSTANTS
|
||||
|
||||
end
|
||||
23
library/crypto/eel/ec/ec_curve.e
Normal file
23
library/crypto/eel/ec/ec_curve.e
Normal file
@@ -0,0 +1,23 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "None are more hopelessly enslaved than those who falsely believe they are free. - Goethe"
|
||||
|
||||
deferred class
|
||||
EC_CURVE
|
||||
|
||||
inherit
|
||||
DEBUG_OUTPUT
|
||||
|
||||
feature
|
||||
a: EC_FIELD_ELEMENT
|
||||
b: EC_FIELD_ELEMENT
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := "a: " + a.debug_output + "%Nb: " + b.debug_output
|
||||
end
|
||||
end
|
||||
419
library/crypto/eel/ec/ec_curve_f2m.e
Normal file
419
library/crypto/eel/ec/ec_curve_f2m.e
Normal file
@@ -0,0 +1,419 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "When the government's boot is on your throat, whether it is a left boot or a right boot is of no consequence. - Gary Lloyd"
|
||||
|
||||
class
|
||||
EC_CURVE_F2M
|
||||
|
||||
inherit
|
||||
EC_CURVE
|
||||
redefine
|
||||
is_equal,
|
||||
a,
|
||||
b
|
||||
end
|
||||
STANDARD_CURVES
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
F2M_REPRESENTATIONS
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
|
||||
create
|
||||
make,
|
||||
make_sec_t113r1,
|
||||
make_sec_t113r2,
|
||||
make_sec_t131r1,
|
||||
make_sec_t131r2,
|
||||
make_sec_t163k1,
|
||||
make_sec_t163r1,
|
||||
make_sec_t163r2,
|
||||
make_sec_t193r1,
|
||||
make_sec_t193r2,
|
||||
make_sec_t233k1,
|
||||
make_sec_t233r1,
|
||||
make_sec_t239k1,
|
||||
make_sec_t283k1,
|
||||
make_sec_t283r1,
|
||||
make_sec_t409k1,
|
||||
make_sec_t409r1,
|
||||
make_sec_t571k1,
|
||||
make_sec_t571r1,
|
||||
make_k163,
|
||||
make_k233,
|
||||
make_k283,
|
||||
make_k409,
|
||||
make_k571,
|
||||
make_b163,
|
||||
make_b233,
|
||||
make_b283,
|
||||
make_b409,
|
||||
make_b571
|
||||
|
||||
feature -- SEC curves
|
||||
make_sec_t113r1
|
||||
do
|
||||
m := sec_t113r1_m
|
||||
k1 := sec_t113r1_k1
|
||||
k2 := sec_t113r1_k2
|
||||
k3 := sec_t113r1_k3
|
||||
n := sec_t113r1_r
|
||||
create a.make (sec_t113r1_a)
|
||||
create b.make (sec_t113r1_b)
|
||||
end
|
||||
|
||||
make_sec_t113r2
|
||||
do
|
||||
m := sec_t113r2_m
|
||||
k1 := sec_t113r2_k1
|
||||
k2 := sec_t113r2_k2
|
||||
k3 := sec_t113r2_k3
|
||||
n := sec_t113r2_r
|
||||
create a.make (sec_t113r2_a)
|
||||
create b.make (sec_t113r2_b)
|
||||
end
|
||||
|
||||
make_sec_t131r1
|
||||
do
|
||||
m := sec_t131r1_m
|
||||
k1 := sec_t131r1_k1
|
||||
k2 := sec_t131r1_k2
|
||||
k3 := sec_t131r1_k3
|
||||
n := sec_t131r1_r
|
||||
create a.make (sec_t131r1_a)
|
||||
create b.make (sec_t131r1_b)
|
||||
end
|
||||
|
||||
make_sec_t131r2
|
||||
do
|
||||
m := sec_t131r2_m
|
||||
k1 := sec_t131r2_k1
|
||||
k2 := sec_t131r2_k2
|
||||
k3 := sec_t131r2_k3
|
||||
n := sec_t131r2_r
|
||||
create a.make (sec_t131r2_a)
|
||||
create b.make (sec_t131r2_b)
|
||||
end
|
||||
|
||||
make_sec_t163k1
|
||||
do
|
||||
m := sec_t163k1_m
|
||||
k1 := sec_t163k1_k1
|
||||
k2 := sec_t163k1_k2
|
||||
k3 := sec_t163k1_k3
|
||||
n := sec_t163k1_r
|
||||
create a.make (sec_t163k1_a)
|
||||
create b.make (sec_t163k1_b)
|
||||
end
|
||||
|
||||
make_sec_t163r1
|
||||
do
|
||||
m := sec_t163r1_m
|
||||
k1 := sec_t163r1_k1
|
||||
k2 := sec_t163r1_k2
|
||||
k3 := sec_t163r1_k3
|
||||
n := sec_t163r1_r
|
||||
create a.make (sec_t163r1_a)
|
||||
create b.make (sec_t163r1_b)
|
||||
end
|
||||
|
||||
make_sec_t163r2
|
||||
do
|
||||
m := sec_t163r2_m
|
||||
k1 := sec_t163r2_k1
|
||||
k2 := sec_t163r2_k2
|
||||
k3 := sec_t163r2_k3
|
||||
n := sec_t163r1_r
|
||||
create a.make (sec_t163r2_a)
|
||||
create b.make (sec_t163r2_b)
|
||||
end
|
||||
|
||||
make_sec_t193r1
|
||||
do
|
||||
m := sec_t193r1_m
|
||||
k1 := sec_t193r1_k1
|
||||
k2 := sec_t193r1_k2
|
||||
k3 := sec_t193r1_k3
|
||||
n := sec_t193r1_r
|
||||
create a.make (sec_t193r1_a)
|
||||
create b.make (sec_t193r1_b)
|
||||
end
|
||||
|
||||
make_sec_t193r2
|
||||
do
|
||||
m := sec_t193r2_m
|
||||
k1 := sec_t193r2_k1
|
||||
k2 := sec_t193r2_k2
|
||||
k3 := sec_t193r2_k3
|
||||
n := sec_t193r2_r
|
||||
create a.make (sec_t193r2_a)
|
||||
create b.make (sec_t193r2_b)
|
||||
end
|
||||
|
||||
make_sec_t233k1
|
||||
do
|
||||
m := sec_t233k1_m
|
||||
k1 := sec_t233k1_k1
|
||||
k2 := sec_t233k1_k2
|
||||
k3 := sec_t233k1_k3
|
||||
n := sec_t233k1_r
|
||||
create a.make (sec_t233k1_a)
|
||||
create b.make (sec_t233k1_b)
|
||||
end
|
||||
|
||||
make_sec_t233r1
|
||||
do
|
||||
m := sec_t233r1_m
|
||||
k1 := sec_t233r1_k1
|
||||
k2 := sec_t233r1_k2
|
||||
k3 := sec_t233r1_k3
|
||||
n := sec_t233r1_r
|
||||
create a.make (sec_t233r1_a)
|
||||
create b.make (sec_t233r1_b)
|
||||
end
|
||||
|
||||
make_sec_t239k1
|
||||
do
|
||||
m := sec_t239k1_m
|
||||
k1 := sec_t239k1_k1
|
||||
k2 := sec_t239k1_k2
|
||||
k3 := sec_t239k1_k3
|
||||
n := sec_t239k1_r
|
||||
create a.make (sec_t239k1_a)
|
||||
create b.make (sec_t239k1_b)
|
||||
end
|
||||
|
||||
make_sec_t283k1
|
||||
do
|
||||
m := sec_t283k1_m
|
||||
k1 := sec_t283k1_k1
|
||||
k2 := sec_t283k1_k2
|
||||
k3 := sec_t283k1_k3
|
||||
n := sec_t283k1_r
|
||||
create a.make (sec_t283k1_a)
|
||||
create b.make (sec_t283k1_b)
|
||||
end
|
||||
|
||||
make_sec_t283r1
|
||||
do
|
||||
m := sec_t283r1_m
|
||||
k1 := sec_t283r1_k1
|
||||
k2 := sec_t283r1_k2
|
||||
k3 := sec_t283r1_k3
|
||||
n := sec_t283r1_r
|
||||
create a.make (sec_t283r1_a)
|
||||
create b.make (sec_t283r1_b)
|
||||
end
|
||||
|
||||
make_sec_t409k1
|
||||
do
|
||||
m := sec_t409k1_m
|
||||
k1 := sec_t409k1_k1
|
||||
k2 := sec_t409k1_k2
|
||||
k3 := sec_t409k1_k3
|
||||
n := sec_t409k1_r
|
||||
create a.make (sec_t409k1_a)
|
||||
create b.make (sec_t409k1_b)
|
||||
end
|
||||
|
||||
make_sec_t409r1
|
||||
do
|
||||
m := sec_t409r1_m
|
||||
k1 := sec_t409r1_k1
|
||||
k2 := sec_t409r1_k2
|
||||
k3 := sec_t409r1_k3
|
||||
n := sec_t409r1_r
|
||||
create a.make (sec_t409r1_a)
|
||||
create b.make (sec_t409r1_b)
|
||||
end
|
||||
|
||||
make_sec_t571k1
|
||||
do
|
||||
m := sec_t571k1_m
|
||||
k1 := sec_t571k1_k1
|
||||
k2 := sec_t571k1_k2
|
||||
k3 := sec_t571k1_k3
|
||||
n := sec_t571k1_r
|
||||
create a.make (sec_t571k1_a)
|
||||
create b.make (sec_t571k1_b)
|
||||
end
|
||||
|
||||
make_sec_t571r1
|
||||
do
|
||||
m := sec_t571r1_m
|
||||
k1 := sec_t571r1_k1
|
||||
k2 := sec_t571r1_k2
|
||||
k3 := sec_t571r1_k3
|
||||
n := sec_t571r1_r
|
||||
create a.make (sec_t571r1_a)
|
||||
create b.make (sec_t571r1_b)
|
||||
end
|
||||
|
||||
feature -- FIPS curves
|
||||
make_k163
|
||||
do
|
||||
m := k163_m
|
||||
k1 := k163_k1
|
||||
k2 := k163_k2
|
||||
k3 := k163_k3
|
||||
n := k163_r
|
||||
create a.make (k163_a)
|
||||
create b.make (k163_b)
|
||||
end
|
||||
|
||||
make_k233
|
||||
do
|
||||
m := k233_m
|
||||
k1 := k233_k1
|
||||
k2 := k233_k2
|
||||
k3 := k233_k3
|
||||
n := k233_r
|
||||
create a.make (k233_a)
|
||||
create b.make (k233_b)
|
||||
end
|
||||
|
||||
make_k283
|
||||
do
|
||||
m := k283_m
|
||||
k1 := k283_k1
|
||||
k2 := k283_k2
|
||||
k3 := k283_k3
|
||||
n := k283_r
|
||||
create a.make (k283_a)
|
||||
create b.make (k283_b)
|
||||
end
|
||||
|
||||
make_k409
|
||||
do
|
||||
m := k409_m
|
||||
k1 := k409_k1
|
||||
k2 := k409_k2
|
||||
k3 := k409_k3
|
||||
n := k409_r
|
||||
create a.make (k409_a)
|
||||
create b.make (k409_b)
|
||||
end
|
||||
|
||||
make_k571
|
||||
do
|
||||
m := k571_m
|
||||
k1 := k571_k1
|
||||
k2 := k571_k2
|
||||
k3 := k571_k3
|
||||
n := k571_r
|
||||
create a.make (k571_a)
|
||||
create b.make (k571_b)
|
||||
end
|
||||
|
||||
make_b163
|
||||
do
|
||||
m := b163_m
|
||||
k1 := b163_k1
|
||||
k2 := b163_k2
|
||||
k3 := b163_k3
|
||||
n := b163_r
|
||||
create a.make (b163_a)
|
||||
create b.make (b163_b)
|
||||
end
|
||||
|
||||
make_b233
|
||||
do
|
||||
m := b233_m
|
||||
k1 := b233_k1
|
||||
k2 := b233_k2
|
||||
k3 := b233_k3
|
||||
n := b233_r
|
||||
create a.make (b233_a)
|
||||
create b.make (b233_b)
|
||||
end
|
||||
|
||||
make_b283
|
||||
do
|
||||
m := b283_m
|
||||
k1 := b283_k1
|
||||
k2 := b283_k2
|
||||
k3 := b283_k3
|
||||
n := b283_r
|
||||
create a.make (b283_a)
|
||||
create b.make (b283_b)
|
||||
end
|
||||
|
||||
make_b409
|
||||
do
|
||||
m := b409_m
|
||||
k1 := b409_k1
|
||||
k2 := b409_k2
|
||||
k3 := b409_k3
|
||||
n := b409_r
|
||||
create a.make (b409_a)
|
||||
create b.make (b409_b)
|
||||
end
|
||||
|
||||
make_b571
|
||||
do
|
||||
m := b571_m
|
||||
k1 := b571_k1
|
||||
k2 := b571_k2
|
||||
k3 := b571_k3
|
||||
n := b571_r
|
||||
create a.make (b571_a)
|
||||
create b.make (b571_b)
|
||||
end
|
||||
|
||||
make (m_new: INTEGER_32 k1_new: INTEGER_32 k2_new: INTEGER_32 k3_new: INTEGER_32 a_a: EC_FIELD_ELEMENT_F2M b_a: EC_FIELD_ELEMENT_F2M n_a: INTEGER_X)
|
||||
require
|
||||
K1_greater_Than_zero: k1_new > 0
|
||||
k2_and_k3_equal_zero: (k2_new = 0) implies (k3_new = 0)
|
||||
k2_greater_than_k1: (k2_new /= 0) implies (k2_new > k1_new)
|
||||
k3_greater_than_k2: (k3_new /= 0) implies (k3_new > k2_new)
|
||||
do
|
||||
m := m_new
|
||||
k1 := k1_new
|
||||
k2 := k2_new
|
||||
k3 := k3_new
|
||||
a := a_a
|
||||
b := b_a
|
||||
n := n_a
|
||||
end
|
||||
|
||||
feature -- F2M components
|
||||
m: INTEGER_32
|
||||
n: INTEGER_X
|
||||
k1: INTEGER_32
|
||||
k2: INTEGER_32
|
||||
k3: INTEGER_32
|
||||
|
||||
feature
|
||||
representation: INTEGER
|
||||
do
|
||||
if
|
||||
k2 = 0
|
||||
then
|
||||
result := TPB
|
||||
else
|
||||
result := PPB
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
is_equal (other: like current): BOOLEAN
|
||||
do
|
||||
Result := (m = other.m) and (k1 = other.k1) and (k2 = other.k2) and (k3 = other.k3) and a.x ~ other.a.x and b.x ~ other.b.x
|
||||
end
|
||||
|
||||
a: EC_FIELD_ELEMENT_F2M
|
||||
b: EC_FIELD_ELEMENT_F2M
|
||||
|
||||
invariant
|
||||
-- k2_smaller: k2 = 0 implies k2 < k3
|
||||
-- k2_zero: k2 = 0 implies k2 /= 0
|
||||
K1_greater_Than_zero: k1 > 0
|
||||
k2_and_k3_equal_zero: (k2 = 0) implies (k3 = 0)
|
||||
k2_greater_than_k1: (k2 /= 0) implies (k2 > k1)
|
||||
k3_greater_than_k2: (k3 /= 0) implies (k3 > k2)
|
||||
end
|
||||
230
library/crypto/eel/ec/ec_curve_fp.e
Normal file
230
library/crypto/eel/ec/ec_curve_fp.e
Normal file
@@ -0,0 +1,230 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Every decent man is ashamed of the government he lives under. - H.L. Mencken"
|
||||
|
||||
class
|
||||
EC_CURVE_FP
|
||||
|
||||
inherit
|
||||
EC_CONSTANTS
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
EC_CURVE
|
||||
redefine
|
||||
is_equal,
|
||||
a,
|
||||
b
|
||||
end
|
||||
STANDARD_CURVES
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
|
||||
create
|
||||
make_q_a_b,
|
||||
make_sec_p112r1,
|
||||
make_sec_p112r2,
|
||||
make_sec_p128r1,
|
||||
make_sec_p128r2,
|
||||
make_sec_p160k1,
|
||||
make_sec_p160r1,
|
||||
make_sec_p160r2,
|
||||
make_sec_p192k1,
|
||||
make_sec_p192r1,
|
||||
make_sec_p224k1,
|
||||
make_sec_p224r1,
|
||||
make_sec_p256k1,
|
||||
make_sec_p256r1,
|
||||
make_sec_p384r1,
|
||||
make_sec_p521r1,
|
||||
make_p192,
|
||||
make_p224,
|
||||
make_p256,
|
||||
make_p384,
|
||||
make_p521
|
||||
|
||||
create {EC_FIELD_ELEMENT_FP}
|
||||
make_zero
|
||||
|
||||
feature {EC_FIELD_ELEMENT_FP}
|
||||
make_zero
|
||||
do
|
||||
create q.default_create
|
||||
create a.make_zero
|
||||
create b.make_zero
|
||||
end
|
||||
|
||||
feature
|
||||
make_q_a_b (q_new: INTEGER_X a_a: INTEGER_X b_a: INTEGER_X)
|
||||
-- Create an EC over FP from q, a, and b
|
||||
do
|
||||
q := q_new
|
||||
create a.make_p_x (a_a)
|
||||
create b.make_p_x (b_a)
|
||||
end
|
||||
|
||||
feature -- SEC curves
|
||||
make_sec_p112r1
|
||||
do
|
||||
q := sec_p112r1_p
|
||||
create a.make_p_x (sec_p112r1_a)
|
||||
create b.make_p_x (sec_p112r1_b)
|
||||
end
|
||||
|
||||
make_sec_p112r2
|
||||
do
|
||||
q := sec_p112r2_p
|
||||
create a.make_p_x (sec_p112r2_a)
|
||||
create b.make_p_x (sec_p112r2_b)
|
||||
end
|
||||
|
||||
make_sec_p128r1
|
||||
do
|
||||
q := sec_p128r1_p
|
||||
create a.make_p_x (sec_p128r1_a)
|
||||
create b.make_p_x (sec_p128r1_b)
|
||||
end
|
||||
|
||||
make_sec_p128r2
|
||||
do
|
||||
q := sec_p128r2_p
|
||||
create a.make_p_x (sec_p128r2_a)
|
||||
create b.make_p_x (sec_p128r2_b)
|
||||
end
|
||||
|
||||
make_sec_p160k1
|
||||
do
|
||||
q := sec_p160k1_p
|
||||
create a.make_p_x (sec_p160k1_a)
|
||||
create b.make_p_x (sec_p160k1_b)
|
||||
end
|
||||
|
||||
make_sec_p160r1
|
||||
do
|
||||
q := sec_p160r1_p
|
||||
create a.make_p_x (sec_p160r1_a)
|
||||
create b.make_p_x (sec_p160r1_b)
|
||||
end
|
||||
|
||||
make_sec_p160r2
|
||||
do
|
||||
q := sec_p160r2_p
|
||||
create a.make_p_x (sec_p160r2_a)
|
||||
create b.make_p_x (sec_p160r2_b)
|
||||
end
|
||||
|
||||
make_sec_p192k1
|
||||
do
|
||||
q := sec_p192k1_p
|
||||
create a.make_p_x (sec_p192k1_a)
|
||||
create b.make_p_x (sec_p192k1_b)
|
||||
end
|
||||
|
||||
make_sec_p192r1
|
||||
do
|
||||
q := sec_p192r1_p
|
||||
create a.make_p_x (sec_p192r1_a)
|
||||
create b.make_p_x (sec_p192r1_b)
|
||||
end
|
||||
|
||||
make_sec_p224k1
|
||||
do
|
||||
q := sec_p224k1_p
|
||||
create a.make_p_x (sec_p224k1_a)
|
||||
create b.make_p_x (sec_p224k1_b)
|
||||
end
|
||||
|
||||
make_sec_p224r1
|
||||
do
|
||||
q := sec_p224r1_p
|
||||
create a.make_p_x (sec_p224r1_a)
|
||||
create b.make_p_x (sec_p224r1_b)
|
||||
end
|
||||
|
||||
make_sec_p256k1
|
||||
do
|
||||
q := sec_p256k1_p
|
||||
create a.make_p_x (sec_p256k1_a)
|
||||
create b.make_p_x (sec_p256k1_b)
|
||||
end
|
||||
|
||||
make_sec_p256r1
|
||||
do
|
||||
q := sec_p256r1_p
|
||||
create a.make_p_x (sec_p256r1_a)
|
||||
create b.make_p_x (sec_p256r1_b)
|
||||
end
|
||||
|
||||
make_sec_p384r1
|
||||
do
|
||||
q := sec_p384r1_p
|
||||
create a.make_p_x (sec_p384r1_a)
|
||||
create b.make_p_x (sec_p384r1_b)
|
||||
end
|
||||
|
||||
make_sec_p521r1
|
||||
do
|
||||
q := sec_p521r1_p
|
||||
create a.make_p_x (sec_p521r1_a)
|
||||
create b.make_p_x (sec_p521r1_b)
|
||||
end
|
||||
|
||||
feature
|
||||
make_p192
|
||||
do
|
||||
q := p192_p
|
||||
create a.make_p_x (p192_a)
|
||||
create b.make_p_x (p192_b)
|
||||
end
|
||||
|
||||
make_p224
|
||||
do
|
||||
q := p224_p
|
||||
create a.make_p_x (p224_a)
|
||||
create b.make_p_x (p224_b)
|
||||
end
|
||||
|
||||
make_p256
|
||||
do
|
||||
q := p256_p
|
||||
create a.make_p_x (p256_a)
|
||||
create b.make_p_x (p256_b)
|
||||
end
|
||||
|
||||
make_p384
|
||||
do
|
||||
q := p384_p
|
||||
create a.make_p_x (p384_a)
|
||||
create b.make_p_x (p384_b)
|
||||
end
|
||||
|
||||
make_p521
|
||||
do
|
||||
q := p521_p
|
||||
create a.make_p_x (p521_a)
|
||||
create b.make_p_x (p521_b)
|
||||
end
|
||||
|
||||
feature
|
||||
q: INTEGER_X
|
||||
a: EC_FIELD_ELEMENT_FP
|
||||
attribute
|
||||
create result.make_zero
|
||||
end
|
||||
b: EC_FIELD_ELEMENT_FP
|
||||
attribute
|
||||
create result.make_zero
|
||||
end
|
||||
|
||||
is_equal (other: like current): BOOLEAN
|
||||
-- Is current equal to other
|
||||
do
|
||||
result := q ~ other.q and a.x ~ other.a.x and b.x ~ other.b.x
|
||||
ensure then
|
||||
q /~ other.q implies not result
|
||||
end
|
||||
end
|
||||
44
library/crypto/eel/ec/ec_domain_parameters.e
Normal file
44
library/crypto/eel/ec/ec_domain_parameters.e
Normal file
@@ -0,0 +1,44 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "The urge to save humanity is almost always a false front for the urge to rule. - H.L. Mencken"
|
||||
|
||||
deferred class
|
||||
EC_DOMAIN_PARAMETERS
|
||||
|
||||
inherit
|
||||
EC_CONSTANTS
|
||||
DEBUG_OUTPUT
|
||||
|
||||
feature
|
||||
curve: EC_CURVE
|
||||
g: EC_POINT
|
||||
n: INTEGER_X
|
||||
h: INTEGER_X
|
||||
|
||||
make_curve_g_n (curve_new: like curve g_new: like g n_new: INTEGER_X)
|
||||
-- Construct this domain with no seed and h= 1
|
||||
do
|
||||
curve := curve_new
|
||||
g := g_new
|
||||
n := n_new
|
||||
h := ONE
|
||||
end
|
||||
|
||||
make_curve_g_n_h (curve_new: like curve g_new: like g n_new: INTEGER_X h_new: INTEGER_X)
|
||||
-- construct this domain with no seed
|
||||
do
|
||||
curve := curve_new
|
||||
g := g_new
|
||||
n := n_new
|
||||
h := h_new
|
||||
end
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := "Curve: " + curve.debug_output + "%Ng: " + g.debug_output + "%Nn: " + n.out_hex + "%Nh: " + h.out_hex
|
||||
end
|
||||
end
|
||||
44
library/crypto/eel/ec/ec_domain_parameters.e.orig
Normal file
44
library/crypto/eel/ec/ec_domain_parameters.e.orig
Normal file
@@ -0,0 +1,44 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "The urge to save humanity is almost always a false front for the urge to rule. - H.L. Mencken"
|
||||
|
||||
deferred class
|
||||
EC_DOMAIN_PARAMETERS
|
||||
|
||||
inherit
|
||||
EC_CONSTANTS
|
||||
DEBUG_OUTPUT
|
||||
|
||||
feature
|
||||
curve: EC_CURVE
|
||||
g: EC_POINT
|
||||
n: INTEGER_X
|
||||
h: INTEGER_X
|
||||
|
||||
make_curve_g_n (curve_new: like curve; g_new: like g; n_new: INTEGER_X) is
|
||||
-- Construct this domain with no seed and h= 1
|
||||
do
|
||||
curve := curve_new
|
||||
g := g_new
|
||||
n := n_new
|
||||
h := ONE
|
||||
end
|
||||
|
||||
make_curve_g_n_h (curve_new: like curve g_new: like g n_new: INTEGER_X h_new: INTEGER_X) is
|
||||
-- construct this domain with no seed
|
||||
do
|
||||
curve := curve_new
|
||||
g := g_new
|
||||
n := n_new
|
||||
h := h_new
|
||||
end
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := "Curve: " + curve.debug_output + "%Ng: " + g.debug_output + "%Nn: " + n.out_hex + "%Nh: " + h.out_hex
|
||||
end
|
||||
end
|
||||
279
library/crypto/eel/ec/ec_domain_parameters_f2m.e
Normal file
279
library/crypto/eel/ec/ec_domain_parameters_f2m.e
Normal file
@@ -0,0 +1,279 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Where morality is present, laws are unnecessary. Without morality, laws are unenforceable. - Anonymous"
|
||||
|
||||
class
|
||||
EC_DOMAIN_PARAMETERS_F2M
|
||||
|
||||
inherit
|
||||
EC_DOMAIN_PARAMETERS
|
||||
redefine
|
||||
curve,
|
||||
g
|
||||
end
|
||||
STANDARD_CURVES
|
||||
|
||||
create
|
||||
make_curve_g_n,
|
||||
make_curve_g_n_h,
|
||||
make_sec_t113r1,
|
||||
make_sec_t113r2,
|
||||
make_sec_t131r1,
|
||||
make_sec_t131r2,
|
||||
make_sec_t163k1,
|
||||
make_sec_t163r1,
|
||||
make_sec_t163r2,
|
||||
make_sec_t193r1,
|
||||
make_sec_t193r2,
|
||||
make_sec_t233k1,
|
||||
make_sec_t233r1,
|
||||
make_sec_t239k1,
|
||||
make_sec_t283k1,
|
||||
make_sec_t283r1,
|
||||
make_sec_t409k1,
|
||||
make_sec_t409r1,
|
||||
make_sec_t571k1,
|
||||
make_sec_t571r1,
|
||||
make_k163,
|
||||
make_k233,
|
||||
make_k283,
|
||||
make_k409,
|
||||
make_k571,
|
||||
make_b163,
|
||||
make_b233,
|
||||
make_b283,
|
||||
make_b409,
|
||||
make_b571
|
||||
|
||||
feature --SEC recommended polynomial curves
|
||||
make_sec_t113r1
|
||||
do
|
||||
create curve.make_sec_t113r1
|
||||
create g.make_sec_t113r1
|
||||
n := sec_t113r1_r
|
||||
h := sec_t113r1_h
|
||||
end
|
||||
|
||||
make_sec_t113r2
|
||||
do
|
||||
create curve.make_sec_t113r2
|
||||
create g.make_sec_t113r2
|
||||
n := sec_t113r2_r
|
||||
h := sec_t113r2_h
|
||||
end
|
||||
|
||||
make_sec_t131r1
|
||||
do
|
||||
create curve.make_sec_t131r1
|
||||
create g.make_sec_t131r1
|
||||
n := sec_t131r1_r
|
||||
h := sec_t131r1_h
|
||||
end
|
||||
|
||||
make_sec_t131r2
|
||||
do
|
||||
create curve.make_sec_t131r2
|
||||
create g.make_sec_t131r2
|
||||
n := sec_t131r2_r
|
||||
h := sec_t131r2_h
|
||||
end
|
||||
|
||||
make_sec_t163k1
|
||||
do
|
||||
create curve.make_sec_t163k1
|
||||
create g.make_sec_t163k1
|
||||
n := sec_t163k1_r
|
||||
h := sec_t163k1_h
|
||||
end
|
||||
|
||||
make_sec_t163r1
|
||||
do
|
||||
create curve.make_sec_t163r1
|
||||
create g.make_sec_t163r1
|
||||
n := sec_t163r1_r
|
||||
h := sec_t163r1_h
|
||||
end
|
||||
|
||||
make_sec_t163r2
|
||||
do
|
||||
create curve.make_sec_t163r2
|
||||
create g.make_sec_t163r2
|
||||
n := sec_t163r2_r
|
||||
h := sec_t163r2_h
|
||||
end
|
||||
|
||||
make_sec_t193r1
|
||||
do
|
||||
create curve.make_sec_t193r1
|
||||
create g.make_sec_t193r1
|
||||
n := sec_t193r1_r
|
||||
h := sec_t193r1_h
|
||||
end
|
||||
|
||||
make_sec_t193r2
|
||||
do
|
||||
create curve.make_sec_t193r2
|
||||
create g.make_sec_t193r2
|
||||
n := sec_t193r2_r
|
||||
h := sec_t193r2_h
|
||||
end
|
||||
|
||||
make_sec_t233k1
|
||||
do
|
||||
create curve.make_sec_t233k1
|
||||
create g.make_sec_t233k1
|
||||
n := sec_t233k1_r
|
||||
h := sec_t233k1_h
|
||||
end
|
||||
|
||||
make_sec_t233r1
|
||||
do
|
||||
create curve.make_sec_t233r1
|
||||
create g.make_sec_t233r1
|
||||
n := sec_t233r1_r
|
||||
h := sec_t233r1_h
|
||||
end
|
||||
|
||||
make_sec_t239k1
|
||||
do
|
||||
create curve.make_sec_t239k1
|
||||
create g.make_sec_t239k1
|
||||
n := sec_t239k1_r
|
||||
h := sec_t239k1_h
|
||||
end
|
||||
|
||||
make_sec_t283k1
|
||||
do
|
||||
create curve.make_sec_t283k1
|
||||
create g.make_sec_t283k1
|
||||
n := sec_t283k1_r
|
||||
h := sec_t283k1_h
|
||||
end
|
||||
|
||||
make_sec_t283r1
|
||||
do
|
||||
create curve.make_sec_t283r1
|
||||
create g.make_sec_t283r1
|
||||
n := sec_t283r1_r
|
||||
h := sec_t283r1_h
|
||||
end
|
||||
|
||||
make_sec_t409k1
|
||||
do
|
||||
create curve.make_sec_t409k1
|
||||
create g.make_sec_t409k1
|
||||
n := sec_t409k1_r
|
||||
h := sec_t409k1_h
|
||||
end
|
||||
|
||||
make_sec_t409r1
|
||||
do
|
||||
create curve.make_sec_t409r1
|
||||
create g.make_sec_t409r1
|
||||
n := sec_t409r1_r
|
||||
h := sec_t409r1_h
|
||||
end
|
||||
|
||||
make_sec_t571k1
|
||||
do
|
||||
create curve.make_sec_t571k1
|
||||
create g.make_sec_t571k1
|
||||
n := sec_t571k1_r
|
||||
h := sec_t571k1_h
|
||||
end
|
||||
|
||||
make_sec_t571r1
|
||||
do
|
||||
create curve.make_sec_t571r1
|
||||
create g.make_sec_t571r1
|
||||
n := sec_t571r1_r
|
||||
h := sec_t571r1_h
|
||||
end
|
||||
|
||||
feature --FIPS curves
|
||||
make_k163
|
||||
do
|
||||
create curve.make_k163
|
||||
create g.make_k163
|
||||
n := k163_r
|
||||
h := k163_h
|
||||
end
|
||||
|
||||
make_k233
|
||||
do
|
||||
create curve.make_k233
|
||||
create g.make_k233
|
||||
n := k233_r
|
||||
h := k233_h
|
||||
end
|
||||
|
||||
make_k283
|
||||
do
|
||||
create curve.make_k283
|
||||
create g.make_k283
|
||||
n := k283_r
|
||||
h := k283_h
|
||||
end
|
||||
|
||||
make_k409
|
||||
do
|
||||
create curve.make_k409
|
||||
create g.make_k409
|
||||
n := k409_r
|
||||
h := k409_h
|
||||
end
|
||||
|
||||
make_k571
|
||||
do
|
||||
create curve.make_k571
|
||||
create g.make_k571
|
||||
n := k571_r
|
||||
h := k571_h
|
||||
end
|
||||
|
||||
make_b163
|
||||
do
|
||||
create curve.make_b163
|
||||
create g.make_b163
|
||||
n := b163_r
|
||||
h := b163_h
|
||||
end
|
||||
|
||||
make_b233
|
||||
do
|
||||
create curve.make_b233
|
||||
create g.make_b233
|
||||
n := b233_r
|
||||
h := b233_h
|
||||
end
|
||||
|
||||
make_b283
|
||||
do
|
||||
create curve.make_b283
|
||||
create g.make_b283
|
||||
n := b283_r
|
||||
h := b283_h
|
||||
end
|
||||
|
||||
make_b409
|
||||
do
|
||||
create curve.make_b409
|
||||
create g.make_b409
|
||||
n := b409_r
|
||||
h := b409_h
|
||||
end
|
||||
|
||||
make_b571
|
||||
do
|
||||
create curve.make_b571
|
||||
create g.make_b571
|
||||
n := b571_r
|
||||
h := b571_h
|
||||
end
|
||||
|
||||
curve: EC_CURVE_F2M
|
||||
g: EC_POINT_F2M
|
||||
end
|
||||
214
library/crypto/eel/ec/ec_domain_parameters_fp.e
Normal file
214
library/crypto/eel/ec/ec_domain_parameters_fp.e
Normal file
@@ -0,0 +1,214 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Extremism in the defense of liberty is no vice. Moderation in the pursuit of justice is no virtue. - Barry Goldwater (1964)"
|
||||
|
||||
class
|
||||
EC_DOMAIN_PARAMETERS_FP
|
||||
|
||||
inherit
|
||||
EC_DOMAIN_PARAMETERS
|
||||
redefine
|
||||
curve,
|
||||
g
|
||||
end
|
||||
STANDARD_CURVES
|
||||
export
|
||||
{NONE}
|
||||
all
|
||||
undefine
|
||||
default_create
|
||||
end
|
||||
|
||||
create
|
||||
make_curve_g_n,
|
||||
make_curve_g_n_h,
|
||||
make_sec_p112r1,
|
||||
make_sec_p112r2,
|
||||
make_sec_p128r1,
|
||||
make_sec_p128r2,
|
||||
make_sec_p160k1,
|
||||
make_sec_p160r1,
|
||||
make_sec_p160r2,
|
||||
make_sec_p192k1,
|
||||
make_sec_p192r1,
|
||||
make_sec_p224k1,
|
||||
make_sec_p224r1,
|
||||
make_sec_p256k1,
|
||||
make_sec_p256r1,
|
||||
make_sec_p384r1,
|
||||
make_sec_p521r1,
|
||||
make_p192,
|
||||
make_p224,
|
||||
make_p256,
|
||||
make_p384,
|
||||
make_p521
|
||||
|
||||
feature
|
||||
make_sec_p112r1
|
||||
do
|
||||
create curve.make_sec_p112r1
|
||||
create g.make_sec_p112r1
|
||||
n := sec_p112r1_r
|
||||
h := sec_p112r1_h
|
||||
end
|
||||
|
||||
make_sec_p112r2
|
||||
do
|
||||
create curve.make_sec_p112r2
|
||||
create g.make_sec_p112r2
|
||||
n := sec_p112r2_r
|
||||
h := sec_p112r2_h
|
||||
end
|
||||
|
||||
make_sec_p128r1
|
||||
do
|
||||
create curve.make_sec_p128r1
|
||||
create g.make_sec_p128r1
|
||||
n := sec_p128r1_r
|
||||
h := sec_p128r1_h
|
||||
end
|
||||
|
||||
make_sec_p128r2
|
||||
do
|
||||
create curve.make_sec_p128r2
|
||||
create g.make_sec_p128r2
|
||||
n := sec_p128r2_r
|
||||
h := sec_p128r2_h
|
||||
end
|
||||
|
||||
make_sec_p160k1
|
||||
do
|
||||
create curve.make_sec_p160k1
|
||||
create g.make_sec_p160k1
|
||||
n := sec_p160k1_r
|
||||
h := sec_p160k1_h
|
||||
end
|
||||
|
||||
make_sec_p160r1
|
||||
do
|
||||
create curve.make_sec_p160r1
|
||||
create g.make_sec_p160r1
|
||||
n := sec_p160r1_r
|
||||
h := sec_p160r1_h
|
||||
end
|
||||
|
||||
make_sec_p160r2
|
||||
do
|
||||
create curve.make_sec_p160r2
|
||||
create g.make_sec_p160r2
|
||||
n := sec_p160r2_r
|
||||
h := sec_p160r2_h
|
||||
end
|
||||
|
||||
make_sec_p192k1
|
||||
do
|
||||
create curve.make_sec_p192k1
|
||||
create g.make_sec_p192k1
|
||||
n := sec_p192k1_r
|
||||
h := sec_p192k1_h
|
||||
end
|
||||
|
||||
make_sec_p192r1
|
||||
do
|
||||
create curve.make_sec_p192r1
|
||||
create g.make_sec_p192r1
|
||||
n := sec_p192r1_r
|
||||
h := sec_p192r1_h
|
||||
end
|
||||
|
||||
make_sec_p224k1
|
||||
do
|
||||
create curve.make_sec_p224k1
|
||||
create g.make_sec_p224k1
|
||||
n := sec_p224k1_r
|
||||
h := sec_p224k1_h
|
||||
end
|
||||
|
||||
make_sec_p224r1
|
||||
do
|
||||
create curve.make_sec_p224r1
|
||||
create g.make_sec_p224r1
|
||||
n := sec_p224r1_r
|
||||
h := sec_p224r1_h
|
||||
end
|
||||
|
||||
make_sec_p256k1
|
||||
do
|
||||
create curve.make_sec_p256k1
|
||||
create g.make_sec_p256k1
|
||||
n := sec_p256k1_r
|
||||
h := sec_p256k1_h
|
||||
end
|
||||
|
||||
make_sec_p256r1
|
||||
do
|
||||
create curve.make_sec_p256r1
|
||||
create g.make_sec_p256r1
|
||||
n := sec_p256r1_r
|
||||
h := sec_p256r1_h
|
||||
end
|
||||
|
||||
make_sec_p384r1
|
||||
do
|
||||
create curve.make_sec_p384r1
|
||||
create g.make_sec_p384r1
|
||||
n := sec_p384r1_r
|
||||
h := sec_p384r1_h
|
||||
end
|
||||
|
||||
make_sec_p521r1
|
||||
do
|
||||
create curve.make_sec_p521r1
|
||||
create g.make_sec_p521r1
|
||||
n := sec_p521r1_r
|
||||
h := sec_p521r1_h
|
||||
end
|
||||
|
||||
make_p192
|
||||
do
|
||||
create curve.make_p192
|
||||
create g.make_p192
|
||||
n := p192_r
|
||||
h := p192_h
|
||||
end
|
||||
|
||||
make_p224
|
||||
do
|
||||
create curve.make_p224
|
||||
create g.make_p224
|
||||
n := p224_r
|
||||
h := p224_h
|
||||
end
|
||||
|
||||
make_p256
|
||||
do
|
||||
create curve.make_p256
|
||||
create g.make_p256
|
||||
n := p256_r
|
||||
h := p256_h
|
||||
end
|
||||
|
||||
make_p384
|
||||
do
|
||||
create curve.make_p384
|
||||
create g.make_p384
|
||||
n := p384_r
|
||||
h := p384_h
|
||||
end
|
||||
|
||||
make_p521
|
||||
do
|
||||
create curve.make_p521
|
||||
create g.make_p521
|
||||
n := p521_r
|
||||
h := p521_h
|
||||
end
|
||||
|
||||
feature
|
||||
curve: EC_CURVE_FP
|
||||
g: EC_POINT_FP
|
||||
|
||||
end
|
||||
134
library/crypto/eel/ec/ec_field_element.e
Normal file
134
library/crypto/eel/ec/ec_field_element.e
Normal file
@@ -0,0 +1,134 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Liberty is not a means to a political end. It is itself the highest political end. - Lord Acton"
|
||||
|
||||
deferred class
|
||||
EC_FIELD_ELEMENT
|
||||
|
||||
inherit
|
||||
ANY
|
||||
redefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
DEBUG_OUTPUT
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
EC_CONSTANTS
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
x: INTEGER_X
|
||||
|
||||
copy (other: like Current)
|
||||
do
|
||||
x.copy (other.x)
|
||||
end
|
||||
|
||||
encoded_field_size (curve: EC_CURVE): INTEGER_32
|
||||
-- Return the size of this ecfieldelement in bytes when encoded according to x9.62
|
||||
-- This was added as a deviation from the lcrypto origional and seems to be cleaner
|
||||
-- Replacement for class X9IntegerConverter
|
||||
deferred
|
||||
end
|
||||
|
||||
plus (other: like Current; curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
plus_value (other: like Current; curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.plus (other, curve)
|
||||
end
|
||||
|
||||
minus (other: like Current; curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
minus_value (other: like Current; curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.minus (other, curve)
|
||||
end
|
||||
|
||||
product (other: like Current; curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
product_value (other: like Current; curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.product (other, curve)
|
||||
end
|
||||
|
||||
quotient (other: like Current; curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
quotient_value (other: like Current; curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.quotient (other, curve)
|
||||
end
|
||||
|
||||
opposite (curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
opposite_value (curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.opposite (curve)
|
||||
end
|
||||
|
||||
square (curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
square_value (curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.square (curve)
|
||||
end
|
||||
|
||||
inverse (curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
inverse_value (curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.inverse (curve)
|
||||
end
|
||||
|
||||
sqrt (curve: EC_CURVE): like Current
|
||||
-- Return a new ECFIELDELEMENT that is sqrt(current)
|
||||
deferred
|
||||
end
|
||||
|
||||
is_equal (other: like Current): BOOLEAN
|
||||
do
|
||||
Result := x ~ other.x
|
||||
ensure then
|
||||
Result = (x ~ other.x)
|
||||
end
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := x.out_hex
|
||||
end
|
||||
|
||||
invariant
|
||||
negative: not x.is_negative
|
||||
end
|
||||
518
library/crypto/eel/ec/ec_field_element_f2m.e
Normal file
518
library/crypto/eel/ec/ec_field_element_f2m.e
Normal file
@@ -0,0 +1,518 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "The power to tax is the power to destroy. - John Marshall"
|
||||
|
||||
class
|
||||
EC_FIELD_ELEMENT_F2M
|
||||
|
||||
inherit
|
||||
EC_FIELD_ELEMENT
|
||||
redefine
|
||||
is_equal,
|
||||
plus_value,
|
||||
minus_value,
|
||||
product_value,
|
||||
quotient_value,
|
||||
opposite_value,
|
||||
square_value,
|
||||
inverse_value
|
||||
end
|
||||
|
||||
F2M_REPRESENTATIONS
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
|
||||
INTEGER_X_FACILITIES
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
|
||||
LIMB_MANIPULATION
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
|
||||
SPECIAL_UTILITY
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
convert
|
||||
make ({INTEGER_X})
|
||||
|
||||
feature {NONE}
|
||||
|
||||
make (x_a: INTEGER_X)
|
||||
require
|
||||
non_negative_x: not x_a.is_negative
|
||||
do
|
||||
x := x_a
|
||||
end
|
||||
|
||||
feature -- Field element components
|
||||
|
||||
multZModF (a: INTEGER_X; m_limb_position: INTEGER m_bit_position: INTEGER k1_limb_position: INTEGER k1_bit_position: INTEGER)
|
||||
require
|
||||
a.is_positive
|
||||
local
|
||||
special: SPECIAL [NATURAL_32]
|
||||
limb: NATURAL_32
|
||||
do
|
||||
a.bit_shift_left (1)
|
||||
special := a.item
|
||||
limb := special [m_limb_position]
|
||||
if
|
||||
limb.bit_test (m_bit_position)
|
||||
then
|
||||
special [m_limb_position] := limb.set_bit (False, m_bit_position)
|
||||
special [0] := special [0].bit_xor (1)
|
||||
special [k1_limb_position] := special [k1_limb_position].bit_xor ((1).to_natural_32 |<< k1_bit_position)
|
||||
end
|
||||
ensure
|
||||
a.is_positive
|
||||
end
|
||||
|
||||
|
||||
multZModF_p (a: INTEGER_X; m_limb_position: INTEGER m_bit_position: INTEGER k1_limb_position: INTEGER k1_bit_position: INTEGER k2_limb_position: INTEGER k2_bit_position: INTEGER k3_limb_position: INTEGER k3_bit_position: INTEGER)
|
||||
require
|
||||
a.is_positive
|
||||
local
|
||||
special: SPECIAL [NATURAL_32]
|
||||
limb: NATURAL_32
|
||||
do
|
||||
a.bit_shift_left (1)
|
||||
special := a.item
|
||||
limb := special [m_limb_position]
|
||||
if
|
||||
limb.bit_test (m_bit_position)
|
||||
then
|
||||
special [m_limb_position] := limb.set_bit (False, m_bit_position)
|
||||
special [0] := special [0].bit_xor (1)
|
||||
special [k1_limb_position] := special [k1_limb_position].bit_xor ((1).to_natural_32 |<< k1_bit_position)
|
||||
special [k2_limb_position] := special [k2_limb_position].bit_xor ((1).to_natural_32 |<< k2_bit_position)
|
||||
special [k3_limb_position] := special [k3_limb_position].bit_xor ((1).to_natural_32 |<< k3_bit_position)
|
||||
end
|
||||
ensure
|
||||
a.is_positive
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
encoded_field_size (curve: EC_CURVE_F2M): INTEGER_32
|
||||
--
|
||||
obsolete
|
||||
"Needs implementation"
|
||||
do
|
||||
|
||||
end
|
||||
|
||||
feature -- Implementing features of ECFIELDELEMENT
|
||||
|
||||
plus_value (other: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
plus (other: like Current; curve: EC_CURVE_F2M)
|
||||
do
|
||||
x.bit_xor (other.x)
|
||||
end
|
||||
|
||||
minus_value (other: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
minus (other: like Current; curve: EC_CURVE_F2M)
|
||||
do
|
||||
plus (other, curve)
|
||||
end
|
||||
|
||||
product_value (b: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (b, curve)
|
||||
end
|
||||
|
||||
product (b: like Current; curve: EC_CURVE_F2M)
|
||||
local
|
||||
m: INTEGER
|
||||
m_bit_position: INTEGER
|
||||
m_limb_position: INTEGER
|
||||
k1_bit_position: INTEGER
|
||||
k1_limb_position: INTEGER
|
||||
k2_bit_position: INTEGER
|
||||
k2_limb_position: INTEGER
|
||||
k3_bit_position: INTEGER
|
||||
k3_limb_position: INTEGER
|
||||
bz: INTEGER_X
|
||||
cz: INTEGER_X
|
||||
special: SPECIAL [NATURAL_32]
|
||||
limb: NATURAL_32
|
||||
limb_position: INTEGER
|
||||
bit_position: INTEGER
|
||||
new_bit_position: INTEGER
|
||||
do
|
||||
m := curve.m
|
||||
m_limb_position := bit_index_to_limb_index (m)
|
||||
m_bit_position := m \\ limb_bits
|
||||
k1_limb_position := bit_index_to_limb_index (curve.k1)
|
||||
k1_bit_position := curve.k1 \\ limb_bits
|
||||
k2_limb_position := bit_index_to_limb_index (curve.k2)
|
||||
k2_bit_position := curve.k2 \\ limb_bits
|
||||
k3_limb_position := bit_index_to_limb_index (curve.k3)
|
||||
k3_bit_position := curve.k3 \\ limb_bits
|
||||
create bz.make_bits (m + m)
|
||||
bz.copy (b.x)
|
||||
limb_position := 0
|
||||
bit_position := 0
|
||||
special := x.item
|
||||
x.resize (bits_to_limbs (m))
|
||||
limb := special [limb_position]
|
||||
create cz.make_bits (m + m)
|
||||
from
|
||||
bit_position := 0
|
||||
until
|
||||
limb_position * limb_bits + bit_position >= m
|
||||
loop
|
||||
if
|
||||
limb.bit_test (bit_position)
|
||||
then
|
||||
cz.bit_xor (bz)
|
||||
end
|
||||
new_bit_position := (bit_position + 1) \\ limb_bits
|
||||
if new_bit_position < bit_position then
|
||||
limb_position := limb_position + 1
|
||||
limb := special [limb_position]
|
||||
end
|
||||
bit_position := new_bit_position
|
||||
if curve.representation = PPB then
|
||||
multZmodF_p (bz, m_limb_position, m_bit_position, k1_limb_position, k1_bit_position, k2_limb_position, k2_bit_position, k3_limb_position, k3_bit_position)
|
||||
else
|
||||
multZmodF (bz, m_limb_position, m_bit_position, k1_limb_position, k1_bit_position)
|
||||
end
|
||||
end
|
||||
x := cz
|
||||
end
|
||||
|
||||
quotient_value (other: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
quotient (other: like Current; curve: EC_CURVE_F2M)
|
||||
local
|
||||
bInv: like Current
|
||||
do
|
||||
bInv := other.inverse_value (curve)
|
||||
product (bInv, curve)
|
||||
end
|
||||
|
||||
opposite_value (curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
opposite (curve: EC_CURVE_F2M)
|
||||
do
|
||||
do_nothing
|
||||
end
|
||||
|
||||
square_value (curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
square (curve: EC_CURVE_F2M)
|
||||
local
|
||||
i: INTEGER_32
|
||||
limb_position: INTEGER
|
||||
bit_position: INTEGER
|
||||
new_bit_position: INTEGER
|
||||
square_limb_position: INTEGER
|
||||
square_bit_position: INTEGER
|
||||
limb: NATURAL_32
|
||||
square_limb: NATURAL_32
|
||||
special: SPECIAL [NATURAL_32]
|
||||
do
|
||||
from
|
||||
i := curve.m
|
||||
x.resize (bits_to_limbs (i + i))
|
||||
special := x.item
|
||||
limb_position := bit_index_to_limb_index (i)
|
||||
bit_position := i \\ limb_bits
|
||||
square_limb_position := bit_index_to_limb_index (i + i)
|
||||
square_bit_position := (i + i) \\ limb_bits
|
||||
limb := special [limb_position]
|
||||
square_limb := special [square_limb_position]
|
||||
invariant
|
||||
i = limb_position * limb_bits + bit_position
|
||||
until
|
||||
i < 0
|
||||
loop
|
||||
if
|
||||
limb.bit_test (bit_position)
|
||||
then
|
||||
-- x.set_bit (True, i + i)
|
||||
square_limb := square_limb.set_bit (True, square_bit_position)
|
||||
else
|
||||
-- x.set_bit (False, i + i)
|
||||
square_limb := square_limb.set_bit (False, square_bit_position)
|
||||
end
|
||||
-- x.set_bit (False, i + i + 1)
|
||||
square_limb := square_limb.set_bit (False, square_bit_position + 1)
|
||||
new_bit_position := bit_position - 1
|
||||
if new_bit_position < 0 and limb_position > 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
limb_position := limb_position - 1
|
||||
limb := special [limb_position]
|
||||
end
|
||||
bit_position := new_bit_position
|
||||
new_bit_position := square_bit_position - 2
|
||||
if new_bit_position < 0 and square_limb_position > 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
special [square_limb_position] := square_limb
|
||||
square_limb_position := square_limb_position - 1
|
||||
square_limb := special [square_limb_position]
|
||||
end
|
||||
square_bit_position := new_bit_position
|
||||
i := i - 1
|
||||
variant
|
||||
i + 3
|
||||
end
|
||||
if square_bit_position /= limb_bits - 2 then
|
||||
special [square_limb_position] := square_limb
|
||||
else
|
||||
do_nothing
|
||||
end
|
||||
reduce (x, curve)
|
||||
x.count := x.normalize (special, 0, bits_to_limbs (curve.m))
|
||||
end
|
||||
|
||||
reduce (in: INTEGER_X; curve: EC_CURVE_F2M)
|
||||
local
|
||||
m: INTEGER
|
||||
i: INTEGER
|
||||
k1: INTEGER
|
||||
k1_limb_position: INTEGER
|
||||
k1_limb_diff: NATURAL_32
|
||||
k1_bit_position: INTEGER
|
||||
k2: INTEGER
|
||||
k2_limb_position: INTEGER
|
||||
k2_limb_diff: NATURAL_32
|
||||
k2_bit_position: INTEGER
|
||||
k3: INTEGER
|
||||
k3_limb_position: INTEGER
|
||||
k3_limb_diff: NATURAL_32
|
||||
k3_bit_position: INTEGER
|
||||
low_limb_position: INTEGER
|
||||
low_limb_diff: NATURAL_32
|
||||
low_bit_position: INTEGER
|
||||
special: SPECIAL [NATURAL_32]
|
||||
limb: NATURAL_32
|
||||
limb_diff: NATURAL_32
|
||||
limb_position: INTEGER
|
||||
bit_position: INTEGER
|
||||
new_bit_position: INTEGER
|
||||
do
|
||||
m := curve.m
|
||||
k1 := curve.k1
|
||||
k2 := curve.k2
|
||||
k3 := curve.k3
|
||||
special := in.item
|
||||
from
|
||||
i := m + m - 1
|
||||
limb_position := bit_index_to_limb_index (i)
|
||||
low_limb_position := bit_index_to_limb_index (i - m)
|
||||
k1_limb_position := bit_index_to_limb_index (k1 + i - m)
|
||||
bit_position := i \\ limb_bits
|
||||
low_bit_position := (i - m) \\ limb_bits
|
||||
k1_bit_position := (k1 + i - m) \\ limb_bits
|
||||
if curve.representation = PPB then
|
||||
k2_limb_position := bit_index_to_limb_index (k2 + i - m)
|
||||
k3_limb_position := bit_index_to_limb_index (k3 + i - m)
|
||||
k2_bit_position := (k2 + i - m) \\ limb_bits
|
||||
k3_bit_position := (k3 + i - m) \\ limb_bits
|
||||
end
|
||||
limb := special [limb_position]
|
||||
invariant
|
||||
i = limb_position * limb_bits + bit_position
|
||||
until
|
||||
i < m
|
||||
loop
|
||||
if
|
||||
limb.bit_test (bit_position)
|
||||
then
|
||||
limb_diff := limb_diff.set_bit (True, bit_position)
|
||||
low_limb_diff := low_limb_diff.set_bit (True, low_bit_position)
|
||||
k1_limb_diff := k1_limb_diff.set_bit (True, k1_bit_position)
|
||||
if
|
||||
curve.representation = PPB
|
||||
then
|
||||
k2_limb_diff := k2_limb_diff.set_bit (True, k2_bit_position)
|
||||
k3_limb_diff := k3_limb_diff.set_bit (True, k3_bit_position)
|
||||
end
|
||||
end
|
||||
new_bit_position := bit_position - 1
|
||||
if new_bit_position < 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
special [limb_position] := special [limb_position].bit_xor (limb_diff)
|
||||
limb_position := limb_position - 1
|
||||
limb := special [limb_position]
|
||||
limb_diff := 0
|
||||
end
|
||||
bit_position := new_bit_position
|
||||
new_bit_position := low_bit_position - 1
|
||||
if new_bit_position < 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
special [low_limb_position] := special [low_limb_position].bit_xor (low_limb_diff)
|
||||
low_limb_position := low_limb_position - 1
|
||||
low_limb_diff := 0
|
||||
end
|
||||
low_bit_position := new_bit_position
|
||||
new_bit_position := k1_bit_position - 1
|
||||
if new_bit_position < 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
special [k1_limb_position] := special [k1_limb_position].bit_xor (k1_limb_diff)
|
||||
k1_limb_position := k1_limb_position - 1
|
||||
k1_limb_diff := 0
|
||||
end
|
||||
k1_bit_position := new_bit_position
|
||||
if curve.representation = PPB then
|
||||
new_bit_position := k2_bit_position - 1
|
||||
if new_bit_position < 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
special [k2_limb_position] := special [k2_limb_position].bit_xor (k2_limb_diff)
|
||||
k2_limb_position := k2_limb_position - 1
|
||||
k2_limb_diff := 0
|
||||
end
|
||||
k2_bit_position := new_bit_position
|
||||
new_bit_position := k3_bit_position - 1
|
||||
if new_bit_position < 0 then
|
||||
new_bit_position := new_bit_position + limb_bits
|
||||
special [k3_limb_position] := special [k3_limb_position].bit_xor (k3_limb_diff)
|
||||
k3_limb_position := k3_limb_position - 1
|
||||
k3_limb_diff := 0
|
||||
end
|
||||
k3_bit_position := new_bit_position
|
||||
end
|
||||
i := i - 1
|
||||
end
|
||||
if bit_position /= limb_bits - 1 then
|
||||
special [limb_position] := special [limb_position].bit_xor (limb_diff)
|
||||
end
|
||||
if low_bit_position /= limb_bits - 1 then
|
||||
special [low_limb_position] := special [low_limb_position].bit_xor (low_limb_diff)
|
||||
end
|
||||
if k1_bit_position /= limb_bits - 1 then
|
||||
special [k1_limb_position] := special [k1_limb_position].bit_xor (k1_limb_diff)
|
||||
end
|
||||
if curve.representation = PPB then
|
||||
if k2_bit_position /= limb_bits - 1 then
|
||||
special [k2_limb_position] := special [k2_limb_position].bit_xor (k2_limb_diff)
|
||||
end
|
||||
if k3_bit_position /= limb_bits - 1 then
|
||||
special [k3_limb_position] := special [k3_limb_position].bit_xor (k3_limb_diff)
|
||||
end
|
||||
end
|
||||
in.count := in.normalize (special, 0, in.count)
|
||||
end
|
||||
|
||||
inverse_value (curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
inverse (curve: EC_CURVE_F2M)
|
||||
local
|
||||
uz: INTEGER_X
|
||||
vz: INTEGER_X
|
||||
-- g1z: INTEGER_X
|
||||
-- g2z: INTEGER_X
|
||||
-- j: INTEGER_32
|
||||
-- tmp_int: INTEGER_X
|
||||
m: INTEGER
|
||||
-- uz_bits: INTEGER
|
||||
-- vz_bits: INTEGER
|
||||
-- tmp_int2: INTEGER
|
||||
-- uz_old: INTEGER_X
|
||||
-- gz_old: INTEGER_X
|
||||
do
|
||||
m := curve.m
|
||||
create uz.make_bits (m + m)
|
||||
uz.copy (x)
|
||||
create vz.make_bits (m + m)
|
||||
vz.set_bit (True, m)
|
||||
vz.set_bit (True, 0)
|
||||
vz.set_bit (True, curve.k1)
|
||||
if
|
||||
curve.representation = PPB
|
||||
then
|
||||
vz.set_bit (True, curve.k2)
|
||||
vz.set_bit (True, curve.k3)
|
||||
end
|
||||
vz.count := normalize (vz.item, 0, bits_to_limbs (m))
|
||||
|
||||
x.invert_gf (vz)
|
||||
-- create g1z.make_bits (m + m)
|
||||
-- g1z.set_from_integer (1)
|
||||
-- create g2z.make_bits (m + m)
|
||||
-- from
|
||||
-- until
|
||||
-- uz.is_zero
|
||||
-- loop
|
||||
-- uz_bits := uz.bits
|
||||
-- vz_bits := vz.bits
|
||||
-- if
|
||||
-- uz_bits < vz_bits
|
||||
-- then
|
||||
-- tmp_int := uz
|
||||
-- uz := vz
|
||||
-- vz := tmp_int
|
||||
-- tmp_int := g1z
|
||||
-- g1z := g2z
|
||||
-- g2z := tmp_int
|
||||
-- tmp_int2 := uz_bits
|
||||
-- uz_bits := vz_bits
|
||||
-- vz_bits := tmp_int2
|
||||
-- end
|
||||
-- if uz_bits /= vz_bits then
|
||||
-- j := uz_bits - vz_bits
|
||||
---- vz.bit_shift_left (j)
|
||||
---- uz_old := uz.bit_xor_value (vz)
|
||||
---- vz.bit_shift_right (j)
|
||||
---- g2z.bit_shift_left (j)
|
||||
---- gz_old := g1z.bit_xor_value (g2z)
|
||||
---- g2z.bit_shift_right (j)
|
||||
-- uz.bit_xor_left_shift (vz, j)
|
||||
-- g1z.bit_xor_left_shift (g2z, j)
|
||||
-- else
|
||||
-- uz.bit_xor (vz)
|
||||
-- g1z.bit_xor (g2z)
|
||||
-- end
|
||||
-- end
|
||||
-- x := g2z
|
||||
end
|
||||
|
||||
sqrt (curve: EC_CURVE_F2M): like Current
|
||||
-- Not implemented
|
||||
do
|
||||
create Result.make (create {INTEGER_X}.default_create)
|
||||
end
|
||||
|
||||
is_equal (other: like Current): BOOLEAN
|
||||
do
|
||||
Result := x ~ other.x
|
||||
end
|
||||
|
||||
end
|
||||
214
library/crypto/eel/ec/ec_field_element_fp.e
Normal file
214
library/crypto/eel/ec/ec_field_element_fp.e
Normal file
@@ -0,0 +1,214 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Liberty lies in the hearts of men and women. When it dies there, no constitution, no law, no court can save it. - Justice Learned Hand"
|
||||
|
||||
class
|
||||
EC_FIELD_ELEMENT_FP
|
||||
|
||||
inherit
|
||||
EC_FIELD_ELEMENT
|
||||
redefine
|
||||
is_equal,
|
||||
plus_value,
|
||||
minus_value,
|
||||
product_value,
|
||||
quotient_value,
|
||||
opposite_value,
|
||||
square_value,
|
||||
inverse_value
|
||||
end
|
||||
|
||||
create
|
||||
make_p_x,
|
||||
make_q_x_hex
|
||||
|
||||
create {EC_POINT, EC_CURVE_FP}
|
||||
make_zero
|
||||
|
||||
feature {EC_POINT_FP, EC_CURVE_FP}
|
||||
make_zero
|
||||
do
|
||||
create x.default_create
|
||||
end
|
||||
|
||||
feature
|
||||
make_p_x (x_new: INTEGER_X)
|
||||
-- create a new ECFIELDELEMENTFP based on q and x
|
||||
do
|
||||
x := x_new
|
||||
end
|
||||
|
||||
make_q_x_hex(curve_a: EC_CURVE_FP x_hex_a: STRING)
|
||||
do
|
||||
make_p_x (create {INTEGER_X}.make_from_hex_string (x_hex_a))
|
||||
end
|
||||
|
||||
feature {EC_FIELD_ELEMENT_FP}
|
||||
|
||||
W (n: INTEGER_X r: INTEGER_X x_new: INTEGER_X p_a: INTEGER_X): INTEGER_X
|
||||
-- I'm not sure what this does
|
||||
local
|
||||
w_one: INTEGER_X
|
||||
w_two: INTEGER_X
|
||||
do
|
||||
if
|
||||
n ~ (ONE)
|
||||
then
|
||||
result := ((r * r * x_new.powm_value ((p_a - TWO), p_a)) - TWO) \\ p_a
|
||||
elseif
|
||||
not n.bit_test(0)
|
||||
then
|
||||
w_one := W (n / TWO, r, x, p_a)
|
||||
result := ((w_one * w_one) - TWO) \\ p_a
|
||||
else
|
||||
w_one := W ((n + ONE) / TWO, r, x, p_a)
|
||||
w_two := W ((n - ONE) / TWO, r, x, p_a)
|
||||
result := ((w_one * w_two) - W (ONE, r, x, p_a)) \\ p_a
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
encoded_field_size (curve: EC_CURVE_FP): INTEGER_32
|
||||
-- Return the encoded field size for FP field elements
|
||||
local
|
||||
p: INTEGER_X
|
||||
do
|
||||
p := curve.q
|
||||
result := p.bytes
|
||||
end
|
||||
|
||||
plus_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
plus (other: like Current; curve: EC_CURVE_FP)
|
||||
do
|
||||
x.plus (other.x)
|
||||
x.modulo (curve.q)
|
||||
end
|
||||
|
||||
minus_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
minus (other: like Current; curve: EC_CURVE_FP)
|
||||
do
|
||||
x.minus (other.x)
|
||||
x.modulo (curve.q)
|
||||
end
|
||||
|
||||
product_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
product (other: like Current; curve: EC_CURVE_FP)
|
||||
do
|
||||
x.product (other.x)
|
||||
x.modulo (curve.q)
|
||||
end
|
||||
|
||||
quotient_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
quotient (other: like Current; curve: EC_CURVE_FP)
|
||||
local
|
||||
p: INTEGER_X
|
||||
do
|
||||
p := curve.q
|
||||
x.product (other.x.inverse_value (p))
|
||||
x.modulo (p)
|
||||
end
|
||||
|
||||
opposite_value (curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
opposite (curve: EC_CURVE_FP)
|
||||
do
|
||||
x.opposite
|
||||
x.modulo (curve.q)
|
||||
end
|
||||
|
||||
square_value (curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
square (curve: EC_CURVE_FP)
|
||||
do
|
||||
x.product (x)
|
||||
x.modulo (curve.q)
|
||||
end
|
||||
|
||||
inverse_value (curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
inverse (curve: EC_CURVE_FP)
|
||||
do
|
||||
x.inverse (curve.q)
|
||||
end
|
||||
|
||||
sqrt (curve: EC_CURVE_FP): like Current
|
||||
-- Implement sqrt over FP
|
||||
local
|
||||
z: EC_FIELD_ELEMENT_FP
|
||||
legendreExponent: INTEGER_X
|
||||
fourX: INTEGER_X
|
||||
r: INTEGER_X
|
||||
n1: INTEGER_X
|
||||
n2: INTEGER_X
|
||||
root: INTEGER_X
|
||||
exponent: INTEGER_X
|
||||
p: INTEGER_X
|
||||
do
|
||||
p := curve.q
|
||||
if
|
||||
p.bit_test (1)
|
||||
then
|
||||
create z.make_p_x (x.powm_value (p.bit_shift_right_value (2) + one, p))
|
||||
Result := z
|
||||
elseif
|
||||
p.bit_test (0)
|
||||
then
|
||||
legendreExponent := (p - ONE) / TWO
|
||||
exponent := x.powm_value (legendreExponent, p)
|
||||
check exponent ~ one end
|
||||
fourX := FOUR * x
|
||||
r := TWO
|
||||
from
|
||||
until
|
||||
not ((r * r - fourx).powm_value (legendreExponent, p) ~ (p - ONE))
|
||||
loop
|
||||
--Is this correct? There's a slightly higher chance that the
|
||||
-- number is in the range 0 - q than q - 2^q.bits
|
||||
create r.make_random (p.bits)
|
||||
r := r \\ p
|
||||
end
|
||||
n1 := (p - ONE) / FOUR
|
||||
n2 := (p + THREE) / FOUR
|
||||
root := (x * (TWO * r).powm_value (p - TWO, p) * (W (n1, r, x, p) + W (n2, r, x, p))) \\ p
|
||||
create z.make_p_x (root)
|
||||
Result := z
|
||||
else
|
||||
create Result.make_p_x (create {INTEGER_X}.default_create)
|
||||
(create {EXCEPTION}.default_create).raise
|
||||
end
|
||||
end
|
||||
|
||||
is_equal (other: like current): BOOLEAN
|
||||
-- Is this FP = other
|
||||
do
|
||||
result := x ~ other.x
|
||||
end
|
||||
end
|
||||
334
library/crypto/eel/ec/ec_key_pair.e
Normal file
334
library/crypto/eel/ec/ec_key_pair.e
Normal file
@@ -0,0 +1,334 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "A nation of sheep will beget a government of wolves. - Edward R. Murrow"
|
||||
|
||||
class
|
||||
EC_KEY_PAIR
|
||||
|
||||
inherit
|
||||
DEBUG_OUTPUT
|
||||
|
||||
create
|
||||
make,
|
||||
make_p192,
|
||||
make_p224,
|
||||
make_p256,
|
||||
make_p384,
|
||||
make_p521,
|
||||
make_k163,
|
||||
make_k233,
|
||||
make_k283,
|
||||
make_k409,
|
||||
make_k571,
|
||||
make_b163,
|
||||
make_b233,
|
||||
make_b283,
|
||||
make_b409,
|
||||
make_b571,
|
||||
make_sec_p112r1,
|
||||
make_sec_p112r2,
|
||||
make_sec_p128r1,
|
||||
make_sec_p128r2,
|
||||
make_sec_p160k1,
|
||||
make_sec_p160r1,
|
||||
make_sec_p160r2,
|
||||
make_sec_p192k1,
|
||||
make_sec_p192r1,
|
||||
make_sec_p224k1,
|
||||
make_sec_p224r1,
|
||||
make_sec_p256k1,
|
||||
make_sec_p256r1,
|
||||
make_sec_p384r1,
|
||||
make_sec_p521r1,
|
||||
make_sec_t113r1,
|
||||
make_sec_t113r2,
|
||||
make_sec_t131r1,
|
||||
make_sec_t131r2,
|
||||
make_sec_t163k1,
|
||||
make_sec_t163r1,
|
||||
make_sec_t163r2,
|
||||
make_sec_t193r1,
|
||||
make_sec_t193r2,
|
||||
make_sec_t233k1,
|
||||
make_sec_t233r1,
|
||||
make_sec_t239k1,
|
||||
make_sec_t283k1,
|
||||
make_sec_t283r1,
|
||||
make_sec_t409k1,
|
||||
make_sec_t409r1,
|
||||
make_sec_t571k1,
|
||||
make_sec_t571r1
|
||||
|
||||
feature
|
||||
make (params: EC_DOMAIN_PARAMETERS)
|
||||
local
|
||||
d: INTEGER_X
|
||||
q: EC_POINT
|
||||
do
|
||||
from
|
||||
create d.make_random_max (params.n)
|
||||
until
|
||||
not d.is_zero
|
||||
loop
|
||||
create d.make_random_max (params.n)
|
||||
end
|
||||
q := params.g.product_value (d, params.curve)
|
||||
create public.make_q_parameters (q, params)
|
||||
create private.make_d_params (d, params)
|
||||
end
|
||||
|
||||
feature --SEC recommended prime curves
|
||||
make_sec_p112r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p112r1)
|
||||
end
|
||||
|
||||
make_sec_p112r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p112r2)
|
||||
end
|
||||
|
||||
make_sec_p128r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p128r1)
|
||||
end
|
||||
|
||||
make_sec_p128r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p128r2)
|
||||
end
|
||||
|
||||
make_sec_p160k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p160k1)
|
||||
end
|
||||
|
||||
make_sec_p160r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p160r1)
|
||||
end
|
||||
|
||||
make_sec_p160r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p160r2)
|
||||
end
|
||||
|
||||
make_sec_p192k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p192k1)
|
||||
end
|
||||
|
||||
make_sec_p192r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p192r1)
|
||||
end
|
||||
|
||||
make_sec_p224k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p224k1)
|
||||
end
|
||||
|
||||
make_sec_p224r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p224r1)
|
||||
end
|
||||
|
||||
make_sec_p256k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p256k1)
|
||||
end
|
||||
|
||||
make_sec_p256r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p256r1)
|
||||
end
|
||||
|
||||
make_sec_p384r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p384r1)
|
||||
end
|
||||
|
||||
make_sec_p521r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p521r1)
|
||||
end
|
||||
|
||||
feature --SEC recommended polynomial curves
|
||||
make_sec_t113r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t113r1)
|
||||
end
|
||||
|
||||
make_sec_t113r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t113r2)
|
||||
end
|
||||
|
||||
make_sec_t131r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t131r1)
|
||||
end
|
||||
|
||||
make_sec_t131r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t131r2)
|
||||
end
|
||||
|
||||
make_sec_t163k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t163k1)
|
||||
end
|
||||
|
||||
make_sec_t163r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t163r1)
|
||||
end
|
||||
|
||||
make_sec_t163r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t163r2)
|
||||
end
|
||||
|
||||
make_sec_t193r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t193r1)
|
||||
end
|
||||
|
||||
make_sec_t193r2
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t193r2)
|
||||
end
|
||||
|
||||
make_sec_t233k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t233k1)
|
||||
end
|
||||
|
||||
make_sec_t233r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t233r1)
|
||||
end
|
||||
|
||||
make_sec_t239k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t239k1)
|
||||
end
|
||||
|
||||
make_sec_t283k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t283k1)
|
||||
end
|
||||
|
||||
make_sec_t283r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t283r1)
|
||||
end
|
||||
|
||||
make_sec_t409k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t409k1)
|
||||
end
|
||||
|
||||
make_sec_t409r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t409r1)
|
||||
end
|
||||
|
||||
make_sec_t571k1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t571k1)
|
||||
end
|
||||
|
||||
make_sec_t571r1
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t571r1)
|
||||
end
|
||||
|
||||
feature --FIPS curves
|
||||
make_p192
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p192)
|
||||
end
|
||||
|
||||
make_p224
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p224)
|
||||
end
|
||||
|
||||
make_p256
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p256)
|
||||
end
|
||||
|
||||
make_p384
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p384)
|
||||
end
|
||||
|
||||
make_p521
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p521)
|
||||
end
|
||||
|
||||
make_k163
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k163)
|
||||
end
|
||||
|
||||
make_k233
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k233)
|
||||
end
|
||||
|
||||
make_k283
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k283)
|
||||
end
|
||||
|
||||
make_k409
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k409)
|
||||
end
|
||||
|
||||
make_k571
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k571)
|
||||
end
|
||||
|
||||
make_b163
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b163)
|
||||
end
|
||||
|
||||
make_b233
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b233)
|
||||
end
|
||||
|
||||
make_b283
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b283)
|
||||
end
|
||||
|
||||
make_b409
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b409)
|
||||
end
|
||||
|
||||
make_b571
|
||||
do
|
||||
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b571)
|
||||
end
|
||||
|
||||
public: EC_PUBLIC_KEY
|
||||
private: EC_PRIVATE_KEY
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := "Public:%N" + public.debug_output + "%NPrivate:%N" + private.debug_output
|
||||
end
|
||||
end
|
||||
13
library/crypto/eel/ec/ec_key_parameters.e
Normal file
13
library/crypto/eel/ec/ec_key_parameters.e
Normal file
@@ -0,0 +1,13 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "It is not the responsibility of the government or the legal system to protect a citizen from himself. - Justice Casey Percell"
|
||||
|
||||
deferred class
|
||||
EC_KEY_PARAMETERS
|
||||
|
||||
feature
|
||||
params: EC_DOMAIN_PARAMETERS
|
||||
end
|
||||
122
library/crypto/eel/ec/ec_point.e
Normal file
122
library/crypto/eel/ec/ec_point.e
Normal file
@@ -0,0 +1,122 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "The government was set to protect man from criminals - and the Constitution was written to protect man from the government. - Ayn Rand"
|
||||
|
||||
deferred class
|
||||
EC_POINT
|
||||
|
||||
inherit
|
||||
ANY
|
||||
redefine
|
||||
is_equal
|
||||
end
|
||||
DEBUG_OUTPUT
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
|
||||
feature
|
||||
x: EC_FIELD_ELEMENT
|
||||
y: EC_FIELD_ELEMENT
|
||||
infinity: BOOLEAN
|
||||
|
||||
make_infinity
|
||||
deferred
|
||||
ensure
|
||||
infinity
|
||||
end
|
||||
|
||||
set_infinity
|
||||
deferred
|
||||
ensure
|
||||
infinity
|
||||
end
|
||||
|
||||
is_equal (other: like Current): BOOLEAN
|
||||
-- Is current point equal to other point
|
||||
do
|
||||
result := (infinity = other.infinity) and then (not infinity implies (x ~ other.x and y ~ other.y))
|
||||
end
|
||||
|
||||
to_byte_array_compressed (curve: EC_CURVE): SPECIAL[NATURAL_8]
|
||||
-- Return the Uncompressed version of this point, regardless of the creation
|
||||
deferred
|
||||
end
|
||||
|
||||
to_byte_array_uncompressed (curve: EC_CURVE): SPECIAL[NATURAL_8]
|
||||
-- Return the compressed version of this point
|
||||
deferred
|
||||
end
|
||||
|
||||
plus (other: like Current curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
plus_value (other: like Current curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.plus (other, curve)
|
||||
ensure
|
||||
infinity implies Result ~ other
|
||||
other.infinity implies Result ~ Current
|
||||
(Current ~ other) implies (Result ~ twice_value (curve))
|
||||
end
|
||||
|
||||
minus (other: like Current curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
minus_value (other: like Current curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.minus (other, curve)
|
||||
ensure
|
||||
infinity implies Result ~ other
|
||||
other.infinity implies Result ~ Current
|
||||
end
|
||||
|
||||
twice (curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
twice_value (curve:EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.twice (curve)
|
||||
ensure
|
||||
twice_definition: Result ~ Current.plus_value (Current, curve)
|
||||
end
|
||||
|
||||
product (other: INTEGER_X; curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
product_value (other: INTEGER_X; curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.product (other, curve)
|
||||
end
|
||||
|
||||
opposite (curve: EC_CURVE)
|
||||
deferred
|
||||
end
|
||||
|
||||
opposite_value (curve: EC_CURVE): like Current
|
||||
do
|
||||
Result := deep_twin
|
||||
Result.opposite (curve)
|
||||
end
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := "0x" + x.debug_output + "%N0x" + y.debug_output
|
||||
end
|
||||
|
||||
invariant
|
||||
infinity_x: infinity implies x.x.is_zero
|
||||
infinity_y: infinity implies y.x.is_zero
|
||||
end
|
||||
593
library/crypto/eel/ec/ec_point_f2m.e
Normal file
593
library/crypto/eel/ec/ec_point_f2m.e
Normal file
@@ -0,0 +1,593 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "What this country needs are more unemployed politicians. - Edward Langley"
|
||||
|
||||
class
|
||||
EC_POINT_F2M
|
||||
|
||||
inherit
|
||||
EC_POINT
|
||||
redefine
|
||||
x,
|
||||
y,
|
||||
opposite_value,
|
||||
twice_value,
|
||||
product_value,
|
||||
minus_value,
|
||||
plus_value
|
||||
end
|
||||
EC_CONSTANTS
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
STANDARD_CURVES
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
INTEGER_X_FACILITIES
|
||||
undefine
|
||||
is_equal
|
||||
end
|
||||
|
||||
create
|
||||
make_curve_x_y,
|
||||
make_infinity,
|
||||
make_from_bytes,
|
||||
make_sec_t113r1,
|
||||
make_sec_t113r2,
|
||||
make_sec_t131r1,
|
||||
make_sec_t131r2,
|
||||
make_sec_t163k1,
|
||||
make_sec_t163r1,
|
||||
make_sec_t163r2,
|
||||
make_sec_t193r1,
|
||||
make_sec_t193r2,
|
||||
make_sec_t233k1,
|
||||
make_sec_t233r1,
|
||||
make_sec_t239k1,
|
||||
make_sec_t283k1,
|
||||
make_sec_t283r1,
|
||||
make_sec_t409k1,
|
||||
make_sec_t409r1,
|
||||
make_sec_t571k1,
|
||||
make_sec_t571r1,
|
||||
make_k163,
|
||||
make_k233,
|
||||
make_k283,
|
||||
make_k409,
|
||||
make_k571,
|
||||
make_b163,
|
||||
make_b233,
|
||||
make_b283,
|
||||
make_b409,
|
||||
make_b571
|
||||
|
||||
feature
|
||||
make_infinity
|
||||
do
|
||||
set_infinity
|
||||
end
|
||||
|
||||
feature -- SEC points
|
||||
make_sec_t113r1
|
||||
do
|
||||
create x.make (sec_t113r1_gx)
|
||||
create y.make (sec_t113r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t113r2
|
||||
do
|
||||
create x.make (sec_t113r2_gx)
|
||||
create y.make (sec_t113r2_gy)
|
||||
end
|
||||
|
||||
make_sec_t131r1
|
||||
do
|
||||
create x.make (sec_t131r1_gx)
|
||||
create y.make (sec_t131r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t131r2
|
||||
do
|
||||
create x.make (sec_t131r2_gx)
|
||||
create y.make (sec_t131r2_gy)
|
||||
end
|
||||
|
||||
make_sec_t163k1
|
||||
do
|
||||
create x.make (sec_t163k1_gx)
|
||||
create y.make (sec_t163k1_gy)
|
||||
end
|
||||
|
||||
make_sec_t163r1
|
||||
do
|
||||
create x.make (sec_t163r1_gx)
|
||||
create y.make (sec_t163r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t163r2
|
||||
do
|
||||
create x.make (sec_t163r2_gx)
|
||||
create y.make (sec_t163r2_gy)
|
||||
end
|
||||
|
||||
make_sec_t193r1
|
||||
do
|
||||
create x.make (sec_t193r1_gx)
|
||||
create y.make (sec_t193r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t193r2
|
||||
do
|
||||
create x.make (sec_t193r2_gx)
|
||||
create y.make (sec_t193r2_gy)
|
||||
end
|
||||
|
||||
make_sec_t233k1
|
||||
do
|
||||
create x.make (sec_t233k1_gx)
|
||||
create y.make (sec_t233k1_gy)
|
||||
end
|
||||
|
||||
make_sec_t233r1
|
||||
do
|
||||
create x.make (sec_t233r1_gx)
|
||||
create y.make (sec_t233r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t239k1
|
||||
do
|
||||
create x.make (sec_t239k1_gx)
|
||||
create y.make (sec_t239k1_gy)
|
||||
end
|
||||
|
||||
make_sec_t283k1
|
||||
do
|
||||
create x.make (sec_t283k1_gx)
|
||||
create y.make (sec_t283k1_gy)
|
||||
end
|
||||
|
||||
make_sec_t283r1
|
||||
do
|
||||
create x.make (sec_t283r1_gx)
|
||||
create y.make (sec_t283r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t409k1
|
||||
do
|
||||
create x.make (sec_t409k1_gx)
|
||||
create y.make (sec_t409k1_gy)
|
||||
end
|
||||
|
||||
make_sec_t409r1
|
||||
do
|
||||
create x.make (sec_t409r1_gx)
|
||||
create y.make (sec_t409r1_gy)
|
||||
end
|
||||
|
||||
make_sec_t571k1
|
||||
do
|
||||
create x.make (sec_t571k1_gx)
|
||||
create y.make (sec_t571k1_gy)
|
||||
end
|
||||
|
||||
make_sec_t571r1
|
||||
do
|
||||
create x.make (sec_t571r1_gx)
|
||||
create y.make (sec_t571r1_gy)
|
||||
end
|
||||
|
||||
feature -- FIPS points
|
||||
make_k163
|
||||
do
|
||||
create x.make (k163_gx)
|
||||
create y.make (k163_gy)
|
||||
end
|
||||
|
||||
make_k233
|
||||
do
|
||||
create x.make (k233_gx)
|
||||
create y.make (k233_gy)
|
||||
end
|
||||
|
||||
make_k283
|
||||
do
|
||||
create x.make (k283_gx)
|
||||
create y.make (k283_gy)
|
||||
end
|
||||
|
||||
make_k409
|
||||
do
|
||||
create x.make (k409_gx)
|
||||
create y.make (k409_gy)
|
||||
end
|
||||
|
||||
make_k571
|
||||
do
|
||||
create x.make (k571_gx)
|
||||
create y.make (k571_gy)
|
||||
end
|
||||
|
||||
make_b163
|
||||
do
|
||||
create x.make (b163_gx)
|
||||
create y.make (b163_gy)
|
||||
end
|
||||
|
||||
make_b233
|
||||
do
|
||||
create x.make (b233_gx)
|
||||
create y.make (b233_gy)
|
||||
end
|
||||
|
||||
make_b283
|
||||
do
|
||||
create x.make (b283_gx)
|
||||
create y.make (b283_gy)
|
||||
end
|
||||
|
||||
make_b409
|
||||
do
|
||||
create x.make (b409_gx)
|
||||
create y.make (b409_gy)
|
||||
end
|
||||
|
||||
make_b571
|
||||
do
|
||||
create x.make (b571_gx)
|
||||
create y.make (b571_gy)
|
||||
end
|
||||
|
||||
make_curve_x_y (x_a: EC_FIELD_ELEMENT_F2M; y_a: EC_FIELD_ELEMENT_F2M)
|
||||
do
|
||||
x := x_a
|
||||
y := y_a
|
||||
end
|
||||
|
||||
make_from_bytes (bytes: SPECIAL[NATURAL_8]; curve: EC_CURVE_F2M)
|
||||
do
|
||||
decodepoint (bytes, curve)
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
x: EC_FIELD_ELEMENT_F2M
|
||||
y: EC_FIELD_ELEMENT_F2M
|
||||
|
||||
set_from_other (other: like Current)
|
||||
do
|
||||
x.copy (other.x)
|
||||
y.copy (other.y)
|
||||
end
|
||||
|
||||
feature -- Decode/encode
|
||||
|
||||
set_infinity
|
||||
do
|
||||
create x.make (create {INTEGER_X}.default_create)
|
||||
create y.make (create {INTEGER_X}.default_create)
|
||||
infinity := True
|
||||
end
|
||||
|
||||
decodePoint (source: SPECIAL [NATURAL_8] curve: EC_CURVE_F2M)
|
||||
require
|
||||
Source_too_small: source.capacity > 0
|
||||
local
|
||||
enc: SPECIAL [NATURAL_8]
|
||||
do
|
||||
create enc.make_filled (0, source.count - 1)
|
||||
enc.copy_data (source, 1, 0, enc.count)
|
||||
inspect
|
||||
source[0]
|
||||
when 0x02 then
|
||||
decodeCompressedPoint (enc, 0, curve)
|
||||
when 0x03 then
|
||||
decodeCompressedPoint (enc, 1, curve)
|
||||
when 0x04 then
|
||||
decodeUncompressedPoint (enc)
|
||||
end
|
||||
end
|
||||
|
||||
decodeCompressedPoint (source: SPECIAL [NATURAL_8] ypBit: INTEGER curve: EC_CURVE_F2M)
|
||||
local
|
||||
xp: EC_FIELD_ELEMENT_F2M
|
||||
yp: EC_FIELD_ELEMENT_F2M
|
||||
i: INTEGER_32
|
||||
beta: EC_FIELD_ELEMENT_F2M
|
||||
z: EC_FIELD_ELEMENT_F2M
|
||||
oneEC: EC_FIELD_ELEMENT_F2M
|
||||
zBit: INTEGER
|
||||
do
|
||||
create xp.make (create {INTEGER_X}.make_from_bytes (source, source.lower, source.upper))
|
||||
if
|
||||
xp.x.is_zero
|
||||
then
|
||||
yp := curve.b
|
||||
from
|
||||
i := 0
|
||||
until
|
||||
i = curve.m - 1
|
||||
loop
|
||||
yp := yp.square_value (curve)
|
||||
i := i + 1
|
||||
end
|
||||
else
|
||||
beta := xp.plus_value (curve.a, curve).plus_value (curve.b.product_value (xp.square_value (curve).inverse_value (curve), curve), curve)
|
||||
--z := solveQuadraticEquation(beta)
|
||||
create z.make (create {INTEGER_X}.default_create)
|
||||
zBit := 0
|
||||
if
|
||||
z.x.bit_test (0)
|
||||
then
|
||||
zBit := 1
|
||||
end
|
||||
if
|
||||
zBit /= ypBit
|
||||
then
|
||||
create oneEC.make (ONE)
|
||||
z := z.plus_value (oneEC, curve)
|
||||
end
|
||||
yp := xp.product_value (z, curve)
|
||||
end
|
||||
x := xp
|
||||
y := yp
|
||||
end
|
||||
|
||||
decodeUncompressedPoint (source: SPECIAL [NATURAL_8])
|
||||
require
|
||||
X_and_y_different_sizes: source.capacity \\ 2 = 0
|
||||
local
|
||||
xEnc: SPECIAL [NATURAL_8]
|
||||
yEnc: SPECIAL [NATURAL_8]
|
||||
x_mpz: INTEGER_X
|
||||
y_mpz: INTEGER_X
|
||||
do
|
||||
create xEnc.make_filled (0, source.count // 2)
|
||||
xEnc.copy_data (source, 0, 0, xEnc.count)
|
||||
create yEnc.make_filled (0, source.count // 2)
|
||||
yEnc.copy_data (source, source.count // 2, 0, yEnc.count)
|
||||
check -- Field elements should be same size
|
||||
xEnc.capacity = yEnc.capacity
|
||||
end
|
||||
create x_mpz.make_from_bytes (xEnc, xEnc.lower, xEnc.upper)
|
||||
create y_mpz.make_from_bytes (yEnc, yEnc.lower, yEnc.upper)
|
||||
create x.make (x_mpz)
|
||||
create y.make (y_mpz)
|
||||
end
|
||||
|
||||
to_byte_array_uncompressed (curve: EC_CURVE_F2M): SPECIAL [NATURAL_8]
|
||||
local
|
||||
byteCount: INTEGER_32
|
||||
y_array: SPECIAL [NATURAL_8]
|
||||
x_array: SPECIAL [NATURAL_8]
|
||||
p0: SPECIAL [NATURAL_8]
|
||||
do
|
||||
bytecount := x.x.bytes
|
||||
x_array := x.x.as_bytes
|
||||
y_array := y.x.as_fixed_width_byte_array (byteCount)
|
||||
create p0.make_filled (0, byteCount + byteCount + 1)
|
||||
p0.put (0x04, 0)
|
||||
check
|
||||
x_array.capacity = y_array.capacity
|
||||
end
|
||||
p0.copy_data (x_array, 0, x_array.upper, 1)
|
||||
p0.copy_data (y_array, 0, y_array.upper, x_array.upper + 1)
|
||||
result := p0
|
||||
end
|
||||
|
||||
to_byte_array_compressed (curve: EC_CURVE_F2M): SPECIAL [NATURAL_8]
|
||||
local
|
||||
byteCount: INTEGER_32
|
||||
x_array: SPECIAL [NATURAL_8]
|
||||
P0: SPECIAL [NATURAL_8]
|
||||
do
|
||||
x_array := x.x.as_bytes
|
||||
byteCount := x.x.bytes
|
||||
-- See X9.62 4.3.6 and 4.2.2
|
||||
create P0.make_filled (0, byteCount + 1)
|
||||
p0.put (0x02, 0)
|
||||
|
||||
-- X9.62 4.2.2 and 4.3.6:
|
||||
-- if x = 0 then ypTilde := 0, else ypTilde is the rightmost
|
||||
-- bit of y * x^(-1)
|
||||
-- if ypTilde = 0, then PC := 02, else PC := 03
|
||||
-- Note: PC === PO[0]
|
||||
if
|
||||
(not (x.x.is_zero)) and ((y.product_value (x.inverse_value (curve), curve)).x.bit_test(0))
|
||||
then
|
||||
-- ypTilde = 1, hence PC = 03
|
||||
p0.put (0x03, 0)
|
||||
end
|
||||
p0.copy_data (x_array, 0, x_array.upper, 1)
|
||||
result := p0
|
||||
end
|
||||
|
||||
feature -- Implement ECPOINT
|
||||
|
||||
plus_value (other: like Current; curve: EC_CURVE_F2M): EC_POINT_F2M
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
plus (other: like Current; curve: EC_CURVE_F2M)
|
||||
do
|
||||
if
|
||||
infinity
|
||||
then
|
||||
copy (other)
|
||||
elseif
|
||||
other.infinity
|
||||
then
|
||||
|
||||
else
|
||||
add_not_infinity (other, curve)
|
||||
end
|
||||
end
|
||||
|
||||
minus_value (other: like Current; curve: EC_CURVE_F2M): EC_POINT_F2M
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
minus (other: like Current; curve: EC_CURVE_F2M)
|
||||
do
|
||||
if
|
||||
other.infinity
|
||||
then
|
||||
else
|
||||
add_minus_b (other, curve)
|
||||
end
|
||||
end
|
||||
|
||||
product_value (b: INTEGER_X; curve: EC_CURVE_F2M): EC_POINT_F2M
|
||||
do
|
||||
Result := Precursor (b, curve)
|
||||
end
|
||||
|
||||
product (b: INTEGER_X; curve: EC_CURVE_F2M)
|
||||
local
|
||||
p: like Current
|
||||
q: like Current
|
||||
t: INTEGER_32
|
||||
-- i: INTEGER_32
|
||||
special: SPECIAL [NATURAL_32]
|
||||
limb: NATURAL_32
|
||||
limb_position: INTEGER
|
||||
new_bit_position: INTEGER
|
||||
bit_position: INTEGER
|
||||
do
|
||||
p := Current
|
||||
create q.make_infinity
|
||||
t := b.bits
|
||||
from
|
||||
special := b.item
|
||||
limb := special [limb_position]
|
||||
limb_position := 0
|
||||
bit_position := 0
|
||||
until
|
||||
limb_position * 32 + bit_position >= t
|
||||
loop
|
||||
if limb.bit_test (bit_position) then
|
||||
q.plus (p, curve)
|
||||
end
|
||||
p.twice (curve)
|
||||
new_bit_position := (bit_position + 1) \\ 32
|
||||
if new_bit_position < bit_position then
|
||||
limb_position := limb_position + 1
|
||||
limb := special [limb_position]
|
||||
end
|
||||
bit_position := new_bit_position
|
||||
end
|
||||
-- p := Current
|
||||
-- create q.make_infinity
|
||||
-- t := b.bits
|
||||
-- from
|
||||
-- i := 0
|
||||
-- until
|
||||
-- i = t
|
||||
-- loop
|
||||
-- if
|
||||
-- b.bit_test (i)
|
||||
-- then
|
||||
-- q.plus (p, curve)
|
||||
-- end
|
||||
-- p.twice (curve)
|
||||
-- i := i + 1
|
||||
-- end
|
||||
copy (q)
|
||||
end
|
||||
|
||||
twice_value (curve: EC_CURVE_F2M): EC_POINT_F2M
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
twice (curve: EC_CURVE_F2M)
|
||||
do
|
||||
if
|
||||
infinity
|
||||
then
|
||||
elseif
|
||||
x.x.is_zero
|
||||
then
|
||||
set_infinity
|
||||
else
|
||||
twice_not_infinity (curve)
|
||||
end
|
||||
end
|
||||
|
||||
opposite_value (curve: EC_CURVE_F2M): EC_POINT_F2M
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
opposite (curve: EC_CURVE_F2M)
|
||||
do
|
||||
y.plus (x, curve)
|
||||
end
|
||||
|
||||
feature -- Implementation support features
|
||||
|
||||
twice_not_infinity (curve: EC_CURVE_F2M)
|
||||
local
|
||||
lambda: EC_FIELD_ELEMENT_F2M
|
||||
x3: EC_FIELD_ELEMENT_F2M
|
||||
y3: EC_FIELD_ELEMENT_F2M
|
||||
one_element: EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
create one_element.make (one)
|
||||
lambda := y.quotient_value (x, curve)
|
||||
lambda.plus (x, curve)
|
||||
x3 := lambda.square_value (curve)
|
||||
x3.plus (lambda, curve)
|
||||
x3.plus (curve.a, curve)
|
||||
y3 := x.square_value (curve)
|
||||
lambda.plus (one_element, curve)
|
||||
lambda.product (x3, curve)
|
||||
y3.plus (lambda, curve)
|
||||
x := x3
|
||||
y := y3
|
||||
end
|
||||
|
||||
add_minus_b (other: like Current curve: EC_CURVE_F2M)
|
||||
local
|
||||
minusB: like Current
|
||||
do
|
||||
create minusB.make_curve_x_y (other.x, other.x.plus_value (other.y, curve))
|
||||
plus (minusB, curve)
|
||||
end
|
||||
|
||||
add_not_infinity (other: like Current; curve: EC_CURVE_F2M)
|
||||
do
|
||||
if
|
||||
x ~ other.x
|
||||
then
|
||||
if
|
||||
y ~ other.y
|
||||
then
|
||||
copy (twice_value (curve))
|
||||
else
|
||||
set_infinity
|
||||
end
|
||||
else
|
||||
add_normal (other, curve)
|
||||
end
|
||||
end
|
||||
|
||||
add_normal (other: like Current; curve: EC_CURVE_F2M)
|
||||
local
|
||||
lambda: EC_FIELD_ELEMENT_F2M
|
||||
x3: EC_FIELD_ELEMENT_F2M
|
||||
y3: EC_FIELD_ELEMENT_F2M
|
||||
do
|
||||
lambda := (y.plus_value (other.y, curve)).quotient_value (x.plus_value (other.x, curve), curve)
|
||||
x3 := lambda.square_value (curve)
|
||||
x3 := x3.plus_value (lambda, curve).plus_value (x, curve).plus_value (other.x, curve).plus_value (curve.a, curve)
|
||||
y3 := ((lambda.product_value (x.plus_value (x3, curve), curve)).plus_value (x3, curve)).plus_value (y, curve)
|
||||
x := x3
|
||||
y := y3
|
||||
end
|
||||
end
|
||||
481
library/crypto/eel/ec/ec_point_fp.e
Normal file
481
library/crypto/eel/ec/ec_point_fp.e
Normal file
@@ -0,0 +1,481 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Those who expect to reap the benefits of freedom, must, like men, undergo the fatigue of supporting it. - Thomas Paine"
|
||||
|
||||
class
|
||||
EC_POINT_FP
|
||||
|
||||
inherit
|
||||
EC_POINT
|
||||
redefine
|
||||
x,
|
||||
y,
|
||||
copy,
|
||||
opposite_value,
|
||||
product_value,
|
||||
twice_value,
|
||||
minus_value,
|
||||
plus_value
|
||||
end
|
||||
EC_CONSTANTS
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
STANDARD_CURVES
|
||||
undefine
|
||||
is_equal,
|
||||
copy
|
||||
end
|
||||
|
||||
create
|
||||
make_curve_x_y,
|
||||
make_from_bytes,
|
||||
make_infinity,
|
||||
make_sec_p112r1,
|
||||
make_sec_p112r2,
|
||||
make_sec_p128r1,
|
||||
make_sec_p128r2,
|
||||
make_sec_p160k1,
|
||||
make_sec_p160r1,
|
||||
make_sec_p160r2,
|
||||
make_sec_p192k1,
|
||||
make_sec_p192r1,
|
||||
make_sec_p224k1,
|
||||
make_sec_p224r1,
|
||||
make_sec_p256k1,
|
||||
make_sec_p256r1,
|
||||
make_sec_p384r1,
|
||||
make_sec_p521r1,
|
||||
make_p192,
|
||||
make_p224,
|
||||
make_p256,
|
||||
make_p384,
|
||||
make_p521
|
||||
|
||||
feature
|
||||
make_infinity
|
||||
do
|
||||
set_infinity
|
||||
end
|
||||
|
||||
feature -- SEC curves
|
||||
make_sec_p112r1
|
||||
do
|
||||
create x.make_p_x (sec_p112r1_gx)
|
||||
create y.make_p_x (sec_p112r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p112r2
|
||||
do
|
||||
create x.make_p_x (sec_p112r2_gx)
|
||||
create y.make_p_x (sec_p112r2_gy)
|
||||
end
|
||||
|
||||
make_sec_p128r1
|
||||
do
|
||||
create x.make_p_x (sec_p128r1_gx)
|
||||
create y.make_p_x (sec_p128r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p128r2
|
||||
do
|
||||
create x.make_p_x (sec_p128r2_gx)
|
||||
create y.make_p_x (sec_p128r2_gy)
|
||||
end
|
||||
|
||||
make_sec_p160k1
|
||||
do
|
||||
create x.make_p_x (sec_p160k1_gx)
|
||||
create y.make_p_x (sec_p160k1_gy)
|
||||
end
|
||||
|
||||
make_sec_p160r1
|
||||
do
|
||||
create x.make_p_x (sec_p160r1_gx)
|
||||
create y.make_p_x (sec_p160r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p160r2
|
||||
do
|
||||
create x.make_p_x (sec_p160r2_gx)
|
||||
create y.make_p_x (sec_p160r2_gy)
|
||||
end
|
||||
|
||||
make_sec_p192k1
|
||||
do
|
||||
create x.make_p_x (sec_p192k1_gx)
|
||||
create y.make_p_x (sec_p192k1_gy)
|
||||
end
|
||||
|
||||
make_sec_p192r1
|
||||
do
|
||||
create x.make_p_x (sec_p192r1_gx)
|
||||
create y.make_p_x (sec_p192r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p224k1
|
||||
do
|
||||
create x.make_p_x (sec_p224k1_gx)
|
||||
create y.make_p_x (sec_p224k1_gy)
|
||||
end
|
||||
|
||||
make_sec_p224r1
|
||||
do
|
||||
create x.make_p_x (sec_p224r1_gx)
|
||||
create y.make_p_x (sec_p224r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p256k1
|
||||
do
|
||||
create x.make_p_x (sec_p256k1_gx)
|
||||
create y.make_p_x (sec_p256k1_gy)
|
||||
end
|
||||
|
||||
make_sec_p256r1
|
||||
do
|
||||
create x.make_p_x (sec_p256r1_gx)
|
||||
create y.make_p_x (sec_p256r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p384r1
|
||||
do
|
||||
create x.make_p_x (sec_p384r1_gx)
|
||||
create y.make_p_x (sec_p384r1_gy)
|
||||
end
|
||||
|
||||
make_sec_p521r1
|
||||
do
|
||||
create x.make_p_x (sec_p521r1_gx)
|
||||
create y.make_p_x (sec_p521r1_gy)
|
||||
end
|
||||
|
||||
feature
|
||||
make_p192
|
||||
do
|
||||
create x.make_p_x (p192_gx)
|
||||
create y.make_p_x (p192_gy)
|
||||
end
|
||||
|
||||
make_p224
|
||||
do
|
||||
create x.make_p_x (p224_gx)
|
||||
create y.make_p_x (p224_gy)
|
||||
end
|
||||
|
||||
make_p256
|
||||
do
|
||||
create x.make_p_x (p256_gx)
|
||||
create y.make_p_x (p256_gy)
|
||||
end
|
||||
|
||||
make_p384
|
||||
do
|
||||
create x.make_p_x (p384_gx)
|
||||
create y.make_p_x (p384_gy)
|
||||
end
|
||||
|
||||
make_p521
|
||||
do
|
||||
create x.make_p_x (p521_gx)
|
||||
create y.make_p_x (p521_gy)
|
||||
end
|
||||
|
||||
make_curve_x_y (x_a: EC_FIELD_ELEMENT_FP; y_a: EC_FIELD_ELEMENT_FP)
|
||||
do
|
||||
x := x_a
|
||||
y := y_a
|
||||
end
|
||||
|
||||
make_from_bytes (encoded: SPECIAL [NATURAL_8] curve: EC_CURVE_FP)
|
||||
-- Decode a point on this curve from its ASN.1 encoding
|
||||
-- encodings are taken account of, including point compression for
|
||||
-- <code>F<sub>p</sub><code> (X9.62 s 4.2.1 pg 17).
|
||||
-- @return The decoded point.
|
||||
require
|
||||
first_byte_indicator: encoded [0] = 0x02 or encoded [0] = 0x3 or encoded [0] = 0x4
|
||||
do
|
||||
inspect
|
||||
encoded [0]
|
||||
when 0x02 then
|
||||
decodeCompressedPoint (encoded, 0, curve)
|
||||
when 0x03 then
|
||||
decodeCompressedPoint (encoded, 1, curve)
|
||||
when 0x04 then
|
||||
decodeUncompressedPoint (encoded)
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
x: EC_FIELD_ELEMENT_FP
|
||||
y: EC_FIELD_ELEMENT_FP
|
||||
|
||||
copy (other: like Current)
|
||||
do
|
||||
x.copy (other.x)
|
||||
y.copy (other.y)
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
set_infinity
|
||||
do
|
||||
create x.make_p_x (create {INTEGER_X}.default_create)
|
||||
create y.make_p_x (create {INTEGER_X}.default_create)
|
||||
infinity := True
|
||||
end
|
||||
|
||||
to_byte_array_compressed (curve: EC_CURVE_FP): SPECIAL [NATURAL_8]
|
||||
-- Return a compressed encoded version of this point
|
||||
local
|
||||
x_array: SPECIAL [NATURAL_8]
|
||||
do
|
||||
x_array := x.x.as_fixed_width_byte_array (x.encoded_field_size (curve))
|
||||
create result.make_filled (0, x_array.count + 1)
|
||||
result.copy_data (x_array, 0, 1, x_array.count)
|
||||
result [0] := compressed_PC_byte (y.x)
|
||||
end
|
||||
|
||||
to_byte_array_uncompressed (curve: EC_CURVE_FP): SPECIAL [NATURAL_8]
|
||||
-- Return an uncompressed encoded version of this point
|
||||
local
|
||||
x_array: SPECIAL [NATURAL_8]
|
||||
y_array: SPECIAL [NATURAL_8]
|
||||
p0: SPECIAL [NATURAL_8]
|
||||
qLength: INTEGER_32
|
||||
do
|
||||
qLength := x.encoded_field_size (curve)
|
||||
x_array := x.x.as_fixed_width_byte_array (qlength)
|
||||
y_array := y.x.as_fixed_width_byte_array (qLength)
|
||||
check
|
||||
x_array.capacity = qlength
|
||||
y_array.capacity = qlength
|
||||
end
|
||||
create p0.make_filled (0, x_array.capacity + y_array.capacity + 1)
|
||||
p0.copy_data (x_array, 0, x_array.upper, 1)
|
||||
p0.copy_data (y_array, 0, y_array.upper, x_array.capacity + 1)
|
||||
p0.put (0x04, 0)
|
||||
result := p0
|
||||
end
|
||||
|
||||
plus_value (other: like Current; curve: EC_CURVE_FP): EC_POINT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
plus (other: like Current; curve: EC_CURVE_FP)
|
||||
-- Addition over FP
|
||||
local
|
||||
gamma: EC_FIELD_ELEMENT_FP
|
||||
x3: EC_FIELD_ELEMENT_FP
|
||||
y3: EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
if
|
||||
infinity
|
||||
then
|
||||
copy (other)
|
||||
elseif
|
||||
other.infinity
|
||||
then
|
||||
elseif
|
||||
x ~ other.x
|
||||
then
|
||||
if
|
||||
y ~ other.y
|
||||
then
|
||||
copy (twice_value (curve))
|
||||
else
|
||||
set_infinity
|
||||
end
|
||||
else
|
||||
gamma := (other.y.minus_value (y, curve)).quotient_value (other.x.minus_value (x, curve), curve)
|
||||
x3 := (gamma.product_value (gamma, curve)).minus_value (x, curve).minus_value (other.x, curve)
|
||||
y3 := (gamma.product_value (x.minus_value (x3, curve), curve)).minus_value (y, curve)
|
||||
x := x3
|
||||
y := y3
|
||||
end
|
||||
end
|
||||
|
||||
twice_value (curve: EC_CURVE_FP): EC_POINT_FP
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
twice (curve: EC_CURVE_FP)
|
||||
-- Return current * current over FP
|
||||
local
|
||||
two_element: EC_FIELD_ELEMENT_FP
|
||||
three_element: EC_FIELD_ELEMENT_FP
|
||||
gamma: EC_FIELD_ELEMENT_FP
|
||||
x3: EC_FIELD_ELEMENT_FP
|
||||
y3: EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
if
|
||||
infinity
|
||||
then
|
||||
elseif
|
||||
y.x.is_zero
|
||||
then
|
||||
set_infinity
|
||||
else
|
||||
create two_element.make_p_x (two)
|
||||
create three_element.make_p_x (three)
|
||||
gamma := (((x.product_value (x, curve)).product_value (three_element, curve)).plus_value (curve.a, curve)).quotient_value (y.product_value (two_element, curve), curve)
|
||||
x3 := (gamma.product_value (gamma, curve)).minus_value (x.product_value (two_element, curve), curve)
|
||||
y3 := (gamma.product_value (x.minus_value (x3, curve), curve)).minus_value (y, curve)
|
||||
x := x3
|
||||
y := y3
|
||||
end
|
||||
end
|
||||
|
||||
minus_value (other: like Current; curve: EC_CURVE_FP): EC_POINT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
minus (other: like Current; curve: EC_CURVE_FP)
|
||||
do
|
||||
if
|
||||
other.infinity
|
||||
then
|
||||
else
|
||||
plus (other.opposite_value (curve), curve)
|
||||
end
|
||||
end
|
||||
|
||||
product_value (other: INTEGER_X; curve: EC_CURVE_FP): EC_POINT_FP
|
||||
do
|
||||
Result := Precursor (other, curve)
|
||||
end
|
||||
|
||||
product (other: INTEGER_X; curve: EC_CURVE_FP)
|
||||
-- return current * k over FP
|
||||
local
|
||||
e: INTEGER_X
|
||||
h: INTEGER_X
|
||||
R: like Current
|
||||
i: INTEGER_32
|
||||
do
|
||||
if
|
||||
infinity
|
||||
then
|
||||
elseif
|
||||
other.is_zero
|
||||
then
|
||||
set_infinity
|
||||
else
|
||||
e := other
|
||||
h := e * three
|
||||
R := deep_twin
|
||||
from
|
||||
i := (h.bits - 2)
|
||||
until
|
||||
i <= 0
|
||||
loop
|
||||
R := r.twice_value (curve)
|
||||
if
|
||||
h.bit_test (i) and not e.bit_test (i)
|
||||
then
|
||||
r := r.plus_value (Current, curve)
|
||||
elseif
|
||||
not h.bit_test (i) and e.bit_test (i)
|
||||
then
|
||||
r := r.minus_value (Current, curve)
|
||||
end
|
||||
i := i - 1
|
||||
end
|
||||
copy (r)
|
||||
end
|
||||
end
|
||||
|
||||
opposite_value (curve: EC_CURVE_FP): like Current
|
||||
do
|
||||
Result := Precursor (curve)
|
||||
end
|
||||
|
||||
opposite (curve: EC_CURVE_FP)
|
||||
do
|
||||
y.opposite (curve)
|
||||
end
|
||||
|
||||
feature {NONE} -- support features
|
||||
ytilde_set (source: INTEGER_X): BOOLEAN
|
||||
-- Test the least significant bit, this is ytilde
|
||||
-- X9.62 4.2.1
|
||||
do
|
||||
result := source.bit_test (0)
|
||||
end
|
||||
|
||||
compressed_PC_byte (source: INTEGER_X): NATURAL_8
|
||||
-- Return the PC byte depending on if ytilde is set
|
||||
-- X9.62 4.3.6
|
||||
do
|
||||
if
|
||||
ytilde_set (source)
|
||||
then
|
||||
result := 0x03
|
||||
else
|
||||
result := 0x02
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE}
|
||||
decodeCompressedPoint (encoded: SPECIAL [NATURAL_8] ytilde: INTEGER curve: EC_CURVE_FP)
|
||||
-- Decode a compressed point
|
||||
require
|
||||
encoded.lower = 0
|
||||
local
|
||||
i: SPECIAL [NATURAL_8]
|
||||
x_new: EC_FIELD_ELEMENT_FP
|
||||
alpha: EC_FIELD_ELEMENT_FP
|
||||
beta: EC_FIELD_ELEMENT_FP
|
||||
x_int: INTEGER_X
|
||||
bit0: INTEGER
|
||||
q_minus_beta: EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
create i.make_filled (0, encoded.count - 1)
|
||||
i.copy_data (encoded, 1, 0, i.count)
|
||||
create x_int.make_from_bytes (i, i.lower, i.upper)
|
||||
create x_new.make_p_x (x_int)
|
||||
alpha := (x_new.product_value (x_new.square_value (curve).plus_value (curve.a, curve), curve)).plus_value (curve.b, curve)
|
||||
beta := alpha.sqrt (curve)
|
||||
if
|
||||
beta.x.bit_test (0)
|
||||
then
|
||||
bit0 := 1
|
||||
else
|
||||
bit0 := 0
|
||||
end
|
||||
if
|
||||
bit0 = ytilde
|
||||
then
|
||||
make_curve_x_y (x_new, beta)
|
||||
else
|
||||
create q_minus_beta.make_p_x (curve.q - beta.x)
|
||||
make_curve_x_y (x_new, q_minus_beta)
|
||||
end
|
||||
end
|
||||
|
||||
decodeUncompressedPoint (encoded: SPECIAL [NATURAL_8])
|
||||
-- Decode an uncompressed point
|
||||
require
|
||||
encoded_not_split_even: (encoded.count \\ 2) = 1
|
||||
local
|
||||
xEnc: SPECIAL [NATURAL_8]
|
||||
yEnc: SPECIAL [NATURAL_8]
|
||||
x_new: EC_FIELD_ELEMENT_FP
|
||||
y_new: EC_FIELD_ELEMENT_FP
|
||||
do
|
||||
create xEnc.make_filled (0, (encoded.capacity - 1) // 2)
|
||||
create yEnc.make_filled (0, (encoded.capacity - 1) // 2)
|
||||
encoded.copy_data (xEnc, 1, 0, xEnc.capacity)
|
||||
encoded.copy_data (yEnc, xEnc.capacity, 0, yEnc.capacity)
|
||||
create x_new.make_p_x (create {INTEGER_X}.make_from_bytes (xEnc, xEnc.lower, xEnc.upper))
|
||||
create y_new.make_p_x (create {INTEGER_X}.make_from_bytes (yEnc, yEnc.lower, yEnc.upper))
|
||||
x := x_new
|
||||
y := y_new
|
||||
end
|
||||
end
|
||||
89
library/crypto/eel/ec/ec_private_key.e
Normal file
89
library/crypto/eel/ec/ec_private_key.e
Normal file
@@ -0,0 +1,89 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "Liberty is always dangerous, but it is the safest thing we have. - Harry Emerson Fosdick"
|
||||
|
||||
class
|
||||
EC_PRIVATE_KEY
|
||||
|
||||
inherit
|
||||
EC_KEY_PARAMETERS
|
||||
DEBUG_OUTPUT
|
||||
EC_CONSTANTS
|
||||
|
||||
create
|
||||
make_d_params
|
||||
|
||||
feature
|
||||
make_d_params (d_new: INTEGER_X params_new: EC_DOMAIN_PARAMETERS)
|
||||
do
|
||||
params := params_new
|
||||
d := d_new
|
||||
end
|
||||
|
||||
agreement (other: EC_PUBLIC_KEY): INTEGER_X
|
||||
do
|
||||
result := (other.q.product_value (d, params.curve)).x.x
|
||||
ensure
|
||||
symmetric: result ~ other.agreement (current)
|
||||
end
|
||||
|
||||
sign (e: INTEGER_X): TUPLE [r: INTEGER_X s: INTEGER_X]
|
||||
require
|
||||
message_too_big: e < params.n
|
||||
local
|
||||
r: INTEGER_X
|
||||
s: INTEGER_X
|
||||
k: INTEGER_X
|
||||
nBitLength: INTEGER_32
|
||||
p: EC_POINT
|
||||
x: INTEGER_X
|
||||
n: INTEGER_X
|
||||
do
|
||||
n := params.n
|
||||
create s.default_create
|
||||
create r.default_create
|
||||
create k.default_create
|
||||
nBitLength := params.n.bits
|
||||
from
|
||||
until
|
||||
s /~ s.zero
|
||||
loop
|
||||
from
|
||||
until
|
||||
r /~ r.zero
|
||||
loop
|
||||
from
|
||||
until
|
||||
k /~ k.zero
|
||||
loop
|
||||
create k.make_random (nBitLength)
|
||||
end
|
||||
p := params.g.product_value (k, params.curve)
|
||||
x := p.x.x
|
||||
r := x \\ params.n
|
||||
end
|
||||
--s := ((k.inverse_value (params.n) * (e + d * r))) \\ params.n
|
||||
s := d.identity
|
||||
s.product (r)
|
||||
s.plus (e)
|
||||
k.inverse (n)
|
||||
s.product (k)
|
||||
s.modulo (n)
|
||||
end
|
||||
create result
|
||||
result.r := r
|
||||
result.s := s
|
||||
end
|
||||
|
||||
feature
|
||||
d: INTEGER_X
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := "0x" + d.out_hex
|
||||
end
|
||||
end
|
||||
74
library/crypto/eel/ec/ec_public_key.e
Normal file
74
library/crypto/eel/ec/ec_public_key.e
Normal file
@@ -0,0 +1,74 @@
|
||||
note
|
||||
description: "Objects that ..."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "It is much more important to kill bad bills than to pass good ones. - Calvin Coolidge"
|
||||
|
||||
class
|
||||
EC_PUBLIC_KEY
|
||||
|
||||
inherit
|
||||
EC_KEY_PARAMETERS
|
||||
DEBUG_OUTPUT
|
||||
EC_CONSTANTS
|
||||
|
||||
create
|
||||
make_q_parameters
|
||||
|
||||
feature -- Creation procedures
|
||||
make_q_parameters (q_new: EC_POINT params_new: EC_DOMAIN_PARAMETERS)
|
||||
do
|
||||
params := params_new
|
||||
q := q_new
|
||||
end
|
||||
|
||||
agreement (other: EC_PRIVATE_KEY): INTEGER_X
|
||||
do
|
||||
Result := (q.product_value (other.d, params.curve)).x.x
|
||||
ensure
|
||||
symmetric: Result ~ other.agreement (Current)
|
||||
end
|
||||
|
||||
verify (message: INTEGER_X signature: TUPLE [r: INTEGER_X s: INTEGER_X]): BOOLEAN
|
||||
do
|
||||
result := verify_r_s (message, signature.r, signature.s, params.curve)
|
||||
end
|
||||
|
||||
verify_r_s (e: INTEGER_X r: INTEGER_X s: INTEGER_X curve: EC_CURVE): BOOLEAN
|
||||
require
|
||||
message_small_enough: e < params.n
|
||||
local
|
||||
c: INTEGER_X
|
||||
u1: INTEGER_X
|
||||
u2: INTEGER_X
|
||||
point: EC_POINT
|
||||
v: INTEGER_X
|
||||
do
|
||||
if
|
||||
(r < r.one) or (r >= params.n)
|
||||
then
|
||||
result := false
|
||||
elseif
|
||||
(s < s.one) or (s >= params.n)
|
||||
then
|
||||
result := false
|
||||
else
|
||||
c := s.inverse_value (params.n)
|
||||
u1 := e * c \\ params.n
|
||||
u2 := r * c \\ params.n
|
||||
point := (params.g.product_value (u1, params.curve)).plus_value (q.product_value (u2, params.curve), params.curve)
|
||||
v := point.x.x \\ params.n
|
||||
result := v ~ r
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
q: EC_POINT
|
||||
|
||||
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
|
||||
debug_output: STRING
|
||||
do
|
||||
result := q.debug_output
|
||||
end
|
||||
end
|
||||
18
library/crypto/eel/ec/f2m_representations.e
Normal file
18
library/crypto/eel/ec/f2m_representations.e
Normal file
@@ -0,0 +1,18 @@
|
||||
note
|
||||
description: "Summary description for {F2M_REPRESENTATIONS}."
|
||||
author: "Colin LeMahieu"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
quote: "To compel a man to furnish contributions of money for the propagation of opinions which he disbelieves and abhors, is sinful and tyrannical. - Thomas Jefferson"
|
||||
|
||||
deferred class
|
||||
F2M_REPRESENTATIONS
|
||||
|
||||
feature -- Field element representations
|
||||
GNB: INTEGER = 1
|
||||
|
||||
TPB: INTEGER = 2
|
||||
|
||||
PPB: INTEGER = 3
|
||||
|
||||
end
|
||||
1807
library/crypto/eel/ec/standard_curves.e
Normal file
1807
library/crypto/eel/ec/standard_curves.e
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user