From: Chris Duncan Date: Wed, 28 Oct 2026 19:28:19 +0000 (-0700) Subject: Refactor blake to zero with WASM instructions instead of fill or copy which must... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=f0410aa13a2327f9c3d3fc201ae1bb548a5be6d5;p=nano25519.git Refactor blake to zero with WASM instructions instead of fill or copy which must call out to C helpers. --- diff --git a/scripts/blake2b-gen.mjs b/scripts/blake2b-gen.mjs index 116c23a..1deda8c 100644 --- a/scripts/blake2b-gen.mjs +++ b/scripts/blake2b-gen.mjs @@ -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(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(this.b), 0xffffffffffffffff) // return byte array of 64-byte chain buffer - memory.copy(changetype(output), changetype(this.h), 64) + const h_ptr = changetype(this.h) + const output_ptr = changetype(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 } } diff --git a/src/assembly/ed25519/blake2b.ts b/src/assembly/ed25519/blake2b.ts index 58d06cb..9da890b 100644 --- a/src/assembly/ed25519/blake2b.ts +++ b/src/assembly/ed25519/blake2b.ts @@ -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(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(this.b), 0xffffffffffffffff) // return byte array of 64-byte chain buffer - memory.copy(changetype(output), changetype(this.h), 64) + const h_ptr = changetype(this.h) + const output_ptr = changetype(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(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