]> git.codecow.com Git - nano25519.git/commitdiff
Deprecate unnecessary parameter block to remove expensive loads, stores, and fills.
authorChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 15:58:24 +0000 (08:58 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 15:58:24 +0000 (08:58 -0700)
scripts/blake2b-gen.mjs
src/assembly/ed25519/blake2b.ts

index 121e79b8d311912c0b535de264ef2c66c455bdc7..116c23adcb2a7ecbc975edf62e210a9bff10cf61 100644 (file)
@@ -61,14 +61,13 @@ const DECLARATIONS = (() => {
         * Buffers and counters
         * - b: input buffer, filled and refilled by input data in ${B_LENGTH} byte increments
         * - h: chain buffer, updated after each compression
-        * - p: set according to BLAKE2 layout from constructor arguments
+        * - p: preset in this implementation and according to BLAKE2 layout
         * - c: buffer counter, points to current index in \`b\`
         * - t: total byte counter, incremented by \`c\`
         *   - Note: BLAKE2b reference supports 2¹²⁸-1, but this implementation restricts to 2⁶⁴-1 for compatibility with WASM without SIMD)
         */
        b: StaticArray<u8> = new StaticArray<u8>(${B_LENGTH})
        h: StaticArray<u64> = new StaticArray<u64>(${H_LENGTH})
-       p: StaticArray<u8> = new StaticArray<u8>(64)
        c: u8 = 0
        t: u64 = 0
 `
@@ -78,7 +77,6 @@ const DECLARATIONS = (() => {
 function CLEAR () {
        let output = `          this.b.fill(0)
                this.h.fill(0)
-               this.p.fill(0)
                this.c = 0
                this.t = 0
 `
@@ -90,20 +88,19 @@ const INIT = (() => {
        let output = `  init (): Blake2b {
                // reset buffers and counters
 ${CLEAR()}
-               // initialize parameter block
-               this.p[0] = 64 // always 64 bytes for this implementation
-               this.p[1] = 0 // no key
-               this.p[2] = 1 // fanout
-               this.p[3] = 1 // depth
+               // initialize parameter block where each u8 combines to u32 little-endian
+               // - p0 = 64: always 64-byte digest for this implementation
+               // - p1 = 0: no key
+               // - p2 = 1: fanout
+               // - p3 = 1: depth
+               this.h[0] = ${blake2b_iv[0]} ^ ${(1 << 24 | 1 << 16 | 0 << 8 | 64 << 0)}
 `
 
        output += `
-               // initialize hash state
-               const p_ptr = changetype<usize>(this.p)
-`
-       for (let i = 0; i < H_LENGTH; i++) {
+               // initialize remaining hash state`
+       for (let i = 1; i < H_LENGTH; i++) {
                output += `
-               this.h[${i}] = ${blake2b_iv[i]} ^ load<u64>(p_ptr, ${i << 3})`
+               this.h[${i}] = ${blake2b_iv[i]}`
        }
 
        output += `
@@ -227,6 +224,7 @@ export class Blake2b {
 ${DECLARATIONS}
 ${COMPRESS}
 ${INIT}
+
        // input: variable-length message bytes passed by user to be hashed
        update (input: StaticArray<u8>, length: i32): Blake2b {
                const input_ptr: usize = changetype<usize>(input)
index 119918a0c4eb3641b68d95667c03680effeb1178..58d06cbd50baa95e9aab4cceebf659da584dc9ce 100644 (file)
@@ -6,14 +6,13 @@ export class Blake2b {
         * Buffers and counters
         * - b: input buffer, filled and refilled by input data in 128 byte increments
         * - h: chain buffer, updated after each compression
-        * - p: set according to BLAKE2 layout from constructor arguments
+        * - p: preset in this implementation and according to BLAKE2 layout
         * - c: buffer counter, points to current index in `b`
         * - t: total byte counter, incremented by `c`
         *   - Note: BLAKE2b reference supports 2¹²⁸-1, but this implementation restricts to 2⁶⁴-1 for compatibility with WASM without SIMD)
         */
        b: StaticArray<u8> = new StaticArray<u8>(128)
        h: StaticArray<u64> = new StaticArray<u64>(8)
-       p: StaticArray<u8> = new StaticArray<u8>(64)
        c: u8 = 0
        t: u64 = 0
 
@@ -960,30 +959,28 @@ export class Blake2b {
                // reset buffers and counters
                this.b.fill(0)
                this.h.fill(0)
-               this.p.fill(0)
                this.c = 0
                this.t = 0
 
-               // initialize parameter block
-               this.p[0] = 64 // always 64 bytes for this implementation
-               this.p[1] = 0 // no key
-               this.p[2] = 1 // fanout
-               this.p[3] = 1 // depth
-
-               // initialize hash state
-               const p_ptr = changetype<usize>(this.p)
-
-               this.h[0] = 0x6a09e667f3bcc908 ^ load<u64>(p_ptr, 0)
-               this.h[1] = 0xbb67ae8584caa73b ^ load<u64>(p_ptr, 8)
-               this.h[2] = 0x3c6ef372fe94f82b ^ load<u64>(p_ptr, 16)
-               this.h[3] = 0xa54ff53a5f1d36f1 ^ load<u64>(p_ptr, 24)
-               this.h[4] = 0x510e527fade682d1 ^ load<u64>(p_ptr, 32)
-               this.h[5] = 0x9b05688c2b3e6c1f ^ load<u64>(p_ptr, 40)
-               this.h[6] = 0x1f83d9abfb41bd6b ^ load<u64>(p_ptr, 48)
-               this.h[7] = 0x5be0cd19137e2179 ^ load<u64>(p_ptr, 56)
+               // initialize parameter block where each u8 combines to u32 little-endian
+               // - p0 = 64: always 64-byte digest for this implementation
+               // - p1 = 0: no key
+               // - p2 = 1: fanout
+               // - p3 = 1: depth
+               this.h[0] = 0x6a09e667f3bcc908 ^ 16842816
+
+               // initialize remaining hash state
+               this.h[1] = 0xbb67ae8584caa73b
+               this.h[2] = 0x3c6ef372fe94f82b
+               this.h[3] = 0xa54ff53a5f1d36f1
+               this.h[4] = 0x510e527fade682d1
+               this.h[5] = 0x9b05688c2b3e6c1f
+               this.h[6] = 0x1f83d9abfb41bd6b
+               this.h[7] = 0x5be0cd19137e2179
 
                return this
        }
+
        // input: variable-length message bytes passed by user to be hashed
        update (input: StaticArray<u8>, length: i32): Blake2b {
                const input_ptr: usize = changetype<usize>(input)
@@ -1037,7 +1034,6 @@ export class Blake2b {
                // clear internal buffers and counters
                this.b.fill(0)
                this.h.fill(0)
-               this.p.fill(0)
                this.c = 0
                this.t = 0