forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class for field elements and ellsq mapping functions
- source: https://github.com/sipa/writeups/tree/main/elligator-square-for-bn - f maps maps every field element to a curve point - r is a partial reverse function which can map a curve point to a field element
- Loading branch information
1 parent
1a32739
commit 64d4a89
Showing
2 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Source: https://github.com/sipa/writeups/tree/main/elligator-square-for-bn | ||
"""Test-only Elligator Squared implementation | ||
WARNING: This code is slow, uses bad randomness, does not properly protect | ||
keys, and is trivially vulnerable to side channel attacks. Do not use for | ||
anything but tests.""" | ||
|
||
from .key import fe | ||
|
||
C1 = fe(-3).sqrt() | ||
C2 = (C1 - fe(1)) / fe(2) | ||
B = fe(7) | ||
|
||
def f(u): | ||
"""Forward mapping function""" | ||
s = u**2 | ||
x1 = C2 - C1*s / (fe(1)+B+s) | ||
g1 = x1**3 + B | ||
if g1.is_square(): | ||
x, g = x1, g1 | ||
else: | ||
x2 = -x1 - fe(1) | ||
g2 = x2**3 + B | ||
if g2.is_square(): | ||
x, g = x2, g2 | ||
else: | ||
x3 = fe(1) - (fe(1)+B+s)**2 / (fe(3)*s) | ||
g3 = x3**3 + B | ||
x, g = x3, g3 | ||
y = g.sqrt() | ||
if y.is_odd() == u.is_odd(): | ||
return (x, y) | ||
else: | ||
return (x, -y) | ||
|
||
def r(x,y,i): | ||
"""Reverse mapping function""" | ||
if i == 0 or i == 1: | ||
z = fe(2)*x + fe(1) | ||
t1 = C1 - z | ||
t2 = C1 + z | ||
if not (t1*t2).is_square(): | ||
return None | ||
if i == 0: | ||
if t2 == fe(0): | ||
return None | ||
if t1 == fe(0) and y.is_odd(): | ||
return None | ||
u = ((fe(1)+B)*t1/t2).sqrt() | ||
else: | ||
x1 = -x-fe(1) | ||
if (x1**3 + B).is_square(): | ||
return None | ||
u = ((fe(1)+B)*t2/t1).sqrt() | ||
else: | ||
z = fe(2) - fe(4)*B - fe(6)*x | ||
if not (z**2 - fe(16)*(B+fe(1))**2).is_square(): | ||
return None | ||
if i == 2: | ||
s = (z + (z**2 - fe(16)*(B+fe(1))**2).sqrt()) / fe(4) | ||
else: | ||
if z**2 == fe(16)*(B+fe(1))**2: | ||
return None | ||
s = (z - (z**2 - fe(16)*(B+fe(1))**2).sqrt()) / fe(4) | ||
if not s.is_square(): | ||
return None | ||
x1 = C2 - C1*s / (fe(1)+B+s) | ||
if (x1**3 + B).is_square(): | ||
return None | ||
u = s.sqrt() | ||
if y.is_odd() == u.is_odd(): | ||
return u | ||
else: | ||
return -u |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters