Skip to content

Commit a340d95

Browse files
jonasnickroconnor-blockstream
authored andcommitted
ci: add int128_struct tests
1 parent dceaa1f commit a340d95

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

.cirrus.yml

+10-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ task:
6969
- env: {WIDEMUL: int64, RECOVERY: yes}
7070
- env: {WIDEMUL: int64, ECDH: yes, SCHNORRSIG: yes}
7171
- env: {WIDEMUL: int128}
72+
- env: {WIDEMUL: int128_struct}
7273
- env: {WIDEMUL: int128, RECOVERY: yes, SCHNORRSIG: yes}
7374
- env: {WIDEMUL: int128, ECDH: yes, SCHNORRSIG: yes}
7475
- env: {WIDEMUL: int128, ASM: x86_64}
@@ -272,20 +273,22 @@ task:
272273
EXPERIMENTAL: yes
273274
SCHNORRSIG: yes
274275
CTIMETEST: no
276+
# Use a MinGW-w64 host to tell ./configure we're building for Windows.
277+
# This will detect some MinGW-w64 tools but then make will need only
278+
# the MSVC tools CC, AR and NM as specified below.
279+
HOST: x86_64-w64-mingw32
280+
CC: /opt/msvc/bin/x64/cl
281+
AR: /opt/msvc/bin/x64/lib
282+
NM: /opt/msvc/bin/x64/dumpbin -symbols -headers
275283
# Set non-essential options that affect the CLI messages here.
276284
# (They depend on the user's taste, so we don't want to set them automatically in configure.ac.)
277285
CFLAGS: -nologo -diagnostics:caret
278286
LDFLAGS: -XCClinker -nologo -XCClinker -diagnostics:caret
279-
# Use a MinGW-w64 host to tell ./configure we're building for Windows.
280-
# This will detect some MinGW-w64 tools but then make will need only
281-
# the MSVC tools CC, AR and NM as specified below.
282287
matrix:
283288
- name: "x86_64 (MSVC): Windows (Debian stable, Wine)"
289+
- name: "x86_64 (MSVC): Windows (Debian stable, Wine, int128_struct)"
284290
env:
285-
HOST: x86_64-w64-mingw32
286-
CC: /opt/msvc/bin/x64/cl
287-
AR: /opt/msvc/bin/x64/lib
288-
NM: /opt/msvc/bin/x64/dumpbin -symbols -headers
291+
WIDEMUL: int128_struct
289292
- name: "i686 (MSVC): Windows (Debian stable, Wine)"
290293
env:
291294
HOST: i686-w64-mingw32

src/tests.c

+45
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "modinv32_impl.h"
2727
#ifdef SECP256K1_WIDEMUL_INT128
2828
#include "modinv64_impl.h"
29+
#include "int128_impl.h"
2930
#endif
3031

3132
#define CONDITIONAL_TEST(cnt, nam) if (count < (cnt)) { printf("Skipping %s (iteration count too low)\n", nam); } else
@@ -430,6 +431,47 @@ void run_scratch_tests(void) {
430431
secp256k1_context_destroy(none);
431432
}
432433

434+
435+
#ifdef SECP256K1_WIDEMUL_INT128
436+
void run_int128_tests(void) {
437+
{ /* secp256k1_u128_accum_mul */
438+
secp256k1_uint128 res;
439+
440+
/* Check secp256k1_u128_accum_mul overflow */
441+
secp256k1_u128_from_u64(&res, 0);
442+
secp256k1_u128_accum_mul(&res, UINT64_MAX, UINT64_MAX);
443+
secp256k1_u128_accum_mul(&res, UINT64_MAX, UINT64_MAX);
444+
CHECK(secp256k1_u128_to_u64(&res) == 2);
445+
CHECK(secp256k1_u128_hi_u64(&res) == 18446744073709551612U);
446+
}
447+
{ /* secp256k1_u128_accum_mul */
448+
secp256k1_int128 res;
449+
450+
/* Compute INT128_MAX = 2^127 - 1 with secp256k1_i128_accum_mul */
451+
secp256k1_i128_from_i64(&res, 0);
452+
secp256k1_i128_accum_mul(&res, INT64_MAX, INT64_MAX);
453+
secp256k1_i128_accum_mul(&res, INT64_MAX, INT64_MAX);
454+
CHECK(secp256k1_i128_to_i64(&res) == 2);
455+
secp256k1_i128_accum_mul(&res, 4, 9223372036854775807);
456+
secp256k1_i128_accum_mul(&res, 1, 1);
457+
CHECK((uint64_t)secp256k1_i128_to_i64(&res) == UINT64_MAX);
458+
secp256k1_i128_rshift(&res, 64);
459+
CHECK(secp256k1_i128_to_i64(&res) == INT64_MAX);
460+
461+
/* Compute INT128_MIN = - 2^127 with secp256k1_i128_accum_mul */
462+
secp256k1_i128_from_i64(&res, 0);
463+
secp256k1_i128_accum_mul(&res, INT64_MAX, INT64_MIN);
464+
CHECK(secp256k1_i128_to_i64(&res) == INT64_MIN);
465+
secp256k1_i128_accum_mul(&res, INT64_MAX, INT64_MIN);
466+
CHECK(secp256k1_i128_to_i64(&res) == 0);
467+
secp256k1_i128_accum_mul(&res, 2, INT64_MIN);
468+
CHECK(secp256k1_i128_to_i64(&res) == 0);
469+
secp256k1_i128_rshift(&res, 64);
470+
CHECK(secp256k1_i128_to_i64(&res) == INT64_MIN);
471+
}
472+
}
473+
#endif
474+
433475
void run_ctz_tests(void) {
434476
static const uint32_t b32[] = {1, 0xffffffff, 0x5e56968f, 0xe0d63129};
435477
static const uint64_t b64[] = {1, 0xffffffffffffffff, 0xbcd02462139b3fc3, 0x98b5f80c769693ef};
@@ -7100,6 +7142,9 @@ int main(int argc, char **argv) {
71007142
run_rand_bits();
71017143
run_rand_int();
71027144

7145+
#ifdef SECP256K1_WIDEMUL_INT128
7146+
run_int128_tests();
7147+
#endif
71037148
run_ctz_tests();
71047149
run_modinv_tests();
71057150
run_inverse_tests();

0 commit comments

Comments
 (0)