@@ -82,6 +82,17 @@ size_t secp256k1_context_preallocated_size(unsigned int flags) {
8282 return ret ;
8383}
8484
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+
8596secp256k1_context * secp256k1_context_preallocated_create (void * prealloc , unsigned int flags ) {
8697 void * const base = prealloc ;
8798 size_t prealloc_size = secp256k1_context_preallocated_size (flags );
@@ -120,22 +131,22 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) {
120131 return ctx ;
121132}
122133
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 ;
133137 memcpy (ret , ctx , prealloc_size );
134138 secp256k1_ecmult_gen_context_finalize_memcpy (& ret -> ecmult_gen_ctx , & ctx -> ecmult_gen_ctx );
135139 secp256k1_ecmult_context_finalize_memcpy (& ret -> ecmult_ctx , & ctx -> ecmult_ctx );
136140 return ret ;
137141}
138142
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+
139150void secp256k1_context_preallocated_destroy (secp256k1_context * ctx ) {
140151 CHECK (ctx != secp256k1_context_no_precomp );
141152 if (ctx != NULL ) {
0 commit comments