⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asgtmath.int

📁 CryptoKit使用简便的加密解密控件。(源代码
💻 INT
字号:
unit ASGtMath;
interface
uses
  SysUtils, Windows, ASUtils;

const
  MaxGIntDWords = 1024;

type
  GiantInt = ^TGiantInt;
  TGiantInt = record
    sign: Integer;                                            // size = abs(sign), sign = Sign(sign)
    case Integer of
    0: (n: array[0..MaxGIntDWords-1]   of DWord);             // array of DWord, abs(Sign) in length
    1: (b: array[0..MaxGIntDWords*4-1] of Byte);              // array of Byte
  end;

  function  NewGiantInt: GiantInt;
  procedure FreeGiantInt(g: GiantInt);
  function  popg: GiantInt;
  procedure pushg(a: Integer);

  procedure clearg(g: GiantInt);
  procedure gtog(src, dest: GiantInt);                        // dest becomes equal to src.
  procedure itog(i: Integer; g: GiantInt);                    // The GiantInt g becomes set to the integer value i.
  function  gtoi(g: GiantInt): Integer;                       // Calculate the value of an int-sized giant NOT exceeding 31 bits.
  procedure utog(u: DWord; g: GiantInt);

  procedure pdwtog(p: PDWordArray; sign: Integer; g: GiantInt); // The GiantInt g becomes set to array p
  procedure pbtog(p: PByteArray; count: Integer; g: GiantInt; reverse: Boolean);
  procedure gtopb(g: GiantInt; p: Pointer; count: Integer; reverse: Boolean);

  function  iszero(g: GiantInt): Boolean;                     // Returns True if g = 0.
  function  isone(g: GiantInt): Boolean;
  function  isodd(g: GiantInt): Boolean;
  function  isprime(n: GiantInt; checks: Integer): Boolean;   // Returns true if n is prime
  function  signg(g: GiantInt): Integer;                      // Returns the sign of g.
  procedure absg(g: GiantInt);                                // g becomes the absolute value of g.
  procedure negg(g: GiantInt);                                // g becomes -g.
  procedure justg(g: GiantInt);                               // truncate GiantInt length to significant
  function  bitlen(g: GiantInt): Integer;
  function  bitval(g: GiantInt; pos: Integer): DWord;
  procedure shlg(bits: Integer; g: GiantInt);                 // shift g left bits bits. Equivalent to g = g*2^bits.
  procedure shrg(bits: Integer; g: GiantInt);                 // shift g right bits bits. Equivalent to g = g/2^bits.
  function  numtrailzeros(g: GiantInt): Integer;              // Returns the number of trailing zero bits in g.

  procedure powermodg(x, e, n: GiantInt);                     // x becomes x^e (mod n)
  procedure powerg(m, n: Integer; g: GiantInt);               // g becomes m^n, NO mod performed.
  function  invmodg(p, a: GiantInt): Boolean;                 // solves (a * y) mod p = 1, then a becomes y
  function  mod_inverse(n, a: GiantInt): Boolean;             // solves ax == 1 (mod n), then a becomes x
  function  binvg(p, a: GiantInt): Boolean;                   // Returns False if no binary inverse exists, in which case x becomes GCD(x,p).

  procedure mulg(a, b: GiantInt);                             // b becomes a * b.
  procedure squareg(b: GiantInt);                             // b becomes b^2.

  procedure gtodivisor(g: GiantInt);                          // g becomes current divisor for divgnext, modgnext
  procedure divg(d, n: GiantInt);                             // n becomes n / d. n is arbitrary, but the denominator d must be positive!
  procedure divgnext(n: GiantInt);                            // n becomes n/d. n is arbitrary, but the denominator d must be positive!
  function  idivg(d: Integer; g: GiantInt): Integer;          // g becomes g / d. Returns remainder.
  procedure bdivg(v, u: GiantInt);                            // u becomes greatest power of two not exceeding u/v.
  procedure modg(d, n: GiantInt);                             // n becomes n % d. n is arbitrary, but the denominator d must be positive!
  procedure modgnext(n: GiantInt);                            // n becomes n % d. n is arbitrary, but the denominator d must be positive!
  function  imodg(d: Integer; g: GiantInt): Integer;          // Returns n % d.

  procedure subg(a, b: GiantInt);                             // b := b - a, any signs, any result.
  procedure addg(a, b: GiantInt);                             // b := b + a, any signs any result.
  procedure iaddg(i: Integer; g: GiantInt);                   // GiantInt g becomes g + i.
  function  compg(a, b: GiantInt): Integer;                   // Returns -1,0,1 if a<b, a=b, a>b, respectively.
  procedure gcdg(a, b: GiantInt);                             // b becomes the gcd of a,b.
  procedure randg(rnd: GiantInt; bits: Integer; top, bottom: Boolean);

  function  IntIsPrime(t: Int64): Boolean;
  function  MillerRabinPrimeTest(n: GiantInt; checks: Integer): Boolean;

  function  GiantIntToHex(g: GiantInt): string;               // 1 byte becomes 2 chars 
  procedure HexToGiantInt(const S: string; g: GiantInt);      // 2 chars give us 1 byte

type
  EGiantIntError = class(EMathError) end;

implementation

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -