public static BigInteger ModPow(BigInteger value, BigInteger exponent, BigInteger modulus)
        {
            if (exponent.Sign < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(exponent));
            }

            value.AssertValid();
            exponent.AssertValid();
            modulus.AssertValid();

            bool trivialValue    = value._bits == null;
            bool trivialExponent = exponent._bits == null;
            bool trivialModulus  = modulus._bits == null;

            if (trivialModulus)
            {
                uint bits = trivialValue && trivialExponent?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) :
                                trivialValue?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits, NumericsHelpers.Abs(modulus._sign)) :
                                    trivialExponent?BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) :
                                        BigIntegerCalculator.Pow(value._bits, exponent._bits, NumericsHelpers.Abs(modulus._sign));

                return(value._sign < 0 && !exponent.IsEven ? -1 * bits : bits);
            }
            else
            {
                uint[] bits = trivialValue && trivialExponent?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), modulus._bits) :
                                  trivialValue?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits, modulus._bits) :
                                      trivialExponent?BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent._sign), modulus._bits) :
                                          BigIntegerCalculator.Pow(value._bits, exponent._bits, modulus._bits);

                return(new BigInteger(bits, value._sign < 0 && !exponent.IsEven));
            }
        }
예제 #2
0
 public BigIntegerBuilder(BigInteger bn)
 {
     this._fWritable = false;
     this._rgu       = bn._Bits;
     if (this._rgu == null)
     {
         this._iuLast = 0;
         this._uSmall = NumericsHelpers.Abs(bn._Sign);
     }
     else
     {
         this._iuLast = this._rgu.Length - 1;
         this._uSmall = this._rgu[0];
         while ((this._iuLast > 0) && (this._rgu[this._iuLast] == 0))
         {
             this._iuLast--;
         }
     }
 }
예제 #3
0
 public BigIntegerBuilder(BigInteger bn)
 {
     _fWritable = false;
     _rgu       = bn._Bits;
     if (_rgu == null)
     {
         _iuLast = 0;
         _uSmall = NumericsHelpers.Abs(bn._Sign);
     }
     else
     {
         _iuLast = _rgu.Length - 1;
         _uSmall = _rgu[0];
         while (_iuLast > 0 && _rgu[_iuLast] == 0)
         {
             --_iuLast;
         }
     }
 }
예제 #4
0
 public BigIntegerBuilder(BigInteger bn)
 {
     _fWritable = false;
     _bits      = bn.InternalBits;
     if (_bits != null)
     {
         _iuLast = _bits.Length - 1;
         _uSmall = _bits[0];
         while (_iuLast > 0 && _bits[_iuLast] == 0)
         {
             _iuLast--;
         }
     }
     else
     {
         _iuLast = 0;
         _uSmall = NumericsHelpers.Abs(bn.InternalSign);
     }
 }