-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(perf): speed up construction of bbjs Frs & cache zero hashes in …
…ephemeral trees (redo) (#11894) Redo of AztecProtocol/aztec-packages#11851 --------- Co-authored-by: dbanks12 <david@aztecprotocol.com> Co-authored-by: David Banks <47112877+dbanks12@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
63 additions
and
30 deletions.
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 |
---|---|---|
@@ -1,21 +1,43 @@ | ||
export function toBigIntBE(bytes: Uint8Array) { | ||
// A Buffer in node, *is* a Uint8Array. We can't refuse it's type. | ||
// However the algo below only works on an actual Uint8Array, hence we make a new one to be safe. | ||
bytes = new Uint8Array(bytes); | ||
let bigint = BigInt(0); | ||
const view = new DataView(bytes.buffer); | ||
for (let i = 0; i < bytes.byteLength; i++) { | ||
bigint = (bigint << BigInt(8)) + BigInt(view.getUint8(i)); | ||
} | ||
return bigint; | ||
/** | ||
* Convert a 32-byte BE Buffer to a BigInt. | ||
*/ | ||
export function buffer32BytesToBigIntBE(buf: Buffer): bigint { | ||
return ( | ||
(buf.readBigUInt64BE(0) << 192n) + | ||
(buf.readBigUInt64BE(8) << 128n) + | ||
(buf.readBigUInt64BE(16) << 64n) + | ||
buf.readBigUInt64BE(24) | ||
); | ||
} | ||
|
||
/** | ||
* Convert a BE Uint8Array to a BigInt. | ||
*/ | ||
export function uint8ArrayToBigIntBE(bytes: Uint8Array): bigint { | ||
const buffer = Buffer.from(bytes); | ||
return buffer32BytesToBigIntBE(buffer); | ||
} | ||
|
||
export function toBufferBE(value: bigint, byteLength = 32) { | ||
const bytes = new Uint8Array(byteLength); | ||
const view = new DataView(bytes.buffer); | ||
for (let i = 0; i < byteLength; i++) { | ||
view.setUint8(byteLength - i - 1, Number(value & BigInt(0xff))); | ||
value >>= BigInt(8); | ||
/** | ||
* Convert a BigInt to a 32-byte BE Buffer. | ||
*/ | ||
export function bigIntToBufferBE(value: bigint, byteLength = 32): Buffer { | ||
if (byteLength != 32) { | ||
throw new Error( | ||
`Only 32 bytes supported for conversion from bigint to buffer, attempted byte length: ${byteLength}`, | ||
); | ||
} | ||
return bytes; | ||
const buf = Buffer.alloc(byteLength); | ||
buf.writeBigUInt64BE(value >> 192n, 0); | ||
buf.writeBigUInt64BE((value >> 128n) & 0xffffffffffffffffn, 8); | ||
buf.writeBigUInt64BE((value >> 64n) & 0xffffffffffffffffn, 16); | ||
buf.writeBigUInt64BE(value & 0xffffffffffffffffn, 24); | ||
return buf; | ||
} | ||
|
||
/** | ||
* Convert a BigInt to a 32-byte BE Uint8Array. | ||
*/ | ||
export function bigIntToUint8ArrayBE(value: bigint, byteLength = 32): Uint8Array { | ||
return new Uint8Array(bigIntToBufferBE(value, byteLength)); | ||
} |
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