Beispiel #1
0
    /// <summary>
    /// Finds the remainder of a/b, using the shift and subtract method
    /// </summary>
    public static SimpleBignum operator %(SimpleBignum a, SimpleBignum b)
    {
        SimpleBignum result = new SimpleBignum(a); // Start off with out=a, and whittle it down
        int          len_a  = a.SignificantBytes;  // Get the lengths
        int          len_b  = b.SignificantBytes;

        if (len_b > len_a)
        {
            return(result);                             // Simple case: since b is bigger, a is already the modulus
        }
        int          byte_shifts = len_a - len_b + 1;   // Figure out how many shifts needed to make b bigger than a
        SimpleBignum shifted     = new SimpleBignum(b); // shifted = b

        shifted.ShiftLeftDigits(byte_shifts);           // Now b is shifted bigger than a
        // Now do a series of bit shifts on B, subtracting it from A each time
        for (int i = 0; i < byte_shifts * 8; i++)
        {
            shifted.ShiftRight1Bit();

            if (result >= shifted)
            {
                result -= shifted;
            }
        }
        return(result);
    }
Beispiel #2
0
    /// <summary>
    /// Power Modulus: this ^ power % mod
    /// This does modular exponentiation using the right-to-left binary method
    /// This is actually quite slow, mainly due to the mod function, but also the mult is slow too
    /// Clobbers power
    /// </summary>
    public SimpleBignum PowMod(SimpleBignum power, SimpleBignum mod)
    {
        SimpleBignum result  = new SimpleBignum("01"); // result = 1
        SimpleBignum baseNum = new SimpleBignum(this); // Make a copy of this

        while (power.GreaterThanZero)                  // while power > 0
        {
            if ((power.digits[0] & 1) == 1)            // If lowest bit is set
            {
                result = (result * baseNum) % mod;     // result = result*base % mod
            }
            baseNum = (baseNum * baseNum) % mod;       // base = base^2 % mod

            power.ShiftRight1Bit();                    // power>>=1
        }

        return(result);
    }
Beispiel #3
0
  /// <summary>
  /// Power Modulus: this ^ power % mod
  /// This does modular exponentiation using the right-to-left binary method
  /// This is actually quite slow, mainly due to the mod function, but also the mult is slow too
  /// Clobbers power
  /// </summary>
  public SimpleBignum PowMod(SimpleBignum power, SimpleBignum mod)
  {
    SimpleBignum result = new SimpleBignum("01"); // result = 1
    SimpleBignum baseNum = new SimpleBignum(this); // Make a copy of this

    while (power.GreaterThanZero) // while power > 0
    {
      if ((power.digits[0] & 1) == 1) // If lowest bit is set
        result = (result * baseNum) % mod; // result = result*base % mod

      baseNum = (baseNum * baseNum) % mod; // base = base^2 % mod

      power.ShiftRight1Bit(); // power>>=1
    }

    return result;
  }
Beispiel #4
0
  /// <summary>
  /// Finds the remainder of a/b, using the shift and subtract method
  /// </summary>
  public static SimpleBignum operator %(SimpleBignum a, SimpleBignum b)
  {
    SimpleBignum result = new SimpleBignum(a); // Start off with out=a, and whittle it down 
    int len_a = a.SignificantBytes; // Get the lengths
    int len_b = b.SignificantBytes;
    if (len_b > len_a) return result; // Simple case: since b is bigger, a is already the modulus
    int byte_shifts = len_a - len_b + 1; // Figure out how many shifts needed to make b bigger than a
    SimpleBignum shifted = new SimpleBignum(b); // shifted = b
    shifted.ShiftLeftDigits(byte_shifts); // Now b is shifted bigger than a
    // Now do a series of bit shifts on B, subtracting it from A each time
    for (int i = 0; i < byte_shifts * 8; i++)
    {
      shifted.ShiftRight1Bit();

      if (result >= shifted)
        result -= shifted;
    }
    return result;
  }