/**
         * Computes <code>z * a(z) mod f(z)</code>, where <code>f(z)</code> is
         * the reduction polynomial of <code>this</code>.
         * @param a The polynomial <code>a(z)</code> to be multiplied by
         * <code>z mod f(z)</code>.
         * @return <code>z * a(z) mod f(z)</code>
         */
        private BigInteger multZModF(
            BigInteger a)
        {
            // Left-shift of a(z)
            BigInteger az = a.ShiftLeft(1);

            if (az.TestBit(this.m))
            {
                // If the coefficient of z^m in a(z) Equals 1, reduction
                // modulo f(z) is performed: Add f(z) to to a(z):
                // Step 1: Unset mth coeffient of a(z)
                az = az.ClearBit(this.m);

                // Step 2: Add r(z) to a(z), where r(z) is defined as
                // f(z) = z^m + r(z), and k1, k2, k3 are the positions of
                // the non-zero coefficients in r(z)
                az = az.FlipBit(0);
                az = az.FlipBit(this.k1);
                if (this.representation == Ppb)
                {
                    az = az.FlipBit(this.k2);
                    az = az.FlipBit(this.k3);
                }
            }
            return(az);
        }
Пример #2
0
        public void TestClearBit()
        {
            Assert.AreEqual(Zero, Zero.ClearBit(0));
            Assert.AreEqual(Zero, One.ClearBit(0));
            Assert.AreEqual(Two, Two.ClearBit(0));

            Assert.AreEqual(Zero, Zero.ClearBit(1));
            Assert.AreEqual(One, One.ClearBit(1));
            Assert.AreEqual(Zero, Two.ClearBit(1));

            // TODO Tests for clearing bits in negative numbers

            // TODO Tests for clearing extended bits

            for (int i = 0; i < 10; ++i)
            {
                var n = new BigInteger(128, Rnd);

                for (int j = 0; j < 10; ++j)
                {
                    int        pos  = Rnd.Next(128);
                    BigInteger m    = n.ClearBit(pos);
                    bool       test = m.ShiftRight(pos).Remainder(Two).Equals(One);

                    Assert.IsFalse(test);
                }
            }

            for (int i = 0; i < 100; ++i)
            {
                BigInteger pow2      = One.ShiftLeft(i);
                BigInteger minusPow2 = pow2.Negate();

                Assert.AreEqual(Zero, pow2.ClearBit(i));
                Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.ClearBit(i));

                BigInteger bigI = BigInteger.ValueOf(i);
                BigInteger negI = bigI.Negate();

                for (int j = 0; j < 10; ++j)
                {
                    string data = "i=" + i + ", j=" + j;
                    Assert.AreEqual(bigI.AndNot(One.ShiftLeft(j)), bigI.ClearBit(j), data);
                    Assert.AreEqual(negI.AndNot(One.ShiftLeft(j)), negI.ClearBit(j), data);
                }
            }
        }
Пример #3
0
        public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda)
        {
            if (mu != 1 && mu != -1)
            {
                throw new ArgumentException("mu must be 1 or -1");
            }
            BigInteger bigInteger = Tnaf.Norm(mu, lambda);
            int        bitLength  = bigInteger.BitLength;
            int        num        = (bitLength > 30) ? (bitLength + 4) : 34;

            sbyte[]    array       = new sbyte[num];
            int        num2        = 0;
            int        num3        = 0;
            BigInteger bigInteger2 = lambda.u;
            BigInteger bigInteger3 = lambda.v;

            while (!bigInteger2.Equals(BigInteger.Zero) || !bigInteger3.Equals(BigInteger.Zero))
            {
                if (bigInteger2.TestBit(0))
                {
                    array[num2] = (sbyte)BigInteger.Two.Subtract(bigInteger2.Subtract(bigInteger3.ShiftLeft(1)).Mod(Tnaf.Four)).IntValue;
                    if (array[num2] == 1)
                    {
                        bigInteger2 = bigInteger2.ClearBit(0);
                    }
                    else
                    {
                        bigInteger2 = bigInteger2.Add(BigInteger.One);
                    }
                    num3 = num2;
                }
                else
                {
                    array[num2] = 0;
                }
                BigInteger bigInteger4 = bigInteger2;
                BigInteger bigInteger5 = bigInteger2.ShiftRight(1);
                if (mu == 1)
                {
                    bigInteger2 = bigInteger3.Add(bigInteger5);
                }
                else
                {
                    bigInteger2 = bigInteger3.Subtract(bigInteger5);
                }
                bigInteger3 = bigInteger4.ShiftRight(1).Negate();
                num2++;
            }
            num3++;
            sbyte[] array2 = new sbyte[num3];
            Array.Copy(array, 0, array2, 0, num3);
            return(array2);
        }
Пример #4
0
        public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda)
        {
            if ((mu != 1) && (mu != -1))
            {
                throw new ArgumentException("mu must be 1 or -1");
            }
            int bitLength = Norm(mu, lambda).BitLength;
            int num2      = (bitLength <= 30) ? 0x22 : (bitLength + 4);

            sbyte[]    sourceArray = new sbyte[num2];
            int        index       = 0;
            int        length      = 0;
            BigInteger u           = lambda.u;
            BigInteger v           = lambda.v;

            while (!u.Equals(BigInteger.Zero) || !v.Equals(BigInteger.Zero))
            {
                if (u.TestBit(0))
                {
                    sourceArray[index] = (sbyte)BigInteger.Two.Subtract(u.Subtract(v.ShiftLeft(1)).Mod(Four)).IntValue;
                    if (sourceArray[index] == 1)
                    {
                        u = u.ClearBit(0);
                    }
                    else
                    {
                        u = u.Add(BigInteger.One);
                    }
                    length = index;
                }
                else
                {
                    sourceArray[index] = 0;
                }
                BigInteger integer4 = u;
                BigInteger integer5 = u.ShiftRight(1);
                if (mu == 1)
                {
                    u = v.Add(integer5);
                }
                else
                {
                    u = v.Subtract(integer5);
                }
                v = integer4.ShiftRight(1).Negate();
                index++;
            }
            length++;
            sbyte[] destinationArray = new sbyte[length];
            Array.Copy(sourceArray, 0, destinationArray, 0, length);
            return(destinationArray);
        }
Пример #5
0
        public void TestFlipBit()
        {
            for (int i = 0; i < 10; ++i)
            {
                BigInteger a = new BigInteger(128, 0, random);
                BigInteger b = a;

                for (int x = 0; x < 100; ++x)
                {
                    // Note: Intentionally greater than initial size
                    int pos = random.Next(256);

                    a = a.FlipBit(pos);
                    b = b.TestBit(pos) ? b.ClearBit(pos) : b.SetBit(pos);
                }

                Assert.AreEqual(a, b);
            }

            for (int i = 0; i < 100; ++i)
            {
                BigInteger pow2      = one.ShiftLeft(i);
                BigInteger minusPow2 = pow2.Negate();

                Assert.AreEqual(zero, pow2.FlipBit(i));
                Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.FlipBit(i));

                BigInteger bigI = BigInteger.ValueOf(i);
                BigInteger negI = bigI.Negate();

                for (int j = 0; j < 10; ++j)
                {
                    string data = "i=" + i + ", j=" + j;
                    Assert.AreEqual(bigI.Xor(one.ShiftLeft(j)), bigI.FlipBit(j), data);
                    Assert.AreEqual(negI.Xor(one.ShiftLeft(j)), negI.FlipBit(j), data);
                }
            }
        }
Пример #6
0
        public void TestClearBit()
        {
            Assert.AreEqual(Zero, Zero.ClearBit(0));
            Assert.AreEqual(Zero, One.ClearBit(0));
            Assert.AreEqual(Two, Two.ClearBit(0));

            Assert.AreEqual(Zero, Zero.ClearBit(1));
            Assert.AreEqual(One, One.ClearBit(1));
            Assert.AreEqual(Zero, Two.ClearBit(1));

            // TODO Tests for clearing bits in negative numbers

            // TODO Tests for clearing extended bits

            for (int i = 0; i < 10; ++i)
            {
                var n = new BigInteger(128, Rnd);

                for (int j = 0; j < 10; ++j)
                {
                    int pos = Rnd.Next(128);
                    BigInteger m = n.ClearBit(pos);
                    bool test = m.ShiftRight(pos).Remainder(Two).Equals(One);

                    Assert.IsFalse(test);
                }
            }

            for (int i = 0; i < 100; ++i)
            {
                BigInteger pow2 = One.ShiftLeft(i);
                BigInteger minusPow2 = pow2.Negate();

                Assert.AreEqual(Zero, pow2.ClearBit(i));
                Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.ClearBit(i));

                BigInteger bigI = BigInteger.ValueOf(i);
                BigInteger negI = bigI.Negate();

                for (int j = 0; j < 10; ++j)
                {
                    string data = "i=" + i + ", j=" + j;
                    Assert.AreEqual(bigI.AndNot(One.ShiftLeft(j)), bigI.ClearBit(j), data);
                    Assert.AreEqual(negI.AndNot(One.ShiftLeft(j)), negI.ClearBit(j), data);
                }
            }
        }
Пример #7
0
        /**
         * Computes the <code>&#964;</code>-adic NAF (non-adjacent form) of an
         * element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>.
         * @param mu The parameter <code>&#956;</code> of the elliptic curve.
         * @param lambda The element <code>&#955;</code> of
         * <code><b>Z</b>[&#964;]</code>.
         * @return The <code>&#964;</code>-adic NAF of <code>&#955;</code>.
         */
        public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda)
        {
            if (!((mu == 1) || (mu == -1)))
            {
                throw new ArgumentException("mu must be 1 or -1");
            }

            BigInteger norm = Norm(mu, lambda);

            // Ceiling of log2 of the norm
            int log2Norm = norm.BitLength;

            // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52
            int maxLength = log2Norm > 30 ? log2Norm + 4 : 34;

            // The array holding the TNAF
            sbyte[] u = new sbyte[maxLength];
            int     i = 0;

            // The actual length of the TNAF
            int length = 0;

            BigInteger r0 = lambda.u;
            BigInteger r1 = lambda.v;

            while (!((r0.Equals(BigInteger.Zero)) && (r1.Equals(BigInteger.Zero))))
            {
                // If r0 is odd
                if (r0.TestBit(0))
                {
                    u[i] = (sbyte)BigInteger.Two.Subtract((r0.Subtract(r1.ShiftLeft(1))).Mod(Four)).IntValue;

                    // r0 = r0 - u[i]
                    if (u[i] == 1)
                    {
                        r0 = r0.ClearBit(0);
                    }
                    else
                    {
                        // u[i] == -1
                        r0 = r0.Add(BigInteger.One);
                    }
                    length = i;
                }
                else
                {
                    u[i] = 0;
                }

                BigInteger t = r0;
                BigInteger s = r0.ShiftRight(1);
                if (mu == 1)
                {
                    r0 = r1.Add(s);
                }
                else
                {
                    // mu == -1
                    r0 = r1.Subtract(s);
                }

                r1 = t.ShiftRight(1).Negate();
                i++;
            }

            length++;

            // Reduce the TNAF array to its actual length
            sbyte[] tnaf = new sbyte[length];
            Array.Copy(u, 0, tnaf, 0, length);
            return(tnaf);
        }
Пример #8
0
 public void ClearBit_pos_outside()
 {
     BigInteger x = new BigInteger(1, 0xFFFF0000, 0xFFFF0000);
     BigInteger y = x.ClearBit(99);
     Expect(y, SameAs(x));
 }
Пример #9
0
 public void ClearBit_pos_inside_initial_set()
 {
     BigInteger x = new BigInteger(1, 0xFFFF0000, 0xFFFF0000);
     BigInteger w = new BigInteger(1, 0xFEFF0000, 0xFFFF0000);
     BigInteger y = x.ClearBit(56);
     Expect(y == w);
 }
Пример #10
0
 public void ClearBit_pos_inside_initial_clear()
 {
     BigInteger x = new BigInteger(1, 0xFFFF0000, 0xFFFF0000);
     BigInteger y = x.ClearBit(39);
     Expect(y, SameAs(x));
 }
Пример #11
0
 public void ClearBit_neg_outside()
 {
     BigInteger x = new BigInteger(-1, 0xFFFF0000, 0xFFFF0000);
     BigInteger y = x.ClearBit(99);
     Expect(SameValue(y, -1, new uint[] { 8, 0, 0xFFFF0000, 0xFFFF0000 }));
 }
Пример #12
0
 public void ClearBit_neg_inside_initial_set()
 {
     BigInteger x = new BigInteger(-1, 0xFFFF0000, 0xFFFF0000);
     BigInteger y = x.ClearBit(39);
     Expect(SameValue(y, -1, new uint[] { 0xFFFF0080, 0xFFFF0000 }));
 }
Пример #13
0
            /// <summary>
            /// Chooses the nth particular prime which is lower than the startValue
            /// </summary>
            /// <param name="startValue">The value which we start to look for a prime from</param>
            /// <param name="e">Exponent</param>
            /// <param name="n">The index of the prime</param>
            /// <returns></returns>
            protected BigInteger ChooseNthPrime(BigInteger startValue, BigInteger e, int n, int bitLength)
            {
                bool eIsKnownOddPrime = (e.BitLength <= SPECIAL_E_BITS) && Arrays.Contains(SPECIAL_E_VALUES, e.IntValue);

                BigInteger p = new BigInteger(startValue.ToByteArray());

                while (true)
                {
                    if (p.BitLength > bitLength)
                    {
                        Console.WriteLine($" -NOTE- The bitrate raised from {bitLength} to {p.BitLength}, so we reset our p value.");
                        p = p.ClearBit(bitLength);
                    }

                    if (p.BitLength < bitLength)
                    {
                        Console.WriteLine($" -NOTE- The bitrate dropped from {bitLength} to {p.BitLength}, so we reset our p value.");
                        p = p.SetBit(bitLength - 1);
                    }



                    if (n > 0)
                    {
                        // Add one to p in an efficient way
                        for (int i = 0; i <= p.BitLength; i++)
                        {
                            if (!p.TestBit(i))
                            {
                                p = p.SetBit(i);
                                break;
                            }
                            p = p.ClearBit(i);
                        }
                    }
                    else if (n < 0)
                    {
                        // Subtract one from p in an efficient way
                        int lsb = p.GetLowestSetBit();
                        p = p.ClearBit(lsb);
                        for (int i = 0; i < lsb; i++)
                        {
                            p = p.SetBit(i);
                        }
                    }

                    if (!p.TestBit(0))
                    {
                        continue;
                    }

                    if (p.Mod(e).Equals(One))
                    {
                        continue;
                    }

                    if (!p.IsProbablePrime(this.parameters.Certainty))
                    {
                        continue;
                    }

                    if (!eIsKnownOddPrime && !e.Gcd(p.Subtract(One)).Equals(One))
                    {
                        continue;
                    }

                    // p is a prime
                    if (n > 0)
                    {
                        n--;
                    }
                    else if (n < 0)
                    {
                        n++;
                    }

                    if (n == 0)
                    {
                        break;
                    }
                }

                return(p);
            }
Пример #14
0
        public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda)
        {
            //IL_000d: Unknown result type (might be due to invalid IL or missing references)
            if (mu != 1 && mu != -1)
            {
                throw new ArgumentException("mu must be 1 or -1");
            }
            BigInteger bigInteger = Norm(mu, lambda);
            int        bitLength  = bigInteger.BitLength;
            int        num        = ((bitLength > 30) ? (bitLength + 4) : 34);

            sbyte[]    array       = new sbyte[num];
            int        num2        = 0;
            int        num3        = 0;
            BigInteger bigInteger2 = lambda.u;
            BigInteger bigInteger3 = lambda.v;

            while (!bigInteger2.Equals(BigInteger.Zero) || !bigInteger3.Equals(BigInteger.Zero))
            {
                if (bigInteger2.TestBit(0))
                {
                    array[num2] = (sbyte)BigInteger.Two.Subtract(bigInteger2.Subtract(bigInteger3.ShiftLeft(1)).Mod(Four)).IntValue;
                    bigInteger2 = ((array[num2] != 1) ? bigInteger2.Add(BigInteger.One) : bigInteger2.ClearBit(0));
                    num3        = num2;
                }
                else
                {
                    array[num2] = 0;
                }
                BigInteger bigInteger4 = bigInteger2;
                BigInteger bigInteger5 = bigInteger2.ShiftRight(1);
                bigInteger2 = ((mu != 1) ? bigInteger3.Subtract(bigInteger5) : bigInteger3.Add(bigInteger5));
                bigInteger3 = bigInteger4.ShiftRight(1).Negate();
                num2++;
            }
            num3++;
            sbyte[] array2 = new sbyte[num3];
            global::System.Array.Copy((global::System.Array)array, 0, (global::System.Array)array2, 0, num3);
            return(array2);
        }