-
Notifications
You must be signed in to change notification settings - Fork 6
/
addchain_sqrt.py
executable file
·140 lines (119 loc) · 3.81 KB
/
addchain_sqrt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
# Addition chains needed for calculation of square roots on the Pasta fields:
#
# r = (m_p - 1)/2 for p = 1 + m_p * 2^32
# s = (m_q - 1)/2 for q = 1 + m_q * 2^32
class Chain(object):
SQR_COST = 0.8
MUL_COST = 1
def __init__(self):
self.muls = 0
self.sqrs = 0
self.computed = set((1,))
def sqr(self, x, n=1):
for i in range(n):
assert(x in self.computed)
self.sqrs += 1
x = 2*x
self.computed.add(x)
return x
def mul(self, x, y):
assert(x in self.computed)
assert(y in self.computed)
assert(x != y)
self.muls += 1
r = x+y
self.computed.add(r)
return r
def sqrmul(self, x, n, y):
return self.mul(self.sqr(x, n), y)
def cost(self):
return self.sqrs*self.SQR_COST + self.muls*self.MUL_COST
def __repr__(self):
return "%dS + %dM (%.1f)" % (self.sqrs, self.muls, self.cost())
p = 0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001
q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
n = 32
assert(p % (1<<n) == 1)
assert(q % (1<<n) == 1)
# print(format(p >> (n+1), 'b'))
r = (1<<221) + 0b100010010001101001100011111100000010010100110011111001000110111001100100101101001100001110110
# a b c d e f g h i j k l m n o p q r st
assert(r == p >> (n+1))
rch = Chain()
r1 = 1
r10 = rch.sqr(r1)
r11 = rch.mul(r10, r1)
r110 = rch.sqr(r11)
r111 = rch.mul(r110, r1)
r1001 = rch.mul(r111, r10)
r1101 = rch.mul(r111, r110)
ra = rch.sqrmul(r1, 129, r1)
rb = rch.sqrmul(ra, 7, r1001)
rc = rch.sqrmul(rb, 7, r1101)
rd = rch.sqrmul(rc, 4, r11)
re = rch.sqrmul(rd, 6, r111)
rf = rch.sqrmul(re, 3, r111)
rg = rch.sqrmul(rf, 10, r1001)
rh = rch.sqrmul(rg, 5, r1001)
ri = rch.sqrmul(rh, 4, r1001)
rj = rch.sqrmul(ri, 3, r111)
rk = rch.sqrmul(rj, 4, r1001)
rl = rch.sqrmul(rk, 5, r11)
rm = rch.sqrmul(rl, 4, r111)
rn = rch.sqrmul(rm, 4, r11)
ro = rch.sqrmul(rn, 6, r1001)
rp = rch.sqrmul(ro, 5, r1101)
rq = rch.sqrmul(rp, 4, r11)
rr = rch.sqrmul(rq, 7, r111)
rs = rch.sqrmul(rr, 3, r11)
rt = rch.sqr(rs)
assert rt == r, format(rt, 'b')
print(rch)
# print(format(q >> (n+1), 'b'))
s = (1<<221) + 0b100010010001101001100011111100000010011001010010101000110111011000110001000110111010110010000
# a b c d e f g h i j k l m n o p q r s t
# 1001
# 1001
assert(s == q >> (n+1))
sch = Chain()
s1 = 1
s10 = sch.sqr(s1)
s11 = sch.mul(s10, s1)
s111 = sch.sqrmul(s11, 1, s1)
s1001 = sch.mul(s111, s10)
s1011 = sch.mul(s1001, s10)
s1101 = sch.mul(s1011, s10)
sa = sch.sqrmul(s1, 129, s1)
sb = sch.sqrmul(sa, 7, s1001)
sc = sch.sqrmul(sb, 7, s1101)
sd = sch.sqrmul(sc, 4, s11)
se = sch.sqrmul(sd, 6, s111)
sf = sch.sqrmul(se, 3, s111)
sg = sch.sqrmul(sf, 10, s1001)
sh = sch.sqrmul(sg, 4, s1001)
si = sch.sqrmul(sh, 5, s1001)
sj = sch.sqrmul(si, 5, s1001)
sk = sch.sqrmul(sj, 3, s1001)
sl = sch.sqrmul(sk, 4, s1011)
sm = sch.sqrmul(sl, 4, s1011)
sn = sch.sqrmul(sm, 5, s11)
so = sch.sqrmul(sn, 4, s1)
sp = sch.sqrmul(so, 5, s11)
sq = sch.sqrmul(sp, 4, s111)
sr = sch.sqrmul(sq, 5, s1011)
ss = sch.sqrmul(sr, 3, s1)
st = sch.sqr(ss, 4)
assert st == s, format(st, 'b')
print(sch)
t = (1<<32) - 1
assert(s == q >> (n+1))
tch = Chain()
t1 = 1
t2 = tch.sqrmul(t1, 1, t1)
t4 = tch.sqrmul(t2, 2, t2)
t8 = tch.sqrmul(t4, 4, t4)
t16 = tch.sqrmul(t8, 8, t8)
t32 = tch.sqrmul(t16, 16, t16)
assert t32 == t, format(t32, 'b')
print(tch)