Exemple #1
0
        //      ec-function: Y^2 = X^3 + a*X + b 'modulo' P
        //P = {Xp, Yp}; Q = {Xq, Yq}
        //ADD:    S = (Yp - Yq) / (Xp - Xq)
        //        RESULT = {Xr = S * S - Xp - Xq; Yr = S(Xp - Xr) - Yp}
        //DUB:    S = (3 * Xp * Xp + a) / (2 * Yp)
        //        RESULT = {Xr = S * S - 2 * Xp ; Yr = S(Xp - Xr) - Yp}
        public void GetDouble(ref OwnECPoint p)
        {   //return: 2 * p
            if (p.IsInfinity)
            {
                return;
            }
            if (IsZero(p.Y))
            {
                p.SetInfinity();
                return;
            }

            Let(S1, p.X);
            SquareModP(S1); AddScaledModP(S1, S1, 2);
            AddModP(S1, A);
            Let(S2, p.Y);
            AddModP(S2, S2);

            this.RemodFriendlyPrime(S1);
            this.RemodFriendlyPrime(S2);
            AsmX64Operations.DivideModuloPrime(S1, S2, P, N);

            Let(R1, S1);
            SquareModP(S1);
            SubScaledModP(S1, p.X, 2); //S1 = Rx = S * S - 2 * Px

            SubtractModP(p.X, S1, S2); //S2 = Px - Rx
            MultiplyModP(S2, R1);
            SubModP(S2, p.Y);          //S2 = S(Px - Rx) - Py

            p.SetFrom(S1, S2, false);
        }
Exemple #2
0
        public void Add(ref OwnECPoint p, OwnECPoint q, bool subtract = false)
        {   //returns: P + Q
            if (p.IsInfinity || q.IsInfinity)
            {
                if (!q.IsInfinity)
                {
                    p.SetFrom(q.X, q.Y, q.IsInfinity);
                    if (subtract)
                    {
                        SubtractModP(P, p.Y, p.Y);
                    }
                }
                return;
            }

            SubtractModP(p.X, q.X, S2);     //s2 = Px - Qx
            if (subtract)
            {                               //s1 = Py - Qy; in case of subtract real Q.Y is -Q.Y so we add the value modulo P.
                AdditionModP(p.Y, q.Y, S1); //s1 = Py - Qy
            }
            else
            {
                SubtractModP(p.Y, q.Y, S1);
            }

            if (IsZero(S2))
            {     //Px == Qx
                if (IsZero(S1))
                { // P == Q
                    GetDouble(ref p);
                    return;
                }
                p.SetInfinity();
                return;
            }

            this.RemodFriendlyPrime(S1);
            this.RemodFriendlyPrime(S2);
            AsmX64Operations.DivideModuloPrime(S1, S2, P, N);        //S = s1 = s1 / s2 'modulo' P.

            Let(R1, S1);
            SquareModP(S1);
            SubModP(S1, p.X);
            SubModP(S1, q.X);          //s1 = Result.x = S * S - Px - Qx

            SubtractModP(p.X, S1, S2); //s2 = Px - Rx
            MultiplyModP(S2, R1);
            SubModP(S2, p.Y);          //s2 = S(Px - Rx) - Py

            p.SetFrom(S1, S2, false);
        }