-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdg.go
77 lines (64 loc) · 1.6 KB
/
dg.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
package verifier_sdk
import (
"bytes"
"fmt"
"gitlab.com/distributed_lab/logan/v3/errors"
"math/big"
"strings"
)
// Dg represents Dg block hash
type Dg struct {
checkName string
digest []byte
}
func NewDg(checkName string, digest []byte) (Dg, error) {
if len(digest) == 0 {
return Dg{}, errors.New("failed to create a new Dg: digest is empty")
}
return Dg{
checkName, digest,
}, nil
}
// Verify checks that the provided signals concatenation is dg hash
func (dg *Dg) Verify(paramSignals []string) error {
ints, err := stringsToArrayBigInt([]string{paramSignals[0], paramSignals[1]})
if err != nil {
return errors.Wrap(err, "failed to convert strings to big integers")
}
hashBytes := make([]byte, 0)
hashBytes = append(hashBytes, ints[0].Bytes()...)
hashBytes = append(hashBytes, ints[1].Bytes()...)
if !bytes.Equal(dg.digest, hashBytes) {
return errors.New("encapsulated data and proof pub signals hashes are different")
}
return nil
}
func stringsToArrayBigInt(publicSignals []string) ([]*big.Int, error) {
p := make([]*big.Int, 0, len(publicSignals))
for _, s := range publicSignals {
sb, err := stringToBigInt(s)
if err != nil {
return nil, err
}
p = append(p, sb)
}
return p, nil
}
func stringToBigInt(s string) (*big.Int, error) {
base := 10
if bytes.HasPrefix([]byte(s), []byte("0x")) {
base = 16
s = strings.TrimPrefix(s, "0x")
}
n, ok := new(big.Int).SetString(s, base)
if !ok {
return nil, fmt.Errorf("can not parse string to *big.Int: %s", s)
}
return n, nil
}
func (dg *Dg) GetLength() int {
return 2
}
func (dg *Dg) GetName() string {
return dg.checkName
}