package merkle import ( "encoding/hex" "fmt" "testing" ) func TestRootAndProof(t *testing.T) { inputs := [][]byte{HashLeaf([]byte("a")), HashLeaf([]byte("b")), HashLeaf([]byte("c"))} root := Root(inputs) if len(root) == 0 { t.Fatalf("expected root") } for i := range inputs { proof := BuildProof(inputs, i) if !VerifyProof(inputs[i], proof, root) { t.Fatalf("proof verification failed for index %d", i) } } } func TestRootEmpty(t *testing.T) { r := Root(nil) if got := hex.EncodeToString(r); got != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { t.Fatalf("unexpected empty root: %s", got) } } func TestAccumulatorRootMatchesRoot(t *testing.T) { for n := 1; n <= 128; n++ { leaves := make([][]byte, 0, n) acc := NewAccumulator() for i := 0; i < n; i++ { leaf := HashLeaf([]byte(fmt.Sprintf("leaf-%d", i))) leaves = append(leaves, leaf) acc.AddLeafHash(leaf) } want := hex.EncodeToString(Root(leaves)) got := hex.EncodeToString(acc.RootDuplicateLast()) if got != want { t.Fatalf("root mismatch n=%d got=%s want=%s", n, got, want) } } }