-
Notifications
You must be signed in to change notification settings - Fork 11
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
update stem generation implementation #64
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERSION_LEAF_KEY; | ||
|
||
import org.hyperledger.besu.ethereum.trie.verkle.hasher.Hasher; | ||
import org.hyperledger.besu.ethereum.trie.verkle.hasher.PedersenHasher; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
@@ -48,12 +49,17 @@ public class TrieKeyAdapter { | |
|
||
private final Hasher hasher; | ||
|
||
/** Creates a TrieKeyAdapter with the default Perdersen hasher. */ | ||
public TrieKeyAdapter() { | ||
this.hasher = new PedersenHasher(); | ||
} | ||
|
||
/** | ||
* Creates a TrieKeyAdapter with the provided hasher. | ||
* | ||
* @param hasher The hasher used for key generation. | ||
*/ | ||
public TrieKeyAdapter(Hasher hasher) { | ||
public TrieKeyAdapter(final Hasher hasher) { | ||
this.hasher = hasher; | ||
} | ||
|
||
|
@@ -73,51 +79,63 @@ public Hasher getHasher() { | |
* @param storageKey The storage key. | ||
* @return The generated storage key. | ||
*/ | ||
public Bytes32 storageKey(Bytes address, Bytes32 storageKey) { | ||
final UInt256 pos = locateStorageKeyOffset(storageKey); | ||
final Bytes32 base = hasher.trieKeyHash(address, pos); | ||
final UInt256 suffix = locateStorageKeySuffix(storageKey); | ||
return swapLastByte(base, suffix); | ||
public Bytes32 storageKey(final Bytes address, final Bytes32 storageKey) { | ||
final Bytes stem = getStorageStem(address, storageKey); | ||
final UInt256 suffix = getStorageKeySuffix(storageKey); | ||
return swapLastByte(stem, suffix); | ||
} | ||
|
||
public UInt256 locateStorageKeyOffset(Bytes32 storageKey) { | ||
UInt256 index = UInt256.fromBytes(storageKey); | ||
if (index.compareTo(HEADER_STORAGE_SIZE) < 0) { | ||
return index.add(HEADER_STORAGE_OFFSET).divide(VERKLE_NODE_WIDTH); | ||
public UInt256 getStorageKeyTrieIndex(final Bytes32 storageKey) { | ||
final UInt256 uintStorageKey = UInt256.fromBytes(storageKey); | ||
if (uintStorageKey.compareTo(HEADER_STORAGE_SIZE) < 0) { | ||
return uintStorageKey.add(HEADER_STORAGE_OFFSET).divide(VERKLE_NODE_WIDTH); | ||
} else { | ||
// We divide by VerkleNodeWidthLog2 to make space and prevent any potential overflow | ||
// Then, we increment, a step that is safeguarded against overflow. | ||
return index | ||
return uintStorageKey | ||
.shiftRight(VERKLE_NODE_WIDTH_LOG2.intValue()) | ||
.add(MAIN_STORAGE_OFFSET_SHIFT_LEFT_VERKLE_NODE_WIDTH); | ||
} | ||
} | ||
|
||
public UInt256 locateStorageKeySuffix(Bytes32 storageKey) { | ||
UInt256 index = UInt256.fromBytes(storageKey); | ||
if (index.compareTo(HEADER_STORAGE_SIZE) < 0) { | ||
final UInt256 mod = index.add(HEADER_STORAGE_OFFSET).mod(VERKLE_NODE_WIDTH); | ||
public UInt256 getStorageKeySuffix(final Bytes32 storageKey) { | ||
final UInt256 uintStorageKey = UInt256.fromBytes(storageKey); | ||
if (uintStorageKey.compareTo(HEADER_STORAGE_SIZE) < 0) { | ||
final UInt256 mod = uintStorageKey.add(HEADER_STORAGE_OFFSET).mod(VERKLE_NODE_WIDTH); | ||
return UInt256.fromBytes(mod.slice(mod.size() - 1)); | ||
} else { | ||
return UInt256.fromBytes(storageKey.slice(Bytes32.SIZE - 1)); | ||
} | ||
} | ||
|
||
public Bytes getStorageStem(final Bytes address, final Bytes32 storageKey) { | ||
final UInt256 trieIndex = getStorageKeyTrieIndex(storageKey); | ||
return hasher.computeStem(address, trieIndex); | ||
} | ||
|
||
/** | ||
* Generates a code chunk key for a given address and chunkId. | ||
* | ||
* @param address The address. | ||
* @param chunkId The chunk ID. | ||
* @return The generated code chunk key. | ||
*/ | ||
public Bytes32 codeChunkKey(Bytes address, UInt256 chunkId) { | ||
UInt256 pos = locateCodeChunkKeyOffset(chunkId); | ||
Bytes32 base = hasher.trieKeyHash(address, pos.divide(VERKLE_NODE_WIDTH)); | ||
return swapLastByte(base, pos.mod(VERKLE_NODE_WIDTH)); | ||
public Bytes32 codeChunkKey(final Bytes address, final UInt256 chunkId) { | ||
final Bytes stem = getCodeChunkStem(address, chunkId); | ||
return swapLastByte(stem, getCodeChunkKeySuffix(chunkId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine the same as above here applies |
||
} | ||
|
||
public UInt256 getCodeChunkKeyTrieIndex(final Bytes32 chunkId) { | ||
return CODE_OFFSET.add(UInt256.fromBytes(chunkId)).divide(VERKLE_NODE_WIDTH); | ||
} | ||
|
||
public UInt256 getCodeChunkKeySuffix(final Bytes32 chunkId) { | ||
return CODE_OFFSET.add(UInt256.fromBytes(chunkId)).mod(VERKLE_NODE_WIDTH); | ||
} | ||
|
||
public UInt256 locateCodeChunkKeyOffset(Bytes32 chunkId) { | ||
return CODE_OFFSET.add(UInt256.fromBytes(chunkId)); | ||
public Bytes getCodeChunkStem(final Bytes address, final UInt256 chunkId) { | ||
final UInt256 trieIndex = getCodeChunkKeyTrieIndex(chunkId); | ||
return hasher.computeStem(address, trieIndex); | ||
} | ||
|
||
/** | ||
|
@@ -127,9 +145,13 @@ public UInt256 locateCodeChunkKeyOffset(Bytes32 chunkId) { | |
* @param leafKey The leaf key. | ||
* @return The generated header key. | ||
*/ | ||
Bytes32 headerKey(Bytes address, UInt256 leafKey) { | ||
Bytes32 base = hasher.trieKeyHash(address, UInt256.valueOf(0).toBytes()); | ||
return swapLastByte(base, leafKey); | ||
public Bytes32 headerKey(final Bytes address, final UInt256 leafKey) { | ||
final Bytes stem = getHeaderStem(address); | ||
return swapLastByte(stem, leafKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
} | ||
|
||
public Bytes getHeaderStem(final Bytes address) { | ||
return hasher.computeStem(address, UInt256.valueOf(0).toBytes()); | ||
} | ||
|
||
/** | ||
|
@@ -139,9 +161,9 @@ Bytes32 headerKey(Bytes address, UInt256 leafKey) { | |
* @param subIndex The subIndex. | ||
* @return The modified key. | ||
*/ | ||
public Bytes32 swapLastByte(Bytes32 base, Bytes subIndex) { | ||
public Bytes32 swapLastByte(final Bytes base, final Bytes subIndex) { | ||
final Bytes lastByte = subIndex.slice(subIndex.size() - 1, 1); | ||
return (Bytes32) Bytes.concatenate(base.slice(0, 31), lastByte); | ||
return (Bytes32) Bytes.concatenate(base, lastByte); | ||
} | ||
|
||
/** | ||
|
@@ -150,7 +172,7 @@ public Bytes32 swapLastByte(Bytes32 base, Bytes subIndex) { | |
* @param address The address. | ||
* @return The generated version key. | ||
*/ | ||
public Bytes32 versionKey(Bytes address) { | ||
public Bytes32 versionKey(final Bytes address) { | ||
return headerKey(address, VERSION_LEAF_KEY); | ||
} | ||
|
||
|
@@ -160,7 +182,7 @@ public Bytes32 versionKey(Bytes address) { | |
* @param address The address. | ||
* @return The generated balance key. | ||
*/ | ||
public Bytes32 balanceKey(Bytes address) { | ||
public Bytes32 balanceKey(final Bytes address) { | ||
return headerKey(address, BALANCE_LEAF_KEY); | ||
} | ||
|
||
|
@@ -170,7 +192,7 @@ public Bytes32 balanceKey(Bytes address) { | |
* @param address The address. | ||
* @return The generated nonce key. | ||
*/ | ||
public Bytes32 nonceKey(Bytes address) { | ||
public Bytes32 nonceKey(final Bytes address) { | ||
return headerKey(address, NONCE_LEAF_KEY); | ||
} | ||
|
||
|
@@ -180,7 +202,7 @@ public Bytes32 nonceKey(Bytes address) { | |
* @param address The address. | ||
* @return The generated code Keccak key. | ||
*/ | ||
public Bytes32 codeKeccakKey(Bytes address) { | ||
public Bytes32 codeKeccakKey(final Bytes address) { | ||
return headerKey(address, CODE_KECCAK_LEAF_KEY); | ||
} | ||
|
||
|
@@ -190,11 +212,11 @@ public Bytes32 codeKeccakKey(Bytes address) { | |
* @param address The address. | ||
* @return The generated code size key. | ||
*/ | ||
public Bytes32 codeSizeKey(Bytes address) { | ||
public Bytes32 codeSizeKey(final Bytes address) { | ||
return (headerKey(address, CODE_SIZE_LEAF_KEY)); | ||
} | ||
|
||
public int getNbChunk(Bytes bytecode) { | ||
public int getNbChunk(final Bytes bytecode) { | ||
return bytecode.isEmpty() ? 0 : (1 + ((bytecode.size() - 1) / CHUNK_SIZE)); | ||
} | ||
|
||
|
@@ -205,15 +227,15 @@ public int getNbChunk(Bytes bytecode) { | |
* @param bytecode Code's bytecode | ||
* @return List of 32-bytes code chunks | ||
*/ | ||
public List<UInt256> chunkifyCode(Bytes bytecode) { | ||
public List<UInt256> chunkifyCode(final Bytes bytecode) { | ||
if (bytecode.isEmpty()) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
// Chunking variables | ||
final int CHUNK_SIZE = 31; | ||
int nChunks = getNbChunk(bytecode); | ||
int padSize = nChunks * CHUNK_SIZE - bytecode.size(); | ||
final int nChunks = getNbChunk(bytecode); | ||
final int padSize = nChunks * CHUNK_SIZE - bytecode.size(); | ||
final Bytes code = Bytes.concatenate(bytecode, Bytes.repeat((byte) 0, padSize)); | ||
final List<UInt256> chunks = new ArrayList<>(nChunks); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is something that I might be missing here.
It seems to me like stem is the 31-bytes prefix part of the trie-key that you get from hashing the address and storageKey.
Then, why would you need to swap the last byte ? I thought this was done on the 32nd (last) byte only.
To me, the storageKey would be just the concatenation here of stem and suffix.