| 1 |
|
| 2 |
#include <string.h> |
| 3 |
#include "crypto_nG.h" |
| 4 |
#include "crypto_hash_sha512.h" |
| 5 |
#include "randombytes.h" |
| 6 |
#include "fe25519.h" |
| 7 |
#include "sc25519.h" |
| 8 |
#include "ge25519.h" |
| 9 |
#include "crypto_uint8.h" |
| 10 |
|
| 11 |
void crypto_nG(unsigned char *pk,const unsigned char *sk) |
| 12 |
{ |
| 13 |
unsigned char e[32]; |
| 14 |
sc25519 scsk; |
| 15 |
ge25519 gepk; |
| 16 |
fe25519 ZplusY; |
| 17 |
fe25519 ZminusY; |
| 18 |
fe25519 recip; |
| 19 |
fe25519 x; |
| 20 |
fe25519 y; |
| 21 |
int wantmont; |
| 22 |
|
| 23 |
for (int i = 0;i < 32;++i) e[i] = sk[i]; |
| 24 |
wantmont = crypto_uint8_topbit_01(e[31]); |
| 25 |
e[31] &= 127; |
| 26 |
|
| 27 |
sc25519_from32bytes(&scsk,e); |
| 28 |
|
| 29 |
ge25519_scalarmult_base(&gepk, &scsk); |
| 30 |
|
| 31 |
fe25519_add(&ZplusY,&gepk.z,&gepk.y); |
| 32 |
fe25519_sub(&ZminusY,&gepk.z,&gepk.y); |
| 33 |
fe25519_cmov(&gepk.y,&ZplusY,wantmont); |
| 34 |
fe25519_cmov(&gepk.z,&ZminusY,wantmont); |
| 35 |
|
| 36 |
fe25519_invert(&recip,&gepk.z); |
| 37 |
fe25519_mul(&y,&gepk.y,&recip); |
| 38 |
fe25519_pack(pk,&y); |
| 39 |
|
| 40 |
fe25519_mul(&x,&gepk.x,&recip); |
| 41 |
pk[31] ^= crypto_uint8_shlmod((1-wantmont) & fe25519_getparity(&x),7); |
| 42 |
} |