Repository: bitcoin-core/secp256k1 (c)

Issue:
illegal-callback called with message that says "secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)"
trying to wrap secp256k1 in lisp and I am getting this message when trying to derive a public child key of a public parent key.
my code looks something like this(I have used cl-autowrap, have not given everthing nice names yet)

```
(with-context (ctx secp256k1.ffi:+secp256k1-context-sign+)
    (secp256k1.ffi:secp256k1-context-set-illegal-callback ctx
                                                          (autowrap:callback 'illegal-callback)
                                                          'nil)
    (autowrap:with-many-alloc ((pubkey 'secp256k1.ffi:secp256k1-pubkey)
                               (serialized ':unsigned-char 65)
                               (size-serialized ':unsigned-long 1))
      (unless (secp256k1.ffi:secp256k1-ec-pubkey-parse ctx pubkey key_ (length key))
        (error "when parsing pubkey"))
      (unless (secp256k1.ffi:secp256k1-ec-pubkey-tweak-add ctx pubkey hash_)
        (error "error when tweaking pubkey"))
      (setf (autowrap:c-aref size-serialized 0 :unsigned-long) 65)
      (unless (secp256k1.ffi:secp256k1-ec-pubkey-serialize ctx
                                                           serialized
                                                           size-serialized
                                                           pubkey
                                                           secp256k1.ffi:+secp256k1-ec-compressed+)
        (error "could not serialize new public key"))
      (values (cffi:foreign-array-to-lisp serialized `(:array :unsigned-char
                                                              ,(autowrap:c-aref size-serialized
                                                                                0
                                                                                :unsigned-long)))
              (subseq hash 32 64))))
```

i get the call to my illegal-callback after the call to tweak-add function and before the call to the ec-pubkey-serialize.

Questions:
what does this message mean? do i need to configure the context in some specific way? do I need to have a scratch-space set up? i have tried with one. 

I am running my lisp code in SBCL.

Pull request description:
Make randomization of a non-signing context a noop
Before this commit secp256k1_context_randomize called illegal_callback
when called on a context not initialized for signing. This is not
documented. Moreover, it is not desirable because non-signing contexts
may use randomization in the future.

This commit makes secp256k1_context_randomize a noop in this case. This
is safe because the context cannot be used for signing anyway.

This fixes #573 and it fixes rust-bitcoin/rust-secp256k1#82.

Patch:
diff --git a/include/secp256k1.h b/include/secp256k1.h
index f1f78ab..43af09c 100644
--- a/include/secp256k1.h
+++ b/include/secp256k1.h
@@ -615,7 +615,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
 
 /** Updates the context randomization to protect against side-channel leakage.
- *  Returns: 1: randomization successfully updated
+ *  Returns: 1: randomization successfully updated or nothing to randomize
  *           0: error
  *  Args:    ctx:       pointer to a context object (cannot be NULL)
  *  In:      seed32:    pointer to a 32-byte random seed (NULL resets to initial state)
@@ -630,6 +630,11 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
  * that it does not affect function results, but shields against attacks which
  * rely on any input-dependent behaviour.
  *
+ * This function has currently an effect only on contexts initialized for signing
+ * because randomization is currently used only for signing. However, this is not
+ * guaranteed and may change in the future. It is safe to call this function on
+ * contexts not initialized for signing; then it will have no effect and return 1.
+ *
  * You should call this after secp256k1_context_create or
  * secp256k1_context_clone, and may call this repeatedly afterwards.
  */
diff --git a/src/secp256k1.c b/src/secp256k1.c
index a1e3908..15981f4 100644
--- a/src/secp256k1.c
+++ b/src/secp256k1.c
@@ -570,9 +570,9 @@ int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey
 
 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
     VERIFY_CHECK(ctx != NULL);
-    CHECK(ctx != secp256k1_context_no_precomp);
-    ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
-    secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
+    if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
+        secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
+    }
     return 1;
 }
 


Test patch:
diff --git a/src/tests.c b/src/tests.c
index 61bb7fc..a7f841d 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -218,8 +218,12 @@ void run_context_tests(void) {
     CHECK(ecount == 3);
     CHECK(secp256k1_ec_pubkey_tweak_mul(vrfy, &pubkey, ctmp) == 1);
     CHECK(ecount == 3);
-    CHECK(secp256k1_context_randomize(vrfy, ctmp) == 0);
-    CHECK(ecount == 4);
+    CHECK(secp256k1_context_randomize(vrfy, ctmp) == 1);
+    CHECK(ecount == 3);
+    CHECK(secp256k1_context_randomize(vrfy, NULL) == 1);
+    CHECK(ecount == 3);
+    CHECK(secp256k1_context_randomize(sign, ctmp) == 1);
+    CHECK(ecount2 == 14);
     CHECK(secp256k1_context_randomize(sign, NULL) == 1);
     CHECK(ecount2 == 14);
     secp256k1_context_set_illegal_callback(vrfy, NULL, NULL);