Unit Big Integer
Return to Unit Reference
Description
Ultibo Big Integer Interface unit
This unit implements multiple precision integer arithmetic operations as well as multiple precision modular arithmetic including addition, subtraction, multiplication, division, square, modular reduction and modular exponentiation.
The unit is primarily intended to support the RSA functions within the Crypto unit as well as other cryptographic functionality.
Constants
BIGINT_*
Maintain a number of precomputed variables when doing reduction | |
BIGINT_M_OFFSET = 0;
|
Normal modulo offset |
BIGINT_P_OFFSET = 1;
|
P modulo offset |
BIGINT_Q_OFFSET = 2;
|
Q modulo offset |
BIGINT_NUM_MODS = 3;
|
The number of modulus constants used |
BIGINT_COMP_RADIX = UInt64(4294967296);
|
Max component + 1 |
BIGINT_COMP_MAX = UInt64($FFFFFFFFFFFFFFFF);
|
Max dbl component - 1 |
BIGINT_COMP_BIT_SIZE = 32;
|
Number of bits in a component |
BIGINT_COMP_BYTE_SIZE = 4;
|
Number of bytes in a component |
BIGINT_COMP_NUM_NIBBLES = 8;
|
Used for diagnostics only |
BIGINT_PERMANENT = $7FFF55AA;
|
A magic number for permanents |
Type definitions
Component
PComponent = ^TComponent;
TComponent = LongWord;
A single precision component | |
Long component
PLongComponent = ^TLongComponent;
TLongComponent = UInt64;
A double precision component | |
Signed long component
PSignedLongComponent = ^TSignedLongComponent;
TSignedLongComponent = Int64;
A signed double precision component | |
BigInt
PPBigInt = ^PBigInt;
PBigInt = ^TBigInt;
TBigInt = record
A big integer basic object | |
Next:PBigInt;
|
The next bigint in the cache |
Size:Integer;
|
The number of components in this bigint |
MaxComponents:Integer;
|
The number of components allocated for this bigint |
References:Integer;
|
An internal reference count |
Components:PComponent;
|
A ptr to the actual component data |
procedure Zero;
|
|
procedure Clear;
|
|
function ToString:String;
|
BigInt context
PBigIntContext = ^TBigIntContext;
TBigIntContext = record
Maintains the state of the cache, and a number of variables used in reduction. | |
ActiveList:PBigInt;
|
Bigints currently used |
FreeList:PBigInt;
|
Bigints not used |
BIRadix:PBigInt;
|
The radix used |
BIMod:array[0..BIGINT_NUM_MODS - 1] of PBigInt;
|
Modulus |
BImu:array[0..BIGINT_NUM_MODS - 1] of PBigInt;
|
Storage for mu |
BIbk1:array[0..BIGINT_NUM_MODS - 1] of PBigInt;
|
Storage for b(k+1) |
BINormalisedMod:array[0..BIGINT_NUM_MODS - 1] of PBigInt;
|
Normalised mod storage |
G:PPBigInt;
|
Used by sliding-window |
Window:Integer;
|
The size of the sliding window |
ActiveCount:Integer;
|
Number of active bigints |
FreeCount:Integer;
|
Number of free bigints |
ModOffset:Byte;
|
The mod offset we are using |
Public variables
None defined
Function declarations
BigInt functions
function BIInitialize:PBigIntContext;
Return | A bigint context |
---|
procedure BITerminate(Context:PBigIntContext);
Context | The bigint session context |
---|
procedure BIPermanent(BI:PBigInt);
BI | The bigint to be made permanent |
---|
procedure BIDepermanent(BI:PBigInt);
BI | The bigint to be made freeable |
---|
procedure BIClearCache(Context:PBigIntContext);
Note | None documented |
---|
procedure BIFree(Context:PBigIntContext; BI:PBigInt);
Context | The bigint session context |
---|---|
BI | The bigint to be freed |
function BICopy(BI:PBigInt):PBigInt;
BI | The bigint to copy |
---|---|
Return | A reference to the same bigint |
function BIClone(Context:PBigIntContext; const BI:TBigInt):PBigInt;
Context | The bigint session context |
---|---|
BI | The bigint object to be copied |
Return | A copy of the bigint object |
procedure BIExport(Context:PBigIntContext; BI:PBigInt; Data:PByte; Size:Integer);
Context | The bigint session context |
---|---|
BI | The bigint to be converted |
Data | The converted data as a byte stream |
Size | The maximum size of the byte stream. Unused bytes will be zeroed |
function BIImport(Context:PBigIntContext; Data:PByte; Size:Integer):PBigInt;
Context | The bigint session context |
---|---|
Data | The data to be converted |
Size | The number of bytes of data |
Return | A bigint representing this data |
function IntToBI(Context:PBigIntContext; I:TComponent):PBigInt;
Context | The bigint session context |
---|---|
I | The (unsigned) integer to be converted |
function BIAdd(Context:PBigIntContext; BIA,BIB:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BIA | A bigint |
BIB | Another bigint |
Return | The result of the addition |
function BISubtract(Context:PBigIntContext; BIA,BIB:PBigInt; var IsNegative:Boolean):PBigInt;
Context | The bigint session context |
---|---|
BIA | A bigint |
BIB | Another bigint |
IsNegative | Indicates that the result was negative |
Return | The result of the subtraction. The result is always positive. |
function BIDivide(Context:PBigIntContext; U,V:PBigInt; IsMod:Boolean):PBigInt;
Context | The bigint session context |
---|---|
U | A bigint which is the numerator |
V | Either the denominator or the modulus depending on the mode |
IsMod | Determines if this is a normal division (False) or a reduction (True) |
Return | The result of the division/reduction |
function BIMultiply(Context:PBigIntContext; BIA,BIB:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BIA | A bigint |
BIB | Another bigint |
Return | The result of the multiplication |
function BIModPower(Context:PBigIntContext; BI,BIExp:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BI | The bigint on which to perform the mod power operation |
BIExp | The bigint exponent |
Return | The result of the mod exponentiation operation |
Note | This function requires BISetMod() to have been called previously. This is one of the optimisations used for performance. |
function BIModPower2(Context:PBigIntContext; BI,BIM,BIExp:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BI | The bigint to perform the exp/mod |
BIM | The temporary modulus |
BIExp | The bigint exponent |
Return | The result of the mod exponentiation operation |
Note | We need this function to check the signatures of certificates. The modulus of this function is temporary as it's just used for authentication. |
function BICompare(BIA,BIB:PBigInt):Integer;
BIA | A bigint |
---|---|
BIB | Another bigint |
Return | -1 if smaller, 1 if larger and 0 if equal |
procedure BISetMod(Context:PBigIntContext; BIM:PBigInt; ModOffset:Integer);
Context | The bigint session context |
---|---|
BIM | The bigint modulus that will be used |
ModOffset | There are three moduluii that can be stored - the standard modulus, and its two primes p and q. This offset refers to which modulus we are referring to. |
Note | This function should only be called once (normally when a session starts)
When the session is over, BIFreeMod() should be called. BIModPower() and BIMod() rely on this function being called. |
procedure BIFreeMod(Context:PBigIntContext; ModOffset:Integer);
Context | The bigint session context |
---|---|
ModOffset | The offset to use |
function BIMod(Context:PBigIntContext; BI:PBigInt):PBigInt; inline;
Note | BISetMod() must be called beforehand |
---|
function BIResidue(Context:PBigIntContext; BI:PBigInt):PBigInt; inline;
Note | None documented |
---|
function BIBarrett(Context:PBigIntContext; BI:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BI | A bigint |
Return | The result of the Barrett reduction |
function BISquare(Context:PBigIntContext; BI:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BI | A bigint |
Return | The result of the multiplication |
function BICRT(Context:PBigIntContext; BI,DP,DQ,P,Q,QInv:PBigInt):PBigInt;
Context | The bigint session context |
---|---|
BI | The bigint to perform the exp/mod |
DP | CRT's dP bigint |
DQ | CRT's dQ bigint |
P | CRT's p bigint |
Q | CRT's q bigint |
QInv | CRT's qInv bigint |
Return | The result of the CRT operation |
BigInt helper functions
function BIToString(BI:PBigInt):String;
BI | The bigint to convert |
---|---|
Return | A string representing the bigint |
function StringToBI(Context:PBigIntContext; const Value:String):PBigInt;
Context | The bigint session context |
---|---|
Value | A string consisting of hex characters |
Return | A bigint representing this data |
Return to Unit Reference