Ed448.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc7748;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032
  12. {
  13. public abstract class Ed448
  14. {
  15. public enum Algorithm
  16. {
  17. Ed448 = 0,
  18. Ed448ph = 1,
  19. }
  20. private const ulong M26UL = 0x03FFFFFFUL;
  21. private const ulong M28UL = 0x0FFFFFFFUL;
  22. private const int PointBytes = 57;
  23. private const int ScalarUints = 14;
  24. private const int ScalarBytes = ScalarUints * 4 + 1;
  25. public static readonly int PrehashSize = 64;
  26. public static readonly int PublicKeySize = PointBytes;
  27. public static readonly int SecretKeySize = 57;
  28. public static readonly int SignatureSize = PointBytes + ScalarBytes;
  29. private static readonly byte[] Dom4Prefix = Strings.ToByteArray("SigEd448");
  30. private static readonly uint[] P = { 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU,
  31. 0xFFFFFFFEU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU };
  32. private static readonly uint[] L = { 0xAB5844F3U, 0x2378C292U, 0x8DC58F55U, 0x216CC272U, 0xAED63690U, 0xC44EDB49U, 0x7CCA23E9U,
  33. 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0x3FFFFFFFU };
  34. private static readonly BigInteger N = Nat.ToBigInteger(L.Length, L);
  35. private const int L_0 = 0x04A7BB0D; // L_0:26/24
  36. private const int L_1 = 0x0873D6D5; // L_1:27/23
  37. private const int L_2 = 0x0A70AADC; // L_2:27/26
  38. private const int L_3 = 0x03D8D723; // L_3:26/--
  39. private const int L_4 = 0x096FDE93; // L_4:27/25
  40. private const int L_5 = 0x0B65129C; // L_5:27/26
  41. private const int L_6 = 0x063BB124; // L_6:27/--
  42. private const int L_7 = 0x08335DC1; // L_7:27/22
  43. private const int L4_0 = 0x029EEC34; // L4_0:25/24
  44. private const int L4_1 = 0x01CF5B55; // L4_1:25/--
  45. private const int L4_2 = 0x09C2AB72; // L4_2:27/25
  46. private const int L4_3 = 0x0F635C8E; // L4_3:28/--
  47. private const int L4_4 = 0x05BF7A4C; // L4_4:26/25
  48. private const int L4_5 = 0x0D944A72; // L4_5:28/--
  49. private const int L4_6 = 0x08EEC492; // L4_6:27/24
  50. private const int L4_7 = 0x20CD7705; // L4_7:29/24
  51. private static readonly uint[] B_x = { 0x070CC05EU, 0x026A82BCU, 0x00938E26U, 0x080E18B0U, 0x0511433BU, 0x0F72AB66U, 0x0412AE1AU,
  52. 0x0A3D3A46U, 0x0A6DE324U, 0x00F1767EU, 0x04657047U, 0x036DA9E1U, 0x05A622BFU, 0x0ED221D1U, 0x066BED0DU, 0x04F1970CU };
  53. private static readonly uint[] B_y = { 0x0230FA14U, 0x008795BFU, 0x07C8AD98U, 0x0132C4EDU, 0x09C4FDBDU, 0x01CE67C3U, 0x073AD3FFU,
  54. 0x005A0C2DU, 0x07789C1EU, 0x0A398408U, 0x0A73736CU, 0x0C7624BEU, 0x003756C9U, 0x02488762U, 0x016EB6BCU, 0x0693F467U };
  55. private const int C_d = -39081;
  56. private const int WnafWidthBase = 7;
  57. private const int PrecompBlocks = 5;
  58. private const int PrecompTeeth = 5;
  59. private const int PrecompSpacing = 18;
  60. private const int PrecompPoints = 1 << (PrecompTeeth - 1);
  61. private const int PrecompMask = PrecompPoints - 1;
  62. private static readonly object precompLock = new object();
  63. // TODO[ed448] Convert to PointPrecomp
  64. private static PointExt[] precompBaseTable = null;
  65. private static uint[] precompBase = null;
  66. private class PointExt
  67. {
  68. internal uint[] x = X448Field.Create();
  69. internal uint[] y = X448Field.Create();
  70. internal uint[] z = X448Field.Create();
  71. }
  72. private class PointPrecomp
  73. {
  74. internal uint[] x = X448Field.Create();
  75. internal uint[] y = X448Field.Create();
  76. }
  77. private static byte[] CalculateS(byte[] r, byte[] k, byte[] s)
  78. {
  79. uint[] t = new uint[ScalarUints * 2]; DecodeScalar(r, 0, t);
  80. uint[] u = new uint[ScalarUints]; DecodeScalar(k, 0, u);
  81. uint[] v = new uint[ScalarUints]; DecodeScalar(s, 0, v);
  82. Nat.MulAddTo(14, u, v, t);
  83. byte[] result = new byte[ScalarBytes * 2];
  84. for (int i = 0; i < t.Length; ++i)
  85. {
  86. Encode32(t[i], result, i * 4);
  87. }
  88. return ReduceScalar(result);
  89. }
  90. private static bool CheckContextVar(byte[] ctx)
  91. {
  92. return ctx != null && ctx.Length < 256;
  93. }
  94. private static bool CheckPointVar(byte[] p)
  95. {
  96. if ((p[PointBytes - 1] & 0x7F) != 0x00)
  97. return false;
  98. uint[] t = new uint[14];
  99. Decode32(p, 0, t, 0, 14);
  100. return !Nat.Gte(14, t, P);
  101. }
  102. private static bool CheckScalarVar(byte[] s)
  103. {
  104. if (s[ScalarBytes - 1] != 0x00)
  105. return false;
  106. uint[] n = new uint[ScalarUints];
  107. DecodeScalar(s, 0, n);
  108. return !Nat.Gte(ScalarUints, n, L);
  109. }
  110. public static IXof CreatePrehash()
  111. {
  112. return CreateXof();
  113. }
  114. private static IXof CreateXof()
  115. {
  116. return new ShakeDigest(256);
  117. }
  118. private static uint Decode16(byte[] bs, int off)
  119. {
  120. uint n = bs[off];
  121. n |= (uint)bs[++off] << 8;
  122. return n;
  123. }
  124. private static uint Decode24(byte[] bs, int off)
  125. {
  126. uint n = bs[off];
  127. n |= (uint)bs[++off] << 8;
  128. n |= (uint)bs[++off] << 16;
  129. return n;
  130. }
  131. private static uint Decode32(byte[] bs, int off)
  132. {
  133. uint n = bs[off];
  134. n |= (uint)bs[++off] << 8;
  135. n |= (uint)bs[++off] << 16;
  136. n |= (uint)bs[++off] << 24;
  137. return n;
  138. }
  139. private static void Decode32(byte[] bs, int bsOff, uint[] n, int nOff, int nLen)
  140. {
  141. for (int i = 0; i < nLen; ++i)
  142. {
  143. n[nOff + i] = Decode32(bs, bsOff + i * 4);
  144. }
  145. }
  146. private static bool DecodePointVar(byte[] p, int pOff, bool negate, PointExt r)
  147. {
  148. byte[] py = Arrays.CopyOfRange(p, pOff, pOff + PointBytes);
  149. if (!CheckPointVar(py))
  150. return false;
  151. int x_0 = (py[PointBytes - 1] & 0x80) >> 7;
  152. py[PointBytes - 1] &= 0x7F;
  153. X448Field.Decode(py, 0, r.y);
  154. uint[] u = X448Field.Create();
  155. uint[] v = X448Field.Create();
  156. X448Field.Sqr(r.y, u);
  157. X448Field.Mul(u, (uint)-C_d, v);
  158. X448Field.Negate(u, u);
  159. X448Field.AddOne(u);
  160. X448Field.AddOne(v);
  161. if (!X448Field.SqrtRatioVar(u, v, r.x))
  162. return false;
  163. X448Field.Normalize(r.x);
  164. if (x_0 == 1 && X448Field.IsZeroVar(r.x))
  165. return false;
  166. if (negate ^ (x_0 != (r.x[0] & 1)))
  167. {
  168. X448Field.Negate(r.x, r.x);
  169. }
  170. PointExtendXY(r);
  171. return true;
  172. }
  173. private static void DecodeScalar(byte[] k, int kOff, uint[] n)
  174. {
  175. Debug.Assert(k[kOff + ScalarBytes - 1] == 0x00);
  176. Decode32(k, kOff, n, 0, ScalarUints);
  177. }
  178. private static void Dom4(IXof d, byte x, byte[] y)
  179. {
  180. d.BlockUpdate(Dom4Prefix, 0, Dom4Prefix.Length);
  181. d.Update(x);
  182. d.Update((byte)y.Length);
  183. d.BlockUpdate(y, 0, y.Length);
  184. }
  185. private static void Encode24(uint n, byte[] bs, int off)
  186. {
  187. bs[off] = (byte)(n);
  188. bs[++off] = (byte)(n >> 8);
  189. bs[++off] = (byte)(n >> 16);
  190. }
  191. private static void Encode32(uint n, byte[] bs, int off)
  192. {
  193. bs[off] = (byte)(n);
  194. bs[++off] = (byte)(n >> 8);
  195. bs[++off] = (byte)(n >> 16);
  196. bs[++off] = (byte)(n >> 24);
  197. }
  198. private static void Encode56(ulong n, byte[] bs, int off)
  199. {
  200. Encode32((uint)n, bs, off);
  201. Encode24((uint)(n >> 32), bs, off + 4);
  202. }
  203. private static void EncodePoint(PointExt p, byte[] r, int rOff)
  204. {
  205. uint[] x = X448Field.Create();
  206. uint[] y = X448Field.Create();
  207. X448Field.Inv(p.z, y);
  208. X448Field.Mul(p.x, y, x);
  209. X448Field.Mul(p.y, y, y);
  210. X448Field.Normalize(x);
  211. X448Field.Normalize(y);
  212. X448Field.Encode(y, r, rOff);
  213. r[rOff + PointBytes - 1] = (byte)((x[0] & 1) << 7);
  214. }
  215. public static void GeneratePrivateKey(SecureRandom random, byte[] k)
  216. {
  217. random.NextBytes(k);
  218. }
  219. public static void GeneratePublicKey(byte[] sk, int skOff, byte[] pk, int pkOff)
  220. {
  221. IXof d = CreateXof();
  222. byte[] h = new byte[ScalarBytes * 2];
  223. d.BlockUpdate(sk, skOff, SecretKeySize);
  224. d.DoFinal(h, 0, h.Length);
  225. byte[] s = new byte[ScalarBytes];
  226. PruneScalar(h, 0, s);
  227. ScalarMultBaseEncoded(s, pk, pkOff);
  228. }
  229. private static sbyte[] GetWnaf(uint[] n, int width)
  230. {
  231. Debug.Assert(n[ScalarUints - 1] >> 31 == 0U);
  232. uint[] t = new uint[ScalarUints * 2];
  233. {
  234. uint c = 0;
  235. int tPos = t.Length, i = ScalarUints;
  236. while (--i >= 0)
  237. {
  238. uint next = n[i];
  239. t[--tPos] = (next >> 16) | (c << 16);
  240. t[--tPos] = c = next;
  241. }
  242. }
  243. sbyte[] ws = new sbyte[448];
  244. uint pow2 = 1U << width;
  245. uint mask = pow2 - 1U;
  246. uint sign = pow2 >> 1;
  247. uint carry = 0U;
  248. int j = 0;
  249. for (int i = 0; i < t.Length; ++i, j -= 16)
  250. {
  251. uint word = t[i];
  252. while (j < 16)
  253. {
  254. uint word16 = word >> j;
  255. uint bit = word16 & 1U;
  256. if (bit == carry)
  257. {
  258. ++j;
  259. continue;
  260. }
  261. uint digit = (word16 & mask) + carry;
  262. carry = digit & sign;
  263. digit -= (carry << 1);
  264. carry >>= (width - 1);
  265. ws[(i << 4) + j] = (sbyte)digit;
  266. j += width;
  267. }
  268. }
  269. Debug.Assert(carry == 0);
  270. return ws;
  271. }
  272. private static void ImplSign(IXof d, byte[] h, byte[] s, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  273. byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  274. {
  275. Dom4(d, phflag, ctx);
  276. d.BlockUpdate(h, ScalarBytes, ScalarBytes);
  277. d.BlockUpdate(m, mOff, mLen);
  278. d.DoFinal(h, 0, h.Length);
  279. byte[] r = ReduceScalar(h);
  280. byte[] R = new byte[PointBytes];
  281. ScalarMultBaseEncoded(r, R, 0);
  282. Dom4(d, phflag, ctx);
  283. d.BlockUpdate(R, 0, PointBytes);
  284. d.BlockUpdate(pk, pkOff, PointBytes);
  285. d.BlockUpdate(m, mOff, mLen);
  286. d.DoFinal(h, 0, h.Length);
  287. byte[] k = ReduceScalar(h);
  288. byte[] S = CalculateS(r, k, s);
  289. Array.Copy(R, 0, sig, sigOff, PointBytes);
  290. Array.Copy(S, 0, sig, sigOff + PointBytes, ScalarBytes);
  291. }
  292. private static void ImplSign(byte[] sk, int skOff, byte[] ctx, byte phflag, byte[] m, int mOff, int mLen,
  293. byte[] sig, int sigOff)
  294. {
  295. if (!CheckContextVar(ctx))
  296. throw new ArgumentException("ctx");
  297. IXof d = CreateXof();
  298. byte[] h = new byte[ScalarBytes * 2];
  299. d.BlockUpdate(sk, skOff, SecretKeySize);
  300. d.DoFinal(h, 0, h.Length);
  301. byte[] s = new byte[ScalarBytes];
  302. PruneScalar(h, 0, s);
  303. byte[] pk = new byte[PointBytes];
  304. ScalarMultBaseEncoded(s, pk, 0);
  305. ImplSign(d, h, s, pk, 0, ctx, phflag, m, mOff, mLen, sig, sigOff);
  306. }
  307. private static void ImplSign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  308. byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  309. {
  310. if (!CheckContextVar(ctx))
  311. throw new ArgumentException("ctx");
  312. IXof d = CreateXof();
  313. byte[] h = new byte[ScalarBytes * 2];
  314. d.BlockUpdate(sk, skOff, SecretKeySize);
  315. d.DoFinal(h, 0, h.Length);
  316. byte[] s = new byte[ScalarBytes];
  317. PruneScalar(h, 0, s);
  318. ImplSign(d, h, s, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  319. }
  320. private static bool ImplVerify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  321. byte[] m, int mOff, int mLen)
  322. {
  323. if (!CheckContextVar(ctx))
  324. throw new ArgumentException("ctx");
  325. byte[] R = Arrays.CopyOfRange(sig, sigOff, sigOff + PointBytes);
  326. byte[] S = Arrays.CopyOfRange(sig, sigOff + PointBytes, sigOff + SignatureSize);
  327. if (!CheckPointVar(R))
  328. return false;
  329. if (!CheckScalarVar(S))
  330. return false;
  331. PointExt pA = new PointExt();
  332. if (!DecodePointVar(pk, pkOff, true, pA))
  333. return false;
  334. IXof d = CreateXof();
  335. byte[] h = new byte[ScalarBytes * 2];
  336. Dom4(d, phflag, ctx);
  337. d.BlockUpdate(R, 0, PointBytes);
  338. d.BlockUpdate(pk, pkOff, PointBytes);
  339. d.BlockUpdate(m, mOff, mLen);
  340. d.DoFinal(h, 0, h.Length);
  341. byte[] k = ReduceScalar(h);
  342. uint[] nS = new uint[ScalarUints];
  343. DecodeScalar(S, 0, nS);
  344. uint[] nA = new uint[ScalarUints];
  345. DecodeScalar(k, 0, nA);
  346. PointExt pR = new PointExt();
  347. ScalarMultStraussVar(nS, nA, pA, pR);
  348. byte[] check = new byte[PointBytes];
  349. EncodePoint(pR, check, 0);
  350. return Arrays.AreEqual(check, R);
  351. }
  352. private static void PointAddVar(bool negate, PointExt p, PointExt r)
  353. {
  354. uint[] A = X448Field.Create();
  355. uint[] B = X448Field.Create();
  356. uint[] C = X448Field.Create();
  357. uint[] D = X448Field.Create();
  358. uint[] E = X448Field.Create();
  359. uint[] F = X448Field.Create();
  360. uint[] G = X448Field.Create();
  361. uint[] H = X448Field.Create();
  362. uint[] b, e, f, g;
  363. if (negate)
  364. {
  365. b = E; e = B; f = G; g = F;
  366. X448Field.Sub(p.y, p.x, H);
  367. }
  368. else
  369. {
  370. b = B; e = E; f = F; g = G;
  371. X448Field.Add(p.y, p.x, H);
  372. }
  373. X448Field.Mul(p.z, r.z, A);
  374. X448Field.Sqr(A, B);
  375. X448Field.Mul(p.x, r.x, C);
  376. X448Field.Mul(p.y, r.y, D);
  377. X448Field.Mul(C, D, E);
  378. X448Field.Mul(E, -C_d, E);
  379. //X448Field.Apm(B, E, F, G);
  380. X448Field.Add(B, E, f);
  381. X448Field.Sub(B, E, g);
  382. X448Field.Add(r.x, r.y, E);
  383. X448Field.Mul(H, E, H);
  384. //X448Field.Apm(D, C, B, E);
  385. X448Field.Add(D, C, b);
  386. X448Field.Sub(D, C, e);
  387. X448Field.Carry(b);
  388. X448Field.Sub(H, B, H);
  389. X448Field.Mul(H, A, H);
  390. X448Field.Mul(E, A, E);
  391. X448Field.Mul(F, H, r.x);
  392. X448Field.Mul(E, G, r.y);
  393. X448Field.Mul(F, G, r.z);
  394. }
  395. private static void PointAddPrecomp(PointPrecomp p, PointExt r)
  396. {
  397. uint[] B = X448Field.Create();
  398. uint[] C = X448Field.Create();
  399. uint[] D = X448Field.Create();
  400. uint[] E = X448Field.Create();
  401. uint[] F = X448Field.Create();
  402. uint[] G = X448Field.Create();
  403. uint[] H = X448Field.Create();
  404. X448Field.Sqr(r.z, B);
  405. X448Field.Mul(p.x, r.x, C);
  406. X448Field.Mul(p.y, r.y, D);
  407. X448Field.Mul(C, D, E);
  408. X448Field.Mul(E, -C_d, E);
  409. //X448Field.Apm(B, E, F, G);
  410. X448Field.Add(B, E, F);
  411. X448Field.Sub(B, E, G);
  412. X448Field.Add(p.x, p.y, B);
  413. X448Field.Add(r.x, r.y, E);
  414. X448Field.Mul(B, E, H);
  415. //X448Field.Apm(D, C, B, E);
  416. X448Field.Add(D, C, B);
  417. X448Field.Sub(D, C, E);
  418. X448Field.Carry(B);
  419. X448Field.Sub(H, B, H);
  420. X448Field.Mul(H, r.z, H);
  421. X448Field.Mul(E, r.z, E);
  422. X448Field.Mul(F, H, r.x);
  423. X448Field.Mul(E, G, r.y);
  424. X448Field.Mul(F, G, r.z);
  425. }
  426. private static PointExt PointCopy(PointExt p)
  427. {
  428. PointExt r = new PointExt();
  429. X448Field.Copy(p.x, 0, r.x, 0);
  430. X448Field.Copy(p.y, 0, r.y, 0);
  431. X448Field.Copy(p.z, 0, r.z, 0);
  432. return r;
  433. }
  434. private static void PointDouble(PointExt r)
  435. {
  436. uint[] B = X448Field.Create();
  437. uint[] C = X448Field.Create();
  438. uint[] D = X448Field.Create();
  439. uint[] E = X448Field.Create();
  440. uint[] H = X448Field.Create();
  441. uint[] J = X448Field.Create();
  442. X448Field.Add(r.x, r.y, B);
  443. X448Field.Sqr(B, B);
  444. X448Field.Sqr(r.x, C);
  445. X448Field.Sqr(r.y, D);
  446. X448Field.Add(C, D, E);
  447. X448Field.Carry(E);
  448. X448Field.Sqr(r.z, H);
  449. X448Field.Add(H, H, H);
  450. X448Field.Carry(H);
  451. X448Field.Sub(E, H, J);
  452. X448Field.Sub(B, E, B);
  453. X448Field.Sub(C, D, C);
  454. X448Field.Mul(B, J, r.x);
  455. X448Field.Mul(E, C, r.y);
  456. X448Field.Mul(E, J, r.z);
  457. }
  458. private static void PointExtendXY(PointExt p)
  459. {
  460. X448Field.One(p.z);
  461. }
  462. private static void PointLookup(int block, int index, PointPrecomp p)
  463. {
  464. Debug.Assert(0 <= block && block < PrecompBlocks);
  465. Debug.Assert(0 <= index && index < PrecompPoints);
  466. int off = block * PrecompPoints * 2 * X448Field.Size;
  467. for (int i = 0; i < PrecompPoints; ++i)
  468. {
  469. int mask = ((i ^ index) - 1) >> 31;
  470. Nat.CMov(X448Field.Size, mask, precompBase, off, p.x, 0); off += X448Field.Size;
  471. Nat.CMov(X448Field.Size, mask, precompBase, off, p.y, 0); off += X448Field.Size;
  472. }
  473. }
  474. private static PointExt[] PointPrecompVar(PointExt p, int count)
  475. {
  476. Debug.Assert(count > 0);
  477. PointExt d = PointCopy(p);
  478. PointDouble(d);
  479. PointExt[] table = new PointExt[count];
  480. table[0] = PointCopy(p);
  481. for (int i = 1; i < count; ++i)
  482. {
  483. table[i] = PointCopy(table[i - 1]);
  484. PointAddVar(false, d, table[i]);
  485. }
  486. return table;
  487. }
  488. private static void PointSetNeutral(PointExt p)
  489. {
  490. X448Field.Zero(p.x);
  491. X448Field.One(p.y);
  492. X448Field.One(p.z);
  493. }
  494. public static void Precompute()
  495. {
  496. lock (precompLock)
  497. {
  498. if (precompBase != null)
  499. return;
  500. PointExt p = new PointExt();
  501. X448Field.Copy(B_x, 0, p.x, 0);
  502. X448Field.Copy(B_y, 0, p.y, 0);
  503. PointExtendXY(p);
  504. precompBaseTable = PointPrecompVar(p, 1 << (WnafWidthBase - 2));
  505. precompBase = new uint[PrecompBlocks * PrecompPoints * 2 * X448Field.Size];
  506. int off = 0;
  507. for (int b = 0; b < PrecompBlocks; ++b)
  508. {
  509. PointExt[] ds = new PointExt[PrecompTeeth];
  510. PointExt sum = new PointExt();
  511. PointSetNeutral(sum);
  512. for (int t = 0; t < PrecompTeeth; ++t)
  513. {
  514. PointAddVar(true, p, sum);
  515. PointDouble(p);
  516. ds[t] = PointCopy(p);
  517. if (b + t != PrecompBlocks + PrecompTeeth - 2)
  518. {
  519. for (int s = 1; s < PrecompSpacing; ++s)
  520. {
  521. PointDouble(p);
  522. }
  523. }
  524. }
  525. PointExt[] points = new PointExt[PrecompPoints];
  526. int k = 0;
  527. points[k++] = sum;
  528. for (int t = 0; t < (PrecompTeeth - 1); ++t)
  529. {
  530. int size = 1 << t;
  531. for (int j = 0; j < size; ++j, ++k)
  532. {
  533. points[k] = PointCopy(points[k - size]);
  534. PointAddVar(false, ds[t], points[k]);
  535. }
  536. }
  537. Debug.Assert(k == PrecompPoints);
  538. for (int i = 0; i < PrecompPoints; ++i)
  539. {
  540. PointExt q = points[i];
  541. // TODO[ed448] Batch inversion
  542. X448Field.Inv(q.z, q.z);
  543. X448Field.Mul(q.x, q.z, q.x);
  544. X448Field.Mul(q.y, q.z, q.y);
  545. //X448Field.Normalize(q.x);
  546. //X448Field.Normalize(q.y);
  547. X448Field.Copy(q.x, 0, precompBase, off); off += X448Field.Size;
  548. X448Field.Copy(q.y, 0, precompBase, off); off += X448Field.Size;
  549. }
  550. }
  551. Debug.Assert(off == precompBase.Length);
  552. }
  553. }
  554. private static void PruneScalar(byte[] n, int nOff, byte[] r)
  555. {
  556. Array.Copy(n, nOff, r, 0, ScalarBytes - 1);
  557. r[0] &= 0xFC;
  558. r[ScalarBytes - 2] |= 0x80;
  559. r[ScalarBytes - 1] = 0x00;
  560. }
  561. private static byte[] ReduceScalar(byte[] n)
  562. {
  563. ulong x00 = Decode32(n, 0); // x00:32/--
  564. ulong x01 = (Decode24(n, 4) << 4); // x01:28/--
  565. ulong x02 = Decode32(n, 7); // x02:32/--
  566. ulong x03 = (Decode24(n, 11) << 4); // x03:28/--
  567. ulong x04 = Decode32(n, 14); // x04:32/--
  568. ulong x05 = (Decode24(n, 18) << 4); // x05:28/--
  569. ulong x06 = Decode32(n, 21); // x06:32/--
  570. ulong x07 = (Decode24(n, 25) << 4); // x07:28/--
  571. ulong x08 = Decode32(n, 28); // x08:32/--
  572. ulong x09 = (Decode24(n, 32) << 4); // x09:28/--
  573. ulong x10 = Decode32(n, 35); // x10:32/--
  574. ulong x11 = (Decode24(n, 39) << 4); // x11:28/--
  575. ulong x12 = Decode32(n, 42); // x12:32/--
  576. ulong x13 = (Decode24(n, 46) << 4); // x13:28/--
  577. ulong x14 = Decode32(n, 49); // x14:32/--
  578. ulong x15 = (Decode24(n, 53) << 4); // x15:28/--
  579. ulong x16 = Decode32(n, 56); // x16:32/--
  580. ulong x17 = (Decode24(n, 60) << 4); // x17:28/--
  581. ulong x18 = Decode32(n, 63); // x18:32/--
  582. ulong x19 = (Decode24(n, 67) << 4); // x19:28/--
  583. ulong x20 = Decode32(n, 70); // x20:32/--
  584. ulong x21 = (Decode24(n, 74) << 4); // x21:28/--
  585. ulong x22 = Decode32(n, 77); // x22:32/--
  586. ulong x23 = (Decode24(n, 81) << 4); // x23:28/--
  587. ulong x24 = Decode32(n, 84); // x24:32/--
  588. ulong x25 = (Decode24(n, 88) << 4); // x25:28/--
  589. ulong x26 = Decode32(n, 91); // x26:32/--
  590. ulong x27 = (Decode24(n, 95) << 4); // x27:28/--
  591. ulong x28 = Decode32(n, 98); // x28:32/--
  592. ulong x29 = (Decode24(n, 102) << 4); // x29:28/--
  593. ulong x30 = Decode32(n, 105); // x30:32/--
  594. ulong x31 = (Decode24(n, 109) << 4); // x31:28/--
  595. ulong x32 = Decode16(n, 112); // x32:16/--
  596. // x32 += (x31 >> 28); x31 &= M28UL;
  597. x16 += x32 * L4_0; // x16:42/--
  598. x17 += x32 * L4_1; // x17:41/28
  599. x18 += x32 * L4_2; // x18:43/42
  600. x19 += x32 * L4_3; // x19:44/28
  601. x20 += x32 * L4_4; // x20:43/--
  602. x21 += x32 * L4_5; // x21:44/28
  603. x22 += x32 * L4_6; // x22:43/41
  604. x23 += x32 * L4_7; // x23:45/41
  605. x31 += (x30 >> 28); x30 &= M28UL; // x31:28/--, x30:28/--
  606. x15 += x31 * L4_0; // x15:54/--
  607. x16 += x31 * L4_1; // x16:53/42
  608. x17 += x31 * L4_2; // x17:55/54
  609. x18 += x31 * L4_3; // x18:56/44
  610. x19 += x31 * L4_4; // x19:55/--
  611. x20 += x31 * L4_5; // x20:56/43
  612. x21 += x31 * L4_6; // x21:55/53
  613. x22 += x31 * L4_7; // x22:57/53
  614. //x30 += (x29 >> 28); x29 &= M28UL;
  615. x14 += x30 * L4_0; // x14:54/--
  616. x15 += x30 * L4_1; // x15:54/53
  617. x16 += x30 * L4_2; // x16:56/--
  618. x17 += x30 * L4_3; // x17:57/--
  619. x18 += x30 * L4_4; // x18:56/55
  620. x19 += x30 * L4_5; // x19:56/55
  621. x20 += x30 * L4_6; // x20:57/--
  622. x21 += x30 * L4_7; // x21:57/56
  623. x29 += (x28 >> 28); x28 &= M28UL; // x29:28/--, x28:28/--
  624. x13 += x29 * L4_0; // x13:54/--
  625. x14 += x29 * L4_1; // x14:54/53
  626. x15 += x29 * L4_2; // x15:56/--
  627. x16 += x29 * L4_3; // x16:57/--
  628. x17 += x29 * L4_4; // x17:57/55
  629. x18 += x29 * L4_5; // x18:57/55
  630. x19 += x29 * L4_6; // x19:57/52
  631. x20 += x29 * L4_7; // x20:58/52
  632. //x28 += (x27 >> 28); x27 &= M28UL;
  633. x12 += x28 * L4_0; // x12:54/--
  634. x13 += x28 * L4_1; // x13:54/53
  635. x14 += x28 * L4_2; // x14:56/--
  636. x15 += x28 * L4_3; // x15:57/--
  637. x16 += x28 * L4_4; // x16:57/55
  638. x17 += x28 * L4_5; // x17:58/--
  639. x18 += x28 * L4_6; // x18:58/--
  640. x19 += x28 * L4_7; // x19:58/53
  641. x27 += (x26 >> 28); x26 &= M28UL; // x27:28/--, x26:28/--
  642. x11 += x27 * L4_0; // x11:54/--
  643. x12 += x27 * L4_1; // x12:54/53
  644. x13 += x27 * L4_2; // x13:56/--
  645. x14 += x27 * L4_3; // x14:57/--
  646. x15 += x27 * L4_4; // x15:57/55
  647. x16 += x27 * L4_5; // x16:58/--
  648. x17 += x27 * L4_6; // x17:58/56
  649. x18 += x27 * L4_7; // x18:59/--
  650. //x26 += (x25 >> 28); x25 &= M28UL;
  651. x10 += x26 * L4_0; // x10:54/--
  652. x11 += x26 * L4_1; // x11:54/53
  653. x12 += x26 * L4_2; // x12:56/--
  654. x13 += x26 * L4_3; // x13:57/--
  655. x14 += x26 * L4_4; // x14:57/55
  656. x15 += x26 * L4_5; // x15:58/--
  657. x16 += x26 * L4_6; // x16:58/56
  658. x17 += x26 * L4_7; // x17:59/--
  659. x25 += (x24 >> 28); x24 &= M28UL; // x25:28/--, x24:28/--
  660. x09 += x25 * L4_0; // x09:54/--
  661. x10 += x25 * L4_1; // x10:54/53
  662. x11 += x25 * L4_2; // x11:56/--
  663. x12 += x25 * L4_3; // x12:57/--
  664. x13 += x25 * L4_4; // x13:57/55
  665. x14 += x25 * L4_5; // x14:58/--
  666. x15 += x25 * L4_6; // x15:58/56
  667. x16 += x25 * L4_7; // x16:59/--
  668. x21 += (x20 >> 28); x20 &= M28UL; // x21:58/--, x20:28/--
  669. x22 += (x21 >> 28); x21 &= M28UL; // x22:57/54, x21:28/--
  670. x23 += (x22 >> 28); x22 &= M28UL; // x23:45/42, x22:28/--
  671. x24 += (x23 >> 28); x23 &= M28UL; // x24:28/18, x23:28/--
  672. x08 += x24 * L4_0; // x08:54/--
  673. x09 += x24 * L4_1; // x09:55/--
  674. x10 += x24 * L4_2; // x10:56/46
  675. x11 += x24 * L4_3; // x11:57/46
  676. x12 += x24 * L4_4; // x12:57/55
  677. x13 += x24 * L4_5; // x13:58/--
  678. x14 += x24 * L4_6; // x14:58/56
  679. x15 += x24 * L4_7; // x15:59/--
  680. x07 += x23 * L4_0; // x07:54/--
  681. x08 += x23 * L4_1; // x08:54/53
  682. x09 += x23 * L4_2; // x09:56/53
  683. x10 += x23 * L4_3; // x10:57/46
  684. x11 += x23 * L4_4; // x11:57/55
  685. x12 += x23 * L4_5; // x12:58/--
  686. x13 += x23 * L4_6; // x13:58/56
  687. x14 += x23 * L4_7; // x14:59/--
  688. x06 += x22 * L4_0; // x06:54/--
  689. x07 += x22 * L4_1; // x07:54/53
  690. x08 += x22 * L4_2; // x08:56/--
  691. x09 += x22 * L4_3; // x09:57/53
  692. x10 += x22 * L4_4; // x10:57/55
  693. x11 += x22 * L4_5; // x11:58/--
  694. x12 += x22 * L4_6; // x12:58/56
  695. x13 += x22 * L4_7; // x13:59/--
  696. x18 += (x17 >> 28); x17 &= M28UL; // x18:59/31, x17:28/--
  697. x19 += (x18 >> 28); x18 &= M28UL; // x19:58/54, x18:28/--
  698. x20 += (x19 >> 28); x19 &= M28UL; // x20:30/29, x19:28/--
  699. x21 += (x20 >> 28); x20 &= M28UL; // x21:28/03, x20:28/--
  700. x05 += x21 * L4_0; // x05:54/--
  701. x06 += x21 * L4_1; // x06:55/--
  702. x07 += x21 * L4_2; // x07:56/31
  703. x08 += x21 * L4_3; // x08:57/31
  704. x09 += x21 * L4_4; // x09:57/56
  705. x10 += x21 * L4_5; // x10:58/--
  706. x11 += x21 * L4_6; // x11:58/56
  707. x12 += x21 * L4_7; // x12:59/--
  708. x04 += x20 * L4_0; // x04:54/--
  709. x05 += x20 * L4_1; // x05:54/53
  710. x06 += x20 * L4_2; // x06:56/53
  711. x07 += x20 * L4_3; // x07:57/31
  712. x08 += x20 * L4_4; // x08:57/55
  713. x09 += x20 * L4_5; // x09:58/--
  714. x10 += x20 * L4_6; // x10:58/56
  715. x11 += x20 * L4_7; // x11:59/--
  716. x03 += x19 * L4_0; // x03:54/--
  717. x04 += x19 * L4_1; // x04:54/53
  718. x05 += x19 * L4_2; // x05:56/--
  719. x06 += x19 * L4_3; // x06:57/53
  720. x07 += x19 * L4_4; // x07:57/55
  721. x08 += x19 * L4_5; // x08:58/--
  722. x09 += x19 * L4_6; // x09:58/56
  723. x10 += x19 * L4_7; // x10:59/--
  724. x15 += (x14 >> 28); x14 &= M28UL; // x15:59/31, x14:28/--
  725. x16 += (x15 >> 28); x15 &= M28UL; // x16:59/32, x15:28/--
  726. x17 += (x16 >> 28); x16 &= M28UL; // x17:31/29, x16:28/--
  727. x18 += (x17 >> 28); x17 &= M28UL; // x18:28/04, x17:28/--
  728. x02 += x18 * L4_0; // x02:54/--
  729. x03 += x18 * L4_1; // x03:55/--
  730. x04 += x18 * L4_2; // x04:56/32
  731. x05 += x18 * L4_3; // x05:57/32
  732. x06 += x18 * L4_4; // x06:57/56
  733. x07 += x18 * L4_5; // x07:58/--
  734. x08 += x18 * L4_6; // x08:58/56
  735. x09 += x18 * L4_7; // x09:59/--
  736. x01 += x17 * L4_0; // x01:54/--
  737. x02 += x17 * L4_1; // x02:54/53
  738. x03 += x17 * L4_2; // x03:56/53
  739. x04 += x17 * L4_3; // x04:57/32
  740. x05 += x17 * L4_4; // x05:57/55
  741. x06 += x17 * L4_5; // x06:58/--
  742. x07 += x17 * L4_6; // x07:58/56
  743. x08 += x17 * L4_7; // x08:59/--
  744. x16 *= 4;
  745. x16 += (x15 >> 26); x15 &= M26UL;
  746. x16 += 1; // x16:30/01
  747. x00 += x16 * L_0;
  748. x01 += x16 * L_1;
  749. x02 += x16 * L_2;
  750. x03 += x16 * L_3;
  751. x04 += x16 * L_4;
  752. x05 += x16 * L_5;
  753. x06 += x16 * L_6;
  754. x07 += x16 * L_7;
  755. x01 += (x00 >> 28); x00 &= M28UL;
  756. x02 += (x01 >> 28); x01 &= M28UL;
  757. x03 += (x02 >> 28); x02 &= M28UL;
  758. x04 += (x03 >> 28); x03 &= M28UL;
  759. x05 += (x04 >> 28); x04 &= M28UL;
  760. x06 += (x05 >> 28); x05 &= M28UL;
  761. x07 += (x06 >> 28); x06 &= M28UL;
  762. x08 += (x07 >> 28); x07 &= M28UL;
  763. x09 += (x08 >> 28); x08 &= M28UL;
  764. x10 += (x09 >> 28); x09 &= M28UL;
  765. x11 += (x10 >> 28); x10 &= M28UL;
  766. x12 += (x11 >> 28); x11 &= M28UL;
  767. x13 += (x12 >> 28); x12 &= M28UL;
  768. x14 += (x13 >> 28); x13 &= M28UL;
  769. x15 += (x14 >> 28); x14 &= M28UL;
  770. x16 = (x15 >> 26); x15 &= M26UL;
  771. x16 -= 1;
  772. Debug.Assert(x16 == 0UL || x16 == ulong.MaxValue);
  773. x00 -= x16 & L_0;
  774. x01 -= x16 & L_1;
  775. x02 -= x16 & L_2;
  776. x03 -= x16 & L_3;
  777. x04 -= x16 & L_4;
  778. x05 -= x16 & L_5;
  779. x06 -= x16 & L_6;
  780. x07 -= x16 & L_7;
  781. x01 += (ulong)((long)x00 >> 28); x00 &= M28UL;
  782. x02 += (ulong)((long)x01 >> 28); x01 &= M28UL;
  783. x03 += (ulong)((long)x02 >> 28); x02 &= M28UL;
  784. x04 += (ulong)((long)x03 >> 28); x03 &= M28UL;
  785. x05 += (ulong)((long)x04 >> 28); x04 &= M28UL;
  786. x06 += (ulong)((long)x05 >> 28); x05 &= M28UL;
  787. x07 += (ulong)((long)x06 >> 28); x06 &= M28UL;
  788. x08 += (ulong)((long)x07 >> 28); x07 &= M28UL;
  789. x09 += (ulong)((long)x08 >> 28); x08 &= M28UL;
  790. x10 += (ulong)((long)x09 >> 28); x09 &= M28UL;
  791. x11 += (ulong)((long)x10 >> 28); x10 &= M28UL;
  792. x12 += (ulong)((long)x11 >> 28); x11 &= M28UL;
  793. x13 += (ulong)((long)x12 >> 28); x12 &= M28UL;
  794. x14 += (ulong)((long)x13 >> 28); x13 &= M28UL;
  795. x15 += (ulong)((long)x14 >> 28); x14 &= M28UL;
  796. Debug.Assert(x15 >> 26 == 0UL);
  797. byte[] r = new byte[ScalarBytes];
  798. Encode56(x00 | (x01 << 28), r, 0);
  799. Encode56(x02 | (x03 << 28), r, 7);
  800. Encode56(x04 | (x05 << 28), r, 14);
  801. Encode56(x06 | (x07 << 28), r, 21);
  802. Encode56(x08 | (x09 << 28), r, 28);
  803. Encode56(x10 | (x11 << 28), r, 35);
  804. Encode56(x12 | (x13 << 28), r, 42);
  805. Encode56(x14 | (x15 << 28), r, 49);
  806. //r[ScalarBytes - 1] = 0;
  807. return r;
  808. }
  809. private static void ScalarMultBase(byte[] k, PointExt r)
  810. {
  811. Precompute();
  812. PointSetNeutral(r);
  813. uint[] n = new uint[ScalarUints + 1];
  814. DecodeScalar(k, 0, n);
  815. // Recode the scalar into signed-digit form
  816. {
  817. n[ScalarUints] = 4U + Nat.CAdd(ScalarUints, ~(int)n[0] & 1, n, L, n);
  818. uint c = Nat.ShiftDownBit(n.Length, n, 0);
  819. Debug.Assert(c == (1U << 31));
  820. }
  821. PointPrecomp p = new PointPrecomp();
  822. int cOff = PrecompSpacing - 1;
  823. for (;;)
  824. {
  825. int tPos = cOff;
  826. for (int b = 0; b < PrecompBlocks; ++b)
  827. {
  828. uint w = 0;
  829. for (int t = 0; t < PrecompTeeth; ++t)
  830. {
  831. uint tBit = n[tPos >> 5] >> (tPos & 0x1F);
  832. w &= ~(1U << t);
  833. w ^= (tBit << t);
  834. tPos += PrecompSpacing;
  835. }
  836. int sign = (int)(w >> (PrecompTeeth - 1)) & 1;
  837. int abs = ((int)w ^ -sign) & PrecompMask;
  838. Debug.Assert(sign == 0 || sign == 1);
  839. Debug.Assert(0 <= abs && abs < PrecompPoints);
  840. PointLookup(b, abs, p);
  841. X448Field.CNegate(sign, p.x);
  842. PointAddPrecomp(p, r);
  843. }
  844. if (--cOff < 0)
  845. break;
  846. PointDouble(r);
  847. }
  848. }
  849. private static void ScalarMultBaseEncoded(byte[] k, byte[] r, int rOff)
  850. {
  851. PointExt p = new PointExt();
  852. ScalarMultBase(k, p);
  853. EncodePoint(p, r, rOff);
  854. }
  855. internal static void ScalarMultBaseXY(byte[] k, int kOff, uint[] x, uint[] y)
  856. {
  857. byte[] n = new byte[ScalarBytes];
  858. PruneScalar(k, kOff, n);
  859. PointExt p = new PointExt();
  860. ScalarMultBase(n, p);
  861. X448Field.Copy(p.x, 0, x, 0);
  862. X448Field.Copy(p.y, 0, y, 0);
  863. }
  864. private static void ScalarMultStraussVar(uint[] nb, uint[] np, PointExt p, PointExt r)
  865. {
  866. Precompute();
  867. int width = 5;
  868. sbyte[] ws_b = GetWnaf(nb, WnafWidthBase);
  869. sbyte[] ws_p = GetWnaf(np, width);
  870. PointExt[] tp = PointPrecompVar(p, 1 << (width - 2));
  871. PointSetNeutral(r);
  872. int bit = 447;
  873. while (bit > 0 && (ws_b[bit] | ws_p[bit]) == 0)
  874. {
  875. --bit;
  876. }
  877. for (;;)
  878. {
  879. int wb = ws_b[bit];
  880. if (wb != 0)
  881. {
  882. int sign = wb >> 31;
  883. int index = (wb ^ sign) >> 1;
  884. PointAddVar((sign != 0), precompBaseTable[index], r);
  885. }
  886. int wp = ws_p[bit];
  887. if (wp != 0)
  888. {
  889. int sign = wp >> 31;
  890. int index = (wp ^ sign) >> 1;
  891. PointAddVar((sign != 0), tp[index], r);
  892. }
  893. if (--bit < 0)
  894. break;
  895. PointDouble(r);
  896. }
  897. }
  898. public static void Sign(byte[] sk, int skOff, byte[] ctx, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  899. {
  900. byte phflag = 0x00;
  901. ImplSign(sk, skOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  902. }
  903. public static void Sign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  904. {
  905. byte phflag = 0x00;
  906. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  907. }
  908. public static void SignPrehash(byte[] sk, int skOff, byte[] ctx, byte[] ph, int phOff, byte[] sig, int sigOff)
  909. {
  910. byte phflag = 0x01;
  911. ImplSign(sk, skOff, ctx, phflag, ph, phOff, PrehashSize, sig, sigOff);
  912. }
  913. public static void SignPrehash(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte[] ph, int phOff, byte[] sig, int sigOff)
  914. {
  915. byte phflag = 0x01;
  916. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, ph, phOff, PrehashSize, sig, sigOff);
  917. }
  918. public static void SignPrehash(byte[] sk, int skOff, byte[] ctx, IXof ph, byte[] sig, int sigOff)
  919. {
  920. byte[] m = new byte[PrehashSize];
  921. if (PrehashSize != ph.DoFinal(m, 0, PrehashSize))
  922. throw new ArgumentException("ph");
  923. byte phflag = 0x01;
  924. ImplSign(sk, skOff, ctx, phflag, m, 0, m.Length, sig, sigOff);
  925. }
  926. public static void SignPrehash(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, IXof ph, byte[] sig, int sigOff)
  927. {
  928. byte[] m = new byte[PrehashSize];
  929. if (PrehashSize != ph.DoFinal(m, 0, PrehashSize))
  930. throw new ArgumentException("ph");
  931. byte phflag = 0x01;
  932. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, 0, m.Length, sig, sigOff);
  933. }
  934. public static bool Verify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte[] m, int mOff, int mLen)
  935. {
  936. byte phflag = 0x00;
  937. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, mOff, mLen);
  938. }
  939. public static bool VerifyPrehash(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte[] ph, int phOff)
  940. {
  941. byte phflag = 0x01;
  942. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, ph, phOff, PrehashSize);
  943. }
  944. public static bool VerifyPrehash(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, IXof ph)
  945. {
  946. byte[] m = new byte[PrehashSize];
  947. if (PrehashSize != ph.DoFinal(m, 0, PrehashSize))
  948. throw new ArgumentException("ph");
  949. byte phflag = 0x01;
  950. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, 0, m.Length);
  951. }
  952. }
  953. }
  954. #pragma warning restore
  955. #endif