1
1
import { RLP } from '@ethereumjs/rlp'
2
- import { bigIntToBytes , bytesToBigInt , setLengthRight } from '@ethereumjs/util'
2
+ import { setLengthRight } from '@ethereumjs/util'
3
3
4
4
import { InternalNode } from './internalNode.js'
5
5
import { LeafNode } from './leafNode.js'
6
- import {
7
- VerkleLeafNodeValue ,
8
- type VerkleNode ,
9
- VerkleNodeType ,
10
- createDeletedLeafValue ,
11
- createUntouchedLeafValue ,
12
- } from './types.js'
6
+ import { VerkleLeafNodeValue , type VerkleNode , VerkleNodeType } from './types.js'
13
7
14
8
import type { VerkleCrypto } from '@ethereumjs/util'
15
9
@@ -37,19 +31,46 @@ export function isRawNode(node: Uint8Array | Uint8Array[]): node is Uint8Array[]
37
31
return Array . isArray ( node ) && ! ( node instanceof Uint8Array )
38
32
}
39
33
34
+ export function isLeafNode ( node : VerkleNode ) : node is LeafNode {
35
+ return node . type === VerkleNodeType . Leaf
36
+ }
37
+
38
+ export function isInternalNode ( node : VerkleNode ) : node is InternalNode {
39
+ return node . type === VerkleNodeType . Internal
40
+ }
41
+
42
+ export const createUntouchedLeafValue = ( ) => new Uint8Array ( 32 )
43
+
44
+ /**
45
+ * Generates a 32 byte array of zeroes and sets the 129th bit to 1, which the EIP
46
+ * refers to as the leaf marker to indicate a leaf value that has been touched previously
47
+ * and contains only zeroes
48
+ *
49
+ * Note: this value should only used in the commitment update process
50
+ *
51
+ * @returns a 32 byte array of zeroes with the 129th bit set to 1
52
+ */
53
+ export const createDeletedLeafValue = ( ) => {
54
+ const bytes = new Uint8Array ( 32 )
55
+ // Set the 129th bit to 1 directly by setting the 17th byte (index 16) to 0x80
56
+ bytes [ 16 ] = 0x80
57
+
58
+ return bytes
59
+ }
60
+
61
+ export const createDefaultLeafValues = ( ) => new Array ( 256 ) . fill ( 0 )
62
+
40
63
/***
41
- * Converts 128 32byte values of a leaf node into 16 byte values for generating a commitment for half of a
42
- * leaf node's values
43
- * @param values - an array of Uint8Arrays representing the first or second set of 128 values stored by the verkle trie leaf node
44
- * @param deletedValues - an array of booleans where a value of true at a given position indicates a value
45
- * that is being deleted - should always be false if generating C2 values
46
- * Returns an array of 256 16byte UintArrays with the leaf marker set for each value that is deleted
64
+ * Converts 128 32byte values of a leaf node into an array of 256 32 byte values representing
65
+ * the first and second 16 bytes of each value right padded with zeroes for generating a
66
+ * commitment for half of a leaf node's values
67
+ * @param values - an array of Uint8Arrays representing the first or second set of 128 values
68
+ * stored by the verkle trie leaf node
69
+ * Returns an array of 256 32 byte UintArrays with the leaf marker set for each value that is
70
+ * deleted
47
71
*/
48
- export const createCValues = (
49
- values : ( Uint8Array | VerkleLeafNodeValue ) [ ] ,
50
- deletedValues = new Array ( 128 ) . fill ( false )
51
- ) => {
52
- if ( values . length !== 128 || deletedValues . length !== 128 )
72
+ export const createCValues = ( values : ( Uint8Array | VerkleLeafNodeValue ) [ ] ) => {
73
+ if ( values . length !== 128 )
53
74
throw new Error ( `got wrong number of values, expected 128, got ${ values . length } ` )
54
75
const expandedValues : Uint8Array [ ] = new Array ( 256 )
55
76
for ( let x = 0 ; x < 128 ; x ++ ) {
@@ -59,30 +80,18 @@ export const createCValues = (
59
80
case VerkleLeafNodeValue . Untouched : // Leaf value that has never been written before
60
81
val = createUntouchedLeafValue ( )
61
82
break
62
- case VerkleLeafNodeValue . Deleted : // Leaf value that has been overwritten with zeros (i.e. a deleted value)
83
+ case VerkleLeafNodeValue . Deleted : // Leaf value that has been written with zeros (either zeroes or a deleted value)
63
84
val = createDeletedLeafValue ( )
64
-
65
85
break
66
86
default :
67
87
val = retrievedValue
68
88
break
69
89
}
70
90
// We add 16 trailing zeros to each value since all commitments are padded to an array of 32 byte values
71
- expandedValues [ x * 2 ] = setLengthRight (
72
- deletedValues [ x ] === true
73
- ? bigIntToBytes ( bytesToBigInt ( val . subarray ( 0 , 16 ) ) + BigInt ( 2 ** 128 ) )
74
- : val . slice ( 0 , 16 ) ,
75
- 32
76
- )
77
- // TODO: Decide if we should use slice or subarray here (i.e. do we need to copy these slices or not)
91
+ // TODO: Determine whether we need to apply the leaf marker (i.e. set 129th bit) for all written values
92
+ // regardless of whether the value stored is zero or not
93
+ expandedValues [ x * 2 ] = setLengthRight ( val . slice ( 0 , 16 ) , 32 )
78
94
expandedValues [ x * 2 + 1 ] = setLengthRight ( val . slice ( 16 ) , 32 )
79
95
}
80
96
return expandedValues
81
97
}
82
- export function isLeafNode ( node : VerkleNode ) : node is LeafNode {
83
- return node . type === VerkleNodeType . Leaf
84
- }
85
-
86
- export function isInternalNode ( node : VerkleNode ) : node is InternalNode {
87
- return node . type === VerkleNodeType . Internal
88
- }
0 commit comments