prysm-pulse/encoding/ssz/htrutils.go
terence tsao d6a02833dc
Add HTR for txs (#10030)
* Add htr for txs

* gaz

* minimal

* test pack
2022-01-05 18:36:39 +00:00

172 lines
6.0 KiB
Go

package ssz
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
// Uint64Root computes the HashTreeRoot Merkleization of
// a simple uint64 value according to the Ethereum
// Simple Serialize specification.
func Uint64Root(val uint64) [32]byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, val)
root := bytesutil.ToBytes32(buf)
return root
}
// ForkRoot computes the HashTreeRoot Merkleization of
// a Fork struct value according to the Ethereum
// Simple Serialize specification.
func ForkRoot(fork *ethpb.Fork) ([32]byte, error) {
fieldRoots := make([][]byte, 3)
if fork != nil {
prevRoot := bytesutil.ToBytes32(fork.PreviousVersion)
fieldRoots[0] = prevRoot[:]
currRoot := bytesutil.ToBytes32(fork.CurrentVersion)
fieldRoots[1] = currRoot[:]
forkEpochBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(forkEpochBuf, uint64(fork.Epoch))
epochRoot := bytesutil.ToBytes32(forkEpochBuf)
fieldRoots[2] = epochRoot[:]
}
return BitwiseMerkleize(hash.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
// CheckpointRoot computes the HashTreeRoot Merkleization of
// a InitWithReset struct value according to the Ethereum
// Simple Serialize specification.
func CheckpointRoot(hasher HashFn, checkpoint *ethpb.Checkpoint) ([32]byte, error) {
fieldRoots := make([][]byte, 2)
if checkpoint != nil {
epochBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(epochBuf, uint64(checkpoint.Epoch))
epochRoot := bytesutil.ToBytes32(epochBuf)
fieldRoots[0] = epochRoot[:]
ckpRoot := bytesutil.ToBytes32(checkpoint.Root)
fieldRoots[1] = ckpRoot[:]
}
return BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
// ByteArrayRootWithLimit computes the HashTreeRoot Merkleization of
// a list of [32]byte roots according to the Ethereum Simple Serialize
// specification.
func ByteArrayRootWithLimit(roots [][]byte, limit uint64) ([32]byte, error) {
result, err := BitwiseMerkleize(hash.CustomSHA256Hasher(), roots, uint64(len(roots)), limit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute byte array merkleization")
}
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, uint64(len(roots))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal byte array length")
}
// We need to mix in the length of the slice.
output := make([]byte, 32)
copy(output, buf.Bytes())
mixedLen := MixInLength(result, output)
return mixedLen, nil
}
// SlashingsRoot computes the HashTreeRoot Merkleization of
// a list of uint64 slashing values according to the Ethereum
// Simple Serialize specification.
func SlashingsRoot(slashings []uint64) ([32]byte, error) {
slashingMarshaling := make([][]byte, fieldparams.SlashingsLength)
for i := 0; i < len(slashings) && i < len(slashingMarshaling); i++ {
slashBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(slashBuf, slashings[i])
slashingMarshaling[i] = slashBuf
}
slashingChunks, err := Pack(slashingMarshaling)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not pack slashings into chunks")
}
return BitwiseMerkleize(hash.CustomSHA256Hasher(), slashingChunks, uint64(len(slashingChunks)), uint64(len(slashingChunks)))
}
// TransactionsRoot computes the HTR for the Transactions' property of the ExecutionPayload
// The code was largely copy/pasted from the code generated to compute the HTR of the entire
// ExecutionPayload.
func TransactionsRoot(txs [][]byte) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
listMarshaling := make([][]byte, 0)
for i := 0; i < len(txs); i++ {
rt, err := transactionRoot(txs[i])
if err != nil {
return [32]byte{}, err
}
listMarshaling = append(listMarshaling, rt[:])
}
bytesRoot, err := BitwiseMerkleize(hasher, listMarshaling, uint64(len(listMarshaling)), fieldparams.MaxTxsPerPayloadLength)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(txs))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
}
func transactionRoot(tx []byte) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
chunkedRoots, err := PackChunks(tx)
if err != nil {
return [32]byte{}, err
}
maxLength := (fieldparams.MaxBytesPerTxLength + 31) / 32
bytesRoot, err := BitwiseMerkleize(hasher, chunkedRoots, uint64(len(chunkedRoots)), uint64(maxLength))
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(tx))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
}
// PackChunks a given byte array into chunks. It'll pad the last chunk with zero bytes if
// it does not have length bytes per chunk.
func PackChunks(bytes []byte) ([][]byte, error) {
numItems := len(bytes)
var chunks [][]byte
for i := 0; i < numItems; i += 32 {
j := i + 32
// We create our upper bound index of the chunk, if it is greater than numItems,
// we set it as numItems itself.
if j > numItems {
j = numItems
}
// We create chunks from the list of items based on the
// indices determined above.
chunks = append(chunks, bytes[i:j])
}
if len(chunks) == 0 {
return chunks, nil
}
// Right-pad the last chunk with zero bytes if it does not
// have length bytes.
lastChunk := chunks[len(chunks)-1]
for len(lastChunk) < 32 {
lastChunk = append(lastChunk, 0)
}
chunks[len(chunks)-1] = lastChunk
return chunks, nil
}