| 1 |
#include <string.h> |
| 2 |
#include "crypto_verify_32.h" |
| 3 |
#include "ge.h" |
| 4 |
|
| 5 |
static const fe d = { |
| 6 |
#include "d.h" |
| 7 |
} ; |
| 8 |
|
| 9 |
static const fe sqrtm1 = { |
| 10 |
#include "sqrtm1.h" |
| 11 |
} ; |
| 12 |
|
| 13 |
#include "point26.h" |
| 14 |
|
| 15 |
int ge_frombytes_negate_vartime(ge_p3 *h,const unsigned char *s) |
| 16 |
{ |
| 17 |
unsigned char scheck[32]; |
| 18 |
fe u; |
| 19 |
fe v; |
| 20 |
fe v3; |
| 21 |
fe vxx; |
| 22 |
fe check; |
| 23 |
int ok = 1; |
| 24 |
|
| 25 |
fe_frombytes(h->Y,s); |
| 26 |
|
| 27 |
fe_tobytes(scheck,h->Y); |
| 28 |
scheck[31] |= s[31] & 128; |
| 29 |
if (crypto_verify_32(scheck,s)) ok = 0; |
| 30 |
|
| 31 |
fe_1(h->Z); |
| 32 |
fe_sq(u,h->Y); |
| 33 |
fe_mul(v,u,d); |
| 34 |
fe_sub(u,u,h->Z); /* u = y^2-1 */ |
| 35 |
fe_add(v,v,h->Z); /* v = dy^2+1 */ |
| 36 |
|
| 37 |
fe_sq(v3,v); |
| 38 |
fe_mul(v3,v3,v); /* v3 = v^3 */ |
| 39 |
fe_sq(h->X,v3); |
| 40 |
fe_mul(h->X,h->X,v); |
| 41 |
fe_mul(h->X,h->X,u); /* x = uv^7 */ |
| 42 |
|
| 43 |
fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ |
| 44 |
fe_mul(h->X,h->X,v3); |
| 45 |
fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ |
| 46 |
|
| 47 |
fe_sq(vxx,h->X); |
| 48 |
fe_mul(vxx,vxx,v); |
| 49 |
fe_sub(check,vxx,u); /* vx^2-u */ |
| 50 |
if (fe_isnonzero(check)) { |
| 51 |
fe_add(check,vxx,u); /* vx^2+u */ |
| 52 |
if (fe_isnonzero(check)) ok = 0; |
| 53 |
fe_mul(h->X,h->X,sqrtm1); |
| 54 |
} |
| 55 |
|
| 56 |
if (fe_isnegative(h->X) == (s[31] >> 7)) |
| 57 |
fe_neg(h->X,h->X); |
| 58 |
if (!fe_isnonzero(h->X)) |
| 59 |
if (s[31] >> 7) |
| 60 |
ok = 0; |
| 61 |
|
| 62 |
if (!ok) { /* treat all invalid points as point26 */ |
| 63 |
memcpy(h->X,point26_x,sizeof point26_x); |
| 64 |
memcpy(h->Y,point26_y,sizeof point26_y); |
| 65 |
} |
| 66 |
|
| 67 |
fe_mul(h->T,h->X,h->Y); |
| 68 |
return ok; |
| 69 |
} |