crypto.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import hashlib
  2. import random
  3. def ff_inv(a, p):
  4. a %= p
  5. # extended euclidean algorithm
  6. # ps + at = 1
  7. t = 0
  8. new_t = 1
  9. r = p
  10. new_r = a
  11. while new_r != 0:
  12. quotient = r // new_r
  13. t, new_t = new_t, t - quotient * new_t
  14. r, new_r = new_r, r - quotient * new_r
  15. assert r == 1
  16. if t < 0:
  17. t += p
  18. return t
  19. class EllipticCurve:
  20. def __init__(self, p, A, B, order, G, H, J):
  21. self.p = p
  22. self.A = A
  23. self.B = B
  24. self.order = order
  25. self.G = G
  26. self.H = H
  27. self.J = J
  28. assert self.is_valid(G)
  29. assert self.is_valid(H)
  30. def is_valid(self, P):
  31. x, y, z = P
  32. if z == 0:
  33. return x != 0 or y != 0
  34. z_inv = ff_inv(z, self.p)
  35. x, y = x * z_inv, y * z_inv
  36. return y**2 % self.p == (x**3 + self.A * x + self.B) % self.p
  37. def add(self, p1, p2):
  38. x1, y1, z1 = p1
  39. x2, y2, z2 = p2
  40. if z1 == 0:
  41. return (x2, y2, z2)
  42. elif z2 == 0:
  43. return (x1, y1, z1)
  44. if x1 == x2:
  45. if y1 != y2:
  46. return (0, 1, 0)
  47. assert y1 != 0
  48. m = (3 * x1**2 + self.A) * ff_inv(2*y1, self.p)
  49. else:
  50. m = (y2 - y1) * ff_inv(x2 - x1, self.p)
  51. x3 = (m**2 - x1 - x2) % self.p
  52. y3 = (m * (x1 - x3) - y1) % self.p
  53. return (x3, y3, 1)
  54. def multiply(self, m, p):
  55. bits = f"{m:b}"
  56. result = (0, 1, 0)
  57. temp = p
  58. for bit in bits[::-1]:
  59. if bit == "1":
  60. result = self.add(result, temp)
  61. temp = self.add(temp, temp)
  62. return result
  63. def random_point(self):
  64. m = self.random_scalar()
  65. return self.multiply(m, self.G)
  66. def random_scalar(self):
  67. m = random.randrange(0, self.order - 1)
  68. return m
  69. def random_base(self):
  70. m = random.randrange(0, self.p - 1)
  71. return m
  72. def pallas_curve():
  73. # Pallas
  74. p = 0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001
  75. q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
  76. G = (5, 5392431450607408583390510508521091931943415030464003135511088002453056875732, 1)
  77. H = (9762257241998025279988087154025308614062019274413483967640476725944341089207,
  78. 12058632856930756995627167820351407063813260358041446014729496773111030695755, 1)
  79. J = (7795559447963065356059848000022900528974048197507738248625163674930282081839,
  80. 5156492880772775379342191094371887365795329446468828588866320184016504353483, 1)
  81. ec = EllipticCurve(p, 0, 5, q, G, H, J)
  82. A = (144931808354919915876542440378319484704499556634959420306426167479163065488,
  83. 2699682121356767698440748624399854659825391162912545787181017961871465868196, 1)
  84. B = (16017037670495191561606513965775243786961447026019262496667491008912834496943,
  85. 20395164507282344548629891414360366999207473153143014512687861307997120664849, 1)
  86. assert ec.add(A, B) == (2414658659502531855741199170408914396997834981355655923471364687102714431309, 21133344194418979683767005688724798091220515434220043854575260979109407444719, 1)
  87. m = 26322809409216846271933211244226061368157231119725763192402071651286829040466
  88. assert ec.multiply(m, G) == (15862887453366837597569434439063150886012590021428640083047997467990450633825, 25887284719793568129480941070850220101898092026705204234126448799557008384178, 1)
  89. return ec
  90. def pedersen_encrypt(x, y, ec):
  91. vcv = ec.multiply(x, ec.G)
  92. vcr = ec.multiply(y, ec.H)
  93. return ec.add(vcv, vcr)
  94. def _add_to_hasher(hasher, args):
  95. for arg in args:
  96. match arg:
  97. case int() as arg:
  98. hasher.update(arg.to_bytes(32, byteorder="little"))
  99. case bytes() as arg:
  100. hasher.update(arg)
  101. case list() as arg:
  102. _add_to_hasher(hasher, arg)
  103. case _:
  104. raise Exception(f"unknown hash arg '{arg}' type: {type(arg)}")
  105. def ff_hash(p, *args):
  106. hasher = hashlib.sha256()
  107. _add_to_hasher(hasher, args)
  108. value = int.from_bytes(hasher.digest(), byteorder="little")
  109. return value % p
  110. def hash_point(point, message=None):
  111. hasher = hashlib.sha256()
  112. for x_i in point:
  113. hasher.update(x_i.to_bytes(32, byteorder="little"))
  114. # Optional message
  115. if message is not None:
  116. hasher.update(message)
  117. value = int.from_bytes(hasher.digest(), byteorder="little")
  118. return value
  119. def sign(message, secret, ec):
  120. ephem_secret = ec.random_scalar()
  121. ephem_public = ec.multiply(ephem_secret, ec.G)
  122. challenge = hash_point(ephem_public, message) % ec.order
  123. response = (ephem_secret + challenge * secret) % ec.order
  124. return ephem_public, response
  125. def verify(message, signature, public, ec):
  126. ephem_public, response = signature
  127. challenge = hash_point(ephem_public, message) % ec.order
  128. # sG
  129. lhs = ec.multiply(response, ec.G)
  130. # R + cP
  131. rhs_cP = ec.multiply(challenge, public)
  132. rhs = ec.add(ephem_public, rhs_cP)
  133. return lhs == rhs