Debian Codegraph @

crypto_core/invsntrup953/portable/recip.c

raw
1 // 20250302 djb: port to -Wc++-compat
2 // 20240806 djb: some automated conversion to cryptoint
3 #include "crypto_int8.h"
4 #include "crypto_int16.h"
5 #include "crypto_int32.h"
6 #include "crypto_int64.h"
7 #define int8 crypto_int8
8 #define int16 crypto_int16
9 #define int32 crypto_int32
10
11 #include "crypto_core.h"
12
13 #include "params.h"
14
15 /* ----- arithmetic mod q */
16
17 typedef int8 small;
18
19 typedef int16 Fq;
20 /* always represented as -(q-1)/2...(q-1)/2 */
21
22 /* works for -7000000 < x < 7000000 if q in 4591, 4621, 5167, 6343, 7177, 7879 */
23 static Fq Fq_freeze(int32 x)
24 {
25 x -= q*((q18*x)>>18);
26 x -= q*((q27*x+67108864)>>27);
27 return x;
28 }
29
30 static Fq Fq_bigfreeze(int32 x)
31 {
32 x -= q*((q14*x)>>14);
33 x -= q*((q18*x)>>18);
34 x -= q*((q27*x+67108864)>>27);
35 x -= q*((q27*x+67108864)>>27);
36 return x;
37 }
38
39 /* nonnegative e */
40 static Fq Fq_pow(Fq a,int e)
41 {
42 if (e == 0) return 1;
43 if (e == 1) return a;
44 if (crypto_int64_bottombit_01(e)) return Fq_bigfreeze(a*(int32)Fq_pow(a,e-1));
45 a = Fq_bigfreeze(a*(int32)a);
46 return Fq_pow(a,e>>1);
47 }
48
49 static Fq Fq_recip(Fq a)
50 {
51 return Fq_pow(a,q-2);
52 }
53
54 /* ----- more */
55
56 static inline Fq montproduct(Fq x,Fq y,Fq yqinv)
57 {
58 Fq d = x*yqinv;
59 Fq hi = (((crypto_int32) x)*((crypto_int32) y))>>16;
60 Fq e = (((crypto_int32) d)*((crypto_int32) q))>>16;
61 return hi-e;
62 }
63
64 static inline void vectormodq_swapeliminate(Fq *f,Fq *g,int len,const Fq f0,const Fq g0,int mask)
65 {
66 Fq f0qinv = f0*qinv;
67 Fq g0qinv = g0*qinv;
68 Fq fi,gi,finew,ginew;
69
70 while (len > 0) {
71 fi = f[0];
72 gi = g[0];
73 finew = (fi&~mask)|(gi&mask);
74 ginew = (gi&~mask)|(fi&mask);
75 ginew = montproduct(ginew,f0,f0qinv)-montproduct(finew,g0,g0qinv);
76 f[0] = finew;
77 g[-1] = ginew;
78 ++f;
79 ++g;
80 --len;
81 }
82 }
83
84 static inline void vectormodq_xswapeliminate(Fq *f,Fq *g,int len,const Fq f0,const Fq g0,int mask)
85 {
86 Fq f0qinv = f0*qinv;
87 Fq g0qinv = g0*qinv;
88 Fq fi,gi,finew,ginew;
89
90 f += len;
91 g += len;
92 while (len > 0) {
93 --f;
94 --g;
95 --len;
96 fi = f[0];
97 gi = g[0];
98 finew = (fi&~mask)|(gi&mask);
99 ginew = (gi&~mask)|(fi&mask);
100 ginew = montproduct(ginew,f0,f0qinv)-montproduct(finew,g0,g0qinv);
101 f[1] = finew;
102 g[0] = ginew;
103 }
104 }
105
106 void crypto_core(unsigned char *outbytes,const unsigned char *inbytes,const unsigned char *kbytes,const unsigned char *cbytes)
107 {
108 small *in = (small *) inbytes;
109 int loop;
110 Fq out[p],f[ppad],g[ppad],v[ppad],r[ppad];
111 Fq f0,g0;
112 Fq scale;
113 int i;
114 int delta = 1;
115 int minusdelta;
116 int fgflip;
117 int swap;
118
119 for (i = 0;i < ppad;++i) f[i] = 0;
120 f[0] = 1;
121 f[p-1] = -1;
122 f[p] = -1;
123 /* generalization: initialize f to reversal of any deg-p polynomial m */
124
125 for (i = 0;i < p;++i) g[i] = in[p-1-i];
126 for (i = p;i < ppad;++i) g[i] = 0;
127
128 for (i = 0;i < ppad;++i) r[i] = 0;
129 r[0] = Fq_recip(3);
130
131 for (i = 0;i < ppad;++i) v[i] = 0;
132
133 for (loop = 0;loop < p;++loop) {
134 g0 = Fq_freeze(g[0]);
135 f0 = f[0];
136 if (q > 5167) f0 = Fq_freeze(f0);
137
138 minusdelta = -delta;
139 swap = crypto_int16_negative_mask(minusdelta) & crypto_int16_nonzero_mask(g0);
140 delta ^= swap & (delta ^ minusdelta);
141 delta += 1;
142
143 fgflip = swap & (f0 ^ g0);
144 f0 ^= fgflip;
145 g0 ^= fgflip;
146
147 f[0] = f0;
148
149 vectormodq_swapeliminate(f+1,g+1,p,f0,g0,swap);
150 vectormodq_xswapeliminate(v,r,loop+1,f0,g0,swap);
151 }
152
153 for (loop = p-1;loop > 0;--loop) {
154 g0 = Fq_freeze(g[0]);
155 f0 = f[0];
156 if (q > 5167) f0 = Fq_freeze(f0);
157
158 minusdelta = -delta;
159 swap = crypto_int16_negative_mask(minusdelta) & crypto_int16_nonzero_mask(g0);
160 delta ^= swap & (delta ^ minusdelta);
161 delta += 1;
162
163 fgflip = swap & (f0 ^ g0);
164 f0 ^= fgflip;
165 g0 ^= fgflip;
166
167 f[0] = f0;
168
169 vectormodq_swapeliminate(f+1,g+1,loop,f0,g0,swap);
170 vectormodq_xswapeliminate(v,r,p,f0,g0,swap);
171 }
172
173 scale = Fq_recip(Fq_freeze(f[0]));
174 for (i = 0;i < p;++i) out[i] = Fq_bigfreeze(scale*(int32)Fq_freeze(v[p-i]));
175
176 crypto_encode_pxint16(outbytes,out);
177 outbytes[2*p] = crypto_int16_nonzero_mask(delta);
178 }