]> git.codecow.com Git - nano25519.git/commitdiff
Refactor blake to zero with WASM instructions instead of fill or copy which must...
authorChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 19:28:19 +0000 (12:28 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 19:28:19 +0000 (12:28 -0700)
scripts/blake2b-gen.mjs
src/assembly/ed25519/blake2b.ts

index 116c23adcb2a7ecbc975edf62e210a9bff10cf61..1deda8c2db3f794f800a245a04583360f44e47bf 100644 (file)
@@ -75,19 +75,25 @@ const DECLARATIONS = (() => {
 })()
 
 function CLEAR () {
-       let output = `          this.b.fill(0)
-               this.h.fill(0)
+       let output = `          // reset buffers and counters
+               const b_ptr = changetype<usize>(this.b)`
+
+       for (let i = 0; i < 128; i += 16) {
+               output += `
+               v128.store(b_ptr, i64x2.splat(0), ${i})`
+       }
+
+       output += `
                this.c = 0
-               this.t = 0
-`
+               this.t = 0`
 
        return output
 }
 
 const INIT = (() => {
        let output = `  init (): Blake2b {
-               // reset buffers and counters
 ${CLEAR()}
+
                // initialize parameter block where each u8 combines to u32 little-endian
                // - p0 = 64: always 64-byte digest for this implementation
                // - p1 = 0: no key
@@ -273,10 +279,21 @@ ${INIT}
                this.compress(changetype<usize>(this.b), 0xffffffffffffffff)
 
                // return byte array of 64-byte chain buffer
-               memory.copy(changetype<usize>(output), changetype<usize>(this.h), 64)
+               const h_ptr = changetype<usize>(this.h)
+               const output_ptr = changetype<usize>(output)
+
+               v128.store(output_ptr, v128.load(h_ptr, 0), 0)
+               v128.store(output_ptr, v128.load(h_ptr, 16), 16)
+               v128.store(output_ptr, v128.load(h_ptr, 32), 32)
+               v128.store(output_ptr, v128.load(h_ptr, 48), 48)
+
+               v128.store(h_ptr, i64x2.splat(0), 0)
+               v128.store(h_ptr, i64x2.splat(0), 16)
+               v128.store(h_ptr, i64x2.splat(0), 32)
+               v128.store(h_ptr, i64x2.splat(0), 48)
 
-               // clear internal buffers and counters
 ${CLEAR()}
+
                return
        }
 }
index 58d06cbd50baa95e9aab4cceebf659da584dc9ce..9da890b69a93244a62ae1187b67fc286f300708c 100644 (file)
@@ -957,8 +957,15 @@ export class Blake2b {
 
        init (): Blake2b {
                // reset buffers and counters
-               this.b.fill(0)
-               this.h.fill(0)
+               const b_ptr = changetype<usize>(this.b)
+               v128.store(b_ptr, i64x2.splat(0), 0)
+               v128.store(b_ptr, i64x2.splat(0), 16)
+               v128.store(b_ptr, i64x2.splat(0), 32)
+               v128.store(b_ptr, i64x2.splat(0), 48)
+               v128.store(b_ptr, i64x2.splat(0), 64)
+               v128.store(b_ptr, i64x2.splat(0), 80)
+               v128.store(b_ptr, i64x2.splat(0), 96)
+               v128.store(b_ptr, i64x2.splat(0), 112)
                this.c = 0
                this.t = 0
 
@@ -1029,11 +1036,29 @@ export class Blake2b {
                this.compress(changetype<usize>(this.b), 0xffffffffffffffff)
 
                // return byte array of 64-byte chain buffer
-               memory.copy(changetype<usize>(output), changetype<usize>(this.h), 64)
+               const h_ptr = changetype<usize>(this.h)
+               const output_ptr = changetype<usize>(output)
 
-               // clear internal buffers and counters
-               this.b.fill(0)
-               this.h.fill(0)
+               v128.store(output_ptr, v128.load(h_ptr, 0), 0)
+               v128.store(output_ptr, v128.load(h_ptr, 16), 16)
+               v128.store(output_ptr, v128.load(h_ptr, 32), 32)
+               v128.store(output_ptr, v128.load(h_ptr, 48), 48)
+
+               v128.store(h_ptr, i64x2.splat(0), 0)
+               v128.store(h_ptr, i64x2.splat(0), 16)
+               v128.store(h_ptr, i64x2.splat(0), 32)
+               v128.store(h_ptr, i64x2.splat(0), 48)
+
+               // reset buffers and counters
+               const b_ptr = changetype<usize>(this.b)
+               v128.store(b_ptr, i64x2.splat(0), 0)
+               v128.store(b_ptr, i64x2.splat(0), 16)
+               v128.store(b_ptr, i64x2.splat(0), 32)
+               v128.store(b_ptr, i64x2.splat(0), 48)
+               v128.store(b_ptr, i64x2.splat(0), 64)
+               v128.store(b_ptr, i64x2.splat(0), 80)
+               v128.store(b_ptr, i64x2.splat(0), 96)
+               v128.store(b_ptr, i64x2.splat(0), 112)
                this.c = 0
                this.t = 0