Skip to content

Commit e0b45ac

Browse files
committed
secp256k1-zkp-sys: redo patch files for latest upstream
This should really be folded into the next commit, since these patches do not apply to the currently-vendored version of libsecp-zkp. But to make review easier I am doing them in a separate commit. This updates the patchfiles to delete the context and scratch space allocation/deallocation functions. Upstream has updated the comments and other minor stuff so the old patchfiles did not work. While I am at it, change the patchfiles from normal diffs to git-diffs, since we have git available and it's easier to use git since it can figure out paths correctly.
1 parent 8733740 commit e0b45ac

7 files changed

+250
-120
lines changed
+36-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
13,37d12
2-
< static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) {
3-
< const size_t base_alloc = ROUND_TO_ALIGN(sizeof(secp256k1_scratch));
4-
< void *alloc = checked_malloc(error_callback, base_alloc + size);
5-
< secp256k1_scratch* ret = (secp256k1_scratch *)alloc;
6-
< if (ret != NULL) {
7-
< memset(ret, 0, sizeof(*ret));
8-
< memcpy(ret->magic, "scratch", 8);
9-
< ret->data = (void *) ((char *) alloc + base_alloc);
10-
< ret->max_size = size;
11-
< }
12-
< return ret;
13-
< }
14-
<
15-
< static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
16-
< if (scratch != NULL) {
17-
< VERIFY_CHECK(scratch->alloc_size == 0); /* all checkpoints should be applied */
18-
< if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
19-
< secp256k1_callback_call(error_callback, "invalid scratch space");
20-
< return;
21-
< }
22-
< memset(scratch->magic, 0, sizeof(scratch->magic));
23-
< free(scratch);
24-
< }
25-
< }
26-
<
1+
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h b/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h
2+
index f71a20b..5389571 100644
3+
--- a/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h
4+
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h
5+
@@ -10,31 +10,6 @@
6+
#include "util.h"
7+
#include "scratch.h"
8+
9+
-static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) {
10+
- const size_t base_alloc = ROUND_TO_ALIGN(sizeof(secp256k1_scratch));
11+
- void *alloc = checked_malloc(error_callback, base_alloc + size);
12+
- secp256k1_scratch* ret = (secp256k1_scratch *)alloc;
13+
- if (ret != NULL) {
14+
- memset(ret, 0, sizeof(*ret));
15+
- memcpy(ret->magic, "scratch", 8);
16+
- ret->data = (void *) ((char *) alloc + base_alloc);
17+
- ret->max_size = size;
18+
- }
19+
- return ret;
20+
-}
21+
-
22+
-static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
23+
- if (scratch != NULL) {
24+
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
25+
- secp256k1_callback_call(error_callback, "invalid scratch space");
26+
- return;
27+
- }
28+
- VERIFY_CHECK(scratch->alloc_size == 0); /* all checkpoints should be applied */
29+
- memset(scratch->magic, 0, sizeof(scratch->magic));
30+
- free(scratch);
31+
- }
32+
-}
33+
-
34+
static size_t secp256k1_scratch_checkpoint(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch) {
35+
if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
36+
secp256k1_callback_call(error_callback, "invalid scratch space");
+78-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,78 @@
1-
139,149d138
2-
< secp256k1_context* secp256k1_context_create(unsigned int flags) {
3-
< size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
4-
< secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
5-
< if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
6-
< free(ctx);
7-
< return NULL;
8-
< }
9-
<
10-
< return ctx;
11-
< }
12-
<
13-
164,174d152
14-
< secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
15-
< secp256k1_context* ret;
16-
< size_t prealloc_size;
17-
<
18-
< VERIFY_CHECK(ctx != NULL);
19-
< prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
20-
< ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
21-
< ret = secp256k1_context_preallocated_clone(ctx, ret);
22-
< return ret;
23-
< }
24-
<
25-
183,189d160
26-
< void secp256k1_context_destroy(secp256k1_context* ctx) {
27-
< if (ctx != NULL) {
28-
< secp256k1_context_preallocated_destroy(ctx);
29-
< free(ctx);
30-
< }
31-
< }
32-
<
33-
206,215d176
34-
< }
35-
<
36-
< secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
37-
< VERIFY_CHECK(ctx != NULL);
38-
< return secp256k1_scratch_create(&ctx->error_callback, max_size);
39-
< }
40-
<
41-
< void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
42-
< VERIFY_CHECK(ctx != NULL);
43-
< secp256k1_scratch_destroy(&ctx->error_callback, scratch);
1+
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c b/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c
2+
index 4c57826..dacaed2 100644
3+
--- a/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c
4+
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c
5+
@@ -158,17 +158,6 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
6+
return ret;
7+
}
8+
9+
-secp256k1_context* secp256k1_context_create(unsigned int flags) {
10+
- size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
11+
- secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
12+
- if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
13+
- free(ctx);
14+
- return NULL;
15+
- }
16+
-
17+
- return ctx;
18+
-}
19+
-
20+
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
21+
secp256k1_context* ret;
22+
VERIFY_CHECK(ctx != NULL);
23+
@@ -180,19 +169,6 @@ secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context*
24+
return ret;
25+
}
26+
27+
-secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
28+
- secp256k1_context* ret;
29+
- size_t prealloc_size;
30+
-
31+
- VERIFY_CHECK(ctx != NULL);
32+
- ARG_CHECK(secp256k1_context_is_proper(ctx));
33+
-
34+
- prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
35+
- ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
36+
- ret = secp256k1_context_preallocated_clone(ctx, ret);
37+
- return ret;
38+
-}
39+
-
40+
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
41+
ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
42+
43+
@@ -204,18 +180,6 @@ void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
44+
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
45+
}
46+
47+
-void secp256k1_context_destroy(secp256k1_context* ctx) {
48+
- ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
49+
-
50+
- /* Defined as noop */
51+
- if (ctx == NULL) {
52+
- return;
53+
- }
54+
-
55+
- secp256k1_context_preallocated_destroy(ctx);
56+
- free(ctx);
57+
-}
58+
-
59+
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
60+
/* We compare pointers instead of checking secp256k1_context_is_proper() here
61+
because setting callbacks is allowed on *copies* of the static context:
62+
@@ -240,16 +204,6 @@ void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(co
63+
ctx->error_callback.data = data;
64+
}
65+
66+
-secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
67+
- VERIFY_CHECK(ctx != NULL);
68+
- return secp256k1_scratch_create(&ctx->error_callback, max_size);
69+
-}
70+
-
71+
-void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
72+
- VERIFY_CHECK(ctx != NULL);
73+
- secp256k1_scratch_destroy(&ctx->error_callback, scratch);
74+
-}
75+
-
76+
/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
77+
* of the software.
78+
*/
+105-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,105 @@
1-
226,228d225
2-
< SECP256K1_API secp256k1_context* secp256k1_context_create(
3-
< unsigned int flags
4-
< ) SECP256K1_WARN_UNUSED_RESULT;
5-
231,233d227
6-
< SECP256K1_API secp256k1_context* secp256k1_context_clone(
7-
< const secp256k1_context* ctx
8-
< ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
9-
248,250d241
10-
< SECP256K1_API void secp256k1_context_destroy(
11-
< secp256k1_context* ctx
12-
< ) SECP256K1_ARG_NONNULL(1);
13-
327,330d317
14-
< SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
15-
< const secp256k1_context* ctx,
16-
< size_t size
17-
< ) SECP256K1_ARG_NONNULL(1);
18-
338,341d324
19-
< SECP256K1_API void secp256k1_scratch_space_destroy(
20-
< const secp256k1_context* ctx,
21-
< secp256k1_scratch_space* scratch
22-
< ) SECP256K1_ARG_NONNULL(1);
1+
diff --git a/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h b/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h
2+
index f4053f2..aa2d18b 100644
3+
--- a/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h
4+
+++ b/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h
5+
@@ -257,70 +257,6 @@ SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
6+
*/
7+
SECP256K1_API void secp256k1_selftest(void);
8+
9+
-
10+
-/** Create a secp256k1 context object (in dynamically allocated memory).
11+
- *
12+
- * This function uses malloc to allocate memory. It is guaranteed that malloc is
13+
- * called at most once for every call of this function. If you need to avoid dynamic
14+
- * memory allocation entirely, see secp256k1_context_static and the functions in
15+
- * secp256k1_preallocated.h.
16+
- *
17+
- * Returns: pointer to a newly created context object.
18+
- * In: flags: Always set to SECP256K1_CONTEXT_NONE (see below).
19+
- *
20+
- * The only valid non-deprecated flag in recent library versions is
21+
- * SECP256K1_CONTEXT_NONE, which will create a context sufficient for all functionality
22+
- * offered by the library. All other (deprecated) flags will be treated as equivalent
23+
- * to the SECP256K1_CONTEXT_NONE flag. Though the flags parameter primarily exists for
24+
- * historical reasons, future versions of the library may introduce new flags.
25+
- *
26+
- * If the context is intended to be used for API functions that perform computations
27+
- * involving secret keys, e.g., signing and public key generation, then it is highly
28+
- * recommended to call secp256k1_context_randomize on the context before calling
29+
- * those API functions. This will provide enhanced protection against side-channel
30+
- * leakage, see secp256k1_context_randomize for details.
31+
- *
32+
- * Do not create a new context object for each operation, as construction and
33+
- * randomization can take non-negligible time.
34+
- */
35+
-SECP256K1_API secp256k1_context *secp256k1_context_create(
36+
- unsigned int flags
37+
-) SECP256K1_WARN_UNUSED_RESULT;
38+
-
39+
-/** Copy a secp256k1 context object (into dynamically allocated memory).
40+
- *
41+
- * This function uses malloc to allocate memory. It is guaranteed that malloc is
42+
- * called at most once for every call of this function. If you need to avoid dynamic
43+
- * memory allocation entirely, see the functions in secp256k1_preallocated.h.
44+
- *
45+
- * Cloning secp256k1_context_static is not possible, and should not be emulated by
46+
- * the caller (e.g., using memcpy). Create a new context instead.
47+
- *
48+
- * Returns: pointer to a newly created context object.
49+
- * Args: ctx: pointer to a context to copy (not secp256k1_context_static).
50+
- */
51+
-SECP256K1_API secp256k1_context *secp256k1_context_clone(
52+
- const secp256k1_context *ctx
53+
-) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
54+
-
55+
-/** Destroy a secp256k1 context object (created in dynamically allocated memory).
56+
- *
57+
- * The context pointer may not be used afterwards.
58+
- *
59+
- * The context to destroy must have been created using secp256k1_context_create
60+
- * or secp256k1_context_clone. If the context has instead been created using
61+
- * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
62+
- * behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
63+
- * be used instead.
64+
- *
65+
- * Args: ctx: pointer to a context to destroy, constructed using
66+
- * secp256k1_context_create or secp256k1_context_clone
67+
- * (i.e., not secp256k1_context_static).
68+
- */
69+
-SECP256K1_API void secp256k1_context_destroy(
70+
- secp256k1_context *ctx
71+
-) SECP256K1_ARG_NONNULL(1);
72+
-
73+
/** Set a callback function to be called when an illegal argument is passed to
74+
* an API call. It will only trigger for violations that are mentioned
75+
* explicitly in the header.
76+
@@ -392,29 +328,6 @@ SECP256K1_API void secp256k1_context_set_error_callback(
77+
const void *data
78+
) SECP256K1_ARG_NONNULL(1);
79+
80+
-/** Create a secp256k1 scratch space object.
81+
- *
82+
- * Returns: a newly created scratch space.
83+
- * Args: ctx: pointer to a context object.
84+
- * In: size: amount of memory to be available as scratch space. Some extra
85+
- * (<100 bytes) will be allocated for extra accounting.
86+
- */
87+
-SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space *secp256k1_scratch_space_create(
88+
- const secp256k1_context *ctx,
89+
- size_t size
90+
-) SECP256K1_ARG_NONNULL(1);
91+
-
92+
-/** Destroy a secp256k1 scratch space.
93+
- *
94+
- * The pointer may not be used afterwards.
95+
- * Args: ctx: pointer to a context object.
96+
- * scratch: space to destroy
97+
- */
98+
-SECP256K1_API void secp256k1_scratch_space_destroy(
99+
- const secp256k1_context *ctx,
100+
- secp256k1_scratch_space *scratch
101+
-) SECP256K1_ARG_NONNULL(1);
102+
-
103+
/** Parse a variable-length public key into the pubkey object.
104+
*
105+
* Returns: 1 if the public key was fully valid.

secp256k1-zkp-sys/depend/surjection_impl.h.patch

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/surjection_impl.h b/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/surjection_impl.h
2-
index 8680d0b..0c82d19 100644
2+
index e125cbc..8ac4fbe 100644
33
--- a/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/surjection_impl.h
44
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/surjection_impl.h
55
@@ -7,7 +7,6 @@
6-
#ifndef _SECP256K1_SURJECTION_IMPL_H_
7-
#define _SECP256K1_SURJECTION_IMPL_H_
6+
#ifndef SECP256K1_SURJECTION_IMPL_H
7+
#define SECP256K1_SURJECTION_IMPL_H
88

99
-#include <assert.h>
1010
#include <string.h>

secp256k1-zkp-sys/depend/surjection_main_impl.h.patch

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/main_impl.h b/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/main_impl.h
2-
index 5367fe5..afa5103 100644
2+
index f1d7d42..3aadd75 100644
33
--- a/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/main_impl.h
44
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/main_impl.h
55
@@ -6,7 +6,6 @@
6-
#ifndef SECP256K1_MODULE_SURJECTION_MAIN
7-
#define SECP256K1_MODULE_SURJECTION_MAIN
6+
#ifndef SECP256K1_MODULE_SURJECTION_MAIN_H
7+
#define SECP256K1_MODULE_SURJECTION_MAIN_H
88

99
-#include <assert.h>
1010
#include <string.h>
1111

12-
#if defined HAVE_CONFIG_H
13-
@@ -172,48 +172,6 @@ static size_t secp256k1_surjectionproof_csprng_next(secp256k1_surjectionproof_cs
12+
#include "../../../include/secp256k1_rangeproof.h"
13+
@@ -168,48 +167,6 @@ static size_t secp256k1_surjectionproof_csprng_next(secp256k1_surjectionproof_cs
1414
}
1515
}
1616

secp256k1-zkp-sys/depend/util.h.patch

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
71,86d70
2-
< static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
3-
< void *ret = malloc(size);
4-
< if (ret == NULL) {
5-
< secp256k1_callback_call(cb, "Out of memory");
6-
< }
7-
< return ret;
8-
< }
9-
<
10-
< static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
11-
< void *ret = realloc(ptr, size);
12-
< if (ret == NULL) {
13-
< secp256k1_callback_call(cb, "Out of memory");
14-
< }
15-
< return ret;
16-
< }
17-
<
1+
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/util.h b/secp256k1-zkp-sys/depend/secp256k1/src/util.h
2+
index 10ea516..4066d2a 100644
3+
--- a/secp256k1-zkp-sys/depend/secp256k1/src/util.h
4+
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/util.h
5+
@@ -153,14 +153,6 @@ static const secp256k1_callback default_error_callback = {
6+
#define VERIFY_CHECK(cond)
7+
#endif
8+
9+
-static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
10+
- void *ret = malloc(size);
11+
- if (ret == NULL) {
12+
- secp256k1_callback_call(cb, "Out of memory");
13+
- }
14+
- return ret;
15+
-}
16+
-
17+
#if defined(__BIGGEST_ALIGNMENT__)
18+
#define ALIGNMENT __BIGGEST_ALIGNMENT__
19+
#else

0 commit comments

Comments
 (0)