예제 #1
0
        private static bool Secp256K1EcdsaSignRecoverable(Context ctx, EcdsaRecoverableSignature signature, byte[] msg32, byte[] seckey, NonceFunction noncefp, byte[] noncedata)
        {
            if (ctx == null || msg32 == null || signature == null || seckey == null)
            {
                throw new NullReferenceException();
            }

            if (!EcMultGen.ContextIsBuilt(ctx.EcMultGenCtx))
            {
                throw new ArithmeticException();
            }

            if (noncefp == null)
            {
                noncefp = Secp256K1T.NonceFunctionDefault;
            }

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

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

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

                    Scalar.SetB32(non, nonce32, ref overflow);
                    if (!Scalar.IsZero(non) && !overflow)
                    {
                        if (Secp256K1EcdsaSigSign(ctx.EcMultGenCtx, r, s, sec, msg, non, out recid))
                        {
                            break;
                        }
                    }
                    count++;
                }
                Util.MemSet(nonce32, 0, 32); //memset(nonce32, 0, 32);
                Scalar.Clear(msg);
                Scalar.Clear(non);
                Scalar.Clear(sec);
            }
            if (ret)
            {
                Secp256K1EcdsaRecoverableSignatureSave(signature, r, s, recid);
            }
            else
            {
                Util.MemSet(signature.Data, 0, EcdsaRecoverableSignature.Size); //memset(signature, 0, sizeof(* signature));
            }
            return(ret);
        }
        private static bool Secp256K1EcdsaSignRecoverable(Context ctx, EcdsaRecoverableSignature signature, byte[] msg32, byte[] seckey, NonceFunction noncefp, byte[] noncedata)
        {
            if (ctx == null || msg32 == null || (signature == null || seckey == null))
            {
                throw new NullReferenceException();
            }
            if (!EcMultGen.ContextIsBuilt(ctx.EcMultGenCtx))
            {
                throw new ArithmeticException();
            }
            if (noncefp == null)
            {
                noncefp = Secp256K1T.NonceFunctionDefault;
            }
            byte   recid    = 1;
            bool   flag     = false;
            bool   overflow = false;
            Scalar scalar1  = new Scalar();

            Scalar.SetB32(scalar1, seckey, ref overflow);
            Scalar scalar2 = new Scalar();
            Scalar scalar3 = new Scalar();

            if (!overflow && !Scalar.IsZero(scalar1))
            {
                byte[] numArray = new byte[32];
                uint   attempt  = 0;
                Scalar scalar4  = new Scalar();
                Scalar.SetB32(scalar4, msg32);
                Scalar scalar5 = new Scalar();
                while (true)
                {
                    flag = noncefp(numArray, msg32, seckey, (byte[])null, noncedata, attempt);
                    if (flag)
                    {
                        Scalar.SetB32(scalar5, numArray, ref overflow);
                        if (Scalar.IsZero(scalar5) || overflow || !Secp256K1Manager.Secp256K1EcdsaSigSign(ctx.EcMultGenCtx, scalar2, scalar3, scalar1, scalar4, scalar5, out recid))
                        {
                            ++attempt;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                Util.MemSet(numArray, (byte)0, 32);
                Scalar.Clear(scalar4);
                Scalar.Clear(scalar5);
                Scalar.Clear(scalar1);
            }
            if (flag)
            {
                Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSave(signature, scalar2, scalar3, recid);
            }
            else
            {
                Util.MemSet(signature.Data, (byte)0, 65);
            }
            return(flag);
        }