|
| 1 | +// Copyright 2025 the libevm authors. |
| 2 | +// |
| 3 | +// The libevm additions to go-ethereum are free software: you can redistribute |
| 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License |
| 5 | +// as published by the Free Software Foundation, either version 3 of the License, |
| 6 | +// or (at your option) any later version. |
| 7 | +// |
| 8 | +// The libevm additions are distributed in the hope that they will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +// General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public License |
| 14 | +// along with the go-ethereum library. If not, see |
| 15 | +// <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +// Package p256verify implements an EVM precompile to verify P256 ECDSA |
| 18 | +// signatures, as described in RIP-7212. |
| 19 | +package p256verify |
| 20 | + |
| 21 | +import ( |
| 22 | + "crypto/ecdsa" |
| 23 | + "crypto/elliptic" |
| 24 | + "math/big" |
| 25 | + |
| 26 | + "github.com/ava-labs/libevm/params" |
| 27 | +) |
| 28 | + |
| 29 | +// Precompile implements ECDSA verification on the P256 curve, as defined by |
| 30 | +// [RIP-7212]. |
| 31 | +// |
| 32 | +// [RIP-7212]: https://github.com/ethereum/RIPs/blob/1f55794f65caa4c4bb2b8d9bda7d713b8c734157/RIPS/rip-7212.md |
| 33 | +type Precompile struct{} |
| 34 | + |
| 35 | +// RequiredGas returns [params.P256VerifyGas]. |
| 36 | +func (Precompile) RequiredGas([]byte) uint64 { |
| 37 | + return params.P256VerifyGas |
| 38 | +} |
| 39 | + |
| 40 | +const ( |
| 41 | + wordLen = 32 |
| 42 | + inputLen = 5 * wordLen |
| 43 | +) |
| 44 | + |
| 45 | +type input [inputLen]byte |
| 46 | + |
| 47 | +type index int |
| 48 | + |
| 49 | +const ( |
| 50 | + hashPos index = iota * wordLen |
| 51 | + rPos |
| 52 | + sPos |
| 53 | + xPos |
| 54 | + yPos |
| 55 | +) |
| 56 | + |
| 57 | +// Run parses and verifies the signature. On success it returns a 32-byte |
| 58 | +// big-endian representation of the number 1, otherwise it returns an empty |
| 59 | +// slice. The returned error is always nil. |
| 60 | +func (Precompile) Run(sig []byte) ([]byte, error) { |
| 61 | + if len(sig) != inputLen || !(*input)(sig).verify() { |
| 62 | + return nil, nil |
| 63 | + } |
| 64 | + return bigEndianOne(), nil |
| 65 | +} |
| 66 | + |
| 67 | +func bigEndianOne() []byte { |
| 68 | + return []byte{wordLen - 1: 1} |
| 69 | +} |
| 70 | + |
| 71 | +func (in *input) verify() bool { |
| 72 | + key, ok := in.pubkey() |
| 73 | + if !ok { |
| 74 | + return false |
| 75 | + } |
| 76 | + return ecdsa.Verify(key, in.word(hashPos), in.bigWord(rPos), in.bigWord(sPos)) |
| 77 | +} |
| 78 | + |
| 79 | +func (in *input) pubkey() (*ecdsa.PublicKey, bool) { |
| 80 | + x := in.bigWord(xPos) |
| 81 | + y := in.bigWord(yPos) |
| 82 | + |
| 83 | + // There is no need to explicitly check for the point at infinity because |
| 84 | + // [elliptic.Curve] documentation states that it's not on the curve and the |
| 85 | + // check would therefore be performed twice. |
| 86 | + // See https://cs.opensource.google/go/go/+/refs/tags/go1.24.3:src/crypto/elliptic/nistec.go;l=132 |
| 87 | + curve := elliptic.P256() |
| 88 | + if !curve.IsOnCurve(x, y) { |
| 89 | + return nil, false |
| 90 | + } |
| 91 | + return &ecdsa.PublicKey{ |
| 92 | + Curve: curve, |
| 93 | + X: x, |
| 94 | + Y: y, |
| 95 | + }, true |
| 96 | +} |
| 97 | + |
| 98 | +func (in *input) word(i index) []byte { |
| 99 | + return in[i : i+wordLen] |
| 100 | +} |
| 101 | + |
| 102 | +func (in *input) bigWord(i index) *big.Int { |
| 103 | + return new(big.Int).SetBytes(in.word(i)) |
| 104 | +} |
| 105 | + |
| 106 | +// Pack packs the arguments into a byte slice compatible with [Precompile.Run]. |
| 107 | +// It does NOT perform any validation on its inputs and therefore may panic if, |
| 108 | +// for example, a [big.Int] with >256 bits is received. Keys and signatures |
| 109 | +// generated with [elliptic.GenerateKey] and [ecdsa.Sign] are valid inputs. |
| 110 | +func Pack(hash [32]byte, r, s *big.Int, key *ecdsa.PublicKey) []byte { |
| 111 | + var in input |
| 112 | + |
| 113 | + copy(in.word(hashPos), hash[:]) |
| 114 | + |
| 115 | + r.FillBytes(in.word(rPos)) |
| 116 | + s.FillBytes(in.word(sPos)) |
| 117 | + |
| 118 | + key.X.FillBytes(in.word(xPos)) |
| 119 | + key.Y.FillBytes(in.word(yPos)) |
| 120 | + |
| 121 | + return in[:] |
| 122 | +} |
0 commit comments