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.

secp256k1.sage 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Test libsecp256k1' group operation implementations using prover.sage
  2. import sys
  3. load("group_prover.sage")
  4. load("weierstrass_prover.sage")
  5. def formula_secp256k1_gej_double_var(a):
  6. """libsecp256k1's secp256k1_gej_double_var, used by various addition functions"""
  7. rz = a.Z * a.Y
  8. rz = rz * 2
  9. t1 = a.X^2
  10. t1 = t1 * 3
  11. t2 = t1^2
  12. t3 = a.Y^2
  13. t3 = t3 * 2
  14. t4 = t3^2
  15. t4 = t4 * 2
  16. t3 = t3 * a.X
  17. rx = t3
  18. rx = rx * 4
  19. rx = -rx
  20. rx = rx + t2
  21. t2 = -t2
  22. t3 = t3 * 6
  23. t3 = t3 + t2
  24. ry = t1 * t3
  25. t2 = -t4
  26. ry = ry + t2
  27. return jacobianpoint(rx, ry, rz)
  28. def formula_secp256k1_gej_add_var(branch, a, b):
  29. """libsecp256k1's secp256k1_gej_add_var"""
  30. if branch == 0:
  31. return (constraints(), constraints(nonzero={a.Infinity : 'a_infinite'}), b)
  32. if branch == 1:
  33. return (constraints(), constraints(zero={a.Infinity : 'a_finite'}, nonzero={b.Infinity : 'b_infinite'}), a)
  34. z22 = b.Z^2
  35. z12 = a.Z^2
  36. u1 = a.X * z22
  37. u2 = b.X * z12
  38. s1 = a.Y * z22
  39. s1 = s1 * b.Z
  40. s2 = b.Y * z12
  41. s2 = s2 * a.Z
  42. h = -u1
  43. h = h + u2
  44. i = -s1
  45. i = i + s2
  46. if branch == 2:
  47. r = formula_secp256k1_gej_double_var(a)
  48. return (constraints(), constraints(zero={h : 'h=0', i : 'i=0', a.Infinity : 'a_finite', b.Infinity : 'b_finite'}), r)
  49. if branch == 3:
  50. return (constraints(), constraints(zero={h : 'h=0', a.Infinity : 'a_finite', b.Infinity : 'b_finite'}, nonzero={i : 'i!=0'}), point_at_infinity())
  51. i2 = i^2
  52. h2 = h^2
  53. h3 = h2 * h
  54. h = h * b.Z
  55. rz = a.Z * h
  56. t = u1 * h2
  57. rx = t
  58. rx = rx * 2
  59. rx = rx + h3
  60. rx = -rx
  61. rx = rx + i2
  62. ry = -rx
  63. ry = ry + t
  64. ry = ry * i
  65. h3 = h3 * s1
  66. h3 = -h3
  67. ry = ry + h3
  68. return (constraints(), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite'}, nonzero={h : 'h!=0'}), jacobianpoint(rx, ry, rz))
  69. def formula_secp256k1_gej_add_ge_var(branch, a, b):
  70. """libsecp256k1's secp256k1_gej_add_ge_var, which assume bz==1"""
  71. if branch == 0:
  72. return (constraints(zero={b.Z - 1 : 'b.z=1'}), constraints(nonzero={a.Infinity : 'a_infinite'}), b)
  73. if branch == 1:
  74. return (constraints(zero={b.Z - 1 : 'b.z=1'}), constraints(zero={a.Infinity : 'a_finite'}, nonzero={b.Infinity : 'b_infinite'}), a)
  75. z12 = a.Z^2
  76. u1 = a.X
  77. u2 = b.X * z12
  78. s1 = a.Y
  79. s2 = b.Y * z12
  80. s2 = s2 * a.Z
  81. h = -u1
  82. h = h + u2
  83. i = -s1
  84. i = i + s2
  85. if (branch == 2):
  86. r = formula_secp256k1_gej_double_var(a)
  87. return (constraints(zero={b.Z - 1 : 'b.z=1'}), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite', h : 'h=0', i : 'i=0'}), r)
  88. if (branch == 3):
  89. return (constraints(zero={b.Z - 1 : 'b.z=1'}), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite', h : 'h=0'}, nonzero={i : 'i!=0'}), point_at_infinity())
  90. i2 = i^2
  91. h2 = h^2
  92. h3 = h * h2
  93. rz = a.Z * h
  94. t = u1 * h2
  95. rx = t
  96. rx = rx * 2
  97. rx = rx + h3
  98. rx = -rx
  99. rx = rx + i2
  100. ry = -rx
  101. ry = ry + t
  102. ry = ry * i
  103. h3 = h3 * s1
  104. h3 = -h3
  105. ry = ry + h3
  106. return (constraints(zero={b.Z - 1 : 'b.z=1'}), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite'}, nonzero={h : 'h!=0'}), jacobianpoint(rx, ry, rz))
  107. def formula_secp256k1_gej_add_zinv_var(branch, a, b):
  108. """libsecp256k1's secp256k1_gej_add_zinv_var"""
  109. bzinv = b.Z^(-1)
  110. if branch == 0:
  111. return (constraints(), constraints(nonzero={b.Infinity : 'b_infinite'}), a)
  112. if branch == 1:
  113. bzinv2 = bzinv^2
  114. bzinv3 = bzinv2 * bzinv
  115. rx = b.X * bzinv2
  116. ry = b.Y * bzinv3
  117. rz = 1
  118. return (constraints(), constraints(zero={b.Infinity : 'b_finite'}, nonzero={a.Infinity : 'a_infinite'}), jacobianpoint(rx, ry, rz))
  119. azz = a.Z * bzinv
  120. z12 = azz^2
  121. u1 = a.X
  122. u2 = b.X * z12
  123. s1 = a.Y
  124. s2 = b.Y * z12
  125. s2 = s2 * azz
  126. h = -u1
  127. h = h + u2
  128. i = -s1
  129. i = i + s2
  130. if branch == 2:
  131. r = formula_secp256k1_gej_double_var(a)
  132. return (constraints(), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite', h : 'h=0', i : 'i=0'}), r)
  133. if branch == 3:
  134. return (constraints(), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite', h : 'h=0'}, nonzero={i : 'i!=0'}), point_at_infinity())
  135. i2 = i^2
  136. h2 = h^2
  137. h3 = h * h2
  138. rz = a.Z
  139. rz = rz * h
  140. t = u1 * h2
  141. rx = t
  142. rx = rx * 2
  143. rx = rx + h3
  144. rx = -rx
  145. rx = rx + i2
  146. ry = -rx
  147. ry = ry + t
  148. ry = ry * i
  149. h3 = h3 * s1
  150. h3 = -h3
  151. ry = ry + h3
  152. return (constraints(), constraints(zero={a.Infinity : 'a_finite', b.Infinity : 'b_finite'}, nonzero={h : 'h!=0'}), jacobianpoint(rx, ry, rz))
  153. def formula_secp256k1_gej_add_ge(branch, a, b):
  154. """libsecp256k1's secp256k1_gej_add_ge"""
  155. zeroes = {}
  156. nonzeroes = {}
  157. a_infinity = False
  158. if (branch & 4) != 0:
  159. nonzeroes.update({a.Infinity : 'a_infinite'})
  160. a_infinity = True
  161. else:
  162. zeroes.update({a.Infinity : 'a_finite'})
  163. zz = a.Z^2
  164. u1 = a.X
  165. u2 = b.X * zz
  166. s1 = a.Y
  167. s2 = b.Y * zz
  168. s2 = s2 * a.Z
  169. t = u1
  170. t = t + u2
  171. m = s1
  172. m = m + s2
  173. rr = t^2
  174. m_alt = -u2
  175. tt = u1 * m_alt
  176. rr = rr + tt
  177. degenerate = (branch & 3) == 3
  178. if (branch & 1) != 0:
  179. zeroes.update({m : 'm_zero'})
  180. else:
  181. nonzeroes.update({m : 'm_nonzero'})
  182. if (branch & 2) != 0:
  183. zeroes.update({rr : 'rr_zero'})
  184. else:
  185. nonzeroes.update({rr : 'rr_nonzero'})
  186. rr_alt = s1
  187. rr_alt = rr_alt * 2
  188. m_alt = m_alt + u1
  189. if not degenerate:
  190. rr_alt = rr
  191. m_alt = m
  192. n = m_alt^2
  193. q = n * t
  194. n = n^2
  195. if degenerate:
  196. n = m
  197. t = rr_alt^2
  198. rz = a.Z * m_alt
  199. infinity = False
  200. if (branch & 8) != 0:
  201. if not a_infinity:
  202. infinity = True
  203. zeroes.update({rz : 'r.z=0'})
  204. else:
  205. nonzeroes.update({rz : 'r.z!=0'})
  206. rz = rz * 2
  207. q = -q
  208. t = t + q
  209. rx = t
  210. t = t * 2
  211. t = t + q
  212. t = t * rr_alt
  213. t = t + n
  214. ry = -t
  215. rx = rx * 4
  216. ry = ry * 4
  217. if a_infinity:
  218. rx = b.X
  219. ry = b.Y
  220. rz = 1
  221. if infinity:
  222. return (constraints(zero={b.Z - 1 : 'b.z=1', b.Infinity : 'b_finite'}), constraints(zero=zeroes, nonzero=nonzeroes), point_at_infinity())
  223. return (constraints(zero={b.Z - 1 : 'b.z=1', b.Infinity : 'b_finite'}), constraints(zero=zeroes, nonzero=nonzeroes), jacobianpoint(rx, ry, rz))
  224. def formula_secp256k1_gej_add_ge_old(branch, a, b):
  225. """libsecp256k1's old secp256k1_gej_add_ge, which fails when ay+by=0 but ax!=bx"""
  226. a_infinity = (branch & 1) != 0
  227. zero = {}
  228. nonzero = {}
  229. if a_infinity:
  230. nonzero.update({a.Infinity : 'a_infinite'})
  231. else:
  232. zero.update({a.Infinity : 'a_finite'})
  233. zz = a.Z^2
  234. u1 = a.X
  235. u2 = b.X * zz
  236. s1 = a.Y
  237. s2 = b.Y * zz
  238. s2 = s2 * a.Z
  239. z = a.Z
  240. t = u1
  241. t = t + u2
  242. m = s1
  243. m = m + s2
  244. n = m^2
  245. q = n * t
  246. n = n^2
  247. rr = t^2
  248. t = u1 * u2
  249. t = -t
  250. rr = rr + t
  251. t = rr^2
  252. rz = m * z
  253. infinity = False
  254. if (branch & 2) != 0:
  255. if not a_infinity:
  256. infinity = True
  257. else:
  258. return (constraints(zero={b.Z - 1 : 'b.z=1', b.Infinity : 'b_finite'}), constraints(nonzero={z : 'conflict_a'}, zero={z : 'conflict_b'}), point_at_infinity())
  259. zero.update({rz : 'r.z=0'})
  260. else:
  261. nonzero.update({rz : 'r.z!=0'})
  262. rz = rz * (0 if a_infinity else 2)
  263. rx = t
  264. q = -q
  265. rx = rx + q
  266. q = q * 3
  267. t = t * 2
  268. t = t + q
  269. t = t * rr
  270. t = t + n
  271. ry = -t
  272. rx = rx * (0 if a_infinity else 4)
  273. ry = ry * (0 if a_infinity else 4)
  274. t = b.X
  275. t = t * (1 if a_infinity else 0)
  276. rx = rx + t
  277. t = b.Y
  278. t = t * (1 if a_infinity else 0)
  279. ry = ry + t
  280. t = (1 if a_infinity else 0)
  281. rz = rz + t
  282. if infinity:
  283. return (constraints(zero={b.Z - 1 : 'b.z=1', b.Infinity : 'b_finite'}), constraints(zero=zero, nonzero=nonzero), point_at_infinity())
  284. return (constraints(zero={b.Z - 1 : 'b.z=1', b.Infinity : 'b_finite'}), constraints(zero=zero, nonzero=nonzero), jacobianpoint(rx, ry, rz))
  285. if __name__ == "__main__":
  286. check_symbolic_jacobian_weierstrass("secp256k1_gej_add_var", 0, 7, 5, formula_secp256k1_gej_add_var)
  287. check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge_var", 0, 7, 5, formula_secp256k1_gej_add_ge_var)
  288. check_symbolic_jacobian_weierstrass("secp256k1_gej_add_zinv_var", 0, 7, 5, formula_secp256k1_gej_add_zinv_var)
  289. check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge", 0, 7, 16, formula_secp256k1_gej_add_ge)
  290. check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge_old [should fail]", 0, 7, 4, formula_secp256k1_gej_add_ge_old)
  291. if len(sys.argv) >= 2 and sys.argv[1] == "--exhaustive":
  292. check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_var", 0, 7, 5, formula_secp256k1_gej_add_var, 43)
  293. check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge_var", 0, 7, 5, formula_secp256k1_gej_add_ge_var, 43)
  294. check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_zinv_var", 0, 7, 5, formula_secp256k1_gej_add_zinv_var, 43)
  295. check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge", 0, 7, 16, formula_secp256k1_gej_add_ge, 43)
  296. check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge_old [should fail]", 0, 7, 4, formula_secp256k1_gej_add_ge_old, 43)