Exemple #1
0
        private static BigInteger Inverse(BigInteger n)
        {
            n.CheckRange();
            var inverse = GCD2(Crypto.FieldPrime, n).invB.Abs();

            Contract.Assert((n * inverse) % Crypto.FieldPrime == 1);
            return(inverse);
        }
Exemple #2
0
        public static BigInteger RationalToWhole(BigInteger numerator, BigInteger denominator)
        {
            numerator.CheckRange(); denominator.CheckRange();

            // Find the multiplicative inverse of the denominator over the field
            var invD = Inverse(denominator);

            return(numerator * invD % Crypto.FieldPrime);
        }
Exemple #3
0
        private static BigInteger GCD(BigInteger a, BigInteger b)
        {
            a.CheckRange(); b.CheckRange();

            if (b == 0)
            {
                return(a);
            }
            return(GCD(b, a % b));
        }
Exemple #4
0
        public static (BigInteger numerator, BigInteger denominator) MultiplyRational(
            BigInteger numeratorLhs, BigInteger denominatorLhs,
            BigInteger numeratorRhs, BigInteger denominatorRhs)
        {
            denominatorRhs = denominatorRhs.Abs();

            numeratorLhs.CheckRange(); denominatorLhs.CheckRange();
            numeratorRhs.CheckRange(); denominatorRhs.CheckRange();

            if (denominatorLhs == 0)
            {
                throw new ArgumentOutOfRangeException("LHS denominator is zero");
            }
            if (denominatorRhs == 0)
            {
                throw new ArgumentOutOfRangeException("RHS denominator is zero");
            }

            var numerator   = (numeratorLhs * numeratorRhs) % Crypto.FieldPrime;
            var denominator = (denominatorLhs * denominatorRhs) % Crypto.FieldPrime;
            var gcd         = GCD(numerator, denominator);

            return(numerator / gcd, denominator / gcd);
        }