Beispiel #1
0
        /// <summary>
        /// Returns the Nth root of this instance plus the Remainder.
        // The root must be greater than or equal to 1.
        /// </summary>
        public static BigInteger NthRoot(this BigInteger source, int root, out BigInteger remainder)
        {
            if (root < 1)
            {
                throw new Exception("Root must be greater than or equal to 1");
            }
            if (source.Sign == -1)
            {
                throw new Exception("Value must be a positive integer");
            }

            if (source == BigInteger.One)
            {
                remainder = 0;
                return(BigInteger.One);
            }
            if (source == BigInteger.Zero)
            {
                remainder = 0;
                return(BigInteger.Zero);
            }
            if (root == 1)
            {
                remainder = 0;
                return(source.Clone());
            }

            BigInteger upperbound = source.Clone();
            BigInteger lowerbound = new BigInteger(0);

            while (true)
            {
                BigInteger nval  = (upperbound + lowerbound) >> 1;
                BigInteger tstsq = BigInteger.Pow(nval, root);

                if (tstsq > source)
                {
                    upperbound = nval;
                }
                if (tstsq < source)
                {
                    lowerbound = nval;
                }
                if (tstsq == source)
                {
                    lowerbound = nval;
                    break;
                }
                if (lowerbound == upperbound - 1)
                {
                    break;
                }
            }
            remainder = source - BigInteger.Pow(lowerbound, root);
            return(lowerbound);
        }
		public PublicKeyEncSessionPacket(
			long					keyId,
			PublicKeyAlgorithmTag	algorithm,
			BigInteger[]			data)
		{
			this.version = 3;
			this.keyId = keyId;
			this.algorithm = algorithm;
			this.data = (BigInteger[]) data.Clone();
		}
Beispiel #3
0
        /// <summary>
        /// Returns the remainder of this instance modulus the specified number.
        /// </summary>
        public static BigInteger Mod(this BigInteger source, BigInteger mod)
        {
            if (mod.Equals(BigInteger.Zero))
            {
                throw new DivideByZeroException($"Parameter '{nameof(mod)}' must not be zero.");
            }
            BigInteger n = source.Clone();
            BigInteger r = (n >= mod) ? n % mod : n;

            return((r < 0) ? (r + mod) : r);
        }
Beispiel #4
0
    public static void Test_Clone()
    {
        BigInteger n1 = new BigInteger("12345");

        Console.WriteLine("n1: {0}", n1);
        BigInteger n2 = (BigInteger)n1.Clone();

        Console.WriteLine("n2: {0}", n2);
        Console.WriteLine("{0}", n1 == n2);
        Console.WriteLine();
    }
Beispiel #5
0
        public T Inverse()
        {
            // Define our initial variables and give them their initial assignments.
            BigInteger[] newt = new BigInteger[Degree + 1];
            newt[0] = 1;

            BigInteger[] t = new BigInteger[Degree + 1];

            BigInteger[] newr = new BigInteger[Coefficients.Count + 1];
            Coefficients.CopyTo(newr, 0);

            BigInteger[] r = new BigInteger[ModulusCoefficients.Count + 1];
            ModulusCoefficients.CopyTo(r, 0);
            r[r.Length - 1] = 1;

            // Loop while there are elements which are non-zero.
            while (GetDegree(newr) != 0)
            {
                BigInteger[] quotient = DividePolynomialRounded(r, newr);
                Array.Resize(ref quotient, Degree + 1);

                BigInteger[] tempt = (BigInteger[])t.Clone();
                BigInteger[] tempr = (BigInteger[])r.Clone();
                for (int i = 0; i < Degree + 1; i++)
                {
                    for (int j = 0; j < Degree + 1 - i; j++)
                    {
                        tempt[i + j] -= newt[i] * quotient[j];
                        tempr[i + j] -= newr[i] * quotient[j];
                    }
                }

                // Perform modulo on tempt
                for (int i = 0; i < tempt.Length; i++)
                {
                    tempt[i] = tempt[i].Mod(Bn128Curve.P);
                }

                // Perform modulo on tempr
                for (int i = 0; i < tempr.Length; i++)
                {
                    tempr[i] = tempr[i].Mod(Bn128Curve.P);
                }

                // Swap state for the next iteration.
                (newt, newr, t, r) = (tempt, tempr, newt, newr);
            }

            // Resize the array to the degree size accordingly, divide and return.
            Array.Resize(ref newt, Degree);
            return(New(newt).Divide(newr[0]));
        }
        public static BigInteger Mod(this BigInteger n, BigInteger mod)
        {
            BigInteger r = (n >= mod) ? n % mod : n;

            return((r < 0) ? BigInteger.Add(r, mod) : r.Clone());
        }
 public Term(BigInteger coefficient, Indeterminate[] variables)
 {
     CoEfficient = coefficient.Clone();
     Variables   = CloneHelper <Indeterminate> .CloneCollection(variables).ToArray();
 }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Term"/> class with the specified coefficient and exponent.
 /// </summary>
 public Term(BigInteger coefficient, int exponent)
 {
     Exponent    = exponent;
     CoEfficient = coefficient.Clone();
 }