Skip to content

Commit 4a63d30

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 3382716 commit 4a63d30

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

test/functional/test_framework/ellsq.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
WARNING: This code is slow and uses bad randomness.
55
Do not use for anything but tests."""
66

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

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

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
TEST_FRAMEWORK_MODULES = [
7171
"address",
7272
"blocktools",
73+
"ellsq",
7374
"muhash",
7475
"key",
7576
"script",

0 commit comments

Comments
 (0)