Skip to content

Commit a92bc6c

Browse files
committed
Squashed 'src/secp256k1/' changes from ff4714e641..0129b77767
0129b77767 Merge ElementsProject#113: Upstream PRs ElementsProject#849 ElementsProject#851 e1756dfddc Merge commits '3a106966 8f0c6f15 ' into temp-merge-851 7093e633b8 Merge pull request ElementsProject#106 from apoelstra/2020-11-reduce-test-rounds 29f9a7dc62 reduce test rounds for rangeproof and surjectionproof 8f0c6f1545 Merge ElementsProject#851: make test count iteration configurable by environment variable f4fa8d226a forbid a test iteration of 0 or less 3a106966aa Merge ElementsProject#849: Convert Sage code to Python 3 (as used by Sage >= 9) 13c88efed0 Convert Sage code to Python 3 (as used by Sage >= 9) 0ce4554881 make test count iteration configurable by environment variable git-subtree-dir: src/secp256k1 git-subtree-split: 0129b77767ea001e5693e39ac6deecea0c461817
1 parent 5eca32d commit a92bc6c

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

src/secp256k1/sage/group_prover.sage

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class fastfrac:
6565
return self.top in I and self.bot not in I
6666

6767
def reduce(self,assumeZero):
68-
zero = self.R.ideal(map(numerator, assumeZero))
68+
zero = self.R.ideal(list(map(numerator, assumeZero)))
6969
return fastfrac(self.R, zero.reduce(self.top)) / fastfrac(self.R, zero.reduce(self.bot))
7070

7171
def __add__(self,other):
@@ -100,14 +100,19 @@ class fastfrac:
100100
"""Multiply something else with a fraction."""
101101
return self.__mul__(other)
102102

103-
def __div__(self,other):
103+
def __truediv__(self,other):
104104
"""Divide two fractions."""
105105
if parent(other) == ZZ:
106106
return fastfrac(self.R,self.top,self.bot * other)
107107
if other.__class__ == fastfrac:
108108
return fastfrac(self.R,self.top * other.bot,self.bot * other.top)
109109
return NotImplemented
110110

111+
# Compatibility wrapper for Sage versions based on Python 2
112+
def __div__(self,other):
113+
"""Divide two fractions."""
114+
return self.__truediv__(other)
115+
111116
def __pow__(self,other):
112117
"""Compute a power of a fraction."""
113118
if parent(other) == ZZ:
@@ -175,7 +180,7 @@ class constraints:
175180

176181
def conflicts(R, con):
177182
"""Check whether any of the passed non-zero assumptions is implied by the zero assumptions"""
178-
zero = R.ideal(map(numerator, con.zero))
183+
zero = R.ideal(list(map(numerator, con.zero)))
179184
if 1 in zero:
180185
return True
181186
# First a cheap check whether any of the individual nonzero terms conflict on
@@ -195,7 +200,7 @@ def conflicts(R, con):
195200

196201
def get_nonzero_set(R, assume):
197202
"""Calculate a simple set of nonzero expressions"""
198-
zero = R.ideal(map(numerator, assume.zero))
203+
zero = R.ideal(list(map(numerator, assume.zero)))
199204
nonzero = set()
200205
for nz in map(numerator, assume.nonzero):
201206
for (f,n) in nz.factor():
@@ -208,7 +213,7 @@ def get_nonzero_set(R, assume):
208213

209214
def prove_nonzero(R, exprs, assume):
210215
"""Check whether an expression is provably nonzero, given assumptions"""
211-
zero = R.ideal(map(numerator, assume.zero))
216+
zero = R.ideal(list(map(numerator, assume.zero)))
212217
nonzero = get_nonzero_set(R, assume)
213218
expl = set()
214219
ok = True
@@ -250,7 +255,7 @@ def prove_zero(R, exprs, assume):
250255
r, e = prove_nonzero(R, dict(map(lambda x: (fastfrac(R, x.bot, 1), exprs[x]), exprs)), assume)
251256
if not r:
252257
return (False, map(lambda x: "Possibly zero denominator: %s" % x, e))
253-
zero = R.ideal(map(numerator, assume.zero))
258+
zero = R.ideal(list(map(numerator, assume.zero)))
254259
nonzero = prod(x for x in assume.nonzero)
255260
expl = []
256261
for expr in exprs:
@@ -265,8 +270,8 @@ def describe_extra(R, assume, assumeExtra):
265270
"""Describe what assumptions are added, given existing assumptions"""
266271
zerox = assume.zero.copy()
267272
zerox.update(assumeExtra.zero)
268-
zero = R.ideal(map(numerator, assume.zero))
269-
zeroextra = R.ideal(map(numerator, zerox))
273+
zero = R.ideal(list(map(numerator, assume.zero)))
274+
zeroextra = R.ideal(list(map(numerator, zerox)))
270275
nonzero = get_nonzero_set(R, assume)
271276
ret = set()
272277
# Iterate over the extra zero expressions

src/secp256k1/sage/weierstrass_prover.sage

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,24 @@ laws_jacobian_weierstrass = {
175175
def check_exhaustive_jacobian_weierstrass(name, A, B, branches, formula, p):
176176
"""Verify an implementation of addition of Jacobian points on a Weierstrass curve, by executing and validating the result for every possible addition in a prime field"""
177177
F = Integers(p)
178-
print "Formula %s on Z%i:" % (name, p)
178+
print("Formula %s on Z%i:" % (name, p))
179179
points = []
180-
for x in xrange(0, p):
181-
for y in xrange(0, p):
180+
for x in range(0, p):
181+
for y in range(0, p):
182182
point = affinepoint(F(x), F(y))
183183
r, e = concrete_verify(on_weierstrass_curve(A, B, point))
184184
if r:
185185
points.append(point)
186186

187-
for za in xrange(1, p):
188-
for zb in xrange(1, p):
187+
for za in range(1, p):
188+
for zb in range(1, p):
189189
for pa in points:
190190
for pb in points:
191-
for ia in xrange(2):
192-
for ib in xrange(2):
191+
for ia in range(2):
192+
for ib in range(2):
193193
pA = jacobianpoint(pa.x * F(za)^2, pa.y * F(za)^3, F(za), ia)
194194
pB = jacobianpoint(pb.x * F(zb)^2, pb.y * F(zb)^3, F(zb), ib)
195-
for branch in xrange(0, branches):
195+
for branch in range(0, branches):
196196
assumeAssert, assumeBranch, pC = formula(branch, pA, pB)
197197
pC.X = F(pC.X)
198198
pC.Y = F(pC.Y)
@@ -206,13 +206,13 @@ def check_exhaustive_jacobian_weierstrass(name, A, B, branches, formula, p):
206206
r, e = concrete_verify(assumeLaw)
207207
if r:
208208
if match:
209-
print " multiple branches for (%s,%s,%s,%s) + (%s,%s,%s,%s)" % (pA.X, pA.Y, pA.Z, pA.Infinity, pB.X, pB.Y, pB.Z, pB.Infinity)
209+
print(" multiple branches for (%s,%s,%s,%s) + (%s,%s,%s,%s)" % (pA.X, pA.Y, pA.Z, pA.Infinity, pB.X, pB.Y, pB.Z, pB.Infinity))
210210
else:
211211
match = True
212212
r, e = concrete_verify(require)
213213
if not r:
214-
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)
215-
print
214+
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))
215+
print()
216216

217217

218218
def check_symbolic_function(R, assumeAssert, assumeBranch, f, A, B, pa, pb, pA, pB, pC):
@@ -242,9 +242,9 @@ def check_symbolic_jacobian_weierstrass(name, A, B, branches, formula):
242242
for key in laws_jacobian_weierstrass:
243243
res[key] = []
244244

245-
print ("Formula " + name + ":")
245+
print("Formula " + name + ":")
246246
count = 0
247-
for branch in xrange(branches):
247+
for branch in range(branches):
248248
assumeFormula, assumeBranch, pC = formula(branch, pA, pB)
249249
pC.X = lift(pC.X)
250250
pC.Y = lift(pC.Y)
@@ -255,10 +255,10 @@ def check_symbolic_jacobian_weierstrass(name, A, B, branches, formula):
255255
res[key].append((check_symbolic_function(R, assumeFormula, assumeBranch, laws_jacobian_weierstrass[key], A, B, pa, pb, pA, pB, pC), branch))
256256

257257
for key in res:
258-
print " %s:" % key
258+
print(" %s:" % key)
259259
val = res[key]
260260
for x in val:
261261
if x[0] is not None:
262-
print " branch %i: %s" % (x[1], x[0])
262+
print(" branch %i: %s" % (x[1], x[0]))
263263

264-
print
264+
print()

src/secp256k1/src/modules/rangeproof/tests_impl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static void test_rangeproof(void) {
498498
CHECK(maxv >= v);
499499
}
500500
memcpy(&commit2, &commit, sizeof(commit));
501-
for (i = 0; i < (size_t) 2*count; i++) {
501+
for (i = 0; i < (size_t) count; i++) {
502502
int exp;
503503
int min_bits;
504504
v = secp256k1_testrandi64(0, UINT64_MAX >> (secp256k1_testrand32()&63));
@@ -532,11 +532,11 @@ static void test_rangeproof(void) {
532532
CHECK(secp256k1_rangeproof_rewind(ctx, blindout, &vout, NULL, NULL, commit.data, &minv, &maxv, &commit, proof, len, NULL, 0, secp256k1_generator_h));
533533
memcpy(&commit2, &commit, sizeof(commit));
534534
}
535-
for (j = 0; j < 5; j++) {
535+
for (j = 0; j < 3; j++) {
536536
for (i = 0; i < 96; i++) {
537537
secp256k1_testrand256(&proof[i * 32]);
538538
}
539-
for (k = 0; k < 128; k++) {
539+
for (k = 0; k < 128; k += 3) {
540540
len = k;
541541
CHECK(!secp256k1_rangeproof_verify(ctx, &minv, &maxv, &commit2, proof, len, NULL, 0, secp256k1_generator_h));
542542
}
@@ -696,10 +696,10 @@ void run_rangeproof_tests(void) {
696696
test_api();
697697
test_rangeproof_fixed_vectors();
698698
test_pedersen_commitment_fixed_vector();
699-
for (i = 0; i < 10*count; i++) {
699+
for (i = 0; i < count / 2 + 1; i++) {
700700
test_pedersen();
701701
}
702-
for (i = 0; i < 10*count; i++) {
702+
for (i = 0; i < count / 2 + 1; i++) {
703703
test_borromean();
704704
}
705705
test_rangeproof();

src/secp256k1/src/modules/surjection/tests_impl.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,7 @@ void test_fixed_vectors(void) {
667667
}
668668

669669
void run_surjection_tests(void) {
670-
int i;
671-
for (i = 0; i < count; i++) {
672-
test_surjectionproof_api();
673-
}
670+
test_surjectionproof_api();
674671
test_fixed_vectors();
675672

676673
test_input_selection(0);

src/secp256k1/src/tests.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5802,6 +5802,15 @@ int main(int argc, char **argv) {
58025802
/* find iteration count */
58035803
if (argc > 1) {
58045804
count = strtol(argv[1], NULL, 0);
5805+
} else {
5806+
const char* env = getenv("SECP256K1_TEST_ITERS");
5807+
if (env) {
5808+
count = strtol(env, NULL, 0);
5809+
}
5810+
}
5811+
if (count <= 0) {
5812+
fputs("An iteration count of 0 or less is not allowed.\n", stderr);
5813+
return EXIT_FAILURE;
58055814
}
58065815
printf("test count = %i\n", count);
58075816

0 commit comments

Comments
 (0)