Skip to content

Commit

Permalink
move verkleCrypto to verkleNode constructor opts
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Jun 7, 2024
1 parent 1902ebc commit 541240c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
7 changes: 4 additions & 3 deletions packages/verkle/src/node/baseVerkleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import type { VerkleCrypto } from 'verkle-cryptography-wasm'
export abstract class BaseVerkleNode<T extends VerkleNodeType> implements VerkleNodeInterface {
public commitment: Uint8Array
public depth: number

protected verkleCrypto: VerkleCrypto
constructor(options: VerkleNodeOptions[T]) {
this.commitment = options.commitment
this.depth = options.depth
this.verkleCrypto = options.verkleCrypto
}

abstract commit(): Uint8Array

// Hash returns the field representation of the commitment.
hash(verkleCrypto: VerkleCrypto): Uint8Array {
return verkleCrypto.hashCommitment(this.commitment)
hash(): Uint8Array {
return this.verkleCrypto.hashCommitment(this.commitment)
}

abstract insert(key: Uint8Array, value: Uint8Array, nodeResolverFn: () => void): void
Expand Down
23 changes: 18 additions & 5 deletions packages/verkle/src/node/internalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export class InternalNode extends BaseVerkleNode<VerkleNodeType.Internal> {
// TODO: Update commitment as well
}

static fromRawNode(rawNode: Uint8Array[], depth: number): InternalNode {
static fromRawNode(
rawNode: Uint8Array[],
depth: number,
verkleCrypto: VerkleCrypto
): InternalNode {
const nodeType = rawNode[0][0]
if (nodeType !== VerkleNodeType.Internal) {
throw new Error('Invalid node type')
Expand All @@ -49,13 +53,14 @@ export class InternalNode extends BaseVerkleNode<VerkleNodeType.Internal> {
// TODO: Generate Point from rawNode value
const commitment = rawNode[rawNode.length - 1]

return new InternalNode({ commitment, depth })
return new InternalNode({ commitment, depth, verkleCrypto })
}

static create(depth: number): InternalNode {
static create(depth: number, verkleCrypto: VerkleCrypto): InternalNode {
const node = new InternalNode({
commitment: POINT_IDENTITY,
depth,
verkleCrypto,
})

return node
Expand Down Expand Up @@ -98,7 +103,7 @@ export class InternalNode extends BaseVerkleNode<VerkleNodeType.Internal> {
// on the next byte in both keys, a recursion into
// the moved leaf node can occur.
const nextByteInExistingKey = child.stem[this.depth + 1]
const newBranch = InternalNode.create(this.depth + 1)
const newBranch = InternalNode.create(this.depth + 1, this.verkleCrypto)
newBranch.cowChild(nextByteInExistingKey)
this.children[childIndex] = newBranch.commitment
newBranch.children[nextByteInExistingKey] = child
Expand Down Expand Up @@ -132,7 +137,15 @@ export class InternalNode extends BaseVerkleNode<VerkleNodeType.Internal> {
}
}

const leafNode = LeafNode.create(stem, values, this.depth + 1, leafCommitment, c1, c2)
const leafNode = LeafNode.create(
stem,
values,
this.depth + 1,
leafCommitment,
c1,
c2,
this.verkleCrypto
)

// TODO - Why is the leaf node set at depth + 2 instead of + 1)?
leafNode.setDepth(this.depth + 2)
Expand Down
18 changes: 14 additions & 4 deletions packages/verkle/src/node/leafNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseVerkleNode } from './baseVerkleNode.js'
import { NODE_WIDTH, VerkleNodeType } from './types.js'

import type { VerkleCrypto } from '../types.js'
import type { VerkleNodeOptions } from './types.js'

export class LeafNode extends BaseVerkleNode<VerkleNodeType.Leaf> {
Expand All @@ -25,12 +26,21 @@ export class LeafNode extends BaseVerkleNode<VerkleNodeType.Leaf> {
depth: number,
commitment: Uint8Array,
c1: Uint8Array,
c2: Uint8Array
c2: Uint8Array,
verkleCrypto: VerkleCrypto
): LeafNode {
return new LeafNode({ stem, values, depth, commitment, c1, c2 })
return new LeafNode({
stem,
values,
depth,
commitment,
c1,
c2,
verkleCrypto,
})
}

static fromRawNode(rawNode: Uint8Array[], depth: number): LeafNode {
static fromRawNode(rawNode: Uint8Array[], depth: number, verkleCrypto: VerkleCrypto): LeafNode {
const nodeType = rawNode[0][0]
if (nodeType !== VerkleNodeType.Leaf) {
throw new Error('Invalid node type')
Expand All @@ -48,7 +58,7 @@ export class LeafNode extends BaseVerkleNode<VerkleNodeType.Leaf> {
const c2 = rawNode[4]
const values = rawNode.slice(5, rawNode.length)

return new LeafNode({ depth, stem, values, c1, c2, commitment })
return new LeafNode({ depth, stem, values, c1, c2, commitment, verkleCrypto })
}
commit(): Uint8Array {
throw new Error('Not implemented')
Expand Down
1 change: 1 addition & 0 deletions packages/verkle/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface BaseVerkleNodeOptions {
// Value of the commitment
commitment: Uint8Array
depth: number
verkleCrypto: VerkleCrypto
}

interface VerkleInternalNodeOptions extends BaseVerkleNodeOptions {
Expand Down
20 changes: 14 additions & 6 deletions packages/verkle/src/verkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ export class VerkleTree {
const node =
rawNode[0][0] === VerkleNodeType.Leaf
? // TODO: Figure out how to determine depth from key
LeafNode.fromRawNode(rawNode, 1)
: InternalNode.fromRawNode(rawNode, 1)
LeafNode.fromRawNode(rawNode, 1, this.verkleCrypto)
: InternalNode.fromRawNode(rawNode, 1, this.verkleCrypto)

if (node instanceof LeafNode)
if (node instanceof LeafNode) {
Expand Down Expand Up @@ -259,8 +259,16 @@ export class VerkleTree {
new Uint8Array(32),
this.verkleCrypto.hashCommitment(c2)
)
leafNode = LeafNode.create(key.slice(0, 31), values, leafNode.length, commitment, c1, c2)
await this._db.put(leafNode.hash(this.verkleCrypto), leafNode.serialize())
leafNode = LeafNode.create(
key.slice(0, 31),
values,
leafNode.length,
commitment,
c1,
c2,
this.verkleCrypto
)
await this._db.put(leafNode.hash(), leafNode.serialize())
}

// Walk up the tree and update internal nodes
Expand All @@ -271,7 +279,7 @@ export class VerkleTree {
while (currentDepth > 0) {
const parentKey = currentKey.slice(0, -1)
const parentIndex = currentKey[currentKey.length - 1]
const parentNode = InternalNode.create(currentDepth)
const parentNode = InternalNode.create(currentDepth, this.verkleCrypto)
parentNode.children[parentIndex] = currentNode
await this._db.put(parentKey, parentNode.serialize())

Expand All @@ -280,7 +288,7 @@ export class VerkleTree {
currentDepth--
}

this._root = currentNode.hash(this.verkleCrypto)
this._root = currentNode.hash()
}

/**
Expand Down

0 comments on commit 541240c

Please sign in to comment.