Skip to content

Commit 692ef04

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 2b621b5 commit 692ef04

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)
@@ -137,3 +140,42 @@ def reverse_map(x, y, i):
137140
[(fe(0xd09a4047f158fe52f96c661d02c68657c4c976ea96ea85ef46d6985bd540756b), fe(0xe793bfaae9300f18e6f9b55aae26322368b61d51ae5022efe266c72d574178bc)), [fe(0x7e6175fdfbb9fb4faf6e2b925ef86c4a444d819aaa82dbee545d3d9b296375be), None , None , None ]],
138141
[(fe(0x3498662504b73c7c8cecb6c33cd493bdfc190e0f87d913d7ff9ad42e222bfe95), fe(0x245b3a61b8d46997f14f2fea2874899691eb32542b9907d65eb9d21d42454021)), [fe(0x7f556282c3dd9d263390d6bbddada698ab8fd7c7d1a06498f42b30437c8361ad), None , None , None ]]
139142
]
143+
144+
class TestFrameworkEllsq(unittest.TestCase):
145+
def test_fe_to_ge_to_fe(self):
146+
for i in range(100):
147+
matches = 0
148+
t = fe(random.randrange(1, SECP256K1_ORDER))
149+
ge = forward_map(t)
150+
jac_ge = ge[0].val, ge[1].val, 1
151+
assert SECP256K1.on_curve(jac_ge)
152+
# t should appear exactly once in preimages
153+
for j in range(4):
154+
field_ele = reverse_map(ge[0], ge[1], j)
155+
if field_ele is not None:
156+
matches += (field_ele == t)
157+
assert matches == 1
158+
159+
def test_ge_to_fe_to_ge(self):
160+
for i in range(100):
161+
m = random.randrange(1, SECP256K1_ORDER)
162+
A = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, m)]))
163+
ge = fe(A[0]), fe(A[1])
164+
preimages = []
165+
for j in range(4):
166+
field_ele = reverse_map(ge[0], ge[1], j)
167+
if field_ele is not None:
168+
preimages.append(field_ele)
169+
group_ele = forward_map(field_ele)
170+
assert ge == group_ele
171+
assert len(set(preimages)) == len(preimages)
172+
173+
def test_ellsq_mapping(self):
174+
for test_vector in ELLSQ_TESTS:
175+
ge, fes = test_vector
176+
for j, fe1 in enumerate(fes):
177+
fe2 = reverse_map(ge[0], ge[1], j)
178+
assert fe1 == fe2
179+
if fe2 is not None:
180+
group_ele = forward_map(fe2)
181+
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)