ClpECFieldElement.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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, temp: 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. temp := Vl.Multiply(Vl).Subtract(Ql.ShiftLeft(1));
  842. Vl := ModReduce(temp);
  843. Ql := ModMult(Ql, Ql);
  844. System.Inc(j);
  845. end;
  846. result := TCryptoLibGenericArray<TBigInteger>.Create(Uh, Vl);
  847. end;
  848. function TFpFieldElement.ModAdd(const x1, x2: TBigInteger): TBigInteger;
  849. var
  850. x3: TBigInteger;
  851. begin
  852. x3 := x1.Add(x2);
  853. if (x3.CompareTo(Q) >= 0) then
  854. begin
  855. x3 := x3.Subtract(Q);
  856. end;
  857. result := x3;
  858. end;
  859. function TFpFieldElement.ModDouble(const x: TBigInteger): TBigInteger;
  860. var
  861. _2x: TBigInteger;
  862. begin
  863. _2x := x.ShiftLeft(1);
  864. if (_2x.CompareTo(Q) >= 0) then
  865. begin
  866. _2x := _2x.Subtract(Q);
  867. end;
  868. result := _2x;
  869. end;
  870. function TFpFieldElement.ModHalf(x: TBigInteger): TBigInteger;
  871. begin
  872. if (x.TestBit(0)) then
  873. begin
  874. x := Q.Add(x);
  875. end;
  876. result := x.ShiftRight(1);
  877. end;
  878. function TFpFieldElement.ModHalfAbs(x: TBigInteger): TBigInteger;
  879. begin
  880. if (x.TestBit(0)) then
  881. begin
  882. x := Q.Subtract(x);
  883. end;
  884. result := x.ShiftRight(1);
  885. end;
  886. function TFpFieldElement.ModInverse(const x: TBigInteger): TBigInteger;
  887. var
  888. bits, len: Int32;
  889. P, n, z: TCryptoLibUInt32Array;
  890. begin
  891. bits := FieldSize;
  892. len := TBits.Asr32((bits + 31), 5);
  893. P := TNat.FromBigInteger(bits, Q);
  894. n := TNat.FromBigInteger(bits, x);
  895. z := TNat.Create(len);
  896. TMod.Invert(P, n, z);
  897. result := TNat.ToBigInteger(len, z);
  898. end;
  899. function TFpFieldElement.ModMult(const x1, x2: TBigInteger): TBigInteger;
  900. begin
  901. result := ModReduce(x1.Multiply(x2));
  902. end;
  903. function TFpFieldElement.ModReduce(x: TBigInteger): TBigInteger;
  904. var
  905. negative, rIsOne: Boolean;
  906. qLen, d: Int32;
  907. qMod, u, v, mu, quot, bk1: TBigInteger;
  908. begin
  909. if (not(Fr.IsInitialized)) then
  910. begin
  911. x := x.&Mod(Q);
  912. end
  913. else
  914. begin
  915. negative := x.SignValue < 0;
  916. if (negative) then
  917. begin
  918. x := x.Abs();
  919. end;
  920. qLen := Q.BitLength;
  921. if (Fr.SignValue > 0) then
  922. begin
  923. qMod := TBigInteger.One.ShiftLeft(qLen);
  924. rIsOne := Fr.Equals(TBigInteger.One);
  925. while (x.BitLength > (qLen + 1)) do
  926. begin
  927. u := x.ShiftRight(qLen);
  928. v := x.Remainder(qMod);
  929. if (not rIsOne) then
  930. begin
  931. u := u.Multiply(Fr);
  932. end;
  933. x := u.Add(v);
  934. end
  935. end
  936. else
  937. begin
  938. d := ((qLen - 1) and 31) + 1;
  939. mu := Fr.Negate();
  940. u := mu.Multiply(x.ShiftRight(qLen - d));
  941. quot := u.ShiftRight(qLen + d);
  942. v := quot.Multiply(Q);
  943. bk1 := TBigInteger.One.ShiftLeft(qLen + d);
  944. v := v.Remainder(bk1);
  945. x := x.Remainder(bk1);
  946. x := x.Subtract(v);
  947. if (x.SignValue < 0) then
  948. begin
  949. x := x.Add(bk1);
  950. end
  951. end;
  952. while (x.CompareTo(Q) >= 0) do
  953. begin
  954. x := x.Subtract(Q);
  955. end;
  956. if ((negative) and (x.SignValue <> 0)) then
  957. begin
  958. x := Q.Subtract(x);
  959. end;
  960. end;
  961. result := x;
  962. end;
  963. function TFpFieldElement.ModSubtract(const x1, x2: TBigInteger): TBigInteger;
  964. var
  965. x3: TBigInteger;
  966. begin
  967. x3 := x1.Subtract(x2);
  968. if (x3.SignValue < 0) then
  969. begin
  970. x3 := x3.Add(Q);
  971. end;
  972. result := x3;
  973. end;
  974. function TFpFieldElement.Multiply(const b: IECFieldElement): IECFieldElement;
  975. begin
  976. result := TFpFieldElement.Create(Fq, Fr, ModMult(Fx, b.ToBigInteger()));
  977. end;
  978. function TFpFieldElement.MultiplyMinusProduct(const b, x, y: IECFieldElement)
  979. : IECFieldElement;
  980. var
  981. ax, bx, xx, yx, ab, xy: TBigInteger;
  982. begin
  983. ax := Fx;
  984. bx := b.ToBigInteger();
  985. xx := x.ToBigInteger();
  986. yx := y.ToBigInteger();
  987. ab := ax.Multiply(bx);
  988. xy := xx.Multiply(yx);
  989. result := TFpFieldElement.Create(Fq, Fr, ModReduce(ab.Subtract(xy)));
  990. end;
  991. function TFpFieldElement.MultiplyPlusProduct(const b, x, y: IECFieldElement)
  992. : IECFieldElement;
  993. var
  994. ax, bx, xx, yx, ab, xy, sum: TBigInteger;
  995. begin
  996. ax := Fx;
  997. bx := b.ToBigInteger();
  998. xx := x.ToBigInteger();
  999. yx := y.ToBigInteger();
  1000. ab := ax.Multiply(bx);
  1001. xy := xx.Multiply(yx);
  1002. sum := ab.Add(xy);
  1003. if ((Fr.IsInitialized) and (Fr.SignValue < 0) and
  1004. (sum.BitLength > (Fq.BitLength shl 1))) then
  1005. begin
  1006. sum := sum.Subtract(Fq.ShiftLeft(Q.BitLength));
  1007. end;
  1008. result := TFpFieldElement.Create(Fq, Fr, ModReduce(sum));
  1009. end;
  1010. function TFpFieldElement.Negate: IECFieldElement;
  1011. begin
  1012. if Fx.SignValue = 0 then
  1013. begin
  1014. result := Self as IECFieldElement
  1015. end
  1016. else
  1017. begin
  1018. result := TFpFieldElement.Create(Fq, Fr, Fq.Subtract(Fx));
  1019. end;
  1020. end;
  1021. function TFpFieldElement.Sqrt: IECFieldElement;
  1022. var
  1023. u, v, K, e, t1, t2, t3, t4, y, legendreExponent, x, fourX, qMinusOne,
  1024. P: TBigInteger;
  1025. tempRes: TCryptoLibGenericArray<TBigInteger>;
  1026. begin
  1027. if (IsZero or IsOne) then
  1028. begin
  1029. result := Self as IECFieldElement;
  1030. Exit;
  1031. end;
  1032. if (not Fq.TestBit(0)) then
  1033. begin
  1034. raise ENotImplementedCryptoLibException.CreateRes(@SEvenValue);
  1035. end;
  1036. if (Fq.TestBit(1)) then // q == 4m + 3
  1037. begin
  1038. e := Fq.ShiftRight(2).Add(TBigInteger.One);
  1039. result := CheckSqrt(TFpFieldElement.Create(Fq, Fr, Fx.ModPow(e, Fq))
  1040. as IFpFieldElement);
  1041. Exit;
  1042. end;
  1043. if (Fq.TestBit(2)) then // q == 8m + 5
  1044. begin
  1045. t1 := Fx.ModPow(Fq.ShiftRight(3), Fq);
  1046. t2 := ModMult(t1, Fx);
  1047. t3 := ModMult(t2, t1);
  1048. if (t3.Equals(TBigInteger.One)) then
  1049. begin
  1050. result := CheckSqrt(TFpFieldElement.Create(Fq, Fr, t2)
  1051. as IFpFieldElement);
  1052. Exit;
  1053. end;
  1054. // TODO This is constant and could be precomputed
  1055. t4 := TBigInteger.Two.ModPow(Fq.ShiftRight(2), Fq);
  1056. y := ModMult(t2, t4);
  1057. result := CheckSqrt(TFpFieldElement.Create(Fq, Fr, y) as IFpFieldElement);
  1058. end;
  1059. // q == 8m + 1
  1060. legendreExponent := Fq.ShiftRight(1);
  1061. if (not(Fx.ModPow(legendreExponent, Fq).Equals(TBigInteger.One))) then
  1062. begin
  1063. result := Nil;
  1064. Exit;
  1065. end;
  1066. x := Fx;
  1067. fourX := ModDouble(ModDouble(x));
  1068. K := legendreExponent.Add(TBigInteger.One);
  1069. qMinusOne := Fq.Subtract(TBigInteger.One);
  1070. repeat
  1071. repeat
  1072. P := TBigInteger.Arbitrary(Fq.BitLength);
  1073. until ((not P.CompareTo(Q) >= 0) or (ModReduce(P.Multiply(P).Subtract(fourX)
  1074. ).ModPow(legendreExponent, Q).Equals(qMinusOne)));
  1075. tempRes := LucasSequence(P, x, K);
  1076. u := tempRes[0];
  1077. v := tempRes[1];
  1078. if (ModMult(v, v).Equals(fourX)) then
  1079. begin
  1080. result := TFpFieldElement.Create(Fq, Fr, ModHalfAbs(v));
  1081. Exit;
  1082. end;
  1083. until ((not u.Equals(TBigInteger.One)) or (not u.Equals(qMinusOne)));
  1084. result := Nil;
  1085. end;
  1086. function TFpFieldElement.Square: IECFieldElement;
  1087. begin
  1088. result := TFpFieldElement.Create(Fq, Fr, ModMult(Fx, Fx));
  1089. end;
  1090. function TFpFieldElement.SquareMinusProduct(const x, y: IECFieldElement)
  1091. : IECFieldElement;
  1092. var
  1093. ax, xx, yx, aa, xy: TBigInteger;
  1094. begin
  1095. ax := Fx;
  1096. xx := x.ToBigInteger();
  1097. yx := y.ToBigInteger();
  1098. aa := ax.Multiply(ax);
  1099. xy := xx.Multiply(yx);
  1100. result := TFpFieldElement.Create(Fq, Fr, ModReduce(aa.Subtract(xy)));
  1101. end;
  1102. function TFpFieldElement.SquarePlusProduct(const x, y: IECFieldElement)
  1103. : IECFieldElement;
  1104. var
  1105. ax, xx, yx, aa, xy, sum: TBigInteger;
  1106. begin
  1107. ax := Fx;
  1108. xx := x.ToBigInteger();
  1109. yx := y.ToBigInteger();
  1110. aa := ax.Multiply(ax);
  1111. xy := xx.Multiply(yx);
  1112. sum := aa.Add(xy);
  1113. if ((Fr.IsInitialized) and (Fr.SignValue < 0) and
  1114. (sum.BitLength > (Fq.BitLength shl 1))) then
  1115. begin
  1116. sum := sum.Subtract(Fq.ShiftLeft(Fq.BitLength));
  1117. end;
  1118. result := TFpFieldElement.Create(Fq, Fr, ModReduce(sum));
  1119. end;
  1120. function TFpFieldElement.Subtract(const b: IECFieldElement): IECFieldElement;
  1121. begin
  1122. result := TFpFieldElement.Create(Fq, Fr, ModSubtract(Fx, b.ToBigInteger()));
  1123. end;
  1124. function TFpFieldElement.ToBigInteger: TBigInteger;
  1125. begin
  1126. result := Fx;
  1127. end;
  1128. end.