Skip to content

Commit a77605a

Browse files
committed
Test the Elligator Squared mapping functions
- source: src/modules/ellsq/tests_impl.h from bitcoin-core/secp256k1#982 - 3 tests are added: 1. Generate random field elements and use f to map it to a valid group element on the curve. Then use r to map back the group element to the 4 possible pre-images, out of which only 1 is the field element we started with. 2. Generate random group elements on the curve and use r to map it to the 4 possible pre-images. Then map the field elements back to the group element and check if it's the same group element we started with, also making sure that the pre-images are distinct. 3. Verify the test cases which consists of group element and the 4 field elements.Map the group element to the 4 possible pre-images using r and check whether it's consistent with the 4 field elements given in the test case. Map the field element back to the group element using f and check whether it matches the test case.
1 parent 0410b05 commit a77605a

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

test/functional/test_framework/ellsq.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
keys, and is trivially vulnerable to side channel attacks. Do not use for
66
anything but tests."""
77

8-
from .key import fe
8+
import random
9+
import unittest
10+
11+
from .key import fe, SECP256K1, SECP256K1_G, SECP256K1_ORDER
912

1013
C1 = fe(-3).sqrt()
1114
C2 = (C1 - fe(1)) / fe(2)
@@ -120,3 +123,42 @@ def r(x,y,i):
120123
[(fe(0xd09a4047f158fe52f96c661d02c68657c4c976ea96ea85ef46d6985bd540756b), fe(0xe793bfaae9300f18e6f9b55aae26322368b61d51ae5022efe266c72d574178bc)), [fe(0x7e6175fdfbb9fb4faf6e2b925ef86c4a444d819aaa82dbee545d3d9b296375be), None , None , None ]],
121124
[(fe(0x3498662504b73c7c8cecb6c33cd493bdfc190e0f87d913d7ff9ad42e222bfe95), fe(0x245b3a61b8d46997f14f2fea2874899691eb32542b9907d65eb9d21d42454021)), [fe(0x7f556282c3dd9d263390d6bbddada698ab8fd7c7d1a06498f42b30437c8361ad), None , None , None ]]
122125
]
126+
127+
class TestFrameworkEllsq(unittest.TestCase):
128+
def test_fe_to_ge_to_fe(self):
129+
for i in range(100):
130+
matches = 0
131+
t = fe(random.randrange(1, SECP256K1_ORDER))
132+
ge = f(t)
133+
jac_ge = (ge[0].val, ge[1].val, 1)
134+
assert(SECP256K1.on_curve(jac_ge))
135+
# t should appear exactly once in preimages
136+
for j in range(4):
137+
field_ele = r(ge[0], ge[1], j)
138+
if field_ele is not None:
139+
matches += (field_ele == t)
140+
assert(matches == 1)
141+
142+
def test_ge_to_fe_to_ge(self):
143+
for i in range(100):
144+
m = random.randrange(1, SECP256K1_ORDER)
145+
A = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, m)]))
146+
ge = (fe(A[0]), fe(A[1]))
147+
preimages = []
148+
for j in range(4):
149+
field_ele = r(ge[0], ge[1], j)
150+
if field_ele is not None:
151+
preimages.append(field_ele)
152+
group_ele = f(field_ele)
153+
assert (ge == group_ele)
154+
assert len(set(preimages)) == len(preimages)
155+
156+
def test_ellsq_mapping(self):
157+
for test_vector in ELLSQ_TESTS:
158+
ge, fe = test_vector
159+
for j, fe1 in enumerate(fe):
160+
fe2 = r(ge[0], ge[1], j)
161+
assert(fe1 == fe2)
162+
if fe2 is not None:
163+
group_ele = f(fe2)
164+
assert (ge == group_ele)

0 commit comments

Comments
 (0)