Example #1
0
        protected virtual Curve25519Point TwiceJacobianModified(bool calculateW)
        {
            Curve25519FieldElement X1 = (Curve25519FieldElement)this.RawXCoord, Y1 = (Curve25519FieldElement)this.RawYCoord,
                                   Z1 = (Curve25519FieldElement)this.RawZCoords[0], W1 = GetJacobianModifiedW();

            uint c;

            uint[] M    = Nat256.Create();
            Curve25519Field.Square(X1.x, M);
            c  = Nat256.AddBothTo(M, M, M);
            c += Nat256.AddTo(W1.x, M);
            Curve25519Field.Reduce27(c, M);

            uint[] _2Y1 = Nat256.Create();
            Curve25519Field.Twice(Y1.x, _2Y1);

            uint[] _2Y1Squared = Nat256.Create();
            Curve25519Field.Multiply(_2Y1, Y1.x, _2Y1Squared);

            uint[] S    = Nat256.Create();
            Curve25519Field.Multiply(_2Y1Squared, X1.x, S);
            Curve25519Field.Twice(S, S);

            uint[] _8T = Nat256.Create();
            Curve25519Field.Square(_2Y1Squared, _8T);
            Curve25519Field.Twice(_8T, _8T);

            Curve25519FieldElement X3 = new Curve25519FieldElement(_2Y1Squared);

            Curve25519Field.Square(M, X3.x);
            Curve25519Field.Subtract(X3.x, S, X3.x);
            Curve25519Field.Subtract(X3.x, S, X3.x);

            Curve25519FieldElement Y3 = new Curve25519FieldElement(S);

            Curve25519Field.Subtract(S, X3.x, Y3.x);
            Curve25519Field.Multiply(Y3.x, M, Y3.x);
            Curve25519Field.Subtract(Y3.x, _8T, Y3.x);

            Curve25519FieldElement Z3 = new Curve25519FieldElement(_2Y1);

            if (!Nat256.IsOne(Z1.x))
            {
                Curve25519Field.Multiply(Z3.x, Z1.x, Z3.x);
            }

            Curve25519FieldElement W3 = null;

            if (calculateW)
            {
                W3 = new Curve25519FieldElement(_8T);
                Curve25519Field.Multiply(W3.x, W1.x, W3.x);
                Curve25519Field.Twice(W3.x, W3.x);
            }

            return(new Curve25519Point(this.Curve, X3, Y3, new ECFieldElement[] { Z3, W3 }, IsCompressed));
        }
Example #2
0
        protected virtual Curve25519Point TwiceJacobianModified(bool calculateW)
        {
            Curve25519FieldElement curve25519FieldElement  = (Curve25519FieldElement)base.RawXCoord;
            Curve25519FieldElement curve25519FieldElement2 = (Curve25519FieldElement)base.RawYCoord;
            Curve25519FieldElement curve25519FieldElement3 = (Curve25519FieldElement)base.RawZCoords[0];
            Curve25519FieldElement jacobianModifiedW       = GetJacobianModifiedW();

            uint[] array = Nat256.Create();
            Curve25519Field.Square(curve25519FieldElement.x, array);
            uint num = Nat256.AddBothTo(array, array, array);

            num += Nat256.AddTo(jacobianModifiedW.x, array);
            Curve25519Field.Reduce27(num, array);
            uint[] array2 = Nat256.Create();
            Curve25519Field.Twice(curve25519FieldElement2.x, array2);
            uint[] array3 = Nat256.Create();
            Curve25519Field.Multiply(array2, curve25519FieldElement2.x, array3);
            uint[] array4 = Nat256.Create();
            Curve25519Field.Multiply(array3, curve25519FieldElement.x, array4);
            Curve25519Field.Twice(array4, array4);
            uint[] array5 = Nat256.Create();
            Curve25519Field.Square(array3, array5);
            Curve25519Field.Twice(array5, array5);
            Curve25519FieldElement curve25519FieldElement4 = new Curve25519FieldElement(array3);

            Curve25519Field.Square(array, curve25519FieldElement4.x);
            Curve25519Field.Subtract(curve25519FieldElement4.x, array4, curve25519FieldElement4.x);
            Curve25519Field.Subtract(curve25519FieldElement4.x, array4, curve25519FieldElement4.x);
            Curve25519FieldElement curve25519FieldElement5 = new Curve25519FieldElement(array4);

            Curve25519Field.Subtract(array4, curve25519FieldElement4.x, curve25519FieldElement5.x);
            Curve25519Field.Multiply(curve25519FieldElement5.x, array, curve25519FieldElement5.x);
            Curve25519Field.Subtract(curve25519FieldElement5.x, array5, curve25519FieldElement5.x);
            Curve25519FieldElement curve25519FieldElement6 = new Curve25519FieldElement(array2);

            if (!Nat256.IsOne(curve25519FieldElement3.x))
            {
                Curve25519Field.Multiply(curve25519FieldElement6.x, curve25519FieldElement3.x, curve25519FieldElement6.x);
            }
            Curve25519FieldElement curve25519FieldElement7 = null;

            if (calculateW)
            {
                curve25519FieldElement7 = new Curve25519FieldElement(array5);
                Curve25519Field.Multiply(curve25519FieldElement7.x, jacobianModifiedW.x, curve25519FieldElement7.x);
                Curve25519Field.Twice(curve25519FieldElement7.x, curve25519FieldElement7.x);
            }
            return(new Curve25519Point(Curve, curve25519FieldElement4, curve25519FieldElement5, new ECFieldElement[2]
            {
                curve25519FieldElement6,
                curve25519FieldElement7
            }, base.IsCompressed));
        }
Example #3
0
        public static void Square(uint[] x, uint[] zz)
        {
            Nat256.Square(x, zz);
            Nat256.Square(x, 8, zz, 16);
            uint num = Nat256.AddToEachOther(zz, 8, zz, 16);
            uint cIn = num + Nat256.AddTo(zz, 0, zz, 8, 0u);

            num += Nat256.AddTo(zz, 24, zz, 16, cIn);
            uint[] array = Nat256.Create();
            Nat256.Diff(x, 8, x, 0, array, 0);
            uint[] array2 = Nat256.CreateExt();
            Nat256.Square(array, array2);
            num = (uint)((int)num + Nat.SubFrom(16, array2, 0, zz, 8));
            Nat.AddWordAt(32, num, zz, 24);
        }
        protected virtual Curve25519Point TwiceJacobianModified(bool calculateW)
        {
            Curve25519FieldElement rawXCoord         = (Curve25519FieldElement)base.RawXCoord;
            Curve25519FieldElement rawYCoord         = (Curve25519FieldElement)base.RawYCoord;
            Curve25519FieldElement element3          = (Curve25519FieldElement)base.RawZCoords[0];
            Curve25519FieldElement jacobianModifiedW = this.GetJacobianModifiedW();

            uint[] z = Nat256.Create();
            Curve25519Field.Square(rawXCoord.x, z);
            uint x = Nat256.AddBothTo(z, z, z) + Nat256.AddTo(jacobianModifiedW.x, z);

            Curve25519Field.Reduce27(x, z);
            uint[] numArray2 = Nat256.Create();
            Curve25519Field.Twice(rawYCoord.x, numArray2);
            uint[] numArray3 = Nat256.Create();
            Curve25519Field.Multiply(numArray2, rawYCoord.x, numArray3);
            uint[] numArray4 = Nat256.Create();
            Curve25519Field.Multiply(numArray3, rawXCoord.x, numArray4);
            Curve25519Field.Twice(numArray4, numArray4);
            uint[] numArray5 = Nat256.Create();
            Curve25519Field.Square(numArray3, numArray5);
            Curve25519Field.Twice(numArray5, numArray5);
            Curve25519FieldElement element5 = new Curve25519FieldElement(numArray3);

            Curve25519Field.Square(z, element5.x);
            Curve25519Field.Subtract(element5.x, numArray4, element5.x);
            Curve25519Field.Subtract(element5.x, numArray4, element5.x);
            Curve25519FieldElement y = new Curve25519FieldElement(numArray4);

            Curve25519Field.Subtract(numArray4, element5.x, y.x);
            Curve25519Field.Multiply(y.x, z, y.x);
            Curve25519Field.Subtract(y.x, numArray5, y.x);
            Curve25519FieldElement element7 = new Curve25519FieldElement(numArray2);

            if (!Nat256.IsOne(element3.x))
            {
                Curve25519Field.Multiply(element7.x, element3.x, element7.x);
            }
            Curve25519FieldElement element8 = null;

            if (calculateW)
            {
                element8 = new Curve25519FieldElement(numArray5);
                Curve25519Field.Multiply(element8.x, jacobianModifiedW.x, element8.x);
                Curve25519Field.Twice(element8.x, element8.x);
            }
            return(new Curve25519Point(this.Curve, element5, y, new ECFieldElement[] { element7, element8 }, base.IsCompressed));
        }
Example #5
0
        public static void Mul(uint[] x, uint[] y, uint[] zz)
        {
            Nat256.Mul(x, y, zz);
            Nat256.Mul(x, 8, y, 8, zz, 16);
            uint num = Nat256.AddToEachOther(zz, 8, zz, 16);
            uint cIn = num + Nat256.AddTo(zz, 0, zz, 8, 0u);

            num += Nat256.AddTo(zz, 24, zz, 16, cIn);
            uint[] array  = Nat256.Create();
            uint[] array2 = Nat256.Create();
            bool   flag   = Nat256.Diff(x, 8, x, 0, array, 0) != Nat256.Diff(y, 8, y, 0, array2, 0);

            uint[] array3 = Nat256.CreateExt();
            Nat256.Mul(array, array2, array3);
            num = (uint)((int)num + ((!flag) ? Nat.SubFrom(16, array3, 0, zz, 8) : ((int)Nat.AddTo(16, array3, 0, zz, 8))));
            Nat.AddWordAt(32, num, zz, 24);
        }
Example #6
0
        public static void Mul(uint[] x, uint[] y, uint[] zz)
        {
            Nat256.Mul(x, y, zz);
            Nat256.Mul(x, 8, y, 8, zz, 16);

            uint c24 = Nat256.AddToEachOther(zz, 8, zz, 16);
            uint c16 = c24 + Nat256.AddTo(zz, 0, zz, 8, 0);

            c24 += Nat256.AddTo(zz, 24, zz, 16, c16);

            uint[] dx  = Nat256.Create(), dy = Nat256.Create();
            bool   neg = Nat256.Diff(x, 8, x, 0, dx, 0) != Nat256.Diff(y, 8, y, 0, dy, 0);

            uint[] tt = Nat256.CreateExt();
            Nat256.Mul(dx, dy, tt);

            c24 += neg ? Nat.AddTo(16, tt, 0, zz, 8) : (uint)Nat.SubFrom(16, tt, 0, zz, 8);
            Nat.AddWordAt(32, c24, zz, 24);
        }
Example #7
0
        public static void Square(uint[] x, uint[] zz)
        {
            Nat256.Square(x, zz);
            Nat256.Square(x, 8, zz, 16);

            uint c24 = Nat256.AddToEachOther(zz, 8, zz, 16);
            uint c16 = c24 + Nat256.AddTo(zz, 0, zz, 8, 0);

            c24 += Nat256.AddTo(zz, 24, zz, 16, c16);

            uint[] dx = Nat256.Create();
            Nat256.Diff(x, 8, x, 0, dx, 0);

            uint[] m = Nat256.CreateExt();
            Nat256.Square(dx, m);

            c24 += (uint)Nat.SubFrom(16, m, 0, zz, 8);
            Nat.AddWordAt(32, c24, zz, 24);
        }