forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converters.go
65 lines (57 loc) · 1.6 KB
/
converters.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
package etherman
import (
"fmt"
"math/big"
"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/proverclient/pb"
)
const (
minProofLen = 2
maxProofLen = 3
)
func stringToFixedByteArray(str string) ([32]byte, error) {
var res [32]byte
data, err := hex.DecodeHex(str)
if err != nil {
return [32]byte{}, err
}
copy(res[:], data)
return res, nil
}
func strSliceToBigIntArray(data []string) ([2]*big.Int, error) {
if len(data) < minProofLen || len(data) > maxProofLen {
return [2]*big.Int{}, fmt.Errorf("wrong slice length, current %d, expected between %d or %d", len(data), minProofLen, maxProofLen)
}
var res [2]*big.Int
for i, v := range data {
if i < minProofLen {
bigInt, ok := new(big.Int).SetString(v, encoding.Base10)
if !ok {
return [2]*big.Int{}, fmt.Errorf("failed to convert string to big int, str: %s", v)
}
res[i] = bigInt
}
}
return res, nil
}
func proofSlcToIntArray(proofs []*pb.ProofB) ([2][2]*big.Int, error) {
if len(proofs) < minProofLen || len(proofs) > maxProofLen {
return [2][2]*big.Int{}, fmt.Errorf("wrong slice length, current %d, expected between %d or %d", len(proofs), minProofLen, maxProofLen)
}
var res [2][2]*big.Int
for i, v := range proofs {
if i < minProofLen {
for j, b := range proofs[i].Proofs {
if j < minProofLen {
bigInt, ok := new(big.Int).SetString(b, encoding.Base10)
if !ok {
return [2][2]*big.Int{}, fmt.Errorf("failed to convert string to big int, str: %s", v)
}
res[i][1-j] = bigInt
}
}
}
}
return res, nil
}