Debian Codegraph @

curve/ed25519/additions/ge_montx_to_p3.c

raw symbols SCIP
1 #include "fe.h"
2 #include "ge.h"
3 #include "assert.h"
4 #include "crypto_additions.h"
5 #include "utility.h"
6
7 /* sqrt(-(A+2)) */
8 static unsigned char A_bytes[32] = {
9 0x06, 0x7e, 0x45, 0xff, 0xaa, 0x04, 0x6e, 0xcc,
10 0x82, 0x1a, 0x7d, 0x4b, 0xd1, 0xd3, 0xa1, 0xc5,
11 0x7e, 0x4f, 0xfc, 0x03, 0xdc, 0x08, 0x7b, 0xd2,
12 0xbb, 0x06, 0xa0, 0x60, 0xf4, 0xed, 0x26, 0x0f
13 };
14
15 void ge_montx_to_p3(ge_p3* p, const fe u, const unsigned char ed_sign_bit)
16 {
17 fe x, y, A, v, v2, iv, nx;
18
19 fe_frombytes(A, A_bytes);
20
21 /* given u, recover edwards y */
22 /* given u, recover v */
23 /* given u and v, recover edwards x */
24
25 fe_montx_to_edy(y, u); /* y = (u - 1) / (u + 1) */
26
27 fe_mont_rhs(v2, u); /* v^2 = u(u^2 + Au + 1) */
28 fe_sqrt(v, v2); /* v = sqrt(v^2) */
29
30 fe_mul(x, u, A); /* x = u * sqrt(-(A+2)) */
31 fe_invert(iv, v); /* 1/v */
32 fe_mul(x, x, iv); /* x = (u/v) * sqrt(-(A+2)) */
33
34 fe_neg(nx, x); /* negate x to match sign bit */
35 fe_cmov(x, nx, fe_isnegative(x) ^ ed_sign_bit);
36
37 fe_copy(p->X, x);
38 fe_copy(p->Y, y);
39 fe_1(p->Z);
40 fe_mul(p->T, p->X, p->Y);
41
42 /* POSTCONDITION: check that p->X and p->Y satisfy the Ed curve equation */
43 /* -x^2 + y^2 = 1 + dx^2y^2 */
44 #ifndef NDEBUG
45 {
46 fe one, d, x2, y2, x2y2, dx2y2;
47
48 unsigned char dbytes[32] = {
49 0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
50 0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
51 0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
52 0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52
53 };
54
55 fe_frombytes(d, dbytes);
56 fe_1(one);
57 fe_sq(x2, p->X); /* x^2 */
58 fe_sq(y2, p->Y); /* y^2 */
59
60 fe_mul(dx2y2, x2, y2); /* x^2y^2 */
61 fe_mul(dx2y2, dx2y2, d); /* dx^2y^2 */
62 fe_add(dx2y2, dx2y2, one); /* dx^2y^2 + 1 */
63
64 fe_neg(x2y2, x2); /* -x^2 */
65 fe_add(x2y2, x2y2, y2); /* -x^2 + y^2 */
66
67 assert(fe_isequal(x2y2, dx2y2));
68 }
69 #endif
70 }