5
5
6
6
#define MAX_TERMS 10
7
7
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
+
8
32
void test_batch_api (void ) {
9
33
/** setup **/
10
34
secp256k1_context * none = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
11
35
secp256k1_context * sign = secp256k1_context_create (SECP256K1_CONTEXT_SIGN );
12
36
secp256k1_context * vrfy = secp256k1_context_create (SECP256K1_CONTEXT_VERIFY );
13
37
secp256k1_context * both = secp256k1_context_create (SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY );
38
+ secp256k1_context * sttc = secp256k1_context_clone (secp256k1_context_no_precomp );
14
39
secp256k1_batch * batch_none ;
15
40
secp256k1_batch * batch_sign ;
16
41
secp256k1_batch * batch_vrfy ;
17
42
secp256k1_batch * batch_both ;
43
+ secp256k1_batch * batch_sttc ;
18
44
int ecount ;
19
45
20
46
secp256k1_context_set_error_callback (none , counting_illegal_callback_fn , & ecount );
21
47
secp256k1_context_set_error_callback (sign , counting_illegal_callback_fn , & ecount );
22
48
secp256k1_context_set_error_callback (vrfy , counting_illegal_callback_fn , & ecount );
23
49
secp256k1_context_set_error_callback (both , counting_illegal_callback_fn , & ecount );
50
+ secp256k1_context_set_error_callback (sttc , counting_illegal_callback_fn , & ecount );
24
51
secp256k1_context_set_illegal_callback (none , counting_illegal_callback_fn , & ecount );
25
52
secp256k1_context_set_illegal_callback (sign , counting_illegal_callback_fn , & ecount );
26
53
secp256k1_context_set_illegal_callback (vrfy , counting_illegal_callback_fn , & ecount );
27
54
secp256k1_context_set_illegal_callback (both , counting_illegal_callback_fn , & ecount );
55
+ secp256k1_context_set_illegal_callback (sttc , counting_illegal_callback_fn , & ecount );
28
56
29
57
/** main test body **/
58
+ /* todo: need to add tests for 1/4th, 3/4th of MAX_TERMS size? */
30
59
ecount = 0 ;
31
60
batch_none = secp256k1_batch_create (none , MAX_TERMS );
32
61
CHECK (batch_none != NULL );
33
62
CHECK (ecount == 0 );
34
- batch_sign = secp256k1_batch_create (sign , MAX_TERMS );
63
+ batch_sign = secp256k1_batch_create (sign , MAX_TERMS / 2 );
35
64
CHECK (batch_sign != NULL );
36
65
CHECK (ecount == 0 );
37
- batch_vrfy = secp256k1_batch_create (vrfy , MAX_TERMS );
66
+ batch_vrfy = secp256k1_batch_create (vrfy , MAX_TERMS - 1 );
38
67
CHECK (batch_vrfy != NULL );
39
68
CHECK (ecount == 0 );
40
- batch_both = secp256k1_batch_create (both , MAX_TERMS );
69
+ batch_both = secp256k1_batch_create (both , 1 );
41
70
CHECK (batch_both != NULL );
42
71
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 );
43
80
44
81
ecount = 0 ;
45
82
CHECK (secp256k1_batch_verify (none , batch_none ) == 1 );
@@ -50,6 +87,8 @@ void test_batch_api(void) {
50
87
CHECK (ecount == 0 );
51
88
CHECK (secp256k1_batch_verify (both , batch_both ) == 1 );
52
89
CHECK (ecount == 0 );
90
+ CHECK (secp256k1_batch_verify (sttc , NULL ) == 0 );
91
+ CHECK (ecount == 1 );
53
92
54
93
ecount = 0 ;
55
94
secp256k1_batch_destroy (none , batch_none );
@@ -60,16 +99,20 @@ void test_batch_api(void) {
60
99
CHECK (ecount == 0 );
61
100
secp256k1_batch_destroy (both , batch_both );
62
101
CHECK (ecount == 0 );
102
+ secp256k1_batch_destroy (sttc , NULL );
103
+ CHECK (ecount == 0 );
63
104
64
105
secp256k1_context_destroy (none );
65
106
secp256k1_context_destroy (sign );
66
107
secp256k1_context_destroy (vrfy );
67
108
secp256k1_context_destroy (both );
109
+ secp256k1_context_destroy (sttc );
68
110
}
69
111
70
112
71
113
void run_batch_tests (void ) {
72
114
test_batch_api ();
115
+ test_batch_sha256_tagged ();
73
116
}
74
117
75
118
#endif /* SECP256K1_MODULE_BATCH_TESTS_H */
0 commit comments