sqrtm1.py 563 B

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