Beispiel #1
0
        public static void ScalarMult(byte[] k, int kOff, byte[] u, int uOff, byte[] r, int rOff)
        {
            uint[] n = new uint[8];     DecodeScalar(k, kOff, n);

            int[] x1 = X25519Field.Create();        X25519Field.Decode(u, uOff, x1);
            int[] x2 = X25519Field.Create();        X25519Field.Copy(x1, 0, x2, 0);
            int[] z2 = X25519Field.Create();        z2[0] = 1;
            int[] x3 = X25519Field.Create();        x3[0] = 1;
            int[] z3 = X25519Field.Create();

            int[] t1 = X25519Field.Create();
            int[] t2 = X25519Field.Create();

            Debug.Assert(n[7] >> 30 == 1U);

            int bit = 254, swap = 1;

            do
            {
                X25519Field.Apm(x3, z3, t1, x3);
                X25519Field.Apm(x2, z2, z3, x2);
                X25519Field.Mul(t1, x2, t1);
                X25519Field.Mul(x3, z3, x3);
                X25519Field.Sqr(z3, z3);
                X25519Field.Sqr(x2, x2);

                X25519Field.Sub(z3, x2, t2);
                X25519Field.Mul(t2, C_A24, z2);
                X25519Field.Add(z2, x2, z2);
                X25519Field.Mul(z2, t2, z2);
                X25519Field.Mul(x2, z3, x2);

                X25519Field.Apm(t1, x3, x3, z3);
                X25519Field.Sqr(x3, x3);
                X25519Field.Sqr(z3, z3);
                X25519Field.Mul(z3, x1, z3);

                --bit;

                int word = bit >> 5, shift = bit & 0x1F;
                int kt = (int)(n[word] >> shift) & 1;
                swap ^= kt;
                X25519Field.CSwap(swap, x2, x3);
                X25519Field.CSwap(swap, z2, z3);
                swap = kt;
            }while (bit >= 3);

            Debug.Assert(swap == 0);

            for (int i = 0; i < 3; ++i)
            {
                PointDouble(x2, z2);
            }

            X25519Field.Inv(z2, z2);
            X25519Field.Mul(x2, z2, x2);

            X25519Field.Normalize(x2);
            X25519Field.Encode(x2, r, rOff);
        }
Beispiel #2
0
        private static void PointDouble(int[] x, int[] z)
        {
            int[] A = X25519Field.Create();
            int[] B = X25519Field.Create();

            X25519Field.Apm(x, z, A, B);
            X25519Field.Sqr(A, A);
            X25519Field.Sqr(B, B);
            X25519Field.Mul(A, B, x);
            X25519Field.Sub(A, B, A);
            X25519Field.Mul(A, C_A24, z);
            X25519Field.Add(z, B, z);
            X25519Field.Mul(z, A, z);
        }
Beispiel #3
0
        public static void ScalarMultBase(byte[] k, int kOff, byte[] r, int rOff)
        {
            int[] y = X25519Field.Create();
            int[] z = X25519Field.Create();

            Ed25519.ScalarMultBaseYZ(k, kOff, y, z);

            X25519Field.Apm(z, y, y, z);

            X25519Field.Inv(z, z);
            X25519Field.Mul(y, z, y);

            X25519Field.Normalize(y);
            X25519Field.Encode(y, r, rOff);
        }