test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import random
  2. import misc
  3. import pasta
  4. from polynomial_evalrep import make_polynomial_evalrep
  5. n = 8
  6. omega_base = misc.get_omega(pasta.fp, 2**32, seed=0)
  7. assert misc.is_power_of_two(8)
  8. omega = omega_base ** (2 ** 32 // n)
  9. # Order of omega is n
  10. assert omega ** n == 1
  11. # Compute complete roots of this group
  12. ROOTS = [omega ** i for i in range(n)]
  13. PolyEvalRep = make_polynomial_evalrep(pasta.fp, omega, n)
  14. import numpy as np
  15. from tabulate import tabulate
  16. # Wires
  17. a = ["x", "v1", "v2", "1", "1", "v3", "e1", "e2"]
  18. b = ["x", "x", "x", "5", "35", "5", "e3", "e4"]
  19. c = ["v1", "v2", "v3", "5", "35", "35", "e5", "e6"]
  20. wires = a + b + c
  21. # Gates
  22. # La + Rb + Oc + Mab + C = 0
  23. add = np.array([1, 1, 0, -1, 0])
  24. mul = np.array([0, 0, 1, -1, 0])
  25. const5 = np.array([0, 1, 0, 0, -5])
  26. public_input = np.array([0, 1, 0, 0, 0])
  27. empty = np.array([0, 0, 0, 0, 0])
  28. gates_matrix = np.array(
  29. [mul, mul, add, const5, public_input, add, empty, empty])
  30. print("Wires:")
  31. print(tabulate([["a ="] + a, ["b ="] + b, ["c ="] + c]))
  32. print()
  33. print("Gates:")
  34. print(gates_matrix)
  35. print()
  36. # The index of the public input in the gates_matrix
  37. # We specify its position and its value
  38. public_input_values = [(4, 35)]
  39. def permute_indices(wires):
  40. size = len(wires)
  41. permutation = [i + 1 for i in range(size)]
  42. for i in range(size):
  43. for j in range(i + 1, size):
  44. if wires[i] == wires[j]:
  45. permutation[i], permutation[j] = permutation[j], permutation[i]
  46. break
  47. return permutation
  48. permutation = permute_indices(wires)
  49. table = [
  50. ["Wires"] + wires,
  51. ["Indices"] + list(i + 1 for i in range(len(wires))),
  52. ["Permutations"] + permutation
  53. ]
  54. print(tabulate(table))
  55. print()
  56. import misc
  57. from pasta import fp
  58. def setup(wires, gates_matrix):
  59. # Section 8.1
  60. # The selector polynomials that define the circuit's arithmetisation
  61. gates_matrix = gates_matrix.transpose()
  62. ql = PolyEvalRep(ROOTS, [fp(i) for i in gates_matrix[0]])
  63. qr = PolyEvalRep(ROOTS, [fp(i) for i in gates_matrix[1]])
  64. qm = PolyEvalRep(ROOTS, [fp(i) for i in gates_matrix[2]])
  65. qo = PolyEvalRep(ROOTS, [fp(i) for i in gates_matrix[3]])
  66. qc = PolyEvalRep(ROOTS, [fp(i) for i in gates_matrix[4]])
  67. selector_polys = [ql, qr, qm, qo, qc]
  68. public_input = [fp(0) for i in range(len(ROOTS))]
  69. for (index, value) in public_input_values:
  70. # This is negative because the value is added to
  71. # the output of the const selector poly:
  72. # La + Rb + Oc + Mab + (C + PI) = 0
  73. public_input[index] = fp(-value)
  74. public_input_poly = PolyEvalRep(ROOTS, public_input)
  75. # Identity permutations applied to a, b, c
  76. # Ideally H, k_1 H, k_2 H are distinct cosets of H
  77. # Here we just sample k and assume it's high-order
  78. # Random high order k to form distinct cosets
  79. k = misc.sample_random(fp)
  80. id_domain_a = ROOTS
  81. id_domain_b = [k * root for root in ROOTS]
  82. id_domain_c = [k**2 * root for root in ROOTS]
  83. id_domain = id_domain_a + id_domain_b + id_domain_c
  84. # Intermediate step where we permute the positions of the domain
  85. # generated above
  86. permuted_domain = [id_domain[i - 1] for i in permutation]
  87. permuted_domain_a = permuted_domain[:n]
  88. permuted_domain_b = permuted_domain[n:2 * n]
  89. permuted_domain_c = permuted_domain[2*n:3 * n]
  90. # The copy permuation applied to a, b, c
  91. # Returns the permuted index value (corresponding root of unity coset)
  92. # when evaluated on the domain.
  93. ssigma_1 = PolyEvalRep(ROOTS, permuted_domain_a)
  94. ssigma_2 = PolyEvalRep(ROOTS, permuted_domain_b)
  95. ssigma_3 = PolyEvalRep(ROOTS, permuted_domain_c)
  96. copy_permutes = [ssigma_1, ssigma_2, ssigma_3]
  97. setup(wires, gates_matrix)