diff --git a/proof_ipa.go b/proof_ipa.go index 3f7af941..5b5e4d3b 100644 --- a/proof_ipa.go +++ b/proof_ipa.go @@ -30,7 +30,6 @@ import ( "errors" "fmt" "sort" - "unsafe" ipa "github.com/crate-crypto/go-ipa" "github.com/crate-crypto/go-ipa/common" @@ -83,17 +82,11 @@ type Proof struct { PostValues [][]byte } -type SuffixStateDiff struct { - Suffix byte `json:"suffix"` - CurrentValue *[32]byte `json:"currentValue"` - NewValue *[32]byte `json:"newValue"` -} - -type SuffixStateDiffs []SuffixStateDiff - type StemStateDiff struct { - Stem [StemSize]byte `json:"stem"` - SuffixDiffs SuffixStateDiffs `json:"suffixDiffs"` + Stem [StemSize]byte `json:"stem"` + Suffixes []byte `json:"suffixes"` + Current [][]byte `json:"current"` + New [][]byte `json:"new"` } type StateDiff []StemStateDiff @@ -102,16 +95,22 @@ func (sd StateDiff) Copy() StateDiff { ret := make(StateDiff, len(sd)) for i := range sd { copy(ret[i].Stem[:], sd[i].Stem[:]) - ret[i].SuffixDiffs = make([]SuffixStateDiff, len(sd[i].SuffixDiffs)) - for j := range sd[i].SuffixDiffs { - ret[i].SuffixDiffs[j].Suffix = sd[i].SuffixDiffs[j].Suffix - if sd[i].SuffixDiffs[j].CurrentValue != nil { - ret[i].SuffixDiffs[j].CurrentValue = &[32]byte{} - copy((*ret[i].SuffixDiffs[j].CurrentValue)[:], (*sd[i].SuffixDiffs[j].CurrentValue)[:]) + ret[i].Suffixes = make([]byte, len(sd[i].Suffixes)) + copy(ret[i].Suffixes, sd[i].Suffixes) + ret[i].Current = make([][]byte, len(sd[i].Current)) + for j := range sd[i].Current { + if len(sd[i].Current[j]) == 0 { + + } else { + copy(ret[i].Current[j], sd[i].Current[j]) } - if sd[i].SuffixDiffs[j].NewValue != nil { - ret[i].SuffixDiffs[j].NewValue = &[32]byte{} - copy((*ret[i].SuffixDiffs[j].NewValue)[:], (*sd[i].SuffixDiffs[j].NewValue)[:]) + } + ret[i].New = make([][]byte, len(sd[i].New)) + for j := range sd[i].New { + if len(sd[i].New[j]) == 0 { + + } else { + copy(ret[i].New[j], sd[i].New[j]) } } } @@ -257,32 +256,32 @@ func SerializeProof(proof *Proof) (*VerkleProof, StateDiff, error) { stemdiff = &statediff[len(statediff)-1] copy(stemdiff.Stem[:], stem) } - stemdiff.SuffixDiffs = append(stemdiff.SuffixDiffs, SuffixStateDiff{Suffix: key[StemSize]}) - newsd := &stemdiff.SuffixDiffs[len(stemdiff.SuffixDiffs)-1] + stemdiff.Suffixes = append(stemdiff.Suffixes, key[StemSize]) var valueLen = len(proof.PreValues[i]) switch valueLen { case 0: // null value + stemdiff.Current = append(stemdiff.Current, nil) case 32: - newsd.CurrentValue = (*[32]byte)(proof.PreValues[i]) + stemdiff.Current = append(stemdiff.Current, proof.PreValues[i]) default: var aligned [32]byte copy(aligned[:valueLen], proof.PreValues[i]) - newsd.CurrentValue = (*[32]byte)(unsafe.Pointer(&aligned[0])) + stemdiff.Current = append(stemdiff.Current, aligned[:]) } valueLen = len(proof.PostValues[i]) switch valueLen { case 0: // null value + stemdiff.Current = append(stemdiff.Current, nil) case 32: - newsd.NewValue = (*[32]byte)(proof.PostValues[i]) + stemdiff.Current = append(stemdiff.Current, proof.PostValues[i]) default: - // TODO remove usage of unsafe var aligned [32]byte copy(aligned[:valueLen], proof.PostValues[i]) - newsd.NewValue = (*[32]byte)(unsafe.Pointer(&aligned[0])) + stemdiff.Current = append(stemdiff.Current, aligned[:]) } } @@ -347,19 +346,19 @@ func DeserializeProof(vp *VerkleProof, statediff StateDiff) (*Proof, error) { // turn statediff into keys and values for _, stemdiff := range statediff { - for _, suffixdiff := range stemdiff.SuffixDiffs { + for i, suffix := range stemdiff.Suffixes { var k [32]byte copy(k[:StemSize], stemdiff.Stem[:]) - k[StemSize] = suffixdiff.Suffix + k[StemSize] = suffix keys = append(keys, k[:]) - if suffixdiff.CurrentValue != nil { - prevalues = append(prevalues, suffixdiff.CurrentValue[:]) + if stemdiff.Current[i] != nil { + prevalues = append(prevalues, stemdiff.Current[i]) } else { prevalues = append(prevalues, nil) } - if suffixdiff.NewValue != nil { - postvalues = append(postvalues, suffixdiff.NewValue[:]) + if stemdiff.New != nil { + postvalues = append(postvalues, stemdiff.New[i]) } else { postvalues = append(postvalues, nil) } @@ -528,12 +527,12 @@ func PostStateTreeFromStateDiff(preroot VerkleNode, statediff StateDiff) (Verkle overwrites bool ) - for _, suffixdiff := range stemstatediff.SuffixDiffs { - if /* len(suffixdiff.NewValue) > 0 - this only works for a slice */ suffixdiff.NewValue != nil { + for i, suffix := range stemstatediff.Suffixes { + if /* len(suffixdiff.NewValue) > 0 - this only works for a slice */ stemstatediff.New[i] != nil { // if this value is non-nil, it means InsertValuesAtStem should be // called, otherwise, skip updating the tree. overwrites = true - values[suffixdiff.Suffix] = suffixdiff.NewValue[:] + values[suffix] = stemstatediff.New[i] } } diff --git a/proof_json.go b/proof_json.go index 7828697e..aa61b76d 100644 --- a/proof_json.go +++ b/proof_json.go @@ -183,32 +183,38 @@ func (vp *VerkleProof) UnmarshalJSON(data []byte) error { return nil } +// TODO use gencodec if that works type stemStateDiffMarshaller struct { - Stem string `json:"stem"` - SuffixDiffs SuffixStateDiffs `json:"suffixDiffs"` + Stem string `json:"stem"` + Suffixes string `json:"suffixes"` + Current []string `json:"current"` + New []string `json:"new"` } func (ssd StemStateDiff) MarshalJSON() ([]byte, error) { return json.Marshal(&stemStateDiffMarshaller{ - Stem: HexToPrefixedString(ssd.Stem[:]), - SuffixDiffs: ssd.SuffixDiffs, + Stem: HexToPrefixedString(ssd.Stem[:]), + Suffixes: HexToPrefixedString(ssd.Suffixes), + // Current: + // TODO implement if it makes sense. }) } func (ssd *StemStateDiff) UnmarshalJSON(data []byte) error { - var aux stemStateDiffMarshaller - if err := json.Unmarshal(data, &aux); err != nil { - return fmt.Errorf("stemdiff unmarshal error: %w", err) - } - - stem, err := PrefixedHexStringToBytes(aux.Stem) - if err != nil { - return fmt.Errorf("invalid hex string for stem: %w", err) - } - *ssd = StemStateDiff{ - SuffixDiffs: aux.SuffixDiffs, - } - copy(ssd.Stem[:], stem) + // var aux stemStateDiffMarshaller + // if err := json.Unmarshal(data, &aux); err != nil { + // return fmt.Errorf("stemdiff unmarshal error: %w", err) + // } + + // stem, err := PrefixedHexStringToBytes(aux.Stem) + // if err != nil { + // return fmt.Errorf("invalid hex string for stem: %w", err) + // } + // *ssd = StemStateDiff{ + // SuffixDiffs: aux.SuffixDiffs, + // } + // copy(ssd.Stem[:], stem) + // TODO implement if it makes sense return nil } @@ -217,58 +223,3 @@ type suffixStateDiffMarshaller struct { CurrentValue *string `json:"currentValue"` NewValue *string `json:"newValue"` } - -func (ssd SuffixStateDiff) MarshalJSON() ([]byte, error) { - var cvstr, nvstr *string - if ssd.CurrentValue != nil { - tempstr := HexToPrefixedString(ssd.CurrentValue[:]) - cvstr = &tempstr - } - if ssd.NewValue != nil { - tempstr := HexToPrefixedString(ssd.NewValue[:]) - nvstr = &tempstr - } - return json.Marshal(&suffixStateDiffMarshaller{ - Suffix: ssd.Suffix, - CurrentValue: cvstr, - NewValue: nvstr, - }) -} - -func (ssd *SuffixStateDiff) UnmarshalJSON(data []byte) error { - aux := &suffixStateDiffMarshaller{} - - if err := json.Unmarshal(data, &aux); err != nil { - return fmt.Errorf("suffix diff unmarshal error: %w", err) - } - - if aux.CurrentValue != nil && len(*aux.CurrentValue) != 64 && len(*aux.CurrentValue) != 0 && len(*aux.CurrentValue) != 66 { - return fmt.Errorf("invalid hex string for current value: %s", *aux.CurrentValue) - } - - *ssd = SuffixStateDiff{ - Suffix: aux.Suffix, - } - - if aux.CurrentValue != nil && len(*aux.CurrentValue) != 0 { - currentValueBytes, err := PrefixedHexStringToBytes(*aux.CurrentValue) - if err != nil { - return fmt.Errorf("error decoding hex string for current value: %v", err) - } - - ssd.CurrentValue = &[32]byte{} - copy(ssd.CurrentValue[:], currentValueBytes) - } - - if aux.NewValue != nil && len(*aux.NewValue) != 0 { - newValueBytes, err := PrefixedHexStringToBytes(*aux.NewValue) - if err != nil { - return fmt.Errorf("error decoding hex string for current value: %v", err) - } - - ssd.NewValue = &[32]byte{} - copy(ssd.NewValue[:], newValueBytes) - } - - return nil -} diff --git a/proof_test.go b/proof_test.go index 8486551e..9a4479d9 100644 --- a/proof_test.go +++ b/proof_test.go @@ -568,113 +568,113 @@ func TestProofOfAbsenceNoneMultipleStems(t *testing.T) { } } -func TestSuffixStateDiffJSONMarshalUn(t *testing.T) { - t.Parallel() - - ssd := SuffixStateDiff{ - Suffix: 0x41, - CurrentValue: &[32]byte{ - 0x10, 0x20, 0x30, 0x40, - 0x50, 0x60, 0x70, 0x80, - 0x90, 0xA0, 0xB0, 0xC0, - 0xD0, 0xE0, 0xF0, 0x00, - 0x11, 0x22, 0x33, 0x44, - 0x55, 0x66, 0x77, 0x88, - 0x99, 0xAA, 0xBB, 0xCC, - 0xDD, 0xEE, 0xFF, 0x00, - }, - } - - expectedJSON := `{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00","newValue":null}` - actualJSON, err := json.Marshal(ssd) - if err != nil { - t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err) - } - - if string(actualJSON) != expectedJSON { - t.Errorf("JSON output doesn't match expected value.\nExpected: %s\nActual: %s", expectedJSON, string(actualJSON)) - } - - var actualSSD SuffixStateDiff - err = json.Unmarshal(actualJSON, &actualSSD) - if err != nil { - t.Errorf("error unmarshalling JSON to SuffixStateDiff: %v", err) - } - - if !reflect.DeepEqual(actualSSD, ssd) { - t.Errorf("SuffixStateDiff doesn't match expected value.\nExpected: %+v\nActual: %+v", ssd, actualSSD) - } -} - -func TestStemStateDiffJSONMarshalUn(t *testing.T) { - t.Parallel() - - ssd := StemStateDiff{ - Stem: [StemSize]byte{10}, - SuffixDiffs: []SuffixStateDiff{{ - Suffix: 0x41, - CurrentValue: &[32]byte{ - 0x10, 0x20, 0x30, 0x40, - 0x50, 0x60, 0x70, 0x80, - 0x90, 0xA0, 0xB0, 0xC0, - 0xD0, 0xE0, 0xF0, 0x00, - 0x11, 0x22, 0x33, 0x44, - 0x55, 0x66, 0x77, 0x88, - 0x99, 0xAA, 0xBB, 0xCC, - 0xDD, 0xEE, 0xFF, 0x00, - }, - }}, - } - - expectedJSON := `{"stem":"0x0a000000000000000000000000000000000000000000000000000000000000","suffixDiffs":[{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00","newValue":null}]}` - actualJSON, err := json.Marshal(ssd) - if err != nil { - t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err) - } - - if string(actualJSON) != expectedJSON { - t.Errorf("JSON output doesn't match expected value.\nExpected: %s\nActual: %s", expectedJSON, string(actualJSON)) - } - - var actualSSD StemStateDiff - err = json.Unmarshal(actualJSON, &actualSSD) - if err != nil { - t.Errorf("error unmarshalling JSON to StemStateDiff: %v", err) - } - - if !reflect.DeepEqual(actualSSD, ssd) { - t.Errorf("SuffixStateDiff doesn't match expected value.\nExpected: %+v\nActual: %+v", ssd, actualSSD) - } -} - -func TestSuffixStateDiffJSONMarshalUnCurrentValueNil(t *testing.T) { - t.Parallel() - - ssd := SuffixStateDiff{ - Suffix: 0x41, - CurrentValue: nil, - } - - expectedJSON := `{"suffix":65,"currentValue":null,"newValue":null}` - actualJSON, err := json.Marshal(ssd) - if err != nil { - t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err) - } - - if string(actualJSON) != expectedJSON { - t.Errorf("JSON output doesn't match expected value.\nExpected: %s\nActual: %s", expectedJSON, string(actualJSON)) - } - - var actualSSD SuffixStateDiff - err = json.Unmarshal(actualJSON, &actualSSD) - if err != nil { - t.Errorf("error unmarshalling JSON to SuffixStateDiff: %v", err) - } - - if !reflect.DeepEqual(actualSSD, ssd) { - t.Errorf("SuffixStateDiff doesn't match expected value.\nExpected: %+v\nActual: %+v", ssd, actualSSD) - } -} +// func TestSuffixStateDiffJSONMarshalUn(t *testing.T) { +// t.Parallel() + +// ssd := SuffixStateDiff{ +// Suffix: 0x41, +// CurrentValue: &[32]byte{ +// 0x10, 0x20, 0x30, 0x40, +// 0x50, 0x60, 0x70, 0x80, +// 0x90, 0xA0, 0xB0, 0xC0, +// 0xD0, 0xE0, 0xF0, 0x00, +// 0x11, 0x22, 0x33, 0x44, +// 0x55, 0x66, 0x77, 0x88, +// 0x99, 0xAA, 0xBB, 0xCC, +// 0xDD, 0xEE, 0xFF, 0x00, +// }, +// } + +// expectedJSON := `{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00","newValue":null}` +// actualJSON, err := json.Marshal(ssd) +// if err != nil { +// t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err) +// } + +// if string(actualJSON) != expectedJSON { +// t.Errorf("JSON output doesn't match expected value.\nExpected: %s\nActual: %s", expectedJSON, string(actualJSON)) +// } + +// var actualSSD SuffixStateDiff +// err = json.Unmarshal(actualJSON, &actualSSD) +// if err != nil { +// t.Errorf("error unmarshalling JSON to SuffixStateDiff: %v", err) +// } + +// if !reflect.DeepEqual(actualSSD, ssd) { +// t.Errorf("SuffixStateDiff doesn't match expected value.\nExpected: %+v\nActual: %+v", ssd, actualSSD) +// } +// } + +// func TestStemStateDiffJSONMarshalUn(t *testing.T) { +// t.Parallel() + +// ssd := StemStateDiff{ +// Stem: [StemSize]byte{10}, +// SuffixDiffs: []SuffixStateDiff{{ +// Suffix: 0x41, +// CurrentValue: &[32]byte{ +// 0x10, 0x20, 0x30, 0x40, +// 0x50, 0x60, 0x70, 0x80, +// 0x90, 0xA0, 0xB0, 0xC0, +// 0xD0, 0xE0, 0xF0, 0x00, +// 0x11, 0x22, 0x33, 0x44, +// 0x55, 0x66, 0x77, 0x88, +// 0x99, 0xAA, 0xBB, 0xCC, +// 0xDD, 0xEE, 0xFF, 0x00, +// }, +// }}, +// } + +// expectedJSON := `{"stem":"0x0a000000000000000000000000000000000000000000000000000000000000","suffixDiffs":[{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00","newValue":null}]}` +// actualJSON, err := json.Marshal(ssd) +// if err != nil { +// t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err) +// } + +// if string(actualJSON) != expectedJSON { +// t.Errorf("JSON output doesn't match expected value.\nExpected: %s\nActual: %s", expectedJSON, string(actualJSON)) +// } + +// var actualSSD StemStateDiff +// err = json.Unmarshal(actualJSON, &actualSSD) +// if err != nil { +// t.Errorf("error unmarshalling JSON to StemStateDiff: %v", err) +// } + +// if !reflect.DeepEqual(actualSSD, ssd) { +// t.Errorf("SuffixStateDiff doesn't match expected value.\nExpected: %+v\nActual: %+v", ssd, actualSSD) +// } +// } + +// func TestSuffixStateDiffJSONMarshalUnCurrentValueNil(t *testing.T) { +// t.Parallel() + +// ssd := SuffixStateDiff{ +// Suffix: 0x41, +// CurrentValue: nil, +// } + +// expectedJSON := `{"suffix":65,"currentValue":null,"newValue":null}` +// actualJSON, err := json.Marshal(ssd) +// if err != nil { +// t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err) +// } + +// if string(actualJSON) != expectedJSON { +// t.Errorf("JSON output doesn't match expected value.\nExpected: %s\nActual: %s", expectedJSON, string(actualJSON)) +// } + +// var actualSSD SuffixStateDiff +// err = json.Unmarshal(actualJSON, &actualSSD) +// if err != nil { +// t.Errorf("error unmarshalling JSON to SuffixStateDiff: %v", err) +// } + +// if !reflect.DeepEqual(actualSSD, ssd) { +// t.Errorf("SuffixStateDiff doesn't match expected value.\nExpected: %+v\nActual: %+v", ssd, actualSSD) +// } +// } func TestIPAProofMarshalUnmarshalJSON(t *testing.T) { t.Parallel()