Skip to content

Commit 85b00a1

Browse files
committed
Merge bitcoin-core/secp256k1#1068: sage: Fix incompatibility with sage 9.4
ebb1bee sage: Ensure that constraints are always fastfracs (Tim Ruffing) d8d5485 ci: Run sage prover on CI (Tim Ruffing) 77cfa98 sage: Normalize sign of polynomial factors in prover (Tim Ruffing) eae7586 sage: Exit with non-zero status in case of failures (Tim Ruffing) b54d843 sage: Fix printing of errors (Tim Ruffing) e108d00 sage: Fix incompatibility with sage 9.4 (Tim Ruffing) Pull request description: ACKs for top commit: sipa: ACK ebb1bee jonasnick: ACK ebb1bee Tree-SHA512: 7a4732fd31d925d3dff471911183acc465ddcadbb5c88c46995502df61a913433c7639cb52fad3db72373b7cc47b9b0f063f7f5d5f8189c9ef998955e409479f
2 parents d8a2463 + ebb1bee commit 85b00a1

5 files changed

+79
-31
lines changed

.cirrus.yml

+7
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,10 @@ task:
322322
test_script:
323323
- ./ci/cirrus.sh
324324
<< : *CAT_LOGS
325+
326+
task:
327+
name: "sage prover"
328+
<< : *LINUX_CONTAINER
329+
test_script:
330+
- cd sage
331+
- sage prove_group_implementations.sage

ci/linux-debian.Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ RUN apt-get install --no-install-recommends --no-upgrade -y \
1919
gcc-arm-linux-gnueabihf libc6-dev-armhf-cross libc6-dbg:armhf \
2020
gcc-aarch64-linux-gnu libc6-dev-arm64-cross libc6-dbg:arm64 \
2121
gcc-powerpc64le-linux-gnu libc6-dev-ppc64el-cross libc6-dbg:ppc64el \
22-
wine gcc-mingw-w64-x86-64
22+
wine gcc-mingw-w64-x86-64 \
23+
sagemath
2324

2425
# Run a dummy command in wine to make it set up configuration
2526
RUN wine64-stable xcopy || true

sage/group_prover.sage

+45-19
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ class constraints:
164164
def negate(self):
165165
return constraints(zero=self.nonzero, nonzero=self.zero)
166166

167+
def map(self, fun):
168+
return constraints(zero={fun(k): v for k, v in self.zero.items()}, nonzero={fun(k): v for k, v in self.nonzero.items()})
169+
167170
def __add__(self, other):
168171
zero = self.zero.copy()
169172
zero.update(other.zero)
@@ -177,6 +180,30 @@ class constraints:
177180
def __repr__(self):
178181
return "%s" % self
179182

183+
def normalize_factor(p):
184+
"""Normalizes the sign of primitive polynomials (as returned by factor())
185+
186+
This function ensures that the polynomial has a positive leading coefficient.
187+
188+
This is necessary because recent sage versions (starting with v9.3 or v9.4,
189+
we don't know) are inconsistent about the placement of the minus sign in
190+
polynomial factorizations:
191+
```
192+
sage: R.<ax,bx,ay,by,Az,Bz,Ai,Bi> = PolynomialRing(QQ,8,order='invlex')
193+
sage: R((-2 * (bx - ax)) ^ 1).factor()
194+
(-2) * (bx - ax)
195+
sage: R((-2 * (bx - ax)) ^ 2).factor()
196+
(4) * (-bx + ax)^2
197+
sage: R((-2 * (bx - ax)) ^ 3).factor()
198+
(8) * (-bx + ax)^3
199+
```
200+
"""
201+
# Assert p is not 0 and that its non-zero coeffients are coprime.
202+
# (We could just work with the primitive part p/p.content() but we want to be
203+
# aware if factor() does not return a primitive part in future sage versions.)
204+
assert p.content() == 1
205+
# Ensure that the first non-zero coefficient is positive.
206+
return p if p.lc() > 0 else -p
180207

181208
def conflicts(R, con):
182209
"""Check whether any of the passed non-zero assumptions is implied by the zero assumptions"""
@@ -204,10 +231,10 @@ def get_nonzero_set(R, assume):
204231
nonzero = set()
205232
for nz in map(numerator, assume.nonzero):
206233
for (f,n) in nz.factor():
207-
nonzero.add(f)
234+
nonzero.add(normalize_factor(f))
208235
rnz = zero.reduce(nz)
209236
for (f,n) in rnz.factor():
210-
nonzero.add(f)
237+
nonzero.add(normalize_factor(f))
211238
return nonzero
212239

213240

@@ -222,27 +249,27 @@ def prove_nonzero(R, exprs, assume):
222249
return (False, [exprs[expr]])
223250
allexprs = reduce(lambda a,b: numerator(a)*numerator(b), exprs, 1)
224251
for (f, n) in allexprs.factor():
225-
if f not in nonzero:
252+
if normalize_factor(f) not in nonzero:
226253
ok = False
227254
if ok:
228255
return (True, None)
229256
ok = True
230-
for (f, n) in zero.reduce(numerator(allexprs)).factor():
231-
if f not in nonzero:
257+
for (f, n) in zero.reduce(allexprs).factor():
258+
if normalize_factor(f) not in nonzero:
232259
ok = False
233260
if ok:
234261
return (True, None)
235262
ok = True
236263
for expr in exprs:
237264
for (f,n) in numerator(expr).factor():
238-
if f not in nonzero:
265+
if normalize_factor(f) not in nonzero:
239266
ok = False
240267
if ok:
241268
return (True, None)
242269
ok = True
243270
for expr in exprs:
244271
for (f,n) in zero.reduce(numerator(expr)).factor():
245-
if f not in nonzero:
272+
if normalize_factor(f) not in nonzero:
246273
expl.add(exprs[expr])
247274
if expl:
248275
return (False, list(expl))
@@ -254,7 +281,7 @@ def prove_zero(R, exprs, assume):
254281
"""Check whether all of the passed expressions are provably zero, given assumptions"""
255282
r, e = prove_nonzero(R, dict(map(lambda x: (fastfrac(R, x.bot, 1), exprs[x]), exprs)), assume)
256283
if not r:
257-
return (False, map(lambda x: "Possibly zero denominator: %s" % x, e))
284+
return (False, list(map(lambda x: "Possibly zero denominator: %s" % x, e)))
258285
zero = R.ideal(list(map(numerator, assume.zero)))
259286
nonzero = prod(x for x in assume.nonzero)
260287
expl = []
@@ -279,17 +306,17 @@ def describe_extra(R, assume, assumeExtra):
279306
if base not in zero:
280307
add = []
281308
for (f, n) in numerator(base).factor():
282-
if f not in nonzero:
283-
add += ["%s" % f]
309+
if normalize_factor(f) not in nonzero:
310+
add += ["%s" % normalize_factor(f)]
284311
if add:
285312
ret.add((" * ".join(add)) + " = 0 [%s]" % assumeExtra.zero[base])
286313
# Iterate over the extra nonzero expressions
287314
for nz in assumeExtra.nonzero:
288315
nzr = zeroextra.reduce(numerator(nz))
289316
if nzr not in zeroextra:
290317
for (f,n) in nzr.factor():
291-
if zeroextra.reduce(f) not in nonzero:
292-
ret.add("%s != 0" % zeroextra.reduce(f))
318+
if normalize_factor(zeroextra.reduce(f)) not in nonzero:
319+
ret.add("%s != 0" % normalize_factor(zeroextra.reduce(f)))
293320
return ", ".join(x for x in ret)
294321

295322

@@ -299,22 +326,21 @@ def check_symbolic(R, assumeLaw, assumeAssert, assumeBranch, require):
299326

300327
if conflicts(R, assume):
301328
# This formula does not apply
302-
return None
329+
return (True, None)
303330

304331
describe = describe_extra(R, assumeLaw + assumeBranch, assumeAssert)
332+
if describe != "":
333+
describe = " (assuming " + describe + ")"
305334

306335
ok, msg = prove_zero(R, require.zero, assume)
307336
if not ok:
308-
return "FAIL, %s fails (assuming %s)" % (str(msg), describe)
337+
return (False, "FAIL, %s fails%s" % (str(msg), describe))
309338

310339
res, expl = prove_nonzero(R, require.nonzero, assume)
311340
if not res:
312-
return "FAIL, %s fails (assuming %s)" % (str(expl), describe)
341+
return (False, "FAIL, %s fails%s" % (str(expl), describe))
313342

314-
if describe != "":
315-
return "OK (assuming %s)" % describe
316-
else:
317-
return "OK"
343+
return (True, "OK%s" % describe)
318344

319345

320346
def concrete_verify(c):

sage/prove_group_implementations.sage

+13-10
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,18 @@ def formula_secp256k1_gej_add_ge_old(branch, a, b):
292292
return (constraints(zero={b.Z - 1 : 'b.z=1', b.Infinity : 'b_finite'}), constraints(zero=zero, nonzero=nonzero), jacobianpoint(rx, ry, rz))
293293

294294
if __name__ == "__main__":
295-
check_symbolic_jacobian_weierstrass("secp256k1_gej_add_var", 0, 7, 5, formula_secp256k1_gej_add_var)
296-
check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge_var", 0, 7, 5, formula_secp256k1_gej_add_ge_var)
297-
check_symbolic_jacobian_weierstrass("secp256k1_gej_add_zinv_var", 0, 7, 5, formula_secp256k1_gej_add_zinv_var)
298-
check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge", 0, 7, 16, formula_secp256k1_gej_add_ge)
299-
check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge_old [should fail]", 0, 7, 4, formula_secp256k1_gej_add_ge_old)
295+
success = True
296+
success = success & check_symbolic_jacobian_weierstrass("secp256k1_gej_add_var", 0, 7, 5, formula_secp256k1_gej_add_var)
297+
success = success & check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge_var", 0, 7, 5, formula_secp256k1_gej_add_ge_var)
298+
success = success & check_symbolic_jacobian_weierstrass("secp256k1_gej_add_zinv_var", 0, 7, 5, formula_secp256k1_gej_add_zinv_var)
299+
success = success & check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge", 0, 7, 16, formula_secp256k1_gej_add_ge)
300+
success = success & (not check_symbolic_jacobian_weierstrass("secp256k1_gej_add_ge_old [should fail]", 0, 7, 4, formula_secp256k1_gej_add_ge_old))
300301

301302
if len(sys.argv) >= 2 and sys.argv[1] == "--exhaustive":
302-
check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_var", 0, 7, 5, formula_secp256k1_gej_add_var, 43)
303-
check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge_var", 0, 7, 5, formula_secp256k1_gej_add_ge_var, 43)
304-
check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_zinv_var", 0, 7, 5, formula_secp256k1_gej_add_zinv_var, 43)
305-
check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge", 0, 7, 16, formula_secp256k1_gej_add_ge, 43)
306-
check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge_old [should fail]", 0, 7, 4, formula_secp256k1_gej_add_ge_old, 43)
303+
success = success & check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_var", 0, 7, 5, formula_secp256k1_gej_add_var, 43)
304+
success = success & check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge_var", 0, 7, 5, formula_secp256k1_gej_add_ge_var, 43)
305+
success = success & check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_zinv_var", 0, 7, 5, formula_secp256k1_gej_add_zinv_var, 43)
306+
success = success & check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge", 0, 7, 16, formula_secp256k1_gej_add_ge, 43)
307+
success = success & (not check_exhaustive_jacobian_weierstrass("secp256k1_gej_add_ge_old [should fail]", 0, 7, 4, formula_secp256k1_gej_add_ge_old, 43))
308+
309+
sys.exit(int(not success))

sage/weierstrass_prover.sage

+12-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def check_exhaustive_jacobian_weierstrass(name, A, B, branches, formula, p):
184184
if r:
185185
points.append(point)
186186

187+
ret = True
187188
for za in range(1, p):
188189
for zb in range(1, p):
189190
for pa in points:
@@ -211,8 +212,11 @@ def check_exhaustive_jacobian_weierstrass(name, A, B, branches, formula, p):
211212
match = True
212213
r, e = concrete_verify(require)
213214
if not r:
215+
ret = False
214216
print(" failure in branch %i for (%s,%s,%s,%s) + (%s,%s,%s,%s) = (%s,%s,%s,%s): %s" % (branch, pA.X, pA.Y, pA.Z, pA.Infinity, pB.X, pB.Y, pB.Z, pB.Infinity, pC.X, pC.Y, pC.Z, pC.Infinity, e))
217+
215218
print()
219+
return ret
216220

217221

218222
def check_symbolic_function(R, assumeAssert, assumeBranch, f, A, B, pa, pb, pA, pB, pC):
@@ -244,15 +248,21 @@ def check_symbolic_jacobian_weierstrass(name, A, B, branches, formula):
244248

245249
print("Formula " + name + ":")
246250
count = 0
251+
ret = True
247252
for branch in range(branches):
248253
assumeFormula, assumeBranch, pC = formula(branch, pA, pB)
254+
assumeBranch = assumeBranch.map(lift)
255+
assumeFormula = assumeFormula.map(lift)
249256
pC.X = lift(pC.X)
250257
pC.Y = lift(pC.Y)
251258
pC.Z = lift(pC.Z)
252259
pC.Infinity = lift(pC.Infinity)
253260

254261
for key in laws_jacobian_weierstrass:
255-
res[key].append((check_symbolic_function(R, assumeFormula, assumeBranch, laws_jacobian_weierstrass[key], A, B, pa, pb, pA, pB, pC), branch))
262+
success, msg = check_symbolic_function(R, assumeFormula, assumeBranch, laws_jacobian_weierstrass[key], A, B, pa, pb, pA, pB, pC)
263+
if not success:
264+
ret = False
265+
res[key].append((msg, branch))
256266

257267
for key in res:
258268
print(" %s:" % key)
@@ -262,3 +272,4 @@ def check_symbolic_jacobian_weierstrass(name, A, B, branches, formula):
262272
print(" branch %i: %s" % (x[1], x[0]))
263273

264274
print()
275+
return ret

0 commit comments

Comments
 (0)