Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions auth/tree/child_cell_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tree

import (
"bytes"
"testing"

"github.com/dewebprotocol/malt-core/maltcid"
)

func TestChildCellInheritsTreeContext(t *testing.T) {
for _, layout := range []maltcid.Layout{maltcid.Map, maltcid.List} {
for _, profileID := range []maltcid.ProfileID{maltcid.KZG4096, maltcid.IPA256} {
profile, err := maltcid.Profile(profileID)
if err != nil {
t.Fatal(err)
}
raw := bytes.Repeat([]byte{0xab}, profile.CommitmentSize)
cell := childCell(raw)
if !bytes.Equal(cell, append([]byte{0x02}, raw...)) {
t.Fatalf("unexpected child cell for profile %d: %x", profileID, cell)
}
parent := maltcid.NodeRef{Layout: layout, Profile: profileID}
child, err := parseChild(cell, parent)
if err != nil || child.Layout != layout || child.Profile != profileID || !bytes.Equal(child.Commitment, raw) {
t.Fatalf("child reference for layout %d, profile %d: %+v, %v", layout, profileID, child, err)
}
for _, invalid := range [][]byte{cell[:len(cell)-1], append(cell, 0), append([]byte{mapLeaf}, raw...)} {
if _, err := parseChild(invalid, parent); err == nil {
t.Fatalf("accepted malformed child cell for profile %d: %x", profileID, invalid)
}
}
}
}
}
14 changes: 7 additions & 7 deletions auth/tree/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TransferBindingProof(s *proofwire.Stream, d maltcid.RootDescriptor, query S
if last {
cell, err = terminalWitnessCell(s, d.Layout, query.Label, target, node.Cell)
} else {
cell, err = childWitnessCell(s, d, profile.CommitmentSize, node.Cell)
cell, err = childWitnessCell(s, profile.CommitmentSize, node.Cell)
}
if err != nil {
return err
Expand Down Expand Up @@ -137,20 +137,20 @@ func transferRootMetadata(s *proofwire.Stream, profile maltcid.ProfileID, node *
return err
}

func childWitnessCell(s *proofwire.Stream, d maltcid.RootDescriptor, width int, original []byte) (commitment.Cell, error) {
ref := maltcid.NodeRef{Layout: d.Layout, Profile: d.Profile}
func childWitnessCell(s *proofwire.Stream, width int, original []byte) (commitment.Cell, error) {
var raw []byte
if !s.Reading {
child, err := parseChild(original, ref)
var err error
raw, err = childPayload(original, width)
if err != nil {
return nil, err
}
ref = child
}
ref.Commitment = s.Fixed(ref.Commitment, width)
raw = s.Fixed(raw, width)
if s.Err != nil {
return nil, s.Err
}
return childCell(ref)
return childCell(raw), nil
}

func terminalWitnessCell(s *proofwire.Stream, layout maltcid.Layout, label []byte, target cid.Cid, original []byte) (commitment.Cell, error) {
Expand Down
25 changes: 15 additions & 10 deletions auth/tree/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,25 @@ const (
metadataCell byte = maltcid.MetadataCell
)

func childCell(ref maltcid.NodeRef) (commitment.Cell, error) {
data, err := ref.Bytes()
return append(commitment.Cell{childNode}, data...), err
func childCell(raw []byte) commitment.Cell {
return append(commitment.Cell{childNode}, raw...)
}
func parseChild(cell commitment.Cell, expected maltcid.NodeRef) (maltcid.NodeRef, error) {
if len(cell) < 2 || cell[0] != childNode {
return maltcid.NodeRef{}, errors.New("expected internal node reference")

func childPayload(cell commitment.Cell, width int) ([]byte, error) {
if len(cell) != 1+width || cell[0] != childNode {
return nil, errors.New("invalid child commitment cell")
}
ref, err := maltcid.ParseNodeRef(cell[1:])
return cell[1:], nil
}

func parseChild(cell commitment.Cell, parent maltcid.NodeRef) (maltcid.NodeRef, error) {
profile, err := maltcid.Profile(parent.Profile)
if err != nil {
return maltcid.NodeRef{}, err
}
if ref.Layout != expected.Layout || ref.Profile != expected.Profile {
return maltcid.NodeRef{}, errors.New("mixed layout or VC profile inside authentication tree")
raw, err := childPayload(cell, profile.CommitmentSize)
if err != nil {
return maltcid.NodeRef{}, err
}
return ref, nil
return maltcid.NodeRef{Layout: parent.Layout, Profile: parent.Profile, Commitment: bytes.Clone(raw)}, nil
}
5 changes: 1 addition & 4 deletions auth/tree/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ func (b *builder) listNode(items []binding, p listGeometry, offset uint64, meta
if err != nil {
return maltcid.NodeRef{}, err
}
cells[slot], err = childCell(ref)
if err != nil {
return maltcid.NodeRef{}, err
}
cells[slot] = childCell(ref.Commitment)
items = items[n:]
}
}
Expand Down
2 changes: 1 addition & 1 deletion auth/tree/list_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func storeListChild(b *builder, cells []commitment.Cell) (commitment.Cell, error
if err != nil {
return nil, err
}
return childCell(ref)
return childCell(ref.Commitment), nil
}
}
return nil, nil
Expand Down
5 changes: 1 addition & 4 deletions auth/tree/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func (b *builder) mapNode(items []binding, depth int) (maltcid.NodeRef, error) {
if err != nil {
return maltcid.NodeRef{}, err
}
cells[i], err = childCell(ref)
if err != nil {
return maltcid.NodeRef{}, err
}
cells[i] = childCell(ref.Commitment)
}
}
return b.commit(cells)
Expand Down
10 changes: 2 additions & 8 deletions auth/tree/map_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func (u *nodeEdit) mapNode(b *builder, ref maltcid.NodeRef, depth int, items []c
if err != nil {
return nil, err
}
cells[slot], err = childCell(child)
if err != nil {
return nil, err
}
cells[slot] = childCell(child.Commitment)
}
continue
}
Expand Down Expand Up @@ -107,10 +104,7 @@ func (u *nodeEdit) mapNode(b *builder, ref maltcid.NodeRef, depth int, items []c
if err != nil {
return nil, err
}
cells[slot], err = childCell(child)
if err != nil {
return nil, err
}
cells[slot] = childCell(child.Commitment)
}
}
return cells, nil
Expand Down
19 changes: 12 additions & 7 deletions auth/tree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,20 @@ func (u *nodeEdit) finish(ref maltcid.NodeRef) (cid.Cid, error) {
if err != nil {
return cid.Undef, err
}
key, err := ref.Bytes()
if err != nil {
return cid.Undef, err
}
if err := u.writePending(string(key)); err != nil {
if err := u.writePending(ref); err != nil {
return cid.Undef, err
}
return root, nil
}

// Write each reachable staged node after its children. Shrinking a List may
// promote a staged vector to root and discard its intermediate child encoding.
func (u *nodeEdit) writePending(key string) error {
func (u *nodeEdit) writePending(ref maltcid.NodeRef) error {
raw, err := ref.Bytes()
if err != nil {
return err
}
key := string(raw)
node, ok := u.pending[key]
if !ok {
return nil // an unchanged child remains in the base materialization
Expand All @@ -106,7 +107,11 @@ func (u *nodeEdit) writePending(key string) error {
delete(u.pending, key)
for _, cell := range node.cells {
if len(cell) > 0 && cell[0] == childNode {
if err := u.writePending(string(cell[1:])); err != nil {
child, err := parseChild(cell, ref)
if err != nil {
return err
}
if err := u.writePending(child); err != nil {
return err
}
}
Expand Down
Loading
Loading