-
Notifications
You must be signed in to change notification settings - Fork 2
#134 Use big endian bit ordering and byte array paths in smt #135
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
Merged
Merged
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /** | ||
| * Thrown when a sparse Merkle tree insertion targets a key that is already | ||
| * present in the tree. | ||
| */ | ||
| export class LeafExistsError extends Error { | ||
|
martti007 marked this conversation as resolved.
|
||
| public constructor() { | ||
| super('Leaf already exists.'); | ||
| this.name = 'LeafExistsError'; | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -1,43 +1,71 @@ | ||
| type CommonPath = { length: number; path: bigint }; | ||
|
|
||
| export function calculateCommonPath(path1: bigint, path2: bigint): CommonPath { | ||
| let path = 1n; | ||
| let mask = 1n; | ||
| let length = 0; | ||
| /** | ||
| * Length of the common big-endian bit prefix shared by keys `a` and `b`, capped at `maxDepth` | ||
| * (depth 0 is the most significant bit of byte 0). Used to find where a new key bifurcates from an | ||
| * existing branch: pass `256` for a leaf (compare the whole key) or the node's depth for an interior | ||
| * branch (its stored region is only meaningful up to that depth). | ||
| */ | ||
| export function commonPrefixLength(a: Uint8Array, b: Uint8Array, maxDepth: number): number { | ||
| const fullBytes = maxDepth >> 3; | ||
| for (let i = 0; i < fullBytes; i++) { | ||
| if (a[i] !== b[i]) { | ||
| return (i << 3) + Math.clz32(a[i] ^ b[i]) - 24; | ||
| } | ||
| } | ||
|
|
||
| while ((path1 & mask) === (path2 & mask) && path < path1 && path < path2) { | ||
| mask <<= 1n; | ||
| length += 1; | ||
| path = mask | ((mask - 1n) & path1); | ||
| const remainderBits = maxDepth & 7; | ||
| if (remainderBits > 0) { | ||
| const diff = (a[fullBytes] ^ b[fullBytes]) & (0xff << (8 - remainderBits)); | ||
| if (diff !== 0) { | ||
| return (fullBytes << 3) + Math.clz32(diff) - 24; | ||
| } | ||
| } | ||
|
|
||
| return { length, path }; | ||
| return maxDepth; | ||
| } | ||
|
martti007 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Region committed by an interior node: the `depth`-bit common prefix of all leaves in the node's | ||
| * sub-tree. The `i`th lowest bit of path will be the `i mod 8`th lowest bit in the `i div 8`th | ||
| * byte of the returned 32-byte array (so the packing is little-endian); the remaining bits of the array | ||
| * are set to zero. | ||
| * The key's first `depth` bits, with the remaining bits of the 32-byte array zeroed. | ||
| */ | ||
| export function pathToRegion(path: bigint, depth: number): Uint8Array { | ||
| export function regionFromKey(key: Uint8Array, depth: number): Uint8Array { | ||
| const region = new Uint8Array(32); | ||
| const fullBytes = Math.floor(depth / 8); | ||
| const remainderBits = depth % 8; | ||
|
|
||
| let bits = path; | ||
| for (let j = 0; j < fullBytes; j++, bits >>= 8n) { | ||
| region[j] = Number(bits & 0xffn); | ||
| } | ||
| const fullBytes = depth >> 3; | ||
| const remainderBits = depth & 7; | ||
| region.set(key.subarray(0, fullBytes)); | ||
| if (remainderBits > 0) { | ||
| region[fullBytes] = Number(bits & 0xffn) & ((1 << remainderBits) - 1); | ||
| region[fullBytes] = key[fullBytes] & ((0xff << (8 - remainderBits)) & 0xff); | ||
| } | ||
| return region; | ||
| } | ||
|
martti007 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Big-endian bit of `data` at the given depth per the Yellowpaper: depth 0 is the most significant | ||
| * bit of `data[0]` (`data[0] & 0x80`) and depth 255 is the least significant bit of `data[31]`. | ||
| */ | ||
| export function getBitAtDepth(data: Uint8Array, depth: number): number { | ||
| depth = Number(depth); | ||
| const byteIndex = Math.floor(depth / 8); | ||
| const bitInByte = depth % 8; | ||
| return (data[byteIndex] >> bitInByte) & 1; | ||
| if (!Number.isInteger(depth) || depth < 0 || depth >= data.length * 8) { | ||
| throw new Error(`Depth ${depth} is out of bounds for a ${data.length}-byte value.`); | ||
| } | ||
| const byteIndex = depth >> 3; | ||
| const bitInByte = depth & 7; | ||
| return (data[byteIndex] >> (7 - bitInByte)) & 1; | ||
| } | ||
|
martti007 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Render the first `length` big-endian bits of `data` as a `'0'`/`'1'` string: whole bytes first, | ||
| * then the high bits of the partial byte. | ||
| */ | ||
| export function bitsToString(data: Uint8Array, length: number): string { | ||
| if (!Number.isInteger(length) || length < 0 || length > data.length * 8) { | ||
| throw new Error(`Length ${length} is out of bounds for a ${data.length}-byte value.`); | ||
| } | ||
| const fullBytes = length >> 3; | ||
| const remainderBits = length & 7; | ||
| let bits = ''; | ||
| for (let i = 0; i < fullBytes; i++) { | ||
| bits += data[i].toString(2).padStart(8, '0'); | ||
| } | ||
| if (remainderBits > 0) { | ||
| bits += data[fullBytes].toString(2).padStart(8, '0').slice(0, remainderBits); | ||
| } | ||
| return bits; | ||
| } | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.