]> zoso.dev Git - nano-pow.git/commitdiff
Create DataView on hash message to enable setting endianness.
authorChris Duncan <chris@zoso.dev>
Mon, 3 Mar 2025 14:56:20 +0000 (06:56 -0800)
committerChris Duncan <chris@zoso.dev>
Mon, 3 Mar 2025 14:56:20 +0000 (06:56 -0800)
src/classes/cpu.ts

index 133b6ea5685abf40747e4e51a002c939c818570d..44fea21290990b12fce420b793a17e2db72b2aab 100644 (file)
@@ -14,6 +14,7 @@ export class NanoPowCpu {
        static #blake2b_sigma: number[][]
        static #v: BigUint64Array
        static #m: BigUint64Array
+       static #mView: DataView
 
        static async init (): Promise<void> {
                if (this.#busy) return
@@ -45,6 +46,7 @@ export class NanoPowCpu {
                        ]
                        this.#v = new BigUint64Array(16)
                        this.#m = new BigUint64Array(16)
+                       this.#mView = new DataView(this.#m.buffer)
                        console.log(`NanoPow CPU initialized.`)
                } catch (err) {
                        throw new Error('NanoPow CPU initialization failed.', { cause: err })
@@ -124,11 +126,11 @@ export class NanoPowCpu {
                this.#v[12] ^= 40n // Output length
 
                // Set up input buffers
-               this.#m[0] = seed
-               if (this.#debug) console.log(`seed: ${this.#m[0].toString(16).padStart(16, '0')}`)
+               this.#mView.setBigUint64(0, seed, true)
+               if (this.#debug) console.log(`seed: ${this.#m[0].toString(16).padStart(16, '0')} (${this.#m[0]})`)
                for (let i = 0; i < hash.length; i += 16) {
                        const u64 = hash.slice(i, i + 16)
-                       this.#m[i / 16 + 1] = BigInt(`0x${u64}`)
+                       this.#mView.setBigUint64(i / 2 + 8, BigInt(`0x${u64}`))
                }
                if (this.#debug) console.log('m', this.#m)