forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 22
/
base.go
93 lines (69 loc) · 1.84 KB
/
base.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
package precompiled
import (
"crypto/sha256"
"math/big"
"golang.org/x/crypto/ripemd160" //nolint:staticcheck
"github.com/dogechain-lab/dogechain/chain"
"github.com/dogechain-lab/dogechain/crypto"
"github.com/dogechain-lab/dogechain/helper/keccak"
)
type ecrecover struct {
p *Precompiled
}
func (e *ecrecover) gas(input []byte, config *chain.ForksInTime) uint64 {
return 3000
}
func (e *ecrecover) run(input []byte) ([]byte, error) {
input, _ = e.p.get(input, 128)
// recover the value v. Expect all zeros except the last byte
for i := 32; i < 63; i++ {
if input[i] != 0 {
return nil, nil
}
}
v := input[63] - 27
r := big.NewInt(0).SetBytes(input[64:96])
s := big.NewInt(0).SetBytes(input[96:128])
if !crypto.ValidateSignatureValues(v, r, s) {
return nil, nil
}
pubKey, err := crypto.Ecrecover(input[:32], append(input[64:128], v))
if err != nil {
return nil, nil
}
dst := keccak.Keccak256(nil, pubKey[1:])
dst = e.p.leftPad(dst[12:], 32)
return dst, nil
}
type identity struct {
}
func (i *identity) gas(input []byte, config *chain.ForksInTime) uint64 {
return baseGasCalc(input, 15, 3)
}
func (i *identity) run(in []byte) ([]byte, error) {
return in, nil
}
type sha256h struct {
}
func (s *sha256h) gas(input []byte, config *chain.ForksInTime) uint64 {
return baseGasCalc(input, 60, 12)
}
func (s *sha256h) run(input []byte) ([]byte, error) {
h := sha256.Sum256(input)
return h[:], nil
}
type ripemd160h struct {
p *Precompiled
}
func (r *ripemd160h) gas(input []byte, config *chain.ForksInTime) uint64 {
return baseGasCalc(input, 600, 120)
}
func (r *ripemd160h) run(input []byte) ([]byte, error) {
ripemd := ripemd160.New()
ripemd.Write(input)
res := ripemd.Sum(nil)
return r.p.leftPad(res, 32), nil
}
func baseGasCalc(input []byte, base, word uint64) uint64 {
return base + uint64(len(input)+31)/32*word
}