-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tree.mjs * test tree * small.txt without new line * ci on windows * allow reading files on deno
- Loading branch information
1 parent
ccf8cdc
commit 367b01f
Showing
9 changed files
with
136 additions
and
47 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** @type {(uint5: number) => string} */ | ||
const toBase32 = uint5 => '0123456789abcdefghjkmnpqrstvwxyz'[uint5] | ||
|
||
/** @type {(uint256: bigint) => number} */ | ||
const getParityBit = uint256 => { | ||
let xor = uint256 ^ (uint256 >> 128n) | ||
xor ^= xor >> 64n | ||
xor ^= xor >> 32n | ||
xor ^= xor >> 16n | ||
xor ^= xor >> 8n | ||
xor ^= xor >> 4n | ||
xor ^= xor >> 2n | ||
xor ^= xor >> 1n | ||
return Number(xor & 1n) | ||
} | ||
|
||
/** @type {(bu224: bigint) => string} */ | ||
const toAddress = bu224 => { | ||
let address = '' | ||
const parity = getParityBit(bu224) | ||
for (let j = 0; j < 45; j++) { | ||
let uint5 = Number(bu224 & 0b11111n) | ||
if (j == 44) { | ||
uint5 += parity << 4 | ||
} | ||
address += toBase32(uint5) | ||
bu224 >>= 5n | ||
} | ||
return address | ||
} | ||
|
||
export default { | ||
toAddress, | ||
getParityBit | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Base32 is an encoding method based on the base-32 numeral system. It uses an alphabet of 32 digits, each of which represents a different combination of 5 bits (25). Since base32 is not very widely adopted, the question of notation—which characters to use to represent the 32 digits—is not as settled as in the case of more well-known numeral systems (such as hexadecimal), though RFCs and unofficial and de-facto standards exist. One way to represent Base32 numbers in a human-readable way is by using the digits 0–9 and the twenty-two upper-case letters A–V. However, many other variations are used in different contexts. Historically, Baudot code could be considered a modified (stateful) base32 code. |
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import digest256 from './digest256.mjs' | ||
import subtree from './subtree.mjs' | ||
import sha224 from './sha224.mjs' | ||
/** @typedef {import('./subtree.mjs').State} SubTreeState */ | ||
const { byteToDigest } = digest256 | ||
const { newState: newSubTree, push: pushSubTree, end: endSubTree } = subtree | ||
const { compress } = sha224 | ||
|
||
/** | ||
* @typedef {SubTreeState[]} State | ||
*/ | ||
|
||
const mask244 = ((1n << 224n) - 1n) | ||
|
||
/** @type {(state: State) => (nu8: number) => void} */ | ||
const push = state => nu8 => { | ||
let i = 0 | ||
let last0 = byteToDigest(nu8) | ||
while (true) { | ||
let subTree = state[i] | ||
if (subTree === undefined) { | ||
state.push(newSubTree(last0)) | ||
return | ||
} | ||
const last1 = pushSubTree(subTree)(last0) | ||
if (last1 === null) { | ||
return | ||
} | ||
last0 = last1 | ||
i += 1 | ||
} | ||
} | ||
|
||
/** @type {(state: State) => bigint} */ | ||
const end = state => { | ||
let last0 = 0n | ||
for (let subTree of state) { | ||
last0 = endSubTree(subTree)(last0) | ||
} | ||
return compress(last0) & mask244 | ||
} | ||
|
||
export default { | ||
push, | ||
end | ||
} |