コード例 #1
0
 static Secp256k1()
 {
     secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
     secp256k1_nonce_function_default = nonce_function_rfc6979;
 }
コード例 #2
0
        private static bool secp256k1_ecdsa_sign_recoverable(secp256k1_context ctx, secp256k1_ecdsa_recoverable_signature signature,
                                                             byte[] msg32, byte[] seckey, secp256k1_nonce_function noncefp, byte[] noncedata)
        {
            if (ctx == null || msg32 == null || signature == null || seckey == null)
            {
                throw new NullReferenceException();
            }

            if (!ECMultGen.secp256k1_ecmult_gen_context_is_built(ctx.ecmult_gen_ctx))
            {
                throw new ArithmeticException();
            }

            if (noncefp == null)
            {
                noncefp = Secp256k1.secp256k1_nonce_function_default;
            }

            secp256k1_scalar r, s;
            secp256k1_scalar sec, non, msg;
            byte             recid = 1;
            bool             ret   = false;
            var overflow           = false;

            sec = new secp256k1_scalar();
            Scalar.secp256k1_scalar_set_b32(sec, seckey, ref overflow);
            r = new secp256k1_scalar();
            s = new secp256k1_scalar();
            /* Fail if the secret key is invalid. */
            if (!overflow && !Scalar.secp256k1_scalar_is_zero(sec))
            {
                var  nonce32 = new byte[32];
                uint count   = 0;
                msg = new secp256k1_scalar();
                Scalar.secp256k1_scalar_set_b32(msg, msg32);
                non = new secp256k1_scalar();

                while (true)
                {
                    ret = noncefp(nonce32, msg32, seckey, null, noncedata, count);
                    if (!ret)
                    {
                        break;
                    }

                    Scalar.secp256k1_scalar_set_b32(non, nonce32, ref overflow);
                    if (!Scalar.secp256k1_scalar_is_zero(non) && !overflow)
                    {
                        if (secp256k1_ecdsa_sig_sign(ctx.ecmult_gen_ctx, r, s, sec, msg, non, out recid))
                        {
                            break;
                        }
                    }
                    count++;
                }
                Util.MemSet(nonce32, 0, 32); //memset(nonce32, 0, 32);
                Scalar.secp256k1_scalar_clear(msg);
                Scalar.secp256k1_scalar_clear(non);
                Scalar.secp256k1_scalar_clear(sec);
            }
            if (ret)
            {
                secp256k1_ecdsa_recoverable_signature_save(signature, r, s, recid);
            }
            else
            {
                Util.MemSet(signature.data, 0, secp256k1_ecdsa_recoverable_signature.Size); //memset(signature, 0, sizeof(* signature));
            }
            return(ret);
        }