Skip to content

Commit

Permalink
secp256k1-zkp-sys: redo patch files for latest upstream
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apoelstra committed Jul 9, 2024
1 parent 8733740 commit e0b45ac
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 120 deletions.
62 changes: 36 additions & 26 deletions secp256k1-zkp-sys/depend/scratch_impl.h.patch
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
13,37d12
< static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) {
< const size_t base_alloc = ROUND_TO_ALIGN(sizeof(secp256k1_scratch));
< void *alloc = checked_malloc(error_callback, base_alloc + size);
< secp256k1_scratch* ret = (secp256k1_scratch *)alloc;
< if (ret != NULL) {
< memset(ret, 0, sizeof(*ret));
< memcpy(ret->magic, "scratch", 8);
< ret->data = (void *) ((char *) alloc + base_alloc);
< ret->max_size = size;
< }
< return ret;
< }
<
< static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
< if (scratch != NULL) {
< VERIFY_CHECK(scratch->alloc_size == 0); /* all checkpoints should be applied */
< if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
< secp256k1_callback_call(error_callback, "invalid scratch space");
< return;
< }
< memset(scratch->magic, 0, sizeof(scratch->magic));
< free(scratch);
< }
< }
<
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h b/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h
index f71a20b..5389571 100644
--- a/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/scratch_impl.h
@@ -10,31 +10,6 @@
#include "util.h"
#include "scratch.h"

-static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) {
- const size_t base_alloc = ROUND_TO_ALIGN(sizeof(secp256k1_scratch));
- void *alloc = checked_malloc(error_callback, base_alloc + size);
- secp256k1_scratch* ret = (secp256k1_scratch *)alloc;
- if (ret != NULL) {
- memset(ret, 0, sizeof(*ret));
- memcpy(ret->magic, "scratch", 8);
- ret->data = (void *) ((char *) alloc + base_alloc);
- ret->max_size = size;
- }
- return ret;
-}
-
-static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
- if (scratch != NULL) {
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
- secp256k1_callback_call(error_callback, "invalid scratch space");
- return;
- }
- VERIFY_CHECK(scratch->alloc_size == 0); /* all checkpoints should be applied */
- memset(scratch->magic, 0, sizeof(scratch->magic));
- free(scratch);
- }
-}
-
static size_t secp256k1_scratch_checkpoint(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch) {
if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
121 changes: 78 additions & 43 deletions secp256k1-zkp-sys/depend/secp256k1.c.patch
Original file line number Diff line number Diff line change
@@ -1,43 +1,78 @@
139,149d138
< secp256k1_context* secp256k1_context_create(unsigned int flags) {
< size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
< secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
< if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
< free(ctx);
< return NULL;
< }
<
< return ctx;
< }
<
164,174d152
< secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
< secp256k1_context* ret;
< size_t prealloc_size;
<
< VERIFY_CHECK(ctx != NULL);
< prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
< ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
< ret = secp256k1_context_preallocated_clone(ctx, ret);
< return ret;
< }
<
183,189d160
< void secp256k1_context_destroy(secp256k1_context* ctx) {
< if (ctx != NULL) {
< secp256k1_context_preallocated_destroy(ctx);
< free(ctx);
< }
< }
<
206,215d176
< }
<
< secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
< VERIFY_CHECK(ctx != NULL);
< return secp256k1_scratch_create(&ctx->error_callback, max_size);
< }
<
< void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
< VERIFY_CHECK(ctx != NULL);
< secp256k1_scratch_destroy(&ctx->error_callback, scratch);
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c b/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c
index 4c57826..dacaed2 100644
--- a/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/secp256k1.c
@@ -158,17 +158,6 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
return ret;
}

-secp256k1_context* secp256k1_context_create(unsigned int flags) {
- size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
- secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
- if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
- free(ctx);
- return NULL;
- }
-
- return ctx;
-}
-
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
secp256k1_context* ret;
VERIFY_CHECK(ctx != NULL);
@@ -180,19 +169,6 @@ secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context*
return ret;
}

-secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
- secp256k1_context* ret;
- size_t prealloc_size;
-
- VERIFY_CHECK(ctx != NULL);
- ARG_CHECK(secp256k1_context_is_proper(ctx));
-
- prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
- ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
- ret = secp256k1_context_preallocated_clone(ctx, ret);
- return ret;
-}
-
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));

@@ -204,18 +180,6 @@ void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
}

-void secp256k1_context_destroy(secp256k1_context* ctx) {
- ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
-
- /* Defined as noop */
- if (ctx == NULL) {
- return;
- }
-
- secp256k1_context_preallocated_destroy(ctx);
- free(ctx);
-}
-
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
/* We compare pointers instead of checking secp256k1_context_is_proper() here
because setting callbacks is allowed on *copies* of the static context:
@@ -240,16 +204,6 @@ void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(co
ctx->error_callback.data = data;
}

-secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
- VERIFY_CHECK(ctx != NULL);
- return secp256k1_scratch_create(&ctx->error_callback, max_size);
-}
-
-void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
- VERIFY_CHECK(ctx != NULL);
- secp256k1_scratch_destroy(&ctx->error_callback, scratch);
-}
-
/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
* of the software.
*/
127 changes: 105 additions & 22 deletions secp256k1-zkp-sys/depend/secp256k1.h.patch
Original file line number Diff line number Diff line change
@@ -1,22 +1,105 @@
226,228d225
< SECP256K1_API secp256k1_context* secp256k1_context_create(
< unsigned int flags
< ) SECP256K1_WARN_UNUSED_RESULT;
231,233d227
< SECP256K1_API secp256k1_context* secp256k1_context_clone(
< const secp256k1_context* ctx
< ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
248,250d241
< SECP256K1_API void secp256k1_context_destroy(
< secp256k1_context* ctx
< ) SECP256K1_ARG_NONNULL(1);
327,330d317
< SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
< const secp256k1_context* ctx,
< size_t size
< ) SECP256K1_ARG_NONNULL(1);
338,341d324
< SECP256K1_API void secp256k1_scratch_space_destroy(
< const secp256k1_context* ctx,
< secp256k1_scratch_space* scratch
< ) SECP256K1_ARG_NONNULL(1);
diff --git a/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h b/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h
index f4053f2..aa2d18b 100644
--- a/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h
+++ b/secp256k1-zkp-sys/depend/secp256k1/include/secp256k1.h
@@ -257,70 +257,6 @@ SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
*/
SECP256K1_API void secp256k1_selftest(void);

-
-/** Create a secp256k1 context object (in dynamically allocated memory).
- *
- * This function uses malloc to allocate memory. It is guaranteed that malloc is
- * called at most once for every call of this function. If you need to avoid dynamic
- * memory allocation entirely, see secp256k1_context_static and the functions in
- * secp256k1_preallocated.h.
- *
- * Returns: pointer to a newly created context object.
- * In: flags: Always set to SECP256K1_CONTEXT_NONE (see below).
- *
- * The only valid non-deprecated flag in recent library versions is
- * SECP256K1_CONTEXT_NONE, which will create a context sufficient for all functionality
- * offered by the library. All other (deprecated) flags will be treated as equivalent
- * to the SECP256K1_CONTEXT_NONE flag. Though the flags parameter primarily exists for
- * historical reasons, future versions of the library may introduce new flags.
- *
- * If the context is intended to be used for API functions that perform computations
- * involving secret keys, e.g., signing and public key generation, then it is highly
- * recommended to call secp256k1_context_randomize on the context before calling
- * those API functions. This will provide enhanced protection against side-channel
- * leakage, see secp256k1_context_randomize for details.
- *
- * Do not create a new context object for each operation, as construction and
- * randomization can take non-negligible time.
- */
-SECP256K1_API secp256k1_context *secp256k1_context_create(
- unsigned int flags
-) SECP256K1_WARN_UNUSED_RESULT;
-
-/** Copy a secp256k1 context object (into dynamically allocated memory).
- *
- * This function uses malloc to allocate memory. It is guaranteed that malloc is
- * called at most once for every call of this function. If you need to avoid dynamic
- * memory allocation entirely, see the functions in secp256k1_preallocated.h.
- *
- * Cloning secp256k1_context_static is not possible, and should not be emulated by
- * the caller (e.g., using memcpy). Create a new context instead.
- *
- * Returns: pointer to a newly created context object.
- * Args: ctx: pointer to a context to copy (not secp256k1_context_static).
- */
-SECP256K1_API secp256k1_context *secp256k1_context_clone(
- const secp256k1_context *ctx
-) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
-
-/** Destroy a secp256k1 context object (created in dynamically allocated memory).
- *
- * The context pointer may not be used afterwards.
- *
- * The context to destroy must have been created using secp256k1_context_create
- * or secp256k1_context_clone. If the context has instead been created using
- * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
- * behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
- * be used instead.
- *
- * Args: ctx: pointer to a context to destroy, constructed using
- * secp256k1_context_create or secp256k1_context_clone
- * (i.e., not secp256k1_context_static).
- */
-SECP256K1_API void secp256k1_context_destroy(
- secp256k1_context *ctx
-) SECP256K1_ARG_NONNULL(1);
-
/** Set a callback function to be called when an illegal argument is passed to
* an API call. It will only trigger for violations that are mentioned
* explicitly in the header.
@@ -392,29 +328,6 @@ SECP256K1_API void secp256k1_context_set_error_callback(
const void *data
) SECP256K1_ARG_NONNULL(1);

-/** Create a secp256k1 scratch space object.
- *
- * Returns: a newly created scratch space.
- * Args: ctx: pointer to a context object.
- * In: size: amount of memory to be available as scratch space. Some extra
- * (<100 bytes) will be allocated for extra accounting.
- */
-SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space *secp256k1_scratch_space_create(
- const secp256k1_context *ctx,
- size_t size
-) SECP256K1_ARG_NONNULL(1);
-
-/** Destroy a secp256k1 scratch space.
- *
- * The pointer may not be used afterwards.
- * Args: ctx: pointer to a context object.
- * scratch: space to destroy
- */
-SECP256K1_API void secp256k1_scratch_space_destroy(
- const secp256k1_context *ctx,
- secp256k1_scratch_space *scratch
-) SECP256K1_ARG_NONNULL(1);
-
/** Parse a variable-length public key into the pubkey object.
*
* Returns: 1 if the public key was fully valid.
6 changes: 3 additions & 3 deletions secp256k1-zkp-sys/depend/surjection_impl.h.patch
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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
index 8680d0b..0c82d19 100644
index e125cbc..8ac4fbe 100644
--- a/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/surjection_impl.h
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/surjection_impl.h
@@ -7,7 +7,6 @@
#ifndef _SECP256K1_SURJECTION_IMPL_H_
#define _SECP256K1_SURJECTION_IMPL_H_
#ifndef SECP256K1_SURJECTION_IMPL_H
#define SECP256K1_SURJECTION_IMPL_H

-#include <assert.h>
#include <string.h>
Expand Down
10 changes: 5 additions & 5 deletions secp256k1-zkp-sys/depend/surjection_main_impl.h.patch
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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
index 5367fe5..afa5103 100644
index f1d7d42..3aadd75 100644
--- a/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/main_impl.h
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/modules/surjection/main_impl.h
@@ -6,7 +6,6 @@
#ifndef SECP256K1_MODULE_SURJECTION_MAIN
#define SECP256K1_MODULE_SURJECTION_MAIN
#ifndef SECP256K1_MODULE_SURJECTION_MAIN_H
#define SECP256K1_MODULE_SURJECTION_MAIN_H

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

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

Expand Down
36 changes: 19 additions & 17 deletions secp256k1-zkp-sys/depend/util.h.patch
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
71,86d70
< static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
< void *ret = malloc(size);
< if (ret == NULL) {
< secp256k1_callback_call(cb, "Out of memory");
< }
< return ret;
< }
<
< static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
< void *ret = realloc(ptr, size);
< if (ret == NULL) {
< secp256k1_callback_call(cb, "Out of memory");
< }
< return ret;
< }
<
diff --git a/secp256k1-zkp-sys/depend/secp256k1/src/util.h b/secp256k1-zkp-sys/depend/secp256k1/src/util.h
index 10ea516..4066d2a 100644
--- a/secp256k1-zkp-sys/depend/secp256k1/src/util.h
+++ b/secp256k1-zkp-sys/depend/secp256k1/src/util.h
@@ -153,14 +153,6 @@ static const secp256k1_callback default_error_callback = {
#define VERIFY_CHECK(cond)
#endif

-static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
- void *ret = malloc(size);
- if (ret == NULL) {
- secp256k1_callback_call(cb, "Out of memory");
- }
- return ret;
-}
-
#if defined(__BIGGEST_ALIGNMENT__)
#define ALIGNMENT __BIGGEST_ALIGNMENT__
#else
Loading

0 comments on commit e0b45ac

Please sign in to comment.