Debian Codegraph @

crypto_nG/merged25519/ref10/nG.c

raw
1 // 20240926 djb: use cryptoint
2
3 #include <string.h>
4 #include "randombytes.h"
5 #include "crypto_nG.h"
6 #include "crypto_hash_sha512.h"
7 #include "ge.h"
8 #include "crypto_uint8.h"
9
10 void crypto_nG(unsigned char *pk,const unsigned char *sk)
11 {
12 unsigned char e[32];
13 ge_p3 A;
14 fe ZplusY;
15 fe ZminusY;
16 fe recip;
17 fe x;
18 fe y;
19 int wantmont;
20
21 for (int i = 0;i < 32;++i) e[i] = sk[i];
22 wantmont = crypto_uint8_topbit_01(e[31]);
23 e[31] &= 127;
24
25 ge_scalarmult_base(&A,e);
26
27 // A has X,Y,Z,T representing X/Z,Y/Z in edwards form
28 // if wantmont: output (Z+Y)/(Z-Y)
29 // else: output Y/Z, with a bit reflecting X/Z
30
31 // doing this in constant time (at minor expense)
32 // in case wantmont is secret
33
34 fe_add(ZplusY,A.Z,A.Y);
35 fe_sub(ZminusY,A.Z,A.Y);
36 fe_cmov(A.Y,ZplusY,wantmont);
37 fe_cmov(A.Z,ZminusY,wantmont);
38
39 fe_invert(recip,A.Z);
40 fe_mul(y,A.Y,recip);
41 fe_tobytes(pk,y);
42
43 fe_mul(x,A.X,recip);
44 pk[31] ^= crypto_uint8_shlmod((1-wantmont) & fe_isnegative(x),7);
45 }