-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
69 lines (62 loc) · 1.48 KB
/
help.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
package fastcommit
import (
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
)
//func allPoly(data []Account) {
// keys := make([]fr.Element, len(data))
// for i := range data {
// keys[i] = data[i].key
// }
//
// var wg sync.WaitGroup
// p, _ := ants.NewPoolWithFunc(12, func(i interface{}) {
// defer wg.Done()
// poly := lagrange(keys, i.(int))
// kzg.Commit(poly, srs.Pk)
// })
// defer p.Release()
//
// for i := 0; i < len(keys); i++ {
// wg.Add(i)
// _ = p.Invoke(i)
// }
// wg.Wait()
//}
func lagrange(keys []fr.Element, index int) []fr.Element {
var one = fr.NewElement(1)
var coefficients = []fr.Element{fr.NewElement(1)}
var denominator fr.Element
for i := 0; i < len(keys); i++ {
if i == index {
continue
}
coefficients = polynomialMul([]fr.Element{*new(fr.Element).Neg(&keys[i]), one}, coefficients)
var tmp fr.Element
tmp.Sub(&keys[index], &keys[i])
denominator.Mul(&denominator, &tmp)
}
var denominatorInv fr.Element
denominatorInv.Div(&one, &denominator)
for i := range coefficients {
coefficients[i].Mul(&coefficients[i], &denominatorInv)
}
return coefficients
}
func polynomialMul(a, b []fr.Element) []fr.Element {
r := arrayOfZeroes(len(a) + len(b) - 1)
var tmp fr.Element
for i := 0; i < len(a); i++ {
for j := 0; j < len(b); j++ {
tmp.Mul(&a[i], &b[j])
r[i+j].Add(&r[i+j], &tmp)
}
}
return r
}
func arrayOfZeroes(n int) []fr.Element {
r := make([]fr.Element, n)
for i := 0; i < n; i++ {
r[i] = fr.NewElement(0)
}
return r[:]
}