Exemplo n.º 1
0
 public ECPoint Subtract(ECPoint b)
 {
     return Add(b.Negate());
 }
Exemplo n.º 2
0
        public ECPoint Add(ECPoint b)
        {
            BigInteger m;
            BigInteger r = 0;

            if (this.IsInfinity)
            {
                return b;
            }
            if (b.IsInfinity)
            {
                return this;
            }

            if (X - b.X == 0)
            {
                if (Y - b.Y == 0)
                {
                    m = 3 * X * X * (2 * Y).ModInverse(Secp256k1.P);
                }
                else
                {
                    return ECPoint.Infinity;
                }
            }
            else
            {
                var mx = (X - b.X);
                if (mx < 0)
                {
                    mx += Secp256k1.P;
                }
                m = (Y - b.Y) * mx.ModInverse(Secp256k1.P);
            }

            m = m % Secp256k1.P;

            var v = Y - m * X;
            var x3 = (m * m - X - b.X);
            x3 = x3 % Secp256k1.P;
            if (x3 < 0)
            {
                x3 += Secp256k1.P;
            }
            var y3 = -(m * x3 + v);
            y3 = y3 % Secp256k1.P;
            if (y3 < 0)
            {
                y3 += Secp256k1.P;
            }

            return new ECPoint(x3, y3);
        }