Skip to content

Commit f793514

Browse files
committed
tests: impl test_batch_tagged_sha and update api_tests with null values
1 parent c41eb65 commit f793514

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

include/secp256k1_schnorrsig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "secp256k1.h"
55
#include "secp256k1_extrakeys.h"
6-
#include "include/secp256k1_batch.h"
6+
#include "secp256k1_batch.h"
77

88
#ifdef __cplusplus
99
extern "C" {

src/modules/batch/tests_impl.h

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,78 @@
55

66
#define MAX_TERMS 10
77

8+
9+
/* Tests for the equality of two sha256 structs. This function only produces a
10+
* correct result if an integer multiple of 64 many bytes have been written
11+
* into the hash functions. */
12+
void batch_test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256 *sha2) {
13+
/* Is buffer fully consumed? */
14+
CHECK((sha1->bytes & 0x3F) == 0);
15+
16+
CHECK(sha1->bytes == sha2->bytes);
17+
CHECK(secp256k1_memcmp_var(sha1->s, sha2->s, sizeof(sha1->s)) == 0);
18+
}
19+
20+
/* Checks that hash initialized by secp256k1_batch_sha256_tagged has the
21+
* expected state. */
22+
void test_batch_sha256_tagged(void) {
23+
unsigned char tag[13] = "BIP0340/batch";
24+
secp256k1_sha256 sha;
25+
secp256k1_sha256 sha_optimized;
26+
27+
secp256k1_sha256_initialize_tagged(&sha, (unsigned char *) tag, sizeof(tag));
28+
secp256k1_batch_sha256_tagged(&sha_optimized);
29+
batch_test_sha256_eq(&sha, &sha_optimized);
30+
}
31+
832
void test_batch_api(void) {
933
/** setup **/
1034
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
1135
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
1236
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
1337
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
38+
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_no_precomp);
1439
secp256k1_batch *batch_none;
1540
secp256k1_batch *batch_sign;
1641
secp256k1_batch *batch_vrfy;
1742
secp256k1_batch *batch_both;
43+
secp256k1_batch *batch_sttc;
1844
int ecount;
1945

2046
secp256k1_context_set_error_callback(none, counting_illegal_callback_fn, &ecount);
2147
secp256k1_context_set_error_callback(sign, counting_illegal_callback_fn, &ecount);
2248
secp256k1_context_set_error_callback(vrfy, counting_illegal_callback_fn, &ecount);
2349
secp256k1_context_set_error_callback(both, counting_illegal_callback_fn, &ecount);
50+
secp256k1_context_set_error_callback(sttc, counting_illegal_callback_fn, &ecount);
2451
secp256k1_context_set_illegal_callback(none, counting_illegal_callback_fn, &ecount);
2552
secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount);
2653
secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount);
2754
secp256k1_context_set_illegal_callback(both, counting_illegal_callback_fn, &ecount);
55+
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
2856

2957
/** main test body **/
58+
/* todo: need to add tests for 1/4th, 3/4th of MAX_TERMS size? */
3059
ecount = 0;
3160
batch_none = secp256k1_batch_create(none, MAX_TERMS);
3261
CHECK(batch_none != NULL);
3362
CHECK(ecount == 0);
34-
batch_sign = secp256k1_batch_create(sign, MAX_TERMS);
63+
batch_sign = secp256k1_batch_create(sign, MAX_TERMS/2);
3564
CHECK(batch_sign != NULL);
3665
CHECK(ecount == 0);
37-
batch_vrfy = secp256k1_batch_create(vrfy, MAX_TERMS);
66+
batch_vrfy = secp256k1_batch_create(vrfy, MAX_TERMS-1);
3867
CHECK(batch_vrfy != NULL);
3968
CHECK(ecount == 0);
40-
batch_both = secp256k1_batch_create(both, MAX_TERMS);
69+
batch_both = secp256k1_batch_create(both, 1);
4170
CHECK(batch_both != NULL);
4271
CHECK(ecount == 0);
72+
/* ARG_CHECK(max_terms != 0) in `batch_create` should fail*/
73+
batch_sttc = secp256k1_batch_create(sttc, 0);
74+
CHECK(batch_sttc == NULL);
75+
CHECK(ecount == 1);
76+
/* ARG_CHECK(max_terms <= SIZE_MAX/2) in `batch_create` should fail*/
77+
batch_sttc = secp256k1_batch_create(sttc, SIZE_MAX - 1);
78+
CHECK(batch_sttc == NULL);
79+
CHECK(ecount == 2);
4380

4481
ecount = 0;
4582
CHECK(secp256k1_batch_verify(none, batch_none) == 1);
@@ -50,6 +87,8 @@ void test_batch_api(void) {
5087
CHECK(ecount == 0);
5188
CHECK(secp256k1_batch_verify(both, batch_both) == 1);
5289
CHECK(ecount == 0);
90+
CHECK(secp256k1_batch_verify(sttc, NULL) == 0);
91+
CHECK(ecount == 1);
5392

5493
ecount = 0;
5594
secp256k1_batch_destroy(none, batch_none);
@@ -60,16 +99,20 @@ void test_batch_api(void) {
6099
CHECK(ecount == 0);
61100
secp256k1_batch_destroy(both, batch_both);
62101
CHECK(ecount == 0);
102+
secp256k1_batch_destroy(sttc, NULL);
103+
CHECK(ecount == 0);
63104

64105
secp256k1_context_destroy(none);
65106
secp256k1_context_destroy(sign);
66107
secp256k1_context_destroy(vrfy);
67108
secp256k1_context_destroy(both);
109+
secp256k1_context_destroy(sttc);
68110
}
69111

70112

71113
void run_batch_tests(void) {
72114
test_batch_api();
115+
test_batch_sha256_tagged();
73116
}
74117

75118
#endif /* SECP256K1_MODULE_BATCH_TESTS_H */

0 commit comments

Comments
 (0)