| 1 |
q = 2**255 - 19 |
| 2 |
|
| 3 |
def expmod(b,e,m): |
| 4 |
if e == 0: return 1 |
| 5 |
t = expmod(b,e/2,m)**2 % m |
| 6 |
if e & 1: t = (t*b) % m |
| 7 |
return t |
| 8 |
|
| 9 |
def inv(x): |
| 10 |
return expmod(x,q-2,q) |
| 11 |
|
| 12 |
def radix255(x): |
| 13 |
x = x % q |
| 14 |
if x + x > q: x -= q |
| 15 |
x = [x,0,0,0,0,0,0,0,0,0] |
| 16 |
bits = [26,25,26,25,26,25,26,25,26,25] |
| 17 |
for i in range(9): |
| 18 |
carry = (x[i] + 2**(bits[i]-1)) / 2**bits[i] |
| 19 |
x[i] -= carry * 2**bits[i] |
| 20 |
x[i + 1] += carry |
| 21 |
result = "" |
| 22 |
for i in range(9): |
| 23 |
result = result+str(x[i])+"," |
| 24 |
result = result+str(x[9]) |
| 25 |
return result |
| 26 |
|
| 27 |
d = -121665 * inv(121666) |
| 28 |
print radix255(d) |