Commit caed6a3d authored by Andreas Bigger's avatar Andreas Bigger

Preimage alphabet trace provider method

parent a8e6061d
...@@ -18,27 +18,35 @@ type AlphabetProvider struct { ...@@ -18,27 +18,35 @@ type AlphabetProvider struct {
func NewAlphabetProvider(state string, depth uint64) *AlphabetProvider { func NewAlphabetProvider(state string, depth uint64) *AlphabetProvider {
return &AlphabetProvider{ return &AlphabetProvider{
state: strings.Split(state, ""), state: strings.Split(state, ""),
maxLen: (1 << depth), maxLen: uint64(1 << depth),
} }
} }
// Get returns the claim value at the given index in the trace. // GetPreimage returns the preimage for the given hash.
func (ap *AlphabetProvider) Get(i uint64) (common.Hash, error) { func (ap *AlphabetProvider) GetPreimage(i uint64) ([]byte, error) {
// The index cannot be larger than the maximum index as computed by the depth. // The index cannot be larger than the maximum index as computed by the depth.
if i >= ap.maxLen { if i >= ap.maxLen {
return common.Hash{}, ErrIndexTooLarge return []byte{}, ErrIndexTooLarge
} }
// We extend the deepest hash to the maximum depth if the trace is not expansive. // We extend the deepest hash to the maximum depth if the trace is not expansive.
if i >= uint64(len(ap.state)) { if i >= uint64(len(ap.state)) {
return ap.Get(uint64(len(ap.state)) - 1) return ap.GetPreimage(uint64(len(ap.state)) - 1)
}
return buildAlphabetClaimBytes(i, ap.state[i]), nil
}
// Get returns the claim value at the given index in the trace.
func (ap *AlphabetProvider) Get(i uint64) (common.Hash, error) {
claimBytes, err := ap.GetPreimage(i)
if err != nil {
return common.Hash{}, err
} }
return ap.ComputeAlphabetClaim(i), nil return common.BytesToHash(claimBytes), nil
} }
// ComputeAlphabetClaim computes the claim for the given index in the trace. // buildAlphabetClaimBytes constructs the claim bytes for the index and state item.
func (ap *AlphabetProvider) ComputeAlphabetClaim(i uint64) common.Hash { func buildAlphabetClaimBytes(i uint64, letter string) []byte {
concatenated := append(IndexToBytes(i), []byte(ap.state[i])...) return append(IndexToBytes(i), []byte(letter)...)
return common.BytesToHash(concatenated)
} }
// IndexToBytes converts an index to a byte slice big endian // IndexToBytes converts an index to a byte slice big endian
......
...@@ -50,17 +50,26 @@ func FuzzIndexToBytes(f *testing.F) { ...@@ -50,17 +50,26 @@ func FuzzIndexToBytes(f *testing.F) {
}) })
} }
// TestComputeAlphabetClaim tests the ComputeAlphabetClaim function. // TestGetPreimage_Succeeds tests the GetPreimage function
func TestComputeAlphabetClaim(t *testing.T) { // returns the correct pre-image for a index.
func TestGetPreimage_Succeeds(t *testing.T) {
ap := NewAlphabetProvider("abc", 2) ap := NewAlphabetProvider("abc", 2)
claim := ap.ComputeAlphabetClaim(0) expected := append(IndexToBytes(uint64(0)), []byte("a")...)
concatenated := append(IndexToBytes(0), []byte("a")...) retrieved, err := ap.GetPreimage(uint64(0))
expected := common.BytesToHash(concatenated) require.NoError(t, err)
require.Equal(t, expected, claim) require.Equal(t, expected, retrieved)
}
// TestGetPreimage_TooLargeIndex_Fails tests the GetPreimage
// function errors if the index is too large.
func TestGetPreimage_TooLargeIndex_Fails(t *testing.T) {
ap := NewAlphabetProvider("abc", 2)
_, err := ap.GetPreimage(4)
require.ErrorIs(t, err, ErrIndexTooLarge)
} }
// TestGet tests the Get function. // TestGet_Succeeds tests the Get function.
func TestGet(t *testing.T) { func TestGet_Succeeds(t *testing.T) {
ap := NewAlphabetProvider("abc", 2) ap := NewAlphabetProvider("abc", 2)
claim, err := ap.Get(0) claim, err := ap.Get(0)
require.NoError(t, err) require.NoError(t, err)
......
...@@ -17,6 +17,7 @@ var ( ...@@ -17,6 +17,7 @@ var (
// The [AlphabetProvider] is a minimal implementation of this interface. // The [AlphabetProvider] is a minimal implementation of this interface.
type TraceProvider interface { type TraceProvider interface {
Get(i uint64) (common.Hash, error) Get(i uint64) (common.Hash, error)
GetPreimage(i uint64) ([]byte, error)
} }
// ClaimData is the core of a claim. It must be unique inside a specific game. // ClaimData is the core of a claim. It must be unique inside a specific game.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment