test.py 3.7 KB

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