Skip to content
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

pbss: serialized points in uncompressed form #370

Merged
merged 2 commits into from
Jun 30, 2023

Conversation

jsign
Copy link
Collaborator

@jsign jsign commented Jun 26, 2023

This PR serializes commitment points in uncompressed form.

It leverages a go-ipa tentative branch that supports uncompressed form serialization.

While doing this, I also realized some extra optimizations I'll refer to in the comments.

@jsign jsign force-pushed the jsign/pbssuncompressed branch from dc97f00 to 5bb850f Compare June 26, 2023 13:42
internalBitlistOffset = nodeTypeOffset + nodeTypeSize
internalNodeChildrenOffset = internalBitlistOffset + bitlistSize
internalBitlistOffset = nodeTypeOffset + nodeTypeSize
internalCommitmentOffset = internalBitlistOffset + bitlistSize
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was a pending rename @gballet referred in deleted line L858.

Comment on lines -70 to +71
// - Internal nodes: <nodeType><bitlist><children...>
// - Leaf nodes: <nodeType><stem><bitlist><c1comm><c2comm><children...>
// - Internal nodes: <nodeType><bitlist><commitment>
// - Leaf nodes: <nodeType><stem><bitlist><comm><c1comm><c2comm><children...>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this comments; this is mostly for the base branch than this PR.

comm := n.commitment.Bytes()
// XXX rename
copy(ret[internalNodeChildrenOffset:], comm[:])
// Note that children were already appended in ret through the children slice.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this comment since doesn't make sense now.

case *LeafNode:
pointsToCompress = append(pointsToCompress, n.commitment, n.c1, n.c2)
compressedPointsIdxs[n] = len(pointsToCompress) - 3
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gballet, I removed this line. We don't use the serializedPointsIdxs for LeafNodes.
This allowed me to reduce the preallocated size in L1493 a bit, and avoid a bit of work here.

func (n *InternalNode) serializeInternalWithCompressedCommitment(compressedPointsIdxs map[VerkleNode]int, compressedPoints [][32]byte) ([]byte, error) {
serialized := make([]byte, nodeTypeSize+bitlistSize+SerializedPointCompressedSize)
bitlist := serialized[internalBitlistOffset:internalNodeChildrenOffset]
func (n *InternalNode) serializeInternalWithUncompressedCommitment(pointsIdx map[VerkleNode]int, serializedPoints [][SerializedPointUncompressedSize]byte) ([]byte, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also tried to rename variables to avoid referring to "compressed" or "uncompressed"; and use a more general "serialized". (If it's compressed or uncompressed, should be irrelevant)

@jsign jsign force-pushed the jsign/pbssuncompressed branch from 5bb850f to 245360e Compare June 26, 2023 13:47
@jsign jsign marked this pull request as ready for review June 26, 2023 13:50
@jsign jsign requested a review from gballet June 26, 2023 13:50
Comment on lines -1596 to +1606
baseSize := nodeTypeSize + StemSize + bitlistSize + 3*SerializedPointCompressedSize
result := make([]byte, baseSize, baseSize+4*32) // Extra pre-allocated capacity for 4 values.
result := make([]byte, nodeTypeSize+StemSize+bitlistSize+3*SerializedPointUncompressedSize+len(children))
result[0] = leafRLPType
copy(result[leafSteamOffset:], n.stem[:StemSize])
copy(result[leafBitlistOffset:], bitlist[:])
copy(result[leafCommitmentOffset:], cBytes[:])
copy(result[leafC1CommitmentOffset:], c1Bytes[:])
copy(result[leafC2CommitmentOffset:], c2Bytes[:])
result = append(result, children...)
copy(result[leafChildrenOffset:], children)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's an improvement that will avoid allocations. I'm not sure why we had that speculative 4*32 when we actually know the exact size. Maybe just due to organic evolution we missed doing this.

This will avoid append to do allocations.

require github.com/crate-crypto/go-ipa v0.0.0-20230601170251-1830d0757c80
require github.com/crate-crypto/go-ipa v0.0.0-20230626131944-6a9b06cf26df
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the PR description, this is targeting a tentative go-ipa version that doesn't have a PR yet.
It exists mostly to assist go-verkle in this PR. We can figure out when it can make sense to make it official after the tests.

jsign added 2 commits June 29, 2023 09:41
…ed form

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
@jsign jsign force-pushed the jsign/pbssuncompressed branch from 245360e to e96a845 Compare June 29, 2023 12:41
Copy link
Member

@gballet gballet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

INFO [06-30|06:11:30.428] Imported new chain segment               number=4,816,909 hash=8393c6..67a483 blocks=30 txs=7357 mgas=233.386 elapsed=8.223s    mgasps=28.382 age=5y6mo4w  dirty=0.00B

Still a lot of work to do, but definitely a step in the right direction.

@gballet gballet merged commit 628f400 into no-reference-to-children Jun 30, 2023
jsign added a commit that referenced this pull request Jul 10, 2023
* config: rename serialized point size constant and value to uncompressed form

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* switch to uncompressed serialization

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
gballet added a commit that referenced this pull request Jul 10, 2023
…serialization (#354)

* don't rely on the commitment in HashedNode

change leaf serialization to add the leaf commitment there

fix a few tests

fix: incorrect size computation caused commitment overwrite

fix some unit tests

fix the batch unit test

* declare paths for serialization

* Add CommitmentBytes back in SerializedNode

* fixes from replaying

* fix: deepsource warnings

* tree: avoid memory allocs while collecting paths (#371)

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* pbss: serialized points in uncompressed form (#370)

* config: rename serialized point size constant and value to uncompressed form

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* switch to uncompressed serialization

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* tree: transform first layer to hashed nodes after serializing (#372)

* tree: after serializing transform first layer to hashed nodes

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* tree: add comment about temporary code

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* tree: make HashedNode non-pointer and fieldless (#373)

* tree: make HashedNode non-pointer and fieldless

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* lints

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* hashednode: add panic comment

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* hashednode: add label in toDot

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* rebase fix

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants