Commit caed6a3d authored by Andreas Bigger's avatar Andreas Bigger

Preimage alphabet trace provider method

parent a8e6061d
......@@ -18,27 +18,35 @@ type AlphabetProvider struct {
func NewAlphabetProvider(state string, depth uint64) *AlphabetProvider {
return &AlphabetProvider{
state: strings.Split(state, ""),
maxLen: (1 << depth),
maxLen: uint64(1 << depth),
}
}
// Get returns the claim value at the given index in the trace.
func (ap *AlphabetProvider) Get(i uint64) (common.Hash, error) {
// GetPreimage returns the preimage for the given hash.
func (ap *AlphabetProvider) GetPreimage(i uint64) ([]byte, error) {
// The index cannot be larger than the maximum index as computed by the depth.
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.
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.
func (ap *AlphabetProvider) ComputeAlphabetClaim(i uint64) common.Hash {
concatenated := append(IndexToBytes(i), []byte(ap.state[i])...)
return common.BytesToHash(concatenated)
// buildAlphabetClaimBytes constructs the claim bytes for the index and state item.
func buildAlphabetClaimBytes(i uint64, letter string) []byte {
return append(IndexToBytes(i), []byte(letter)...)
}
// IndexToBytes converts an index to a byte slice big endian
......
......@@ -50,17 +50,26 @@ func FuzzIndexToBytes(f *testing.F) {
})
}
// TestComputeAlphabetClaim tests the ComputeAlphabetClaim function.
func TestComputeAlphabetClaim(t *testing.T) {
// TestGetPreimage_Succeeds tests the GetPreimage function
// returns the correct pre-image for a index.
func TestGetPreimage_Succeeds(t *testing.T) {
ap := NewAlphabetProvider("abc", 2)
claim := ap.ComputeAlphabetClaim(0)
concatenated := append(IndexToBytes(0), []byte("a")...)
expected := common.BytesToHash(concatenated)
require.Equal(t, expected, claim)
expected := append(IndexToBytes(uint64(0)), []byte("a")...)
retrieved, err := ap.GetPreimage(uint64(0))
require.NoError(t, err)
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.
func TestGet(t *testing.T) {
// TestGet_Succeeds tests the Get function.
func TestGet_Succeeds(t *testing.T) {
ap := NewAlphabetProvider("abc", 2)
claim, err := ap.Get(0)
require.NoError(t, err)
......
......@@ -17,6 +17,7 @@ var (
// The [AlphabetProvider] is a minimal implementation of this interface.
type TraceProvider interface {
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.
......
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