1 /* $OpenBSD: sntrup4591761.c,v 1.3 2019/01/30 19:51:15 markus Exp $ */
4 * Public Domain, Authors:
5 * - Daniel J. Bernstein
6 * - Chitchanok Chuengsatiansup
8 * - Christine van Vredendaal
14 #include "crypto_api.h"
16 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/int32_sort.h */
21 static void int32_sort(crypto_int32 *,int);
25 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/int32_sort.c */
26 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
29 static void minmax(crypto_int32 *x,crypto_int32 *y)
31 crypto_uint32 xi = *x;
32 crypto_uint32 yi = *y;
33 crypto_uint32 xy = xi ^ yi;
34 crypto_uint32 c = yi - xi;
43 static void int32_sort(crypto_int32 *x,int n)
49 while (top < n - top) top += top;
51 for (p = top;p > 0;p >>= 1) {
52 for (i = 0;i < n - p;++i)
54 minmax(x + i,x + i + p);
55 for (q = top;q > p;q >>= 1)
56 for (i = 0;i < n - q;++i)
58 minmax(x + i + p,x + i + q);
62 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/small.h */
67 typedef crypto_int8 small;
69 static void small_encode(unsigned char *,const small *);
71 static void small_decode(small *,const unsigned char *);
74 static void small_random(small *);
76 static void small_random_weightw(small *);
80 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/mod3.h */
85 /* -1 if x is nonzero, 0 otherwise */
86 static inline int mod3_nonzero_mask(small x)
91 /* input between -100000 and 100000 */
92 /* output between -1 and 1 */
93 static inline small mod3_freeze(crypto_int32 a)
95 a -= 3 * ((10923 * a) >> 15);
96 a -= 3 * ((89478485 * a + 134217728) >> 28);
100 static inline small mod3_minusproduct(small a,small b,small c)
105 return mod3_freeze(A - B * C);
108 static inline small mod3_plusproduct(small a,small b,small c)
113 return mod3_freeze(A + B * C);
116 static inline small mod3_product(small a,small b)
121 static inline small mod3_sum(small a,small b)
125 return mod3_freeze(A + B);
128 static inline small mod3_reciprocal(small a1)
133 static inline small mod3_quotient(small num,small den)
135 return mod3_product(num,mod3_reciprocal(den));
140 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/modq.h */
145 typedef crypto_int16 modq;
147 /* -1 if x is nonzero, 0 otherwise */
148 static inline int modq_nonzero_mask(modq x)
150 crypto_int32 r = (crypto_uint16) x;
156 /* input between -9000000 and 9000000 */
157 /* output between -2295 and 2295 */
158 static inline modq modq_freeze(crypto_int32 a)
160 a -= 4591 * ((228 * a) >> 20);
161 a -= 4591 * ((58470 * a + 134217728) >> 28);
165 static inline modq modq_minusproduct(modq a,modq b,modq c)
170 return modq_freeze(A - B * C);
173 static inline modq modq_plusproduct(modq a,modq b,modq c)
178 return modq_freeze(A + B * C);
181 static inline modq modq_product(modq a,modq b)
185 return modq_freeze(A * B);
188 static inline modq modq_square(modq a)
191 return modq_freeze(A * A);
194 static inline modq modq_sum(modq a,modq b)
198 return modq_freeze(A + B);
201 static inline modq modq_reciprocal(modq a1)
203 modq a2 = modq_square(a1);
204 modq a3 = modq_product(a2,a1);
205 modq a4 = modq_square(a2);
206 modq a8 = modq_square(a4);
207 modq a16 = modq_square(a8);
208 modq a32 = modq_square(a16);
209 modq a35 = modq_product(a32,a3);
210 modq a70 = modq_square(a35);
211 modq a140 = modq_square(a70);
212 modq a143 = modq_product(a140,a3);
213 modq a286 = modq_square(a143);
214 modq a572 = modq_square(a286);
215 modq a1144 = modq_square(a572);
216 modq a1147 = modq_product(a1144,a3);
217 modq a2294 = modq_square(a1147);
218 modq a4588 = modq_square(a2294);
219 modq a4589 = modq_product(a4588,a1);
223 static inline modq modq_quotient(modq num,modq den)
225 return modq_product(num,modq_reciprocal(den));
230 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/params.h */
235 /* XXX: also built into modq in various ways */
241 #define rq_encode_len 1218
242 #define small_encode_len 191
246 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/r3.h */
251 static void r3_mult(small *,const small *,const small *);
253 extern int r3_recip(small *,const small *);
257 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/rq.h */
262 static void rq_encode(unsigned char *,const modq *);
264 static void rq_decode(modq *,const unsigned char *);
266 static void rq_encoderounded(unsigned char *,const modq *);
268 static void rq_decoderounded(modq *,const unsigned char *);
270 static void rq_round3(modq *,const modq *);
272 static void rq_mult(modq *,const modq *,const small *);
274 int rq_recip3(modq *,const small *);
278 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/swap.h */
282 static void swap(void *,void *,int,int);
286 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/dec.c */
287 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
293 int crypto_kem_sntrup4591761_dec(
295 const unsigned char *cstr,
296 const unsigned char *sk
307 unsigned char rstr[small_encode_len];
308 unsigned char hash[64];
314 small_decode(grecip,sk + small_encode_len);
315 rq_decode(h,sk + 2 * small_encode_len);
317 rq_decoderounded(c,cstr + 32);
320 for (i = 0;i < p;++i) t3[i] = mod3_freeze(modq_freeze(3*t[i]));
322 r3_mult(r,t3,grecip);
327 printf("decrypt r:");
328 for (j = 0;j < p;++j)
329 if (r[j] == 1) printf(" +%d",j);
330 else if (r[j] == -1) printf(" -%d",j);
336 for (i = 0;i < p;++i) weight += (1 & r[i]);
338 result |= modq_nonzero_mask(weight); /* XXX: puts limit on p */
342 for (i = 0;i < p;++i) result |= modq_nonzero_mask(hr[i] - c[i]);
344 small_encode(rstr,r);
345 crypto_hash_sha512(hash,rstr,sizeof rstr);
346 result |= crypto_verify_32(hash,cstr);
348 for (i = 0;i < 32;++i) k[i] = (hash[32 + i] & ~result);
352 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/enc.c */
353 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
359 int crypto_kem_sntrup4591761_enc(
362 const unsigned char *pk
368 unsigned char rstr[small_encode_len];
369 unsigned char hash[64];
371 small_random_weightw(r);
376 printf("encrypt r:");
377 for (i = 0;i < p;++i)
378 if (r[i] == 1) printf(" +%d",i);
379 else if (r[i] == -1) printf(" -%d",i);
384 small_encode(rstr,r);
385 crypto_hash_sha512(hash,rstr,sizeof rstr);
391 memcpy(k,hash + 32,32);
392 memcpy(cstr,hash,32);
393 rq_encoderounded(cstr + 32,c);
398 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/keypair.c */
399 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
402 #if crypto_kem_sntrup4591761_PUBLICKEYBYTES != rq_encode_len
403 #error "crypto_kem_sntrup4591761_PUBLICKEYBYTES must match rq_encode_len"
405 #if crypto_kem_sntrup4591761_SECRETKEYBYTES != rq_encode_len + 2 * small_encode_len
406 #error "crypto_kem_sntrup4591761_SECRETKEYBYTES must match rq_encode_len + 2 * small_encode_len"
409 int crypto_kem_sntrup4591761_keypair(unsigned char *pk,unsigned char *sk)
419 while (r3_recip(grecip,g) != 0);
421 small_random_weightw(f);
422 rq_recip3(f3recip,f);
424 rq_mult(h,f3recip,g);
428 small_encode(sk + small_encode_len,grecip);
429 memcpy(sk + 2 * small_encode_len,pk,rq_encode_len);
434 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/r3_mult.c */
435 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
438 static void r3_mult(small *h,const small *f,const small *g)
444 for (i = 0;i < p;++i) {
446 for (j = 0;j <= i;++j)
447 result = mod3_plusproduct(result,f[j],g[i - j]);
450 for (i = p;i < p + p - 1;++i) {
452 for (j = i - p + 1;j < p;++j)
453 result = mod3_plusproduct(result,f[j],g[i - j]);
457 for (i = p + p - 2;i >= p;--i) {
458 fg[i - p] = mod3_sum(fg[i - p],fg[i]);
459 fg[i - p + 1] = mod3_sum(fg[i - p + 1],fg[i]);
462 for (i = 0;i < p;++i)
466 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/r3_recip.c */
467 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
470 /* caller must ensure that x-y does not overflow */
471 static int smaller_mask_r3_recip(int x,int y)
473 return (x - y) >> 31;
476 static void vectormod3_product(small *z,int len,const small *x,const small c)
479 for (i = 0;i < len;++i) z[i] = mod3_product(x[i],c);
482 static void vectormod3_minusproduct(small *z,int len,const small *x,const small *y,const small c)
485 for (i = 0;i < len;++i) z[i] = mod3_minusproduct(x[i],y[i],c);
488 static void vectormod3_shift(small *z,int len)
491 for (i = len - 1;i > 0;--i) z[i] = z[i - 1];
496 r = s^(-1) mod m, returning 0, if s is invertible mod m
497 or returning -1 if s is not invertible mod m
498 r,s are polys of degree <p
501 int r3_recip(small *r,const small *s)
503 const int loops = 2*p + 1;
515 for (i = 2;i < p;++i) f[i] = 0;
519 /* generalization: can initialize f to any polynomial m */
520 /* requirements: m has degree exactly p, nonzero constant coefficient */
522 for (i = 0;i < p;++i) g[i] = s[i];
525 for (i = 0;i <= loops;++i) u[i] = 0;
528 for (i = 1;i <= loops;++i) v[i] = 0;
532 /* e == -1 or d + e + loop <= 2*p */
534 /* f has degree p: i.e., f[p]!=0 */
535 /* f[i]==0 for i < p-d */
537 /* g has degree <=p (so it fits in p+1 coefficients) */
538 /* g[i]==0 for i < p-e */
540 /* u has degree <=loop (so it fits in loop+1 coefficients) */
541 /* u[i]==0 for i < p-d */
542 /* if invertible: u[i]==0 for i < loop-p (so can look at just p+1 coefficients) */
544 /* v has degree <=loop (so it fits in loop+1 coefficients) */
545 /* v[i]==0 for i < p-e */
546 /* v[i]==0 for i < loop-p (so can look at just p+1 coefficients) */
548 if (loop >= loops) break;
550 c = mod3_quotient(g[p],f[p]);
552 vectormod3_minusproduct(g,p + 1,g,f,c);
553 vectormod3_shift(g,p + 1);
556 vectormod3_minusproduct(v,loops + 1,v,u,c);
557 vectormod3_shift(v,loops + 1);
560 vectormod3_minusproduct(v,loop + 1,v,u,c);
561 vectormod3_shift(v,loop + 2);
563 vectormod3_minusproduct(v + loop - p,p + 1,v + loop - p,u + loop - p,c);
564 vectormod3_shift(v + loop - p,p + 2);
572 swapmask = smaller_mask_r3_recip(e,d) & mod3_nonzero_mask(g[p]);
573 swap(&e,&d,sizeof e,swapmask);
574 swap(f,g,(p + 1) * sizeof(small),swapmask);
577 swap(u,v,(loops + 1) * sizeof(small),swapmask);
580 swap(u,v,(loop + 1) * sizeof(small),swapmask);
582 swap(u + loop - p,v + loop - p,(p + 1) * sizeof(small),swapmask);
587 c = mod3_reciprocal(f[p]);
588 vectormod3_product(r,p,u + p,c);
589 return smaller_mask_r3_recip(0,d);
592 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/randomsmall.c */
593 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
596 static void small_random(small *g)
600 for (i = 0;i < p;++i) {
601 crypto_uint32 r = small_random32();
602 g[i] = (small) (((1073741823 & r) * 3) >> 30) - 1;
606 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/randomweightw.c */
607 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
610 static void small_random_weightw(small *f)
615 for (i = 0;i < p;++i) r[i] = small_random32();
616 for (i = 0;i < w;++i) r[i] &= -2;
617 for (i = w;i < p;++i) r[i] = (r[i] & -3) | 1;
619 for (i = 0;i < p;++i) f[i] = ((small) (r[i] & 3)) - 1;
622 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/rq.c */
623 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
626 static void rq_encode(unsigned char *c,const modq *f)
628 crypto_int32 f0, f1, f2, f3, f4;
631 for (i = 0;i < p/5;++i) {
637 /* now want f0 + 6144*f1 + ... as a 64-bit integer */
642 /* now want f0 + f1<<11 + f2<<22 + f3<<33 + f4<<44 */
656 /* XXX: using p mod 5 = 1 */
662 static void rq_decode(modq *f,const unsigned char *c)
664 crypto_uint32 c0, c1, c2, c3, c4, c5, c6, c7;
665 crypto_uint32 f0, f1, f2, f3, f4;
668 for (i = 0;i < p/5;++i) {
678 /* f0 + f1*6144 + f2*6144^2 + f3*6144^3 + f4*6144^4 */
679 /* = c0 + c1*256 + ... + c6*256^6 + c7*256^7 */
680 /* with each f between 0 and 4590 */
683 /* c6 <= 23241 = floor(4591*6144^4/2^48) */
684 /* f4 = (16/81)c6 + (1/1296)(c5+[0,1]) - [0,0.75] */
685 /* claim: 2^19 f4 < x < 2^19(f4+1) */
686 /* where x = 103564 c6 + 405(c5+1) */
687 /* proof: x - 2^19 f4 = (76/81)c6 + (37/81)c5 + 405 - (32768/81)[0,1] + 2^19[0,0.75] */
688 /* at least 405 - 32768/81 > 0 */
689 /* at most (76/81)23241 + (37/81)255 + 405 + 2^19 0.75 < 2^19 */
690 f4 = (103564*c6 + 405*(c5+1)) >> 19;
693 c5 -= (f4 * 81) << 4;
696 /* f0 + f1*6144 + f2*6144^2 + f3*6144^3 */
697 /* = c0 + c1*256 + c2*256^2 + c3*256^3 + c4*256^4 */
698 /* c4 <= 247914 = floor(4591*6144^3/2^32) */
699 /* f3 = (1/54)(c4+[0,1]) - [0,0.75] */
700 /* claim: 2^19 f3 < x < 2^19(f3+1) */
701 /* where x = 9709(c4+2) */
702 /* proof: x - 2^19 f3 = 19418 - (1/27)c4 - (262144/27)[0,1] + 2^19[0,0.75] */
703 /* at least 19418 - 247914/27 - 262144/27 > 0 */
704 /* at most 19418 + 2^19 0.75 < 2^19 */
705 f3 = (9709*(c4+2)) >> 19;
707 c4 -= (f3 * 27) << 1;
709 /* f0 + f1*6144 + f2*6144^2 */
710 /* = c0 + c1*256 + c2*256^2 + c3*256^3 */
711 /* c3 <= 10329 = floor(4591*6144^2/2^24) */
712 /* f2 = (4/9)c3 + (1/576)c2 + (1/147456)c1 + (1/37748736)c0 - [0,0.75] */
713 /* claim: 2^19 f2 < x < 2^19(f2+1) */
714 /* where x = 233017 c3 + 910(c2+2) */
715 /* proof: x - 2^19 f2 = 1820 + (1/9)c3 - (2/9)c2 - (32/9)c1 - (1/72)c0 + 2^19[0,0.75] */
716 /* at least 1820 - (2/9)255 - (32/9)255 - (1/72)255 > 0 */
717 /* at most 1820 + (1/9)10329 + 2^19 0.75 < 2^19 */
718 f2 = (233017*c3 + 910*(c2+2)) >> 19;
725 /* c1 <= 110184 = floor(4591*6144/2^8) */
726 /* f1 = (1/24)c1 + (1/6144)c0 - (1/6144)f0 */
727 /* claim: 2^19 f1 < x < 2^19(f1+1) */
728 /* where x = 21845(c1+2) + 85 c0 */
729 /* proof: x - 2^19 f1 = 43690 - (1/3)c1 - (1/3)c0 + 2^19 [0,0.75] */
730 /* at least 43690 - (1/3)110184 - (1/3)255 > 0 */
731 /* at most 43690 + 2^19 0.75 < 2^19 */
732 f1 = (21845*(c1+2) + 85*c0) >> 19;
738 *f++ = modq_freeze(f0 + q - qshift);
739 *f++ = modq_freeze(f1 + q - qshift);
740 *f++ = modq_freeze(f2 + q - qshift);
741 *f++ = modq_freeze(f3 + q - qshift);
742 *f++ = modq_freeze(f4 + q - qshift);
748 *f++ = modq_freeze(c0 + q - qshift);
751 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/rq_mult.c */
752 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
755 static void rq_mult(modq *h,const modq *f,const small *g)
761 for (i = 0;i < p;++i) {
763 for (j = 0;j <= i;++j)
764 result = modq_plusproduct(result,f[j],g[i - j]);
767 for (i = p;i < p + p - 1;++i) {
769 for (j = i - p + 1;j < p;++j)
770 result = modq_plusproduct(result,f[j],g[i - j]);
774 for (i = p + p - 2;i >= p;--i) {
775 fg[i - p] = modq_sum(fg[i - p],fg[i]);
776 fg[i - p + 1] = modq_sum(fg[i - p + 1],fg[i]);
779 for (i = 0;i < p;++i)
783 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/rq_recip3.c */
784 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
787 /* caller must ensure that x-y does not overflow */
788 static int smaller_mask_rq_recip3(int x,int y)
790 return (x - y) >> 31;
793 static void vectormodq_product(modq *z,int len,const modq *x,const modq c)
796 for (i = 0;i < len;++i) z[i] = modq_product(x[i],c);
799 static void vectormodq_minusproduct(modq *z,int len,const modq *x,const modq *y,const modq c)
802 for (i = 0;i < len;++i) z[i] = modq_minusproduct(x[i],y[i],c);
805 static void vectormodq_shift(modq *z,int len)
808 for (i = len - 1;i > 0;--i) z[i] = z[i - 1];
813 r = (3s)^(-1) mod m, returning 0, if s is invertible mod m
814 or returning -1 if s is not invertible mod m
815 r,s are polys of degree <p
818 int rq_recip3(modq *r,const small *s)
820 const int loops = 2*p + 1;
832 for (i = 2;i < p;++i) f[i] = 0;
836 /* generalization: can initialize f to any polynomial m */
837 /* requirements: m has degree exactly p, nonzero constant coefficient */
839 for (i = 0;i < p;++i) g[i] = 3 * s[i];
842 for (i = 0;i <= loops;++i) u[i] = 0;
845 for (i = 1;i <= loops;++i) v[i] = 0;
849 /* e == -1 or d + e + loop <= 2*p */
851 /* f has degree p: i.e., f[p]!=0 */
852 /* f[i]==0 for i < p-d */
854 /* g has degree <=p (so it fits in p+1 coefficients) */
855 /* g[i]==0 for i < p-e */
857 /* u has degree <=loop (so it fits in loop+1 coefficients) */
858 /* u[i]==0 for i < p-d */
859 /* if invertible: u[i]==0 for i < loop-p (so can look at just p+1 coefficients) */
861 /* v has degree <=loop (so it fits in loop+1 coefficients) */
862 /* v[i]==0 for i < p-e */
863 /* v[i]==0 for i < loop-p (so can look at just p+1 coefficients) */
865 if (loop >= loops) break;
867 c = modq_quotient(g[p],f[p]);
869 vectormodq_minusproduct(g,p + 1,g,f,c);
870 vectormodq_shift(g,p + 1);
873 vectormodq_minusproduct(v,loops + 1,v,u,c);
874 vectormodq_shift(v,loops + 1);
877 vectormodq_minusproduct(v,loop + 1,v,u,c);
878 vectormodq_shift(v,loop + 2);
880 vectormodq_minusproduct(v + loop - p,p + 1,v + loop - p,u + loop - p,c);
881 vectormodq_shift(v + loop - p,p + 2);
889 swapmask = smaller_mask_rq_recip3(e,d) & modq_nonzero_mask(g[p]);
890 swap(&e,&d,sizeof e,swapmask);
891 swap(f,g,(p + 1) * sizeof(modq),swapmask);
894 swap(u,v,(loops + 1) * sizeof(modq),swapmask);
897 swap(u,v,(loop + 1) * sizeof(modq),swapmask);
899 swap(u + loop - p,v + loop - p,(p + 1) * sizeof(modq),swapmask);
904 c = modq_reciprocal(f[p]);
905 vectormodq_product(r,p,u + p,c);
906 return smaller_mask_rq_recip3(0,d);
909 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/rq_round3.c */
910 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
913 static void rq_round3(modq *h,const modq *f)
917 for (i = 0;i < p;++i)
918 h[i] = ((21846 * (f[i] + 2295) + 32768) >> 16) * 3 - 2295;
921 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/rq_rounded.c */
922 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
925 static void rq_encoderounded(unsigned char *c,const modq *f)
927 crypto_int32 f0, f1, f2;
930 for (i = 0;i < p/3;++i) {
934 f0 = (21846 * f0) >> 16;
935 f1 = (21846 * f1) >> 16;
936 f2 = (21846 * f2) >> 16;
937 /* now want f0 + f1*1536 + f2*1536^2 as a 32-bit integer */
947 /* XXX: using p mod 3 = 2 */
950 f0 = (21846 * f0) >> 16;
951 f1 = (21846 * f1) >> 16;
959 static void rq_decoderounded(modq *f,const unsigned char *c)
961 crypto_uint32 c0, c1, c2, c3;
962 crypto_uint32 f0, f1, f2;
965 for (i = 0;i < p/3;++i) {
971 /* f0 + f1*1536 + f2*1536^2 */
972 /* = c0 + c1*256 + c2*256^2 + c3*256^3 */
973 /* with each f between 0 and 1530 */
975 /* f2 = (64/9)c3 + (1/36)c2 + (1/9216)c1 + (1/2359296)c0 - [0,0.99675] */
976 /* claim: 2^21 f2 < x < 2^21(f2+1) */
977 /* where x = 14913081*c3 + 58254*c2 + 228*(c1+2) */
978 /* proof: x - 2^21 f2 = 456 - (8/9)c0 + (4/9)c1 - (2/9)c2 + (1/9)c3 + 2^21 [0,0.99675] */
979 /* at least 456 - (8/9)255 - (2/9)255 > 0 */
980 /* at most 456 + (4/9)255 + (1/9)255 + 2^21 0.99675 < 2^21 */
981 f2 = (14913081*c3 + 58254*c2 + 228*(c1+2)) >> 21;
986 /* = c0 + c1*256 + c2*256^2 */
987 /* c2 <= 35 = floor((1530+1530*1536)/256^2) */
988 /* f1 = (128/3)c2 + (1/6)c1 + (1/1536)c0 - (1/1536)f0 */
989 /* claim: 2^21 f1 < x < 2^21(f1+1) */
990 /* where x = 89478485*c2 + 349525*c1 + 1365*(c0+1) */
991 /* proof: x - 2^21 f1 = 1365 - (1/3)c2 - (1/3)c1 - (1/3)c0 + (4096/3)f0 */
992 /* at least 1365 - (1/3)35 - (1/3)255 - (1/3)255 > 0 */
993 /* at most 1365 + (4096/3)1530 < 2^21 */
994 f1 = (89478485*c2 + 349525*c1 + 1365*(c0+1)) >> 21;
1002 *f++ = modq_freeze(f0 * 3 + q - qshift);
1003 *f++ = modq_freeze(f1 * 3 + q - qshift);
1004 *f++ = modq_freeze(f2 * 3 + q - qshift);
1011 f1 = (89478485*c2 + 349525*c1 + 1365*(c0+1)) >> 21;
1014 c1 -= (f1 * 3) << 1;
1019 *f++ = modq_freeze(f0 * 3 + q - qshift);
1020 *f++ = modq_freeze(f1 * 3 + q - qshift);
1023 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/small.c */
1024 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
1027 /* XXX: these functions rely on p mod 4 = 1 */
1029 /* all coefficients in -1, 0, 1 */
1030 static void small_encode(unsigned char *c,const small *f)
1035 for (i = 0;i < p/4;++i) {
1037 c0 += (*f++ + 1) << 2;
1038 c0 += (*f++ + 1) << 4;
1039 c0 += (*f++ + 1) << 6;
1046 static void small_decode(small *f,const unsigned char *c)
1051 for (i = 0;i < p/4;++i) {
1053 *f++ = ((small) (c0 & 3)) - 1; c0 >>= 2;
1054 *f++ = ((small) (c0 & 3)) - 1; c0 >>= 2;
1055 *f++ = ((small) (c0 & 3)) - 1; c0 >>= 2;
1056 *f++ = ((small) (c0 & 3)) - 1;
1059 *f++ = ((small) (c0 & 3)) - 1;
1062 /* from libpqcrypto-20180314/crypto_kem/sntrup4591761/ref/swap.c */
1063 /* See https://ntruprime.cr.yp.to/software.html for detailed documentation. */
1066 static void swap(void *x,void *y,int bytes,int mask)
1073 for (i = 0;i < bytes;++i) {