})()
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
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
}
}
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
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