-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlwe.go
62 lines (51 loc) · 1.25 KB
/
lwe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package database
import (
"io"
"github.com/si-co/vpir-code/lib/matrix"
"github.com/si-co/vpir-code/lib/utils"
)
type LWE struct {
Matrix *matrix.MatrixBytes
Info
}
const plaintextModulus = 2
const blockSizeLWE = 1 // for backward compatibility
func Digest(db *LWE, rows int) *matrix.Matrix {
return matrix.BinaryMul(
matrix.NewRandom(
utils.NewPRG(utils.ParamsDefault().SeedA),
utils.ParamsDefault().N,
rows,
), db.Matrix)
}
func CreateRandomBinaryLWEWithLength(rnd io.Reader, dbLen int) *LWE {
numRows, numColumns := CalculateNumRowsAndColumns(dbLen, true)
return CreateRandomBinaryLWE(rnd, numRows, numColumns)
}
func CreateRandomBinaryLWE(rnd io.Reader, numRows, numColumns int) *LWE {
m := matrix.NewBytes(numRows, numColumns)
// read random bytes for filling out the entries
data := make([]byte, (numRows*numColumns)/8+1)
if _, err := rnd.Read(data); err != nil {
panic(err)
}
for i := 0; i < m.Len(); i++ {
val := (data[i/8] >> (i % 8)) & 1
if val >= plaintextModulus {
panic("Plaintext value too large")
}
m.SetData(i, val)
}
db := &LWE{
Matrix: m,
Info: Info{
NumRows: numRows,
NumColumns: numColumns,
BlockSize: blockSizeLWE,
},
}
db.Auth = &Auth{
DigestLWE: Digest(db, numRows),
}
return db
}