@@ -38,10 +38,23 @@ func (Precompile) RequiredGas([]byte) uint64 {
3838 return params .P256VerifyGas
3939}
4040
41- const inputLen = 160
41+ const (
42+ wordLen = 32
43+ inputLen = 5 * wordLen
44+ )
4245
4346type input [inputLen ]byte
4447
48+ type index int
49+
50+ const (
51+ hashPos index = iota * wordLen
52+ rPos
53+ sPos
54+ xPos
55+ yPos
56+ )
57+
4558// Run parses and verifies the signature. On success it returns a 32-byte
4659// big-endian representation of the number 1, otherwise it returns an empty
4760// slice. The returned error is always nil.
@@ -57,12 +70,12 @@ func (in *input) verify() bool {
5770 if ! ok {
5871 return false
5972 }
60- return ecdsa .Verify (key , in .word (0 ), in .bigWord (1 ), in .bigWord (2 ))
73+ return ecdsa .Verify (key , in .word (hashPos ), in .bigWord (rPos ), in .bigWord (sPos ))
6174}
6275
6376func (in * input ) pubkey () (* ecdsa.PublicKey , bool ) {
64- x := in .bigWord (3 )
65- y := in .bigWord (4 )
77+ x := in .bigWord (xPos )
78+ y := in .bigWord (yPos )
6679 if x .Sign () == 0 && y .Sign () == 0 {
6780 return nil , false
6881 }
@@ -78,13 +91,12 @@ func (in *input) pubkey() (*ecdsa.PublicKey, bool) {
7891 }, true
7992}
8093
81- func (in * input ) word (index int ) []byte {
82- s := index * 32
83- return in [s : s + 32 ]
94+ func (in * input ) word (i index ) []byte {
95+ return in [i : i + wordLen ]
8496}
8597
86- func (in * input ) bigWord (index int ) * big.Int {
87- return new (big.Int ).SetBytes (in .word (index ))
98+ func (in * input ) bigWord (i index ) * big.Int {
99+ return new (big.Int ).SetBytes (in .word (i ))
88100}
89101
90102// Sign signs `hash` with the private key, using [rand.Reader] as the first
@@ -105,10 +117,14 @@ func Sign(priv *ecdsa.PrivateKey, hash [32]byte) ([]byte, error) {
105117// generated with [elliptic.GenerateKey] and [ecdsa.Sign] are valid inputs.
106118func Pack (hash [32 ]byte , r , s * big.Int , key * ecdsa.PublicKey ) []byte {
107119 var in input
108- copy (in .word (0 ), hash [:])
109- r .FillBytes (in .word (1 ))
110- s .FillBytes (in .word (2 ))
111- key .X .FillBytes (in .word (3 ))
112- key .Y .FillBytes (in .word (4 ))
120+
121+ copy (in .word (hashPos ), hash [:])
122+
123+ r .FillBytes (in .word (rPos ))
124+ s .FillBytes (in .word (sPos ))
125+
126+ key .X .FillBytes (in .word (xPos ))
127+ key .Y .FillBytes (in .word (yPos ))
128+
113129 return in [:]
114130}
0 commit comments