forked from MixinNetwork/mobilecoin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.go
181 lines (154 loc) · 3.87 KB
/
generators.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package api
import (
"encoding/binary"
"github.com/bwesterb/go-ristretto"
"golang.org/x/crypto/sha3"
)
type PedersenGens struct {
B *ristretto.Point
BBlinding *ristretto.Point
}
func NewPedersenGens() *PedersenGens {
var base ristretto.Point
base.SetBase()
return &PedersenGens{
B: hashToPoint(&base),
BBlinding: &base,
}
}
func DefaultPedersenGens() *PedersenGens {
var base ristretto.Point
base.SetBase()
h := sha3.New512()
h.Write(base.Bytes())
return &PedersenGens{
B: &base,
BBlinding: pointFromUniformBytes(h.Sum(nil)),
}
}
// CommitPedersenGens includes multiscalar_mul
func (pg *PedersenGens) Commit(value, blinding *ristretto.Scalar) *ristretto.Point {
return multiscalarMul([]*ristretto.Scalar{value, blinding}, []*ristretto.Point{pg.B, pg.BBlinding})
}
type BulletproofGens struct {
GensCapacity int64
PartyCapacity int64
GVec [][]*ristretto.Point
HVec [][]*ristretto.Point
}
func NewBulletproofGens(gensCapacity, partyCapacity int64) *BulletproofGens {
b := &BulletproofGens{
GensCapacity: 0,
PartyCapacity: partyCapacity,
GVec: make([][]*ristretto.Point, partyCapacity),
HVec: make([][]*ristretto.Point, partyCapacity),
}
b.IncreaseCapacity(gensCapacity)
return b
}
func (b *BulletproofGens) IncreaseCapacity(capacity int64) {
if b.GensCapacity >= capacity {
return
}
for i := 0; i < int(b.PartyCapacity); i++ {
var byte32 [4]byte
binary.LittleEndian.PutUint32(byte32[:], uint32(i))
label := []byte("G")
label = append(label, byte32[:]...)
chainG := NewGeneratorsChain(label)
chainG.FastForward(b.GensCapacity)
genPoints := make([]*ristretto.Point, capacity-b.GensCapacity)
for j := 0; j < int(capacity-b.GensCapacity); j++ {
genPoints[j] = chainG.Next()
}
b.GVec[i] = genPoints
label[0] = []byte("H")[0]
chainP := NewGeneratorsChain(label)
chainP.FastForward(b.GensCapacity)
partyPoints := make([]*ristretto.Point, capacity-b.GensCapacity)
for j := 0; j < int(capacity-b.GensCapacity); j++ {
partyPoints[j] = chainP.Next()
}
b.HVec[i] = partyPoints
}
b.GensCapacity = capacity
}
func (b *BulletproofGens) G(n, m int64) *AggregatedGensIter {
return &AggregatedGensIter{
N: n,
M: m,
Array: b.GVec,
PartyIdX: 0,
GenIdX: 0,
}
}
func (b *BulletproofGens) H(n, m int64) *AggregatedGensIter {
return &AggregatedGensIter{
N: n,
M: m,
Array: b.HVec,
PartyIdX: 0,
GenIdX: 0,
}
}
type AggregatedGensIter struct {
Array [][]*ristretto.Point
N, M int64
PartyIdX int64
GenIdX int64
}
func (a *AggregatedGensIter) Next() *ristretto.Point {
if a.GenIdX >= a.N {
a.GenIdX = 0
a.PartyIdX += 1
}
if a.PartyIdX >= a.M {
return nil
}
cur_gen := a.GenIdX
a.GenIdX += 1
return a.Array[a.PartyIdX][cur_gen]
}
type GeneratorsChain struct {
sha3.ShakeHash
}
func NewGeneratorsChain(label []byte) *GeneratorsChain {
h := sha3.NewShake256()
h.Write([]byte("GeneratorsChain"))
h.Write(label)
return &GeneratorsChain{h}
}
func (c *GeneratorsChain) FastForward(n int64) {
for i := 0; i < int(n); i++ {
var data [64]byte
c.Read(data[:])
}
}
func (c *GeneratorsChain) Next() *ristretto.Point {
var data [64]byte
c.Read(data[:])
return pointFromUniformBytes(data[:])
}
func pointFromUniformBytes(key []byte) *ristretto.Point {
var r1Bytes, r2Bytes [32]byte
copy(r1Bytes[:], key[:32])
copy(r2Bytes[:], key[32:])
var r, r1, r2 ristretto.Point
return r.Add(r1.SetElligator(&r1Bytes), r2.SetElligator(&r2Bytes))
}
type BulletproofGensShare struct {
Gens *BulletproofGens
Share int
}
func (g *BulletproofGens) Share(j int) *BulletproofGensShare {
return &BulletproofGensShare{
Gens: g,
Share: j,
}
}
func (g *BulletproofGensShare) G(n int64) []*ristretto.Point {
return g.Gens.GVec[g.Share][:n]
}
func (g *BulletproofGensShare) H(n int64) []*ristretto.Point {
return g.Gens.HVec[g.Share][:n]
}