Skip to content

Add a fuzz test for Sign+Verify #164

@archseer

Description

@archseer

I attempted to do this, but in #163 but it's hard to filter out invalid reports. We'd need to run SplitFelt on the report and validate that each segment is a valid felt.

//go:build go1.18
// +build go1.18

package keys

import (
	cryptorand "crypto/rand"
	"encoding/hex"
	"testing"

	"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/ocr2/medianreport"
	ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2/types"
	"github.com/stretchr/testify/require"
)

// go test -tags=go1.18 -fuzz ./...
func FuzzStarkNetKeyring_Sign_Verify(f *testing.F) {
	kr1, err := NewOCR2Key(cryptorand.Reader)
	require.NoError(f, err)
	kr2, err := NewOCR2Key(cryptorand.Reader)
	require.NoError(f, err)

	digest := "00044e5d4f35325e464c87374b13c512f60e09d1236dd902f4bef4c9aedd7300"
	bytes, err := hex.DecodeString(digest)
	require.NoError(f, err)
	configDigest, err := ocrtypes.BytesToConfigDigest(bytes)
	require.NoError(f, err)

	ctx := ocrtypes.ReportContext{
		ReportTimestamp: ocrtypes.ReportTimestamp{
			ConfigDigest: configDigest,
			Epoch:        1,
			Round:        1,
		},
		ExtraHash: [32]byte{
			255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
			255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		},
	}
	report := ocrtypes.Report{
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 91, 43, 83, // observations_timestamp
		0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // observers
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, // len
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 150, 2, 210, // observation 1
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 150, 2, 211, // observation 2
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 224, 182, 179, 167, 100, 0, 0, // juels per fee coin (1 with 18 decimal places)
	}

	// Seed with valid report
	f.Add([]byte(report))
	f.Fuzz(func(t *testing.T, report []byte) {
		cdc := medianreport.ReportCodec{}
		// skip invalid reports
		if _, err := cdc.MedianFromReport(report); err != nil {
			t.Skip()
		}
		if _, err := ReportToSigData(ctx, report); err != nil {
			t.Skip()
		}
		sig, err := kr1.Sign(ctx, report)
		require.NoError(t, err)
		result := kr2.Verify(kr1.PublicKey(), ctx, report, sig)
		require.True(t, result)
	})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions