Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sage: Overhaul SWU script #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions sage/shallue_van_de_woestijne.sage
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
### See C function `shallue_van_de_woestijne`

### http://www.di.ens.fr/~fouque/pub/latincrypt12.pdf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

load("secp256k1_params.sage")

# Parameters for secp256k1
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
a = 0
b = 7
F = FiniteField (p)
C = EllipticCurve ([F(a), F(b)])
b = F(7)
c = F(-3).nth_root(2)
d = (c - 1) / 2

def svdw(t):
sqrt_neg_3 = F(-3).nth_root(2)

## Compute candidate x values
w = sqrt_neg_3 * t / (1 + b + t^2)
w = c * t / (1 + b + t^2)
x = [ F(0), F(0), F(0) ]
x[0] = (-1 + sqrt_neg_3) / 2 - t * w
x[1] = -1 - x[0]
x[2] = 1 + 1 / w^2
x[0] = d - t * w
x[1] = -(x[0] + 1)
x[2] = 1 + (1 / w^2 if w != 0 else 0)

print
print "On %2d" % t
print " x1 %064x" % x[0]
print " x2 %064x" % x[1]
print " x3 %064x" % x[2]
# print()
# print("On %2d" % t)
# print(" x1 %064x" % x[0])
# print(" x2 %064x" % x[1])
#print(" x3 %064x" % x[2])

## Select which to use
alph = jacobi_symbol(x[0]^3 + b, p)
beta = jacobi_symbol(x[1]^3 + b, p)
alph = jacobi_symbol(x[0]^3 + b, P)
beta = jacobi_symbol(x[1]^3 + b, P)
if alph == 1 and beta == 1:
i = 0
elif alph == 1 and beta == -1:
Expand All @@ -36,16 +32,39 @@ def svdw(t):
elif alph == -1 and beta == -1:
i = 2
else:
print "Help! I don't understand Python!"
assert False

## Expand to full point
sign = 1 - 2 * (int(F(t)) % 2)
ret_x = x[i]
ret_y = sign * F(x[i]^3 + b).nth_root(2)
return C.point((ret_x, ret_y))

def print_fe_const(f):
print(f"SECP256K1_FE_CONST(", end="")
print((7 * "0x%08x, " + "0x%08x") % tuple((int(f) >> (32 * (7 - (i % 8)))) & 0xffffffff for i in range(8)), end="")
print(")", end="")

def print_ge_storage_const(g):
print(f"SECP256K1_GE_STORAGE_CONST(", end="")
print((15 * "0x%08x, " + "0x%08x") % tuple((int(g[int(i // 8)]) >> (32 * (7 - (i % 8)))) & 0xffffffff for i in range(16)), end="")
print(")", end="")

## main
for i in range(1, 11):
res = svdw(i)
print "Result: %064x %064x" % res.xy()
print("secp256k1_fe negc = ", end="")
print_fe_const(-c)
print(";")

print("secp256k1_fe d = ", end="")
print_fe_const(d)
print(";")

print()

print("secp256k1_ge_storage results[34] = {")
for i in range(0, 17):
for s in [1, -1]:
res = svdw(s*i)
print_ge_storage_const(res)
print(",")
print("}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print("};")

Loading