-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add predicate packing helper #710
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad0a59a
Add predicate packing helper
aaronbuchwald 73fbfd4
move predicate code and add readme
aaronbuchwald de0691d
Merge branch 'master' into predicate-packer
aaronbuchwald 7f4591a
fix moved import
aaronbuchwald fd25831
Merge branch 'master' into predicate-packer
aaronbuchwald b93d549
fix merge
aaronbuchwald 196e552
Merge branch 'master' into predicate-packer
aaronbuchwald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Predicate Utils | ||
|
||
This package provides simple helpers to pack/unpack byte slices for a predicate transaction, where a byte slice of size N is encoded in the access list of a transaction. | ||
|
||
## Encoding | ||
|
||
A byte slice of size N is encoded as: | ||
|
||
1. Slice of N bytes | ||
2. Delimiter byte `0xff` | ||
3. Appended 0s to the nearest multiple of 32 bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// (c) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package predicateutils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// PredicateEndByte 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). | ||
// For messages with a length that does not comply to that, this delimiter is used to | ||
// append/remove padding. | ||
var PredicateEndByte = byte(0xff) | ||
|
||
// PackPredicate packs [predicate] by delimiting the actual message with [PredicateEndByte] | ||
// and zero padding to reach a length that is a multiple of 32. | ||
func PackPredicate(predicate []byte) []byte { | ||
predicate = append(predicate, PredicateEndByte) | ||
return common.RightPadBytes(predicate, (len(predicate)+31)/32*32) | ||
} | ||
|
||
// UnpackPredicate unpacks a predicate by stripping right padded zeroes, checking for the delimter, | ||
// ensuring there is not excess padding, and returning the original message. | ||
// Returns an error if it finds an incorrect encoding. | ||
func UnpackPredicate(paddedPredicate []byte) ([]byte, error) { | ||
trimmedPredicateBytes := common.TrimRightZeroes(paddedPredicate) | ||
if len(trimmedPredicateBytes) == 0 { | ||
return nil, fmt.Errorf("predicate specified invalid all zero bytes: 0x%x", paddedPredicate) | ||
} | ||
|
||
if expectedPaddedLength := (len(trimmedPredicateBytes) + 31) / 32 * 32; expectedPaddedLength != len(paddedPredicate) { | ||
return nil, fmt.Errorf("predicate specified invalid padding with length (%d), expected length (%d)", len(paddedPredicate), expectedPaddedLength) | ||
} | ||
|
||
if trimmedPredicateBytes[len(trimmedPredicateBytes)-1] != PredicateEndByte { | ||
return nil, fmt.Errorf("invalid end delimiter") | ||
} | ||
|
||
return trimmedPredicateBytes[:len(trimmedPredicateBytes)-1], nil | ||
} | ||
|
||
// HashSliceToBytes serializes a []common.Hash into a tightly packed byte array. | ||
func HashSliceToBytes(hashes []common.Hash) []byte { | ||
bytes := make([]byte, common.HashLength*len(hashes)) | ||
for i, hash := range hashes { | ||
copy(bytes[i*common.HashLength:], hash[:]) | ||
} | ||
return bytes | ||
} | ||
|
||
// BytesToHashSlice packs [b] into a slice of hash values with zero padding | ||
// to the right if the length of b is not a multiple of 32. | ||
func BytesToHashSlice(b []byte) []common.Hash { | ||
var ( | ||
numHashes = (len(b) + 31) / 32 | ||
hashes = make([]common.Hash, numHashes) | ||
) | ||
|
||
for i := range hashes { | ||
start := i * common.HashLength | ||
copy(hashes[i][:], b[start:]) | ||
} | ||
return hashes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// (c) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package predicateutils | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/utils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func testBytesToHashSlice(t testing.TB, b []byte) { | ||
hashSlice := BytesToHashSlice(b) | ||
|
||
copiedBytes := HashSliceToBytes(hashSlice) | ||
|
||
if len(b)%32 == 0 { | ||
require.Equal(t, b, copiedBytes) | ||
} else { | ||
require.Equal(t, b, copiedBytes[:len(b)]) | ||
// Require that any additional padding is all zeroes | ||
padding := copiedBytes[len(b):] | ||
require.Equal(t, bytes.Repeat([]byte{0x00}, len(padding)), padding) | ||
} | ||
} | ||
|
||
func FuzzHashSliceToBytes(f *testing.F) { | ||
for i := 0; i < 100; i++ { | ||
f.Add(utils.RandomBytes(i)) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, b []byte) { | ||
testBytesToHashSlice(t, b) | ||
}) | ||
} | ||
|
||
func testPackPredicate(t testing.TB, b []byte) { | ||
packedPredicate := PackPredicate(b) | ||
unpackedPredicated, err := UnpackPredicate(packedPredicate) | ||
require.NoError(t, err) | ||
require.Equal(t, b, unpackedPredicated) | ||
} | ||
|
||
func FuzzPackPredicate(f *testing.F) { | ||
for i := 0; i < 100; i++ { | ||
f.Add(utils.RandomBytes(i)) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, b []byte) { | ||
testPackPredicate(t, b) | ||
}) | ||
} | ||
|
||
func FuzzUnpackInvalidPredicate(f *testing.F) { | ||
// Seed the fuzzer with non-zero length padding of zeroes or non-zeroes. | ||
for i := 1; i < 100; i++ { | ||
f.Add(utils.RandomBytes(i)) | ||
f.Add(make([]byte, i)) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, b []byte) { | ||
// Ensure that adding the invalid padding to any length correctly packed predicate | ||
// results in failing to unpack it. | ||
for _, l := range []int{0, 1, 31, 32, 33, 63, 64, 65} { | ||
validPredicate := PackPredicate(utils.RandomBytes(l)) | ||
invalidPredicate := append(validPredicate, b...) | ||
_, err := UnpackPredicate(invalidPredicate) | ||
require.Error(t, err) | ||
} | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use
common.HashLength
instead of using hardcoded32
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also in the rest of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is copying the style used here: https://github.com/ethereum/go-ethereum/blob/master/accounts/abi/pack.go#L33. I think it's clearer to use 32 in this case since we're using 31 as well, so we can either have the current code or:
always use constant and relative constant
use 31 but the constant for 32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if these are supposed to be same with hash length then I'd say we should (and geth should) use constant. but no pressure.