コード例 #1
0
        public static void ScalarMult(byte[] k, int kOff, byte[] u, int uOff, byte[] r, int rOff)
        {
            uint[] n = new uint[14];    DecodeScalar(k, kOff, n);

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

            uint[] t1 = X448Field.Create();
            uint[] t2 = X448Field.Create();

            Debug.Assert(n[13] >> 31 == 1U);

            int bit = 447, swap = 1;

            do
            {
                //X448Field.Apm(x3, z3, t1, x3);
                X448Field.Add(x3, z3, t1);
                X448Field.Sub(x3, z3, x3);
                //X448Field.Apm(x2, z2, z3, x2);
                X448Field.Add(x2, z2, z3);
                X448Field.Sub(x2, z2, x2);

                X448Field.Mul(t1, x2, t1);
                X448Field.Mul(x3, z3, x3);
                X448Field.Sqr(z3, z3);
                X448Field.Sqr(x2, x2);

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

                //X448Field.Apm(t1, x3, x3, z3);
                X448Field.Sub(t1, x3, z3);
                X448Field.Add(t1, x3, x3);
                X448Field.Sqr(x3, x3);
                X448Field.Sqr(z3, z3);
                X448Field.Mul(z3, x1, z3);

                --bit;

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

            Debug.Assert(swap == 0);

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

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

            X448Field.Normalize(x2);
            X448Field.Encode(x2, r, rOff);
        }