You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.3 KiB

  1. from hashlib import sha256
  2. # Implementation of Sigma protocol & OR proofs
  3. def hash_two_points(a, b):
  4. h = sha256((str(a)+str(b)).encode('utf-8'))
  5. return int(h.hexdigest(), 16)
  6. def generic_verify(g, X, A, c, z):
  7. return g * int(z) == X * int(c) + A
  8. ###
  9. # Sigma protocol interactive
  10. ###
  11. class Prover_interactive:
  12. def __init__(self, F, g):
  13. self.F = F # Z_q
  14. self.g = g # elliptic curve generator
  15. def new_key(self):
  16. self.w = self.F.random_element()
  17. X = self.g * int(self.w)
  18. return X
  19. def new_commitment(self):
  20. self.a = self.F.random_element()
  21. A = self.g * int(self.a)
  22. return A
  23. def gen_proof(self, c):
  24. return int(self.a) + int(c) * int(self.w)
  25. class Verifier_interactive:
  26. def __init__(self, F, g):
  27. self.F = F
  28. self.g = g
  29. def new_challenge(self, A):
  30. self.A = A
  31. self.c = self.F.random_element()
  32. return self.c
  33. def verify(self, X, z):
  34. return self.g * int(z) == X * int(self.c) + self.A
  35. ###
  36. # Sigma protocol non-interactive
  37. ###
  38. class Prover:
  39. def __init__(self, F, g):
  40. self.F = F # Z_p
  41. self.g = g # elliptic curve generator
  42. def new_key(self):
  43. self.w = self.F.random_element()
  44. X = self.g * int(self.w)
  45. return X
  46. def gen_proof(self, X):
  47. a = self.F.random_element()
  48. A = self.g * int(a)
  49. c = hash_two_points(A, X)
  50. z = int(a) + c * int(self.w)
  51. return A, z
  52. class Verifier:
  53. def __init__(self, F, g):
  54. self.F = F
  55. self.g = g
  56. def verify(self, X, A, z):
  57. c = hash_two_points(A, X)
  58. return self.g * int(z) == X * c + A
  59. class Simulator:
  60. def __init__(self, F, g):
  61. self.F = F
  62. self.g = g
  63. def simulate(self, X):
  64. c = self.F.random_element()
  65. z = self.F.random_element()
  66. # A = g * int(z) + X*(-int(c))
  67. A = g * int(z) - X * int(c)
  68. return A, c, z
  69. ###
  70. # OR proof (with 2 parties)
  71. ###
  72. class ORProver_2parties:
  73. def __init__(self, F, g):
  74. self.F = F # Z_p
  75. self.g = g # elliptic curve generator
  76. def new_key(self):
  77. self.w = self.F.random_element()
  78. X = self.g * int(self.w)
  79. return X
  80. def gen_commitments(self, xs):
  81. # gen commitment A
  82. self.a = self.F.random_element()
  83. A = self.g * int(self.a)
  84. # run the simulator for 1-b
  85. sim = Simulator(self.F, self.g)
  86. A_1, c_1, z_1 = sim.simulate(xs[1])
  87. self.A_1 = A_1
  88. self.c_1 = c_1
  89. self.z_1 = z_1
  90. return [A, A_1]
  91. def gen_proof(self, s):
  92. # split the challenge s = c xor c_1
  93. c = int(s) ^^ int(self.c_1)
  94. # compute z
  95. z = int(self.a) + int(c) * int(self.w)
  96. # note, here the order of the returned elements is always the same, in
  97. # a real-world implementation would be shuffled
  98. return [c, self.c_1], [z, self.z_1]
  99. class ORVerifier_2parties:
  100. def __init__(self, F, g):
  101. self.F = F
  102. self.g = g
  103. def new_challenge(self, As):
  104. self.As = As
  105. self.s = self.F.random_element()
  106. return self.s
  107. def verify(self, Xs, cs, zs):
  108. assert self.s == int(cs[0]) ^^ int(cs[1])
  109. assert self.g * int(zs[0]) == Xs[0] * int(cs[0]) + self.As[0]
  110. assert self.g * int(zs[1]) == Xs[1] * int(cs[1]) + self.As[1]
  111. ###
  112. # OR proof (with n parties)
  113. ###
  114. class ORProver:
  115. def __init__(self, F, g):
  116. self.F = F # Z_p
  117. self.g = g # elliptic curve generator
  118. def new_key(self):
  119. self.w = self.F.random_element()
  120. X = self.g * int(self.w)
  121. return X
  122. def gen_commitments(self, xs):
  123. # gen commitment A
  124. self.a = self.F.random_element()
  125. A = self.g * int(self.a)
  126. self.As = [A]
  127. # run the simulator for the rest of Xs
  128. sim = Simulator(self.F, self.g)
  129. self.cs = []
  130. self.zs = []
  131. for i in range(1, len(xs)):
  132. A_1, c_1, z_1 = sim.simulate(xs[i])
  133. self.As.append(A_1)
  134. self.cs.append(c_1)
  135. self.zs.append(z_1)
  136. return self.As
  137. def gen_proof(self, s):
  138. # split the challenge s = c xor c_1 xor c_2 xor ... xor c_n
  139. c = int(s)
  140. for i in range(len(self.cs)):
  141. c = c ^^ int(self.cs[i])
  142. self.cs.insert(0, c) # add c at the beginning of cs array
  143. # compute z
  144. z = int(self.a) + int(c) * int(self.w)
  145. self.zs.insert(0, z) # add z at the beginning of zs array
  146. # note, here the order of the returned elements is always the same, in
  147. # a real-world implementation would be shuffled
  148. return self.cs, self.zs
  149. class ORVerifier:
  150. def __init__(self, F, g):
  151. self.F = F
  152. self.g = g
  153. def new_challenge(self, As):
  154. self.As = As
  155. self.s = self.F.random_element()
  156. return self.s
  157. def verify(self, Xs, cs, zs):
  158. # check s == c_0 xor c_1 xor c_2 xor ... xor c_n
  159. computed_s = int(cs[0])
  160. for i in range(1, len(cs)):
  161. computed_s = computed_s ^^ int(cs[i])
  162. assert self.s == computed_s
  163. # check g*z == X*c + A (in multiplicative notation would g^z ==X^c * A)
  164. for i in range(len(Xs)):
  165. assert self.g * int(zs[i]) == Xs[i] * int(cs[i]) + self.As[i]