Skip to content

Commit 548de42

Browse files
apoelstrajonasnick
authored andcommitted
add resizeable scratch space API
Alignment support by Pieter Wuille.
1 parent 6ad5cdb commit 548de42

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed

Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ noinst_HEADERS += src/field_5x52_asm_impl.h
4242
noinst_HEADERS += src/java/org_bitcoin_NativeSecp256k1.h
4343
noinst_HEADERS += src/java/org_bitcoin_Secp256k1Context.h
4444
noinst_HEADERS += src/util.h
45+
noinst_HEADERS += src/scratch.h
46+
noinst_HEADERS += src/scratch_impl.h
4547
noinst_HEADERS += src/testrand.h
4648
noinst_HEADERS += src/testrand_impl.h
4749
noinst_HEADERS += src/hash.h

include/secp256k1.h

+35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ extern "C" {
4242
*/
4343
typedef struct secp256k1_context_struct secp256k1_context;
4444

45+
/** Opaque data structure that holds rewriteable "scratch space"
46+
*
47+
* The purpose of this structure is to replace dynamic memory allocations,
48+
* because we target architectures where this may not be available. It is
49+
* essentially a resizable (within specified parameters) block of bytes,
50+
* which is initially created either by memory allocation or TODO as a pointer
51+
* into some fixed rewritable space.
52+
*
53+
* Unlike the context object, this cannot safely be shared between threads
54+
* without additional synchronization logic.
55+
*/
56+
typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
57+
4558
/** Opaque data structure that holds a parsed and valid public key.
4659
*
4760
* The exact representation of data inside is implementation defined and not
@@ -243,6 +256,28 @@ SECP256K1_API void secp256k1_context_set_error_callback(
243256
const void* data
244257
) SECP256K1_ARG_NONNULL(1);
245258

259+
/** Create a secp256k1 scratch space object.
260+
*
261+
* Returns: a newly created scratch space.
262+
* Args: ctx: an existing context object (cannot be NULL)
263+
* In: init_size: initial amount of memory to allocate
264+
* max_size: maximum amount of memory to allocate
265+
*/
266+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
267+
const secp256k1_context* ctx,
268+
size_t init_size,
269+
size_t max_size
270+
) SECP256K1_ARG_NONNULL(1);
271+
272+
/** Destroy a secp256k1 scratch space.
273+
*
274+
* The pointer may not be used afterwards.
275+
* Args: scratch: space to destroy
276+
*/
277+
SECP256K1_API void secp256k1_scratch_space_destroy(
278+
secp256k1_scratch_space* scratch
279+
);
280+
246281
/** Parse a variable-length public key into the pubkey object.
247282
*
248283
* Returns: 1 if the public key was fully valid.

src/scratch.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**********************************************************************
2+
* Copyright (c) 2017 Andrew Poelstra *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5+
**********************************************************************/
6+
7+
#ifndef _SECP256K1_SCRATCH_
8+
#define _SECP256K1_SCRATCH_
9+
10+
/* The typedef is used internally; the struct name is used in the public API
11+
* (where it is exposed as a different typedef) */
12+
typedef struct secp256k1_scratch_space_struct {
13+
void *data;
14+
size_t offset;
15+
size_t init_size;
16+
size_t max_size;
17+
const secp256k1_callback* error_callback;
18+
} secp256k1_scratch;
19+
20+
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t init_size, size_t max_size);
21+
static void secp256k1_scratch_destroy(secp256k1_scratch* scratch);
22+
23+
/** Returns the maximum allocation the scratch space will allow */
24+
static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch, size_t n_objects);
25+
26+
/** Attempts to allocate so that there are `n` available bytes. Returns 1 on success, 0 on failure */
27+
static int secp256k1_scratch_resize(secp256k1_scratch* scratch, size_t n, size_t n_objects);
28+
29+
/** Returns a pointer into the scratch space or NULL if there is insufficient available space */
30+
static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t n);
31+
32+
/** Resets the returned pointer to the beginning of space */
33+
static void secp256k1_scratch_reset(secp256k1_scratch* scratch);
34+
35+
#endif

src/scratch_impl.h

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**********************************************************************
2+
* Copyright (c) 2017 Andrew Poelstra *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5+
**********************************************************************/
6+
7+
#ifndef _SECP256K1_SCRATCH_IMPL_H_
8+
#define _SECP256K1_SCRATCH_IMPL_H_
9+
10+
#include "scratch.h"
11+
12+
/* Using 16 bytes alignment because common architectures never have alignment
13+
* requirements above 8 for any of the types we care about. In addition we
14+
* leave some room because currently we don't care about a few bytes.
15+
* TODO: Determine this at configure time. */
16+
#define ALIGNMENT 16
17+
18+
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t init_size, size_t max_size) {
19+
secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret));
20+
if (ret != NULL) {
21+
ret->data = checked_malloc(error_callback, init_size);
22+
if (ret->data == NULL) {
23+
free (ret);
24+
return NULL;
25+
}
26+
ret->offset = 0;
27+
ret->init_size = init_size;
28+
ret->max_size = max_size;
29+
ret->error_callback = error_callback;
30+
}
31+
return ret;
32+
}
33+
34+
static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
35+
if (scratch != NULL) {
36+
free(scratch->data);
37+
free(scratch);
38+
}
39+
}
40+
41+
static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch, size_t objects) {
42+
if (scratch->max_size <= objects * ALIGNMENT) {
43+
return 0;
44+
}
45+
return scratch->max_size - objects * ALIGNMENT;
46+
}
47+
48+
static int secp256k1_scratch_resize(secp256k1_scratch* scratch, size_t n, size_t objects) {
49+
n += objects * ALIGNMENT;
50+
if (n > scratch->init_size && n <= scratch->max_size) {
51+
void *tmp = checked_realloc(scratch->error_callback, scratch->data, n);
52+
if (tmp == NULL) {
53+
return 0;
54+
}
55+
scratch->init_size = n;
56+
scratch->data = tmp;
57+
}
58+
return n <= scratch->max_size;
59+
}
60+
61+
static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
62+
void *ret;
63+
size = ((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
64+
if (size + scratch->offset > scratch->init_size) {
65+
return NULL;
66+
}
67+
ret = (void *) ((unsigned char *) scratch->data + scratch->offset);
68+
memset(ret, 0, size);
69+
scratch->offset += size;
70+
return ret;
71+
}
72+
73+
static void secp256k1_scratch_reset(secp256k1_scratch* scratch) {
74+
scratch->offset = 0;
75+
}
76+
77+
#endif

src/secp256k1.c

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ecdsa_impl.h"
1818
#include "eckey_impl.h"
1919
#include "hash_impl.h"
20+
#include "scratch_impl.h"
2021

2122
#define ARG_CHECK(cond) do { \
2223
if (EXPECT(!(cond), 0)) { \
@@ -114,6 +115,17 @@ void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(co
114115
ctx->error_callback.data = data;
115116
}
116117

118+
secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t init_size, size_t max_size) {
119+
VERIFY_CHECK(ctx != NULL);
120+
ARG_CHECK(max_size >= init_size);
121+
122+
return secp256k1_scratch_create(&ctx->error_callback, init_size, max_size);
123+
}
124+
125+
void secp256k1_scratch_space_destroy(secp256k1_scratch_space* scratch) {
126+
secp256k1_scratch_destroy(scratch);
127+
}
128+
117129
static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
118130
if (sizeof(secp256k1_ge_storage) == 64) {
119131
/* When the secp256k1_ge_storage type is exactly 64 byte, use its

src/tests.c

+36
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,41 @@ void run_context_tests(void) {
248248
secp256k1_context_destroy(NULL);
249249
}
250250

251+
void run_scratch_tests(void) {
252+
int32_t ecount = 0;
253+
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
254+
secp256k1_scratch_space *scratch;
255+
256+
/* Test public API */
257+
secp256k1_context_set_illegal_callback(none, counting_illegal_callback_fn, &ecount);
258+
scratch = secp256k1_scratch_space_create(none, 100, 10);
259+
CHECK(scratch == NULL);
260+
CHECK(ecount == 1);
261+
262+
scratch = secp256k1_scratch_space_create(none, 100, 100);
263+
CHECK(scratch != NULL);
264+
CHECK(ecount == 1);
265+
secp256k1_scratch_space_destroy(scratch);
266+
267+
scratch = secp256k1_scratch_space_create(none, 100, 1000);
268+
CHECK(scratch != NULL);
269+
CHECK(ecount == 1);
270+
271+
/* Test internal API */
272+
CHECK(secp256k1_scratch_max_allocation(scratch, 0) == 1000);
273+
CHECK(secp256k1_scratch_max_allocation(scratch, 1) < 1000);
274+
CHECK(secp256k1_scratch_resize(scratch, 50, 1) == 1); /* no-op */
275+
CHECK(secp256k1_scratch_resize(scratch, 200, 1) == 1);
276+
CHECK(secp256k1_scratch_resize(scratch, 950, 1) == 1);
277+
CHECK(secp256k1_scratch_resize(scratch, 1000, 1) == 0);
278+
CHECK(secp256k1_scratch_resize(scratch, 2000, 1) == 0);
279+
CHECK(secp256k1_scratch_max_allocation(scratch, 0) == 1000);
280+
281+
/* cleanup */
282+
secp256k1_scratch_space_destroy(scratch);
283+
secp256k1_context_destroy(none);
284+
}
285+
251286
/***** HASH TESTS *****/
252287

253288
void run_sha256_tests(void) {
@@ -4451,6 +4486,7 @@ int main(int argc, char **argv) {
44514486

44524487
/* initialize */
44534488
run_context_tests();
4489+
run_scratch_tests();
44544490
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
44554491
if (secp256k1_rand_bits(1)) {
44564492
secp256k1_rand256(run32);

src/util.h

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_
7676
return ret;
7777
}
7878

79+
static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
80+
void *ret = realloc(ptr, size);
81+
if (ret == NULL) {
82+
secp256k1_callback_call(cb, "Out of memory");
83+
}
84+
return ret;
85+
}
86+
7987
/* Macro for restrict, when available and not in a VERIFY build. */
8088
#if defined(SECP256K1_BUILD) && defined(VERIFY)
8189
# define SECP256K1_RESTRICT

0 commit comments

Comments
 (0)