@@ -82,6 +82,17 @@ size_t secp256k1_context_preallocated_size(unsigned int flags) {
82
82
return ret ;
83
83
}
84
84
85
+ size_t secp256k1_context_preallocated_clone_size (const secp256k1_context * ctx ) {
86
+ size_t ret = ROUND_TO_ALIGN (sizeof (secp256k1_context ));
87
+ if (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx )) {
88
+ ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE ;
89
+ }
90
+ if (secp256k1_ecmult_context_is_built (& ctx -> ecmult_ctx )) {
91
+ ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE ;
92
+ }
93
+ return ret ;
94
+ }
95
+
85
96
secp256k1_context * secp256k1_context_preallocated_create (void * prealloc , unsigned int flags ) {
86
97
void * const base = prealloc ;
87
98
size_t prealloc_size = secp256k1_context_preallocated_size (flags );
@@ -120,22 +131,22 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) {
120
131
return ctx ;
121
132
}
122
133
123
- secp256k1_context * secp256k1_context_clone (const secp256k1_context * ctx ) {
124
- secp256k1_context * ret ;
125
- size_t prealloc_size = ROUND_TO_ALIGN (sizeof (secp256k1_context ));
126
- if (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx )) {
127
- prealloc_size += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE ;
128
- }
129
- if (secp256k1_ecmult_context_is_built (& ctx -> ecmult_ctx )) {
130
- prealloc_size += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE ;
131
- }
132
- ret = checked_malloc (& ctx -> error_callback , prealloc_size );
134
+ secp256k1_context * secp256k1_context_preallocated_clone (const secp256k1_context * ctx , void * prealloc ) {
135
+ size_t prealloc_size = secp256k1_context_preallocated_clone_size (ctx );
136
+ secp256k1_context * ret = (secp256k1_context * )prealloc ;
133
137
memcpy (ret , ctx , prealloc_size );
134
138
secp256k1_ecmult_gen_context_finalize_memcpy (& ret -> ecmult_gen_ctx , & ctx -> ecmult_gen_ctx );
135
139
secp256k1_ecmult_context_finalize_memcpy (& ret -> ecmult_ctx , & ctx -> ecmult_ctx );
136
140
return ret ;
137
141
}
138
142
143
+ secp256k1_context * secp256k1_context_clone (const secp256k1_context * ctx ) {
144
+ size_t prealloc_size = secp256k1_context_preallocated_clone_size (ctx );
145
+ secp256k1_context * ret = (secp256k1_context * )checked_malloc (& ctx -> error_callback , prealloc_size );
146
+ ret = secp256k1_context_preallocated_clone (ctx , ret );
147
+ return ret ;
148
+ }
149
+
139
150
void secp256k1_context_preallocated_destroy (secp256k1_context * ctx ) {
140
151
CHECK (ctx != secp256k1_context_no_precomp );
141
152
if (ctx != NULL ) {
0 commit comments