Exemplo n.º 1
0
        private bool EccRecover(ECCurve curve, ExecutionEngine engine)
        {
            var  r = new System.Numerics.BigInteger(engine.CurrentContext.EvaluationStack.Pop().GetByteArray().Reverse().Concat(new byte[1]).ToArray());
            var  s = new System.Numerics.BigInteger(engine.CurrentContext.EvaluationStack.Pop().GetByteArray().Reverse().Concat(new byte[1]).ToArray());
            bool v = engine.CurrentContext.EvaluationStack.Pop().GetBoolean();

            byte[] messageHash = engine.CurrentContext.EvaluationStack.Pop().GetByteArray();
            try
            {
                ECPoint point = ECDsa.KeyRecover(curve, r, s, messageHash, v);
                engine.CurrentContext.EvaluationStack.Push(point.EncodePoint(false).Skip(1).ToArray());
            }
            catch
            {
                engine.CurrentContext.EvaluationStack.Push(new byte[0]);
            }
            return(true);
        }
Exemplo n.º 2
0
        public static void KeyRecover(ECCurve Curve)
        {
            byte[]  privateKey = generatekey(32);
            ECPoint publicKey  = Curve.G * privateKey;
            ECDsa   ecdsa      = new ECDsa(privateKey, Curve);

            byte[]       message    = System.Text.Encoding.Default.GetBytes("HelloWorld");
            BigInteger[] signatures = ecdsa.GenerateSignature(message);
            BigInteger   r          = signatures[0];
            BigInteger   s          = signatures[1];
            bool         v;

            if (signatures[2] == 0)
            {
                v = true;
            }
            else
            {
                v = false;
            }
            ECPoint recoverKey = ECDsa.KeyRecover(Curve, r, s, message, v);

            Assert.IsTrue(recoverKey.Equals(publicKey));
            //wrong r part
            r = new BigInteger(generatekey(32));
            s = new BigInteger(generatekey(32));
            try
            {
                recoverKey = ECDsa.KeyRecover(Curve, r, s, message, v);
                Assert.IsFalse(recoverKey.Equals(publicKey));
            }
            catch (Exception e)
            {
                Assert.IsTrue(e.GetType() == typeof(ArithmeticException));
            }
        }