@@ -115,41 +115,117 @@ static void wb_put_gaussian_draw(unsigned int coefficient, unsigned int mag,
115115 word64 magDraw = gauss_1024_12289 [mag ];
116116 unsigned int i ;
117117
118+ if (mag == 0 ) {
119+ XMEMSET (p , 0 , WB_FALCON_DRAW_SZ );
120+ return ;
121+ }
118122 for (i = 0 ; i < 8 ; i ++ ) {
119123 p [i ] = (byte )(signDraw >> (8 * i ));
120124 p [8 + i ] = (byte )(magDraw >> (8 * i ));
121125 }
122126}
123127
128+ /* Screen candidates through the checks before keygen writes its public
129+ * output. This leaves the trim guard as the only reason not to write h. */
130+ static int wb_falcon_reaches_public (const sword8 * f , const sword8 * g , fpr * work )
131+ {
132+ fpr * a = work ;
133+ fpr * b = a + WB_FALCON_N ;
134+ fpr * inv = b + WB_FALCON_N ;
135+ fpr norm = fpr_zero ;
136+ unsigned int i ;
137+
138+ if (poly_small_sqnorm (f , WB_FALCON_LOGN ) +
139+ poly_small_sqnorm (g , WB_FALCON_LOGN ) >= 16823 )
140+ return 0 ;
141+ poly_small_to_fp (a , f , WB_FALCON_LOGN );
142+ poly_small_to_fp (b , g , WB_FALCON_LOGN );
143+ falcon_FFT (a , WB_FALCON_LOGN );
144+ falcon_FFT (b , WB_FALCON_LOGN );
145+ falcon_poly_invnorm2_fft (inv , a , b , WB_FALCON_LOGN );
146+ falcon_poly_adj_fft (a , WB_FALCON_LOGN );
147+ falcon_poly_adj_fft (b , WB_FALCON_LOGN );
148+ falcon_poly_mulconst (a , fpr_q , WB_FALCON_LOGN );
149+ falcon_poly_mulconst (b , fpr_q , WB_FALCON_LOGN );
150+ falcon_poly_mul_autoadj_fft (a , inv , WB_FALCON_LOGN );
151+ falcon_poly_mul_autoadj_fft (b , inv , WB_FALCON_LOGN );
152+ falcon_iFFT (a , WB_FALCON_LOGN );
153+ falcon_iFFT (b , WB_FALCON_LOGN );
154+ for (i = 0 ; i < WB_FALCON_N ; i ++ ) {
155+ norm = fpr_add (norm , fpr_sqr (a [i ]));
156+ norm = fpr_add (norm , fpr_sqr (b [i ]));
157+ }
158+ if (!fpr_lt (norm , fpr_bnorm_max ))
159+ return 0 ;
160+ return falcon_compute_public ((word16 * )work , f , g , WB_FALCON_LOGN ,
161+ (word16 * )work + WB_FALCON_N );
162+ }
163+
124164static int wb_keygen_draw_limits (WC_RNG * rng )
125165{
166+ sword8 seedF [WB_FALCON_N ], seedG [WB_FALCON_N ];
126167 sword8 f [WB_FALCON_N ], g [WB_FALCON_N ], bigF [WB_FALCON_N ];
127- unsigned int poly ;
168+ word16 h [WB_FALCON_N ];
169+ fpr * work = (fpr * )XMALLOC (3 * WB_FALCON_N * sizeof (fpr ), NULL ,
170+ DYNAMIC_TYPE_TMP_BUFFER );
171+ unsigned int poly , i , attempt ;
128172 int negative ;
173+ int ok = 0 ;
129174
175+ if (work == NULL )
176+ return 0 ;
130177 for (poly = 0 ; poly < 2 ; poly ++ ) {
131178 for (negative = 0 ; negative < 2 ; negative ++ ) {
179+ int found = 0 ;
132180 int ret ;
133181
134- XMEMSET (wb_falcon_draws , 0 , sizeof (wb_falcon_draws ));
135- /* A coefficient at either trim limit is representable by the
136- * Gaussian sampler but must reject the entire candidate. Give
137- * each polynomial odd parity so sampling completes. */
138- wb_put_gaussian_draw (poly * WB_FALCON_N , 16 , negative );
139- wb_put_gaussian_draw (WB_FALCON_N - 1 , 1 , 0 );
140- wb_put_gaussian_draw (2 * WB_FALCON_N - 1 , 1 , 0 );
182+ for (attempt = 0 ; attempt < 8 && !found ; attempt ++ ) {
183+ sword8 * target = poly == 0 ? seedF : seedG ;
184+ sword8 limit = (sword8 )(negative ? -16 : 16 );
185+
186+ if (falcon_keygen (rng , seedF , seedG , bigF , NULL , NULL ,
187+ WB_FALCON_LOGN ) != 0 )
188+ goto out ;
189+ for (i = 0 ; i < WB_FALCON_N && !found ; i ++ ) {
190+ sword8 old = target [i ];
191+
192+ /* Keep the sampler's required odd polynomial parity. */
193+ if (old % 2 != 0 )
194+ continue ;
195+ target [i ] = limit ;
196+ found = wb_falcon_reaches_public (seedF , seedG , work );
197+ if (!found )
198+ target [i ] = old ;
199+ }
200+ }
201+ if (!found )
202+ goto out ;
203+
204+ for (i = 0 ; i < WB_FALCON_N ; i ++ ) {
205+ int x = seedF [i ], y = seedG [i ];
206+ wb_put_gaussian_draw (i , (unsigned int )(x < 0 ? - x : x ),
207+ x < 0 );
208+ wb_put_gaussian_draw (i + WB_FALCON_N ,
209+ (unsigned int )(y < 0 ? - y : y ), y < 0 );
210+ h [i ] = 0xffff ;
211+ }
141212 wb_falcon_draw_offset = 0 ;
142213 wb_falcon_draw_active = 1 ;
143- ret = falcon_keygen (rng , f , g , bigF , NULL , NULL , WB_FALCON_LOGN );
214+ ret = falcon_keygen (rng , f , g , bigF , NULL , h , WB_FALCON_LOGN );
144215 wb_falcon_draw_active = 0 ;
145- /* The stream ends during resampling, so no key is produced. */
146216 if (ret != WC_NO_ERR_TRACE (BAD_FUNC_ARG ) ||
147- wb_falcon_draw_offset != sizeof (wb_falcon_draws )) {
148- return 0 ;
217+ wb_falcon_draw_offset != sizeof (wb_falcon_draws ))
218+ goto out ;
219+ for (i = 0 ; i < WB_FALCON_N ; i ++ ) {
220+ if (h [i ] != 0xffff )
221+ goto out ;
149222 }
150223 }
151224 }
152- return 1 ;
225+ ok = 1 ;
226+ out :
227+ XFREE (work , NULL , DYNAMIC_TYPE_TMP_BUFFER );
228+ return ok ;
153229}
154230#endif
155231
@@ -2379,8 +2455,6 @@ int main(void)
23792455#ifdef WB_FALCON_DRAW_TEST
23802456 if (!wb_keygen_draw_limits (& rng )) {
23812457 WB_FAIL ("keygen did not resample Gaussian trim-limit draws" );
2382- wc_FreeRng (& rng );
2383- return 1 ;
23842458 }
23852459#endif
23862460 }
0 commit comments