-
Notifications
You must be signed in to change notification settings - Fork 26
/
kzg.go
116 lines (105 loc) · 2.77 KB
/
kzg.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//go:build !bignum_pure && !bignum_hol256
// +build !bignum_pure,!bignum_hol256
package kzg
import (
"fmt"
"github.com/protolambda/go-kzg/bls"
)
type KZGSettings struct {
*FFTSettings
// setup values
// [b.multiply(b.G1, pow(s, i, MODULUS)) for i in range(WIDTH+1)],
SecretG1 []bls.G1Point
// [b.multiply(b.G2, pow(s, i, MODULUS)) for i in range(WIDTH+1)],
SecretG2 []bls.G2Point
}
func NewKZGSettings(fs *FFTSettings, secretG1 []bls.G1Point, secretG2 []bls.G2Point) *KZGSettings {
if len(secretG1) != len(secretG2) {
panic("secret list lengths don't match")
}
if uint64(len(secretG1)) < fs.MaxWidth {
panic(fmt.Errorf("expected more values for secrets, MaxWidth: %d, got: %d", fs.MaxWidth, len(secretG1)))
}
ks := &KZGSettings{
FFTSettings: fs,
SecretG1: secretG1,
SecretG2: secretG2,
}
return ks
}
type FK20SingleSettings struct {
*KZGSettings
xExtFFT []bls.G1Point
}
func NewFK20SingleSettings(ks *KZGSettings, n2 uint64) *FK20SingleSettings {
if n2 > ks.MaxWidth {
panic("extended size is larger than kzg settings supports")
}
if !bls.IsPowerOfTwo(n2) {
panic("extended size is not a power of two")
}
if n2 < 2 {
panic("extended size is too small")
}
n := n2 / 2
fk := &FK20SingleSettings{
KZGSettings: ks,
}
x := make([]bls.G1Point, n, n)
for i, j := uint64(0), n-2; i < n-1; i, j = i+1, j-1 {
bls.CopyG1(&x[i], &ks.SecretG1[j])
}
bls.CopyG1(&x[n-1], &bls.ZeroG1)
fk.xExtFFT = fk.toeplitzPart1(x)
return fk
}
type FK20MultiSettings struct {
*KZGSettings
chunkLen uint64
// chunkLen files, each of size MaxWidth
xExtFFTFiles [][]bls.G1Point
}
func NewFK20MultiSettings(ks *KZGSettings, n2 uint64, chunkLen uint64) *FK20MultiSettings {
if n2 > ks.MaxWidth {
panic("extended size is larger than kzg settings supports")
}
if !bls.IsPowerOfTwo(n2) {
panic("extended size is not a power of two")
}
if n2 < 2 {
panic("extended size is too small")
}
if chunkLen > n2/2 {
panic("chunk length is too large")
}
if !bls.IsPowerOfTwo(chunkLen) {
panic("chunk length must be power of two")
}
if chunkLen < 1 {
panic("chunk length is too small")
}
fk := &FK20MultiSettings{
KZGSettings: ks,
chunkLen: chunkLen,
xExtFFTFiles: make([][]bls.G1Point, chunkLen, chunkLen),
}
// xext_fft = []
// for i in range(l):
// x = setup[0][n - l - 1 - i::-l] + [b.Z1]
// xext_fft.append(toeplitz_part1(x))
n := n2 / 2
k := n / chunkLen
xExtFFTPrecompute := func(offset uint64) []bls.G1Point {
x := make([]bls.G1Point, k, k)
start := n - chunkLen - 1 - offset
for i, j := uint64(0), start; i+1 < k; i, j = i+1, j-chunkLen {
bls.CopyG1(&x[i], &ks.SecretG1[j])
}
bls.CopyG1(&x[k-1], &bls.ZeroG1)
return ks.toeplitzPart1(x)
}
for i := uint64(0); i < chunkLen; i++ {
fk.xExtFFTFiles[i] = xExtFFTPrecompute(i)
}
return fk
}