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.

131 lines
4.0 KiB

  1. import unittest, operator
  2. load("sigma.sage")
  3. # Tests for Sigma protocol & OR proofs
  4. # ethereum elliptic curve
  5. p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
  6. a = 0
  7. b = 7
  8. F = GF(p)
  9. E = EllipticCurve(F, [a,b])
  10. GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
  11. GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
  12. g = E(GX,GY)
  13. n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
  14. h = 1
  15. q = g.order()
  16. assert is_prime(p)
  17. assert is_prime(q)
  18. class TestSigmaProtocol(unittest.TestCase):
  19. def test_interactive(self):
  20. alice = Prover_interactive(F, g)
  21. # Alice generates witness w & statement X
  22. X = alice.new_key()
  23. assert X == alice.g * int(alice.w)
  24. # Alice generates the commitment A
  25. A = alice.new_commitment()
  26. assert A == alice.g * int(alice.a)
  27. # Bob generates the challenge (and stores A)
  28. bob = Verifier_interactive(F, g)
  29. c = bob.new_challenge(A)
  30. # Alice generates the proof
  31. z = alice.gen_proof(c)
  32. # Bob verifies the proof
  33. assert bob.verify(X, z)
  34. # check with the generic_verify function
  35. assert generic_verify(g, X, A, c, z)
  36. def test_non_interactive(self):
  37. alice = Prover(F, g)
  38. # Alice generates witness w & statement X
  39. X = alice.new_key()
  40. assert X == alice.g * int(alice.w)
  41. # Alice generates the proof
  42. A, z = alice.gen_proof(X)
  43. # Bob generates the challenge
  44. bob = Verifier(F, g)
  45. # Bob verifies the proof
  46. assert bob.verify(X, A, z)
  47. # check with the generic_verify function
  48. c = hash_two_points(A, X)
  49. assert generic_verify(g, X, A, c, z)
  50. def test_simulator(self):
  51. sim = Simulator(F, g)
  52. # set a public key X, for which we don't know w
  53. unknown_w = 3
  54. X = g * unknown_w
  55. # simulate for X
  56. A, c, z = sim.simulate(X)
  57. # verify the simulated proof
  58. assert generic_verify(g, X, A, c, z)
  59. class TestORProof(unittest.TestCase):
  60. def test_2_parties(self):
  61. # set a public key X, for which we don't know w
  62. unknown_w = 3
  63. X_1 = g * unknown_w
  64. alice = ORProver_2parties(F, g)
  65. # Alice generates key pair
  66. X = alice.new_key()
  67. Xs = [X, X_1]
  68. # Alice generates commitments (internally running the simulator)
  69. As = alice.gen_commitments(Xs)
  70. # Bob generates the challenge (and stores As)
  71. bob = ORVerifier_2parties(F, g)
  72. s = bob.new_challenge(As)
  73. # Alice generates the ORproof
  74. cs, zs = alice.gen_proof(s)
  75. # Bob verifies the proofs
  76. bob.verify(Xs, cs, zs)
  77. def test_n_parties(self):
  78. # set n public keys X, for which we don't know w
  79. Xs = []
  80. for i in range(10):
  81. X_i = g * i
  82. Xs.append(X_i)
  83. alice = ORProver(F, g)
  84. # Alice generates key pair
  85. X = alice.new_key()
  86. Xs.insert(0, X) # add X at the begining of Xs array
  87. # Alice generates commitments (internally running the simulator)
  88. As = alice.gen_commitments(Xs)
  89. # Bob generates the challenge (and stores As)
  90. bob = ORVerifier(F, g)
  91. s = bob.new_challenge(As)
  92. # Alice generates the ORproof
  93. cs, zs = alice.gen_proof(s)
  94. # Bob verifies the proofs
  95. bob.verify(Xs, cs, zs)
  96. if __name__ == '__main__':
  97. unittest.main()