dleq.sage 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # What is defined as DLEQ(g,x,h,y) for proving knowledge of some α
  2. # in zero-knowledge is what follows:
  3. # We want to prove knowledge of a value α ∈ Fq, such that x=g*α and y=h*a,
  4. # given g,x,h,y.
  5. # ================
  6. # Parameters setup
  7. # ================
  8. # Pallas curve
  9. p = 0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001
  10. q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
  11. Fp = GF(p)
  12. Fq = GF(q)
  13. Ep = EllipticCurve(Fp, (0, 5))
  14. Ep.set_order(q)
  15. g = Ep.random_point()
  16. h = Ep.random_point()
  17. # Value alpha
  18. α = Fq.random_element()
  19. # =================
  20. # Interactive proof
  21. # =================
  22. # The public data is
  23. x = g*α
  24. y = h*α
  25. # 1. The prover computes a_1 = g*w and a_2 = h*w, where w is a random element
  26. # of Fq, and sends a_1 and a_2 to the verifier.
  27. w = Fq.random_element()
  28. a_1 = g * w
  29. a_2 = h * w
  30. # 2. The verifier sends a challenge e from Fq to the prover
  31. e = Fq.random_element()
  32. # 3. The prover sends a response z = w - αe to the verifier.
  33. z = w - α * e
  34. # 4. The verifier checks the following and accepts the proof if it holds:
  35. assert a_1 == g*z + x*e
  36. assert a_2 == h*z + y*e
  37. # =====================
  38. # Non-interactive proof
  39. # =====================
  40. # This sigma proof can be transformed into a non-interactive ZK proof
  41. # through the Fiat-Shamir heuristic:
  42. from hashlib import sha256
  43. # Prover:
  44. e = sha256()
  45. e.update(str(x).encode())
  46. e.update(str(y).encode())
  47. e.update(str(a_1).encode())
  48. e.update(str(a_2).encode())
  49. e_prover = Fq(int(e.hexdigest(), 16))
  50. z = w - α * e_prover
  51. # Verifier
  52. e = sha256()
  53. e.update(str(x).encode())
  54. e.update(str(y).encode())
  55. e.update(str(a_1).encode())
  56. e.update(str(a_2).encode())
  57. e_verifier = Fq(int(e.hexdigest(), 16))
  58. assert a_1 == g*z + x*e_verifier
  59. assert a_2 == h*z + y*e_verifier
  60. assert e_prover == e_verifier