Skip to content
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

add digest256 #17

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
41 changes: 41 additions & 0 deletions digest256.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sha224 from './sha224.mjs'
const { compress } = sha224

const maxLength = 248n

const u32Mask = 0xffff_ffffn

const hashMask = u32Mask << 224n;

const byteMask = 0x08n << maxLength

const dataMask = (1n << 248n) - 1n

// a length of the sequence in bits.
// The function returns 255 if the digest is a hash.
/** @type {(bu256: bigint) => bigint} */
const len = bu256 => (bu256 >> maxLength) & u32Mask

/** @type {(nu8: number) => bigint} */
const byteToDigest = nu8 => BigInt(nu8) | byteMask

/** @type {(bu256: bigint) => bigint} */
const getData = bu256 => bu256 & dataMask

/** @type {(a: bigint) => (b: bigint) => bigint} */
const merge = a => b => {
const lenA = len(a)
const lenB = len(b)
const lenAB = lenA + lenB
if (lenAB <= maxLength) {
const data = getData(a) | (getData(b) << lenA);
return data | (lenAB << maxLength)
}
return compress(a | (b << 256n))
}

export default {
merge,
byteToDigest,
len
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.mjs",
"scripts": {
"tsc": "tsc",
"test": "tsc & node test.mjs"
"test": "tsc & node --trace-uncaught test.mjs"
},
"repository": {
"type": "git",
Expand Down
39 changes: 39 additions & 0 deletions test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import index from './index.mjs'
import sha224 from './sha224.mjs'
import digest256 from './digest256.mjs'
const { getParityBit } = index
const { compress } = sha224
const { merge, byteToDigest, len } = digest256

{
const parity = getParityBit(0n)
Expand All @@ -17,4 +19,41 @@ const { compress } = sha224
const hash = compress(0x8000_0000n)
const result = hash.toString(16)
if (result !== 'ffffffffc5b3e42f828ea62a15a2b01f288234c4476102bb2a3a2bc9d14a028c') { throw result }
}

{
const a = byteToDigest(0x12)
if (a !== 0x0800_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0012n) { throw a.toString(16) }
const b = byteToDigest(0x34)
if (b !== 0x0800_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0034n) { throw b.toString(16) }
const lenA = len(a)
if (lenA !== 8n) { throw lenA.toString(16) }

const c = merge(a)(b)
if (c !== 0x1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_3412n) { throw c.toString(16) }

const c2 = merge(c)(c)
if (c2 !== 0x2000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_3412_3412n) { throw c2.toString(16) }

const c4 = merge(c2)(c2)
if (c4 !== 0x4000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_3412_3412_3412_3412n) { throw c4.toString(16) }

const c8 = merge(c4)(c4)
if (c8 !== 0x8000_0000_0000_0000_0000_0000_0000_0000_3412_3412_3412_3412_3412_3412_3412_3412n) { throw c8.toString(16) }

const c12 = merge(c8)(c4)
if (c12 !== 0xc000_0000_0000_0000_3412_3412_3412_3412_3412_3412_3412_3412_3412_3412_3412_3412n) { throw c12.toString(16) }

const c16 = merge(c8)(c8)
if (len(c16) !== 0xffn) { throw c16.toString(16) }

{
const result = merge(a)(0n)
if (result !== a) { throw result }
}

{
const result = merge(0n)(a)
if (result !== a) { throw result }
}
}