zkrunner.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. from argparse import ArgumentParser
  3. from darkfi_sdk_py.affine import Affine
  4. from darkfi_sdk_py.base import Base
  5. from darkfi_sdk_py.point import Point
  6. from darkfi_sdk_py.proof import Proof
  7. from darkfi_sdk_py.proving_key import ProvingKey
  8. from darkfi_sdk_py.scalar import Scalar
  9. from darkfi_sdk_py.verifying_key import VerifyingKey
  10. from darkfi_sdk_py.zk_binary import ZkBinary
  11. from darkfi_sdk_py.zk_circuit import ZkCircuit
  12. from pprint import pprint
  13. from sys import getsizeof
  14. from time import time
  15. def heap_add(heap, element):
  16. print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> heap")
  17. pprint(heap)
  18. print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> to add")
  19. pprint(element)
  20. heap.append(element)
  21. def pubins_add(pubins, element):
  22. print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> pubins")
  23. pprint(pubins)
  24. print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> to add")
  25. pprint(element)
  26. pubins.append(element)
  27. def get_pubins(statements, witnesses, constant_count, literals):
  28. # Python heap for executing zk statements
  29. heap = [None] * constant_count + witnesses
  30. pubins = []
  31. for stmt in statements:
  32. print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Statement")
  33. print(stmt)
  34. opcode, args = stmt[0], stmt[1]
  35. if opcode == 'BaseAdd':
  36. a = heap[args[0][1]]
  37. b = heap[args[1][1]]
  38. heap_add(heap, a + b)
  39. elif opcode == 'BaseMul':
  40. a = heap[args[0][1]]
  41. b = heap[args[1][1]]
  42. heap_add(heap, a * b)
  43. elif opcode == 'BaseSub':
  44. a = heap[args[0][1]]
  45. b = heap[args[1][1]]
  46. heap_add(heap, a - b)
  47. elif opcode == 'EcAdd':
  48. a = heap[args[0][1]]
  49. b = heap[args[1][1]]
  50. heap_add(heap, a + b)
  51. elif opcode == 'EcMul':
  52. a = heap[args[0][1]]
  53. heap_add(heap, Point.mul_r_generator(a))
  54. elif opcode in {'EcMulBase', 'EcMulVarBase'}:
  55. i = args[0][1]
  56. base = heap[i]
  57. product = Point.mul_base(base)
  58. heap_add(heap, product)
  59. elif opcode == 'EcMulShort':
  60. value = heap[args[0][1]]
  61. heap_add(heap, Point.mul_short(value))
  62. elif opcode == 'EcGetX':
  63. i = args[0][1]
  64. point = heap[i]
  65. x, _ = point.to_affine().coordinates()
  66. heap_add(heap, x)
  67. elif opcode == 'EcGetY':
  68. i = args[0][1]
  69. point = heap[i]
  70. _, y = point.to_affine().coordinates()
  71. heap_add(heap, y)
  72. elif opcode == 'PoseidonHash':
  73. messages = [heap[m[1]] for m in args]
  74. heap_add(heap, Base.poseidon_hash(messages))
  75. elif opcode == 'MerkleRoot':
  76. i = heap[args[0][1]]
  77. p = heap[args[1][1]]
  78. a = heap[args[2][1]]
  79. heap_add(heap, Base.merkle_root(i, p, a))
  80. elif opcode == 'ConstrainInstance':
  81. i = args[0][1]
  82. element = heap[i]
  83. pubins_add(pubins, element)
  84. elif opcode == 'WitnessBase':
  85. type = args[0][0]
  86. assert type == 'Lit', f"type should LitType instead of {type}"
  87. i = args[0][1]
  88. element = int(literals[i][1]) # (LitType, Lit)
  89. base = Base.from_u64(element)
  90. heap_add(heap, base)
  91. elif opcode == 'CondSelect':
  92. cnd = heap[args[0][1]]
  93. thn = heap[args[1][1]]
  94. els = heap[args[2][1]]
  95. assert cnd == Base.from_u64(0) or cnd == Base.from_u64(
  96. 1), "Failed bool check"
  97. res = thn if cnd == Base.from_u64(1) else els
  98. heap_add(heap, res)
  99. elif opcode in IGNORED_OPCODES:
  100. print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Ignored opcode")
  101. pprint(opcode)
  102. else:
  103. print(
  104. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Missing implementation for opcode"
  105. )
  106. pprint(opcode)
  107. return pubins
  108. def bincode_data(bincode):
  109. with open(bincode, "rb") as f:
  110. bincode = f.read()
  111. zkbin = ZkBinary.decode(bincode)
  112. return {
  113. "zkbin": zkbin,
  114. "namespace": zkbin.namespace(),
  115. "witnesses": zkbin.witnesses(),
  116. "constant_count": zkbin.constant_count(),
  117. "statements": zkbin.opcodes(),
  118. "literals": zkbin.literals()
  119. }
  120. IGNORED_OPCODES = {
  121. 'Noop', 'RangeCheck', 'LessThanStrict', 'LessThanLoose', 'BoolCheck',
  122. 'ConstrainEqualBase', 'ConstrainEqualPoint', 'DebugPrint'
  123. }
  124. K = 13
  125. if __name__ == "__main__":
  126. ##### Script inputs #####
  127. bincode_path = "opcodes.no-nipoint.zk.bin"
  128. # bincode_path = "../../example/simple.zk.bin" # You must change the witnesses as well
  129. bincode_data_ = bincode_data(bincode_path)
  130. zkbin, statements, constant_count, literals = bincode_data_[
  131. 'zkbin'], bincode_data_['statements'], bincode_data_[
  132. 'constant_count'], bincode_data_['literals']
  133. witnesses = [
  134. Base.from_u64(3),
  135. Scalar.from_u64(4),
  136. Base.from_u64(5),
  137. Base.from_u64(6),
  138. Base.from_u64(7),
  139. Base.from_u64(8),
  140. 10,
  141. [Base.from_u64(42)] * 32,
  142. Base.from_u64(1),
  143. ]
  144. ##### Proving #####
  145. print("Making public inputs based off of witnesses......")
  146. pubins = get_pubins(statements, witnesses, constant_count, literals)
  147. print("Witnessing each witness into prover's circuit.....")
  148. zkcircuit = ZkCircuit(zkbin)
  149. zkcircuit.witness_base(witnesses[0])
  150. zkcircuit.witness_scalar(witnesses[1])
  151. zkcircuit.witness_base(witnesses[2])
  152. zkcircuit.witness_base(witnesses[3])
  153. zkcircuit.witness_base(witnesses[4])
  154. zkcircuit.witness_base(witnesses[5])
  155. zkcircuit.witness_u32(witnesses[6])
  156. zkcircuit.witness_merkle_path(witnesses[7])
  157. zkcircuit.witness_base(witnesses[8])
  158. zkcircuit = zkcircuit.build(zkbin)
  159. print("Making proving key.....")
  160. start = time()
  161. proving_key = ProvingKey.build(K, zkcircuit)
  162. print(f"Time for making proving key: {time() - start}")
  163. print("Proving.....")
  164. start = time()
  165. proof = Proof.create(proving_key, [zkcircuit], pubins)
  166. print(f"Time for proving: {time() - start}")
  167. ##### Verifiying #####
  168. zkcircuit_v = zkcircuit.verifier_build(zkbin)
  169. print(f"Making verifying key.....")
  170. start = time()
  171. verifying_key = VerifyingKey.build(K, zkcircuit_v)
  172. print(f"Time for making verifying key: {time() - start}")
  173. print("Verifying.....")
  174. start = time()
  175. proof.verify(verifying_key, pubins)
  176. print(f"Time for verifying {time() - start}")