Skip to content

Commit

Permalink
Apply leaf marker on all touched values (#3520)
Browse files Browse the repository at this point in the history
* Apply leaf marker to touched values

* Add test for leaf marker
  • Loading branch information
acolytec3 authored Jul 18, 2024
1 parent 3abbcd0 commit d7c2619
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/verkle/src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export const createCValues = (values: (Uint8Array | VerkleLeafNodeValue)[]) => {
break
}
// We add 16 trailing zeros to each value since all commitments are padded to an array of 32 byte values
// TODO: Determine whether we need to apply the leaf marker (i.e. set 129th bit) for all written values
// regardless of whether the value stored is zero or not
expandedValues[x * 2] = setLengthRight(val.slice(0, 16), 32)
// Apply leaf marker to all touched values (i.e. flip 129th bit)
if (retrievedValue !== VerkleLeafNodeValue.Untouched) expandedValues[x * 2][16] = 0x80
expandedValues[x * 2 + 1] = setLengthRight(val.slice(16), 32)
}
return expandedValues
Expand Down
19 changes: 16 additions & 3 deletions packages/verkle/test/leafNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { type VerkleCrypto, equalsBytes, randomBytes, setLengthLeft } from '@eth
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import { assert, beforeAll, describe, it } from 'vitest'

import { VerkleLeafNodeValue, VerkleNodeType, decodeNode, isLeafNode } from '../src/node/index.js'
import {
VerkleLeafNodeValue,
VerkleNodeType,
createCValues,
decodeNode,
isLeafNode,
} from '../src/node/index.js'
import { LeafNode } from '../src/node/leafNode.js'

describe('verkle node - leaf', () => {
Expand Down Expand Up @@ -61,11 +67,18 @@ describe('verkle node - leaf', () => {
assert.deepEqual(node.getValue(0), new Uint8Array(32))
})

it('should set the leaf marker on a touched value', async () => {
const key = randomBytes(32)
const node = await LeafNode.create(key.slice(0, 31), verkleCrypto)
node.setValue(0, VerkleLeafNodeValue.Deleted)
const c1Values = createCValues(node.values.slice(0, 128))
assert.equal(c1Values[0][16], 0x80)
})

it('should update a commitment when setting a value', async () => {
const key = randomBytes(32)
const stem = key.slice(0, 31)
const values = new Array<Uint8Array>(256).fill(new Uint8Array(32))
const node = await LeafNode.create(stem, verkleCrypto, values)
const node = await LeafNode.create(stem, verkleCrypto)
assert.deepEqual(node.c1, verkleCrypto.zeroCommitment)
node.setValue(0, randomBytes(32))
assert.notDeepEqual(node.c1, verkleCrypto.zeroCommitment)
Expand Down

0 comments on commit d7c2619

Please sign in to comment.