void ge_montx_to_p2(ge_p2* p, const fe u, const unsigned char ed_sign_bit) { fe x, y, A, v, v2, iv, nx; 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(y, u); /* y = (u - 1) / (u + 1) */ fe_mont_rhs(v2, u); /* v^2 = u(u^2 + Au + 1) */ fe_sqrt(v, v2); /* v = sqrt(v^2) */ fe_mul(x, u, A); /* x = u * sqrt(-(A+2)) */ fe_invert(iv, v); /* 1/v */ fe_mul(x, x, iv); /* x = (u/v) * sqrt(-(A+2)) */ fe_neg(nx, x); /* negate x to match sign bit */ fe_cmov(x, nx, fe_isnegative(x) ^ ed_sign_bit); fe_copy(p->X, x); fe_copy(p->Y, y); 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 }
void fe_sqrt(fe out, const fe a) { fe exp, b, b2, bi, i; #ifndef NDEBUG fe legendre, zero, one; #endif fe_frombytes(i, i_bytes); fe_pow22523(exp, a); /* b = a^(q-5)/8 */ /* PRECONDITION: legendre symbol == 1 (square) or 0 (a == zero) */ #ifndef NDEBUG 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(b, a, exp); /* b = a * a^(q-5)/8 */ 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(bi, b, i); fe_cmov(b, bi, 1 ^ fe_isequal(b2, a)); fe_copy(out, b); /* PRECONDITION: out^2 == a */ #ifndef NDEBUG fe_sq(b2, out); assert(fe_isequal(a, b2)); #endif }
int elligator_fast_test(int silent) { unsigned char elligator_correct_output[32] = { 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 }; unsigned char hashtopoint_correct_output1[32] = { 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 }; unsigned char hashtopoint_correct_output2[32] = { 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 }; unsigned char calculatev_correct_output[32] = { 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; fe in, out; unsigned char bytes[32]; fe_0(in); fe_0(out); for (count = 0; count < 32; count++) { bytes[count] = count; } fe_frombytes(in, bytes); elligator(out, in); fe_tobytes(bytes, out); TEST("Elligator vector", memcmp(bytes, elligator_correct_output, 32) == 0); /* Elligator(0) == 0 test */ fe_0(in); elligator(out, in); TEST("Elligator(0) == 0", memcmp(in, out, 32) == 0); /* ge_montx_to_p3(0) -> order2 point test */ fe one, negone, zero; fe_1(one); fe_0(zero); fe_sub(negone, zero, one); ge_p3 p3; ge_montx_to_p3(&p3, zero, 0); TEST("ge_montx_to_p3(0) == order 2 point", fe_isequal(p3.X, zero) && fe_isequal(p3.Y, negone) && fe_isequal(p3.Z, one) && fe_isequal(p3.T, zero)); /* Hash to point vector test */ unsigned char htp[32]; for (count=0; count < 32; count++) { htp[count] = count; } hash_to_point(&p3, htp, 32); ge_p3_tobytes(htp, &p3); TEST("hash_to_point #1", memcmp(htp, hashtopoint_correct_output1, 32) == 0); for (count=0; count < 32; count++) { htp[count] = count+1; } hash_to_point(&p3, htp, 32); ge_p3_tobytes(htp, &p3); TEST("hash_to_point #2", memcmp(htp, hashtopoint_correct_output2, 32) == 0); /* calculate_U vector test */ ge_p3 Bv; unsigned char V[32]; unsigned char Vbuf[200]; unsigned char a[32]; unsigned char A[32]; unsigned char Vmsg[3]; Vmsg[0] = 0; Vmsg[1] = 1; Vmsg[2] = 2; for (count=0; count < 32; count++) { a[count] = 8 + count; A[count] = 9 + count; } sc_clamp(a); calculate_Bv_and_V(&Bv, V, Vbuf, a, A, Vmsg, 3); TEST("calculate_Bv_and_V vector", memcmp(V, calculatev_correct_output, 32) == 0); return 0; }