Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Modified extension rows #1598

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions mpt-witness-generator/witness/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ func prepareAccountLeafNode(addr common.Address, addrh []byte, leafS, leafC, nei
values[AccountDrifted] = keyDrifted
values[AccountWrong] = wrongValue

for i := 0; i < 6; i++ {
row := make([]byte, rowLen)
values = append(values, row)
}

leaf := AccountNode{
Address: addr,
Key: addrh,
Expand Down Expand Up @@ -538,6 +543,11 @@ func prepareStorageLeafNode(leafS, leafC, neighbourNode []byte, storage_key comm
}
rows = append(rows, nonExistingStorageRow)

for i := 0; i < 6; i++ {
row := make([]byte, rowLen)
rows = append(rows, row)
}

leaf := StorageNode{
Address: storage_key,
Key: trie.HexToKeybytes(key),
Expand Down
24 changes: 15 additions & 9 deletions mpt-witness-generator/witness/modified_extension_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/privacy-scaling-explorations/mpt-witness-generator/trie"
)

// prepareModExtensionNode adds rows for a modified extension node before and after modification.
// equipLeafWithModExtensionNode adds rows for a modified extension node before and after modification.
// These rows are added only when an existing extension node gets shortened or elongated (in terms
// of the extension node nibbles) because of another extension node being added or deleted.
// The rows added are somewhat exceptional as otherwise they do not appear.
func prepareModExtensionNode(statedb *state.StateDB, addr common.Address, rows *[][]byte, proof1, proof2,
func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr common.Address, rows *[][]byte, proof1, proof2,
extNibblesS, extNibblesC [][]byte,
key, neighbourNode []byte,
keyIndex, extensionNodeInd, numberOfNibbles int,
Expand Down Expand Up @@ -175,9 +175,6 @@ func prepareModExtensionNode(statedb *state.StateDB, addr common.Address, rows *
*/

listRlpBytes := [2][]byte{extListRlpBytesS, extListRlpBytesC}
modExtensionNode := ModExtensionNode{
ListRlpBytes: listRlpBytes,
}

var values [][]byte
extValuesS = append(extValuesS[:1], extValuesS[2:]...)
Expand All @@ -189,9 +186,18 @@ func prepareModExtensionNode(statedb *state.StateDB, addr common.Address, rows *
keccakData = append(keccakData, longExtNode)
keccakData = append(keccakData, shortExtNode)

return Node{
ModExtension: &modExtensionNode,
Values: values,
KeccakData: keccakData,
if leafNode.Account == nil {
leafNode.Storage.ModListRlpBytes = listRlpBytes
} else {
leafNode.Account.ModListRlpBytes = listRlpBytes
}

l := len(leafNode.Values)
for i := 0; i < 6; i++ {
leafNode.Values[l-6+i] = values[i]
}

leafNode.KeccakData = append(leafNode.KeccakData, keccakData...)

return leafNode
}
27 changes: 11 additions & 16 deletions mpt-witness-generator/witness/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@ type ExtensionBranchNode struct {
Branch BranchNode `json:"branch"`
}

type ModExtensionNode struct {
ListRlpBytes [2][]byte
}

func (n *ModExtensionNode) MarshalJSON() ([]byte, error) {
listRlpBytes1 := base64ToString(n.ListRlpBytes[0])
listRlpBytes2 := base64ToString(n.ListRlpBytes[1])
jsonResult := fmt.Sprintf(`{"list_rlp_bytes":[%s,%s]}`, listRlpBytes1, listRlpBytes2)
return []byte(jsonResult), nil
}

type AccountNode struct {
Address common.Address
Key []byte
Expand All @@ -89,6 +78,7 @@ type AccountNode struct {
DriftedRlpBytes []byte
WrongRlpBytes []byte
IsModExtension [2]bool `json:"is_mod_extension"`
ModListRlpBytes [2][]byte
}

func (n *AccountNode) MarshalJSON() ([]byte, error) {
Expand All @@ -102,9 +92,11 @@ func (n *AccountNode) MarshalJSON() ([]byte, error) {
valueListRlpBytes2 := base64ToString(n.ValueListRlpBytes[1])
driftedRlpBytes := base64ToString(n.DriftedRlpBytes)
wrongRlpBytes := base64ToString(n.WrongRlpBytes)
jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "value_list_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t]}`,
modListRlpBytes1 := base64ToString(n.ModListRlpBytes[0])
modListRlpBytes2 := base64ToString(n.ModListRlpBytes[1])
jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "value_list_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t], "mod_list_rlp_bytes":[%s,%s]}`,
address, key, listRlpBytes1, listRlpBytes2, valueRlpBytes1, valueRlpBytes2, valueListRlpBytes1, valueListRlpBytes2,
driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1])
driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1], modListRlpBytes1, modListRlpBytes2)
return []byte(jsonResult), nil
}

Expand All @@ -116,6 +108,7 @@ type StorageNode struct {
DriftedRlpBytes []byte `json:"drifted_rlp_bytes"`
WrongRlpBytes []byte `json:"wrong_rlp_bytes"`
IsModExtension [2]bool `json:"is_mod_extension"`
ModListRlpBytes [2][]byte
}

func (n *StorageNode) MarshalJSON() ([]byte, error) {
Expand All @@ -127,8 +120,11 @@ func (n *StorageNode) MarshalJSON() ([]byte, error) {
valueRlpBytes2 := base64ToString(n.ValueRlpBytes[1])
driftedRlpBytes := base64ToString(n.DriftedRlpBytes)
wrongRlpBytes := base64ToString(n.WrongRlpBytes)
jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t]}`,
address, key, listRlpBytes1, listRlpBytes2, valueRlpBytes1, valueRlpBytes2, driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1])
modListRlpBytes1 := base64ToString(n.ModListRlpBytes[0])
modListRlpBytes2 := base64ToString(n.ModListRlpBytes[1])
jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t], "mod_list_rlp_bytes":[%s,%s]}`,
address, key, listRlpBytes1, listRlpBytes2, valueRlpBytes1, valueRlpBytes2, driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1],
modListRlpBytes1, modListRlpBytes2)
return []byte(jsonResult), nil
}

Expand All @@ -153,7 +149,6 @@ type Node struct {
ExtensionBranch *ExtensionBranchNode `json:"extension_branch"`
Account *AccountNode `json:"account"`
Storage *StorageNode `json:"storage"`
ModExtension *ModExtensionNode `json:"mod_extension"`
Values JSONableValues `json:"values"`
KeccakData JSONableValues `json:"keccak_data"`
}
Expand Down
20 changes: 7 additions & 13 deletions mpt-witness-generator/witness/prepare_witness.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package witness

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -445,11 +444,11 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []

nodes = append(nodes, bNode)

var leafNode Node
if isAccountProof {
// Add account leaf after branch placeholder:
var node Node
if !isModifiedExtNode {
node = prepareAccountLeafNode(addr, addrh, proof1[len1-1], proof2[len2-1], neighbourNode, key, false, false, false)
leafNode = prepareAccountLeafNode(addr, addrh, proof1[len1-1], proof2[len2-1], neighbourNode, key, false, false, false)
} else {
isSModExtension := false
isCModExtension := false
Expand All @@ -458,14 +457,12 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
} else {
isCModExtension = true
}
node = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension)
leafNode = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension)
}
nodes = append(nodes, node)
} else {
// Add storage leaf after branch placeholder
var node Node
if !isModifiedExtNode {
node = prepareStorageLeafNode(proof1[len1-1], proof2[len2-1], neighbourNode, storage_key, key, nonExistingStorageProof, false, false, false, false)
leafNode = prepareStorageLeafNode(proof1[len1-1], proof2[len2-1], neighbourNode, storage_key, key, nonExistingStorageProof, false, false, false, false)
} else {
isSModExtension := false
isCModExtension := false
Expand All @@ -474,22 +471,19 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
} else {
isCModExtension = true
}
node = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension)
leafNode = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension)
}
nodes = append(nodes, node)
}

// When a proof element is a modified extension node (new extension node appears at the position
// of the existing extension node), additional rows are added (extension node before and after
// modification).
if isModifiedExtNode {
// TODO
modExtensionNode := prepareModExtensionNode(statedb, addr, &rows, proof1, proof2, extNibblesS, extNibblesC, key, neighbourNode,
leafNode = equipLeafWithModExtensionNode(statedb, leafNode, addr, &rows, proof1, proof2, extNibblesS, extNibblesC, key, neighbourNode,
keyIndex, extensionNodeInd, numberOfNibbles, additionalBranch,
isAccountProof, nonExistingAccountProof, isShorterProofLastLeaf, branchC16, branchC1, &toBeHashed)
// node = append(nodes, modExtensionNode)
fmt.Println(modExtensionNode)
}
nodes = append(nodes, leafNode)
} else {
node := prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, false, false)
nodes = append(nodes, node)
Expand Down