internal static byte[] ToBytes(this GroupElementP3 p3) { byte[] res = new byte[32]; GroupOperations.ge_p3_tobytes(res, 0, ref p3); return(res); }
/* * r = p */ public static void ge_p1p1_to_p3(out GroupElementP3 r, ref GroupElementP1P1 p) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out r.X, ref p.X, ref p.T); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out r.Y, ref p.Y, ref p.Z); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out r.Z, ref p.Z, ref p.T); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out r.T, ref p.X, ref p.Y); }
/* * r = 2 * p */ public static void ge_p3_dbl(out GroupElementP1P1 r, ref GroupElementP3 p) { GroupElementP2 q; GroupOperations.ge_p3_to_p2(out q, ref p); ge_p2_dbl(out r, ref q); }
/* * r = p */ internal static void ge_p3_to_cached(out GroupElementCached r, ref GroupElementP3 p) { FieldOperations.fe_add(out r.YplusX, ref p.Y, ref p.X); FieldOperations.fe_sub(out r.YminusX, ref p.Y, ref p.X); r.Z = p.Z; FieldOperations.fe_mul(out r.T2d, ref p.T, ref LookupTables.d2); }
/* * r = p */ private static void P3ToCached(out GroupElementCached r, ref GroupElementP3 p) { FieldOperations.Add(out r.YplusX, ref p.Y, ref p.X); FieldOperations.Subtract(out r.YminusX, ref p.Y, ref p.X); r.Z = p.Z; FieldOperations.Multiplication(out r.T2d, ref p.T, ref LookupTables.D2); }
public static void ge_p3_0(out GroupElementP3 h) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_0(out h.X); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_1(out h.Y); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_1(out h.Z); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_0(out h.T); }
/* * r = p */ public static void ge_p3_to_cached(out GroupElementCached r, ref GroupElementP3 p) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_add(out r.YplusX, ref p.Y, ref p.X); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sub(out r.YminusX, ref p.Y, ref p.X); r.Z = p.Z; stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out r.T2d, ref p.T, ref stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.LookupTables.d2); }
/* * r = 2 * p */ private static void GetP3Dbl(out GroupElementP1P1 r, ref GroupElementP3 p) { GroupElementP2 q; P3ConvertToP2(out q, ref p); GetP2Dbl(out r, ref q); }
/* * r = p */ private static void P1P1ConvertToP3(out GroupElementP3 r, ref GroupElementP1P1 p) { FieldOperations.Multiplication(out r.X, ref p.X, ref p.T); FieldOperations.Multiplication(out r.Y, ref p.Y, ref p.Z); FieldOperations.Multiplication(out r.Z, ref p.Z, ref p.T); FieldOperations.Multiplication(out r.T, ref p.X, ref p.Y); }
private static GroupElementP1 Madd(ref GroupElementP3 p, ref GroupElementP4 q) { var t0 = FieldElementOperations.Add(ref p.Z, ref p.Z); /* D = 2*Z1 */ var r = new GroupElementP1(); /* YpX1 = Y1+X1 */ r.X = FieldElementOperations.Add(ref p.Y, ref p.X); /* YmX1 = Y1-X1 */ r.Y = FieldElementOperations.Sub(ref p.Y, ref p.X); /* A = YpX1*ypx2 */ r.Z = FieldElementOperations.Multiplication(ref r.X, ref q.YplusX); /* B = YmX1*ymx2 */ r.Y = FieldElementOperations.Multiplication(ref r.Y, ref q.YminusX); /* C = xy2d2*T1 */ r.T = FieldElementOperations.Multiplication(ref q.XY2D, ref p.T); /* X3 = A-B */ r.X = FieldElementOperations.Sub(ref r.Z, ref r.Y); /* Y3 = A+B */ r.Y = FieldElementOperations.Add(ref r.Z, ref r.Y); /* Z3 = D+C */ r.Z = FieldElementOperations.Add(ref t0, ref r.T); /* T3 = D-C */ r.T = FieldElementOperations.Sub(ref t0, ref r.T); return(r); }
internal static void ge_p3_0(out GroupElementP3 h) { FieldOperations.fe_0(out h.X); FieldOperations.fe_1(out h.Y); FieldOperations.fe_1(out h.Z); FieldOperations.fe_0(out h.T); }
/* * r = p */ internal static void ge_p1p1_to_p3(out GroupElementP3 r, ref GroupElementP1P1 p) { FieldOperations.fe_mul(out r.X, ref p.X, ref p.T); FieldOperations.fe_mul(out r.Y, ref p.Y, ref p.Z); FieldOperations.fe_mul(out r.Z, ref p.Z, ref p.T); FieldOperations.fe_mul(out r.T, ref p.X, ref p.Y); }
/* * r = 2 * p */ internal static void ge_p3_dbl(out GroupElementP1P1 r, ref GroupElementP3 p) { GroupElementP2 q; ge_p3_to_p2(out q, ref p); ge_p2_dbl(out r, ref q); }
private static void GetP3(out GroupElementP3 h) { FieldOperations.FieldOperations_0(out h.X); FieldOperations.FieldOperations_1(out h.Y); FieldOperations.FieldOperations_1(out h.Z); FieldOperations.FieldOperations_0(out h.T); }
/* * r = p - q */ private static void Subtract(out GroupElementP1P1 r, ref GroupElementP3 p, ref GroupElementCached q) { /* qhasm: YpX1 = Y1+X1 */ /* asm 1: Add(>YpX1=fe#1,<Y1=fe#12,<X1=fe#11); */ /* asm 2: Add(>YpX1=r.X,<Y1=p.Y,<X1=p.X); */ FieldOperations.Add(out r.X, ref p.Y, ref p.X); /* qhasm: YmX1 = Y1-X1 */ /* asm 1: Subtract(>YmX1=fe#2,<Y1=fe#12,<X1=fe#11); */ /* asm 2: Subtract(>YmX1=r.Y,<Y1=p.Y,<X1=p.X); */ FieldOperations.Subtract(out r.Y, ref p.Y, ref p.X); /* qhasm: A = YpX1*YmX2 */ /* asm 1: Multiplication(>A=fe#3,<YpX1=fe#1,<YmX2=fe#16); */ /* asm 2: Multiplication(>A=r.Z,<YpX1=r.X,<YmX2=q.YminusX); */ FieldOperations.Multiplication(out r.Z, ref r.X, ref q.YminusX); /* qhasm: B = YmX1*YpX2 */ /* asm 1: Multiplication(>B=fe#2,<YmX1=fe#2,<YpX2=fe#15); */ /* asm 2: Multiplication(>B=r.Y,<YmX1=r.Y,<YpX2=q.YplusX); */ FieldOperations.Multiplication(out r.Y, ref r.Y, ref q.YplusX); /* qhasm: C = T2d2*T1 */ /* asm 1: Multiplication(>C=fe#4,<T2d2=fe#18,<T1=fe#14); */ /* asm 2: Multiplication(>C=r.T,<T2d2=q.T2d,<T1=p.T); */ FieldOperations.Multiplication(out r.T, ref q.T2d, ref p.T); /* qhasm: ZZ = Z1*Z2 */ /* asm 1: Multiplication(>ZZ=fe#1,<Z1=fe#13,<Z2=fe#17); */ /* asm 2: Multiplication(>ZZ=r.X,<Z1=p.Z,<Z2=q.Z); */ FieldOperations.Multiplication(out r.X, ref p.Z, ref q.Z); /* qhasm: D = 2*ZZ */ /* asm 1: Add(>D=fe#5,<ZZ=fe#1,<ZZ=fe#1); */ /* asm 2: Add(>D=t0,<ZZ=r.X,<ZZ=r.X); */ FieldOperations.Add(out var t0, ref r.X, ref r.X); /* qhasm: X3 = A-B */ /* asm 1: Subtract(>X3=fe#1,<A=fe#3,<B=fe#2); */ /* asm 2: Subtract(>X3=r.X,<A=r.Z,<B=r.Y); */ FieldOperations.Subtract(out r.X, ref r.Z, ref r.Y); /* qhasm: Y3 = A+B */ /* asm 1: Add(>Y3=fe#2,<A=fe#3,<B=fe#2); */ /* asm 2: Add(>Y3=r.Y,<A=r.Z,<B=r.Y); */ FieldOperations.Add(out r.Y, ref r.Z, ref r.Y); /* qhasm: Z3 = D-C */ /* asm 1: Subtract(>Z3=fe#3,<D=fe#5,<C=fe#4); */ /* asm 2: Subtract(>Z3=r.Z,<D=t0,<C=r.T); */ FieldOperations.Subtract(out r.Z, ref t0, ref r.T); /* qhasm: T3 = D+C */ /* asm 1: Add(>T3=fe#4,<D=fe#5,<C=fe#4); */ /* asm 2: Add(>T3=r.T,<D=t0,<C=r.T); */ FieldOperations.Add(out r.T, ref t0, ref r.T); /* qhasm: return */ }
internal static void ge_p3_tobytes(byte[] s, int offset, ref GroupElementP3 h) { FieldOperations.fe_invert(out FieldElement recip, ref h.Z); FieldOperations.fe_mul(out FieldElement x, ref h.X, ref recip); FieldOperations.fe_mul(out FieldElement y, ref h.Y, ref recip); FieldOperations.fe_tobytes(s, offset, ref y); s[offset + 31] ^= (byte)(FieldOperations.fe_isnegative(ref x) << 7); }
public static void ge_p3_tobytes(byte[] s, int offset, ref GroupElementP3 h) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_invert(out var recip, ref h.Z); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out var x, ref h.X, ref recip); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out var y, ref h.Y, ref recip); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_tobytes(s, offset, ref y); s[offset + 31] ^= (byte)(stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_isnegative(ref x) << 7); }
/* * r = p - q */ private static void Msub(out GroupElementP1P1 r, ref GroupElementP3 p, ref GroupElementPreComp q) { FieldElement t0; /* qhasm: YpX1 = Y1+X1 */ /* asm 1: Add(>YpX1=fe#1,<Y1=fe#12,<X1=fe#11); */ /* asm 2: Add(>YpX1=r.X,<Y1=p.Y,<X1=p.X); */ FieldOperations.Add(out r.X, ref p.Y, ref p.X); /* qhasm: YmX1 = Y1-X1 */ /* asm 1: Subtract(>YmX1=fe#2,<Y1=fe#12,<X1=fe#11); */ /* asm 2: Subtract(>YmX1=r.Y,<Y1=p.Y,<X1=p.X); */ FieldOperations.Subtract(out r.Y, ref p.Y, ref p.X); /* qhasm: A = YpX1*ymx2 */ /* asm 1: Multiplication(>A=fe#3,<YpX1=fe#1,<ymx2=fe#16); */ /* asm 2: Multiplication(>A=r.Z,<YpX1=r.X,<ymx2=q.yminusx); */ FieldOperations.Multiplication(out r.Z, ref r.X, ref q.yminusx); /* qhasm: B = YmX1*ypx2 */ /* asm 1: Multiplication(>B=fe#2,<YmX1=fe#2,<ypx2=fe#15); */ /* asm 2: Multiplication(>B=r.Y,<YmX1=r.Y,<ypx2=q.yplusx); */ FieldOperations.Multiplication(out r.Y, ref r.Y, ref q.yplusx); /* qhasm: C = xy2d2*T1 */ /* asm 1: Multiplication(>C=fe#4,<xy2d2=fe#17,<T1=fe#14); */ /* asm 2: Multiplication(>C=r.T,<xy2d2=q.xy2d,<T1=p.T); */ FieldOperations.Multiplication(out r.T, ref q.xy2d, ref p.T); /* qhasm: D = 2*Z1 */ /* asm 1: Add(>D=fe#5,<Z1=fe#13,<Z1=fe#13); */ /* asm 2: Add(>D=t0,<Z1=p.Z,<Z1=p.Z); */ FieldOperations.Add(out t0, ref p.Z, ref p.Z); /* qhasm: X3 = A-B */ /* asm 1: Subtract(>X3=fe#1,<A=fe#3,<B=fe#2); */ /* asm 2: Subtract(>X3=r.X,<A=r.Z,<B=r.Y); */ FieldOperations.Subtract(out r.X, ref r.Z, ref r.Y); /* qhasm: Y3 = A+B */ /* asm 1: Add(>Y3=fe#2,<A=fe#3,<B=fe#2); */ /* asm 2: Add(>Y3=r.Y,<A=r.Z,<B=r.Y); */ FieldOperations.Add(out r.Y, ref r.Z, ref r.Y); /* qhasm: Z3 = D-C */ /* asm 1: Subtract(>Z3=fe#3,<D=fe#5,<C=fe#4); */ /* asm 2: Subtract(>Z3=r.Z,<D=t0,<C=r.T); */ FieldOperations.Subtract(out r.Z, ref t0, ref r.T); /* qhasm: T3 = D+C */ /* asm 1: Add(>T3=fe#4,<D=fe#5,<C=fe#4); */ /* asm 2: Add(>T3=r.T,<D=t0,<C=r.T); */ FieldOperations.Add(out r.T, ref t0, ref r.T); /* qhasm: return */ }
/* * h = a * B * where a = a[0]+256*a[1]+..+256^31 a[31] * B is the Ed25519 base point (x,4/5) with x positive. * * Preconditions: * a[31] <= 127 */ internal static void ge_scalarmult_base(out GroupElementP3 h, byte[] a, int offset) { // todo: Perhaps remove this allocation var e = new sbyte[64]; sbyte carry; GroupElementP1P1 r; GroupElementP2 s; GroupElementPreComp t; int i; for (i = 0; i < 32; ++i) { e[2 * i + 0] = (sbyte)((a[offset + i] >> 0) & 15); e[2 * i + 1] = (sbyte)((a[offset + i] >> 4) & 15); } /* each e[i] is between 0 and 15 */ /* e[63] is between 0 and 7 */ carry = 0; for (i = 0; i < 63; ++i) { e[i] += carry; carry = (sbyte)(e[i] + 8); carry >>= 4; e[i] -= (sbyte)(carry << 4); } e[63] += carry; /* each e[i] is between -8 and 8 */ ge_p3_0(out h); for (i = 1; i < 64; i += 2) { select(out t, i / 2, e[i]); ge_madd(out r, ref h, ref t); ge_p1p1_to_p3(out h, ref r); } ge_p3_dbl(out r, ref h); ge_p1p1_to_p2(out s, ref r); ge_p2_dbl(out r, ref s); ge_p1p1_to_p2(out s, ref r); ge_p2_dbl(out r, ref s); ge_p1p1_to_p2(out s, ref r); ge_p2_dbl(out r, ref s); ge_p1p1_to_p3(out h, ref r); for (i = 0; i < 64; i += 2) { select(out t, i / 2, e[i]); ge_madd(out r, ref h, ref t); ge_p1p1_to_p3(out h, ref r); } }
public static void ScalarMultBase(out GroupElementP3 h, byte[] a, int offset) { var e = new sbyte[64]; GroupElementP1P1 r; GroupElementP2 s; GroupElementPreComp t; for (var i = 0; i < 32; ++i) { e[2 * i + 0] = (sbyte)(a[offset + i] & 15); e[2 * i + 1] = (sbyte)((a[offset + i] >> 4) & 15); } /* each e[i] is between 0 and 15 */ /* e[63] is between 0 and 7 */ sbyte carry = 0; for (var i = 0; i < 63; ++i) { e[i] += carry; carry = (sbyte)(e[i] + 8); carry >>= 4; e[i] -= (sbyte)(carry << 4); } e[63] += carry; /* each e[i] is between -8 and 8 */ GetP3(out h); for (var i = 1; i < 64; i += 2) { Select(out t, i / 2, e[i]); Madd(out r, ref h, ref t); P1P1ConvertToP3(out h, ref r); } GetP3Dbl(out r, ref h); P1P1ConvertToP2(out s, ref r); GetP2Dbl(out r, ref s); P1P1ConvertToP2(out s, ref r); GetP2Dbl(out r, ref s); P1P1ConvertToP2(out s, ref r); GetP2Dbl(out r, ref s); P1P1ConvertToP3(out h, ref r); for (var i = 0; i < 64; i += 2) { Select(out t, i / 2, e[i]); Madd(out r, ref h, ref t); P1P1ConvertToP3(out h, ref r); } }
/* * h = a * B * where a = a[0]+256*a[1]+...+256^31 a[31] * B is the Ed25519 base point (x,4/5) with x positive. * * Preconditions: * a[31] <= 127 */ public static void GeScalarmultBase(out GroupElementP3 h, byte[] a, int offset) { sbyte[] e = new sbyte[64]; GroupElementP1P1 r; GroupElementPreComp t; int i; for (i = 0; i < 32; ++i) { e[2 * i + 0] = (sbyte)((a[offset + i] >> 0) & 15); e[2 * i + 1] = (sbyte)((a[offset + i] >> 4) & 15); } /* each e[i] is between 0 and 15 */ /* e[63] is between 0 and 7 */ sbyte carry = 0; for (i = 0; i < 63; ++i) { e[i] += carry; carry = (sbyte)(e[i] + 8); carry >>= 4; e[i] -= (sbyte)(carry << 4); } e[63] += carry; /* each e[i] is between -8 and 8 */ ge_p3_0(out h); for (i = 1; i < 64; i += 2) { Select(out t, i / 2, e[i]); ge_madd(out r, ref h, ref t); ge_p1p1_to_p3(out h, ref r); } ge_p3_dbl(out r, ref h); ge_p1p1_to_p2(out GroupElementP2 s, ref r); ge_p2_dbl(out r, ref s); ge_p1p1_to_p2(out s, ref r); ge_p2_dbl(out r, ref s); ge_p1p1_to_p2(out s, ref r); ge_p2_dbl(out r, ref s); ge_p1p1_to_p3(out h, ref r); for (i = 0; i < 64; i += 2) { Select(out t, i / 2, e[i]); ge_madd(out r, ref h, ref t); ge_p1p1_to_p3(out h, ref r); } }
private static GroupElementP1 P3ToP1(ref GroupElementP3 p) { //r = 2 * p var q = new GroupElementP2 { //r = p X = p.X, Y = p.Y, Z = p.Z }; return(P2ToP1(ref q)); }
public static int ge_frombytes_negate_vartime(out GroupElementP3 h, byte[] data, int offset) { FieldElement u; FieldElement v; FieldElement v3; FieldElement vxx; FieldElement check; FieldOperations.fe_frombytes(out h.Y, data, offset); FieldOperations.fe_1(out h.Z); FieldOperations.fe_sq(out u, ref h.Y); FieldOperations.fe_mul(out v, ref u, ref LookupTables.d); FieldOperations.fe_sub(out u, ref u, ref h.Z); /* u = y^2-1 */ FieldOperations.fe_add(out v, ref v, ref h.Z); /* v = dy^2+1 */ FieldOperations.fe_sq(out v3, ref v); FieldOperations.fe_mul(out v3, ref v3, ref v); /* v3 = v^3 */ FieldOperations.fe_sq(out h.X, ref v3); FieldOperations.fe_mul(out h.X, ref h.X, ref v); FieldOperations.fe_mul(out h.X, ref h.X, ref u); /* x = uv^7 */ FieldOperations.fe_pow22523(out h.X, ref h.X); /* x = (uv^7)^((q-5)/8) */ FieldOperations.fe_mul(out h.X, ref h.X, ref v3); FieldOperations.fe_mul(out h.X, ref h.X, ref u); /* x = uv^3(uv^7)^((q-5)/8) */ FieldOperations.fe_sq(out vxx, ref h.X); FieldOperations.fe_mul(out vxx, ref vxx, ref v); FieldOperations.fe_sub(out check, ref vxx, ref u); /* vx^2-u */ if (FieldOperations.fe_isnonzero(ref check) != 0) { FieldOperations.fe_add(out check, ref vxx, ref u); /* vx^2+u */ if (FieldOperations.fe_isnonzero(ref check) != 0) { h = default(GroupElementP3); return(-1); } FieldOperations.fe_mul(out h.X, ref h.X, ref LookupTables.sqrtm1); } if (FieldOperations.fe_isnegative(ref h.X) == (data[offset + 31] >> 7)) { FieldOperations.fe_neg(out h.X, ref h.X); } FieldOperations.fe_mul(out h.T, ref h.X, ref h.Y); return(0); }
internal static void crypto_sign2( byte[] sig, byte[] m, byte[] sk, int keylen) { byte[] privHash = new byte[64]; byte[] seededHash = new byte[64]; byte[] result = new byte[64]; GroupElementP3 R = new GroupElementP3(); var hasher = new KeccakDigest(512); { var reversedPrivateKey = new byte[keylen]; Array.Copy(sk, 0, reversedPrivateKey, 0, keylen); Array.Reverse(reversedPrivateKey); hasher.BlockUpdate(reversedPrivateKey, 0, keylen); hasher.DoFinal(privHash, 0); ScalarOperations.sc_clamp(privHash, 0); hasher.Reset(); hasher.BlockUpdate(privHash, 32, 32); hasher.BlockUpdate(m, 0, m.Length); hasher.DoFinal(seededHash, 0); ScalarOperations.sc_reduce(seededHash); GroupOperations.ge_scalarmult_base(out R, seededHash, 0); GroupOperations.ge_p3_tobytes(sig, 0, ref R); hasher.Reset(); hasher.BlockUpdate(sig, 0, 32); hasher.BlockUpdate(sk, keylen, 32); hasher.BlockUpdate(m, 0, m.Length); hasher.DoFinal(result, 0); ScalarOperations.sc_reduce(result); var s = new byte[32]; //todo: remove allocation Array.Copy(sig, 32, s, 0, 32); ScalarOperations.sc_muladd(s, result, privHash, seededHash); Array.Copy(s, 0, sig, 32, 32); CryptoBytes.Wipe(s); } }
public static int ge_frombytes_negate_vartime(out GroupElementP3 h, byte[] data, int offset) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_frombytes(out h.Y, data, offset); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_1(out h.Z); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sq(out var u, ref h.Y); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out var v, ref u, ref stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.LookupTables.D); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sub(out u, ref u, ref h.Z); /* u = y^2-1 */ stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_add(out v, ref v, ref h.Z); /* v = dy^2+1 */ stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sq(out var v3, ref v); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out v3, ref v3, ref v); /* v3 = v^3 */ stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sq(out h.X, ref v3); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out h.X, ref h.X, ref v); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out h.X, ref h.X, ref u); /* x = uv^7 */ stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_pow22523(out h.X, ref h.X); /* x = (uv^7)^((q-5)/8) */ stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out h.X, ref h.X, ref v3); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out h.X, ref h.X, ref u); /* x = uv^3(uv^7)^((q-5)/8) */ stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sq(out var vxx, ref h.X); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out vxx, ref vxx, ref v); stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_sub(out var check, ref vxx, ref u); /* vx^2-u */ if (stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_isnonzero(ref check) != 0) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_add(out check, ref vxx, ref u); /* vx^2+u */ if (stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_isnonzero(ref check) != 0) { h = default(GroupElementP3); return(-1); } stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out h.X, ref h.X, ref stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.LookupTables.Sqrtm1); } if (stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_isnegative(ref h.X) == data[offset + 31] >> 7) { stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_neg(out h.X, ref h.X); } stellar_dotnet_sdk.chaos.nacl.Internal.Ed25519Ref10.FieldOperations.fe_mul(out h.T, ref h.X, ref h.Y); return(0); }
public static int FromBytes(out GroupElementP3 h, byte[] data, int offset) { FieldOperations.FromBytes(out h.Y, data, offset); FieldOperations.FieldOperations_1(out h.Z); FieldOperations.Square(out var u, ref h.Y); FieldOperations.Multiplication(out var v, ref u, ref LookupTables.D); FieldOperations.Subtract(out u, ref u, ref h.Z); /* u = y^2-1 */ FieldOperations.Add(out v, ref v, ref h.Z); /* v = dy^2+1 */ FieldOperations.Square(out var v3, ref v); FieldOperations.Multiplication(out v3, ref v3, ref v); /* v3 = v^3 */ FieldOperations.Square(out h.X, ref v3); FieldOperations.Multiplication(out h.X, ref h.X, ref v); FieldOperations.Multiplication(out h.X, ref h.X, ref u); /* x = uv^7 */ FieldOperations.Pow22523(out h.X, ref h.X); /* x = (uv^7)^((q-5)/8) */ FieldOperations.Multiplication(out h.X, ref h.X, ref v3); FieldOperations.Multiplication(out h.X, ref h.X, ref u); /* x = uv^3(uv^7)^((q-5)/8) */ FieldOperations.Square(out var vxx, ref h.X); FieldOperations.Multiplication(out vxx, ref vxx, ref v); FieldOperations.Subtract(out var check, ref vxx, ref u); /* vx^2-u */ if (FieldOperations.IsNonZero(ref check) != 0) { FieldOperations.Add(out check, ref vxx, ref u); /* vx^2+u */ if (FieldOperations.IsNonZero(ref check) != 0) { h = default; return(-1); } FieldOperations.Multiplication(out h.X, ref h.X, ref LookupTables.Sqrtm1); } if (FieldOperations.IsNegative(ref h.X) == data[offset + 31] >> 7) { FieldOperations.Negative(out h.X, ref h.X); } FieldOperations.Multiplication(out h.T, ref h.X, ref h.Y); return(0); }
/* * r = p - q */ public static void ge_msub(out GroupElementP1P1 r, ref GroupElementP3 p, ref GroupElementPreComp q) { /* qhasm: enter ge_msub */ /* qhasm: fe X1 */ /* qhasm: fe Y1 */ /* qhasm: fe Z1 */ /* qhasm: fe T1 */ /* qhasm: fe ypx2 */ /* qhasm: fe ymx2 */ /* qhasm: fe xy2d2 */ /* qhasm: fe X3 */ /* qhasm: fe Y3 */ /* qhasm: fe Z3 */ /* qhasm: fe T3 */ /* qhasm: fe YpX1 */ /* qhasm: fe YmX1 */ /* qhasm: fe A */ /* qhasm: fe B */ /* qhasm: fe C */ /* qhasm: fe D */ /* qhasm: YpX1 = Y1+X1 */ /* asm 1: fe_add(>YpX1=fe#1,<Y1=fe#12,<X1=fe#11); */ /* asm 2: fe_add(>YpX1=r.X,<Y1=p.Y,<X1=p.X); */ FieldOperations.fe_add(out r.X, ref p.Y, ref p.X); /* qhasm: YmX1 = Y1-X1 */ /* asm 1: fe_sub(>YmX1=fe#2,<Y1=fe#12,<X1=fe#11); */ /* asm 2: fe_sub(>YmX1=r.Y,<Y1=p.Y,<X1=p.X); */ FieldOperations.fe_sub(out r.Y, ref p.Y, ref p.X); /* qhasm: A = YpX1*ymx2 */ /* asm 1: fe_mul(>A=fe#3,<YpX1=fe#1,<ymx2=fe#16); */ /* asm 2: fe_mul(>A=r.Z,<YpX1=r.X,<ymx2=q.yminusx); */ FieldOperations.fe_mul(out r.Z, ref r.X, ref q.yminusx); /* qhasm: B = YmX1*ypx2 */ /* asm 1: fe_mul(>B=fe#2,<YmX1=fe#2,<ypx2=fe#15); */ /* asm 2: fe_mul(>B=r.Y,<YmX1=r.Y,<ypx2=q.yplusx); */ FieldOperations.fe_mul(out r.Y, ref r.Y, ref q.yplusx); /* qhasm: C = xy2d2*T1 */ /* asm 1: fe_mul(>C=fe#4,<xy2d2=fe#17,<T1=fe#14); */ /* asm 2: fe_mul(>C=r.T,<xy2d2=q.xy2d,<T1=p.T); */ FieldOperations.fe_mul(out r.T, ref q.xy2d, ref p.T); /* qhasm: D = 2*Z1 */ /* asm 1: fe_add(>D=fe#5,<Z1=fe#13,<Z1=fe#13); */ /* asm 2: fe_add(>D=t0,<Z1=p.Z,<Z1=p.Z); */ FieldOperations.fe_add(out FieldElement t0, ref p.Z, ref p.Z); /* qhasm: X3 = A-B */ /* asm 1: fe_sub(>X3=fe#1,<A=fe#3,<B=fe#2); */ /* asm 2: fe_sub(>X3=r.X,<A=r.Z,<B=r.Y); */ FieldOperations.fe_sub(out r.X, ref r.Z, ref r.Y); /* qhasm: Y3 = A+B */ /* asm 1: fe_add(>Y3=fe#2,<A=fe#3,<B=fe#2); */ /* asm 2: fe_add(>Y3=r.Y,<A=r.Z,<B=r.Y); */ FieldOperations.fe_add(out r.Y, ref r.Z, ref r.Y); /* qhasm: Z3 = D-C */ /* asm 1: fe_sub(>Z3=fe#3,<D=fe#5,<C=fe#4); */ /* asm 2: fe_sub(>Z3=r.Z,<D=t0,<C=r.T); */ FieldOperations.fe_sub(out r.Z, ref t0, ref r.T); /* qhasm: T3 = D+C */ /* asm 1: fe_add(>T3=fe#4,<D=fe#5,<C=fe#4); */ /* asm 2: fe_add(>T3=r.T,<D=t0,<C=r.T); */ FieldOperations.fe_add(out r.T, ref t0, ref r.T); /* qhasm: return */ }
public static void DoubleScalarMult(out GroupElementP2 r, byte[] a, ref GroupElementP3 A, byte[] b) { var Bi = LookupTables.Base2; // todo: Perhaps remove these allocations? var aslide = new sbyte[256]; var bslide = new sbyte[256]; var Ai = new GroupElementCached[8]; /* A,3A,5A,7A,9A,11A,13A,15A */ GroupElementP1P1 t; GroupElementP3 u; GroupElementP3 A2; int i; Slide(aslide, a); Slide(bslide, b); P3ToCached(out Ai[0], ref A); GetP3Dbl(out t, ref A); P1P1ConvertToP3(out A2, ref t); Add(out t, ref A2, ref Ai[0]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[1], ref u); Add(out t, ref A2, ref Ai[1]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[2], ref u); Add(out t, ref A2, ref Ai[2]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[3], ref u); Add(out t, ref A2, ref Ai[3]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[4], ref u); Add(out t, ref A2, ref Ai[4]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[5], ref u); Add(out t, ref A2, ref Ai[5]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[6], ref u); Add(out t, ref A2, ref Ai[6]); P1P1ConvertToP3(out u, ref t); P3ToCached(out Ai[7], ref u); GetP2(out r); for (i = 255; i >= 0; --i) { if (aslide[i] != 0 || bslide[i] != 0) { break; } } for (; i >= 0; --i) { GetP2Dbl(out t, ref r); if (aslide[i] > 0) { P1P1ConvertToP3(out u, ref t); Add(out t, ref u, ref Ai[aslide[i] / 2]); } else if (aslide[i] < 0) { P1P1ConvertToP3(out u, ref t); Subtract(out t, ref u, ref Ai[-aslide[i] / 2]); } if (bslide[i] > 0) { P1P1ConvertToP3(out u, ref t); Madd(out t, ref u, ref Bi[bslide[i] / 2]); } else if (bslide[i] < 0) { P1P1ConvertToP3(out u, ref t); Msub(out t, ref u, ref Bi[-bslide[i] / 2]); } P1P1ConvertToP2(out r, ref t); } }
/* * r = p + q */ internal static void ge_add(out GroupElementP1P1 r, ref GroupElementP3 p, ref GroupElementCached q) { /* qhasm: enter GroupElementadd */ /* qhasm: fe X1 */ /* qhasm: fe Y1 */ /* qhasm: fe Z1 */ /* qhasm: fe Z2 */ /* qhasm: fe T1 */ /* qhasm: fe ZZ */ /* qhasm: fe YpX2 */ /* qhasm: fe YmX2 */ /* qhasm: fe T2d2 */ /* qhasm: fe X3 */ /* qhasm: fe Y3 */ /* qhasm: fe Z3 */ /* qhasm: fe T3 */ /* qhasm: fe YpX1 */ /* qhasm: fe YmX1 */ /* qhasm: fe A */ /* qhasm: fe B */ /* qhasm: fe C */ /* qhasm: fe D */ /* qhasm: YpX1 = Y1+X1 */ /* asm 1: fe_add(>YpX1=fe#1,<Y1=fe#12,<X1=fe#11); */ /* asm 2: fe_add(>YpX1=r.X,<Y1=p.Y,<X1=p.X); */ FieldOperations.fe_add(out r.X, ref p.Y, ref p.X); /* qhasm: YmX1 = Y1-X1 */ /* asm 1: fe_sub(>YmX1=fe#2,<Y1=fe#12,<X1=fe#11); */ /* asm 2: fe_sub(>YmX1=r.Y,<Y1=p.Y,<X1=p.X); */ FieldOperations.fe_sub(out r.Y, ref p.Y, ref p.X); /* qhasm: A = YpX1*YpX2 */ /* asm 1: fe_mul(>A=fe#3,<YpX1=fe#1,<YpX2=fe#15); */ /* asm 2: fe_mul(>A=r.Z,<YpX1=r.X,<YpX2=q.YplusX); */ FieldOperations.fe_mul(out r.Z, ref r.X, ref q.YplusX); /* qhasm: B = YmX1*YmX2 */ /* asm 1: fe_mul(>B=fe#2,<YmX1=fe#2,<YmX2=fe#16); */ /* asm 2: fe_mul(>B=r.Y,<YmX1=r.Y,<YmX2=q.YminusX); */ FieldOperations.fe_mul(out r.Y, ref r.Y, ref q.YminusX); /* qhasm: C = T2d2*T1 */ /* asm 1: fe_mul(>C=fe#4,<T2d2=fe#18,<T1=fe#14); */ /* asm 2: fe_mul(>C=r.T,<T2d2=q.T2d,<T1=p.T); */ FieldOperations.fe_mul(out r.T, ref q.T2d, ref p.T); /* qhasm: ZZ = Z1*Z2 */ /* asm 1: fe_mul(>ZZ=fe#1,<Z1=fe#13,<Z2=fe#17); */ /* asm 2: fe_mul(>ZZ=r.X,<Z1=p.Z,<Z2=q.Z); */ FieldOperations.fe_mul(out r.X, ref p.Z, ref q.Z); /* qhasm: D = 2*ZZ */ /* asm 1: fe_add(>D=fe#5,<ZZ=fe#1,<ZZ=fe#1); */ /* asm 2: fe_add(>D=t0,<ZZ=r.X,<ZZ=r.X); */ FieldOperations.fe_add(out FieldElement t0, ref r.X, ref r.X); /* qhasm: X3 = A-B */ /* asm 1: fe_sub(>X3=fe#1,<A=fe#3,<B=fe#2); */ /* asm 2: fe_sub(>X3=r.X,<A=r.Z,<B=r.Y); */ FieldOperations.fe_sub(out r.X, ref r.Z, ref r.Y); /* qhasm: Y3 = A+B */ /* asm 1: fe_add(>Y3=fe#2,<A=fe#3,<B=fe#2); */ /* asm 2: fe_add(>Y3=r.Y,<A=r.Z,<B=r.Y); */ FieldOperations.fe_add(out r.Y, ref r.Z, ref r.Y); /* qhasm: Z3 = D+C */ /* asm 1: fe_add(>Z3=fe#3,<D=fe#5,<C=fe#4); */ /* asm 2: fe_add(>Z3=r.Z,<D=t0,<C=r.T); */ FieldOperations.fe_add(out r.Z, ref t0, ref r.T); /* qhasm: T3 = D-C */ /* asm 1: fe_sub(>T3=fe#4,<D=fe#5,<C=fe#4); */ /* asm 2: fe_sub(>T3=r.T,<D=t0,<C=r.T); */ FieldOperations.fe_sub(out r.T, ref t0, ref r.T); /* qhasm: return */ }
/* * r = a * A + b * B * where a = a[0]+256*a[1]+...+256^31 a[31]. * and b = b[0]+256*b[1]+...+256^31 b[31]. * B is the Ed25519 base point (x,4/5) with x positive. */ public static void ge_double_scalarmult_vartime(out GroupElementP2 r, byte[] a, ref GroupElementP3 A, byte[] b) { GroupElementPreComp[] Bi = LookupTables.Base2; sbyte[] aslide = new sbyte[256]; sbyte[] bslide = new sbyte[256]; GroupElementCached[] ai = new GroupElementCached[8]; /* A,3A,5A,7A,9A,11A,13A,15A */ int i; Slide(aslide, a); Slide(bslide, b); ge_p3_to_cached(out ai[0], ref A); ge_p3_dbl(out GroupElementP1P1 t, ref A); ge_p1p1_to_p3(out GroupElementP3 A2, ref t); ge_add(out t, ref A2, ref ai[0]); ge_p1p1_to_p3(out GroupElementP3 u, ref t); ge_p3_to_cached(out ai[1], ref u); ge_add(out t, ref A2, ref ai[1]); ge_p1p1_to_p3(out u, ref t); ge_p3_to_cached(out ai[2], ref u); ge_add(out t, ref A2, ref ai[2]); ge_p1p1_to_p3(out u, ref t); ge_p3_to_cached(out ai[3], ref u); ge_add(out t, ref A2, ref ai[3]); ge_p1p1_to_p3(out u, ref t); ge_p3_to_cached(out ai[4], ref u); ge_add(out t, ref A2, ref ai[4]); ge_p1p1_to_p3(out u, ref t); ge_p3_to_cached(out ai[5], ref u); ge_add(out t, ref A2, ref ai[5]); ge_p1p1_to_p3(out u, ref t); ge_p3_to_cached(out ai[6], ref u); ge_add(out t, ref A2, ref ai[6]); ge_p1p1_to_p3(out u, ref t); ge_p3_to_cached(out ai[7], ref u); ge_p2_0(out r); for (i = 255; i >= 0; --i) { if (aslide[i] != 0 || bslide[i] != 0) { break; } } for (; i >= 0; --i) { ge_p2_dbl(out t, ref r); if (aslide[i] > 0) { ge_p1p1_to_p3(out u, ref t); ge_add(out t, ref u, ref ai[aslide[i] / 2]); } else if (aslide[i] < 0) { ge_p1p1_to_p3(out u, ref t); GeSub(out t, ref u, ref ai[-aslide[i] / 2]); } if (bslide[i] > 0) { ge_p1p1_to_p3(out u, ref t); ge_madd(out t, ref u, ref Bi[bslide[i] / 2]); } else if (bslide[i] < 0) { ge_p1p1_to_p3(out u, ref t); ge_msub(out t, ref u, ref Bi[-bslide[i] / 2]); } ge_p1p1_to_p2(out r, ref t); } }