forked from bnb-chain/zkbnb-setup
-
Notifications
You must be signed in to change notification settings - Fork 4
/
contribution.go
69 lines (59 loc) · 1.27 KB
/
contribution.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 phase2
import (
"crypto/sha256"
"io"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/worldcoin/semaphore-mtb-setup/common"
)
type Contribution struct {
Delta bn254.G1Affine
PublicKey common.PublicKey
Hash []byte
}
func (c *Contribution) writeTo(writer io.Writer) (int64, error) {
toEncode := []interface{}{
&c.Delta,
&c.PublicKey.S,
&c.PublicKey.SX,
&c.PublicKey.SPX,
}
enc := bn254.NewEncoder(writer)
for _, v := range toEncode {
if err := enc.Encode(v); err != nil {
return enc.BytesWritten(), err
}
}
nBytes, err := writer.Write(c.Hash)
return int64(nBytes), err
}
func (c *Contribution) readFrom(reader io.Reader) (int64, error) {
toDecode := []interface{}{
&c.Delta,
&c.PublicKey.S,
&c.PublicKey.SX,
&c.PublicKey.SPX,
}
dec := bn254.NewDecoder(reader)
for _, v := range toDecode {
if err := dec.Decode(v); err != nil {
return dec.BytesRead(), err
}
}
c.Hash = make([]byte, 32)
nBytes, err := reader.Read(c.Hash)
return int64(nBytes), err
}
func computeHash(c *Contribution) []byte {
sha := sha256.New()
toEncode := []interface{}{
&c.Delta,
&c.PublicKey.S,
&c.PublicKey.SX,
&c.PublicKey.SPX,
}
enc := bn254.NewEncoder(sha)
for _, v := range toEncode {
enc.Encode(v)
}
return sha.Sum(nil)
}