Ed448.cs 51 KB

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