-
Notifications
You must be signed in to change notification settings - Fork 1
/
poly.go
125 lines (109 loc) · 2.22 KB
/
poly.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
117
118
119
120
121
122
123
124
125
package tpke
import (
"github.com/leesper/go_rng"
"github.com/boohyunsik/tpke/bls"
"math/rand"
"time"
)
type Poly struct {
coeff []*bls.FR
}
func randomPoly(degree int) *Poly {
coeff := make([]*bls.FR, degree)
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
uRng := rng.NewUniformGenerator(int64(r1.Int()))
for i := range coeff {
fr:= bls.NewFRRepr(uint64(uRng.Int64()))
coeff[i] = bls.FRReprToFR(fr)
}
return &Poly {
coeff: coeff,
}
}
func (p *Poly) evaluate(x bls.FR) *bls.FR {
i := len(p.coeff) - 1
result := p.coeff[i].Copy()
for i >= 0 {
if i != len(p.coeff) - 1 {
result.MulAssign(&x)
result.AddAssign(p.coeff[i])
}
i--
}
return result
}
func (p *Poly) AddAssign(op *Poly) {
pLen := len(p.coeff)
opLen := len(op.coeff)
FRZero := bls.FRReprToFR(bls.NewFRRepr(0))
for pLen < opLen {
p.coeff = append(p.coeff, FRZero)
pLen++
}
for i := range p.coeff {
p.coeff[i].AddAssign(op.coeff[i])
}
}
func (p *Poly) MulAssign(x bls.FR) {
// TODO : check if op is zero
for _, c := range p.coeff {
c.MulAssign(&x)
}
}
func (p *Poly) degree() int {
return len(p.coeff)
}
func (p *Poly) commitment() *Commitment {
g1One := bls.G1AffineOne
coeff := make([]*bls.G1Projective, len(p.coeff))
for i := range coeff {
coeff[i] = g1One.MulFR(p.coeff[i].ToRepr())
}
return &Commitment {
coeff: coeff,
}
}
type Commitment struct {
coeff []*bls.G1Projective
}
func (c *Commitment) Clone() *Commitment {
coeff := make([]*bls.G1Projective, len(c.coeff))
for i := range coeff {
coeff[i] = c.coeff[i].Copy()
}
return &Commitment {
coeff: coeff,
}
}
func (c *Commitment) degree() int {
return len(c.coeff) - 1
}
func (c *Commitment) evaluate(x bls.FR) *bls.G1Projective {
if len(c.coeff) == 0 {
return bls.G1ProjectiveZero
}
i := len(c.coeff) - 1
result := c.coeff[i]
for i >= 0 {
if i != len(c.coeff) - 1{
result = result.MulFR(x.ToRepr())
result = result.Add(c.coeff[i])
}
i--
}
return result
}
func (c *Commitment) AddAssign(op *Commitment) {
pLen := len(c.coeff)
opLen := len(op.coeff)
for pLen < opLen {
c.coeff = append(c.coeff, bls.G1ProjectiveZero)
pLen++
}
for i := range c.coeff {
c.coeff[i].Add(op.coeff[i])
}
}
func (c *Commitment) MulAssign() {
}