| 1 |
// +build ignore |
| 2 |
// The previous line (and this one up to the warning below) is removed by the |
| 3 |
// template generator. |
| 4 |
|
| 5 |
// Code generated from ./templates/vector.go.tmpl. DO NOT EDIT. |
| 6 |
|
| 7 |
package {{.Name | ToLower}} |
| 8 |
|
| 9 |
import ( |
| 10 |
"io" |
| 11 |
"encoding/binary" |
| 12 |
"math/bits" |
| 13 |
"slices" |
| 14 |
|
| 15 |
"github.com/cloudflare/circl/internal/conv" |
| 16 |
"github.com/cloudflare/circl/internal/sha3" |
| 17 |
"github.com/cloudflare/circl/math" |
| 18 |
"golang.org/x/crypto/cryptobyte" |
| 19 |
) |
| 20 |
|
| 21 |
type Vec []Fp |
| 22 |
|
| 23 |
func (v Vec) Size() uint { return Size*uint(len(v)) } |
| 24 |
|
| 25 |
func (v Vec) AddAssign(x Vec) { |
| 26 |
mustSameLen(v, x) |
| 27 |
for i := range v { |
| 28 |
v[i].AddAssign(&x[i]) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func (v Vec) SubAssign(x Vec) { |
| 33 |
mustSameLen(v, x) |
| 34 |
for i := range v { |
| 35 |
v[i].SubAssign(&x[i]) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func (v Vec) ScalarMul(x *Fp) { |
| 40 |
for i := range v { |
| 41 |
v[i].MulAssign(x) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func (v Vec) DotProduct(x Vec) (out Fp) { |
| 46 |
mustSameLen(v, x) |
| 47 |
var t Fp |
| 48 |
for i := range v { |
| 49 |
t.Mul(&v[i], &x[i]) |
| 50 |
out.AddAssign(&t) |
| 51 |
} |
| 52 |
return |
| 53 |
} |
| 54 |
|
| 55 |
func bitRev(x uint, numBits uint) uint { |
| 56 |
return bits.Reverse(x) >> (bits.UintSize - numBits) |
| 57 |
} |
| 58 |
|
| 59 |
func (v Vec) InvNTT(values Vec, n uint) { |
| 60 |
v.NTT(values, n) |
| 61 |
slices.Reverse(v[1:]) |
| 62 |
} |
| 63 |
|
| 64 |
func (v Vec) NTT(values Vec, n uint) { |
| 65 |
valuesLen := uint(len(values)) |
| 66 |
_, logN := math.NextPow2(n) |
| 67 |
|
| 68 |
for i := range v { |
| 69 |
j := bitRev(uint(i), logN) |
| 70 |
if j < valuesLen { |
| 71 |
v[i] = values[j] |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
var t, w, wn Fp |
| 76 |
for l := uint(1); l <= logN; l++ { |
| 77 |
y := uint(1) << (l - 1) |
| 78 |
chunk := uint(1) << (logN - l) |
| 79 |
|
| 80 |
for j := range chunk { |
| 81 |
x := j << l |
| 82 |
u := v[x] |
| 83 |
v[x+0].Add(&u, &v[x+y]) |
| 84 |
v[x+y].Sub(&u, &v[x+y]) |
| 85 |
} |
| 86 |
|
| 87 |
w.SetOne() |
| 88 |
wn.SetRootOfUnityTwoN(l) |
| 89 |
for i := uint(1); i < y; i++ { |
| 90 |
w.MulAssign(&wn) |
| 91 |
for j := range chunk { |
| 92 |
x := (j << l) + i |
| 93 |
u := v[x] |
| 94 |
t.Mul(&w, &v[x+y]) |
| 95 |
v[x+0].Add(&u, &t) |
| 96 |
v[x+y].Sub(&u, &t) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func (v Vec) SplitBits(n uint64) error { |
| 103 |
if bits.Len64(n) > len(v) { |
| 104 |
return ErrNumberTooLarge |
| 105 |
} |
| 106 |
|
| 107 |
clear(v) |
| 108 |
for i := range v { |
| 109 |
if (n>>i)&0x1 == 1 { |
| 110 |
v[i].SetOne() |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func (v Vec) JoinBits() Fp { |
| 118 |
var two Fp |
| 119 |
_ = two.SetUint64(2) |
| 120 |
return Poly(v).Evaluate(&two) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (v Vec) Random(rnd io.Reader) error { |
| 125 |
var b [Size]byte |
| 126 |
var ok bool |
| 127 |
forVec: |
| 128 |
for i := range v { |
| 129 |
for range maxNumTries { |
| 130 |
_, err := rnd.Read(b[:]) |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
v[i], ok = isInRange(&b) |
| 136 |
if ok { |
| 137 |
v[i].toMont() |
| 138 |
continue forVec |
| 139 |
} |
| 140 |
} |
| 141 |
return ErrMaxNumTries |
| 142 |
} |
| 143 |
|
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func (v Vec) RandomSHA3(s *sha3.State) error { |
| 148 |
for i := range v { |
| 149 |
err := v[i].RandomSHA3(s) |
| 150 |
if err != nil { |
| 151 |
return err |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
func (v Vec) RandomSHA3Bytes(out []byte, s *sha3.State) error { |
| 159 |
var b [Size]byte |
| 160 |
var ok bool |
| 161 |
|
| 162 |
forVec: |
| 163 |
for i := range v { |
| 164 |
for range maxNumTries { |
| 165 |
_, err := s.Read(b[:]) |
| 166 |
if err != nil { |
| 167 |
return err |
| 168 |
} |
| 169 |
|
| 170 |
v[i], ok = isInRange(&b) |
| 171 |
if ok { |
| 172 |
out = append(out, b[:]...) |
| 173 |
v[i].toMont() |
| 174 |
continue forVec |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return ErrMaxNumTries |
| 179 |
} |
| 180 |
return nil |
| 181 |
} |
| 182 |
|
| 183 |
func (v Vec) MarshalBinary() ([]byte, error) { |
| 184 |
return conv.MarshalBinaryLen(v, v.Size()) |
| 185 |
} |
| 186 |
|
| 187 |
func (v Vec) UnmarshalBinary(b []byte) error { |
| 188 |
return conv.UnmarshalBinary(v, b) |
| 189 |
} |
| 190 |
|
| 191 |
func (v Vec) Marshal(b *cryptobyte.Builder) error { |
| 192 |
for i := range v { |
| 193 |
v[i].Marshal(b) |
| 194 |
} |
| 195 |
return nil |
| 196 |
} |
| 197 |
|
| 198 |
func (v Vec) Unmarshal(s *cryptobyte.String) bool{ |
| 199 |
for i := range v { |
| 200 |
if !v[i].Unmarshal(s) { |
| 201 |
return false |
| 202 |
} |
| 203 |
} |
| 204 |
return true |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func isInRange(b *[Size]byte) (out [{{.NumUint64}}]uint64, ok bool) { |
| 209 |
{{- if .NeedsMasking}} |
| 210 |
panic("unimplemented: recall to implement masking before decoding") |
| 211 |
{{- else }} |
| 212 |
{{- if eq .Bits 64}} |
| 213 |
out[0] = binary.LittleEndian.Uint64(b[:8]) |
| 214 |
ok = out[0] < orderP0 |
| 215 |
{{- else if eq .Bits 128}} |
| 216 |
out[1] = binary.LittleEndian.Uint64(b[8:]) |
| 217 |
out[0] = binary.LittleEndian.Uint64(b[:8]) |
| 218 |
ok = out[1] < orderP1 || (out[1] == orderP1 && out[0] < orderP0) |
| 219 |
{{- end }} |
| 220 |
{{- end }} |
| 221 |
return |
| 222 |
} |
| 223 |
|
| 224 |
func mustSameLen[T ~[]E, E any](x, y T) { |
| 225 |
if len(x) != len(y) { |
| 226 |
panic(ErrMatchLen) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func mustSumLen[T ~[]E, E any](z, x, y T) { |
| 231 |
if len(z) != len(x)+len(y)-1 { |
| 232 |
panic(ErrMatchLen) |
| 233 |
} |
| 234 |
} |