ML-KEM ON AVX2.
The NTT, the inverse NTT, MultiplyNTTs, the polynomial additions and a
4-way Keccak-f[1600] (used to expand A-hat and to sample the noise four
polynomials at a time) have AVX2 bodies in the MLKEMAvx2*.inc files. Each
body is written once against a neutral register assignment and is included
by three hosts:
Delphi Win64, Delphi 11 or later - built-in assembler below. Earlier
dcc64 releases do not know the AVX instruction set.
FPC on System V x86-64 (Linux, macOS, BSD) - FPC assembler below.
Delphi Linux64 - dcclinux64 has no assembler, so the bodies come from
stMLKEMAvx2Func.o, assembled by FPC from stMLKEMAvx2Func.pas and linked
by declaration. The object must be on the object path (-O, DCC_ObjPath)
beside stAESNIFunc.o; see the LINUX64 notes in stAESNI.pas for why
neither $L nor $LINK can be used. That object is a build product of
the .inc files, so an edit to any of them must be followed by a
rebuild of the object with the FPC steps in stMLKEMAvx2Func.pas, or
Delphi Linux64 keeps running the old code.
AVX2 is not part of the x86-64 baseline. Initialization checks the CPU
(AVX, AVX2) and that the operating system saves the YMM state, and the
portable Pascal code runs wherever that check fails. ST4_NO_AVX2 - or the
library-wide ST4_NO_ASM - compiles the AVX2 code out, which also removes
the object from a Linux64 link. All targets produce identical results.inherits from eMLError
(no public members)
inherits from eMLError
(no public members)
inherits from eMLError
(no public members)
inherits from eMLError
(no public members)
Implemented by the key classes below. tDSIML reaches a key's expanded public data through it: the matrix A-hat, the decoded t-hat and H(ek). The expansion is built the first time a key is used and then serves every later encapsulation or decapsulation with that key object. It holds public data only, in a record private to this unit, and lives as long as the key object. Applications have no need to call it.
function GetExpansion: Pointer;
inherits from TInterfacedObject
implements iMLKEMKey, iMLKEMKeyExpansion
destructor Destroy; override;
inherits from tMLKEMKey
implements iMLKEMPublicKey
constructor Create(aKeySize: tMLKEMSize; const aKey: tBytes);
inherits from tMLKEMKey
implements iMLKEMPrivateKey
constructor Create(aKeySize: tMLKEMSize; const aKey : tBytes; const aSeed : iSecretKey = nil);
constructor CreateFromSeed(aKeySize: tMLKEMSize; const aSeed : iSecretKey);
destructor Destroy; override;
The iDSIMLInternal interface is used internally and for testing of internal methods. Use the iDSIML interface for application development.
procedure KEMKeyGenInternal(aKeySize : tMLKEMSize; const aD, aZ: tBytes; var aPublicKey : iMLKEMPublicKey; var aPrivateKey: iMLKEMPrivateKey; aSaveSeed : Boolean = False);
procedure KEMEncapsInternal(const aPublicKey : iMLKEMPublicKey; const aM : tByteArray32d1; var aK : tBytes; var aC : tBytes);
procedure KEMDecapsInternal(const aPrivateKey: iMLKEMPrivateKey; const aC: tBytes; var aK: tBytes);
inherits from eMLError
(no public members)
inherits from eMLError
(no public members)
inherits from eMLError
(no public members)
inherits from eMLError
(no public members)
inherits from TInterfacedObject
implements iMLDSAKey, iSignKey
(no public members)
inherits from tMLDSAKey
implements iMLDSAPublicKey, iSignPublicKey
constructor Create(aKeySize: tMLDSASize; const aEncodedKey: tBytes);
inherits from tMLDSAKey
implements iMLDSAPrivateKey, iSignPrivateKey
constructor Create(aKeySize : tMLDSASize; const aEncodedKey: tBytes; const aPublicKey : tBytes; const aSeed : iSecretKey = nil);
constructor CreateFromSeed(aKeySize: tMLDSASize; const aSeed : iSecretKey);
function SignBuf(const aMsg : tBytes; aMOffset: Integer; aMLen : Integer; var aSign : tBytes; aSOffset: Integer; aSLen : Integer) : Integer; overload;
function SignDig(const aDigest : tBytes; aDOffset: Integer; aDLen : Integer; var aSign : tBytes; aSOffset: Integer; aSLen : Integer; aHashAlg: tHashAlgorithm = haDefault) : Integer; overload;
function SignBuf(const aMsg : tBytes; aMOffset: Integer; aMLen : Integer; var aSign : tBytes) : Integer; overload;
function SignDig(const aDigest : tBytes; aDOffset: Integer; aDLen : Integer; var aSign : tBytes; aHashAlg: tHashAlgorithm = haDefault) : Integer; overload;
function Validate(const aPublicKey: IUnknown): Boolean; overload;
function CheckConsistency: Boolean;
procedure SetAttributes(const aPublicKey: IUnknown);
function GetUseVerboseFormat: Boolean;
destructor Destroy; override;
procedure SetPRNG(const Value: iRawRandom);
function GetPRNG: iRawRandom;
procedure DSAKeyGenInternal(aKeySize : tMLDSASize; const aXi : tBytes; var aPublicKey : iMLDSAPublicKey; var aPrivateKey: iMLDSAPrivateKey; aSaveSeed : Boolean = False);
procedure DSASignInternal(const aPrivateKey: iMLDSAPrivateKey; const aMPrim : tBytes; const aRnd : tBytes; var aSignature : tBytes);
function DSAVerifyInternal(const aPublicKey: iMLDSAPublicKey; const aMPrim : tBytes; const aSignature: tBytes) : Boolean;
procedure DSAKeyGen(aKeySize : tMLDSASize; var aPublicKey : iMLDSAPublicKey; var aPrivateKey: iMLDSAPrivateKey; aSaveSeed : Boolean = False);
function DSASign(const aPrivateKey: iMLDSAPrivateKey; const aMsg : tBytes; const aCtx : tBytes) : tBytes;
function DSAVerify(const aPublicKey: iMLDSAPublicKey; const aMsg : tBytes; const aCtx : tBytes; const aSignature: tBytes) : Boolean;
function HashDSASign(const aPrivateKey: iMLDSAPrivateKey; const aMsg : tBytes; const aCtx : tBytes; const aHashAlg : tHashAlgorithm) : tBytes;
function HashDSAVerify(const aPublicKey: iMLDSAPublicKey; const aMsg : tBytes; const aCtx : tBytes; const aHashAlg : tHashAlgorithm; const aSignature: tBytes) : Boolean;
property PRNG: iRawRandom read GetPRNG write SetPRNG;
inherits from TInterfacedObject
implements iDSIMLKEMInternal, iDSIMLDSAInternal, iDSIML
function NewKEMPrivateKey(aKeySize: tMLKEMSize; const aKey : tBytes; const aSeed : iSecretKey = nil) : iMLKEMPrivateKey;
function NewKEMPrivateKeyFromSeed(aKeySize: tMLKEMSize; const aSeed : iSecretKey) : iMLKEMPrivateKey;
function NewKEMPublicKey(aKeySize: tMLKEMSize; const aKey : tBytes) : iMLKEMPublicKey;
function NewDSAPrivateKey(aKeySize: tMLDSASize; const aKey : tBytes; const aPublKey: tBytes; const aSeed : iSecretKey = nil) : iMLDSAPrivateKey;
function NewDSAPrivateKeyFromSeed(aKeySize: tMLDSASize; const aSeed : iSecretKey) : iMLDSAPrivateKey;
function NewDSAPublicKey(aKeySize: tMLDSASize; const aKey : tBytes) : iMLDSAPublicKey;
procedure KEMKeyGen(aKeySize : tMLKEMSize; var aPublicKey : iMLKEMPublicKey; var aPrivateKey: iMLKEMPrivateKey; aSaveSeed : Boolean = False);
procedure KEMEncaps(const aPublicKey: iMLKEMPublicKey; var aK : tBytes; var aC : tBytes);
procedure KEMDecaps(const aPrivateKey: iMLKEMPrivateKey; const aC: tBytes; var aK: tBytes);
procedure DSAKeyGen(aKeySize : tMLDSASize; var aPublicKey : iMLDSAPublicKey; var aPrivateKey: iMLDSAPrivateKey; aSaveSeed : Boolean = False);
function DSASign(const aPrivateKey: iMLDSAPrivateKey; const aMsg : tBytes; const aCtx : tBytes) : tBytes;
function DSAVerify(const aPublicKey: iMLDSAPublicKey; const aMsg : tBytes; const aCtx : tBytes; const aSignature: tBytes) : Boolean;
function HashDSASign(const aPrivateKey: iMLDSAPrivateKey; const aMsg : tBytes; const aCtx : tBytes; const aHashAlg : tHashAlgorithm) : tBytes;
function HashDSASignDigest(const aPrivateKey: iMLDSAPrivateKey; const aDigest : tBytes; const aCtx : tBytes; const aHashAlg : tHashAlgorithm) : tBytes;
function HashDSAVerify(const aPublicKey: iMLDSAPublicKey; const aMsg : tBytes; const aCtx : tBytes; const aHashAlg : tHashAlgorithm; const aSignature: tBytes) : Boolean;
function HashDSAVerifyDigest(const aPublicKey: iMLDSAPublicKey; const aDigest : tBytes; const aCtx : tBytes; const aHashAlg : tHashAlgorithm; const aSignature: tBytes) : Boolean;
property PRNG: iRawRandom read GetPRNG write SetPRNG;
This class is for internal consumption only. It contains internal functions and constants used by the ML-KEM implementation.
class function Compress(d, x: Integer): Integer; static; inline;
class function Decompress(d, x: Integer): Integer; static; inline;
class function BarrettReduce(a: UInt32): UInt32; static; inline;
class function DivQ(n: UInt32): UInt32; static; inline;
class function CompressCT(d, x: Integer): Integer; static; inline;
class procedure BaseCaseMultiply(a0, a1, b0, b1, gamma: Integer; var c0, c1: Integer); static; inline;
class function NTT(const aF: tIntegerArray): tIntegerArray; static;
class function NTTInv(const aF: tIntegerArray): tIntegerArray; static;
class procedure PolyAddMod(var aR: tIntegerArray; const aA: tIntegerArray); static;
class procedure PolySubMod(var aR: tIntegerArray; const aA: tIntegerArray); static;
class function KeySizeToK(aKeySize: tMLKEMSize): Integer; static;
class function GenerateSamplePolyCBD2(aEta: Integer; const aSigma: tBytes; var aN: Integer): tIntegerArray; static;
class function GenerateNTT(const aR: tSample; aK: Integer): tSample; static;
class function SampleNTT(const aB: iRawRandomEx): tIntegerArray; static;
class function SamplePolyCBD2(const aB: iRawRandomEx): tIntegerArray; static;
class function SamplePolyCBD3(const aB: iRawRandomEx): tIntegerArray; static;
class procedure ByteEncode1(const aF: tIntegerArray; var aB: tByteArray32d1); static;
class procedure ByteEncode4(const aF: tIntegerArray; var aB: tByteArray32d4); static;
class procedure ByteEncode5(const aF: tIntegerArray; var aB: tByteArray32d5); static;
class procedure ByteEncode10(const aF: tIntegerArray; var aB: tByteArray32d10); static;
class procedure ByteEncode11(const aF: tIntegerArray; var aB: tByteArray32d11); static;
class procedure ByteEncode12(const aF: tIntegerArray; var aB: tByteArray32d12); static;
class procedure ByteDecode1(var aF: tIntegerArray; const aB: tByteArray32d1); static;
class procedure ByteDecode4(var aF: tIntegerArray; const aB: tByteArray32d4); static;
class procedure ByteDecode5(var aF: tIntegerArray; const aB: tByteArray32d5); static;
class procedure ByteDecode10(var aF: tIntegerArray; const aB: tByteArray32d10); static;
class procedure ByteDecode11(var aF: tIntegerArray; const aB: tByteArray32d11); static;
class procedure ByteDecode12(var aF: tIntegerArray; const aB: tByteArray32d12); static;
class function MultiplyNTTs(const aF,aG: tIntegerArray): tIntegerArray; static;
class function GenerateMatrixAHat(const aRho: tBytes; aK: Integer): tAHat; static;
class function GenerateSamplePolyCBD(const aSigma: tBytes; aK, aEta: Integer; var aN: Integer): tSample; static;
class procedure SetUseVerboseFormat(Value: Boolean);
class function GetUseVerboseFormat: Boolean;
class function Avx2Available: Boolean; static;
class function GetUseAvx2: Boolean; static;
class procedure SetUseAvx2(Value: Boolean); static;
This class is for internal consumption only. It contains internal functions and constants used by the ML-DSA implementation.
class procedure CoeffFromThreeBytes(b0, b1, b2: UInt8; var aResult: DsaInt; var aIndex: Integer); static; inline;
class procedure CoeffFromHalfByteEta2(b: UInt8; var aResult: DsaInt; var aIndex: Integer); static; inline;
class procedure CoeffFromHalfByteEta4(b: UInt8; var aResult: DsaInt; var aIndex: Integer); static; inline;
class procedure SimpleBitPack(const w: tPolynomial; bitlen: Integer; var aResult); static; inline;
class procedure BitPack(const w: tPolynomial; bitlen: Integer; b: UInt64; var aResult); static; inline;
class procedure SimpleBitUnpack(v: pByte; bitlen: Integer; var w: tPolynomial); static; inline;
class procedure BitUnpack(v: pByte; bitlen: Integer; b: UInt64; var w: tPolynomial); static; inline;
class procedure HintBitPackOmega80k4(const h: tBinaryPolynomialVectork4; var y); static;
class procedure HintBitPackOmega55k6(const h: tBinaryPolynomialVectork6; var y); static;
class procedure HintBitPackOmega75k8(const h: tBinaryPolynomialVectork8; var y); static;
class function HintBitUnpackOmega80k4(const y; var h: tBinaryPolynomialVectork4): Boolean; static;
class function HintBitUnpackOmega55k6(const y; var h: tBinaryPolynomialVectork6): Boolean; static;
class function HintBitUnpackOmega75k8(const y; var h: tBinaryPolynomialVectork8): Boolean; static;
class function pkEncodek4(const rho: tBytes; t1: tPolynomialVectork4): tBytes; static;
class function pkEncodek6(const rho: tBytes; t1: tPolynomialVectork6): tBytes; static;
class function pkEncodek8(const rho: tBytes; t1: tPolynomialVectork8): tBytes; static;
class function pkDecodek4(const pk: tBytes; var rho: tBytes; var t1: tPolynomialVectork4): Boolean; static;
class function pkDecodek6(const pk: tBytes; var rho: tBytes; var t1: tPolynomialVectork6): Boolean; static;
class function pkDecodek8(const pk: tBytes; var rho: tBytes; var t1: tPolynomialVectork8): Boolean; static;
class function skEncodek4l4eta2(const rho, kseed, tr: tBytes; const s1: tPolynomialVectorl4; const s2, t0: tPolynomialVectork4): tBytes; static;
class function skEncodek6l5eta4(const rho, kseed, tr: tBytes; const s1: tPolynomialVectorl5; const s2, t0: tPolynomialVectork6): tBytes; static;
class function skEncodek8l7eta2(const rho, kseed, tr: tBytes; const s1: tPolynomialVectorl7; const s2, t0: tPolynomialVectork8): tBytes; static;
class procedure skDecodek4l4eta2(const sk: tBytes; var rho, kseed, tr: tBytes; var s1: tPolynomialVectorl4; var s2, t0: tPolynomialVectork4); static;
class procedure skDecodek6l5eta4(const sk: tBytes; var rho, kseed, tr: tBytes; var s1: tPolynomialVectorl5; var s2, t0: tPolynomialVectork6); static;
class procedure skDecodek8l7eta2(const sk: tBytes; var rho, kseed, tr: tBytes; var s1: tPolynomialVectorl7; var s2, t0: tPolynomialVectork8); static;
class function sigEncode128(const ctilde: tBytes; const z: tPolynomialVectorl4; const h: tBinaryPolynomialVectork4): tBytes; static;
class function sigEncode192(const ctilde: tBytes; const z: tPolynomialVectorl5; const h: tBinaryPolynomialVectork6): tBytes; static;
class function sigEncode256(const ctilde: tBytes; const z: tPolynomialVectorl7; const h: tBinaryPolynomialVectork8): tBytes; static;
class function sigDecode128(const sigma: tBytes; var ctilde: tBytes; var z: tPolynomialVectorl4; var h: tBinaryPolynomialVectork4): Boolean; static;
class function sigDecode192(const sigma: tBytes; var ctilde: tBytes; var z: tPolynomialVectorl5; var h: tBinaryPolynomialVectork6): Boolean; static;
class function sigDecode256(const sigma: tBytes; var ctilde: tBytes; var z: tPolynomialVectorl7; var h: tBinaryPolynomialVectork8): Boolean; static;
class function w1Encodek4(const w1: tPolynomialVectork4): tBytes; static;
class function w1Encodek6(const w1: tPolynomialVectork6): tBytes; static;
class function w1Encodek8(const w1: tPolynomialVectork8): tBytes; static;
class procedure SampleInBalltau39(const rho: tBytes; var c: tPolynomial); static;
class procedure SampleInBalltau49(const rho: tBytes; var c: tPolynomial); static;
class procedure SampleInBalltau60(const rho: tBytes; var c: tPolynomial); static;
class procedure RejNTTPoly(const rho: tBytes; var ahat: tPolynomial); static;
class procedure RejBoundedPolyEta2(const rho: tBytes; var a: tPolynomial); static;
class procedure RejBoundedPolyEta4(const rho: tBytes; var a: tPolynomial); static;
class procedure ExpandAk4l4(const rho: tBytes; var aHat: tPolynomialMatrixk4l4); static;
class procedure ExpandAk6l5(const rho: tBytes; var aHat: tPolynomialMatrixk6l5); static;
class procedure ExpandAk8l7(const rho: tBytes; var aHat: tPolynomialMatrixk8l7); static;
class procedure ExpandSk4l4(const rho: tBytes; var s1: tPolynomialVectorl4; var s2: tPolynomialVectork4); static;
class procedure ExpandSk6l5(const rho: tBytes; var s1: tPolynomialVectorl5; var s2: tPolynomialVectork6); static;
class procedure ExpandSk8l7(const rho: tBytes; var s1: tPolynomialVectorl7; var s2: tPolynomialVectork8); static;
class function H(const aSeed: tBytes; aLength: Integer): tBytes; static;
class procedure ExpandMaskl4(const rho: tBytes; micro: Cardinal; var y: tPolynomialVectorl4); static;
class procedure ExpandMaskl5(const rho: tBytes; micro: Cardinal; var y: tPolynomialVectorl5); static;
class procedure ExpandMaskl7(const rho: tBytes; micro: Cardinal; var y: tPolynomialVectorl7); static;
class procedure Power2Roundk4(const r: tPolynomialVectork4; var r1, r0: tPolynomialVectork4); static;
class procedure Power2Roundk6(const r: tPolynomialVectork6; var r1, r0: tPolynomialVectork6); static;
class procedure Power2Roundk8(const r: tPolynomialVectork8; var r1, r0: tPolynomialVectork8); static;
class procedure Decomposek4(const r: tPolynomialVectork4; var r1, r0: tPolynomialVectork4); static;
class procedure Decomposek6(const r: tPolynomialVectork6; var r1, r0: tPolynomialVectork6); static;
class procedure Decomposek8(const r: tPolynomialVectork8; var r1, r0: tPolynomialVectork8); static;
class procedure HighBitsk4(const r: tPolynomialVectork4; var r1: tPolynomialVectork4); static;
class procedure HighBitsk6(const r: tPolynomialVectork6; var r1: tPolynomialVectork6); static;
class procedure HighBitsk8(const r: tPolynomialVectork8; var r1: tPolynomialVectork8); static;
class procedure LowBitsk4(const r: tPolynomialVectork4; var r0: tPolynomialVectork4); static;
class procedure LowBitsk6(const r: tPolynomialVectork6; var r0: tPolynomialVectork6); static;
class procedure LowBitsk8(const r: tPolynomialVectork8; var r0: tPolynomialVectork8); static;
class procedure MakeHintk4(const z, r: tPolynomialVectork4; var h: tBinaryPolynomialVectork4); static;
class procedure MakeHintk6(const z, r: tPolynomialVectork6; var h: tBinaryPolynomialVectork6); static;
class procedure MakeHintk8(const z, r: tPolynomialVectork8; var h: tBinaryPolynomialVectork8); static;
class procedure UseHintk4(const h: tBinaryPolynomialVectork4; const r: tPolynomialVectork4; var r1: tPolynomialVectork4); static;
class procedure UseHintk6(const h: tBinaryPolynomialVectork6; const r: tPolynomialVectork6; var r1: tPolynomialVectork6); static;
class procedure UseHintk8(const h: tBinaryPolynomialVectork8; const r: tPolynomialVectork8; var r1: tPolynomialVectork8); static;
class procedure NTT(const w: tPolynomial; var what: tPolynomial); static;
class procedure NTTInv(const what: tPolynomial; var w: tPolynomial); static;
class procedure AddNTT(const ahat, bhat: tPolynomial; var chat: tPolynomial); static;
class procedure SubNTT(const ahat, bhat: tPolynomial; var chat: tPolynomial); static;
class procedure MultiplyNTT(const ahat, bhat: tPolynomial; var chat: tPolynomial); static;
class procedure AddVectorNTTl4(const vhat, what: tPolynomialVectorl4; var uhat: tPolynomialVectorl4); static;
class procedure AddVectorNTTl5(const vhat, what: tPolynomialVectorl5; var uhat: tPolynomialVectorl5); static;
class procedure AddVectorNTTl7(const vhat, what: tPolynomialVectorl7; var uhat: tPolynomialVectorl7); static;
class procedure SubVectorNTTk4(const vhat, what: tPolynomialVectork4; var uhat: tPolynomialVectork4); static;
class procedure SubVectorNTTk6(const vhat, what: tPolynomialVectork6; var uhat: tPolynomialVectork6); static;
class procedure SubVectorNTTk8(const vhat, what: tPolynomialVectork8; var uhat: tPolynomialVectork8); static;
class procedure ScalarVectorNTTl4(const chat: tPolynomial; const vhat: tPolynomialVectorl4; var what: tPolynomialVectorl4); static;
class procedure ScalarVectorNTTl5(const chat: tPolynomial; const vhat: tPolynomialVectorl5; var what: tPolynomialVectorl5); static;
class procedure ScalarVectorNTTl7(const chat: tPolynomial; const vhat: tPolynomialVectorl7; var what: tPolynomialVectorl7); static;
class procedure ScalarVectorNTTk4(const chat: tPolynomial; const vhat: tPolynomialVectork4; var what: tPolynomialVectork4); static;
class procedure ScalarVectorNTTk6(const chat: tPolynomial; const vhat: tPolynomialVectork6; var what: tPolynomialVectork6); static;
class procedure ScalarVectorNTTk8(const chat: tPolynomial; const vhat: tPolynomialVectork8; var what: tPolynomialVectork8); static;
class procedure MatrixVectorNTTk4l4(const Mhat: tPolynomialMatrixk4l4; const vhat: tPolynomialVectorl4; var what: tPolynomialVectork4); static;
class procedure MatrixVectorNTTk6l5(const Mhat: tPolynomialMatrixk6l5; const vhat: tPolynomialVectorl5; var what: tPolynomialVectork6); static;
class procedure MatrixVectorNTTk8l7(const Mhat: tPolynomialMatrixk8l7; const vhat: tPolynomialVectorl7; var what: tPolynomialVectork8); static;
class function InfinityNorm(const w: tPolynomial): Int64;
class function MontReduce(const a: Int64): UInt32; static;
class function BarrettReduce(const a: Int64): Int64; static; inline;
class function modu(const v, m: Int64): Int64; static;
class function moda(const v, m, a: Int64): Int64; static;
class procedure SetUseVerboseFormat(Value: Boolean);
class function GetUseVerboseFormat: Boolean;