public Reducer(UInt32MontgomeryReduction reduction, uint modulus)
                : base(reduction, modulus)
            {
                if ((modulus & 1) == 0)
                {
                    throw new InvalidOperationException("not relatively prime");
                }
                var nInv = IntegerMath.ModularInversePowerOfTwoModulus(modulus, 32);

                k0 = IntegerMath.TwosComplement(nInv);
                var rModN = uint.MaxValue % modulus + 1;

                rSquaredModN = IntegerMath.ModularProduct(rModN, rModN, modulus);
            }
Exemplo n.º 2
0
        private long Rho(long n, long xInit, long c)
        {
            if ((n & 1) == 0)
            {
                return(2);
            }

            var x  = xInit;
            var y  = xInit;
            var ys = y;
            var r  = 1;
            var m  = batchSize;
            var g  = (long)1;

            do
            {
                x = y;
                for (int i = 0; i < r; i++)
                {
                    y = F(y, c, n);
                }
                var k = 0;
                while (k < r && g == 1)
                {
                    ys = y;
                    var limit = Math.Min(m, r - k);
                    var q     = (long)1;
                    for (int i = 0; i < limit; i++)
                    {
                        y = F(y, c, n);
                        q = IntegerMath.ModularProduct(q, x - y, n);
                    }
                    g  = IntegerMath.GreatestCommonDivisor(q, n);
                    k += limit;
                }
                r <<= 1;
            }while (g == 1);

            if (g == n)
            {
                do
                {
                    ys = F(ys, c, n);
                    g  = IntegerMath.GreatestCommonDivisor(x - ys, n);
                }while (g == 1);
            }

            return(g);
        }
            public Reducer(UInt64MontgomeryReduction reduction, ulong modulus)
                : base(reduction, modulus)
            {
                if ((modulus & 1) == 0)
                {
                    throw new InvalidOperationException("not relatively prime");
                }
                int rLength   = modulus == (uint)modulus ? 32 : 64;
                var rMinusOne = rLength == 32 ? uint.MaxValue : ulong.MaxValue;
                var rModN     = rMinusOne % modulus + 1;

                rSquaredModN = IntegerMath.ModularProduct(rModN, rModN, modulus);
                var nInv = IntegerMath.ModularInversePowerOfTwoModulus(modulus, rLength);

                k0     = (uint)IntegerMath.TwosComplement(nInv);
                oneRep = 0;
            }
Exemplo n.º 4
0
 public override Rational ModularProduct(Rational a, Rational b, Rational modulus)
 {
     return(IntegerMath.ModularProduct((BigInteger)a, (BigInteger)b, (BigInteger)modulus));
 }
Exemplo n.º 5
0
 public override uint ModularProduct(uint a, uint b, uint modulus)
 {
     return(IntegerMath.ModularProduct(a, b, modulus));
 }
Exemplo n.º 6
0
 public IResidue <int> Multiply(IResidue <int> x)
 {
     r = IntegerMath.ModularProduct(r, ((Residue)x).r, reducer.Modulus);
     return(this);
 }
Exemplo n.º 7
0
 protected static long F(long x, long c, long n)
 {
     return((IntegerMath.ModularProduct(x, x, n) + c) % n);
 }
Exemplo n.º 8
0
 public override ulong ModularProduct(ulong a, ulong b, ulong modulus)
 {
     return(IntegerMath.ModularProduct(a, b, modulus));
 }
 public override double ModularProduct(double a, double b, double modulus)
 {
     return((double)IntegerMath.ModularProduct(ToBigInteger(a), ToBigInteger(b), ToBigInteger(modulus)));
 }
Exemplo n.º 10
0
 public override Complex ModularProduct(Complex a, Complex b, Complex modulus)
 {
     return((Complex)IntegerMath.ModularProduct(ToBigInteger(a), ToBigInteger(b), ToBigInteger(modulus)));
 }
 public override BigInteger ModularProduct(BigInteger a, BigInteger b, BigInteger modulus)
 {
     return(IntegerMath.ModularProduct(a, b, modulus));
 }
Exemplo n.º 12
0
 public override IResidue <UInt128> Multiply(IResidue <UInt128> x)
 {
     r = IntegerMath.ModularProduct(r, GetRep(x), reducer.modulus);
     return(this);
 }