ClpECFieldElement.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpECFieldElement;
  14. {$I ..\..\Include\CryptoLib.inc}
  15. interface
  16. uses
  17. SysUtils,
  18. ClpBits,
  19. ClpBigInteger,
  20. ClpBigIntegers,
  21. ClpNat,
  22. ClpMod,
  23. ClpArrayUtils,
  24. ClpLongArray,
  25. ClpCryptoLibTypes,
  26. ClpIECFieldElement;
  27. resourcestring
  28. SInvalidValue = 'Value Invalid in Fp Field Element, " x "';
  29. SInvalidValue2 = 'Value Invalid in F2m Field Element, "x"';
  30. SInvalidK2Value = 'k2 must be smaller than k3';
  31. SInvalidK2Value2 = 'k2 must be larger than 0';
  32. SInvalidFieldElement =
  33. 'Field elements are not both instances of F2mFieldElement';
  34. SInvalidFieldElements =
  35. 'Field elements are not elements of the same field F2m';
  36. SIncorrectRepresentation =
  37. 'One of the F2m field elements has incorrect representation';
  38. SEvenValue = 'Even Value of Q';
  39. type
  40. TECFieldElement = class abstract(TInterfacedObject, IECFieldElement)
  41. public
  42. constructor Create();
  43. destructor Destroy; override;
  44. function GetBitLength: Int32; virtual;
  45. function GetIsOne: Boolean; virtual;
  46. function GetIsZero: Boolean; virtual;
  47. function GetFieldName: String; virtual; abstract;
  48. function GetFieldSize: Int32; virtual; abstract;
  49. function ToBigInteger(): TBigInteger; virtual; abstract;
  50. function Add(const b: IECFieldElement): IECFieldElement; virtual; abstract;
  51. function AddOne(): IECFieldElement; virtual; abstract;
  52. function Subtract(const b: IECFieldElement): IECFieldElement;
  53. virtual; abstract;
  54. function Multiply(const b: IECFieldElement): IECFieldElement;
  55. virtual; abstract;
  56. function Divide(const b: IECFieldElement): IECFieldElement;
  57. virtual; abstract;
  58. function Negate(): IECFieldElement; virtual; abstract;
  59. function Square(): IECFieldElement; virtual; abstract;
  60. function Invert(): IECFieldElement; virtual; abstract;
  61. function Sqrt(): IECFieldElement; virtual; abstract;
  62. function MultiplyMinusProduct(const b, x, y: IECFieldElement)
  63. : IECFieldElement; virtual;
  64. function MultiplyPlusProduct(const b, x, y: IECFieldElement)
  65. : IECFieldElement; virtual;
  66. function SquareMinusProduct(const x, y: IECFieldElement)
  67. : IECFieldElement; virtual;
  68. function SquarePlusProduct(const x, y: IECFieldElement)
  69. : IECFieldElement; virtual;
  70. function SquarePow(pow: Int32): IECFieldElement; virtual;
  71. function TestBitZero(): Boolean; virtual;
  72. function Equals(const other: IECFieldElement): Boolean; reintroduce;
  73. function GetHashCode(): {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
  74. {$ENDIF DELPHI}override;
  75. function ToString(): String; override;
  76. function GetEncoded(): TCryptoLibByteArray; virtual;
  77. property FieldName: string read GetFieldName;
  78. property FieldSize: Int32 read GetFieldSize;
  79. property BitLength: Int32 read GetBitLength;
  80. property IsOne: Boolean read GetIsOne;
  81. property IsZero: Boolean read GetIsZero;
  82. end;
  83. type
  84. TFpFieldElement = class(TECFieldElement, IFpFieldElement)
  85. strict private
  86. Fq, Fr, Fx: TBigInteger;
  87. function GetQ: TBigInteger; inline;
  88. function CheckSqrt(const z: IECFieldElement): IECFieldElement; inline;
  89. function LucasSequence(const P, Q, K: TBigInteger)
  90. : TCryptoLibGenericArray<TBigInteger>;
  91. strict protected
  92. function ModAdd(const x1, x2: TBigInteger): TBigInteger; virtual;
  93. function ModDouble(const x: TBigInteger): TBigInteger; virtual;
  94. function ModHalf(x: TBigInteger): TBigInteger; virtual;
  95. function ModHalfAbs(x: TBigInteger): TBigInteger; virtual;
  96. function ModInverse(const x: TBigInteger): TBigInteger; virtual;
  97. function ModMult(const x1, x2: TBigInteger): TBigInteger; virtual;
  98. function ModReduce(x: TBigInteger): TBigInteger; virtual;
  99. function ModSubtract(const x1, x2: TBigInteger): TBigInteger; virtual;
  100. public
  101. constructor Create(const Q, x: TBigInteger); overload;
  102. deprecated 'Use ECCurve.FromBigInteger to construct field elements';
  103. constructor Create(const Q, r, x: TBigInteger); overload;
  104. destructor Destroy; override;
  105. /// <summary>
  106. /// return the field name for this field.
  107. /// </summary>
  108. /// <returns>
  109. /// return the string "Fp".
  110. /// </returns>
  111. function GetFieldName: String; override;
  112. function GetFieldSize: Int32; override;
  113. function ToBigInteger(): TBigInteger; override;
  114. function Add(const b: IECFieldElement): IECFieldElement; override;
  115. function AddOne(): IECFieldElement; override;
  116. function Subtract(const b: IECFieldElement): IECFieldElement; override;
  117. function Multiply(const b: IECFieldElement): IECFieldElement; override;
  118. function Divide(const b: IECFieldElement): IECFieldElement; override;
  119. function Negate(): IECFieldElement; override;
  120. function Square(): IECFieldElement; override;
  121. function Invert(): IECFieldElement; override;
  122. /// <summary>
  123. /// return a sqrt root - the routine verifies that the calculation
  124. /// </summary>
  125. /// <returns>
  126. /// returns the right value - if none exists it returns null.
  127. /// </returns>
  128. function Sqrt(): IECFieldElement; override;
  129. function MultiplyMinusProduct(const b, x, y: IECFieldElement)
  130. : IECFieldElement; override;
  131. function MultiplyPlusProduct(const b, x, y: IECFieldElement)
  132. : IECFieldElement; override;
  133. function SquareMinusProduct(const x, y: IECFieldElement)
  134. : IECFieldElement; override;
  135. function SquarePlusProduct(const x, y: IECFieldElement)
  136. : IECFieldElement; override;
  137. property FieldName: string read GetFieldName;
  138. property FieldSize: Int32 read GetFieldSize;
  139. property Q: TBigInteger read GetQ;
  140. function Equals(const other: IFpFieldElement): Boolean; reintroduce;
  141. function GetHashCode(): {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
  142. {$ENDIF DELPHI}override;
  143. class function CalculateResidue(const P: TBigInteger): TBigInteger; static;
  144. end;
  145. type
  146. /// **
  147. // * Class representing the Elements of the finite field
  148. // * <code>F<sub>2<sup>m</sup></sub></code> in polynomial basis (PB)
  149. // * representation. Both trinomial (Tpb) and pentanomial (Ppb) polynomial
  150. // * basis representations are supported. Gaussian normal basis (GNB)
  151. // * representation is not supported.
  152. // */
  153. TF2mFieldElement = class(TECFieldElement, IF2mFieldElement)
  154. strict private
  155. var
  156. Frepresentation, Fm: Int32;
  157. FKs: TCryptoLibInt32Array;
  158. Fx: TLongArray;
  159. // /**
  160. // * The exponent <code>m</code> of <code>F<sub>2<sup>m</sup></sub></code>.
  161. // */
  162. function GetM: Int32; inline;
  163. /// <summary>
  164. /// Tpb or Ppb.
  165. /// </summary>
  166. function GetRepresentation: Int32; inline;
  167. function GetKs: TCryptoLibInt32Array; inline;
  168. function GetX: TLongArray; inline;
  169. function GetK1: Int32; inline;
  170. function GetK2: Int32; inline;
  171. function GetK3: Int32; inline;
  172. public
  173. const
  174. /// <summary>
  175. /// Indicates gaussian normal basis representation (GNB). Number
  176. /// chosen according to X9.62. GNB is not implemented at present. <br />
  177. /// </summary>
  178. Gnb = Int32(1);
  179. /// <summary>
  180. /// Indicates trinomial basis representation (Tpb). Number chosen
  181. /// according to X9.62. <br />
  182. /// </summary>
  183. Tpb = Int32(2);
  184. /// <summary>
  185. /// Indicates pentanomial basis representation (Ppb). Number chosen
  186. /// according to X9.62. <br />
  187. /// </summary>
  188. Ppb = Int32(3);
  189. // /**
  190. // * Constructor for Ppb.
  191. // * @param m The exponent <code>m</code> of
  192. // * <code>F<sub>2<sup>m</sup></sub></code>.
  193. // * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> +
  194. // * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  195. // * represents the reduction polynomial <code>f(z)</code>.
  196. // * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> +
  197. // * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  198. // * represents the reduction polynomial <code>f(z)</code>.
  199. // * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> +
  200. // * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  201. // * represents the reduction polynomial <code>f(z)</code>.
  202. // * @param x The BigInteger representing the value of the field element.
  203. // */
  204. constructor Create(m, k1, k2, k3: Int32; const x: TBigInteger); overload;
  205. // /**
  206. // * Constructor for Tpb.
  207. // * @param m The exponent <code>m</code> of
  208. // * <code>F<sub>2<sup>m</sup></sub></code>.
  209. // * @param k The integer <code>k</code> where <code>x<sup>m</sup> +
  210. // * x<sup>k</sup> + 1</code> represents the reduction
  211. // * polynomial <code>f(z)</code>.
  212. // * @param x The BigInteger representing the value of the field element.
  213. // */
  214. constructor Create(m, K: Int32; const x: TBigInteger); overload;
  215. constructor Create(m: Int32; ks: TCryptoLibInt32Array;
  216. const x: TLongArray); overload;
  217. destructor Destroy; override;
  218. function GetBitLength: Int32; override;
  219. function GetIsOne: Boolean; override;
  220. function GetIsZero: Boolean; override;
  221. function GetFieldName: String; override;
  222. function GetFieldSize: Int32; override;
  223. function TestBitZero(): Boolean; override;
  224. function ToBigInteger(): TBigInteger; override;
  225. function Add(const b: IECFieldElement): IECFieldElement; override;
  226. function AddOne(): IECFieldElement; override;
  227. function Subtract(const b: IECFieldElement): IECFieldElement; override;
  228. function Multiply(const b: IECFieldElement): IECFieldElement; override;
  229. function Divide(const b: IECFieldElement): IECFieldElement; override;
  230. function Negate(): IECFieldElement; override;
  231. function Square(): IECFieldElement; override;
  232. function Invert(): IECFieldElement; override;
  233. /// <summary>
  234. /// return a sqrt root - the routine verifies that the calculation
  235. /// </summary>
  236. /// <returns>
  237. /// returns the right value - if none exists it returns null.
  238. /// </returns>
  239. function Sqrt(): IECFieldElement; override;
  240. function MultiplyMinusProduct(const b, x, y: IECFieldElement)
  241. : IECFieldElement; override;
  242. function MultiplyPlusProduct(const b, x, y: IECFieldElement)
  243. : IECFieldElement; override;
  244. function SquareMinusProduct(const x, y: IECFieldElement)
  245. : IECFieldElement; override;
  246. function SquarePlusProduct(const x, y: IECFieldElement)
  247. : IECFieldElement; override;
  248. function SquarePow(pow: Int32): IECFieldElement; override;
  249. function Equals(const other: IF2mFieldElement): Boolean; reintroduce;
  250. function GetHashCode(): {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
  251. {$ENDIF DELPHI}override;
  252. // /**
  253. // * Checks, if the ECFieldElements <code>a</code> and <code>b</code>
  254. // * are elements of the same field <code>F<sub>2<sup>m</sup></sub></code>
  255. // * (having the same representation).
  256. // * @param a field element.
  257. // * @param b field element to be compared.
  258. // * @throws ArgumentException if <code>a</code> and <code>b</code>
  259. // * are not elements of the same field
  260. // * <code>F<sub>2<sup>m</sup></sub></code> (having the same
  261. // * representation).
  262. // */
  263. class procedure CheckFieldElements(const a, b: IECFieldElement); static;
  264. // /**
  265. // * @return the representation of the field
  266. // * <code>F<sub>2<sup>m</sup></sub></code>, either of
  267. // * {@link F2mFieldElement.Tpb} (trinomial
  268. // * basis representation) or
  269. // * {@link F2mFieldElement.Ppb} (pentanomial
  270. // * basis representation).
  271. // */
  272. property Representation: Int32 read GetRepresentation;
  273. // /**
  274. // * @return the degree <code>m</code> of the reduction polynomial
  275. // * <code>f(z)</code>.
  276. // */
  277. property m: Int32 read GetM;
  278. // /**
  279. // * @return Tpb: The integer <code>k</code> where <code>x<sup>m</sup> +
  280. // * x<sup>k</sup> + 1</code> represents the reduction polynomial
  281. // * <code>f(z)</code>.<br/>
  282. // * Ppb: The integer <code>k1</code> where <code>x<sup>m</sup> +
  283. // * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  284. // * represents the reduction polynomial <code>f(z)</code>.<br/>
  285. // */
  286. property k1: Int32 read GetK1;
  287. // /**
  288. // * @return Tpb: Always returns <code>0</code><br/>
  289. // * Ppb: The integer <code>k2</code> where <code>x<sup>m</sup> +
  290. // * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  291. // * represents the reduction polynomial <code>f(z)</code>.<br/>
  292. // */
  293. property k2: Int32 read GetK2;
  294. // /**
  295. // * @return Tpb: Always set to <code>0</code><br/>
  296. // * Ppb: The integer <code>k3</code> where <code>x<sup>m</sup> +
  297. // * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  298. // * represents the reduction polynomial <code>f(z)</code>.<br/>
  299. // */
  300. property k3: Int32 read GetK3;
  301. property ks: TCryptoLibInt32Array read GetKs;
  302. /// <summary>
  303. /// The <c>LongArray</c> holding the bits.
  304. /// </summary>
  305. property x: TLongArray read GetX;
  306. property FieldName: string read GetFieldName;
  307. property FieldSize: Int32 read GetFieldSize;
  308. property BitLength: Int32 read GetBitLength;
  309. property IsOne: Boolean read GetIsOne;
  310. property IsZero: Boolean read GetIsZero;
  311. end;
  312. implementation
  313. { TF2mFieldElement }
  314. function TF2mFieldElement.Add(const b: IECFieldElement): IECFieldElement;
  315. var
  316. iarrClone: TLongArray;
  317. bF2m: IF2mFieldElement;
  318. begin
  319. // No check performed here for performance reasons. Instead the
  320. // elements involved are checked in ECPoint.F2m
  321. // checkFieldElements(this, b);
  322. iarrClone := Fx.Copy();
  323. bF2m := b as IF2mFieldElement;
  324. iarrClone.AddShiftedByWords(bF2m.x, 0);
  325. result := TF2mFieldElement.Create(Fm, FKs, iarrClone);
  326. end;
  327. function TF2mFieldElement.AddOne: IECFieldElement;
  328. begin
  329. result := TF2mFieldElement.Create(Fm, FKs, Fx.AddOne());
  330. end;
  331. class procedure TF2mFieldElement.CheckFieldElements(const a,
  332. b: IECFieldElement);
  333. var
  334. aF2m, bF2m: IF2mFieldElement;
  335. begin
  336. if (not(Supports(a, IF2mFieldElement, aF2m)) or
  337. (not(Supports(b, IF2mFieldElement, bF2m)))) then
  338. begin
  339. raise EArgumentCryptoLibException.CreateRes(@SInvalidFieldElement);
  340. end;
  341. if (aF2m.Representation <> bF2m.Representation) then
  342. begin
  343. // Should never occur
  344. raise EArgumentCryptoLibException.CreateRes(@SIncorrectRepresentation);
  345. end;
  346. if ((aF2m.m <> bF2m.m) or (not TArrayUtils.AreEqual(aF2m.ks, bF2m.ks))) then
  347. begin
  348. raise EArgumentCryptoLibException.CreateRes(@SInvalidFieldElements);
  349. end;
  350. end;
  351. constructor TF2mFieldElement.Create(m, K: Int32; const x: TBigInteger);
  352. begin
  353. Create(m, K, 0, 0, x);
  354. end;
  355. constructor TF2mFieldElement.Create(m, k1, k2, k3: Int32; const x: TBigInteger);
  356. begin
  357. Inherited Create();
  358. if (not(x.IsInitialized) or (x.SignValue < 0) or (x.BitLength > m)) then
  359. begin
  360. raise EArgumentCryptoLibException.CreateRes(@SInvalidValue2);
  361. end;
  362. if ((k2 = 0) and (k3 = 0)) then
  363. begin
  364. Frepresentation := Tpb;
  365. FKs := TCryptoLibInt32Array.Create(k1);
  366. end
  367. else
  368. begin
  369. if (k2 >= k3) then
  370. begin
  371. raise EArgumentCryptoLibException.CreateRes(@SInvalidK2Value);
  372. end;
  373. if (k2 <= 0) then
  374. begin
  375. raise EArgumentCryptoLibException.CreateRes(@SInvalidK2Value2);
  376. end;
  377. Frepresentation := Ppb;
  378. FKs := TCryptoLibInt32Array.Create(k1, k2, k3);
  379. end;
  380. Fm := m;
  381. Fx := TLongArray.Create(x);
  382. end;
  383. constructor TF2mFieldElement.Create(m: Int32; ks: TCryptoLibInt32Array;
  384. const x: TLongArray);
  385. begin
  386. Inherited Create();
  387. Fm := m;
  388. if (System.Length(ks) = 1) then
  389. begin
  390. Frepresentation := Tpb
  391. end
  392. else
  393. begin
  394. Frepresentation := Ppb;
  395. end;
  396. FKs := ks;
  397. Fx := x;
  398. end;
  399. destructor TF2mFieldElement.Destroy;
  400. begin
  401. inherited Destroy;
  402. end;
  403. function TF2mFieldElement.Divide(const b: IECFieldElement): IECFieldElement;
  404. var
  405. bInv: IECFieldElement;
  406. begin
  407. // There may be more efficient implementations
  408. bInv := b.Invert();
  409. result := Multiply(bInv);
  410. end;
  411. function TF2mFieldElement.Equals(const other: IF2mFieldElement): Boolean;
  412. begin
  413. if (other = Self as IF2mFieldElement) then
  414. begin
  415. result := true;
  416. Exit;
  417. end;
  418. if (Nil = other) then
  419. begin
  420. result := false;
  421. Exit;
  422. end;
  423. result := ((m = other.m) and (Representation = other.Representation) and
  424. TArrayUtils.AreEqual(ks, other.ks) and (x.Equals(other.x)));
  425. end;
  426. function TF2mFieldElement.GetBitLength: Int32;
  427. begin
  428. result := Fx.Degree();
  429. end;
  430. function TF2mFieldElement.GetFieldName: String;
  431. begin
  432. result := 'F2m';
  433. end;
  434. function TF2mFieldElement.GetFieldSize: Int32;
  435. begin
  436. result := Fm;
  437. end;
  438. function TF2mFieldElement.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
  439. {$ENDIF DELPHI}
  440. begin
  441. result := Fx.GetHashCode() xor Fm xor TArrayUtils.GetArrayHashCode(FKs);
  442. end;
  443. function TF2mFieldElement.GetIsOne: Boolean;
  444. begin
  445. result := Fx.IsOne();
  446. end;
  447. function TF2mFieldElement.GetIsZero: Boolean;
  448. begin
  449. result := Fx.IsZero();
  450. end;
  451. function TF2mFieldElement.GetK1: Int32;
  452. begin
  453. result := FKs[0];
  454. end;
  455. function TF2mFieldElement.GetK2: Int32;
  456. begin
  457. if (System.Length(FKs) >= 2) then
  458. begin
  459. result := FKs[1];
  460. end
  461. else
  462. begin
  463. result := 0;
  464. end;
  465. end;
  466. function TF2mFieldElement.GetK3: Int32;
  467. begin
  468. if (System.Length(FKs) >= 3) then
  469. begin
  470. result := FKs[2];
  471. end
  472. else
  473. begin
  474. result := 0;
  475. end;
  476. end;
  477. function TF2mFieldElement.GetKs: TCryptoLibInt32Array;
  478. begin
  479. result := FKs;
  480. end;
  481. function TF2mFieldElement.GetM: Int32;
  482. begin
  483. result := Fm;
  484. end;
  485. function TF2mFieldElement.GetRepresentation: Int32;
  486. begin
  487. result := Frepresentation;
  488. end;
  489. function TF2mFieldElement.GetX: TLongArray;
  490. begin
  491. result := Fx;
  492. end;
  493. function TF2mFieldElement.Invert: IECFieldElement;
  494. begin
  495. result := TF2mFieldElement.Create(Fm, FKs, Fx.ModInverse(Fm, FKs));
  496. end;
  497. function TF2mFieldElement.Multiply(const b: IECFieldElement): IECFieldElement;
  498. begin
  499. // Right-to-left comb multiplication in the LongArray
  500. // Input: Binary polynomials a(z) and b(z) of degree at most m-1
  501. // Output: c(z) = a(z) * b(z) mod f(z)
  502. // No check performed here for performance reasons. Instead the
  503. // elements involved are checked in ECPoint.F2m
  504. // checkFieldElements(this, b);
  505. result := TF2mFieldElement.Create(Fm, FKs,
  506. Fx.ModMultiply((b as IF2mFieldElement).x, Fm, FKs));
  507. end;
  508. function TF2mFieldElement.MultiplyMinusProduct(const b, x, y: IECFieldElement)
  509. : IECFieldElement;
  510. begin
  511. result := MultiplyPlusProduct(b, x, y);
  512. end;
  513. function TF2mFieldElement.MultiplyPlusProduct(const b, x, y: IECFieldElement)
  514. : IECFieldElement;
  515. var
  516. ax, bx, xx, yx, ab, xy: TLongArray;
  517. begin
  518. ax := Fx;
  519. bx := (b as IF2mFieldElement).x;
  520. xx := (x as IF2mFieldElement).x;
  521. yx := (y as IF2mFieldElement).x;
  522. ab := ax.Multiply(bx, Fm, FKs);
  523. xy := xx.Multiply(yx, Fm, FKs);
  524. if ((ab.Equals(ax)) or (ab.Equals(bx))) then
  525. begin
  526. ab := ab.Copy();
  527. end;
  528. ab.AddShiftedByWords(xy, 0);
  529. ab.Reduce(Fm, FKs);
  530. result := TF2mFieldElement.Create(Fm, FKs, ab);
  531. end;
  532. function TF2mFieldElement.Negate: IECFieldElement;
  533. begin
  534. // -x == x holds for all x in F2m
  535. result := Self as IECFieldElement;
  536. end;
  537. function TF2mFieldElement.Sqrt: IECFieldElement;
  538. begin
  539. if ((Fx.IsZero()) or (Fx.IsOne())) then
  540. begin
  541. result := Self as IECFieldElement;
  542. end
  543. else
  544. begin
  545. result := SquarePow(Fm - 1);
  546. end;
  547. end;
  548. function TF2mFieldElement.Square: IECFieldElement;
  549. begin
  550. result := TF2mFieldElement.Create(Fm, FKs, Fx.ModSquare(Fm, FKs));
  551. end;
  552. function TF2mFieldElement.SquareMinusProduct(const x, y: IECFieldElement)
  553. : IECFieldElement;
  554. begin
  555. result := SquarePlusProduct(x, y);
  556. end;
  557. function TF2mFieldElement.SquarePlusProduct(const x, y: IECFieldElement)
  558. : IECFieldElement;
  559. var
  560. ax, xx, yx, aa, xy: TLongArray;
  561. begin
  562. ax := Fx;
  563. xx := (x as IF2mFieldElement).x;
  564. yx := (y as IF2mFieldElement).x;
  565. aa := ax.Square(Fm, FKs);
  566. xy := xx.Multiply(yx, Fm, FKs);
  567. if (aa.Equals(ax)) then
  568. begin
  569. aa := aa.Copy();
  570. end;
  571. aa.AddShiftedByWords(xy, 0);
  572. aa.Reduce(Fm, FKs);
  573. result := TF2mFieldElement.Create(Fm, FKs, aa);
  574. end;
  575. function TF2mFieldElement.SquarePow(pow: Int32): IECFieldElement;
  576. begin
  577. if pow < 1 then
  578. begin
  579. result := Self as IECFieldElement
  580. end
  581. else
  582. begin
  583. result := TF2mFieldElement.Create(Fm, FKs, Fx.ModSquareN(pow, Fm, FKs));
  584. end;
  585. end;
  586. function TF2mFieldElement.Subtract(const b: IECFieldElement): IECFieldElement;
  587. begin
  588. // Addition and subtraction are the same in F2m
  589. result := Add(b);
  590. end;
  591. function TF2mFieldElement.TestBitZero: Boolean;
  592. begin
  593. result := Fx.TestBitZero();
  594. end;
  595. function TF2mFieldElement.ToBigInteger: TBigInteger;
  596. begin
  597. result := Fx.ToBigInteger();
  598. end;
  599. { TECFieldElement }
  600. constructor TECFieldElement.Create;
  601. begin
  602. Inherited Create();
  603. end;
  604. destructor TECFieldElement.Destroy;
  605. begin
  606. inherited Destroy;
  607. end;
  608. function TECFieldElement.Equals(const other: IECFieldElement): Boolean;
  609. begin
  610. if (other = Self as IECFieldElement) then
  611. begin
  612. result := true;
  613. Exit;
  614. end;
  615. if (Nil = other) then
  616. begin
  617. result := false;
  618. Exit;
  619. end;
  620. result := ToBigInteger().Equals(other.ToBigInteger());
  621. end;
  622. function TECFieldElement.GetBitLength: Int32;
  623. begin
  624. result := ToBigInteger().BitLength;
  625. end;
  626. function TECFieldElement.GetEncoded: TCryptoLibByteArray;
  627. begin
  628. result := TBigIntegers.AsUnsignedByteArray((FieldSize + 7) div 8,
  629. ToBigInteger());
  630. end;
  631. function TECFieldElement.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
  632. {$ENDIF DELPHI}
  633. begin
  634. result := ToBigInteger().GetHashCode();
  635. end;
  636. function TECFieldElement.GetIsOne: Boolean;
  637. begin
  638. result := BitLength = 1;
  639. end;
  640. function TECFieldElement.GetIsZero: Boolean;
  641. begin
  642. result := 0 = ToBigInteger().SignValue;
  643. end;
  644. function TECFieldElement.MultiplyMinusProduct(const b, x, y: IECFieldElement)
  645. : IECFieldElement;
  646. begin
  647. result := Multiply(b).Subtract(x.Multiply(y));
  648. end;
  649. function TECFieldElement.MultiplyPlusProduct(const b, x, y: IECFieldElement)
  650. : IECFieldElement;
  651. begin
  652. result := Multiply(b).Add(x.Multiply(y));
  653. end;
  654. function TECFieldElement.SquareMinusProduct(const x, y: IECFieldElement)
  655. : IECFieldElement;
  656. begin
  657. result := Square().Subtract(x.Multiply(y));
  658. end;
  659. function TECFieldElement.SquarePlusProduct(const x, y: IECFieldElement)
  660. : IECFieldElement;
  661. begin
  662. result := Square().Add(x.Multiply(y));
  663. end;
  664. function TECFieldElement.SquarePow(pow: Int32): IECFieldElement;
  665. var
  666. r: IECFieldElement;
  667. i: Int32;
  668. begin
  669. r := Self as IECFieldElement;
  670. i := 0;
  671. while i < pow do
  672. begin
  673. r := r.Square();
  674. System.Inc(i);
  675. end;
  676. result := r;
  677. end;
  678. function TECFieldElement.TestBitZero: Boolean;
  679. begin
  680. result := ToBigInteger().TestBit(0);
  681. end;
  682. function TECFieldElement.ToString: String;
  683. begin
  684. result := ToBigInteger().ToString(16);
  685. end;
  686. { TFpFieldElement }
  687. function TFpFieldElement.Add(const b: IECFieldElement): IECFieldElement;
  688. begin
  689. result := TFpFieldElement.Create(Fq, Fr, ModAdd(Fx, b.ToBigInteger()));
  690. end;
  691. function TFpFieldElement.AddOne: IECFieldElement;
  692. var
  693. x2: TBigInteger;
  694. begin
  695. x2 := Fx.Add(TBigInteger.One);
  696. if (x2.CompareTo(Q) = 0) then
  697. begin
  698. x2 := TBigInteger.Zero;
  699. end;
  700. result := TFpFieldElement.Create(Fq, Fr, x2);
  701. end;
  702. class function TFpFieldElement.CalculateResidue(const P: TBigInteger)
  703. : TBigInteger;
  704. var
  705. BitLength: Int32;
  706. firstWord: TBigInteger;
  707. begin
  708. BitLength := P.BitLength;
  709. if (BitLength >= 96) then
  710. begin
  711. firstWord := P.ShiftRight(BitLength - 64);
  712. if (firstWord.Int64Value = Int64(-1)) then
  713. begin
  714. result := TBigInteger.One.ShiftLeft(BitLength).Subtract(P);
  715. Exit;
  716. end;
  717. if ((BitLength and 7) = 0) then
  718. begin
  719. result := TBigInteger.One.ShiftLeft(BitLength shl 1).Divide(P).Negate();
  720. Exit;
  721. end;
  722. end;
  723. result := Default (TBigInteger);
  724. end;
  725. function TFpFieldElement.CheckSqrt(const z: IECFieldElement): IECFieldElement;
  726. begin
  727. if (z.Square().Equals(Self as IECFieldElement)) then
  728. begin
  729. result := z;
  730. end
  731. else
  732. begin
  733. result := Nil;
  734. end;
  735. end;
  736. constructor TFpFieldElement.Create(const Q, x: TBigInteger);
  737. begin
  738. Create(Q, CalculateResidue(Q), x);
  739. end;
  740. constructor TFpFieldElement.Create(const Q, r, x: TBigInteger);
  741. begin
  742. Inherited Create();
  743. if (not(x.IsInitialized) or (x.SignValue < 0) or (x.CompareTo(Q) >= 0)) then
  744. begin
  745. raise EArgumentCryptoLibException.CreateRes(@SInvalidValue);
  746. end;
  747. Fq := Q;
  748. Fr := r;
  749. Fx := x;
  750. end;
  751. destructor TFpFieldElement.Destroy;
  752. begin
  753. inherited Destroy;
  754. end;
  755. function TFpFieldElement.Divide(const b: IECFieldElement): IECFieldElement;
  756. begin
  757. result := TFpFieldElement.Create(Fq, Fr,
  758. ModMult(Fx, ModInverse(b.ToBigInteger())));
  759. end;
  760. function TFpFieldElement.Equals(const other: IFpFieldElement): Boolean;
  761. begin
  762. if (other = Self as IFpFieldElement) then
  763. begin
  764. result := true;
  765. Exit;
  766. end;
  767. if (other = Nil) then
  768. begin
  769. result := false;
  770. Exit;
  771. end;
  772. result := (Q.Equals(other.Q) and (Inherited Equals(other)));
  773. end;
  774. function TFpFieldElement.GetFieldName: String;
  775. begin
  776. result := 'Fp';
  777. end;
  778. function TFpFieldElement.GetFieldSize: Int32;
  779. begin
  780. result := Q.BitLength;
  781. end;
  782. function TFpFieldElement.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
  783. {$ENDIF DELPHI}
  784. begin
  785. result := Q.GetHashCode() xor (Inherited GetHashCode());
  786. end;
  787. function TFpFieldElement.GetQ: TBigInteger;
  788. begin
  789. result := Fq;
  790. end;
  791. function TFpFieldElement.Invert: IECFieldElement;
  792. begin
  793. // TODO Modular inversion can be faster for a (Generalized) Mersenne Prime.
  794. result := TFpFieldElement.Create(Fq, Fr, ModInverse(Fx));
  795. end;
  796. function TFpFieldElement.LucasSequence(const P, Q, K: TBigInteger)
  797. : TCryptoLibGenericArray<TBigInteger>;
  798. var
  799. n, s, j: Int32;
  800. Uh, Vl, Vh, Ql, Qh: TBigInteger;
  801. begin
  802. // TODO Research and apply "common-multiplicand multiplication here"
  803. n := K.BitLength;
  804. s := K.GetLowestSetBit();
  805. {$IFDEF DEBUG}
  806. System.Assert(K.TestBit(s));
  807. {$ENDIF DEBUG}
  808. Uh := TBigInteger.One;
  809. Vl := TBigInteger.Two;
  810. Vh := P;
  811. Ql := TBigInteger.One;
  812. Qh := TBigInteger.One;
  813. j := n - 1;
  814. while j >= s + 1 do
  815. begin
  816. Ql := ModMult(Ql, Qh);
  817. if (K.TestBit(j)) then
  818. begin
  819. Qh := ModMult(Ql, Q);
  820. Uh := ModMult(Uh, Vh);
  821. Vl := ModReduce(Vh.Multiply(Vl).Subtract(P.Multiply(Ql)));
  822. Vh := ModReduce(Vh.Multiply(Vh).Subtract(Qh.ShiftLeft(1)));
  823. end
  824. else
  825. begin
  826. Qh := Ql;
  827. Uh := ModReduce(Uh.Multiply(Vl).Subtract(Ql));
  828. Vh := ModReduce(Vl.Multiply(Vl).Subtract(Ql.ShiftLeft(1)));
  829. end;
  830. System.Dec(j);
  831. end;
  832. Ql := ModMult(Ql, Qh);
  833. Qh := ModMult(Ql, Q);
  834. Uh := ModReduce(Uh.Multiply(Vl).Subtract(Ql));
  835. Vl := ModReduce(Vh.Multiply(Vl).Subtract(P.Multiply(Ql)));
  836. Ql := ModMult(Ql, Qh);
  837. j := 1;
  838. while j <= s do
  839. begin
  840. Uh := ModMult(Uh, Vl);
  841. Vl := ModReduce(Vl.Multiply(Vl).Subtract(Ql.ShiftLeft(1)));
  842. Ql := ModMult(Ql, Ql);
  843. System.Inc(j);
  844. end;
  845. result := TCryptoLibGenericArray<TBigInteger>.Create(Uh, Vl);
  846. end;
  847. function TFpFieldElement.ModAdd(const x1, x2: TBigInteger): TBigInteger;
  848. var
  849. x3: TBigInteger;
  850. begin
  851. x3 := x1.Add(x2);
  852. if (x3.CompareTo(Q) >= 0) then
  853. begin
  854. x3 := x3.Subtract(Q);
  855. end;
  856. result := x3;
  857. end;
  858. function TFpFieldElement.ModDouble(const x: TBigInteger): TBigInteger;
  859. var
  860. _2x: TBigInteger;
  861. begin
  862. _2x := x.ShiftLeft(1);
  863. if (_2x.CompareTo(Q) >= 0) then
  864. begin
  865. _2x := _2x.Subtract(Q);
  866. end;
  867. result := _2x;
  868. end;
  869. function TFpFieldElement.ModHalf(x: TBigInteger): TBigInteger;
  870. begin
  871. if (x.TestBit(0)) then
  872. begin
  873. x := Q.Add(x);
  874. end;
  875. result := x.ShiftRight(1);
  876. end;
  877. function TFpFieldElement.ModHalfAbs(x: TBigInteger): TBigInteger;
  878. begin
  879. if (x.TestBit(0)) then
  880. begin
  881. x := Q.Subtract(x);
  882. end;
  883. result := x.ShiftRight(1);
  884. end;
  885. function TFpFieldElement.ModInverse(const x: TBigInteger): TBigInteger;
  886. var
  887. bits, len: Int32;
  888. P, n, z: TCryptoLibUInt32Array;
  889. begin
  890. bits := FieldSize;
  891. len := TBits.Asr32((bits + 31), 5);
  892. P := TNat.FromBigInteger(bits, Q);
  893. n := TNat.FromBigInteger(bits, x);
  894. z := TNat.Create(len);
  895. TMod.Invert(P, n, z);
  896. result := TNat.ToBigInteger(len, z);
  897. end;
  898. function TFpFieldElement.ModMult(const x1, x2: TBigInteger): TBigInteger;
  899. begin
  900. result := ModReduce(x1.Multiply(x2));
  901. end;
  902. function TFpFieldElement.ModReduce(x: TBigInteger): TBigInteger;
  903. var
  904. negative, rIsOne: Boolean;
  905. qLen, d: Int32;
  906. qMod, u, v, mu, quot, bk1: TBigInteger;
  907. begin
  908. if (not(Fr.IsInitialized)) then
  909. begin
  910. x := x.&Mod(Q);
  911. end
  912. else
  913. begin
  914. negative := x.SignValue < 0;
  915. if (negative) then
  916. begin
  917. x := x.Abs();
  918. end;
  919. qLen := Q.BitLength;
  920. if (Fr.SignValue > 0) then
  921. begin
  922. qMod := TBigInteger.One.ShiftLeft(qLen);
  923. rIsOne := Fr.Equals(TBigInteger.One);
  924. while (x.BitLength > (qLen + 1)) do
  925. begin
  926. u := x.ShiftRight(qLen);
  927. v := x.Remainder(qMod);
  928. if (not rIsOne) then
  929. begin
  930. u := u.Multiply(Fr);
  931. end;
  932. x := u.Add(v);
  933. end
  934. end
  935. else
  936. begin
  937. d := ((qLen - 1) and 31) + 1;
  938. mu := Fr.Negate();
  939. u := mu.Multiply(x.ShiftRight(qLen - d));
  940. quot := u.ShiftRight(qLen + d);
  941. v := quot.Multiply(Q);
  942. bk1 := TBigInteger.One.ShiftLeft(qLen + d);
  943. v := v.Remainder(bk1);
  944. x := x.Remainder(bk1);
  945. x := x.Subtract(v);
  946. if (x.SignValue < 0) then
  947. begin
  948. x := x.Add(bk1);
  949. end
  950. end;
  951. while (x.CompareTo(Q) >= 0) do
  952. begin
  953. x := x.Subtract(Q);
  954. end;
  955. if ((negative) and (x.SignValue <> 0)) then
  956. begin
  957. x := Q.Subtract(x);
  958. end;
  959. end;
  960. result := x;
  961. end;
  962. function TFpFieldElement.ModSubtract(const x1, x2: TBigInteger): TBigInteger;
  963. var
  964. x3: TBigInteger;
  965. begin
  966. x3 := x1.Subtract(x2);
  967. if (x3.SignValue < 0) then
  968. begin
  969. x3 := x3.Add(Q);
  970. end;
  971. result := x3;
  972. end;
  973. function TFpFieldElement.Multiply(const b: IECFieldElement): IECFieldElement;
  974. begin
  975. result := TFpFieldElement.Create(Fq, Fr, ModMult(Fx, b.ToBigInteger()));
  976. end;
  977. function TFpFieldElement.MultiplyMinusProduct(const b, x, y: IECFieldElement)
  978. : IECFieldElement;
  979. var
  980. ax, bx, xx, yx, ab, xy: TBigInteger;
  981. begin
  982. ax := Fx;
  983. bx := b.ToBigInteger();
  984. xx := x.ToBigInteger();
  985. yx := y.ToBigInteger();
  986. ab := ax.Multiply(bx);
  987. xy := xx.Multiply(yx);
  988. result := TFpFieldElement.Create(Fq, Fr, ModReduce(ab.Subtract(xy)));
  989. end;
  990. function TFpFieldElement.MultiplyPlusProduct(const b, x, y: IECFieldElement)
  991. : IECFieldElement;
  992. var
  993. ax, bx, xx, yx, ab, xy, sum: TBigInteger;
  994. begin
  995. ax := Fx;
  996. bx := b.ToBigInteger();
  997. xx := x.ToBigInteger();
  998. yx := y.ToBigInteger();
  999. ab := ax.Multiply(bx);
  1000. xy := xx.Multiply(yx);
  1001. sum := ab.Add(xy);
  1002. if ((Fr.IsInitialized) and (Fr.SignValue < 0) and
  1003. (sum.BitLength > (Fq.BitLength shl 1))) then
  1004. begin
  1005. sum := sum.Subtract(Fq.ShiftLeft(Q.BitLength));
  1006. end;
  1007. result := TFpFieldElement.Create(Fq, Fr, ModReduce(sum));
  1008. end;
  1009. function TFpFieldElement.Negate: IECFieldElement;
  1010. begin
  1011. if Fx.SignValue = 0 then
  1012. begin
  1013. result := Self as IECFieldElement
  1014. end
  1015. else
  1016. begin
  1017. result := TFpFieldElement.Create(Fq, Fr, Fq.Subtract(Fx));
  1018. end;
  1019. end;
  1020. function TFpFieldElement.Sqrt: IECFieldElement;
  1021. var
  1022. u, v, K, e, t1, t2, t3, t4, y, legendreExponent, x, fourX, qMinusOne,
  1023. P: TBigInteger;
  1024. tempRes: TCryptoLibGenericArray<TBigInteger>;
  1025. begin
  1026. if (IsZero or IsOne) then
  1027. begin
  1028. result := Self as IECFieldElement;
  1029. Exit;
  1030. end;
  1031. if (not Fq.TestBit(0)) then
  1032. begin
  1033. raise ENotImplementedCryptoLibException.CreateRes(@SEvenValue);
  1034. end;
  1035. if (Fq.TestBit(1)) then // q == 4m + 3
  1036. begin
  1037. e := Fq.ShiftRight(2).Add(TBigInteger.One);
  1038. result := CheckSqrt(TFpFieldElement.Create(Fq, Fr, Fx.ModPow(e, Fq))
  1039. as IFpFieldElement);
  1040. Exit;
  1041. end;
  1042. if (Fq.TestBit(2)) then // q == 8m + 5
  1043. begin
  1044. t1 := Fx.ModPow(Fq.ShiftRight(3), Fq);
  1045. t2 := ModMult(t1, Fx);
  1046. t3 := ModMult(t2, t1);
  1047. if (t3.Equals(TBigInteger.One)) then
  1048. begin
  1049. result := CheckSqrt(TFpFieldElement.Create(Fq, Fr, t2)
  1050. as IFpFieldElement);
  1051. Exit;
  1052. end;
  1053. // TODO This is constant and could be precomputed
  1054. t4 := TBigInteger.Two.ModPow(Fq.ShiftRight(2), Fq);
  1055. y := ModMult(t2, t4);
  1056. result := CheckSqrt(TFpFieldElement.Create(Fq, Fr, y) as IFpFieldElement);
  1057. end;
  1058. // q == 8m + 1
  1059. legendreExponent := Fq.ShiftRight(1);
  1060. if (not(Fx.ModPow(legendreExponent, Fq).Equals(TBigInteger.One))) then
  1061. begin
  1062. result := Nil;
  1063. Exit;
  1064. end;
  1065. x := Fx;
  1066. fourX := ModDouble(ModDouble(x));
  1067. K := legendreExponent.Add(TBigInteger.One);
  1068. qMinusOne := Fq.Subtract(TBigInteger.One);
  1069. repeat
  1070. repeat
  1071. P := TBigInteger.Arbitrary(Fq.BitLength);
  1072. until ((not P.CompareTo(Q) >= 0) or (ModReduce(P.Multiply(P).Subtract(fourX)
  1073. ).ModPow(legendreExponent, Q).Equals(qMinusOne)));
  1074. tempRes := LucasSequence(P, x, K);
  1075. u := tempRes[0];
  1076. v := tempRes[1];
  1077. if (ModMult(v, v).Equals(fourX)) then
  1078. begin
  1079. result := TFpFieldElement.Create(Fq, Fr, ModHalfAbs(v));
  1080. Exit;
  1081. end;
  1082. until ((not u.Equals(TBigInteger.One)) or (not u.Equals(qMinusOne)));
  1083. result := Nil;
  1084. end;
  1085. function TFpFieldElement.Square: IECFieldElement;
  1086. begin
  1087. result := TFpFieldElement.Create(Fq, Fr, ModMult(Fx, Fx));
  1088. end;
  1089. function TFpFieldElement.SquareMinusProduct(const x, y: IECFieldElement)
  1090. : IECFieldElement;
  1091. var
  1092. ax, xx, yx, aa, xy: TBigInteger;
  1093. begin
  1094. ax := Fx;
  1095. xx := x.ToBigInteger();
  1096. yx := y.ToBigInteger();
  1097. aa := ax.Multiply(ax);
  1098. xy := xx.Multiply(yx);
  1099. result := TFpFieldElement.Create(Fq, Fr, ModReduce(aa.Subtract(xy)));
  1100. end;
  1101. function TFpFieldElement.SquarePlusProduct(const x, y: IECFieldElement)
  1102. : IECFieldElement;
  1103. var
  1104. ax, xx, yx, aa, xy, sum: TBigInteger;
  1105. begin
  1106. ax := Fx;
  1107. xx := x.ToBigInteger();
  1108. yx := y.ToBigInteger();
  1109. aa := ax.Multiply(ax);
  1110. xy := xx.Multiply(yx);
  1111. sum := aa.Add(xy);
  1112. if ((Fr.IsInitialized) and (Fr.SignValue < 0) and
  1113. (sum.BitLength > (Fq.BitLength shl 1))) then
  1114. begin
  1115. sum := sum.Subtract(Fq.ShiftLeft(Fq.BitLength));
  1116. end;
  1117. result := TFpFieldElement.Create(Fq, Fr, ModReduce(sum));
  1118. end;
  1119. function TFpFieldElement.Subtract(const b: IECFieldElement): IECFieldElement;
  1120. begin
  1121. result := TFpFieldElement.Create(Fq, Fr, ModSubtract(Fx, b.ToBigInteger()));
  1122. end;
  1123. function TFpFieldElement.ToBigInteger: TBigInteger;
  1124. begin
  1125. result := Fx;
  1126. end;
  1127. end.