github.com/cloudflare/circl@v1.5.0/sign/mldsa/mldsa87/internal/vec.go (about) 1 // Code generated from mode3/internal/vec.go by gen.go 2 3 package internal 4 5 import ( 6 common "github.com/cloudflare/circl/sign/internal/dilithium" 7 ) 8 9 // A vector of L polynomials. 10 type VecL [L]common.Poly 11 12 // A vector of K polynomials. 13 type VecK [K]common.Poly 14 15 // Normalize the polynomials in this vector. 16 func (v *VecL) Normalize() { 17 for i := 0; i < L; i++ { 18 v[i].Normalize() 19 } 20 } 21 22 // Normalize the polynomials in this vector assuming their coefficients 23 // are already bounded by 2q. 24 func (v *VecL) NormalizeAssumingLe2Q() { 25 for i := 0; i < L; i++ { 26 v[i].NormalizeAssumingLe2Q() 27 } 28 } 29 30 // Sets v to w + u. Does not normalize. 31 func (v *VecL) Add(w, u *VecL) { 32 for i := 0; i < L; i++ { 33 v[i].Add(&w[i], &u[i]) 34 } 35 } 36 37 // Applies NTT componentwise. See Poly.NTT() for details. 38 func (v *VecL) NTT() { 39 for i := 0; i < L; i++ { 40 v[i].NTT() 41 } 42 } 43 44 // Checks whether any of the coefficients exceeds the given bound in supnorm 45 // 46 // Requires the vector to be normalized. 47 func (v *VecL) Exceeds(bound uint32) bool { 48 for i := 0; i < L; i++ { 49 if v[i].Exceeds(bound) { 50 return true 51 } 52 } 53 return false 54 } 55 56 // Applies Poly.Power2Round componentwise. 57 // 58 // Requires the vector to be normalized. 59 func (v *VecL) Power2Round(v0PlusQ, v1 *VecL) { 60 for i := 0; i < L; i++ { 61 v[i].Power2Round(&v0PlusQ[i], &v1[i]) 62 } 63 } 64 65 // Applies Poly.Decompose componentwise. 66 // 67 // Requires the vector to be normalized. 68 func (v *VecL) Decompose(v0PlusQ, v1 *VecL) { 69 for i := 0; i < L; i++ { 70 PolyDecompose(&v[i], &v0PlusQ[i], &v1[i]) 71 } 72 } 73 74 // Sequentially packs each polynomial using Poly.PackLeqEta(). 75 func (v *VecL) PackLeqEta(buf []byte) { 76 offset := 0 77 for i := 0; i < L; i++ { 78 PolyPackLeqEta(&v[i], buf[offset:]) 79 offset += PolyLeqEtaSize 80 } 81 } 82 83 // Sets v to the polynomials packed in buf using VecL.PackLeqEta(). 84 func (v *VecL) UnpackLeqEta(buf []byte) { 85 offset := 0 86 for i := 0; i < L; i++ { 87 PolyUnpackLeqEta(&v[i], buf[offset:]) 88 offset += PolyLeqEtaSize 89 } 90 } 91 92 // Sequentially packs each polynomial using PolyPackLeGamma1(). 93 func (v *VecL) PackLeGamma1(buf []byte) { 94 offset := 0 95 for i := 0; i < L; i++ { 96 PolyPackLeGamma1(&v[i], buf[offset:]) 97 offset += PolyLeGamma1Size 98 } 99 } 100 101 // Sets v to the polynomials packed in buf using VecL.PackLeGamma1(). 102 func (v *VecL) UnpackLeGamma1(buf []byte) { 103 offset := 0 104 for i := 0; i < L; i++ { 105 PolyUnpackLeGamma1(&v[i], buf[offset:]) 106 offset += PolyLeGamma1Size 107 } 108 } 109 110 // Normalize the polynomials in this vector. 111 func (v *VecK) Normalize() { 112 for i := 0; i < K; i++ { 113 v[i].Normalize() 114 } 115 } 116 117 // Normalize the polynomials in this vector assuming their coefficients 118 // are already bounded by 2q. 119 func (v *VecK) NormalizeAssumingLe2Q() { 120 for i := 0; i < K; i++ { 121 v[i].NormalizeAssumingLe2Q() 122 } 123 } 124 125 // Sets v to w + u. Does not normalize. 126 func (v *VecK) Add(w, u *VecK) { 127 for i := 0; i < K; i++ { 128 v[i].Add(&w[i], &u[i]) 129 } 130 } 131 132 // Checks whether any of the coefficients exceeds the given bound in supnorm 133 // 134 // Requires the vector to be normalized. 135 func (v *VecK) Exceeds(bound uint32) bool { 136 for i := 0; i < K; i++ { 137 if v[i].Exceeds(bound) { 138 return true 139 } 140 } 141 return false 142 } 143 144 // Applies Poly.Power2Round componentwise. 145 // 146 // Requires the vector to be normalized. 147 func (v *VecK) Power2Round(v0PlusQ, v1 *VecK) { 148 for i := 0; i < K; i++ { 149 v[i].Power2Round(&v0PlusQ[i], &v1[i]) 150 } 151 } 152 153 // Applies Poly.Decompose componentwise. 154 // 155 // Requires the vector to be normalized. 156 func (v *VecK) Decompose(v0PlusQ, v1 *VecK) { 157 for i := 0; i < K; i++ { 158 PolyDecompose(&v[i], &v0PlusQ[i], &v1[i]) 159 } 160 } 161 162 // Sets v to the hint vector for v0 the modified low bits and v1 163 // the unmodified high bits --- see makeHint(). 164 // 165 // Returns the number of ones in the hint vector. 166 func (v *VecK) MakeHint(v0, v1 *VecK) (pop uint32) { 167 for i := 0; i < K; i++ { 168 pop += PolyMakeHint(&v[i], &v0[i], &v1[i]) 169 } 170 return 171 } 172 173 // Computes corrections to the high bits of the polynomials in the vector 174 // w using the hints in h and sets v to the corrected high bits. Returns v. 175 // See useHint(). 176 func (v *VecK) UseHint(q, hint *VecK) *VecK { 177 for i := 0; i < K; i++ { 178 PolyUseHint(&v[i], &q[i], &hint[i]) 179 } 180 return v 181 } 182 183 // Sequentially packs each polynomial using Poly.PackT1(). 184 func (v *VecK) PackT1(buf []byte) { 185 offset := 0 186 for i := 0; i < K; i++ { 187 v[i].PackT1(buf[offset:]) 188 offset += common.PolyT1Size 189 } 190 } 191 192 // Sets v to the vector packed into buf by PackT1(). 193 func (v *VecK) UnpackT1(buf []byte) { 194 offset := 0 195 for i := 0; i < K; i++ { 196 v[i].UnpackT1(buf[offset:]) 197 offset += common.PolyT1Size 198 } 199 } 200 201 // Sequentially packs each polynomial using Poly.PackT0(). 202 func (v *VecK) PackT0(buf []byte) { 203 offset := 0 204 for i := 0; i < K; i++ { 205 v[i].PackT0(buf[offset:]) 206 offset += common.PolyT0Size 207 } 208 } 209 210 // Sets v to the vector packed into buf by PackT0(). 211 func (v *VecK) UnpackT0(buf []byte) { 212 offset := 0 213 for i := 0; i < K; i++ { 214 v[i].UnpackT0(buf[offset:]) 215 offset += common.PolyT0Size 216 } 217 } 218 219 // Sequentially packs each polynomial using Poly.PackLeqEta(). 220 func (v *VecK) PackLeqEta(buf []byte) { 221 offset := 0 222 for i := 0; i < K; i++ { 223 PolyPackLeqEta(&v[i], buf[offset:]) 224 offset += PolyLeqEtaSize 225 } 226 } 227 228 // Sets v to the polynomials packed in buf using VecK.PackLeqEta(). 229 func (v *VecK) UnpackLeqEta(buf []byte) { 230 offset := 0 231 for i := 0; i < K; i++ { 232 PolyUnpackLeqEta(&v[i], buf[offset:]) 233 offset += PolyLeqEtaSize 234 } 235 } 236 237 // Applies NTT componentwise. See Poly.NTT() for details. 238 func (v *VecK) NTT() { 239 for i := 0; i < K; i++ { 240 v[i].NTT() 241 } 242 } 243 244 // Sequentially packs each polynomial using PolyPackW1(). 245 func (v *VecK) PackW1(buf []byte) { 246 offset := 0 247 for i := 0; i < K; i++ { 248 PolyPackW1(&v[i], buf[offset:]) 249 offset += PolyW1Size 250 } 251 } 252 253 // Sets v to a - b. 254 // 255 // Warning: assumes coefficients of the polynomials of b are less than 2q. 256 func (v *VecK) Sub(a, b *VecK) { 257 for i := 0; i < K; i++ { 258 v[i].Sub(&a[i], &b[i]) 259 } 260 } 261 262 // Sets v to 2ᵈ w without reducing. 263 func (v *VecK) MulBy2toD(w *VecK) { 264 for i := 0; i < K; i++ { 265 v[i].MulBy2toD(&w[i]) 266 } 267 } 268 269 // Applies InvNTT componentwise. See Poly.InvNTT() for details. 270 func (v *VecK) InvNTT() { 271 for i := 0; i < K; i++ { 272 v[i].InvNTT() 273 } 274 } 275 276 // Applies Poly.ReduceLe2Q() componentwise. 277 func (v *VecK) ReduceLe2Q() { 278 for i := 0; i < K; i++ { 279 v[i].ReduceLe2Q() 280 } 281 }