Ed25519.cs 60 KB

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