Example #1
0
        public static bool verify(string message, Signature signature, PublicKey publicKey)
        {
            string     hashMessage   = sha256(message);
            BigInteger numberMessage = Utils.BinaryAscii.numberFromHex(hashMessage);
            CurveFp    curve         = publicKey.curve;
            BigInteger sigR          = signature.r;
            BigInteger sigS          = signature.s;
            BigInteger inv           = EcdsaMath.inv(sigS, curve.N);

            Point u1 = EcdsaMath.multiply(
                curve.G,
                Utils.Integer.modulo((numberMessage * inv), curve.N),
                curve.N,
                curve.A,
                curve.P
                );
            Point u2 = EcdsaMath.multiply(
                publicKey.point,
                Utils.Integer.modulo((sigR * inv), curve.N),
                curve.N,
                curve.A,
                curve.P
                );
            Point add = EcdsaMath.add(
                u1,
                u2,
                curve.A,
                curve.P
                );

            return(sigR == add.x);
        }
Example #2
0
        public static Signature sign(string message, PrivateKey privateKey)
        {
            string     hashMessage   = sha256(message);
            BigInteger numberMessage = Utils.BinaryAscii.numberFromHex(hashMessage);
            CurveFp    curve         = privateKey.curve;
            BigInteger randNum       = Utils.Integer.randomBetween(BigInteger.One, curve.N - 1);
            Point      randSignPoint = EcdsaMath.multiply(curve.G, randNum, curve.N, curve.A, curve.P);
            BigInteger r             = Utils.Integer.modulo(randSignPoint.x, curve.N);
            BigInteger s             = Utils.Integer.modulo((numberMessage + r * privateKey.secret) * (EcdsaMath.inv(randNum, curve.N)), curve.N);

            return(new Signature(r, s));
        }