Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 64 additions & 56 deletions vms/evm/predicate/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,86 @@ import (
"github.com/ava-labs/libevm/core/types"
)

// delimiter is used as a delimiter for the bytes packed into a precompile predicate.
// Precompile predicates are encoded in the Access List of transactions in the access tuples
// which means that its length must be a multiple of 32 (common.HashLength).
// While the delimiter is always included, for messages that are not a multiple of 32,
// this delimiter is used to append/remove padding.
// delimiter separates the actual predicate bytes from the padded zero bytes.
//
// Predicates are encoded in the Access List of transactions by using in the
// access tuples. This means that the length must be a multiple of
// [common.HashLength].
//
// Even if the original predicate bytes is a multiple of [common.HashLength],
// the delimiter must be appended to support decoding.
const delimiter = 0xff

var (
errEmptyPredicate = errors.New("predicate specified empty predicate")
errAllZeroBytes = errors.New("predicate specified all zero bytes")
errExcessPadding = errors.New("predicate specified excess padding")
errWrongEndDelimiter = errors.New("wrong end delimiter")
errMissingDelimiter = errors.New("no delimiter found")
errExcessPadding = errors.New("predicate included excess padding")
errWrongDelimiter = errors.New("wrong delimiter")
)

type Predicates interface {
HasPredicate(address common.Address) bool
}
// Predicate is a message padded with the delimiter and zeros and chunked into
// 32-byte chunks.
type Predicate []common.Hash

// FromAccessList extracts predicates from a transaction's access list.
// If an address is specified multiple times in the access list, each set of storage
// keys for that address is appended to a slice. Each entry represents a predicate
// encoded as a slice of common.Hash values (32-byte chunks) exactly as stored in
// the access list.
func FromAccessList(rules Predicates, list types.AccessList) map[common.Address][][]common.Hash {
predicateStorageSlots := make(map[common.Address][][]common.Hash)
for _, el := range list {
if !rules.HasPredicate(el.Address) {
continue
}
// Append a copy of the storage keys to avoid aliasing
keys := make([]common.Hash, len(el.StorageKeys))
copy(keys, el.StorageKeys)
predicateStorageSlots[el.Address] = append(predicateStorageSlots[el.Address], keys)
// New constructs a predicate from raw predicate bytes.
//
// It chunks the predicate by appending [predicate.Delimiter] and zero-padding
// to a multiple of 32 bytes.
func New(b []byte) Predicate {
numUnpaddedChunks := len(b) / common.HashLength
chunks := make([]common.Hash, numUnpaddedChunks+1)
// Copy over chunks that don't require padding.
for i := range chunks[:numUnpaddedChunks] {
chunks[i] = common.Hash(b[common.HashLength*i:])
}

return predicateStorageSlots
}

func RoundUpTo32(x int) int {
return (x + 31) / 32 * 32
}

// pack returns a 32-byte aligned predicate from b by appending a delimiter and zero-padding
// until the length is a multiple of 32. 32-byte aligned predicates should not be exposed
// outside of this package.
func pack(b []byte) []byte {
bytes := make([]byte, len(b)+1)
copy(bytes, b)
bytes[len(b)] = delimiter

return common.RightPadBytes(bytes, RoundUpTo32(len(bytes)))
// Add the delimiter and required padding to the last chunk.
copy(chunks[numUnpaddedChunks][:], b[common.HashLength*numUnpaddedChunks:])
chunks[numUnpaddedChunks][len(b)%common.HashLength] = delimiter
return chunks
}

// Unpack unpacks a predicate by stripping right padded zeroes, checking for the delimiter,
// ensuring there is not excess padding, and returning the original message.
// Bytes converts the chunked predicate into the original message.
//
// Returns an error if it finds an incorrect encoding.
func Unpack(p []byte) ([]byte, error) {
if len(p) == 0 {
return nil, errEmptyPredicate
func (p Predicate) Bytes() ([]byte, error) {
padded := make([]byte, common.HashLength*len(p))
for i, chunk := range p {
copy(padded[common.HashLength*i:], chunk[:])
}
trim := common.TrimRightZeroes(p)
if len(trim) == 0 {
return nil, fmt.Errorf("%w: length (%d)", errAllZeroBytes, len(p))
trimmed := common.TrimRightZeroes(padded)
if len(trimmed) == 0 {
return nil, fmt.Errorf("%w: length (%d)", errMissingDelimiter, len(p))
}

if paddedLength := RoundUpTo32(len(trim)); paddedLength != len(p) {
return nil, fmt.Errorf("%w: got length (%d), expected length (%d)", errExcessPadding, len(p), paddedLength)
expectedLen := (len(trimmed) + common.HashLength - 1) / common.HashLength
if expectedLen != len(p) {
return nil, fmt.Errorf("%w: got length (%d), expected length (%d)", errExcessPadding, len(p), expectedLen)
}

if trim[len(trim)-1] != delimiter {
return nil, errWrongEndDelimiter
delimiterIndex := len(trimmed) - 1
if trimmed[delimiterIndex] != delimiter {
return nil, errWrongDelimiter
}

return trimmed[:delimiterIndex], nil
}

type Predicates interface {
HasPredicate(address common.Address) bool
}

// FromAccessList extracts predicates from a transaction's access list.
//
// If an address is specified multiple times in the access list, each set of
// storage keys for that address is considered an individual predicate.
func FromAccessList(rules Predicates, list types.AccessList) map[common.Address][]Predicate {
predicates := make(map[common.Address][]Predicate)
for _, el := range list {
if !rules.HasPredicate(el.Address) {
continue
}
predicates[el.Address] = append(predicates[el.Address], el.StorageKeys)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the copy here... If we want to keep the copy to avoid mutating the predicates if StorageKeys is later modified, then we can do that... But I didn't see a need.

}

return trim[:len(trim)-1], nil
return predicates
}
Loading
Loading