Skip to content

Commit 811e8d2

Browse files
committed
Removed context_create/destroy/clone and scratch_create/destroy/clone functions.
1 parent 9186f02 commit 811e8d2

File tree

8 files changed

+6
-160
lines changed

8 files changed

+6
-160
lines changed

depend/secp256k1/include/secp256k1.h

-67
Original file line numberDiff line numberDiff line change
@@ -188,51 +188,6 @@ typedef int (*secp256k1_nonce_function)(
188188
*/
189189
SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
190190

191-
/** Create a secp256k1 context object (in dynamically allocated memory).
192-
*
193-
* This function uses malloc to allocate memory. It is guaranteed that malloc is
194-
* called at most once for every call of this function. If you need to avoid dynamic
195-
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
196-
*
197-
* Returns: a newly created context object.
198-
* In: flags: which parts of the context to initialize.
199-
*
200-
* See also secp256k1_context_randomize.
201-
*/
202-
SECP256K1_API secp256k1_context* secp256k1_context_create(
203-
unsigned int flags
204-
) SECP256K1_WARN_UNUSED_RESULT;
205-
206-
/** Copy a secp256k1 context object (into dynamically allocated memory).
207-
*
208-
* This function uses malloc to allocate memory. It is guaranteed that malloc is
209-
* called at most once for every call of this function. If you need to avoid dynamic
210-
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
211-
*
212-
* Returns: a newly created context object.
213-
* Args: ctx: an existing context to copy (cannot be NULL)
214-
*/
215-
SECP256K1_API secp256k1_context* secp256k1_context_clone(
216-
const secp256k1_context* ctx
217-
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
218-
219-
/** Destroy a secp256k1 context object (created in dynamically allocated memory).
220-
*
221-
* The context pointer may not be used afterwards.
222-
*
223-
* The context to destroy must have been created using secp256k1_context_create
224-
* or secp256k1_context_clone. If the context has instead been created using
225-
* secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
226-
* behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
227-
* be used instead.
228-
*
229-
* Args: ctx: an existing context to destroy, constructed using
230-
* secp256k1_context_create or secp256k1_context_clone
231-
*/
232-
SECP256K1_API void secp256k1_context_destroy(
233-
secp256k1_context* ctx
234-
);
235-
236191
/** Set a callback function to be called when an illegal argument is passed to
237192
* an API call. It will only trigger for violations that are mentioned
238193
* explicitly in the header.
@@ -301,28 +256,6 @@ SECP256K1_API void secp256k1_context_set_error_callback(
301256
const void* data
302257
) SECP256K1_ARG_NONNULL(1);
303258

304-
/** Create a secp256k1 scratch space object.
305-
*
306-
* Returns: a newly created scratch space.
307-
* Args: ctx: an existing context object (cannot be NULL)
308-
* In: size: amount of memory to be available as scratch space. Some extra
309-
* (<100 bytes) will be allocated for extra accounting.
310-
*/
311-
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
312-
const secp256k1_context* ctx,
313-
size_t size
314-
) SECP256K1_ARG_NONNULL(1);
315-
316-
/** Destroy a secp256k1 scratch space.
317-
*
318-
* The pointer may not be used afterwards.
319-
* Args: ctx: a secp256k1 context object.
320-
* scratch: space to destroy
321-
*/
322-
SECP256K1_API void secp256k1_scratch_space_destroy(
323-
const secp256k1_context* ctx,
324-
secp256k1_scratch_space* scratch
325-
) SECP256K1_ARG_NONNULL(1);
326259

327260
/** Parse a variable-length public key into the pubkey object.
328261
*

depend/secp256k1/src/scratch.h

-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ typedef struct secp256k1_scratch_space_struct {
2121
size_t max_size;
2222
} secp256k1_scratch;
2323

24-
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size);
2524

26-
static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch);
2725

2826
/** Returns an opaque object used to "checkpoint" a scratch space. Used
2927
* with `secp256k1_scratch_apply_checkpoint` to undo allocations. */

depend/secp256k1/src/scratch_impl.h

-25
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,6 @@
1010
#include "util.h"
1111
#include "scratch.h"
1212

13-
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) {
14-
const size_t base_alloc = ((sizeof(secp256k1_scratch) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
15-
void *alloc = checked_malloc(error_callback, base_alloc + size);
16-
secp256k1_scratch* ret = (secp256k1_scratch *)alloc;
17-
if (ret != NULL) {
18-
memset(ret, 0, sizeof(*ret));
19-
memcpy(ret->magic, "scratch", 8);
20-
ret->data = (void *) ((char *) alloc + base_alloc);
21-
ret->max_size = size;
22-
}
23-
return ret;
24-
}
25-
26-
static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
27-
if (scratch != NULL) {
28-
VERIFY_CHECK(scratch->alloc_size == 0); /* all checkpoints should be applied */
29-
if (memcmp(scratch->magic, "scratch", 8) != 0) {
30-
secp256k1_callback_call(error_callback, "invalid scratch space");
31-
return;
32-
}
33-
memset(scratch->magic, 0, sizeof(scratch->magic));
34-
free(scratch);
35-
}
36-
}
37-
3813
static size_t secp256k1_scratch_checkpoint(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch) {
3914
if (memcmp(scratch->magic, "scratch", 8) != 0) {
4015
secp256k1_callback_call(error_callback, "invalid scratch space");

depend/secp256k1/src/secp256k1.c

-39
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,6 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
136136
return (secp256k1_context*) ret;
137137
}
138138

139-
secp256k1_context* secp256k1_context_create(unsigned int flags) {
140-
size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
141-
secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
142-
if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
143-
free(ctx);
144-
return NULL;
145-
}
146-
147-
return ctx;
148-
}
149-
150139
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
151140
size_t prealloc_size;
152141
secp256k1_context* ret;
@@ -161,17 +150,6 @@ secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context*
161150
return ret;
162151
}
163152

164-
secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
165-
secp256k1_context* ret;
166-
size_t prealloc_size;
167-
168-
VERIFY_CHECK(ctx != NULL);
169-
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
170-
ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
171-
ret = secp256k1_context_preallocated_clone(ctx, ret);
172-
return ret;
173-
}
174-
175153
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
176154
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
177155
if (ctx != NULL) {
@@ -180,13 +158,6 @@ void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
180158
}
181159
}
182160

183-
void secp256k1_context_destroy(secp256k1_context* ctx) {
184-
if (ctx != NULL) {
185-
secp256k1_context_preallocated_destroy(ctx);
186-
free(ctx);
187-
}
188-
}
189-
190161
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
191162
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
192163
if (fun == NULL) {
@@ -205,16 +176,6 @@ void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(co
205176
ctx->error_callback.data = data;
206177
}
207178

208-
secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
209-
VERIFY_CHECK(ctx != NULL);
210-
return secp256k1_scratch_create(&ctx->error_callback, max_size);
211-
}
212-
213-
void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
214-
VERIFY_CHECK(ctx != NULL);
215-
secp256k1_scratch_destroy(&ctx->error_callback, scratch);
216-
}
217-
218179
static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
219180
if (sizeof(secp256k1_ge_storage) == 64) {
220181
/* When the secp256k1_ge_storage type is exactly 64 byte, use its

depend/secp256k1/src/util.h

-16
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,6 @@ static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *
6868
#define VERIFY_SETUP(stmt)
6969
#endif
7070

71-
static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
72-
void *ret = malloc(size);
73-
if (ret == NULL) {
74-
secp256k1_callback_call(cb, "Out of memory");
75-
}
76-
return ret;
77-
}
78-
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-
8771
#if defined(__BIGGEST_ALIGNMENT__)
8872
#define ALIGNMENT __BIGGEST_ALIGNMENT__
8973
#else

src/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ mod std_only {
123123

124124
impl<C: Context> Clone for Secp256k1<C> {
125125
fn clone(&self) -> Secp256k1<C> {
126-
let buf = vec![0u8; unsafe { (&*self.buf).len() }].into_boxed_slice();
127-
let ptr = Box::into_raw(buf);
126+
let clone_size = unsafe {ffi::secp256k1_context_preallocated_clone_size(self.ctx)};
127+
let ptr_buf = Box::into_raw(vec![0u8; clone_size].into_boxed_slice());
128128
Secp256k1 {
129-
ctx: unsafe { ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS) },
129+
ctx: unsafe { ffi::secp256k1_context_preallocated_clone(self.ctx, ptr_buf as *mut c_void) },
130130
phantom: PhantomData,
131-
buf: ptr,
131+
buf: ptr_buf,
132132
}
133133
}
134134
}

src/ffi.rs

-6
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ extern "C" {
142142
pub static secp256k1_context_no_precomp: *const Context;
143143

144144
// Contexts
145-
pub fn secp256k1_context_create(flags: c_uint) -> *mut Context;
146-
147145
pub fn secp256k1_context_preallocated_size(flags: c_uint) -> usize;
148146

149147
pub fn secp256k1_context_preallocated_create(prealloc: *mut c_void, flags: c_uint) -> *mut Context;
@@ -154,10 +152,6 @@ extern "C" {
154152

155153
pub fn secp256k1_context_preallocated_clone(cx: *const Context, prealloc: *mut c_void) -> *mut Context;
156154

157-
pub fn secp256k1_context_clone(cx: *mut Context) -> *mut Context;
158-
159-
pub fn secp256k1_context_destroy(cx: *mut Context);
160-
161155
pub fn secp256k1_context_randomize(cx: *mut Context,
162156
seed32: *const c_uchar)
163157
-> c_int;

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ impl<C: Context> Eq for Secp256k1<C> { }
549549

550550
impl<C: Context> Drop for Secp256k1<C> {
551551
fn drop(&mut self) {
552-
C::deallocate(self.buf)
552+
unsafe { ffi::secp256k1_context_preallocated_destroy(self.ctx) };
553+
C::deallocate(self.buf);
553554
}
554555
}
555556

0 commit comments

Comments
 (0)