12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781 |
- { *********************************************************************************** }
- { * CryptoLib Library * }
- { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
- { * Github Repository <https://github.com/Xor-el> * }
- { * Distributed under the MIT software license, see the accompanying file LICENSE * }
- { * or visit http://www.opensource.org/licenses/mit-license.php. * }
- { * Acknowledgements: * }
- { * * }
- { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
- { * development of this library * }
- { * ******************************************************************************* * }
- (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
- unit ClpNat256;
- {$I CryptoLib.inc}
- interface
- uses
- ClpNat,
- ClpConverters,
- ClpBits,
- ClpBigInteger,
- ClpArrayUtils,
- ClpCryptoLibTypes;
- type
- TNat256 = class abstract(TObject)
- strict private
- const
- M = UInt64($FFFFFFFF);
- public
- class function Add(const x, y, z: TCryptoLibUInt32Array): UInt32;
- overload; static;
- class function Add(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32; overload; static;
- class function AddBothTo(const x, y, z: TCryptoLibUInt32Array): UInt32;
- overload; static;
- class function AddBothTo(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32; overload; static;
- class function AddTo(const x, z: TCryptoLibUInt32Array): UInt32;
- overload; static;
- class function AddTo(const x: TCryptoLibUInt32Array; xOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32; cIn: UInt32): UInt32;
- overload; static;
- class function AddToEachOther(const u: TCryptoLibUInt32Array; uOff: Int32;
- const v: TCryptoLibUInt32Array; vOff: Int32): UInt32; static;
- class procedure Copy(const x, z: TCryptoLibUInt32Array); overload;
- static; inline;
- class procedure Copy(const x: TCryptoLibUInt32Array; xOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32); overload; static; inline;
- class procedure Copy64(const x, z: TCryptoLibUInt64Array); overload;
- static; inline;
- class procedure Copy64(const x: TCryptoLibUInt64Array; xOff: Int32;
- const z: TCryptoLibUInt64Array; zOff: Int32); overload; static; inline;
- class function Create(): TCryptoLibUInt32Array; static; inline;
- class function Create64(): TCryptoLibUInt64Array; static; inline;
- class function CreateExt(): TCryptoLibUInt32Array; static; inline;
- class function CreateExt64(): TCryptoLibUInt64Array; static; inline;
- class function Diff(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): Boolean; static; inline;
- class function Eq(const x, y: TCryptoLibUInt32Array): Boolean; static;
- class function Eq64(const x, y: TCryptoLibUInt64Array): Boolean; static;
- class function FromBigInteger(const x: TBigInteger)
- : TCryptoLibUInt32Array; static;
- class function FromBigInteger64(const x: TBigInteger)
- : TCryptoLibUInt64Array; static;
- class function GetBit(const x: TCryptoLibUInt32Array; bit: Int32): UInt32;
- static; inline;
- class function Gte(const x, y: TCryptoLibUInt32Array): Boolean;
- overload; static;
- class function Gte(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32): Boolean; overload; static;
- class function IsOne(const x: TCryptoLibUInt32Array): Boolean; static;
- class function IsOne64(const x: TCryptoLibUInt64Array): Boolean; static;
- class function IsZero(const x: TCryptoLibUInt32Array): Boolean; static;
- class function IsZero64(const x: TCryptoLibUInt64Array): Boolean; static;
- class procedure Mul(const x, y, zz: TCryptoLibUInt32Array);
- overload; static;
- class procedure Mul(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32;
- const zz: TCryptoLibUInt32Array; zzOff: Int32); overload; static;
- class function MulAddTo(const x, y, zz: TCryptoLibUInt32Array): UInt32;
- overload; static;
- class function MulAddTo(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32;
- const zz: TCryptoLibUInt32Array; zzOff: Int32): UInt32; overload; static;
- class function Mul33Add(w: UInt32; const x: TCryptoLibUInt32Array;
- xOff: Int32; const y: TCryptoLibUInt32Array; yOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt64; static;
- class function MulByWord(x: UInt32; const z: TCryptoLibUInt32Array)
- : UInt32; static;
- class function MulByWordAddTo(x: UInt32; const y, z: TCryptoLibUInt32Array)
- : UInt32; static;
- class function MulWordAddTo(x: UInt32; const y: TCryptoLibUInt32Array;
- yOff: Int32; const z: TCryptoLibUInt32Array; zOff: Int32): UInt32; static;
- class function Mul33DWordAdd(x: UInt32; y: UInt64;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32; static;
- class function Mul33WordAdd(x, y: UInt32; const z: TCryptoLibUInt32Array;
- zOff: Int32): UInt32; static;
- class function MulWordDwordAdd(x: UInt32; y: UInt64;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32; static;
- class function MulWord(x: UInt32; const y, z: TCryptoLibUInt32Array;
- zOff: Int32): UInt32; static;
- class procedure Square(const x, zz: TCryptoLibUInt32Array);
- overload; static;
- class procedure Square(const x: TCryptoLibUInt32Array; xOff: Int32;
- const zz: TCryptoLibUInt32Array; zzOff: Int32); overload; static;
- class function Sub(const x, y, z: TCryptoLibUInt32Array): Int32;
- overload; static;
- class function Sub(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): Int32; overload; static;
- class function SubBothFrom(const x, y, z: TCryptoLibUInt32Array)
- : Int32; static;
- class function SubFrom(const x, z: TCryptoLibUInt32Array): Int32;
- overload; static;
- class function SubFrom(const x: TCryptoLibUInt32Array; xOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): Int32; overload; static;
- class function ToBigInteger(const x: TCryptoLibUInt32Array)
- : TBigInteger; static;
- class function ToBigInteger64(const x: TCryptoLibUInt64Array)
- : TBigInteger; static;
- class procedure Zero(const z: TCryptoLibUInt32Array); static; inline;
- end;
- implementation
- { TNat256 }
- class function TNat256.Add(const x, y, z: TCryptoLibUInt32Array): UInt32;
- var
- c: UInt64;
- begin
- c := 0;
- c := c + (UInt64(x[0]) + y[0]);
- z[0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[1]) + y[1]);
- z[1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[2]) + y[2]);
- z[2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[3]) + y[3]);
- z[3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[4]) + y[4]);
- z[4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[5]) + y[5]);
- z[5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[6]) + y[6]);
- z[6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[7]) + y[7]);
- z[7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.Add(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32; const z: TCryptoLibUInt32Array;
- zOff: Int32): UInt32;
- var
- c: UInt64;
- begin
- c := 0;
- c := c + (UInt64(x[xOff + 0]) + y[yOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 1]) + y[yOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 2]) + y[yOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 3]) + y[yOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 4]) + y[yOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 5]) + y[yOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 6]) + y[yOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 7]) + y[yOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.AddBothTo(const x, y, z: TCryptoLibUInt32Array): UInt32;
- var
- c: UInt64;
- begin
- c := 0;
- c := c + (UInt64(x[0]) + y[0] + z[0]);
- z[0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[1]) + y[1] + z[1]);
- z[1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[2]) + y[2] + z[2]);
- z[2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[3]) + y[3] + z[3]);
- z[3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[4]) + y[4] + z[4]);
- z[4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[5]) + y[5] + z[5]);
- z[5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[6]) + y[6] + z[6]);
- z[6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[7]) + y[7] + z[7]);
- z[7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.AddBothTo(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32; const z: TCryptoLibUInt32Array;
- zOff: Int32): UInt32;
- var
- c: UInt64;
- begin
- c := 0;
- c := c + (UInt64(x[xOff + 0]) + y[yOff + 0] + z[zOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 1]) + y[yOff + 1] + z[zOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 2]) + y[yOff + 2] + z[zOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 3]) + y[yOff + 3] + z[zOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 4]) + y[yOff + 4] + z[zOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 5]) + y[yOff + 5] + z[zOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 6]) + y[yOff + 6] + z[zOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 7]) + y[yOff + 7] + z[zOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.AddTo(const x, z: TCryptoLibUInt32Array): UInt32;
- var
- c: UInt64;
- begin
- c := 0;
- c := c + (UInt64(x[0]) + z[0]);
- z[0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[1]) + z[1]);
- z[1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[2]) + z[2]);
- z[2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[3]) + z[3]);
- z[3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[4]) + z[4]);
- z[4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[5]) + z[5]);
- z[5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[6]) + z[6]);
- z[6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[7]) + z[7]);
- z[7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.AddTo(const x: TCryptoLibUInt32Array; xOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32; cIn: UInt32): UInt32;
- var
- c: UInt64;
- begin
- c := cIn;
- c := c + (UInt64(x[xOff + 0]) + z[zOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 1]) + z[zOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 2]) + z[zOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 3]) + z[zOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 4]) + z[zOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 5]) + z[zOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 6]) + z[zOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(x[xOff + 7]) + z[zOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.AddToEachOther(const u: TCryptoLibUInt32Array;
- uOff: Int32; const v: TCryptoLibUInt32Array; vOff: Int32): UInt32;
- var
- c: UInt64;
- begin
- c := 0;
- c := c + (UInt64(u[uOff + 0]) + v[vOff + 0]);
- u[uOff + 0] := UInt32(c);
- v[vOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 1]) + v[vOff + 1]);
- u[uOff + 1] := UInt32(c);
- v[vOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 2]) + v[vOff + 2]);
- u[uOff + 2] := UInt32(c);
- v[vOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 3]) + v[vOff + 3]);
- u[uOff + 3] := UInt32(c);
- v[vOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 4]) + v[vOff + 4]);
- u[uOff + 4] := UInt32(c);
- v[vOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 5]) + v[vOff + 5]);
- u[uOff + 5] := UInt32(c);
- v[vOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 6]) + v[vOff + 6]);
- u[uOff + 6] := UInt32(c);
- v[vOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (UInt64(u[uOff + 7]) + v[vOff + 7]);
- u[uOff + 7] := UInt32(c);
- v[vOff + 7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class procedure TNat256.Copy(const x, z: TCryptoLibUInt32Array);
- begin
- System.Move(x[0], z[0], 8 * System.SizeOf(UInt32));
- end;
- class procedure TNat256.Copy(const x: TCryptoLibUInt32Array; xOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32);
- begin
- System.Move(x[xOff], z[zOff], 8 * System.SizeOf(UInt32));
- end;
- class procedure TNat256.Copy64(const x, z: TCryptoLibUInt64Array);
- begin
- System.Move(x[0], z[0], 4 * System.SizeOf(UInt64));
- end;
- class procedure TNat256.Copy64(const x: TCryptoLibUInt64Array; xOff: Int32;
- const z: TCryptoLibUInt64Array; zOff: Int32);
- begin
- System.Move(x[xOff], z[zOff], 4 * System.SizeOf(UInt64));
- end;
- class function TNat256.Create: TCryptoLibUInt32Array;
- begin
- System.SetLength(result, 8);
- end;
- class function TNat256.Create64: TCryptoLibUInt64Array;
- begin
- System.SetLength(result, 4);
- end;
- class function TNat256.CreateExt: TCryptoLibUInt32Array;
- begin
- System.SetLength(result, 16);
- end;
- class function TNat256.CreateExt64: TCryptoLibUInt64Array;
- begin
- System.SetLength(result, 8);
- end;
- class function TNat256.Diff(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32; const z: TCryptoLibUInt32Array;
- zOff: Int32): Boolean;
- var
- pos: Boolean;
- begin
- pos := Gte(x, xOff, y, yOff);
- if (pos) then
- begin
- Sub(x, xOff, y, yOff, z, zOff);
- end
- else
- begin
- Sub(y, yOff, x, xOff, z, zOff);
- end;
- result := pos;
- end;
- class function TNat256.Eq(const x, y: TCryptoLibUInt32Array): Boolean;
- var
- i: Int32;
- begin
- i := 7;
- while i >= 0 do
- begin
- if (x[i] <> y[i]) then
- begin
- result := false;
- Exit;
- end;
- System.Dec(i);
- end;
- result := true;
- end;
- class function TNat256.Eq64(const x, y: TCryptoLibUInt64Array): Boolean;
- var
- i: Int32;
- begin
- i := 3;
- while i >= 0 do
- begin
- if (x[i] <> y[i]) then
- begin
- result := false;
- Exit;
- end;
- System.Dec(i);
- end;
- result := true;
- end;
- class function TNat256.FromBigInteger(const x: TBigInteger)
- : TCryptoLibUInt32Array;
- var
- i: Int32;
- Lx: TBigInteger;
- begin
- Lx := x;
- if ((Lx.SignValue < 0) or (Lx.BitLength > 256)) then
- begin
- raise EArgumentCryptoLibException.Create('');
- end;
- result := Create();
- i := 0;
- while (Lx.SignValue <> 0) do
- begin
- result[i] := UInt32(Lx.Int32Value);
- System.Inc(i);
- Lx := Lx.ShiftRight(32);
- end;
- end;
- class function TNat256.FromBigInteger64(const x: TBigInteger)
- : TCryptoLibUInt64Array;
- var
- i: Int32;
- Lx: TBigInteger;
- begin
- Lx := x;
- if ((Lx.SignValue < 0) or (Lx.BitLength > 256)) then
- begin
- raise EArgumentCryptoLibException.Create('');
- end;
- result := Create64();
- i := 0;
- while (Lx.SignValue <> 0) do
- begin
- result[i] := UInt64(Lx.Int64Value);
- System.Inc(i);
- Lx := Lx.ShiftRight(64);
- end;
- end;
- class function TNat256.GetBit(const x: TCryptoLibUInt32Array;
- bit: Int32): UInt32;
- var
- w, b: Int32;
- begin
- if (bit = 0) then
- begin
- result := x[0] and 1;
- Exit;
- end;
- if ((bit and 255) <> bit) then
- begin
- result := 0;
- Exit;
- end;
- w := TBits.Asr32(bit, 5);
- b := bit and 31;
- result := (x[w] shr b) and 1;
- end;
- class function TNat256.Gte(const x, y: TCryptoLibUInt32Array): Boolean;
- var
- i: Int32;
- x_i, y_i: UInt32;
- begin
- i := 7;
- while i >= 0 do
- begin
- x_i := x[i];
- y_i := y[i];
- if (x_i < y_i) then
- begin
- result := false;
- Exit;
- end;
- if (x_i > y_i) then
- begin
- result := true;
- Exit;
- end;
- System.Dec(i);
- end;
- result := true;
- end;
- class function TNat256.Gte(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32): Boolean;
- var
- i: Int32;
- x_i, y_i: UInt32;
- begin
- i := 7;
- while i >= 0 do
- begin
- x_i := x[xOff + i];
- y_i := y[yOff + i];
- if (x_i < y_i) then
- begin
- result := false;
- Exit;
- end;
- if (x_i > y_i) then
- begin
- result := true;
- Exit;
- end;
- System.Dec(i);
- end;
- result := true;
- end;
- class function TNat256.IsOne(const x: TCryptoLibUInt32Array): Boolean;
- var
- i: Int32;
- begin
- if (x[0] <> 1) then
- begin
- result := false;
- Exit;
- end;
- i := 1;
- while i < 8 do
- begin
- if (x[i] <> 0) then
- begin
- result := false;
- Exit;
- end;
- System.Inc(i);
- end;
- result := true;
- end;
- class function TNat256.IsOne64(const x: TCryptoLibUInt64Array): Boolean;
- var
- i: Int32;
- begin
- if (x[0] <> UInt64(1)) then
- begin
- result := false;
- Exit;
- end;
- i := 1;
- while i < 4 do
- begin
- if (x[i] <> UInt64(0)) then
- begin
- result := false;
- Exit;
- end;
- System.Inc(i);
- end;
- result := true;
- end;
- class function TNat256.IsZero(const x: TCryptoLibUInt32Array): Boolean;
- var
- i: Int32;
- begin
- i := 0;
- while i < 8 do
- begin
- if (x[i] <> 0) then
- begin
- result := false;
- Exit;
- end;
- System.Inc(i);
- end;
- result := true;
- end;
- class function TNat256.IsZero64(const x: TCryptoLibUInt64Array): Boolean;
- var
- i: Int32;
- begin
- i := 0;
- while i < 4 do
- begin
- if (x[i] <> UInt64(0)) then
- begin
- result := false;
- Exit;
- end;
- System.Inc(i);
- end;
- result := true;
- end;
- class procedure TNat256.Mul(const x, y, zz: TCryptoLibUInt32Array);
- var
- c, x_0, x_i, y_0, y_1, y_2, y_3, y_4, y_5, y_6, y_7: UInt64;
- i: Int32;
- begin
- y_0 := y[0];
- y_1 := y[1];
- y_2 := y[2];
- y_3 := y[3];
- y_4 := y[4];
- y_5 := y[5];
- y_6 := y[6];
- y_7 := y[7];
- c := 0;
- x_0 := x[0];
- c := c + (x_0 * y_0);
- zz[0] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_1);
- zz[1] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_2);
- zz[2] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_3);
- zz[3] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_4);
- zz[4] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_5);
- zz[5] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_6);
- zz[6] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_7);
- zz[7] := UInt32(c);
- c := c shr 32;
- zz[8] := UInt32(c);
- for i := 1 to System.Pred(8) do
- begin
- c := 0;
- x_i := x[i];
- c := c + (x_i * y_0 + zz[i + 0]);
- zz[i + 0] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_1 + zz[i + 1]);
- zz[i + 1] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_2 + zz[i + 2]);
- zz[i + 2] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_3 + zz[i + 3]);
- zz[i + 3] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_4 + zz[i + 4]);
- zz[i + 4] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_5 + zz[i + 5]);
- zz[i + 5] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_6 + zz[i + 6]);
- zz[i + 6] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_7 + zz[i + 7]);
- zz[i + 7] := UInt32(c);
- c := c shr 32;
- zz[i + 8] := UInt32(c);
- end;
- end;
- class procedure TNat256.Mul(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32; const zz: TCryptoLibUInt32Array;
- zzOff: Int32);
- var
- c, x_0, x_i, y_0, y_1, y_2, y_3, y_4, y_5, y_6, y_7: UInt64;
- i: Int32;
- begin
- y_0 := y[yOff + 0];
- y_1 := y[yOff + 1];
- y_2 := y[yOff + 2];
- y_3 := y[yOff + 3];
- y_4 := y[yOff + 4];
- y_5 := y[yOff + 5];
- y_6 := y[yOff + 6];
- y_7 := y[yOff + 7];
- c := 0;
- x_0 := x[xOff + 0];
- c := c + (x_0 * y_0);
- zz[zzOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_1);
- zz[zzOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_2);
- zz[zzOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_3);
- zz[zzOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_4);
- zz[zzOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_5);
- zz[zzOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_6);
- zz[zzOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (x_0 * y_7);
- zz[zzOff + 7] := UInt32(c);
- c := c shr 32;
- zz[zzOff + 8] := UInt32(c);
- for i := 1 to System.Pred(8) do
- begin
- System.Inc(zzOff);
- c := 0;
- x_i := x[xOff + i];
- c := c + (x_i * y_0 + zz[zzOff + 0]);
- zz[zzOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_1 + zz[zzOff + 1]);
- zz[zzOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_2 + zz[zzOff + 2]);
- zz[zzOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_3 + zz[zzOff + 3]);
- zz[zzOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_4 + zz[zzOff + 4]);
- zz[zzOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_5 + zz[zzOff + 5]);
- zz[zzOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_6 + zz[zzOff + 6]);
- zz[zzOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_7 + zz[zzOff + 7]);
- zz[zzOff + 7] := UInt32(c);
- c := c shr 32;
- zz[zzOff + 8] := UInt32(c);
- end;
- end;
- class function TNat256.MulAddTo(const x, y, zz: TCryptoLibUInt32Array): UInt32;
- var
- c, x_i, y_0, y_1, y_2, y_3, y_4, y_5, y_6, y_7, zc: UInt64;
- i: Int32;
- begin
- y_0 := y[0];
- y_1 := y[1];
- y_2 := y[2];
- y_3 := y[3];
- y_4 := y[4];
- y_5 := y[5];
- y_6 := y[6];
- y_7 := y[7];
- zc := 0;
- for i := 0 to System.Pred(8) do
- begin
- c := 0;
- x_i := x[i];
- c := c + (x_i * y_0 + zz[i + 0]);
- zz[i + 0] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_1 + zz[i + 1]);
- zz[i + 1] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_2 + zz[i + 2]);
- zz[i + 2] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_3 + zz[i + 3]);
- zz[i + 3] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_4 + zz[i + 4]);
- zz[i + 4] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_5 + zz[i + 5]);
- zz[i + 5] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_6 + zz[i + 6]);
- zz[i + 6] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_7 + zz[i + 7]);
- zz[i + 7] := UInt32(c);
- c := c shr 32;
- zc := zc + (c + (zz[i + 8] and M));
- zz[i + 8] := UInt32(zc);
- zc := zc shr 32;
- end;
- result := UInt32(zc);
- end;
- class function TNat256.MulAddTo(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32; const zz: TCryptoLibUInt32Array;
- zzOff: Int32): UInt32;
- var
- c, x_i, y_0, y_1, y_2, y_3, y_4, y_5, y_6, y_7, zc: UInt64;
- i: Int32;
- begin
- y_0 := y[yOff + 0];
- y_1 := y[yOff + 1];
- y_2 := y[yOff + 2];
- y_3 := y[yOff + 3];
- y_4 := y[yOff + 4];
- y_5 := y[yOff + 5];
- y_6 := y[yOff + 6];
- y_7 := y[yOff + 7];
- zc := 0;
- for i := 0 to System.Pred(8) do
- begin
- c := 0;
- x_i := x[xOff + i];
- c := c + (x_i * y_0 + zz[zzOff + 0]);
- zz[zzOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_1 + zz[zzOff + 1]);
- zz[zzOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_2 + zz[zzOff + 2]);
- zz[zzOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_3 + zz[zzOff + 3]);
- zz[zzOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_4 + zz[zzOff + 4]);
- zz[zzOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_5 + zz[zzOff + 5]);
- zz[zzOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_6 + zz[zzOff + 6]);
- zz[zzOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (x_i * y_7 + zz[zzOff + 7]);
- zz[zzOff + 7] := UInt32(c);
- c := c shr 32;
- zc := zc + (c + (zz[zzOff + 8] and M));
- zz[zzOff + 8] := UInt32(zc);
- zc := zc shr 32;
- System.Inc(zzOff);
- end;
- result := UInt32(zc);
- end;
- class function TNat256.Mul33Add(w: UInt32; const x: TCryptoLibUInt32Array;
- xOff: Int32; const y: TCryptoLibUInt32Array; yOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt64;
- var
- c, wVal, x0, x1, x2, x3, x4, x5, x6, x7: UInt64;
- begin
- {$IFDEF DEBUG}
- System.Assert(w shr 31 = 0);
- {$ENDIF DEBUG}
- c := 0;
- wVal := w;
- x0 := x[xOff + 0];
- c := c + (wVal * x0 + y[yOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- x1 := x[xOff + 1];
- c := c + (wVal * x1 + x0 + y[yOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- x2 := x[xOff + 2];
- c := c + (wVal * x2 + x1 + y[yOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- x3 := x[xOff + 3];
- c := c + (wVal * x3 + x2 + y[yOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := c shr 32;
- x4 := x[xOff + 4];
- c := c + (wVal * x4 + x3 + y[yOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := c shr 32;
- x5 := x[xOff + 5];
- c := c + (wVal * x5 + x4 + y[yOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := c shr 32;
- x6 := x[xOff + 6];
- c := c + (wVal * x6 + x5 + y[yOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := c shr 32;
- x7 := x[xOff + 7];
- c := c + (wVal * x7 + x6 + y[yOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := c shr 32;
- c := c + x7;
- result := c;
- end;
- class function TNat256.MulByWord(x: UInt32;
- const z: TCryptoLibUInt32Array): UInt32;
- var
- c, xVal: UInt64;
- begin
- c := 0;
- xVal := x;
- c := c + (xVal * UInt64(z[0]));
- z[0] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[1]));
- z[1] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[2]));
- z[2] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[3]));
- z[3] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[4]));
- z[4] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[5]));
- z[5] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[6]));
- z[6] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[7]));
- z[7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.MulByWordAddTo(x: UInt32;
- const y, z: TCryptoLibUInt32Array): UInt32;
- var
- c, xVal: UInt64;
- begin
- c := 0;
- xVal := x;
- c := c + (xVal * UInt64(z[0]) + y[0]);
- z[0] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[1]) + y[1]);
- z[1] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[2]) + y[2]);
- z[2] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[3]) + y[3]);
- z[3] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[4]) + y[4]);
- z[4] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[5]) + y[5]);
- z[5] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[6]) + y[6]);
- z[6] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * UInt64(z[7]) + y[7]);
- z[7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.MulWordAddTo(x: UInt32; const y: TCryptoLibUInt32Array;
- yOff: Int32; const z: TCryptoLibUInt32Array; zOff: Int32): UInt32;
- var
- c, xVal: UInt64;
- begin
- c := 0;
- xVal := x;
- c := c + (xVal * y[yOff + 0] + z[zOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 1] + z[zOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 2] + z[zOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 3] + z[zOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 4] + z[zOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 5] + z[zOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 6] + z[zOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * y[yOff + 7] + z[zOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := c shr 32;
- result := UInt32(c);
- end;
- class function TNat256.Mul33DWordAdd(x: UInt32; y: UInt64;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32;
- var
- c, xVal, y00, y01: UInt64;
- begin
- {$IFDEF DEBUG}
- System.Assert(x shr 31 = 0);
- System.Assert(zOff <= 4);
- {$ENDIF DEBUG}
- c := 0;
- xVal := x;
- y00 := y and M;
- c := c + (xVal * y00 + z[zOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- y01 := y shr 32;
- c := c + (xVal * y01 + y00 + z[zOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (y01 + z[zOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- c := c + (z[zOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := c shr 32;
- if c = 0 then
- begin
- result := 0;
- end
- else
- begin
- result := TNat.IncAt(8, z, zOff, 4);
- end;
- end;
- class function TNat256.Mul33WordAdd(x, y: UInt32;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32;
- var
- c, yVal: UInt64;
- begin
- {$IFDEF DEBUG}
- System.Assert(x shr 31 = 0);
- System.Assert(zOff <= 5);
- {$ENDIF DEBUG}
- c := 0;
- yVal := y;
- c := c + (yVal * x + z[zOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (yVal + z[zOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (z[zOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- if c = 0 then
- begin
- result := 0;
- end
- else
- begin
- result := TNat.IncAt(8, z, zOff, 3);
- end;
- end;
- class function TNat256.MulWordDwordAdd(x: UInt32; y: UInt64;
- const z: TCryptoLibUInt32Array; zOff: Int32): UInt32;
- var
- c, xVal: UInt64;
- begin
- {$IFDEF DEBUG}
- System.Assert(zOff <= 5);
- {$ENDIF DEBUG}
- c := 0;
- xVal := x;
- c := c + (xVal * y + z[zOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := c shr 32;
- c := c + (xVal * (y shr 32) + z[zOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := c shr 32;
- c := c + (z[zOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := c shr 32;
- if c = 0 then
- begin
- result := 0;
- end
- else
- begin
- result := TNat.IncAt(8, z, zOff, 3);
- end;
- end;
- class function TNat256.MulWord(x: UInt32; const y, z: TCryptoLibUInt32Array;
- zOff: Int32): UInt32;
- var
- c, xVal: UInt64;
- i: Int32;
- begin
- c := 0;
- xVal := x;
- i := 0;
- repeat
- c := c + (xVal * y[i]);
- z[zOff + i] := UInt32(c);
- c := c shr 32;
- System.Inc(i);
- until not(i < 8);
- result := UInt32(c);
- end;
- class procedure TNat256.Square(const x, zz: TCryptoLibUInt32Array);
- var
- x_0, zz_1, xVal, p, x_1, zz_2, x_2, zz_3, zz_4, x_3, zz_5, zz_6, x_4, zz_7,
- zz_8, x_5, zz_9, zz_10, x_6, zz_11, zz_12, x_7, zz_13, zz_14: UInt64;
- c, w: UInt32;
- i, j: Int32;
- begin
- x_0 := x[0];
- c := 0;
- i := 7;
- j := 16;
- repeat
- xVal := x[i];
- System.Dec(i);
- p := xVal * xVal;
- System.Dec(j);
- zz[j] := (c shl 31) or UInt32(p shr 33);
- System.Dec(j);
- zz[j] := UInt32(p shr 1);
- c := UInt32(p);
- until not(i > 0);
- p := x_0 * x_0;
- zz_1 := UInt64(c shl 31) or (p shr 33);
- zz[0] := UInt32(p);
- c := UInt32(p shr 32) and 1;
- x_1 := x[1];
- zz_2 := zz[2];
- zz_1 := zz_1 + (x_1 * x_0);
- w := UInt32(zz_1);
- zz[1] := (w shl 1) or c;
- c := w shr 31;
- zz_2 := zz_2 + (zz_1 shr 32);
- x_2 := x[2];
- zz_3 := zz[3];
- zz_4 := zz[4];
- zz_2 := zz_2 + (x_2 * x_0);
- w := UInt32(zz_2);
- zz[2] := (w shl 1) or c;
- c := w shr 31;
- zz_3 := zz_3 + ((zz_2 shr 32) + x_2 * x_1);
- zz_4 := zz_4 + (zz_3 shr 32);
- zz_3 := zz_3 and M;
- x_3 := x[3];
- zz_5 := zz[5] + (zz_4 shr 32);
- zz_4 := zz_4 and M;
- zz_6 := zz[6] + (zz_5 shr 32);
- zz_5 := zz_5 and M;
- zz_3 := zz_3 + (x_3 * x_0);
- w := UInt32(zz_3);
- zz[3] := (w shl 1) or c;
- c := w shr 31;
- zz_4 := zz_4 + ((zz_3 shr 32) + x_3 * x_1);
- zz_5 := zz_5 + ((zz_4 shr 32) + x_3 * x_2);
- zz_4 := zz_4 and M;
- zz_6 := zz_6 + (zz_5 shr 32);
- zz_5 := zz_5 and M;
- x_4 := x[4];
- zz_7 := zz[7] + (zz_6 shr 32);
- zz_6 := zz_6 and M;
- zz_8 := zz[8] + (zz_7 shr 32);
- zz_7 := zz_7 and M;
- zz_4 := zz_4 + (x_4 * x_0);
- w := UInt32(zz_4);
- zz[4] := (w shl 1) or c;
- c := w shr 31;
- zz_5 := zz_5 + ((zz_4 shr 32) + x_4 * x_1);
- zz_6 := zz_6 + ((zz_5 shr 32) + x_4 * x_2);
- zz_5 := zz_5 and M;
- zz_7 := zz_7 + ((zz_6 shr 32) + x_4 * x_3);
- zz_6 := zz_6 and M;
- zz_8 := zz_8 + (zz_7 shr 32);
- zz_7 := zz_7 and M;
- x_5 := x[5];
- zz_9 := zz[9] + (zz_8 shr 32);
- zz_8 := zz_8 and M;
- zz_10 := zz[10] + (zz_9 shr 32);
- zz_9 := zz_9 and M;
- zz_5 := zz_5 + (x_5 * x_0);
- w := UInt32(zz_5);
- zz[5] := (w shl 1) or c;
- c := w shr 31;
- zz_6 := zz_6 + ((zz_5 shr 32) + x_5 * x_1);
- zz_7 := zz_7 + ((zz_6 shr 32) + x_5 * x_2);
- zz_6 := zz_6 and M;
- zz_8 := zz_8 + ((zz_7 shr 32) + x_5 * x_3);
- zz_7 := zz_7 and M;
- zz_9 := zz_9 + ((zz_8 shr 32) + x_5 * x_4);
- zz_8 := zz_8 and M;
- zz_10 := zz_10 + (zz_9 shr 32);
- zz_9 := zz_9 and M;
- x_6 := x[6];
- zz_11 := zz[11] + (zz_10 shr 32);
- zz_10 := zz_10 and M;
- zz_12 := zz[12] + (zz_11 shr 32);
- zz_11 := zz_11 and M;
- zz_6 := zz_6 + (x_6 * x_0);
- w := UInt32(zz_6);
- zz[6] := (w shl 1) or c;
- c := w shr 31;
- zz_7 := zz_7 + ((zz_6 shr 32) + x_6 * x_1);
- zz_8 := zz_8 + ((zz_7 shr 32) + x_6 * x_2);
- zz_7 := zz_7 and M;
- zz_9 := zz_9 + ((zz_8 shr 32) + x_6 * x_3);
- zz_8 := zz_8 and M;
- zz_10 := zz_10 + ((zz_9 shr 32) + x_6 * x_4);
- zz_9 := zz_9 and M;
- zz_11 := zz_11 + ((zz_10 shr 32) + x_6 * x_5);
- zz_10 := zz_10 and M;
- zz_12 := zz_12 + (zz_11 shr 32);
- zz_11 := zz_11 and M;
- x_7 := x[7];
- zz_13 := zz[13] + (zz_12 shr 32);
- zz_12 := zz_12 and M;
- zz_14 := zz[14] + (zz_13 shr 32);
- zz_13 := zz_13 and M;
- zz_7 := zz_7 + (x_7 * x_0);
- w := UInt32(zz_7);
- zz[7] := (w shl 1) or c;
- c := w shr 31;
- zz_8 := zz_8 + ((zz_7 shr 32) + x_7 * x_1);
- zz_9 := zz_9 + ((zz_8 shr 32) + x_7 * x_2);
- zz_10 := zz_10 + ((zz_9 shr 32) + x_7 * x_3);
- zz_11 := zz_11 + ((zz_10 shr 32) + x_7 * x_4);
- zz_12 := zz_12 + ((zz_11 shr 32) + x_7 * x_5);
- zz_13 := zz_13 + ((zz_12 shr 32) + x_7 * x_6);
- zz_14 := zz_14 + (zz_13 shr 32);
- w := UInt32(zz_8);
- zz[8] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_9);
- zz[9] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_10);
- zz[10] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_11);
- zz[11] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_12);
- zz[12] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_13);
- zz[13] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_14);
- zz[14] := (w shl 1) or c;
- c := w shr 31;
- w := zz[15] + UInt32(zz_14 shr 32);
- zz[15] := (w shl 1) or c;
- end;
- class procedure TNat256.Square(const x: TCryptoLibUInt32Array; xOff: Int32;
- const zz: TCryptoLibUInt32Array; zzOff: Int32);
- var
- x_0, zz_1, xVal, p, x_1, zz_2, x_2, zz_3, zz_4, x_3, zz_5, zz_6, x_4, zz_7,
- zz_8, x_5, zz_9, zz_10, x_6, zz_11, zz_12, x_7, zz_13, zz_14: UInt64;
- c, w: UInt32;
- i, j: Int32;
- begin
- x_0 := x[xOff + 0];
- c := 0;
- i := 7;
- j := 16;
- repeat
- xVal := x[xOff + i];
- System.Dec(i);
- p := xVal * xVal;
- System.Dec(j);
- zz[zzOff + j] := (c shl 31) or UInt32(p shr 33);
- System.Dec(j);
- zz[zzOff + j] := UInt32(p shr 1);
- c := UInt32(p);
- until not(i > 0);
- p := x_0 * x_0;
- zz_1 := UInt64(c shl 31) or (p shr 33);
- zz[zzOff + 0] := UInt32(p);
- c := UInt32(p shr 32) and 1;
- x_1 := x[xOff + 1];
- zz_2 := zz[zzOff + 2];
- zz_1 := zz_1 + (x_1 * x_0);
- w := UInt32(zz_1);
- zz[zzOff + 1] := (w shl 1) or c;
- c := w shr 31;
- zz_2 := zz_2 + (zz_1 shr 32);
- x_2 := x[xOff + 2];
- zz_3 := zz[zzOff + 3];
- zz_4 := zz[zzOff + 4];
- zz_2 := zz_2 + (x_2 * x_0);
- w := UInt32(zz_2);
- zz[zzOff + 2] := (w shl 1) or c;
- c := w shr 31;
- zz_3 := zz_3 + ((zz_2 shr 32) + x_2 * x_1);
- zz_4 := zz_4 + (zz_3 shr 32);
- zz_3 := zz_3 and M;
- x_3 := x[xOff + 3];
- zz_5 := zz[zzOff + 5] + (zz_4 shr 32);
- zz_4 := zz_4 and M;
- zz_6 := zz[zzOff + 6] + (zz_5 shr 32);
- zz_5 := zz_5 and M;
- zz_3 := zz_3 + (x_3 * x_0);
- w := UInt32(zz_3);
- zz[zzOff + 3] := (w shl 1) or c;
- c := w shr 31;
- zz_4 := zz_4 + ((zz_3 shr 32) + x_3 * x_1);
- zz_5 := zz_5 + ((zz_4 shr 32) + x_3 * x_2);
- zz_4 := zz_4 and M;
- zz_6 := zz_6 + (zz_5 shr 32);
- zz_5 := zz_5 and M;
- x_4 := x[xOff + 4];
- zz_7 := zz[zzOff + 7] + (zz_6 shr 32);
- zz_6 := zz_6 and M;
- zz_8 := zz[zzOff + 8] + (zz_7 shr 32);
- zz_7 := zz_7 and M;
- zz_4 := zz_4 + (x_4 * x_0);
- w := UInt32(zz_4);
- zz[zzOff + 4] := (w shl 1) or c;
- c := w shr 31;
- zz_5 := zz_5 + ((zz_4 shr 32) + x_4 * x_1);
- zz_6 := zz_6 + ((zz_5 shr 32) + x_4 * x_2);
- zz_5 := zz_5 and M;
- zz_7 := zz_7 + ((zz_6 shr 32) + x_4 * x_3);
- zz_6 := zz_6 and M;
- zz_8 := zz_8 + (zz_7 shr 32);
- zz_7 := zz_7 and M;
- x_5 := x[xOff + 5];
- zz_9 := zz[zzOff + 9] + (zz_8 shr 32);
- zz_8 := zz_8 and M;
- zz_10 := zz[zzOff + 10] + (zz_9 shr 32);
- zz_9 := zz_9 and M;
- zz_5 := zz_5 + (x_5 * x_0);
- w := UInt32(zz_5);
- zz[zzOff + 5] := (w shl 1) or c;
- c := w shr 31;
- zz_6 := zz_6 + ((zz_5 shr 32) + x_5 * x_1);
- zz_7 := zz_7 + ((zz_6 shr 32) + x_5 * x_2);
- zz_6 := zz_6 and M;
- zz_8 := zz_8 + ((zz_7 shr 32) + x_5 * x_3);
- zz_7 := zz_7 and M;
- zz_9 := zz_9 + ((zz_8 shr 32) + x_5 * x_4);
- zz_8 := zz_8 and M;
- zz_10 := zz_10 + (zz_9 shr 32);
- zz_9 := zz_9 and M;
- x_6 := x[xOff + 6];
- zz_11 := zz[zzOff + 11] + (zz_10 shr 32);
- zz_10 := zz_10 and M;
- zz_12 := zz[zzOff + 12] + (zz_11 shr 32);
- zz_11 := zz_11 and M;
- zz_6 := zz_6 + (x_6 * x_0);
- w := UInt32(zz_6);
- zz[zzOff + 6] := (w shl 1) or c;
- c := w shr 31;
- zz_7 := zz_7 + ((zz_6 shr 32) + x_6 * x_1);
- zz_8 := zz_8 + ((zz_7 shr 32) + x_6 * x_2);
- zz_7 := zz_7 and M;
- zz_9 := zz_9 + ((zz_8 shr 32) + x_6 * x_3);
- zz_8 := zz_8 and M;
- zz_10 := zz_10 + ((zz_9 shr 32) + x_6 * x_4);
- zz_9 := zz_9 and M;
- zz_11 := zz_11 + ((zz_10 shr 32) + x_6 * x_5);
- zz_10 := zz_10 and M;
- zz_12 := zz_12 + (zz_11 shr 32);
- zz_11 := zz_11 and M;
- x_7 := x[xOff + 7];
- zz_13 := zz[zzOff + 13] + (zz_12 shr 32);
- zz_12 := zz_12 and M;
- zz_14 := zz[zzOff + 14] + (zz_13 shr 32);
- zz_13 := zz_13 and M;
- zz_7 := zz_7 + (x_7 * x_0);
- w := UInt32(zz_7);
- zz[zzOff + 7] := (w shl 1) or c;
- c := w shr 31;
- zz_8 := zz_8 + ((zz_7 shr 32) + x_7 * x_1);
- zz_9 := zz_9 + ((zz_8 shr 32) + x_7 * x_2);
- zz_10 := zz_10 + ((zz_9 shr 32) + x_7 * x_3);
- zz_11 := zz_11 + ((zz_10 shr 32) + x_7 * x_4);
- zz_12 := zz_12 + ((zz_11 shr 32) + x_7 * x_5);
- zz_13 := zz_13 + ((zz_12 shr 32) + x_7 * x_6);
- zz_14 := zz_14 + (zz_13 shr 32);
- w := UInt32(zz_8);
- zz[zzOff + 8] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_9);
- zz[zzOff + 9] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_10);
- zz[zzOff + 10] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_11);
- zz[zzOff + 11] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_12);
- zz[zzOff + 12] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_13);
- zz[zzOff + 13] := (w shl 1) or c;
- c := w shr 31;
- w := UInt32(zz_14);
- zz[zzOff + 14] := (w shl 1) or c;
- c := w shr 31;
- w := zz[zzOff + 15] + UInt32(zz_14 shr 32);
- zz[zzOff + 15] := (w shl 1) or c;
- end;
- class function TNat256.Sub(const x, y, z: TCryptoLibUInt32Array): Int32;
- var
- c: Int64;
- begin
- c := 0;
- c := c + (Int64(x[0]) - y[0]);
- z[0] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[1]) - y[1]);
- z[1] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[2]) - y[2]);
- z[2] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[3]) - y[3]);
- z[3] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[4]) - y[4]);
- z[4] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[5]) - y[5]);
- z[5] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[6]) - y[6]);
- z[6] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[7]) - y[7]);
- z[7] := UInt32(c);
- c := TBits.Asr64(c, 32);
- result := Int32(c);
- end;
- class function TNat256.Sub(const x: TCryptoLibUInt32Array; xOff: Int32;
- const y: TCryptoLibUInt32Array; yOff: Int32; const z: TCryptoLibUInt32Array;
- zOff: Int32): Int32;
- var
- c: Int64;
- begin
- c := 0;
- c := c + (Int64(x[xOff + 0]) - y[yOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 1]) - y[yOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 2]) - y[yOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 3]) - y[yOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 4]) - y[yOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 5]) - y[yOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 6]) - y[yOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(x[xOff + 7]) - y[yOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := TBits.Asr64(c, 32);
- result := Int32(c);
- end;
- class function TNat256.SubBothFrom(const x, y, z: TCryptoLibUInt32Array): Int32;
- var
- c: Int64;
- begin
- c := 0;
- c := c + (Int64(z[0]) - x[0] - y[0]);
- z[0] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[1]) - x[1] - y[1]);
- z[1] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[2]) - x[2] - y[2]);
- z[2] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[3]) - x[3] - y[3]);
- z[3] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[4]) - x[4] - y[4]);
- z[4] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[5]) - x[5] - y[5]);
- z[5] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[6]) - x[6] - y[6]);
- z[6] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[7]) - x[7] - y[7]);
- z[7] := UInt32(c);
- c := TBits.Asr64(c, 32);
- result := Int32(c);
- end;
- class function TNat256.SubFrom(const x, z: TCryptoLibUInt32Array): Int32;
- var
- c: Int64;
- begin
- c := 0;
- c := c + (Int64(z[0]) - x[0]);
- z[0] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[1]) - x[1]);
- z[1] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[2]) - x[2]);
- z[2] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[3]) - x[3]);
- z[3] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[4]) - x[4]);
- z[4] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[5]) - x[5]);
- z[5] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[6]) - x[6]);
- z[6] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[7]) - x[7]);
- z[7] := UInt32(c);
- c := TBits.Asr64(c, 32);
- result := Int32(c);
- end;
- class function TNat256.SubFrom(const x: TCryptoLibUInt32Array; xOff: Int32;
- const z: TCryptoLibUInt32Array; zOff: Int32): Int32;
- var
- c: Int64;
- begin
- c := 0;
- c := c + (Int64(z[zOff + 0]) - x[xOff + 0]);
- z[zOff + 0] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 1]) - x[xOff + 1]);
- z[zOff + 1] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 2]) - x[xOff + 2]);
- z[zOff + 2] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 3]) - x[xOff + 3]);
- z[zOff + 3] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 4]) - x[xOff + 4]);
- z[zOff + 4] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 5]) - x[xOff + 5]);
- z[zOff + 5] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 6]) - x[xOff + 6]);
- z[zOff + 6] := UInt32(c);
- c := TBits.Asr64(c, 32);
- c := c + (Int64(z[zOff + 7]) - x[xOff + 7]);
- z[zOff + 7] := UInt32(c);
- c := TBits.Asr64(c, 32);
- result := Int32(c);
- end;
- class function TNat256.ToBigInteger(const x: TCryptoLibUInt32Array)
- : TBigInteger;
- var
- bs, temp: TCryptoLibByteArray;
- i: Int32;
- x_i: UInt32;
- begin
- System.SetLength(bs, 32);
- for i := 0 to System.Pred(8) do
- begin
- x_i := x[i];
- if (x_i <> 0) then
- begin
- temp := TConverters.ReadUInt32AsBytesBE(x_i);
- System.Move(temp[0], bs[(7 - i) shl 2], System.Length(temp) *
- System.SizeOf(Byte))
- end;
- end;
- result := TBigInteger.Create(1, bs);
- end;
- class function TNat256.ToBigInteger64(const x: TCryptoLibUInt64Array)
- : TBigInteger;
- var
- bs, temp: TCryptoLibByteArray;
- i: Int32;
- x_i: UInt64;
- begin
- System.SetLength(bs, 32);
- for i := 0 to System.Pred(4) do
- begin
- x_i := x[i];
- if (x_i <> Int64(0)) then
- begin
- temp := TConverters.ReadUInt64AsBytesBE(x_i);
- System.Move(temp[0], bs[(3 - i) shl 3], System.Length(temp) *
- System.SizeOf(Byte))
- end;
- end;
- result := TBigInteger.Create(1, bs);
- end;
- class procedure TNat256.Zero(const z: TCryptoLibUInt32Array);
- begin
- TArrayUtils.Fill(z, 0, 8, UInt32(0));
- end;
- end.
|