1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Diagnostics;
- using Org.BouncyCastle.Crypto.Utilities;
- namespace Org.BouncyCastle.Math.Raw
- {
- internal abstract class Nat224
- {
- private const ulong M = 0xFFFFFFFFUL;
- public static uint Add(uint[] x, uint[] y, uint[] z)
- {
- ulong c = 0;
- c += (ulong)x[0] + y[0];
- z[0] = (uint)c;
- c >>= 32;
- c += (ulong)x[1] + y[1];
- z[1] = (uint)c;
- c >>= 32;
- c += (ulong)x[2] + y[2];
- z[2] = (uint)c;
- c >>= 32;
- c += (ulong)x[3] + y[3];
- z[3] = (uint)c;
- c >>= 32;
- c += (ulong)x[4] + y[4];
- z[4] = (uint)c;
- c >>= 32;
- c += (ulong)x[5] + y[5];
- z[5] = (uint)c;
- c >>= 32;
- c += (ulong)x[6] + y[6];
- z[6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint Add(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
- {
- ulong c = 0;
- c += (ulong)x[xOff + 0] + y[yOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 1] + y[yOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 2] + y[yOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 3] + y[yOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 4] + y[yOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 5] + y[yOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 6] + y[yOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint AddBothTo(uint[] x, uint[] y, uint[] z)
- {
- ulong c = 0;
- c += (ulong)x[0] + y[0] + z[0];
- z[0] = (uint)c;
- c >>= 32;
- c += (ulong)x[1] + y[1] + z[1];
- z[1] = (uint)c;
- c >>= 32;
- c += (ulong)x[2] + y[2] + z[2];
- z[2] = (uint)c;
- c >>= 32;
- c += (ulong)x[3] + y[3] + z[3];
- z[3] = (uint)c;
- c >>= 32;
- c += (ulong)x[4] + y[4] + z[4];
- z[4] = (uint)c;
- c >>= 32;
- c += (ulong)x[5] + y[5] + z[5];
- z[5] = (uint)c;
- c >>= 32;
- c += (ulong)x[6] + y[6] + z[6];
- z[6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint AddBothTo(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
- {
- ulong c = 0;
- c += (ulong)x[xOff + 0] + y[yOff + 0] + z[zOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 1] + y[yOff + 1] + z[zOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 2] + y[yOff + 2] + z[zOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 3] + y[yOff + 3] + z[zOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 4] + y[yOff + 4] + z[zOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 5] + y[yOff + 5] + z[zOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 6] + y[yOff + 6] + z[zOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint AddTo(uint[] x, uint[] z)
- {
- ulong c = 0;
- c += (ulong)x[0] + z[0];
- z[0] = (uint)c;
- c >>= 32;
- c += (ulong)x[1] + z[1];
- z[1] = (uint)c;
- c >>= 32;
- c += (ulong)x[2] + z[2];
- z[2] = (uint)c;
- c >>= 32;
- c += (ulong)x[3] + z[3];
- z[3] = (uint)c;
- c >>= 32;
- c += (ulong)x[4] + z[4];
- z[4] = (uint)c;
- c >>= 32;
- c += (ulong)x[5] + z[5];
- z[5] = (uint)c;
- c >>= 32;
- c += (ulong)x[6] + z[6];
- z[6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint AddTo(uint[] x, int xOff, uint[] z, int zOff, uint cIn)
- {
- ulong c = cIn;
- c += (ulong)x[xOff + 0] + z[zOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 1] + z[zOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 2] + z[zOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 3] + z[zOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 4] + z[zOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 5] + z[zOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- c += (ulong)x[xOff + 6] + z[zOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint AddToEachOther(uint[] u, int uOff, uint[] v, int vOff)
- {
- ulong c = 0;
- c += (ulong)u[uOff + 0] + v[vOff + 0];
- u[uOff + 0] = (uint)c;
- v[vOff + 0] = (uint)c;
- c >>= 32;
- c += (ulong)u[uOff + 1] + v[vOff + 1];
- u[uOff + 1] = (uint)c;
- v[vOff + 1] = (uint)c;
- c >>= 32;
- c += (ulong)u[uOff + 2] + v[vOff + 2];
- u[uOff + 2] = (uint)c;
- v[vOff + 2] = (uint)c;
- c >>= 32;
- c += (ulong)u[uOff + 3] + v[vOff + 3];
- u[uOff + 3] = (uint)c;
- v[vOff + 3] = (uint)c;
- c >>= 32;
- c += (ulong)u[uOff + 4] + v[vOff + 4];
- u[uOff + 4] = (uint)c;
- v[vOff + 4] = (uint)c;
- c >>= 32;
- c += (ulong)u[uOff + 5] + v[vOff + 5];
- u[uOff + 5] = (uint)c;
- v[vOff + 5] = (uint)c;
- c >>= 32;
- c += (ulong)u[uOff + 6] + v[vOff + 6];
- u[uOff + 6] = (uint)c;
- v[vOff + 6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static void Copy(uint[] x, uint[] z)
- {
- z[0] = x[0];
- z[1] = x[1];
- z[2] = x[2];
- z[3] = x[3];
- z[4] = x[4];
- z[5] = x[5];
- z[6] = x[6];
- }
- public static uint[] Create()
- {
- return new uint[7];
- }
- public static uint[] CreateExt()
- {
- return new uint[14];
- }
- public static bool Diff(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
- {
- bool pos = Gte(x, xOff, y, yOff);
- if (pos)
- {
- Sub(x, xOff, y, yOff, z, zOff);
- }
- else
- {
- Sub(y, yOff, x, xOff, z, zOff);
- }
- return pos;
- }
- public static bool Eq(uint[] x, uint[] y)
- {
- for (int i = 6; i >= 0; --i)
- {
- if (x[i] != y[i])
- return false;
- }
- return true;
- }
- public static uint[] FromBigInteger(BigInteger x)
- {
- if (x.SignValue < 0 || x.BitLength > 224)
- throw new ArgumentException();
- uint[] z = Create();
- int i = 0;
- while (x.SignValue != 0)
- {
- z[i++] = (uint)x.IntValue;
- x = x.ShiftRight(32);
- }
- return z;
- }
- public static uint GetBit(uint[] x, int bit)
- {
- if (bit == 0)
- {
- return x[0] & 1;
- }
- int w = bit >> 5;
- if (w < 0 || w >= 7)
- {
- return 0;
- }
- int b = bit & 31;
- return (x[w] >> b) & 1;
- }
- public static bool Gte(uint[] x, uint[] y)
- {
- for (int i = 6; i >= 0; --i)
- {
- uint x_i = x[i], y_i = y[i];
- if (x_i < y_i)
- return false;
- if (x_i > y_i)
- return true;
- }
- return true;
- }
- public static bool Gte(uint[] x, int xOff, uint[] y, int yOff)
- {
- for (int i = 6; i >= 0; --i)
- {
- uint x_i = x[xOff + i], y_i = y[yOff + i];
- if (x_i < y_i)
- return false;
- if (x_i > y_i)
- return true;
- }
- return true;
- }
- public static bool IsOne(uint[] x)
- {
- if (x[0] != 1)
- {
- return false;
- }
- for (int i = 1; i < 7; ++i)
- {
- if (x[i] != 0)
- {
- return false;
- }
- }
- return true;
- }
- public static bool IsZero(uint[] x)
- {
- for (int i = 0; i < 7; ++i)
- {
- if (x[i] != 0)
- {
- return false;
- }
- }
- return true;
- }
- public static void Mul(uint[] x, uint[] y, uint[] zz)
- {
- ulong y_0 = y[0];
- ulong y_1 = y[1];
- ulong y_2 = y[2];
- ulong y_3 = y[3];
- ulong y_4 = y[4];
- ulong y_5 = y[5];
- ulong y_6 = y[6];
- {
- ulong c = 0, x_0 = x[0];
- c += x_0 * y_0;
- zz[0] = (uint)c;
- c >>= 32;
- c += x_0 * y_1;
- zz[1] = (uint)c;
- c >>= 32;
- c += x_0 * y_2;
- zz[2] = (uint)c;
- c >>= 32;
- c += x_0 * y_3;
- zz[3] = (uint)c;
- c >>= 32;
- c += x_0 * y_4;
- zz[4] = (uint)c;
- c >>= 32;
- c += x_0 * y_5;
- zz[5] = (uint)c;
- c >>= 32;
- c += x_0 * y_6;
- zz[6] = (uint)c;
- c >>= 32;
- zz[7] = (uint)c;
- }
- for (int i = 1; i < 7; ++i)
- {
- ulong c = 0, x_i = x[i];
- c += x_i * y_0 + zz[i + 0];
- zz[i + 0] = (uint)c;
- c >>= 32;
- c += x_i * y_1 + zz[i + 1];
- zz[i + 1] = (uint)c;
- c >>= 32;
- c += x_i * y_2 + zz[i + 2];
- zz[i + 2] = (uint)c;
- c >>= 32;
- c += x_i * y_3 + zz[i + 3];
- zz[i + 3] = (uint)c;
- c >>= 32;
- c += x_i * y_4 + zz[i + 4];
- zz[i + 4] = (uint)c;
- c >>= 32;
- c += x_i * y_5 + zz[i + 5];
- zz[i + 5] = (uint)c;
- c >>= 32;
- c += x_i * y_6 + zz[i + 6];
- zz[i + 6] = (uint)c;
- c >>= 32;
- zz[i + 7] = (uint)c;
- }
- }
- public static void Mul(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
- {
- ulong y_0 = y[yOff + 0];
- ulong y_1 = y[yOff + 1];
- ulong y_2 = y[yOff + 2];
- ulong y_3 = y[yOff + 3];
- ulong y_4 = y[yOff + 4];
- ulong y_5 = y[yOff + 5];
- ulong y_6 = y[yOff + 6];
- {
- ulong c = 0, x_0 = x[xOff + 0];
- c += x_0 * y_0;
- zz[zzOff + 0] = (uint)c;
- c >>= 32;
- c += x_0 * y_1;
- zz[zzOff + 1] = (uint)c;
- c >>= 32;
- c += x_0 * y_2;
- zz[zzOff + 2] = (uint)c;
- c >>= 32;
- c += x_0 * y_3;
- zz[zzOff + 3] = (uint)c;
- c >>= 32;
- c += x_0 * y_4;
- zz[zzOff + 4] = (uint)c;
- c >>= 32;
- c += x_0 * y_5;
- zz[zzOff + 5] = (uint)c;
- c >>= 32;
- c += x_0 * y_6;
- zz[zzOff + 6] = (uint)c;
- c >>= 32;
- zz[zzOff + 7] = (uint)c;
- }
- for (int i = 1; i < 7; ++i)
- {
- ++zzOff;
- ulong c = 0, x_i = x[xOff + i];
- c += x_i * y_0 + zz[zzOff + 0];
- zz[zzOff + 0] = (uint)c;
- c >>= 32;
- c += x_i * y_1 + zz[zzOff + 1];
- zz[zzOff + 1] = (uint)c;
- c >>= 32;
- c += x_i * y_2 + zz[zzOff + 2];
- zz[zzOff + 2] = (uint)c;
- c >>= 32;
- c += x_i * y_3 + zz[zzOff + 3];
- zz[zzOff + 3] = (uint)c;
- c >>= 32;
- c += x_i * y_4 + zz[zzOff + 4];
- zz[zzOff + 4] = (uint)c;
- c >>= 32;
- c += x_i * y_5 + zz[zzOff + 5];
- zz[zzOff + 5] = (uint)c;
- c >>= 32;
- c += x_i * y_6 + zz[zzOff + 6];
- zz[zzOff + 6] = (uint)c;
- c >>= 32;
- zz[zzOff + 7] = (uint)c;
- }
- }
- public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
- {
- ulong y_0 = y[0];
- ulong y_1 = y[1];
- ulong y_2 = y[2];
- ulong y_3 = y[3];
- ulong y_4 = y[4];
- ulong y_5 = y[5];
- ulong y_6 = y[6];
- ulong zc = 0;
- for (int i = 0; i < 7; ++i)
- {
- ulong c = 0, x_i = x[i];
- c += x_i * y_0 + zz[i + 0];
- zz[i + 0] = (uint)c;
- c >>= 32;
- c += x_i * y_1 + zz[i + 1];
- zz[i + 1] = (uint)c;
- c >>= 32;
- c += x_i * y_2 + zz[i + 2];
- zz[i + 2] = (uint)c;
- c >>= 32;
- c += x_i * y_3 + zz[i + 3];
- zz[i + 3] = (uint)c;
- c >>= 32;
- c += x_i * y_4 + zz[i + 4];
- zz[i + 4] = (uint)c;
- c >>= 32;
- c += x_i * y_5 + zz[i + 5];
- zz[i + 5] = (uint)c;
- c >>= 32;
- c += x_i * y_6 + zz[i + 6];
- zz[i + 6] = (uint)c;
- c >>= 32;
- c += zc + zz[i + 7];
- zz[i + 7] = (uint)c;
- zc = c >> 32;
- }
- return (uint)zc;
- }
- public static uint MulAddTo(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
- {
- ulong y_0 = y[yOff + 0];
- ulong y_1 = y[yOff + 1];
- ulong y_2 = y[yOff + 2];
- ulong y_3 = y[yOff + 3];
- ulong y_4 = y[yOff + 4];
- ulong y_5 = y[yOff + 5];
- ulong y_6 = y[yOff + 6];
- ulong zc = 0;
- for (int i = 0; i < 7; ++i)
- {
- ulong c = 0, x_i = x[xOff + i];
- c += x_i * y_0 + zz[zzOff + 0];
- zz[zzOff + 0] = (uint)c;
- c >>= 32;
- c += x_i * y_1 + zz[zzOff + 1];
- zz[zzOff + 1] = (uint)c;
- c >>= 32;
- c += x_i * y_2 + zz[zzOff + 2];
- zz[zzOff + 2] = (uint)c;
- c >>= 32;
- c += x_i * y_3 + zz[zzOff + 3];
- zz[zzOff + 3] = (uint)c;
- c >>= 32;
- c += x_i * y_4 + zz[zzOff + 4];
- zz[zzOff + 4] = (uint)c;
- c >>= 32;
- c += x_i * y_5 + zz[zzOff + 5];
- zz[zzOff + 5] = (uint)c;
- c >>= 32;
- c += x_i * y_6 + zz[zzOff + 6];
- zz[zzOff + 6] = (uint)c;
- c >>= 32;
- c += zc + zz[zzOff + 7];
- zz[zzOff + 7] = (uint)c;
- zc = c >> 32;
- ++zzOff;
- }
- return (uint)zc;
- }
- public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
- {
- Debug.Assert(w >> 31 == 0);
- ulong c = 0, wVal = w;
- ulong x0 = x[xOff + 0];
- c += wVal * x0 + y[yOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- ulong x1 = x[xOff + 1];
- c += wVal * x1 + x0 + y[yOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- ulong x2 = x[xOff + 2];
- c += wVal * x2 + x1 + y[yOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- ulong x3 = x[xOff + 3];
- c += wVal * x3 + x2 + y[yOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- ulong x4 = x[xOff + 4];
- c += wVal * x4 + x3 + y[yOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- ulong x5 = x[xOff + 5];
- c += wVal * x5 + x4 + y[yOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- ulong x6 = x[xOff + 6];
- c += wVal * x6 + x5 + y[yOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- c += x6;
- return c;
- }
- public static uint MulByWord(uint x, uint[] z)
- {
- ulong c = 0, xVal = x;
- c += xVal * (ulong)z[0];
- z[0] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[1];
- z[1] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[2];
- z[2] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[3];
- z[3] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[4];
- z[4] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[5];
- z[5] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[6];
- z[6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint MulByWordAddTo(uint x, uint[] y, uint[] z)
- {
- ulong c = 0, xVal = x;
- c += xVal * (ulong)z[0] + y[0];
- z[0] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[1] + y[1];
- z[1] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[2] + y[2];
- z[2] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[3] + y[3];
- z[3] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[4] + y[4];
- z[4] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[5] + y[5];
- z[5] = (uint)c;
- c >>= 32;
- c += xVal * (ulong)z[6] + y[6];
- z[6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint MulWordAddTo(uint x, uint[] y, int yOff, uint[] z, int zOff)
- {
- ulong c = 0, xVal = x;
- c += xVal * y[yOff + 0] + z[zOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += xVal * y[yOff + 1] + z[zOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += xVal * y[yOff + 2] + z[zOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += xVal * y[yOff + 3] + z[zOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- c += xVal * y[yOff + 4] + z[zOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- c += xVal * y[yOff + 5] + z[zOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- c += xVal * y[yOff + 6] + z[zOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- return (uint)c;
- }
- public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
- {
- Debug.Assert(x >> 31 == 0);
- Debug.Assert(zOff <= 3);
- ulong c = 0, xVal = x;
- ulong y00 = y & M;
- c += xVal * y00 + z[zOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- ulong y01 = y >> 32;
- c += xVal * y01 + y00 + z[zOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += y01 + z[zOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += z[zOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- return c == 0 ? 0 : Nat.IncAt(7, z, zOff, 4);
- }
- public static uint Mul33WordAdd(uint x, uint y, uint[] z, int zOff)
- {
- Debug.Assert(x >> 31 == 0);
- Debug.Assert(zOff <= 4);
- ulong c = 0, yVal = y;
- c += yVal * x + z[zOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += yVal + z[zOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += z[zOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- return c == 0 ? 0 : Nat.IncAt(7, z, zOff, 3);
- }
- public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
- {
- Debug.Assert(zOff <= 4);
- ulong c = 0, xVal = x;
- c += xVal * y + z[zOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += xVal * (y >> 32) + z[zOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += z[zOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- return c == 0 ? 0 : Nat.IncAt(7, z, zOff, 3);
- }
- public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
- {
- ulong c = 0, xVal = x;
- int i = 0;
- do
- {
- c += xVal * y[i];
- z[zOff + i] = (uint)c;
- c >>= 32;
- }
- while (++i < 7);
- return (uint)c;
- }
- public static void Square(uint[] x, uint[] zz)
- {
- ulong x_0 = x[0];
- ulong zz_1;
- uint c = 0, w;
- {
- int i = 6, j = 14;
- do
- {
- ulong xVal = x[i--];
- ulong p = xVal * xVal;
- zz[--j] = (c << 31) | (uint)(p >> 33);
- zz[--j] = (uint)(p >> 1);
- c = (uint)p;
- }
- while (i > 0);
- {
- ulong p = x_0 * x_0;
- zz_1 = (ulong)(c << 31) | (p >> 33);
- zz[0] = (uint)p;
- c = (uint)(p >> 32) & 1;
- }
- }
- ulong x_1 = x[1];
- ulong zz_2 = zz[2];
- {
- zz_1 += x_1 * x_0;
- w = (uint)zz_1;
- zz[1] = (w << 1) | c;
- c = w >> 31;
- zz_2 += zz_1 >> 32;
- }
- ulong x_2 = x[2];
- ulong zz_3 = zz[3];
- ulong zz_4 = zz[4];
- {
- zz_2 += x_2 * x_0;
- w = (uint)zz_2;
- zz[2] = (w << 1) | c;
- c = w >> 31;
- zz_3 += (zz_2 >> 32) + x_2 * x_1;
- zz_4 += zz_3 >> 32;
- zz_3 &= M;
- }
- ulong x_3 = x[3];
- ulong zz_5 = zz[5];
- ulong zz_6 = zz[6];
- {
- zz_3 += x_3 * x_0;
- w = (uint)zz_3;
- zz[3] = (w << 1) | c;
- c = w >> 31;
- zz_4 += (zz_3 >> 32) + x_3 * x_1;
- zz_5 += (zz_4 >> 32) + x_3 * x_2;
- zz_4 &= M;
- zz_6 += zz_5 >> 32;
- zz_5 &= M;
- }
- ulong x_4 = x[4];
- ulong zz_7 = zz[7];
- ulong zz_8 = zz[8];
- {
- zz_4 += x_4 * x_0;
- w = (uint)zz_4;
- zz[4] = (w << 1) | c;
- c = w >> 31;
- zz_5 += (zz_4 >> 32) + x_4 * x_1;
- zz_6 += (zz_5 >> 32) + x_4 * x_2;
- zz_5 &= M;
- zz_7 += (zz_6 >> 32) + x_4 * x_3;
- zz_6 &= M;
- zz_8 += zz_7 >> 32;
- zz_7 &= M;
- }
- ulong x_5 = x[5];
- ulong zz_9 = zz[9];
- ulong zz_10 = zz[10];
- {
- zz_5 += x_5 * x_0;
- w = (uint)zz_5;
- zz[5] = (w << 1) | c;
- c = w >> 31;
- zz_6 += (zz_5 >> 32) + x_5 * x_1;
- zz_7 += (zz_6 >> 32) + x_5 * x_2;
- zz_6 &= M;
- zz_8 += (zz_7 >> 32) + x_5 * x_3;
- zz_7 &= M;
- zz_9 += (zz_8 >> 32) + x_5 * x_4;
- zz_8 &= M;
- zz_10 += zz_9 >> 32;
- zz_9 &= M;
- }
- ulong x_6 = x[6];
- ulong zz_11 = zz[11];
- ulong zz_12 = zz[12];
- {
- zz_6 += x_6 * x_0;
- w = (uint)zz_6;
- zz[6] = (w << 1) | c;
- c = w >> 31;
- zz_7 += (zz_6 >> 32) + x_6 * x_1;
- zz_8 += (zz_7 >> 32) + x_6 * x_2;
- zz_9 += (zz_8 >> 32) + x_6 * x_3;
- zz_10 += (zz_9 >> 32) + x_6 * x_4;
- zz_11 += (zz_10 >> 32) + x_6 * x_5;
- zz_12 += zz_11 >> 32;
- }
- w = (uint)zz_7;
- zz[7] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_8;
- zz[8] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_9;
- zz[9] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_10;
- zz[10] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_11;
- zz[11] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_12;
- zz[12] = (w << 1) | c;
- c = w >> 31;
- w = zz[13] + (uint)(zz_12 >> 32);
- zz[13] = (w << 1) | c;
- }
- public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
- {
- ulong x_0 = x[xOff + 0];
- ulong zz_1;
- uint c = 0, w;
- {
- int i = 6, j = 14;
- do
- {
- ulong xVal = x[xOff + i--];
- ulong p = xVal * xVal;
- zz[zzOff + --j] = (c << 31) | (uint)(p >> 33);
- zz[zzOff + --j] = (uint)(p >> 1);
- c = (uint)p;
- }
- while (i > 0);
- {
- ulong p = x_0 * x_0;
- zz_1 = (ulong)(c << 31) | (p >> 33);
- zz[zzOff + 0] = (uint)p;
- c = (uint)(p >> 32) & 1;
- }
- }
- ulong x_1 = x[xOff + 1];
- ulong zz_2 = zz[zzOff + 2];
- {
- zz_1 += x_1 * x_0;
- w = (uint)zz_1;
- zz[zzOff + 1] = (w << 1) | c;
- c = w >> 31;
- zz_2 += zz_1 >> 32;
- }
- ulong x_2 = x[xOff + 2];
- ulong zz_3 = zz[zzOff + 3];
- ulong zz_4 = zz[zzOff + 4];
- {
- zz_2 += x_2 * x_0;
- w = (uint)zz_2;
- zz[zzOff + 2] = (w << 1) | c;
- c = w >> 31;
- zz_3 += (zz_2 >> 32) + x_2 * x_1;
- zz_4 += zz_3 >> 32;
- zz_3 &= M;
- }
- ulong x_3 = x[xOff + 3];
- ulong zz_5 = zz[zzOff + 5];
- ulong zz_6 = zz[zzOff + 6];
- {
- zz_3 += x_3 * x_0;
- w = (uint)zz_3;
- zz[zzOff + 3] = (w << 1) | c;
- c = w >> 31;
- zz_4 += (zz_3 >> 32) + x_3 * x_1;
- zz_5 += (zz_4 >> 32) + x_3 * x_2;
- zz_4 &= M;
- zz_6 += zz_5 >> 32;
- zz_5 &= M;
- }
- ulong x_4 = x[xOff + 4];
- ulong zz_7 = zz[zzOff + 7];
- ulong zz_8 = zz[zzOff + 8];
- {
- zz_4 += x_4 * x_0;
- w = (uint)zz_4;
- zz[zzOff + 4] = (w << 1) | c;
- c = w >> 31;
- zz_5 += (zz_4 >> 32) + x_4 * x_1;
- zz_6 += (zz_5 >> 32) + x_4 * x_2;
- zz_5 &= M;
- zz_7 += (zz_6 >> 32) + x_4 * x_3;
- zz_6 &= M;
- zz_8 += zz_7 >> 32;
- zz_7 &= M;
- }
- ulong x_5 = x[xOff + 5];
- ulong zz_9 = zz[zzOff + 9];
- ulong zz_10 = zz[zzOff + 10];
- {
- zz_5 += x_5 * x_0;
- w = (uint)zz_5;
- zz[zzOff + 5] = (w << 1) | c;
- c = w >> 31;
- zz_6 += (zz_5 >> 32) + x_5 * x_1;
- zz_7 += (zz_6 >> 32) + x_5 * x_2;
- zz_6 &= M;
- zz_8 += (zz_7 >> 32) + x_5 * x_3;
- zz_7 &= M;
- zz_9 += (zz_8 >> 32) + x_5 * x_4;
- zz_8 &= M;
- zz_10 += zz_9 >> 32;
- zz_9 &= M;
- }
- ulong x_6 = x[xOff + 6];
- ulong zz_11 = zz[zzOff + 11];
- ulong zz_12 = zz[zzOff + 12];
- {
- zz_6 += x_6 * x_0;
- w = (uint)zz_6;
- zz[zzOff + 6] = (w << 1) | c;
- c = w >> 31;
- zz_7 += (zz_6 >> 32) + x_6 * x_1;
- zz_8 += (zz_7 >> 32) + x_6 * x_2;
- zz_9 += (zz_8 >> 32) + x_6 * x_3;
- zz_10 += (zz_9 >> 32) + x_6 * x_4;
- zz_11 += (zz_10 >> 32) + x_6 * x_5;
- zz_12 += zz_11 >> 32;
- }
- w = (uint)zz_7;
- zz[zzOff + 7] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_8;
- zz[zzOff + 8] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_9;
- zz[zzOff + 9] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_10;
- zz[zzOff + 10] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_11;
- zz[zzOff + 11] = (w << 1) | c;
- c = w >> 31;
- w = (uint)zz_12;
- zz[zzOff + 12] = (w << 1) | c;
- c = w >> 31;
- w = zz[zzOff + 13] + (uint)(zz_12 >> 32);
- zz[zzOff + 13] = (w << 1) | c;
- }
- public static int Sub(uint[] x, uint[] y, uint[] z)
- {
- long c = 0;
- c += (long)x[0] - y[0];
- z[0] = (uint)c;
- c >>= 32;
- c += (long)x[1] - y[1];
- z[1] = (uint)c;
- c >>= 32;
- c += (long)x[2] - y[2];
- z[2] = (uint)c;
- c >>= 32;
- c += (long)x[3] - y[3];
- z[3] = (uint)c;
- c >>= 32;
- c += (long)x[4] - y[4];
- z[4] = (uint)c;
- c >>= 32;
- c += (long)x[5] - y[5];
- z[5] = (uint)c;
- c >>= 32;
- c += (long)x[6] - y[6];
- z[6] = (uint)c;
- c >>= 32;
- return (int)c;
- }
- public static int Sub(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
- {
- long c = 0;
- c += (long)x[xOff + 0] - y[yOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += (long)x[xOff + 1] - y[yOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += (long)x[xOff + 2] - y[yOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += (long)x[xOff + 3] - y[yOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- c += (long)x[xOff + 4] - y[yOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- c += (long)x[xOff + 5] - y[yOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- c += (long)x[xOff + 6] - y[yOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- return (int)c;
- }
- public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
- {
- long c = 0;
- c += (long)z[0] - x[0] - y[0];
- z[0] = (uint)c;
- c >>= 32;
- c += (long)z[1] - x[1] - y[1];
- z[1] = (uint)c;
- c >>= 32;
- c += (long)z[2] - x[2] - y[2];
- z[2] = (uint)c;
- c >>= 32;
- c += (long)z[3] - x[3] - y[3];
- z[3] = (uint)c;
- c >>= 32;
- c += (long)z[4] - x[4] - y[4];
- z[4] = (uint)c;
- c >>= 32;
- c += (long)z[5] - x[5] - y[5];
- z[5] = (uint)c;
- c >>= 32;
- c += (long)z[6] - x[6] - y[6];
- z[6] = (uint)c;
- c >>= 32;
- return (int)c;
- }
- public static int SubFrom(uint[] x, uint[] z)
- {
- long c = 0;
- c += (long)z[0] - x[0];
- z[0] = (uint)c;
- c >>= 32;
- c += (long)z[1] - x[1];
- z[1] = (uint)c;
- c >>= 32;
- c += (long)z[2] - x[2];
- z[2] = (uint)c;
- c >>= 32;
- c += (long)z[3] - x[3];
- z[3] = (uint)c;
- c >>= 32;
- c += (long)z[4] - x[4];
- z[4] = (uint)c;
- c >>= 32;
- c += (long)z[5] - x[5];
- z[5] = (uint)c;
- c >>= 32;
- c += (long)z[6] - x[6];
- z[6] = (uint)c;
- c >>= 32;
- return (int)c;
- }
- public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
- {
- long c = 0;
- c += (long)z[zOff + 0] - x[xOff + 0];
- z[zOff + 0] = (uint)c;
- c >>= 32;
- c += (long)z[zOff + 1] - x[xOff + 1];
- z[zOff + 1] = (uint)c;
- c >>= 32;
- c += (long)z[zOff + 2] - x[xOff + 2];
- z[zOff + 2] = (uint)c;
- c >>= 32;
- c += (long)z[zOff + 3] - x[xOff + 3];
- z[zOff + 3] = (uint)c;
- c >>= 32;
- c += (long)z[zOff + 4] - x[xOff + 4];
- z[zOff + 4] = (uint)c;
- c >>= 32;
- c += (long)z[zOff + 5] - x[xOff + 5];
- z[zOff + 5] = (uint)c;
- c >>= 32;
- c += (long)z[zOff + 6] - x[xOff + 6];
- z[zOff + 6] = (uint)c;
- c >>= 32;
- return (int)c;
- }
- public static BigInteger ToBigInteger(uint[] x)
- {
- byte[] bs = new byte[28];
- for (int i = 0; i < 7; ++i)
- {
- uint x_i = x[i];
- if (x_i != 0)
- {
- Pack.UInt32_To_BE(x_i, bs, (6 - i) << 2);
- }
- }
- return new BigInteger(1, bs);
- }
- public static void Zero(uint[] z)
- {
- z[0] = 0;
- z[1] = 0;
- z[2] = 0;
- z[3] = 0;
- z[4] = 0;
- z[5] = 0;
- z[6] = 0;
- }
- }
- }
- #endif
|