Ed448.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032
  11. {
  12. using F = Rfc7748.X448Field;
  13. /// <summary>
  14. /// A low-level implementation of the Ed448 and Ed448ph instantiations of the Edwards-Curve Digital Signature
  15. /// Algorithm specified in <a href="https://www.rfc-editor.org/rfc/rfc8032">RFC 8032</a>.
  16. /// </summary>
  17. /// <remarks>
  18. /// The implementation uses the "signed mult-comb" algorithm (for scalar multiplication by a fixed point) from
  19. /// <a href="https://ia.cr/2012/309">Mike Hamburg, "Fast and compact elliptic-curve cryptography"</a>. Standard
  20. /// <a href="https://hyperelliptic.org/EFD/g1p/auto-edwards-projective.html">projective coordinates</a> are used
  21. /// for most point arithmetic.
  22. /// </remarks>
  23. public static class Ed448
  24. {
  25. // x^2 + y^2 == 1 - 39081 * x^2 * y^2
  26. public enum Algorithm
  27. {
  28. Ed448 = 0,
  29. Ed448ph = 1,
  30. }
  31. private const ulong M26UL = 0x03FFFFFFUL;
  32. private const ulong M28UL = 0x0FFFFFFFUL;
  33. private const int CoordUints = 14;
  34. private const int PointBytes = CoordUints * 4 + 1;
  35. private const int ScalarUints = 14;
  36. private const int ScalarBytes = ScalarUints * 4 + 1;
  37. public static readonly int PrehashSize = 64;
  38. public static readonly int PublicKeySize = PointBytes;
  39. public static readonly int SecretKeySize = 57;
  40. public static readonly int SignatureSize = PointBytes + ScalarBytes;
  41. // "SigEd448"
  42. private static readonly byte[] Dom4Prefix = new byte[]{ 0x53, 0x69, 0x67, 0x45, 0x64, 0x34, 0x34, 0x38 };
  43. private static readonly uint[] P = { 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU,
  44. 0xFFFFFFFEU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU };
  45. private static readonly uint[] L = { 0xAB5844F3U, 0x2378C292U, 0x8DC58F55U, 0x216CC272U, 0xAED63690U, 0xC44EDB49U, 0x7CCA23E9U,
  46. 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0x3FFFFFFFU };
  47. private const int L_0 = 0x04A7BB0D; // L_0:26/24
  48. private const int L_1 = 0x0873D6D5; // L_1:27/23
  49. private const int L_2 = 0x0A70AADC; // L_2:27/26
  50. private const int L_3 = 0x03D8D723; // L_3:26/--
  51. private const int L_4 = 0x096FDE93; // L_4:27/25
  52. private const int L_5 = 0x0B65129C; // L_5:27/26
  53. private const int L_6 = 0x063BB124; // L_6:27/--
  54. private const int L_7 = 0x08335DC1; // L_7:27/22
  55. private const int L4_0 = 0x029EEC34; // L4_0:25/24
  56. private const int L4_1 = 0x01CF5B55; // L4_1:25/--
  57. private const int L4_2 = 0x09C2AB72; // L4_2:27/25
  58. private const int L4_3 = 0x0F635C8E; // L4_3:28/--
  59. private const int L4_4 = 0x05BF7A4C; // L4_4:26/25
  60. private const int L4_5 = 0x0D944A72; // L4_5:28/--
  61. private const int L4_6 = 0x08EEC492; // L4_6:27/24
  62. private const int L4_7 = 0x20CD7705; // L4_7:29/24
  63. private static readonly uint[] B_x = { 0x070CC05EU, 0x026A82BCU, 0x00938E26U, 0x080E18B0U, 0x0511433BU, 0x0F72AB66U, 0x0412AE1AU,
  64. 0x0A3D3A46U, 0x0A6DE324U, 0x00F1767EU, 0x04657047U, 0x036DA9E1U, 0x05A622BFU, 0x0ED221D1U, 0x066BED0DU, 0x04F1970CU };
  65. private static readonly uint[] B_y = { 0x0230FA14U, 0x008795BFU, 0x07C8AD98U, 0x0132C4EDU, 0x09C4FDBDU, 0x01CE67C3U, 0x073AD3FFU,
  66. 0x005A0C2DU, 0x07789C1EU, 0x0A398408U, 0x0A73736CU, 0x0C7624BEU, 0x003756C9U, 0x02488762U, 0x016EB6BCU, 0x0693F467U };
  67. private const int C_d = -39081;
  68. private const int WnafWidth = 5;
  69. private const int WnafWidthBase = 7;
  70. // ScalarMultBase supports varying blocks, teeth, spacing so long as their product is in range [449, 479]
  71. private const int PrecompBlocks = 5;
  72. private const int PrecompTeeth = 5;
  73. private const int PrecompSpacing = 18;
  74. private const int PrecompRange = PrecompBlocks * PrecompTeeth * PrecompSpacing; // 448 < range < 480
  75. private const int PrecompPoints = 1 << (PrecompTeeth - 1);
  76. private const int PrecompMask = PrecompPoints - 1;
  77. private static readonly object PrecompLock = new object();
  78. private static PointAffine[] PrecompBaseWnaf = null;
  79. private static uint[] PrecompBaseComb = null;
  80. private struct PointAffine
  81. {
  82. internal uint[] x, y;
  83. }
  84. private struct PointProjective
  85. {
  86. internal uint[] x, y, z;
  87. }
  88. private static byte[] CalculateS(byte[] r, byte[] k, byte[] s)
  89. {
  90. uint[] t = new uint[ScalarUints * 2]; DecodeScalar(r, 0, t);
  91. uint[] u = new uint[ScalarUints]; DecodeScalar(k, 0, u);
  92. uint[] v = new uint[ScalarUints]; DecodeScalar(s, 0, v);
  93. Nat.MulAddTo(ScalarUints, u, v, t);
  94. byte[] result = new byte[ScalarBytes * 2];
  95. for (int i = 0; i < t.Length; ++i)
  96. {
  97. Encode32(t[i], result, i * 4);
  98. }
  99. return ReduceScalar(result);
  100. }
  101. private static bool CheckContextVar(byte[] ctx)
  102. {
  103. return ctx != null && ctx.Length < 256;
  104. }
  105. private static int CheckPoint(uint[] x, uint[] y)
  106. {
  107. uint[] t = F.Create();
  108. uint[] u = F.Create();
  109. uint[] v = F.Create();
  110. F.Sqr(x, u);
  111. F.Sqr(y, v);
  112. F.Mul(u, v, t);
  113. F.Add(u, v, u);
  114. F.Mul(t, -C_d, t);
  115. F.SubOne(t);
  116. F.Add(t, u, t);
  117. F.Normalize(t);
  118. return F.IsZero(t);
  119. }
  120. private static int CheckPoint(uint[] x, uint[] y, uint[] z)
  121. {
  122. uint[] t = F.Create();
  123. uint[] u = F.Create();
  124. uint[] v = F.Create();
  125. uint[] w = F.Create();
  126. F.Sqr(x, u);
  127. F.Sqr(y, v);
  128. F.Sqr(z, w);
  129. F.Mul(u, v, t);
  130. F.Add(u, v, u);
  131. F.Mul(u, w, u);
  132. F.Sqr(w, w);
  133. F.Mul(t, -C_d, t);
  134. F.Sub(t, w, t);
  135. F.Add(t, u, t);
  136. F.Normalize(t);
  137. return F.IsZero(t);
  138. }
  139. private static bool CheckPointVar(byte[] p)
  140. {
  141. if ((p[PointBytes - 1] & 0x7F) != 0x00)
  142. return false;
  143. uint[] t = new uint[CoordUints];
  144. Decode32(p, 0, t, 0, CoordUints);
  145. return !Nat.Gte(CoordUints, t, P);
  146. }
  147. private static bool CheckScalarVar(byte[] s, uint[] n)
  148. {
  149. if (s[ScalarBytes - 1] != 0x00)
  150. return false;
  151. DecodeScalar(s, 0, n);
  152. return !Nat.Gte(ScalarUints, n, L);
  153. }
  154. private static byte[] Copy(byte[] buf, int off, int len)
  155. {
  156. byte[] result = new byte[len];
  157. Array.Copy(buf, off, result, 0, len);
  158. return result;
  159. }
  160. public static IXof CreatePrehash()
  161. {
  162. return CreateXof();
  163. }
  164. private static IXof CreateXof()
  165. {
  166. return new ShakeDigest(256);
  167. }
  168. private static uint Decode16(byte[] bs, int off)
  169. {
  170. uint n = bs[off];
  171. n |= (uint)bs[++off] << 8;
  172. return n;
  173. }
  174. private static uint Decode24(byte[] bs, int off)
  175. {
  176. uint n = bs[off];
  177. n |= (uint)bs[++off] << 8;
  178. n |= (uint)bs[++off] << 16;
  179. return n;
  180. }
  181. private static uint Decode32(byte[] bs, int off)
  182. {
  183. uint n = bs[off];
  184. n |= (uint)bs[++off] << 8;
  185. n |= (uint)bs[++off] << 16;
  186. n |= (uint)bs[++off] << 24;
  187. return n;
  188. }
  189. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  190. private static uint Decode32(ReadOnlySpan<byte> bs)
  191. {
  192. uint n = bs[0];
  193. n |= (uint)bs[1] << 8;
  194. n |= (uint)bs[2] << 16;
  195. n |= (uint)bs[3] << 24;
  196. return n;
  197. }
  198. #endif
  199. private static void Decode32(byte[] bs, int bsOff, uint[] n, int nOff, int nLen)
  200. {
  201. for (int i = 0; i < nLen; ++i)
  202. {
  203. n[nOff + i] = Decode32(bs, bsOff + i * 4);
  204. }
  205. }
  206. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  207. private static void Decode32(ReadOnlySpan<byte> bs, Span<uint> n)
  208. {
  209. for (int i = 0; i < n.Length; ++i)
  210. {
  211. n[i] = Decode32(bs[(i * 4)..]);
  212. }
  213. }
  214. #endif
  215. private static bool DecodePointVar(byte[] p, int pOff, bool negate, ref PointProjective r)
  216. {
  217. byte[] py = Copy(p, pOff, PointBytes);
  218. if (!CheckPointVar(py))
  219. return false;
  220. int x_0 = (py[PointBytes - 1] & 0x80) >> 7;
  221. py[PointBytes - 1] &= 0x7F;
  222. F.Decode(py, 0, r.y);
  223. uint[] u = F.Create();
  224. uint[] v = F.Create();
  225. F.Sqr(r.y, u);
  226. F.Mul(u, (uint)-C_d, v);
  227. F.Negate(u, u);
  228. F.AddOne(u);
  229. F.AddOne(v);
  230. if (!F.SqrtRatioVar(u, v, r.x))
  231. return false;
  232. F.Normalize(r.x);
  233. if (x_0 == 1 && F.IsZeroVar(r.x))
  234. return false;
  235. if (negate ^ (x_0 != (r.x[0] & 1)))
  236. {
  237. F.Negate(r.x, r.x);
  238. }
  239. F.One(r.z);
  240. return true;
  241. }
  242. private static void DecodeScalar(byte[] k, int kOff, uint[] n)
  243. {
  244. Debug.Assert(k[kOff + ScalarBytes - 1] == 0x00);
  245. Decode32(k, kOff, n, 0, ScalarUints);
  246. }
  247. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  248. private static void DecodeScalar(ReadOnlySpan<byte> k, Span<uint> n)
  249. {
  250. Debug.Assert(k[ScalarBytes - 1] == 0x00);
  251. Decode32(k, n[..ScalarUints]);
  252. }
  253. #endif
  254. private static void Dom4(IXof d, byte phflag, byte[] ctx)
  255. {
  256. int n = Dom4Prefix.Length;
  257. byte[] t = new byte[n + 2 + ctx.Length];
  258. Dom4Prefix.CopyTo(t, 0);
  259. t[n] = phflag;
  260. t[n + 1] = (byte)ctx.Length;
  261. ctx.CopyTo(t, n + 2);
  262. d.BlockUpdate(t, 0, t.Length);
  263. }
  264. private static void Encode24(uint n, byte[] bs, int off)
  265. {
  266. bs[off] = (byte)(n);
  267. bs[++off] = (byte)(n >> 8);
  268. bs[++off] = (byte)(n >> 16);
  269. }
  270. private static void Encode32(uint n, byte[] bs, int off)
  271. {
  272. bs[off] = (byte)(n);
  273. bs[++off] = (byte)(n >> 8);
  274. bs[++off] = (byte)(n >> 16);
  275. bs[++off] = (byte)(n >> 24);
  276. }
  277. private static void Encode56(ulong n, byte[] bs, int off)
  278. {
  279. Encode32((uint)n, bs, off);
  280. Encode24((uint)(n >> 32), bs, off + 4);
  281. }
  282. private static int EncodePoint(ref PointProjective p, byte[] r, int rOff)
  283. {
  284. uint[] x = F.Create();
  285. uint[] y = F.Create();
  286. F.Inv(p.z, y);
  287. F.Mul(p.x, y, x);
  288. F.Mul(p.y, y, y);
  289. F.Normalize(x);
  290. F.Normalize(y);
  291. int result = CheckPoint(x, y);
  292. F.Encode(y, r, rOff);
  293. r[rOff + PointBytes - 1] = (byte)((x[0] & 1) << 7);
  294. return result;
  295. }
  296. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  297. private static int EncodePoint(ref PointProjective p, Span<byte> r)
  298. {
  299. uint[] x = F.Create();
  300. uint[] y = F.Create();
  301. F.Inv(p.z, y);
  302. F.Mul(p.x, y, x);
  303. F.Mul(p.y, y, y);
  304. F.Normalize(x);
  305. F.Normalize(y);
  306. int result = CheckPoint(x, y);
  307. F.Encode(y, r);
  308. r[PointBytes - 1] = (byte)((x[0] & 1) << 7);
  309. return result;
  310. }
  311. #endif
  312. public static void GeneratePrivateKey(SecureRandom random, byte[] k)
  313. {
  314. if (k.Length != SecretKeySize)
  315. throw new ArgumentException(nameof(k));
  316. random.NextBytes(k);
  317. }
  318. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  319. public static void GeneratePrivateKey(SecureRandom random, Span<byte> k)
  320. {
  321. if (k.Length != SecretKeySize)
  322. throw new ArgumentException(nameof(k));
  323. random.NextBytes(k);
  324. }
  325. #endif
  326. public static void GeneratePublicKey(byte[] sk, int skOff, byte[] pk, int pkOff)
  327. {
  328. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  329. GeneratePublicKey(sk.AsSpan(skOff), pk.AsSpan(pkOff));
  330. #else
  331. IXof d = CreateXof();
  332. byte[] h = new byte[ScalarBytes * 2];
  333. d.BlockUpdate(sk, skOff, SecretKeySize);
  334. d.OutputFinal(h, 0, h.Length);
  335. byte[] s = new byte[ScalarBytes];
  336. PruneScalar(h, 0, s);
  337. ScalarMultBaseEncoded(s, pk, pkOff);
  338. #endif
  339. }
  340. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  341. public static void GeneratePublicKey(ReadOnlySpan<byte> sk, Span<byte> pk)
  342. {
  343. IXof d = CreateXof();
  344. Span<byte> h = stackalloc byte[ScalarBytes * 2];
  345. d.BlockUpdate(sk[..SecretKeySize]);
  346. d.OutputFinal(h);
  347. Span<byte> s = stackalloc byte[ScalarBytes];
  348. PruneScalar(h, s);
  349. ScalarMultBaseEncoded(s, pk);
  350. }
  351. #endif
  352. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  353. private static uint GetWindow4(ReadOnlySpan<uint> x, int n)
  354. #else
  355. private static uint GetWindow4(uint[] x, int n)
  356. #endif
  357. {
  358. int w = (int)((uint)n >> 3), b = (n & 7) << 2;
  359. return (x[w] >> b) & 15U;
  360. }
  361. private static sbyte[] GetWnafVar(uint[] n, int width)
  362. {
  363. Debug.Assert(n[ScalarUints - 1] <= L[ScalarUints - 1]);
  364. Debug.Assert(2 <= width && width <= 8);
  365. uint[] t = new uint[ScalarUints * 2];
  366. {
  367. uint c = 0;
  368. int tPos = t.Length, i = ScalarUints;
  369. while (--i >= 0)
  370. {
  371. uint next = n[i];
  372. t[--tPos] = (next >> 16) | (c << 16);
  373. t[--tPos] = c = next;
  374. }
  375. }
  376. sbyte[] ws = new sbyte[447];
  377. int lead = 32 - width;
  378. uint carry = 0U;
  379. int j = 0;
  380. for (int i = 0; i < t.Length; ++i, j -= 16)
  381. {
  382. uint word = t[i];
  383. while (j < 16)
  384. {
  385. uint word16 = word >> j;
  386. uint bit = word16 & 1U;
  387. if (bit == carry)
  388. {
  389. ++j;
  390. continue;
  391. }
  392. uint digit = (word16 | 1U) << lead;
  393. carry = digit >> 31;
  394. ws[(i << 4) + j] = (sbyte)((int)digit >> lead);
  395. j += width;
  396. }
  397. }
  398. Debug.Assert(carry == 0);
  399. return ws;
  400. }
  401. private static void ImplSign(IXof d, byte[] h, byte[] s, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  402. byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  403. {
  404. Dom4(d, phflag, ctx);
  405. d.BlockUpdate(h, ScalarBytes, ScalarBytes);
  406. d.BlockUpdate(m, mOff, mLen);
  407. d.OutputFinal(h, 0, h.Length);
  408. byte[] r = ReduceScalar(h);
  409. byte[] R = new byte[PointBytes];
  410. ScalarMultBaseEncoded(r, R, 0);
  411. Dom4(d, phflag, ctx);
  412. d.BlockUpdate(R, 0, PointBytes);
  413. d.BlockUpdate(pk, pkOff, PointBytes);
  414. d.BlockUpdate(m, mOff, mLen);
  415. d.OutputFinal(h, 0, h.Length);
  416. byte[] k = ReduceScalar(h);
  417. byte[] S = CalculateS(r, k, s);
  418. Array.Copy(R, 0, sig, sigOff, PointBytes);
  419. Array.Copy(S, 0, sig, sigOff + PointBytes, ScalarBytes);
  420. }
  421. private static void ImplSign(byte[] sk, int skOff, byte[] ctx, byte phflag, byte[] m, int mOff, int mLen,
  422. byte[] sig, int sigOff)
  423. {
  424. if (!CheckContextVar(ctx))
  425. throw new ArgumentException("ctx");
  426. IXof d = CreateXof();
  427. byte[] h = new byte[ScalarBytes * 2];
  428. d.BlockUpdate(sk, skOff, SecretKeySize);
  429. d.OutputFinal(h, 0, h.Length);
  430. byte[] s = new byte[ScalarBytes];
  431. PruneScalar(h, 0, s);
  432. byte[] pk = new byte[PointBytes];
  433. ScalarMultBaseEncoded(s, pk, 0);
  434. ImplSign(d, h, s, pk, 0, ctx, phflag, m, mOff, mLen, sig, sigOff);
  435. }
  436. private static void ImplSign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  437. byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  438. {
  439. if (!CheckContextVar(ctx))
  440. throw new ArgumentException("ctx");
  441. IXof d = CreateXof();
  442. byte[] h = new byte[ScalarBytes * 2];
  443. d.BlockUpdate(sk, skOff, SecretKeySize);
  444. d.OutputFinal(h, 0, h.Length);
  445. byte[] s = new byte[ScalarBytes];
  446. PruneScalar(h, 0, s);
  447. ImplSign(d, h, s, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  448. }
  449. private static bool ImplVerify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  450. byte[] m, int mOff, int mLen)
  451. {
  452. if (!CheckContextVar(ctx))
  453. throw new ArgumentException("ctx");
  454. byte[] R = Copy(sig, sigOff, PointBytes);
  455. byte[] S = Copy(sig, sigOff + PointBytes, ScalarBytes);
  456. if (!CheckPointVar(R))
  457. return false;
  458. uint[] nS = new uint[ScalarUints];
  459. if (!CheckScalarVar(S, nS))
  460. return false;
  461. Init(out PointProjective pA);
  462. if (!DecodePointVar(pk, pkOff, true, ref pA))
  463. return false;
  464. IXof d = CreateXof();
  465. byte[] h = new byte[ScalarBytes * 2];
  466. Dom4(d, phflag, ctx);
  467. d.BlockUpdate(R, 0, PointBytes);
  468. d.BlockUpdate(pk, pkOff, PointBytes);
  469. d.BlockUpdate(m, mOff, mLen);
  470. d.OutputFinal(h, 0, h.Length);
  471. byte[] k = ReduceScalar(h);
  472. uint[] nA = new uint[ScalarUints];
  473. DecodeScalar(k, 0, nA);
  474. Init(out PointProjective pR);
  475. ScalarMultStrausVar(nS, nA, ref pA, ref pR);
  476. byte[] check = new byte[PointBytes];
  477. return 0 != EncodePoint(ref pR, check, 0) && Arrays.AreEqual(check, R);
  478. }
  479. private static void Init(out PointAffine r)
  480. {
  481. r.x = F.Create();
  482. r.y = F.Create();
  483. }
  484. private static void Init(out PointProjective r)
  485. {
  486. r.x = F.Create();
  487. r.y = F.Create();
  488. r.z = F.Create();
  489. }
  490. private static void InvertZs(PointProjective[] points)
  491. {
  492. int count = points.Length;
  493. uint[] cs = F.CreateTable(count);
  494. uint[] u = F.Create();
  495. F.Copy(points[0].z, 0, u, 0);
  496. F.Copy(u, 0, cs, 0);
  497. int i = 0;
  498. while (++i < count)
  499. {
  500. F.Mul(u, points[i].z, u);
  501. F.Copy(u, 0, cs, i * F.Size);
  502. }
  503. F.InvVar(u, u);
  504. --i;
  505. uint[] t = F.Create();
  506. while (i > 0)
  507. {
  508. int j = i--;
  509. F.Copy(cs, i * F.Size, t, 0);
  510. F.Mul(t, u, t);
  511. F.Mul(u, points[j].z, u);
  512. F.Copy(t, 0, points[j].z, 0);
  513. }
  514. F.Copy(u, 0, points[0].z, 0);
  515. }
  516. private static bool IsNeutralElementVar(uint[] x, uint[] y, uint[] z)
  517. {
  518. return F.IsZeroVar(x) && F.AreEqualVar(y, z);
  519. }
  520. private static void PointAdd(ref PointAffine p, ref PointProjective r)
  521. {
  522. uint[] b = F.Create();
  523. uint[] c = F.Create();
  524. uint[] d = F.Create();
  525. uint[] e = F.Create();
  526. uint[] f = F.Create();
  527. uint[] g = F.Create();
  528. uint[] h = F.Create();
  529. F.Sqr(r.z, b);
  530. F.Mul(p.x, r.x, c);
  531. F.Mul(p.y, r.y, d);
  532. F.Mul(c, d, e);
  533. F.Mul(e, -C_d, e);
  534. //F.Apm(b, e, f, g);
  535. F.Add(b, e, f);
  536. F.Sub(b, e, g);
  537. F.Add(p.y, p.x, h);
  538. F.Add(r.y, r.x, e);
  539. F.Mul(h, e, h);
  540. //F.Apm(d, c, b, e);
  541. F.Add(d, c, b);
  542. F.Sub(d, c, e);
  543. F.Carry(b);
  544. F.Sub(h, b, h);
  545. F.Mul(h, r.z, h);
  546. F.Mul(e, r.z, e);
  547. F.Mul(f, h, r.x);
  548. F.Mul(e, g, r.y);
  549. F.Mul(f, g, r.z);
  550. }
  551. private static void PointAdd(ref PointProjective p, ref PointProjective r)
  552. {
  553. uint[] a = F.Create();
  554. uint[] b = F.Create();
  555. uint[] c = F.Create();
  556. uint[] d = F.Create();
  557. uint[] e = F.Create();
  558. uint[] f = F.Create();
  559. uint[] g = F.Create();
  560. uint[] h = F.Create();
  561. F.Mul(p.z, r.z, a);
  562. F.Sqr(a, b);
  563. F.Mul(p.x, r.x, c);
  564. F.Mul(p.y, r.y, d);
  565. F.Mul(c, d, e);
  566. F.Mul(e, -C_d, e);
  567. //F.Apm(b, e, f, g);
  568. F.Add(b, e, f);
  569. F.Sub(b, e, g);
  570. F.Add(p.y, p.x, h);
  571. F.Add(r.y, r.x, e);
  572. F.Mul(h, e, h);
  573. //F.Apm(d, c, b, e);
  574. F.Add(d, c, b);
  575. F.Sub(d, c, e);
  576. F.Carry(b);
  577. F.Sub(h, b, h);
  578. F.Mul(h, a, h);
  579. F.Mul(e, a, e);
  580. F.Mul(f, h, r.x);
  581. F.Mul(e, g, r.y);
  582. F.Mul(f, g, r.z);
  583. }
  584. private static void PointAddVar(bool negate, ref PointAffine p, ref PointProjective r)
  585. {
  586. uint[] b = F.Create();
  587. uint[] c = F.Create();
  588. uint[] d = F.Create();
  589. uint[] e = F.Create();
  590. uint[] f = F.Create();
  591. uint[] g = F.Create();
  592. uint[] h = F.Create();
  593. uint[] nb, ne, nf, ng;
  594. if (negate)
  595. {
  596. nb = e; ne = b; nf = g; ng = f;
  597. F.Sub(p.y, p.x, h);
  598. }
  599. else
  600. {
  601. nb = b; ne = e; nf = f; ng = g;
  602. F.Add(p.y, p.x, h);
  603. }
  604. F.Sqr(r.z, b);
  605. F.Mul(p.x, r.x, c);
  606. F.Mul(p.y, r.y, d);
  607. F.Mul(c, d, e);
  608. F.Mul(e, -C_d, e);
  609. //F.Apm(b, e, nf, ng);
  610. F.Add(b, e, nf);
  611. F.Sub(b, e, ng);
  612. F.Add(r.y, r.x, e);
  613. F.Mul(h, e, h);
  614. //F.Apm(d, c, nb, ne);
  615. F.Add(d, c, nb);
  616. F.Sub(d, c, ne);
  617. F.Carry(nb);
  618. F.Sub(h, b, h);
  619. F.Mul(h, r.z, h);
  620. F.Mul(e, r.z, e);
  621. F.Mul(f, h, r.x);
  622. F.Mul(e, g, r.y);
  623. F.Mul(f, g, r.z);
  624. }
  625. private static void PointAddVar(bool negate, ref PointProjective p, ref PointProjective r)
  626. {
  627. uint[] a = F.Create();
  628. uint[] b = F.Create();
  629. uint[] c = F.Create();
  630. uint[] d = F.Create();
  631. uint[] e = F.Create();
  632. uint[] f = F.Create();
  633. uint[] g = F.Create();
  634. uint[] h = F.Create();
  635. uint[] nb, ne, nf, ng;
  636. if (negate)
  637. {
  638. nb = e; ne = b; nf = g; ng = f;
  639. F.Sub(p.y, p.x, h);
  640. }
  641. else
  642. {
  643. nb = b; ne = e; nf = f; ng = g;
  644. F.Add(p.y, p.x, h);
  645. }
  646. F.Mul(p.z, r.z, a);
  647. F.Sqr(a, b);
  648. F.Mul(p.x, r.x, c);
  649. F.Mul(p.y, r.y, d);
  650. F.Mul(c, d, e);
  651. F.Mul(e, -C_d, e);
  652. //F.Apm(b, e, nf, ng);
  653. F.Add(b, e, nf);
  654. F.Sub(b, e, ng);
  655. F.Add(r.y, r.x, e);
  656. F.Mul(h, e, h);
  657. //F.Apm(d, c, nb, ne);
  658. F.Add(d, c, nb);
  659. F.Sub(d, c, ne);
  660. F.Carry(nb);
  661. F.Sub(h, b, h);
  662. F.Mul(h, a, h);
  663. F.Mul(e, a, e);
  664. F.Mul(f, h, r.x);
  665. F.Mul(e, g, r.y);
  666. F.Mul(f, g, r.z);
  667. }
  668. private static void PointCopy(ref PointProjective p, ref PointProjective r)
  669. {
  670. F.Copy(p.x, 0, r.x, 0);
  671. F.Copy(p.y, 0, r.y, 0);
  672. F.Copy(p.z, 0, r.z, 0);
  673. }
  674. private static void PointDouble(ref PointProjective r)
  675. {
  676. uint[] b = F.Create();
  677. uint[] c = F.Create();
  678. uint[] d = F.Create();
  679. uint[] e = F.Create();
  680. uint[] h = F.Create();
  681. uint[] j = F.Create();
  682. F.Add(r.x, r.y, b);
  683. F.Sqr(b, b);
  684. F.Sqr(r.x, c);
  685. F.Sqr(r.y, d);
  686. F.Add(c, d, e);
  687. F.Carry(e);
  688. F.Sqr(r.z, h);
  689. F.Add(h, h, h);
  690. F.Carry(h);
  691. F.Sub(e, h, j);
  692. F.Sub(b, e, b);
  693. F.Sub(c, d, c);
  694. F.Mul(b, j, r.x);
  695. F.Mul(e, c, r.y);
  696. F.Mul(e, j, r.z);
  697. }
  698. private static void PointLookup(int block, int index, ref PointAffine p)
  699. {
  700. Debug.Assert(0 <= block && block < PrecompBlocks);
  701. Debug.Assert(0 <= index && index < PrecompPoints);
  702. int off = block * PrecompPoints * 2 * F.Size;
  703. for (int i = 0; i < PrecompPoints; ++i)
  704. {
  705. int cond = ((i ^ index) - 1) >> 31;
  706. F.CMov(cond, PrecompBaseComb, off, p.x, 0); off += F.Size;
  707. F.CMov(cond, PrecompBaseComb, off, p.y, 0); off += F.Size;
  708. }
  709. }
  710. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  711. private static void PointLookup(ReadOnlySpan<uint> x, int n, ReadOnlySpan<uint> table, ref PointProjective r)
  712. {
  713. // TODO This method is currently hardcoded to 4-bit windows and 8 precomputed points
  714. uint w = GetWindow4(x, n);
  715. int sign = (int)(w >> (4 - 1)) ^ 1;
  716. int abs = ((int)w ^ -sign) & 7;
  717. Debug.Assert(sign == 0 || sign == 1);
  718. Debug.Assert(0 <= abs && abs < 8);
  719. for (int i = 0; i < 8; ++i)
  720. {
  721. int cond = ((i ^ abs) - 1) >> 31;
  722. F.CMov(cond, table, r.x); table = table[F.Size..];
  723. F.CMov(cond, table, r.y); table = table[F.Size..];
  724. F.CMov(cond, table, r.z); table = table[F.Size..];
  725. }
  726. F.CNegate(sign, r.x);
  727. }
  728. #else
  729. private static void PointLookup(uint[] x, int n, uint[] table, ref PointProjective r)
  730. {
  731. // TODO This method is currently hardcoded to 4-bit windows and 8 precomputed points
  732. uint w = GetWindow4(x, n);
  733. int sign = (int)(w >> (4 - 1)) ^ 1;
  734. int abs = ((int)w ^ -sign) & 7;
  735. Debug.Assert(sign == 0 || sign == 1);
  736. Debug.Assert(0 <= abs && abs < 8);
  737. for (int i = 0, off = 0; i < 8; ++i)
  738. {
  739. int cond = ((i ^ abs) - 1) >> 31;
  740. F.CMov(cond, table, off, r.x, 0); off += F.Size;
  741. F.CMov(cond, table, off, r.y, 0); off += F.Size;
  742. F.CMov(cond, table, off, r.z, 0); off += F.Size;
  743. }
  744. F.CNegate(sign, r.x);
  745. }
  746. #endif
  747. private static void PointLookup15(uint[] table, ref PointProjective r)
  748. {
  749. int off = F.Size * 3 * 7;
  750. F.Copy(table, off, r.x, 0); off += F.Size;
  751. F.Copy(table, off, r.y, 0); off += F.Size;
  752. F.Copy(table, off, r.z, 0);
  753. }
  754. private static uint[] PointPrecompute(ref PointProjective p, int count)
  755. {
  756. Debug.Assert(count > 0);
  757. Init(out PointProjective q);
  758. PointCopy(ref p, ref q);
  759. Init(out PointProjective d);
  760. PointCopy(ref q, ref d);
  761. PointDouble(ref d);
  762. uint[] table = F.CreateTable(count * 3);
  763. int off = 0;
  764. int i = 0;
  765. for (;;)
  766. {
  767. F.Copy(q.x, 0, table, off); off += F.Size;
  768. F.Copy(q.y, 0, table, off); off += F.Size;
  769. F.Copy(q.z, 0, table, off); off += F.Size;
  770. if (++i == count)
  771. break;
  772. PointAdd(ref d, ref q);
  773. }
  774. return table;
  775. }
  776. private static void PointPrecomputeVar(ref PointProjective p, PointProjective[] points, int count)
  777. {
  778. Debug.Assert(count > 0);
  779. Init(out PointProjective d);
  780. PointCopy(ref p, ref d);
  781. PointDouble(ref d);
  782. Init(out points[0]);
  783. PointCopy(ref p, ref points[0]);
  784. for (int i = 1; i < count; ++i)
  785. {
  786. Init(out points[i]);
  787. PointCopy(ref points[i - 1], ref points[i]);
  788. PointAdd(ref d, ref points[i]);
  789. }
  790. }
  791. private static void PointSetNeutral(ref PointProjective p)
  792. {
  793. F.Zero(p.x);
  794. F.One(p.y);
  795. F.One(p.z);
  796. }
  797. public static void Precompute()
  798. {
  799. lock (PrecompLock)
  800. {
  801. if (PrecompBaseWnaf != null && PrecompBaseComb != null)
  802. return;
  803. Debug.Assert(PrecompRange > 448);
  804. Debug.Assert(PrecompRange < 480);
  805. int wnafPoints = 1 << (WnafWidthBase - 2);
  806. int combPoints = PrecompBlocks * PrecompPoints;
  807. int totalPoints = wnafPoints + combPoints;
  808. PointProjective[] points = new PointProjective[totalPoints];
  809. Init(out PointProjective p);
  810. F.Copy(B_x, 0, p.x, 0);
  811. F.Copy(B_y, 0, p.y, 0);
  812. F.One(p.z);
  813. PointPrecomputeVar(ref p, points, wnafPoints);
  814. int pointsIndex = wnafPoints;
  815. PointProjective[] toothPowers = new PointProjective[PrecompTeeth];
  816. for (int tooth = 0; tooth < PrecompTeeth; ++tooth)
  817. {
  818. Init(out toothPowers[tooth]);
  819. }
  820. for (int block = 0; block < PrecompBlocks; ++block)
  821. {
  822. ref PointProjective sum = ref points[pointsIndex++];
  823. Init(out sum);
  824. for (int tooth = 0; tooth < PrecompTeeth; ++tooth)
  825. {
  826. if (tooth == 0)
  827. {
  828. PointCopy(ref p, ref sum);
  829. }
  830. else
  831. {
  832. PointAdd(ref p, ref sum);
  833. }
  834. PointDouble(ref p);
  835. PointCopy(ref p, ref toothPowers[tooth]);
  836. if (block + tooth != PrecompBlocks + PrecompTeeth - 2)
  837. {
  838. for (int spacing = 1; spacing < PrecompSpacing; ++spacing)
  839. {
  840. PointDouble(ref p);
  841. }
  842. }
  843. }
  844. F.Negate(sum.x, sum.x);
  845. for (int tooth = 0; tooth < (PrecompTeeth - 1); ++tooth)
  846. {
  847. int size = 1 << tooth;
  848. for (int j = 0; j < size; ++j, ++pointsIndex)
  849. {
  850. Init(out points[pointsIndex]);
  851. PointCopy(ref points[pointsIndex - size], ref points[pointsIndex]);
  852. PointAdd(ref toothPowers[tooth], ref points[pointsIndex]);
  853. }
  854. }
  855. }
  856. Debug.Assert(pointsIndex == totalPoints);
  857. InvertZs(points);
  858. PrecompBaseWnaf = new PointAffine[wnafPoints];
  859. for (int i = 0; i < wnafPoints; ++i)
  860. {
  861. ref PointProjective q = ref points[i];
  862. ref PointAffine r = ref PrecompBaseWnaf[i];
  863. Init(out r);
  864. F.Mul(q.x, q.z, r.x); F.Normalize(r.x);
  865. F.Mul(q.y, q.z, r.y); F.Normalize(r.y);
  866. }
  867. PrecompBaseComb = F.CreateTable(combPoints * 2);
  868. int off = 0;
  869. for (int i = wnafPoints; i < totalPoints; ++i)
  870. {
  871. ref PointProjective q = ref points[i];
  872. F.Mul(q.x, q.z, q.x); F.Normalize(q.x);
  873. F.Mul(q.y, q.z, q.y); F.Normalize(q.y);
  874. F.Copy(q.x, 0, PrecompBaseComb, off); off += F.Size;
  875. F.Copy(q.y, 0, PrecompBaseComb, off); off += F.Size;
  876. }
  877. Debug.Assert(off == PrecompBaseComb.Length);
  878. }
  879. }
  880. private static void PruneScalar(byte[] n, int nOff, byte[] r)
  881. {
  882. Array.Copy(n, nOff, r, 0, ScalarBytes - 1);
  883. r[0] &= 0xFC;
  884. r[ScalarBytes - 2] |= 0x80;
  885. r[ScalarBytes - 1] = 0x00;
  886. }
  887. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  888. private static void PruneScalar(ReadOnlySpan<byte> n, Span<byte> r)
  889. {
  890. n[..(ScalarBytes - 1)].CopyTo(r);
  891. r[0] &= 0xFC;
  892. r[ScalarBytes - 2] |= 0x80;
  893. r[ScalarBytes - 1] = 0x00;
  894. }
  895. #endif
  896. private static byte[] ReduceScalar(byte[] n)
  897. {
  898. ulong x00 = Decode32(n, 0); // x00:32/--
  899. ulong x01 = (Decode24(n, 4) << 4); // x01:28/--
  900. ulong x02 = Decode32(n, 7); // x02:32/--
  901. ulong x03 = (Decode24(n, 11) << 4); // x03:28/--
  902. ulong x04 = Decode32(n, 14); // x04:32/--
  903. ulong x05 = (Decode24(n, 18) << 4); // x05:28/--
  904. ulong x06 = Decode32(n, 21); // x06:32/--
  905. ulong x07 = (Decode24(n, 25) << 4); // x07:28/--
  906. ulong x08 = Decode32(n, 28); // x08:32/--
  907. ulong x09 = (Decode24(n, 32) << 4); // x09:28/--
  908. ulong x10 = Decode32(n, 35); // x10:32/--
  909. ulong x11 = (Decode24(n, 39) << 4); // x11:28/--
  910. ulong x12 = Decode32(n, 42); // x12:32/--
  911. ulong x13 = (Decode24(n, 46) << 4); // x13:28/--
  912. ulong x14 = Decode32(n, 49); // x14:32/--
  913. ulong x15 = (Decode24(n, 53) << 4); // x15:28/--
  914. ulong x16 = Decode32(n, 56); // x16:32/--
  915. ulong x17 = (Decode24(n, 60) << 4); // x17:28/--
  916. ulong x18 = Decode32(n, 63); // x18:32/--
  917. ulong x19 = (Decode24(n, 67) << 4); // x19:28/--
  918. ulong x20 = Decode32(n, 70); // x20:32/--
  919. ulong x21 = (Decode24(n, 74) << 4); // x21:28/--
  920. ulong x22 = Decode32(n, 77); // x22:32/--
  921. ulong x23 = (Decode24(n, 81) << 4); // x23:28/--
  922. ulong x24 = Decode32(n, 84); // x24:32/--
  923. ulong x25 = (Decode24(n, 88) << 4); // x25:28/--
  924. ulong x26 = Decode32(n, 91); // x26:32/--
  925. ulong x27 = (Decode24(n, 95) << 4); // x27:28/--
  926. ulong x28 = Decode32(n, 98); // x28:32/--
  927. ulong x29 = (Decode24(n, 102) << 4); // x29:28/--
  928. ulong x30 = Decode32(n, 105); // x30:32/--
  929. ulong x31 = (Decode24(n, 109) << 4); // x31:28/--
  930. ulong x32 = Decode16(n, 112); // x32:16/--
  931. // x32 += (x31 >> 28); x31 &= M28UL;
  932. x16 += x32 * L4_0; // x16:42/--
  933. x17 += x32 * L4_1; // x17:41/28
  934. x18 += x32 * L4_2; // x18:43/42
  935. x19 += x32 * L4_3; // x19:44/28
  936. x20 += x32 * L4_4; // x20:43/--
  937. x21 += x32 * L4_5; // x21:44/28
  938. x22 += x32 * L4_6; // x22:43/41
  939. x23 += x32 * L4_7; // x23:45/41
  940. x31 += (x30 >> 28); x30 &= M28UL; // x31:28/--, x30:28/--
  941. x15 += x31 * L4_0; // x15:54/--
  942. x16 += x31 * L4_1; // x16:53/42
  943. x17 += x31 * L4_2; // x17:55/54
  944. x18 += x31 * L4_3; // x18:56/44
  945. x19 += x31 * L4_4; // x19:55/--
  946. x20 += x31 * L4_5; // x20:56/43
  947. x21 += x31 * L4_6; // x21:55/53
  948. x22 += x31 * L4_7; // x22:57/53
  949. //x30 += (x29 >> 28); x29 &= M28UL;
  950. x14 += x30 * L4_0; // x14:54/--
  951. x15 += x30 * L4_1; // x15:54/53
  952. x16 += x30 * L4_2; // x16:56/--
  953. x17 += x30 * L4_3; // x17:57/--
  954. x18 += x30 * L4_4; // x18:56/55
  955. x19 += x30 * L4_5; // x19:56/55
  956. x20 += x30 * L4_6; // x20:57/--
  957. x21 += x30 * L4_7; // x21:57/56
  958. x29 += (x28 >> 28); x28 &= M28UL; // x29:28/--, x28:28/--
  959. x13 += x29 * L4_0; // x13:54/--
  960. x14 += x29 * L4_1; // x14:54/53
  961. x15 += x29 * L4_2; // x15:56/--
  962. x16 += x29 * L4_3; // x16:57/--
  963. x17 += x29 * L4_4; // x17:57/55
  964. x18 += x29 * L4_5; // x18:57/55
  965. x19 += x29 * L4_6; // x19:57/52
  966. x20 += x29 * L4_7; // x20:58/52
  967. //x28 += (x27 >> 28); x27 &= M28UL;
  968. x12 += x28 * L4_0; // x12:54/--
  969. x13 += x28 * L4_1; // x13:54/53
  970. x14 += x28 * L4_2; // x14:56/--
  971. x15 += x28 * L4_3; // x15:57/--
  972. x16 += x28 * L4_4; // x16:57/55
  973. x17 += x28 * L4_5; // x17:58/--
  974. x18 += x28 * L4_6; // x18:58/--
  975. x19 += x28 * L4_7; // x19:58/53
  976. x27 += (x26 >> 28); x26 &= M28UL; // x27:28/--, x26:28/--
  977. x11 += x27 * L4_0; // x11:54/--
  978. x12 += x27 * L4_1; // x12:54/53
  979. x13 += x27 * L4_2; // x13:56/--
  980. x14 += x27 * L4_3; // x14:57/--
  981. x15 += x27 * L4_4; // x15:57/55
  982. x16 += x27 * L4_5; // x16:58/--
  983. x17 += x27 * L4_6; // x17:58/56
  984. x18 += x27 * L4_7; // x18:59/--
  985. //x26 += (x25 >> 28); x25 &= M28UL;
  986. x10 += x26 * L4_0; // x10:54/--
  987. x11 += x26 * L4_1; // x11:54/53
  988. x12 += x26 * L4_2; // x12:56/--
  989. x13 += x26 * L4_3; // x13:57/--
  990. x14 += x26 * L4_4; // x14:57/55
  991. x15 += x26 * L4_5; // x15:58/--
  992. x16 += x26 * L4_6; // x16:58/56
  993. x17 += x26 * L4_7; // x17:59/--
  994. x25 += (x24 >> 28); x24 &= M28UL; // x25:28/--, x24:28/--
  995. x09 += x25 * L4_0; // x09:54/--
  996. x10 += x25 * L4_1; // x10:54/53
  997. x11 += x25 * L4_2; // x11:56/--
  998. x12 += x25 * L4_3; // x12:57/--
  999. x13 += x25 * L4_4; // x13:57/55
  1000. x14 += x25 * L4_5; // x14:58/--
  1001. x15 += x25 * L4_6; // x15:58/56
  1002. x16 += x25 * L4_7; // x16:59/--
  1003. x21 += (x20 >> 28); x20 &= M28UL; // x21:58/--, x20:28/--
  1004. x22 += (x21 >> 28); x21 &= M28UL; // x22:57/54, x21:28/--
  1005. x23 += (x22 >> 28); x22 &= M28UL; // x23:45/42, x22:28/--
  1006. x24 += (x23 >> 28); x23 &= M28UL; // x24:28/18, x23:28/--
  1007. x08 += x24 * L4_0; // x08:54/--
  1008. x09 += x24 * L4_1; // x09:55/--
  1009. x10 += x24 * L4_2; // x10:56/46
  1010. x11 += x24 * L4_3; // x11:57/46
  1011. x12 += x24 * L4_4; // x12:57/55
  1012. x13 += x24 * L4_5; // x13:58/--
  1013. x14 += x24 * L4_6; // x14:58/56
  1014. x15 += x24 * L4_7; // x15:59/--
  1015. x07 += x23 * L4_0; // x07:54/--
  1016. x08 += x23 * L4_1; // x08:54/53
  1017. x09 += x23 * L4_2; // x09:56/53
  1018. x10 += x23 * L4_3; // x10:57/46
  1019. x11 += x23 * L4_4; // x11:57/55
  1020. x12 += x23 * L4_5; // x12:58/--
  1021. x13 += x23 * L4_6; // x13:58/56
  1022. x14 += x23 * L4_7; // x14:59/--
  1023. x06 += x22 * L4_0; // x06:54/--
  1024. x07 += x22 * L4_1; // x07:54/53
  1025. x08 += x22 * L4_2; // x08:56/--
  1026. x09 += x22 * L4_3; // x09:57/53
  1027. x10 += x22 * L4_4; // x10:57/55
  1028. x11 += x22 * L4_5; // x11:58/--
  1029. x12 += x22 * L4_6; // x12:58/56
  1030. x13 += x22 * L4_7; // x13:59/--
  1031. x18 += (x17 >> 28); x17 &= M28UL; // x18:59/31, x17:28/--
  1032. x19 += (x18 >> 28); x18 &= M28UL; // x19:58/54, x18:28/--
  1033. x20 += (x19 >> 28); x19 &= M28UL; // x20:30/29, x19:28/--
  1034. x21 += (x20 >> 28); x20 &= M28UL; // x21:28/03, x20:28/--
  1035. x05 += x21 * L4_0; // x05:54/--
  1036. x06 += x21 * L4_1; // x06:55/--
  1037. x07 += x21 * L4_2; // x07:56/31
  1038. x08 += x21 * L4_3; // x08:57/31
  1039. x09 += x21 * L4_4; // x09:57/56
  1040. x10 += x21 * L4_5; // x10:58/--
  1041. x11 += x21 * L4_6; // x11:58/56
  1042. x12 += x21 * L4_7; // x12:59/--
  1043. x04 += x20 * L4_0; // x04:54/--
  1044. x05 += x20 * L4_1; // x05:54/53
  1045. x06 += x20 * L4_2; // x06:56/53
  1046. x07 += x20 * L4_3; // x07:57/31
  1047. x08 += x20 * L4_4; // x08:57/55
  1048. x09 += x20 * L4_5; // x09:58/--
  1049. x10 += x20 * L4_6; // x10:58/56
  1050. x11 += x20 * L4_7; // x11:59/--
  1051. x03 += x19 * L4_0; // x03:54/--
  1052. x04 += x19 * L4_1; // x04:54/53
  1053. x05 += x19 * L4_2; // x05:56/--
  1054. x06 += x19 * L4_3; // x06:57/53
  1055. x07 += x19 * L4_4; // x07:57/55
  1056. x08 += x19 * L4_5; // x08:58/--
  1057. x09 += x19 * L4_6; // x09:58/56
  1058. x10 += x19 * L4_7; // x10:59/--
  1059. x15 += (x14 >> 28); x14 &= M28UL; // x15:59/31, x14:28/--
  1060. x16 += (x15 >> 28); x15 &= M28UL; // x16:59/32, x15:28/--
  1061. x17 += (x16 >> 28); x16 &= M28UL; // x17:31/29, x16:28/--
  1062. x18 += (x17 >> 28); x17 &= M28UL; // x18:28/04, x17:28/--
  1063. x02 += x18 * L4_0; // x02:54/--
  1064. x03 += x18 * L4_1; // x03:55/--
  1065. x04 += x18 * L4_2; // x04:56/32
  1066. x05 += x18 * L4_3; // x05:57/32
  1067. x06 += x18 * L4_4; // x06:57/56
  1068. x07 += x18 * L4_5; // x07:58/--
  1069. x08 += x18 * L4_6; // x08:58/56
  1070. x09 += x18 * L4_7; // x09:59/--
  1071. x01 += x17 * L4_0; // x01:54/--
  1072. x02 += x17 * L4_1; // x02:54/53
  1073. x03 += x17 * L4_2; // x03:56/53
  1074. x04 += x17 * L4_3; // x04:57/32
  1075. x05 += x17 * L4_4; // x05:57/55
  1076. x06 += x17 * L4_5; // x06:58/--
  1077. x07 += x17 * L4_6; // x07:58/56
  1078. x08 += x17 * L4_7; // x08:59/--
  1079. x16 *= 4;
  1080. x16 += (x15 >> 26); x15 &= M26UL;
  1081. x16 += 1; // x16:30/01
  1082. x00 += x16 * L_0;
  1083. x01 += x16 * L_1;
  1084. x02 += x16 * L_2;
  1085. x03 += x16 * L_3;
  1086. x04 += x16 * L_4;
  1087. x05 += x16 * L_5;
  1088. x06 += x16 * L_6;
  1089. x07 += x16 * L_7;
  1090. x01 += (x00 >> 28); x00 &= M28UL;
  1091. x02 += (x01 >> 28); x01 &= M28UL;
  1092. x03 += (x02 >> 28); x02 &= M28UL;
  1093. x04 += (x03 >> 28); x03 &= M28UL;
  1094. x05 += (x04 >> 28); x04 &= M28UL;
  1095. x06 += (x05 >> 28); x05 &= M28UL;
  1096. x07 += (x06 >> 28); x06 &= M28UL;
  1097. x08 += (x07 >> 28); x07 &= M28UL;
  1098. x09 += (x08 >> 28); x08 &= M28UL;
  1099. x10 += (x09 >> 28); x09 &= M28UL;
  1100. x11 += (x10 >> 28); x10 &= M28UL;
  1101. x12 += (x11 >> 28); x11 &= M28UL;
  1102. x13 += (x12 >> 28); x12 &= M28UL;
  1103. x14 += (x13 >> 28); x13 &= M28UL;
  1104. x15 += (x14 >> 28); x14 &= M28UL;
  1105. x16 = (x15 >> 26); x15 &= M26UL;
  1106. x16 -= 1;
  1107. Debug.Assert(x16 == 0UL || x16 == ulong.MaxValue);
  1108. x00 -= x16 & L_0;
  1109. x01 -= x16 & L_1;
  1110. x02 -= x16 & L_2;
  1111. x03 -= x16 & L_3;
  1112. x04 -= x16 & L_4;
  1113. x05 -= x16 & L_5;
  1114. x06 -= x16 & L_6;
  1115. x07 -= x16 & L_7;
  1116. x01 += (ulong)((long)x00 >> 28); x00 &= M28UL;
  1117. x02 += (ulong)((long)x01 >> 28); x01 &= M28UL;
  1118. x03 += (ulong)((long)x02 >> 28); x02 &= M28UL;
  1119. x04 += (ulong)((long)x03 >> 28); x03 &= M28UL;
  1120. x05 += (ulong)((long)x04 >> 28); x04 &= M28UL;
  1121. x06 += (ulong)((long)x05 >> 28); x05 &= M28UL;
  1122. x07 += (ulong)((long)x06 >> 28); x06 &= M28UL;
  1123. x08 += (ulong)((long)x07 >> 28); x07 &= M28UL;
  1124. x09 += (ulong)((long)x08 >> 28); x08 &= M28UL;
  1125. x10 += (ulong)((long)x09 >> 28); x09 &= M28UL;
  1126. x11 += (ulong)((long)x10 >> 28); x10 &= M28UL;
  1127. x12 += (ulong)((long)x11 >> 28); x11 &= M28UL;
  1128. x13 += (ulong)((long)x12 >> 28); x12 &= M28UL;
  1129. x14 += (ulong)((long)x13 >> 28); x13 &= M28UL;
  1130. x15 += (ulong)((long)x14 >> 28); x14 &= M28UL;
  1131. Debug.Assert(x15 >> 26 == 0UL);
  1132. byte[] r = new byte[ScalarBytes];
  1133. Encode56(x00 | (x01 << 28), r, 0);
  1134. Encode56(x02 | (x03 << 28), r, 7);
  1135. Encode56(x04 | (x05 << 28), r, 14);
  1136. Encode56(x06 | (x07 << 28), r, 21);
  1137. Encode56(x08 | (x09 << 28), r, 28);
  1138. Encode56(x10 | (x11 << 28), r, 35);
  1139. Encode56(x12 | (x13 << 28), r, 42);
  1140. Encode56(x14 | (x15 << 28), r, 49);
  1141. //r[ScalarBytes - 1] = 0;
  1142. return r;
  1143. }
  1144. private static void ScalarMult(byte[] k, ref PointProjective p, ref PointProjective r)
  1145. {
  1146. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1147. ScalarMult(k.AsSpan(), ref p, ref r);
  1148. #else
  1149. uint[] n = new uint[ScalarUints];
  1150. DecodeScalar(k, 0, n);
  1151. // Recode the scalar into signed-digit form
  1152. {
  1153. uint c1 = Nat.CAdd(ScalarUints, ~(int)n[0] & 1, n, L, n);
  1154. uint c2 = Nat.ShiftDownBit(ScalarUints, n, c1); Debug.Assert(c2 == (1U << 31));
  1155. // NOTE: Bit 448 is implicitly set after the signed-digit recoding
  1156. }
  1157. uint[] table = PointPrecompute(ref p, 8);
  1158. Init(out PointProjective q);
  1159. // Replace first 4 doublings (2^4 * P) with 1 addition (P + 15 * P)
  1160. PointLookup15(table, ref r);
  1161. PointAdd(ref p, ref r);
  1162. int w = 111;
  1163. for (;;)
  1164. {
  1165. PointLookup(n, w, table, ref q);
  1166. PointAdd(ref q, ref r);
  1167. if (--w < 0)
  1168. break;
  1169. for (int i = 0; i < 4; ++i)
  1170. {
  1171. PointDouble(ref r);
  1172. }
  1173. }
  1174. #endif
  1175. }
  1176. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1177. private static void ScalarMult(ReadOnlySpan<byte> k, ref PointProjective p, ref PointProjective r)
  1178. {
  1179. Span<uint> n = stackalloc uint[ScalarUints];
  1180. DecodeScalar(k, n);
  1181. // Recode the scalar into signed-digit form
  1182. {
  1183. uint c1 = Nat.CAdd(ScalarUints, ~(int)n[0] & 1, n, L, n);
  1184. uint c2 = Nat.ShiftDownBit(ScalarUints, n, c1); Debug.Assert(c2 == (1U << 31));
  1185. // NOTE: Bit 448 is implicitly set after the signed-digit recoding
  1186. }
  1187. uint[] table = PointPrecompute(ref p, 8);
  1188. Init(out PointProjective q);
  1189. // Replace first 4 doublings (2^4 * P) with 1 addition (P + 15 * P)
  1190. PointLookup15(table, ref r);
  1191. PointAdd(ref p, ref r);
  1192. int w = 111;
  1193. for (;;)
  1194. {
  1195. PointLookup(n, w, table, ref q);
  1196. PointAdd(ref q, ref r);
  1197. if (--w < 0)
  1198. break;
  1199. for (int i = 0; i < 4; ++i)
  1200. {
  1201. PointDouble(ref r);
  1202. }
  1203. }
  1204. }
  1205. #endif
  1206. private static void ScalarMultBase(byte[] k, ref PointProjective r)
  1207. {
  1208. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1209. ScalarMultBase(k.AsSpan(), ref r);
  1210. #else
  1211. // Equivalent (but much slower)
  1212. //Init(out PointProjective p);
  1213. //F.Copy(B_x, 0, p.x, 0);
  1214. //F.Copy(B_y, 0, p.y, 0);
  1215. //F.One(p.z);
  1216. //ScalarMult(k, ref p, ref r);
  1217. Precompute();
  1218. uint[] n = new uint[ScalarUints + 1];
  1219. DecodeScalar(k, 0, n);
  1220. // Recode the scalar into signed-digit form
  1221. {
  1222. n[ScalarUints] = (1U << (PrecompRange - 448))
  1223. + Nat.CAdd(ScalarUints, ~(int)n[0] & 1, n, L, n);
  1224. uint c = Nat.ShiftDownBit(n.Length, n, 0);
  1225. Debug.Assert(c == (1U << 31));
  1226. }
  1227. Init(out PointAffine p);
  1228. PointSetNeutral(ref r);
  1229. int cOff = PrecompSpacing - 1;
  1230. for (;;)
  1231. {
  1232. int tPos = cOff;
  1233. for (int b = 0; b < PrecompBlocks; ++b)
  1234. {
  1235. uint w = 0;
  1236. for (int t = 0; t < PrecompTeeth; ++t)
  1237. {
  1238. uint tBit = n[tPos >> 5] >> (tPos & 0x1F);
  1239. w &= ~(1U << t);
  1240. w ^= (tBit << t);
  1241. tPos += PrecompSpacing;
  1242. }
  1243. int sign = (int)(w >> (PrecompTeeth - 1)) & 1;
  1244. int abs = ((int)w ^ -sign) & PrecompMask;
  1245. Debug.Assert(sign == 0 || sign == 1);
  1246. Debug.Assert(0 <= abs && abs < PrecompPoints);
  1247. PointLookup(b, abs, ref p);
  1248. F.CNegate(sign, p.x);
  1249. PointAdd(ref p, ref r);
  1250. }
  1251. if (--cOff < 0)
  1252. break;
  1253. PointDouble(ref r);
  1254. }
  1255. #endif
  1256. }
  1257. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1258. private static void ScalarMultBase(ReadOnlySpan<byte> k, ref PointProjective r)
  1259. {
  1260. // Equivalent (but much slower)
  1261. //Init(out PointProjective p);
  1262. //F.Copy(B_x, 0, p.x, 0);
  1263. //F.Copy(B_y, 0, p.y, 0);
  1264. //F.One(p.z);
  1265. //ScalarMult(k, ref p, ref r);
  1266. Precompute();
  1267. Span<uint> n = stackalloc uint[ScalarUints + 1];
  1268. DecodeScalar(k, n);
  1269. // Recode the scalar into signed-digit form
  1270. {
  1271. n[ScalarUints] = (1U << (PrecompRange - 448))
  1272. + Nat.CAdd(ScalarUints, ~(int)n[0] & 1, n, L, n);
  1273. uint c = Nat.ShiftDownBit(n.Length, n, 0);
  1274. Debug.Assert(c == (1U << 31));
  1275. }
  1276. Init(out PointAffine p);
  1277. PointSetNeutral(ref r);
  1278. int cOff = PrecompSpacing - 1;
  1279. for (;;)
  1280. {
  1281. int tPos = cOff;
  1282. for (int b = 0; b < PrecompBlocks; ++b)
  1283. {
  1284. uint w = 0;
  1285. for (int t = 0; t < PrecompTeeth; ++t)
  1286. {
  1287. uint tBit = n[tPos >> 5] >> (tPos & 0x1F);
  1288. w &= ~(1U << t);
  1289. w ^= (tBit << t);
  1290. tPos += PrecompSpacing;
  1291. }
  1292. int sign = (int)(w >> (PrecompTeeth - 1)) & 1;
  1293. int abs = ((int)w ^ -sign) & PrecompMask;
  1294. Debug.Assert(sign == 0 || sign == 1);
  1295. Debug.Assert(0 <= abs && abs < PrecompPoints);
  1296. PointLookup(b, abs, ref p);
  1297. F.CNegate(sign, p.x);
  1298. PointAdd(ref p, ref r);
  1299. }
  1300. if (--cOff < 0)
  1301. break;
  1302. PointDouble(ref r);
  1303. }
  1304. }
  1305. #endif
  1306. private static void ScalarMultBaseEncoded(byte[] k, byte[] r, int rOff)
  1307. {
  1308. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1309. ScalarMultBaseEncoded(k.AsSpan(), r.AsSpan(rOff));
  1310. #else
  1311. Init(out PointProjective p);
  1312. ScalarMultBase(k, ref p);
  1313. if (0 == EncodePoint(ref p, r, rOff))
  1314. throw new InvalidOperationException();
  1315. #endif
  1316. }
  1317. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1318. private static void ScalarMultBaseEncoded(ReadOnlySpan<byte> k, Span<byte> r)
  1319. {
  1320. Init(out PointProjective p);
  1321. ScalarMultBase(k, ref p);
  1322. if (0 == EncodePoint(ref p, r))
  1323. throw new InvalidOperationException();
  1324. }
  1325. #endif
  1326. internal static void ScalarMultBaseXY(byte[] k, int kOff, uint[] x, uint[] y)
  1327. {
  1328. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1329. ScalarMultBaseXY(k.AsSpan(kOff), x.AsSpan(), y.AsSpan());
  1330. #else
  1331. byte[] n = new byte[ScalarBytes];
  1332. PruneScalar(k, kOff, n);
  1333. Init(out PointProjective p);
  1334. ScalarMultBase(n, ref p);
  1335. if (0 == CheckPoint(p.x, p.y, p.z))
  1336. throw new InvalidOperationException();
  1337. F.Copy(p.x, 0, x, 0);
  1338. F.Copy(p.y, 0, y, 0);
  1339. #endif
  1340. }
  1341. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  1342. internal static void ScalarMultBaseXY(ReadOnlySpan<byte> k, Span<uint> x, Span<uint> y)
  1343. {
  1344. Span<byte> n = stackalloc byte[ScalarBytes];
  1345. PruneScalar(k, n);
  1346. Init(out PointProjective p);
  1347. ScalarMultBase(n, ref p);
  1348. if (0 == CheckPoint(p.x, p.y, p.z))
  1349. throw new InvalidOperationException();
  1350. F.Copy(p.x, x);
  1351. F.Copy(p.y, y);
  1352. }
  1353. #endif
  1354. private static void ScalarMultOrderVar(ref PointProjective p, ref PointProjective r)
  1355. {
  1356. sbyte[] ws_p = GetWnafVar(L, WnafWidth);
  1357. int count = 1 << (WnafWidth - 2);
  1358. PointProjective[] tp = new PointProjective[count];
  1359. PointPrecomputeVar(ref p, tp, count);
  1360. PointSetNeutral(ref r);
  1361. for (int bit = 446;;)
  1362. {
  1363. int wp = ws_p[bit];
  1364. if (wp != 0)
  1365. {
  1366. int sign = wp >> 31;
  1367. int index = (wp ^ sign) >> 1;
  1368. PointAddVar(sign != 0, ref tp[index], ref r);
  1369. }
  1370. if (--bit < 0)
  1371. break;
  1372. PointDouble(ref r);
  1373. }
  1374. }
  1375. private static void ScalarMultStrausVar(uint[] nb, uint[] np, ref PointProjective p, ref PointProjective r)
  1376. {
  1377. Precompute();
  1378. sbyte[] ws_b = GetWnafVar(nb, WnafWidthBase);
  1379. sbyte[] ws_p = GetWnafVar(np, WnafWidth);
  1380. int count = 1 << (WnafWidth - 2);
  1381. PointProjective[] tp = new PointProjective[count];
  1382. PointPrecomputeVar(ref p, tp, count);
  1383. PointSetNeutral(ref r);
  1384. for (int bit = 446;;)
  1385. {
  1386. int wb = ws_b[bit];
  1387. if (wb != 0)
  1388. {
  1389. int sign = wb >> 31;
  1390. int index = (wb ^ sign) >> 1;
  1391. PointAddVar(sign != 0, ref PrecompBaseWnaf[index], ref r);
  1392. }
  1393. int wp = ws_p[bit];
  1394. if (wp != 0)
  1395. {
  1396. int sign = wp >> 31;
  1397. int index = (wp ^ sign) >> 1;
  1398. PointAddVar(sign != 0, ref tp[index], ref r);
  1399. }
  1400. if (--bit < 0)
  1401. break;
  1402. PointDouble(ref r);
  1403. }
  1404. }
  1405. public static void Sign(byte[] sk, int skOff, byte[] ctx, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  1406. {
  1407. byte phflag = 0x00;
  1408. ImplSign(sk, skOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  1409. }
  1410. public static void Sign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  1411. {
  1412. byte phflag = 0x00;
  1413. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  1414. }
  1415. public static void SignPrehash(byte[] sk, int skOff, byte[] ctx, byte[] ph, int phOff, byte[] sig, int sigOff)
  1416. {
  1417. byte phflag = 0x01;
  1418. ImplSign(sk, skOff, ctx, phflag, ph, phOff, PrehashSize, sig, sigOff);
  1419. }
  1420. public static void SignPrehash(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte[] ph, int phOff, byte[] sig, int sigOff)
  1421. {
  1422. byte phflag = 0x01;
  1423. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, ph, phOff, PrehashSize, sig, sigOff);
  1424. }
  1425. public static void SignPrehash(byte[] sk, int skOff, byte[] ctx, IXof ph, byte[] sig, int sigOff)
  1426. {
  1427. byte[] m = new byte[PrehashSize];
  1428. if (PrehashSize != ph.OutputFinal(m, 0, PrehashSize))
  1429. throw new ArgumentException("ph");
  1430. byte phflag = 0x01;
  1431. ImplSign(sk, skOff, ctx, phflag, m, 0, m.Length, sig, sigOff);
  1432. }
  1433. public static void SignPrehash(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, IXof ph, byte[] sig, int sigOff)
  1434. {
  1435. byte[] m = new byte[PrehashSize];
  1436. if (PrehashSize != ph.OutputFinal(m, 0, PrehashSize))
  1437. throw new ArgumentException("ph");
  1438. byte phflag = 0x01;
  1439. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, 0, m.Length, sig, sigOff);
  1440. }
  1441. public static bool ValidatePublicKeyFull(byte[] pk, int pkOff)
  1442. {
  1443. Init(out PointProjective p);
  1444. if (!DecodePointVar(pk, pkOff, false, ref p))
  1445. return false;
  1446. F.Normalize(p.x);
  1447. F.Normalize(p.y);
  1448. F.Normalize(p.z);
  1449. if (IsNeutralElementVar(p.x, p.y, p.z))
  1450. return false;
  1451. Init(out PointProjective r);
  1452. ScalarMultOrderVar(ref p, ref r);
  1453. F.Normalize(r.x);
  1454. F.Normalize(r.y);
  1455. F.Normalize(r.z);
  1456. return IsNeutralElementVar(r.x, r.y, r.z);
  1457. }
  1458. public static bool ValidatePublicKeyPartial(byte[] pk, int pkOff)
  1459. {
  1460. Init(out PointProjective p);
  1461. return DecodePointVar(pk, pkOff, false, ref p);
  1462. }
  1463. public static bool Verify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte[] m, int mOff, int mLen)
  1464. {
  1465. byte phflag = 0x00;
  1466. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, mOff, mLen);
  1467. }
  1468. public static bool VerifyPrehash(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte[] ph, int phOff)
  1469. {
  1470. byte phflag = 0x01;
  1471. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, ph, phOff, PrehashSize);
  1472. }
  1473. public static bool VerifyPrehash(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, IXof ph)
  1474. {
  1475. byte[] m = new byte[PrehashSize];
  1476. if (PrehashSize != ph.OutputFinal(m, 0, PrehashSize))
  1477. throw new ArgumentException("ph");
  1478. byte phflag = 0x01;
  1479. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, 0, m.Length);
  1480. }
  1481. }
  1482. }
  1483. #pragma warning restore
  1484. #endif