-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wip] SSZ-optimize proof (type 1) #441
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remember that a proof verifier must now check that |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove this empty case, and only leave the else. |
||
|
||
} else { | ||
copy(ret[i].Current[j], sd[i].Current[j]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a bug here? We never allocated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that in |
||
} | ||
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]) | ||
} | ||
Comment on lines
+110
to
114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto two comments above. |
||
} | ||
} | ||
|
@@ -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] | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, now makes sense. The PR description has a small mistake that you might want to fix. Delete the
SuffixDiffs SuffixStateDiffs json:"suffixDiffs"
line since this field is removed.