@@ -258,28 +258,31 @@ void run_scratch_tests(void) {
258
258
259
259
/* Test public API */
260
260
secp256k1_context_set_illegal_callback (none , counting_illegal_callback_fn , & ecount );
261
- scratch = secp256k1_scratch_space_create (none , 100 , 10 );
262
- CHECK (scratch == NULL );
263
- CHECK (ecount == 1 );
264
-
265
- scratch = secp256k1_scratch_space_create (none , 100 , 100 );
266
- CHECK (scratch != NULL );
267
- CHECK (ecount == 1 );
268
- secp256k1_scratch_space_destroy (scratch );
269
261
270
- scratch = secp256k1_scratch_space_create (none , 100 , 1000 );
262
+ scratch = secp256k1_scratch_space_create (none , 1000 );
271
263
CHECK (scratch != NULL );
272
- CHECK (ecount == 1 );
264
+ CHECK (ecount == 0 );
273
265
274
266
/* Test internal API */
275
267
CHECK (secp256k1_scratch_max_allocation (scratch , 0 ) == 1000 );
276
268
CHECK (secp256k1_scratch_max_allocation (scratch , 1 ) < 1000 );
277
- CHECK (secp256k1_scratch_resize (scratch , 50 , 1 ) == 1 ); /* no-op */
278
- CHECK (secp256k1_scratch_resize (scratch , 200 , 1 ) == 1 );
279
- CHECK (secp256k1_scratch_resize (scratch , 950 , 1 ) == 1 );
280
- CHECK (secp256k1_scratch_resize (scratch , 1000 , 1 ) == 0 );
281
- CHECK (secp256k1_scratch_resize (scratch , 2000 , 1 ) == 0 );
269
+
270
+ /* Allocating 500 bytes with no frame fails */
271
+ CHECK (secp256k1_scratch_alloc (scratch , 500 ) == NULL );
272
+ CHECK (secp256k1_scratch_max_allocation (scratch , 0 ) == 1000 );
273
+
274
+ /* ...but pushing a new stack frame does affect the max allocation */
275
+ CHECK (secp256k1_scratch_allocate_frame (scratch , 500 , 1 == 1 ));
276
+ CHECK (secp256k1_scratch_max_allocation (scratch , 1 ) < 500 ); /* 500 - ALIGNMENT */
277
+ CHECK (secp256k1_scratch_alloc (scratch , 500 ) != NULL );
278
+ CHECK (secp256k1_scratch_alloc (scratch , 500 ) == NULL );
279
+
280
+ CHECK (secp256k1_scratch_allocate_frame (scratch , 500 , 1 ) == 0 );
281
+
282
+ /* ...and this effect is undone by popping the frame */
283
+ secp256k1_scratch_deallocate_frame (scratch );
282
284
CHECK (secp256k1_scratch_max_allocation (scratch , 0 ) == 1000 );
285
+ CHECK (secp256k1_scratch_alloc (scratch , 500 ) == NULL );
283
286
284
287
/* cleanup */
285
288
secp256k1_scratch_space_destroy (scratch );
@@ -2558,7 +2561,6 @@ void test_ecmult_multi(secp256k1_scratch *scratch, secp256k1_ecmult_multi_func e
2558
2561
data .sc = sc ;
2559
2562
data .pt = pt ;
2560
2563
secp256k1_scalar_set_int (& szero , 0 );
2561
- secp256k1_scratch_reset (scratch );
2562
2564
2563
2565
/* No points to multiply */
2564
2566
CHECK (ecmult_multi (& ctx -> ecmult_ctx , scratch , & r , NULL , ecmult_multi_callback , & data , 0 ));
@@ -2590,7 +2592,7 @@ void test_ecmult_multi(secp256k1_scratch *scratch, secp256k1_ecmult_multi_func e
2590
2592
CHECK (secp256k1_gej_is_infinity (& r ));
2591
2593
2592
2594
/* Try to multiply 1 point, but scratch space is empty */
2593
- scratch_empty = secp256k1_scratch_create (& ctx -> error_callback , 0 , 0 );
2595
+ scratch_empty = secp256k1_scratch_create (& ctx -> error_callback , 0 );
2594
2596
CHECK (!ecmult_multi (& ctx -> ecmult_ctx , scratch_empty , & r , & szero , ecmult_multi_callback , & data , 1 ));
2595
2597
secp256k1_scratch_destroy (scratch_empty );
2596
2598
@@ -2816,15 +2818,16 @@ void test_ecmult_multi_pippenger_max_points(void) {
2816
2818
int bucket_window = 0 ;
2817
2819
2818
2820
for (; scratch_size < max_size ; scratch_size += 256 ) {
2819
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , scratch_size );
2821
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , scratch_size );
2820
2822
CHECK (scratch != NULL );
2821
2823
n_points_supported = secp256k1_pippenger_max_points (scratch );
2822
2824
if (n_points_supported == 0 ) {
2823
2825
secp256k1_scratch_destroy (scratch );
2824
2826
continue ;
2825
2827
}
2826
2828
bucket_window = secp256k1_pippenger_bucket_window (n_points_supported );
2827
- CHECK (secp256k1_scratch_resize (scratch , secp256k1_pippenger_scratch_size (n_points_supported , bucket_window ), PIPPENGER_SCRATCH_OBJECTS ));
2829
+ CHECK (secp256k1_scratch_allocate_frame (scratch , secp256k1_pippenger_scratch_size (n_points_supported , bucket_window ), PIPPENGER_SCRATCH_OBJECTS ));
2830
+ secp256k1_scratch_deallocate_frame (scratch );
2828
2831
secp256k1_scratch_destroy (scratch );
2829
2832
}
2830
2833
CHECK (bucket_window == PIPPENGER_MAX_BUCKET_WINDOW );
@@ -2866,13 +2869,13 @@ void test_ecmult_multi_batching(void) {
2866
2869
data .pt = pt ;
2867
2870
2868
2871
/* Test with empty scratch space */
2869
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , 0 );
2872
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 );
2870
2873
CHECK (!secp256k1_ecmult_multi_var (& ctx -> ecmult_ctx , scratch , & r , & scG , ecmult_multi_callback , & data , 1 ));
2871
2874
secp256k1_scratch_destroy (scratch );
2872
2875
2873
2876
/* Test with space for 1 point in pippenger. That's not enough because
2874
2877
* ecmult_multi selects strauss which requires more memory. */
2875
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , secp256k1_pippenger_scratch_size (1 , 1 ) + PIPPENGER_SCRATCH_OBJECTS * ALIGNMENT );
2878
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , secp256k1_pippenger_scratch_size (1 , 1 ) + PIPPENGER_SCRATCH_OBJECTS * ALIGNMENT );
2876
2879
CHECK (!secp256k1_ecmult_multi_var (& ctx -> ecmult_ctx , scratch , & r , & scG , ecmult_multi_callback , & data , 1 ));
2877
2880
secp256k1_scratch_destroy (scratch );
2878
2881
@@ -2881,10 +2884,10 @@ void test_ecmult_multi_batching(void) {
2881
2884
if (i > ECMULT_PIPPENGER_THRESHOLD ) {
2882
2885
int bucket_window = secp256k1_pippenger_bucket_window (i );
2883
2886
size_t scratch_size = secp256k1_pippenger_scratch_size (i , bucket_window );
2884
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , scratch_size + PIPPENGER_SCRATCH_OBJECTS * ALIGNMENT );
2887
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , scratch_size + PIPPENGER_SCRATCH_OBJECTS * ALIGNMENT );
2885
2888
} else {
2886
2889
size_t scratch_size = secp256k1_strauss_scratch_size (i );
2887
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , scratch_size + STRAUSS_SCRATCH_OBJECTS * ALIGNMENT );
2890
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , scratch_size + STRAUSS_SCRATCH_OBJECTS * ALIGNMENT );
2888
2891
}
2889
2892
CHECK (secp256k1_ecmult_multi_var (& ctx -> ecmult_ctx , scratch , & r , & scG , ecmult_multi_callback , & data , n_points ));
2890
2893
secp256k1_gej_add_var (& r , & r , & r2 , NULL );
@@ -2900,14 +2903,14 @@ void run_ecmult_multi_tests(void) {
2900
2903
2901
2904
test_secp256k1_pippenger_bucket_window_inv ();
2902
2905
test_ecmult_multi_pippenger_max_points ();
2903
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , 819200 );
2906
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , 819200 );
2904
2907
test_ecmult_multi (scratch , secp256k1_ecmult_multi_var );
2905
2908
test_ecmult_multi (scratch , secp256k1_ecmult_pippenger_batch_single );
2906
2909
test_ecmult_multi (scratch , secp256k1_ecmult_strauss_batch_single );
2907
2910
secp256k1_scratch_destroy (scratch );
2908
2911
2909
2912
/* Run test_ecmult_multi with space for exactly one point */
2910
- scratch = secp256k1_scratch_create (& ctx -> error_callback , 0 , secp256k1_strauss_scratch_size (1 ) + STRAUSS_SCRATCH_OBJECTS * ALIGNMENT );
2913
+ scratch = secp256k1_scratch_create (& ctx -> error_callback , secp256k1_strauss_scratch_size (1 ) + STRAUSS_SCRATCH_OBJECTS * ALIGNMENT );
2911
2914
test_ecmult_multi (scratch , secp256k1_ecmult_multi_var );
2912
2915
secp256k1_scratch_destroy (scratch );
2913
2916
0 commit comments