예제 #1
0
파일: ECPoint.cs 프로젝트: yegorkr/ECC
        internal FPoint AddS(FPoint b)
        {
            if (this.IsInfinity)
            {
                return(b);
            }

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

            FieldElement x2 = (FieldElement)b.X;
            FieldElement y2 = (FieldElement)b.Y;

            if (this.x.Equals(x2))
            {
                if (this.y.Equals(y2))
                {
                    return((FPoint)this.Twice());
                }

                return((FPoint)this.curve.Infinity);
            }

            ECFieldElement xSum   = this.x.Add(x2);
            FieldElement   lambda = (FieldElement)(this.y.Add(y2)).Divide(xSum);
            FieldElement   x3     = (FieldElement)lambda.Square().Add(lambda).Add(xSum).Add(this.curve.A);
            FieldElement   y3     = (FieldElement)lambda.Multiply(this.x.Add(x3)).Add(x3).Add(this.y);

            return(new FPoint(curve, x3, y3));
        }
예제 #2
0
파일: Program.cs 프로젝트: yegorkr/ECC
        static void Main(string[] args)
        {
            int m  = 163;
            int k1 = 3;
            int k2 = 6;
            int k3 = 7;

            var a = new BigInteger("1", 16);
            var b = new BigInteger("5FF6108462A2DC8210AB403925E638A19C1455D21", 16);
            var n = new BigInteger("400000000000000000002BEC12BE2262D39BCF14D", 16);
            //var basePoint = computeBasePoint();
            var x = new BigInteger("72D867F93A93AC27DF9FF01AFFE74885C8C540420", 16);
            var y = new BigInteger("0224A9C3947852B97C5599D5F4AB81122ADC3FD9B", 16);
            //var x = basePoint.X.ToBigInteger();
            //var y = basePoint.Y.ToBigInteger();
            var curve     = new FCurve(m, k1, k2, k3, a, b);
            var x_element = new FieldElement(m, k1, k2, k3, x);
            var y_element = new FieldElement(m, k1, k2, k3, y);
            var P         = new FPoint(curve, x_element, y_element);

            var d = getRandom();
            var Q = (FPoint)P.Multiply(d).Negate();

            Console.WriteLine("Q (x, y) = ({0}, {1})", Q.X.ToBigInteger().ToString(16), Q.Y.ToBigInteger().ToString(16));

            var M = new BigInteger("1263612ABD726", 16);

            var e  = getRandom();
            var eP = P.Multiply(e);

            Console.WriteLine("eP (x, y) = ({0}, {1})", eP.X.ToBigInteger().ToString(16), eP.Y.ToBigInteger().ToString(16));
            var F_e = eP.X.ToBigInteger();

            Console.WriteLine("F_e = {0}", F_e.ToString(16));
            var            M_temp = new FieldElement(m, k1, k2, k3, M);
            var            F_temp = new FieldElement(m, k1, k2, k3, F_e);
            ECFieldElement y_temp = M_temp.Multiply(F_temp);
            var            r      = y_temp.ToBigInteger();

            Console.WriteLine("y = {0}", r.ToString(16));
            var s = (e.Add(d.Multiply(r))).Remainder(n);

            Console.WriteLine("s = {0}", s.ToString(16));

            // Validation

            var R = P.Multiply(s).Add(Q.Multiply(r));

            Console.WriteLine("R (x, y) = ({0}, {1})", R.X.ToBigInteger().ToString(16), R.Y.ToBigInteger().ToString(16));

            var M2  = new FieldElement(m, k1, k2, k3, M);
            var R_x = new FieldElement(m, k1, k2, k3, R.X.ToBigInteger());
            var y2  = M2.Multiply(R_x);

            Console.WriteLine("y2 = {0}", y_temp.ToBigInteger().ToString(16));
        }
예제 #3
0
파일: Program.cs 프로젝트: yegorkr/ECC
        static FPoint computeRandomPoint()
        {
            int m  = 163;
            int k1 = 3;
            int k2 = 6;
            int k3 = 7;

            var        a     = new BigInteger("1", 16);
            var        b     = new BigInteger("5FF6108462A2DC8210AB403925E638A19C1455D21", 16);
            var        curve = new FCurve(m, k1, k2, k3, a, b);
            BigInteger u     = getRandom();

            var u_element = new FieldElement(m, k1, k2, k3, u);
            var a_element = new FieldElement(m, k1, k2, k3, a);
            var b_element = new FieldElement(m, k1, k2, k3, b);

            var au    = u_element.Multiply(u_element).Multiply(a_element);
            var w     = u_element.Multiply(u_element).Multiply(u_element).Add(au).Add(b_element);
            var z     = quadraticEquation(u_element.ToBigInteger(), w.ToBigInteger());
            var point = new FPoint(curve, u_element, z);

            return(point);
        }
예제 #4
0
파일: Program.cs 프로젝트: yegorkr/ECC
        static FieldElement quadraticEquation(BigInteger u, BigInteger w)
        {
            int m  = 163;
            int k1 = 3;
            int k2 = 6;
            int k3 = 7;
            var w1 = new FieldElement(m, k1, k2, k3, w);
            var u1 = new FieldElement(m, k1, k2, k3, u);
            var u2 = u1.Invert().Square();
            var v  = (FieldElement)w1.Multiply(u2);
            var tr = trace(v);
            var t  = halfTrace(v);
            var z  = (FieldElement)t.Multiply(u1);

            return(z);
        }