anchor.diff:  
     end;
   
  +// Returns sign bit (top bit) of an integer.
   function SignBitOf(Value: Integer): Integer; inline;
   
  +// Returns the minimum of two BigIntegers.
   function Min(const A, B: BigInteger): BigInteger; overload; inline;
  +// Returns the maximum of two BigIntegers.
   function Max(const A, B: BigInteger): BigInteger; overload; inline;
   
   var
  +  // Set this to True if you want to generate debug output.
     DoDebug: Boolean = True;
   
   implementation
  
positive.code:     ShallowCopy(Right, Result);
  end;
  
  class function BigInteger.ModInverse(const Value, Modulus: BigInteger): BigInteger;
  var
    NewResult, Rem, NewRem, Temp, Quot: BigInteger;
  begin
    Result := Zero;
    Rem := Abs(Modulus);
    if (Rem = One) or (Rem = Zero) then
      Error(ecNoInverse, '');
    if Value.IsNegative then
      NewRem := Remainder(Subtract(Rem, Remainder(-Value, Rem)), Rem)
    else
      NewRem := Remainder(Value, Rem);
    Result := Zero;
    NewResult := One;
    while not NewRem.IsZero do
    begin
      Quot := Divide(Rem, NewRem);
      Temp := NewResult;
      NewResult := Subtract(Result, Multiply(Quot, NewResult));
      Result := Temp;
      Temp := NewRem;
      NewRem := Subtract(Rem, Multiply(Quot, NewRem));
      Rem := Temp;
    end;
    if Rem > One then
      Error(ecNoInverse);
    if Result.IsPositive <> Value.IsPositive then
      if Result.IsNegative then
        Result := Add(Result, Abs(Modulus))
      else
        Result := Subtract(Result, Abs(Modulus));
  end;
  
  // http://stackoverflow.com/questions/8496182/calculating-powa-b-mod-n
  class function BigInteger.ModPow(const ABase, AExponent, AModulus: BigInteger): BigInteger;
  
negative.code: {   2016-12-29: Changed return type of Compare() to Integer. Using           }
  {               TValueSign made inlining several comparison operators        }
  {               harder, since that required System.Math.                     }
  {                                                                            }
  {   2016-12-30: Changed implementation of ModInverse to 20% faster version.  }
  {----------------------------------------------------------------------------}
  
  unit Velthuis.BigIntegers;
  