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

Optimize storage of default values in VerkleNode #3476

Merged
merged 24 commits into from
Jul 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
55d425b
Add optimized default value storage
acolytec3 Jun 28, 2024
03534b1
lint
acolytec3 Jun 28, 2024
c3a4ffb
lint
acolytec3 Jun 28, 2024
132c9e4
Check for null instead of equalsBytes
acolytec3 Jul 1, 2024
b0d1523
Fix check for empty child references [no ci]
acolytec3 Jul 1, 2024
0b679a3
Remove unused constant [no ci]
acolytec3 Jul 1, 2024
ce24136
Reorganize tests
acolytec3 Jul 1, 2024
369477e
Fix test
acolytec3 Jul 2, 2024
cc40c72
Merge branch 'master' into optimize-node-value-storage
gabrocheleau Jul 3, 2024
c175633
verkle: enum instead of numbers
gabrocheleau Jul 3, 2024
78e300f
verkle: use enum
gabrocheleau Jul 3, 2024
9c19dfd
verkle: more enum cases
gabrocheleau Jul 3, 2024
320a733
verkle: address todo, use enum and remove now unnecessary typecast
gabrocheleau Jul 3, 2024
ecf4222
Remove obsolete comments
acolytec3 Jul 3, 2024
702b732
verkle: replace bigint conversion with direct bytes manipulation
gabrocheleau Jul 3, 2024
df19cbc
Merge branch 'optimize-node-value-storage' of https://github.com/ethe…
gabrocheleau Jul 3, 2024
5be4cb1
Merge branch 'master' into optimize-node-value-storage
gabrocheleau Jul 3, 2024
c25a93a
verkle: update constant helpers
gabrocheleau Jul 3, 2024
c05e941
Merge branch 'optimize-node-value-storage' of https://github.com/ethe…
gabrocheleau Jul 3, 2024
9c5e3ac
Accept VerkleLeafNodeValue in setValue
acolytec3 Jul 3, 2024
bcfb711
Update test
acolytec3 Jul 3, 2024
275909f
address feedback
acolytec3 Jul 4, 2024
f967f99
Merge branch 'master' into optimize-node-value-storage
acolytec3 Jul 4, 2024
bb7d30c
Merge branch 'master' into optimize-node-value-storage
acolytec3 Jul 4, 2024
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
23 changes: 15 additions & 8 deletions packages/verkle/src/node/leafNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,23 @@ export class LeafNode extends BaseVerkleNode<VerkleNodeType.Leaf> {
}
}

// Set the value at the provided index from the values array and update the node commitments
// TODO: Decide whether we need a separate "deleteValue" function since it has special handling
// since we never actually delete a node in a verkle trie but overwrite instead
setValue(index: number, value: Uint8Array): void {
/**
* Set the value at the provided index from the values array and update the node commitments
* @param index the index of the specific leaf value to be updated
* @param value the value to insert into the leaf value at `index`
*/
setValue(index: number, value: Uint8Array | VerkleLeafNodeValue): void {
let val
// `val` is a bytes representation of `value` used to update the cCommitment
if (value instanceof Uint8Array) val = value
else
val = value === VerkleLeafNodeValue.Untouched ? new Uint8Array(32) : createDeletedLeafValue()
gabrocheleau marked this conversation as resolved.
Show resolved Hide resolved
// First we update c1 or c2 (depending on whether the index is < 128 or not)
// Generate the 16 byte values representing the 32 byte values in the half of the values array that
// contain the old value for the leaf node
const cValues =
index < 128 ? createCValues(this.values.slice(0, 128)) : createCValues(this.values.slice(128))
// The commitment index is the 2 * the suffix (i.e. the position of the value in the values array)
// The commitment index is 2 * the suffix (i.e. the position of the value in the values array)
// here because each 32 byte value in the leaf node is represented as two 16 byte values in the
// cValues array.
const commitmentIndex = index < 128 ? index * 2 : (index - 128) * 2
Expand All @@ -153,15 +160,15 @@ export class LeafNode extends BaseVerkleNode<VerkleNodeType.Leaf> {
commitmentIndex,
cValues[commitmentIndex],
// Right pad the value with zeroes since commitments require 32 byte scalars
setLengthRight(value.slice(0, 16), 32)
setLengthRight(val.slice(0, 16), 32)
)
// Update the commitment for the second 16 bytes of the value
cCommitment = this.verkleCrypto.updateCommitment(
cCommitment!,
commitmentIndex + 1,
cValues[commitmentIndex + 1],
// Right pad the value with zeroes since commitments require 32 byte scalars
setLengthRight(value.slice(16), 32)
setLengthRight(val.slice(16), 32)
)
// Update the cCommitment corresponding to the index
let oldCCommitment: Uint8Array | undefined
Expand All @@ -174,7 +181,7 @@ export class LeafNode extends BaseVerkleNode<VerkleNodeType.Leaf> {
}
// Set the new values in the values array
this.values[index] = value
// Update leaf node commitment
// Update leaf node commitment -- c1 if index is < 128 or c2 otherwise
const cIndex = index < 128 ? 2 : 3
this.commitment = this.verkleCrypto.updateCommitment(
this.commitment,
Expand Down
Loading