lookup.sage 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import random
  2. q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
  3. K = GF(q)
  4. P.<X> = K[]
  5. def get_omega():
  6. generator = K(5)
  7. assert (q - 1) % 2^32 == 0
  8. # Root of unity
  9. t = (q - 1) / 2^32
  10. omega = generator**t
  11. assert omega != 1
  12. assert omega^(2^16) != 1
  13. assert omega^(2^31) != 1
  14. assert omega^(2^32) == 1
  15. return omega
  16. # Order of this element is 2^32
  17. omega = get_omega()
  18. k = 3
  19. n = 2^k
  20. omega = omega^(2^32 / n)
  21. assert omega^n == 1
  22. A = 2
  23. S = [3, 2, 3, 4, 5]
  24. # We are checking that A is in S
  25. # Extend S with random values so it is equal to n
  26. S += [110, 110]
  27. # Last value is impossible to access with the permutation loop
  28. # so it is unused.
  29. assert len(S) == n - 1
  30. # Extend A with dummy values (the other values in S)
  31. # We waste an entire column with this check
  32. # but multiple lookup checks can be combined in a single column
  33. # using a 'tag'
  34. A = [A] + [3, 5, 3, 110, 3, 110]
  35. assert len(A) == len(S)
  36. # Random permutations of A and S
  37. A_prime = [2, 3, 3, 3, 5, 110, 110]
  38. S_prime = [2, 3, 4, 3, 5, 110, 110]
  39. # First values must be the same
  40. assert A_prime[0] == S_prime[0]
  41. # Observe that for the values that do not match S_prime then
  42. # they are equal to the previous value.
  43. for i in range(len(A_prime)):
  44. assert A_prime[i] == S_prime[i] or A_prime[i] == A_prime[i - 1]
  45. beta = K.random_element()
  46. gamma = K.random_element()
  47. # Last row is unused
  48. permutation_points = [(omega^0, K(1))]
  49. for i in range(1, n):
  50. x = omega^i
  51. y = K(1)
  52. for j in range(i):
  53. y *= ((A[j] + beta) * (S[j] + gamma) /
  54. ((A_prime[j] + beta) * (S_prime[j] + gamma)))
  55. Z = P.lagrange_polynomial(permutation_points)
  56. assert Z(omega^0) == 1
  57. assert Z(omega^(n - 1)) == 1
  58. assert omega^n == omega^0
  59. # So now we have proved that A is a permutation of A_prime,
  60. # and S is a permutation of S_prime.