public static void ge_montx_to_p2(Ge_p2 p, int[] u, byte ed_sign_bit) { int[] x = new int[10]; int[] y = new int[10]; int[] A = new int[10]; int[] v = new int[10]; int[] v2 = new int[10]; int[] iv = new int[10]; int[] nx = new int[10]; Fe_frombytes.fe_frombytes(A, A_bytes); /* given u, recover edwards y */ /* given u, recover v */ /* given u and v, recover edwards x */ Fe_montx_to_edy.fe_montx_to_edy(y, u); /* y = (u - 1) / (u + 1) */ Fe_mont_rhs.fe_mont_rhs(v2, u); /* v^2 = u(u^2 + Au + 1) */ Fe_sqrt.fe_sqrt(v, v2); /* v = sqrt(v^2) */ Fe_mul.fe_mul(x, u, A); /* x = u * sqrt(-(A+2)) */ Fe_invert.fe_invert(iv, v); /* 1/v */ Fe_mul.fe_mul(x, x, iv); /* x = (u/v) * sqrt(-(A+2)) */ Fe_neg.fe_neg(nx, x); /* negate x to match sign bit */ Fe_cmov.fe_cmov(x, nx, Fe_isnegative.fe_isnegative(x) ^ ed_sign_bit); Fe_copy.fe_copy(p.X, x); Fe_copy.fe_copy(p.Y, y); Fe_1.fe_1(p.Z); /* POSTCONDITION: check that p->X and p->Y satisfy the Ed curve equation */ /* -x^2 + y^2 = 1 + dx^2y^2 */ //#ifndef NDEBUG //{ //fe one, d, x2, y2, x2y2, dx2y2; // //unsigned char dbytes[32] = { //0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75, //0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00, //0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c, //0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52 //}; // //fe_frombytes(d, dbytes); //fe_1(one); //fe_sq(x2, p->X); /* x^2 */ //fe_sq(y2, p->Y); /* y^2 */ // //fe_mul(dx2y2, x2, y2); /* x^2y^2 */ //fe_mul(dx2y2, dx2y2, d); /* dx^2y^2 */ //fe_add(dx2y2, dx2y2, one); /* dx^2y^2 + 1 */ //fe_neg(x2y2, x2); /* -x^2 */ //fe_add(x2y2, x2y2, y2); /* -x^2 + y^2 */ // //assert(fe_isequal(x2y2, dx2y2)); //} //#endif }
public static bool fe_isreduced(byte[] curve25519_pubkey) { int[] fe = new int[10]; byte[] strict = new byte[32]; Fe_frombytes.fe_frombytes(fe, curve25519_pubkey); Fe_tobytes.fe_tobytes(strict, fe); if (Crypto_verify_32.crypto_verify_32(strict, curve25519_pubkey) != 0) { return(false); } return(true); }
public static int ge_frombytes_negate_vartime(Ge_p3 h, byte[] s) { int[] u = new int[10]; int[] v = new int[10]; int[] v3 = new int[10]; int[] vxx = new int[10]; int[] check = new int[10]; Fe_frombytes.fe_frombytes(h.Y, s); Fe_1.fe_1(h.Z); Fe_sq.fe_sq(u, h.Y); Fe_mul.fe_mul(v, u, d); Fe_sub.fe_sub(u, u, h.Z); /* u = y^2-1 */ Fe_add.fe_add(v, v, h.Z); /* v = dy^2+1 */ Fe_sq.fe_sq(v3, v); Fe_mul.fe_mul(v3, v3, v); /* v3 = v^3 */ Fe_sq.fe_sq(h.X, v3); Fe_mul.fe_mul(h.X, h.X, v); Fe_mul.fe_mul(h.X, h.X, u); /* x = uv^7 */ Fe_pow22523.fe_pow22523(h.X, h.X); /* x = (uv^7)^((q-5)/8) */ Fe_mul.fe_mul(h.X, h.X, v3); Fe_mul.fe_mul(h.X, h.X, u); /* x = uv^3(uv^7)^((q-5)/8) */ Fe_sq.fe_sq(vxx, h.X); Fe_mul.fe_mul(vxx, vxx, v); Fe_sub.fe_sub(check, vxx, u); /* vx^2-u */ if (Fe_isnonzero.fe_isnonzero(check) != 0) { Fe_add.fe_add(check, vxx, u); /* vx^2+u */ if (Fe_isnonzero.fe_isnonzero(check) != 0) { return(-1); } Fe_mul.fe_mul(h.X, h.X, sqrtm1); } if (Fe_isnegative.fe_isnegative(h.X) == ((((uint)s[31]) >> 7) & 0x01)) { Fe_neg.fe_neg(h.X, h.X); } Fe_mul.fe_mul(h.T, h.X, h.Y); return(0); }
/* Preconditions: a is square or zero */ public static void fe_sqrt(int[] iOut, int[] a) { int[] exp = new int[10]; int[] b = new int[10]; int[] b2 = new int[10]; int[] bi = new int[10]; int[] i = new int[10]; Fe_frombytes.fe_frombytes(i, i_bytes); Fe_pow22523.fe_pow22523(exp, a); /* b = a^(q-5)/8 */ /* PRECONDITION: legendre symbol == 1 (square) or 0 (a == zero) */ //#ifndef NDEBUG //fe legendre, zero, one; //fe_sq(legendre, exp); /* in^((q-5)/4) */ //fe_sq(legendre, legendre); /* in^((q-5)/2) */ //fe_mul(legendre, legendre, a); /* in^((q-3)/2) */ //fe_mul(legendre, legendre, a); /* in^((q-1)/2) */ //fe_0(zero); //fe_1(one); //assert(fe_isequal(legendre, zero) || fe_isequal(legendre, one)); //#endif Fe_mul.fe_mul(b, a, exp); /* b = a * a^(q-5)/8 */ Fe_sq.fe_sq(b2, b); /* b^2 = a * a^(q-1)/4 */ /* note b^4 == a^2, so b^2 == a or -a * if b^2 != a, multiply it by sqrt(-1) */ Fe_mul.fe_mul(bi, b, i); Fe_cmov.fe_cmov(b, bi, 1 ^ Fe_isequal.fe_isequal(b2, a)); Fe_copy.fe_copy(iOut, b); /* PRECONDITION: out^2 == a */ //#ifndef NDEBUG //fe_sq(b2, out); //assert(fe_isequal(a, b2)); //#endif }
public void elligator_fast_test() { byte[] elligator_correct_output = new byte[] { 0x5f, 0x35, 0x20, 0x00, 0x1c, 0x6c, 0x99, 0x36, 0xa3, 0x12, 0x06, 0xaf, 0xe7, 0xc7, 0xac, 0x22, 0x4e, 0x88, 0x61, 0x61, 0x9b, 0xf9, 0x88, 0x72, 0x44, 0x49, 0x15, 0x89, 0x9d, 0x95, 0xf4, 0x6e }; byte[] hashtopoint_correct_output1 = new byte[] { 0xce, 0x89, 0x9f, 0xb2, 0x8f, 0xf7, 0x20, 0x91, 0x5e, 0x14, 0xf5, 0xb7, 0x99, 0x08, 0xab, 0x17, 0xaa, 0x2e, 0xe2, 0x45, 0xb4, 0xfc, 0x2b, 0xf6, 0x06, 0x36, 0x29, 0x40, 0xed, 0x7d, 0xe7, 0xed }; byte[] hashtopoint_correct_output2 = new byte[] { 0xa0, 0x35, 0xbb, 0xa9, 0x4d, 0x30, 0x55, 0x33, 0x0d, 0xce, 0xc2, 0x7f, 0x83, 0xde, 0x79, 0xd0, 0x89, 0x67, 0x72, 0x4c, 0x07, 0x8d, 0x68, 0x9d, 0x61, 0x52, 0x1d, 0xf9, 0x2c, 0x5c, 0xba, 0x77 }; byte[] calculatev_correct_output = new byte[] { 0x1b, 0x77, 0xb5, 0xa0, 0x44, 0x84, 0x7e, 0xb9, 0x23, 0xd7, 0x93, 0x18, 0xce, 0xc2, 0xc5, 0xe2, 0x84, 0xd5, 0x79, 0x6f, 0x65, 0x63, 0x1b, 0x60, 0x9b, 0xf1, 0xf8, 0xce, 0x88, 0x0b, 0x50, 0x9c, }; int count; int[] iIn = new int[10]; int[] iOut = new int[10]; byte[] bytes = new byte[32]; Fe_0.fe_0(iIn); Fe_0.fe_0(iOut); for (count = 0; count < 32; count++) { bytes[count] = (byte)count; } Fe_frombytes.fe_frombytes(iIn, bytes); Elligator.elligator(iOut, iIn); Fe_tobytes.fe_tobytes(bytes, iOut); CollectionAssert.AreEqual(elligator_correct_output, bytes, "Elligator vector"); /* Elligator(0) == 0 test */ Fe_0.fe_0(iIn); Elligator.elligator(iOut, iIn); CollectionAssert.AreEqual(iOut, iIn, "Elligator(0) == 0"); /* ge_montx_to_p3(0) -> order2 point test */ int[] one = new int[10]; int[] negone = new int[10]; int[] zero = new int[10]; Fe_1.fe_1(one); Fe_0.fe_0(zero); Fe_sub.fe_sub(negone, zero, one); Ge_p3 p3 = new Ge_p3(); Ge_montx_to_p3.ge_montx_to_p3(p3, zero, 0); Assert.IsTrue(Fe_isequal.fe_isequal(p3.X, zero) != 0 && Fe_isequal.fe_isequal(p3.Y, negone) != 0 && Fe_isequal.fe_isequal(p3.Z, one) != 0 && Fe_isequal.fe_isequal(p3.T, zero) != 0, "ge_montx_to_p3(0) == order 2 point"); /* Hash to point vector test */ byte[] htp = new byte[32]; for (count = 0; count < 32; count++) { htp[count] = (byte)count; } ISha512 sha512provider = new BouncyCastleDotNETSha512Provider(); Elligator.hash_to_point(sha512provider, p3, htp, 32); Ge_p3_tobytes.ge_p3_tobytes(htp, p3); CollectionAssert.AreEqual(hashtopoint_correct_output1, htp, "hash_to_point #1"); for (count = 0; count < 32; count++) { htp[count] = (byte)(count + 1); } Elligator.hash_to_point(sha512provider, p3, htp, 32); Ge_p3_tobytes.ge_p3_tobytes(htp, p3); CollectionAssert.AreEqual(hashtopoint_correct_output2, htp, "hash_to_point #2"); /* calculate_U vector test */ Ge_p3 Bv = new Ge_p3(); byte[] V = new byte[32]; byte[] Vbuf = new byte[200]; byte[] a = new byte[32]; byte[] A = new byte[32]; byte[] Vmsg = new byte[3]; Vmsg[0] = 0; Vmsg[1] = 1; Vmsg[2] = 2; for (count = 0; count < 32; count++) { a[count] = (byte)(8 + count); A[count] = (byte)(9 + count); } Sc_clamp.sc_clamp(a); Elligator.calculate_Bv_and_V(sha512provider, Bv, V, Vbuf, a, A, Vmsg, 3); CollectionAssert.AreEqual(calculatev_correct_output, V, "calculate_Bv_and_V vector"); }
//CONVERT #include "crypto_scalarmult.h" //CONVERT #include "fe.h" public static int crypto_scalarmult(byte[] q, byte[] n, byte[] p) { byte[] e = new byte[32]; int i; int[] x1 = new int[10]; int[] x2 = new int[10]; int[] z2 = new int[10]; int[] x3 = new int[10]; int[] z3 = new int[10]; int[] tmp0 = new int[10]; int[] tmp1 = new int[10]; int pos; int swap; int b; for (i = 0; i < 32; ++i) { e[i] = n[i]; } // e[0] &= 248; // e[31] &= 127; // e[31] |= 64; Fe_frombytes.fe_frombytes(x1, p); Fe_1.fe_1(x2); Fe_0.fe_0(z2); Fe_copy.fe_copy(x3, x1); Fe_1.fe_1(z3); swap = 0; for (pos = 254; pos >= 0; --pos) { b = (int)(((uint)e[pos / 8]) >> (pos & 7)); b &= 1; swap ^= b; Fe_cswap.fe_cswap(x2, x3, swap); Fe_cswap.fe_cswap(z2, z3, swap); swap = b; //CONVERT #include "montgomery.h" /* qhasm: fe X2 */ /* qhasm: fe Z2 */ /* qhasm: fe X3 */ /* qhasm: fe Z3 */ /* qhasm: fe X4 */ /* qhasm: fe Z4 */ /* qhasm: fe X5 */ /* qhasm: fe Z5 */ /* qhasm: fe A */ /* qhasm: fe B */ /* qhasm: fe C */ /* qhasm: fe D */ /* qhasm: fe E */ /* qhasm: fe AA */ /* qhasm: fe BB */ /* qhasm: fe DA */ /* qhasm: fe CB */ /* qhasm: fe t0 */ /* qhasm: fe t1 */ /* qhasm: fe t2 */ /* qhasm: fe t3 */ /* qhasm: fe t4 */ /* qhasm: enter ladder */ /* qhasm: D = X3-Z3 */ /* asm 1: fe_sub.fe_sub(>D=fe#5,<X3=fe#3,<Z3=fe#4); */ /* asm 2: fe_sub.fe_sub(>D=tmp0,<X3=x3,<Z3=z3); */ Fe_sub.fe_sub(tmp0, x3, z3); /* qhasm: B = X2-Z2 */ /* asm 1: fe_sub.fe_sub(>B=fe#6,<X2=fe#1,<Z2=fe#2); */ /* asm 2: fe_sub.fe_sub(>B=tmp1,<X2=x2,<Z2=z2); */ Fe_sub.fe_sub(tmp1, x2, z2); /* qhasm: A = X2+Z2 */ /* asm 1: fe_add.fe_add(>A=fe#1,<X2=fe#1,<Z2=fe#2); */ /* asm 2: fe_add.fe_add(>A=x2,<X2=x2,<Z2=z2); */ Fe_add.fe_add(x2, x2, z2); /* qhasm: C = X3+Z3 */ /* asm 1: fe_add.fe_add(>C=fe#2,<X3=fe#3,<Z3=fe#4); */ /* asm 2: fe_add.fe_add(>C=z2,<X3=x3,<Z3=z3); */ Fe_add.fe_add(z2, x3, z3); /* qhasm: DA = D*A */ /* asm 1: fe_mul.fe_mul(>DA=fe#4,<D=fe#5,<A=fe#1); */ /* asm 2: fe_mul.fe_mul(>DA=z3,<D=tmp0,<A=x2); */ Fe_mul.fe_mul(z3, tmp0, x2); /* qhasm: CB = C*B */ /* asm 1: fe_mul.fe_mul(>CB=fe#2,<C=fe#2,<B=fe#6); */ /* asm 2: fe_mul.fe_mul(>CB=z2,<C=z2,<B=tmp1); */ Fe_mul.fe_mul(z2, z2, tmp1); /* qhasm: BB = B^2 */ /* asm 1: fe_sq.fe_sq(>BB=fe#5,<B=fe#6); */ /* asm 2: fe_sq.fe_sq(>BB=tmp0,<B=tmp1); */ Fe_sq.fe_sq(tmp0, tmp1); /* qhasm: AA = A^2 */ /* asm 1: fe_sq.fe_sq(>AA=fe#6,<A=fe#1); */ /* asm 2: fe_sq.fe_sq(>AA=tmp1,<A=x2); */ Fe_sq.fe_sq(tmp1, x2); /* qhasm: t0 = DA+CB */ /* asm 1: fe_add.fe_add(>t0=fe#3,<DA=fe#4,<CB=fe#2); */ /* asm 2: fe_add.fe_add(>t0=x3,<DA=z3,<CB=z2); */ Fe_add.fe_add(x3, z3, z2); /* qhasm: assign x3 to t0 */ /* qhasm: t1 = DA-CB */ /* asm 1: fe_sub.fe_sub(>t1=fe#2,<DA=fe#4,<CB=fe#2); */ /* asm 2: fe_sub.fe_sub(>t1=z2,<DA=z3,<CB=z2); */ Fe_sub.fe_sub(z2, z3, z2); /* qhasm: X4 = AA*BB */ /* asm 1: fe_mul.fe_mul(>X4=fe#1,<AA=fe#6,<BB=fe#5); */ /* asm 2: fe_mul.fe_mul(>X4=x2,<AA=tmp1,<BB=tmp0); */ Fe_mul.fe_mul(x2, tmp1, tmp0); /* qhasm: E = AA-BB */ /* asm 1: fe_sub.fe_sub(>E=fe#6,<AA=fe#6,<BB=fe#5); */ /* asm 2: fe_sub.fe_sub(>E=tmp1,<AA=tmp1,<BB=tmp0); */ Fe_sub.fe_sub(tmp1, tmp1, tmp0); /* qhasm: t2 = t1^2 */ /* asm 1: fe_sq.fe_sq(>t2=fe#2,<t1=fe#2); */ /* asm 2: fe_sq.fe_sq(>t2=z2,<t1=z2); */ Fe_sq.fe_sq(z2, z2); /* qhasm: t3 = a24*E */ /* asm 1: fe_mul121666(>t3=fe#4,<E=fe#6); */ /* asm 2: fe_mul121666(>t3=z3,<E=tmp1); */ Fe_mul121666.fe_mul121666(z3, tmp1); /* qhasm: X5 = t0^2 */ /* asm 1: fe_sq.fe_sq(>X5=fe#3,<t0=fe#3); */ /* asm 2: fe_sq.fe_sq(>X5=x3,<t0=x3); */ Fe_sq.fe_sq(x3, x3); /* qhasm: t4 = BB+t3 */ /* asm 1: fe_add.fe_add(>t4=fe#5,<BB=fe#5,<t3=fe#4); */ /* asm 2: fe_add.fe_add(>t4=tmp0,<BB=tmp0,<t3=z3); */ Fe_add.fe_add(tmp0, tmp0, z3); /* qhasm: Z5 = X1*t2 */ /* asm 1: fe_mul.fe_mul(>Z5=fe#4,x1,<t2=fe#2); */ /* asm 2: fe_mul.fe_mul(>Z5=z3,x1,<t2=z2); */ Fe_mul.fe_mul(z3, x1, z2); /* qhasm: Z4 = E*t4 */ /* asm 1: fe_mul.fe_mul(>Z4=fe#2,<E=fe#6,<t4=fe#5); */ /* asm 2: fe_mul.fe_mul(>Z4=z2,<E=tmp1,<t4=tmp0); */ Fe_mul.fe_mul(z2, tmp1, tmp0); /* qhasm: return */ } Fe_cswap.fe_cswap(x2, x3, swap); Fe_cswap.fe_cswap(z2, z3, swap); Fe_invert.fe_invert(z2, z2); Fe_mul.fe_mul(x2, x2, z2); Fe_tobytes.fe_tobytes(q, x2); return(0); }