1 // Code generated from kyber512/internal/vec.go by gen.go
2 3 package internal
4 5 import (
6 "github.com/cloudflare/circl/pke/kyber/internal/common"
7 )
8 9 // A vector of K polynomials
10 type Vec [K]common.Poly
11 12 // Samples v[i] from a centered binomial distribution with given η,
13 // seed and nonce+i.
14 //
15 // Essentially CBD_η(PRF(seed, nonce+i)) from the specification.
16 func (v *Vec) DeriveNoise(seed []byte, nonce uint8, eta int) {
17 for i := 0; i < K; i++ {
18 v[i].DeriveNoise(seed, nonce+uint8(i), eta)
19 }
20 }
21 22 // Sets p to the inner product of a and b using "pointwise" multiplication.
23 //
24 // See MulHat() and NTT() for a description of the multiplication.
25 // Assumes a and b are in Montgomery form. p will be in Montgomery form,
26 // and its coefficients will be bounded in absolute value by 2kq.
27 // If a and b are not in Montgomery form, then the action is the same
28 // as "pointwise" multiplication followed by multiplying by R⁻¹, the inverse
29 // of the Montgomery factor.
30 func PolyDotHat(p *common.Poly, a, b *Vec) {
31 var t common.Poly
32 *p = common.Poly{} // set p to zero
33 for i := 0; i < K; i++ {
34 t.MulHat(&a[i], &b[i])
35 p.Add(&t, p)
36 }
37 }
38 39 // Almost normalizes coefficients in-place.
40 //
41 // Ensures each coefficient is in {0, …, q}.
42 func (v *Vec) BarrettReduce() {
43 for i := 0; i < K; i++ {
44 v[i].BarrettReduce()
45 }
46 }
47 48 // Normalizes coefficients in-place.
49 //
50 // Ensures each coefficient is in {0, …, q-1}.
51 func (v *Vec) Normalize() {
52 for i := 0; i < K; i++ {
53 v[i].Normalize()
54 }
55 }
56 57 // Applies in-place inverse NTT(). See Poly.InvNTT() for assumptions.
58 func (v *Vec) InvNTT() {
59 for i := 0; i < K; i++ {
60 v[i].InvNTT()
61 }
62 }
63 64 // Applies in-place forward NTT(). See Poly.NTT() for assumptions.
65 func (v *Vec) NTT() {
66 for i := 0; i < K; i++ {
67 v[i].NTT()
68 }
69 }
70 71 // Sets v to a + b.
72 func (v *Vec) Add(a, b *Vec) {
73 for i := 0; i < K; i++ {
74 v[i].Add(&a[i], &b[i])
75 }
76 }
77 78 // Packs v into buf, which must be of length K*PolySize.
79 func (v *Vec) Pack(buf []byte) {
80 for i := 0; i < K; i++ {
81 v[i].Pack(buf[common.PolySize*i:])
82 }
83 }
84 85 // Unpacks v from buf which must be of length K*PolySize.
86 func (v *Vec) Unpack(buf []byte) {
87 for i := 0; i < K; i++ {
88 v[i].Unpack(buf[common.PolySize*i:])
89 }
90 }
91 92 // Writes Compress_q(v, d) to m.
93 //
94 // Assumes v is normalized and d is in {3, 4, 5, 10, 11}.
95 func (v *Vec) CompressTo(m []byte, d int) {
96 size := compressedPolySize(d)
97 for i := 0; i < K; i++ {
98 v[i].CompressTo(m[size*i:], d)
99 }
100 }
101 102 // Set v to Decompress_q(m, 1).
103 //
104 // Assumes d is in {3, 4, 5, 10, 11}. v will be normalized.
105 func (v *Vec) Decompress(m []byte, d int) {
106 size := compressedPolySize(d)
107 for i := 0; i < K; i++ {
108 v[i].Decompress(m[size*i:], d)
109 }
110 }
111 112 // ⌈(256 d)/8⌉
113 func compressedPolySize(d int) int {
114 switch d {
115 case 4:
116 return 128
117 case 5:
118 return 160
119 case 10:
120 return 320
121 case 11:
122 return 352
123 }
124 panic("unsupported d")
125 }
126