public static BigInteger ValidatePublicValue(BigInteger N, BigInteger val)
 {
     val = val.Mod(N);
     if (val.Equals(BigInteger.Zero))
     {
         throw new Exception("Invalid public value: 0");
     }
     return val;
 }
Exemple #2
0
 public BigInteger ModPow(BigInteger exponent, BigInteger m)
 {
     if (m.sign < 1)
     {
         throw new ArithmeticException("Modulus must be positive");
     }
     if (m.Equals(One))
     {
         return Zero;
     }
     if (exponent.sign == 0)
     {
         return One;
     }
     if (this.sign == 0)
     {
         return Zero;
     }
     int[] array = null;
     int[] a = null;
     bool flag = (m.magnitude[m.magnitude.Length - 1] & 1) == 1;
     long mQuote = 0L;
     if (flag)
     {
         mQuote = m.GetMQuote();
         array = this.ShiftLeft(0x20 * m.magnitude.Length).Mod(m).magnitude;
         flag = array.Length <= m.magnitude.Length;
         if (flag)
         {
             a = new int[m.magnitude.Length + 1];
             if (array.Length < m.magnitude.Length)
             {
                 int[] numArray4 = new int[m.magnitude.Length];
                 array.CopyTo(numArray4, (int)(numArray4.Length - array.Length));
                 array = numArray4;
             }
         }
     }
     if (!flag)
     {
         if (this.magnitude.Length <= m.magnitude.Length)
         {
             array = new int[m.magnitude.Length];
             this.magnitude.CopyTo(array, (int)(array.Length - this.magnitude.Length));
         }
         else
         {
             BigInteger integer2 = this.Remainder(m);
             array = new int[m.magnitude.Length];
             integer2.magnitude.CopyTo(array, (int)(array.Length - integer2.magnitude.Length));
         }
         a = new int[m.magnitude.Length * 2];
     }
     int[] numArray3 = new int[m.magnitude.Length];
     for (int i = 0; i < exponent.magnitude.Length; i++)
     {
         int num3 = exponent.magnitude[i];
         int num4 = 0;
         if (i == 0)
         {
             while (num3 > 0)
             {
                 num3 = num3 << 1;
                 num4++;
             }
             array.CopyTo(numArray3, 0);
             num3 = num3 << 1;
             num4++;
         }
         while (num3 != 0)
         {
             if (flag)
             {
                 MultiplyMonty(a, numArray3, numArray3, m.magnitude, mQuote);
             }
             else
             {
                 Square(a, numArray3);
                 this.Remainder(a, m.magnitude);
                 Array.Copy(a, a.Length - numArray3.Length, numArray3, 0, numArray3.Length);
                 ZeroOut(a);
             }
             num4++;
             if (num3 < 0)
             {
                 if (flag)
                 {
                     MultiplyMonty(a, numArray3, array, m.magnitude, mQuote);
                 }
                 else
                 {
                     Multiply(a, numArray3, array);
                     this.Remainder(a, m.magnitude);
                     Array.Copy(a, a.Length - numArray3.Length, numArray3, 0, numArray3.Length);
                     ZeroOut(a);
                 }
             }
             num3 = num3 << 1;
         }
         while (num4 < 0x20)
         {
             if (flag)
             {
                 MultiplyMonty(a, numArray3, numArray3, m.magnitude, mQuote);
             }
             else
             {
                 Square(a, numArray3);
                 this.Remainder(a, m.magnitude);
                 Array.Copy(a, a.Length - numArray3.Length, numArray3, 0, numArray3.Length);
                 ZeroOut(a);
             }
             num4++;
         }
     }
     if (flag)
     {
         ZeroOut(array);
         array[array.Length - 1] = 1;
         MultiplyMonty(a, numArray3, array, m.magnitude, mQuote);
     }
     BigInteger integer3 = new BigInteger(1, numArray3, true);
     if (exponent.sign <= 0)
     {
         return integer3.ModInverse(m);
     }
     return integer3;
 }