Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

chore: misc tweaks to hashers #377

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions hashers/blake2b.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Hex, hex } from "../util/mod.ts";
import wasmCode from "./blake2b.wasm.ts";

const memory = new WebAssembly.Memory({ initial: 1, maximum: 128 });
Expand All @@ -9,7 +8,6 @@ const wasmInstance = new WebAssembly.Instance(wasmModule, {
});

interface XxhashWasm {
iv_adr: WebAssembly.Global;
free_mem: WebAssembly.Global;
reset(state_adr: number, dk_len: number): void;
update(state_adr: number, msg_adr: number, msg_end: number, written: number): void;
Expand All @@ -21,22 +19,6 @@ const wasm = wasmInstance.exports as never as XxhashWasm;
let memBuf = new Uint8Array(memory.buffer);
let memI = wasm.free_mem.value;

memBuf.set(
hex.decode(
"\
08c9bcf367e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5\
d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b\
0008101820283038404850586068707870502040487868300860001058382818\
5840600028107868507018303808482038481808686058701030285020007840\
4800283810205078700858603040186810603050005840182068382878700848\
6028087870682050003830184810405868583870600818482800782040301050\
3078704858180040601068380820502850104020383008287858487018606800\
0008101820283038404850586068707870502040487868300860001058382818\
" as Hex,
),
wasm.iv_adr.value,
);

const pool: Blake2bInner[] = [];
const finReg = new FinalizationRegistry<Blake2bInner>((inner) => {
pool.push(inner);
Expand Down
56 changes: 31 additions & 25 deletions hashers/blake2b.wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@ import { Hex, hex } from "../util/mod.ts";

export default hex.decode(
"\
0061736d0100000001290660027f7f0060027f7f017e60067f7f7f7f7f7f0060\
047f7f7f7e0060047f7f7f7f0060037f7f7f0002130107626c616b653262066d\
656d6f72790200010307060001020304050618047f0041000b7f004180010b7f\
0041c0010b7f004180030b072f050669765f616472030108667265655f6d656d\
030305726573657400000675706461746500040666696e69736800050ae50406\
21002000230141c000fc0a0000200020002903004280808408852001ad853703\
000b10002000230220016a2d00006a2903000b990101067e2000290300210620\
0129030021072002290300210820032903002109200420051001210a20042005\
41016a1001210b200620077c200a7c2106200920068542208a2109200820097c\
2108200720088542188a2107200620077c200b7c2106200920068542108a2109\
200820097c210820072008854201892107200020063703002001200737030020\
022008370300200320093703000be20201017f4100200041c000fc0a000041c0\
00230141c000fc0a000041e00041e0002903002002ad8537030041f00041f000\
29030020038537030003404100412041c00041e0002001200410024108412841\
c80041e8002001200441026a10024110413041d00041f0002001200441046a10\
024118413841d80041f8002001200441066a10024100412841d00041f8002001\
200441086a10024108413041d80041e00020012004410a6a10024110413841c0\
0041e80020012004410c6a10024118412041c80041f00020012004410e6a1002\
200441106a220441c001490d000b20002000fd0004004100fd000400fd5141c0\
00fd000400fd51fd0b0400200041106a22002000fd0004004110fd000400fd51\
41d000fd000400fd51fd0b0400200041106a22002000fd0004004120fd000400\
fd5141e000fd000400fd51fd0b0400200041106a22002000fd0004004130fd00\
0400fd5141f000fd000400fd51fd0b04000b2400034020034180016a21032000\
200120034200100320014180016a22012002490d000b0b0c0020002001200242\
7f10030b\
0061736d0100000001230560027f7f0060067f7f7f7f7f7f0060047f7f7f7e00\
60047f7f7f7f0060037f7f7f0002130107626c616b653262066d656d6f727902\
000103060500010203040607017f004180030b07260408667265655f6d656d03\
0005726573657400000675706461746500030666696e69736800040ad0040522\
00200041800141c000fc0a0000200020002903004280808408852001ad853703\
000ba20101067e20002903002106200129030021072002290300210820032903\
002109200420052d00c0016a290300210a200420052d00c1016a290300210b20\
0620077c200a7c2106200920068542208a2109200820097c2108200720088542\
188a2107200620077c200b7c2106200920068542108a2109200820097c210820\
0720088542018921072000200637030020012007370300200220083703002003\
20093703000bd40201017f4100200041c000fc0a000041c00041800141c000fc\
0a000041e00041e0002903002002ad8537030041f00041f00029030020038537\
030003404100412041c00041e0002001200410014108412841c80041e8002001\
200441026a10014110413041d00041f0002001200441046a10014118413841d8\
0041f8002001200441066a10014100412841d00041f8002001200441086a1001\
4108413041d80041e00020012004410a6a10014110413841c00041e800200120\
04410c6a10014118412041c80041f00020012004410e6a1001200441106a2204\
41c001490d000b20002000fd0004004100fd000400fd5141c000fd000400fd51\
fd0b040020002000fd0004104110fd000400fd5141d000fd000400fd51fd0b04\
1020002000fd0004204120fd000400fd5141e000fd000400fd51fd0b04202000\
2000fd0004304130fd000400fd5141f000fd000400fd51fd0b04300b24000340\
20034180016a21032000200120034200100220014180016a22012002490d000b\
0b0c00200020012002427f10020b0b880201004180010b800208c9bcf367e609\
6a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e\
511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b00081018202830\
3840485058606870787050204048786830086000105838281858406000281078\
6850701830380848203848180868605870103028502000784048002838102050\
7870085860304018681060305000584018206838287870084860280878706820\
5000383018481040586858387060081848280078204030105030787048581800\
4060106838082050285010402038300828785848701860680000081018202830\
38404850586068707870502040487868300860001058382818\
" as Hex,
);
65 changes: 38 additions & 27 deletions hashers/blake2b.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,39 @@
;; (func $dbg_u32 (param $v i32) (result i32) local.get $v call $log_u32 local.get $v)
;; (func $dbg_u64 (param $v i64) (result i64) local.get $v call $log_u64 local.get $v)

(global $staging_adr i32 (i32.const 0))
(global $iv_adr i32 (i32.const 128))
(export "iv_adr" (global $iv_adr))
(global $sigma_adr i32 (i32.const 192))
;; $staging_adr = 0
;; $iv_adr = 128
;; $sigma_adr = 192
(global $free_mem i32 (i32.const 384))
(export "free_mem" (global $free_mem))

;; iv & sigma
(data (i32.const 128)
"\08\c9\bc\f3\67\e6\09\6a\3b\a7\ca\84\85\ae\67\bb"
"\2b\f8\94\fe\72\f3\6e\3c\f1\36\1d\5f\3a\f5\4f\a5"
"\d1\82\e6\ad\7f\52\0e\51\1f\6c\3e\2b\8c\68\05\9b"
"\6b\bd\41\fb\ab\d9\83\1f\79\21\7e\13\19\cd\e0\5b"
"\00\08\10\18\20\28\30\38\40\48\50\58\60\68\70\78"
"\70\50\20\40\48\78\68\30\08\60\00\10\58\38\28\18"
"\58\40\60\00\28\10\78\68\50\70\18\30\38\08\48\20"
"\38\48\18\08\68\60\58\70\10\30\28\50\20\00\78\40"
"\48\00\28\38\10\20\50\78\70\08\58\60\30\40\18\68"
"\10\60\30\50\00\58\40\18\20\68\38\28\78\70\08\48"
"\60\28\08\78\70\68\20\50\00\38\30\18\48\10\40\58"
"\68\58\38\70\60\08\18\48\28\00\78\20\40\30\10\50"
"\30\78\70\48\58\18\00\40\60\10\68\38\08\20\50\28"
"\50\10\40\20\38\30\08\28\78\58\48\70\18\60\68\00"
"\00\08\10\18\20\28\30\38\40\48\50\58\60\68\70\78"
"\70\50\20\40\48\78\68\30\08\60\00\10\58\38\28\18"
)

(func (export "reset")
(param $state_adr i32)
(param $dk_len i32)

(memory.copy
(local.get $state_adr)
(global.get $iv_adr)
(i32.const 128) ;; $iv_adr = 128
(i32.const 64)
)

Expand All @@ -30,23 +49,13 @@
)
)

(func $get
(param $mem_adr i32)
(param $idx i32)
(result i64)

(i64.load (i32.add (local.get $mem_adr)
(i32.load8_u (i32.add (global.get $sigma_adr) (local.get $idx)))
))
)

(func $g
(param $ai i32)
(param $bi i32)
(param $ci i32)
(param $di i32)
(param $msg_adr i32)
(param $idx_1 i32)
(param $idx i32)

(local $a i64)
(local $b i64)
Expand All @@ -59,8 +68,10 @@
(local.set $b (i64.load (local.get $bi)))
(local.set $c (i64.load (local.get $ci)))
(local.set $d (i64.load (local.get $di)))
(local.set $x (call $get (local.get $msg_adr) (local.get $idx_1)))
(local.set $y (call $get (local.get $msg_adr) (i32.add (local.get $idx_1) (i32.const 1))))

;; $sigma_adr=192
(local.set $x (i64.load (i32.add (local.get $msg_adr) (i32.load8_u offset=192 (local.get $idx)))))
(local.set $y (i64.load (i32.add (local.get $msg_adr) (i32.load8_u offset=193 (local.get $idx)))))

(local.set $a (local.get $a) (i64.add (local.get $b)) (i64.add (local.get $x)))
(local.set $d (local.get $d) (i64.xor (local.get $a)) (i64.rotr (i64.const 32)))
Expand All @@ -87,14 +98,14 @@
(local $idx i32)

(memory.copy
(i32.const 0)
(i32.const 0) ;; $staging_adr
(local.get $state_adr)
(i32.const 64)
)

(memory.copy
(i32.const 64)
(global.get $iv_adr)
(i32.const 64) ;; $staging_adr + 64
(i32.const 128) ;; $iv_adr = 128
(i32.const 64)
)

Expand Down Expand Up @@ -153,20 +164,20 @@
(v128.xor (v128.load (i32.const 64)))
)

(v128.store (local.tee $state_adr (i32.add (local.get $state_adr) (i32.const 16)))
(v128.load (local.get $state_adr))
(v128.store offset=16 (local.get $state_adr)
(v128.load offset=16 (local.get $state_adr))
(v128.xor (v128.load (i32.const 16)))
(v128.xor (v128.load (i32.const 80)))
)

(v128.store (local.tee $state_adr (i32.add (local.get $state_adr) (i32.const 16)))
(v128.load (local.get $state_adr))
(v128.store offset=32 (local.get $state_adr)
(v128.load offset=32 (local.get $state_adr))
(v128.xor (v128.load (i32.const 32)))
(v128.xor (v128.load (i32.const 96)))
)

(v128.store (local.tee $state_adr (i32.add (local.get $state_adr) (i32.const 16)))
(v128.load (local.get $state_adr))
(v128.store offset=48 (local.get $state_adr)
(v128.load offset=48 (local.get $state_adr))
(v128.xor (v128.load (i32.const 48)))
(v128.xor (v128.load (i32.const 112)))
)
Expand Down
60 changes: 30 additions & 30 deletions hashers/xxhash.wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ export default hex.decode(
"\
0061736d0100000001230660000060017b017b60027f7f0060047f7f7f7f0060\
017f017e60067f7f7f7f7f7f0002120106787868617368066d656d6f72790200\
0103070600010203040506ae010d7f0041080b7f0041000b7f004180020b7f00\
41c0020b7e00428795afaf98b6de9b9e7f0b7b00fd0c87caeb85b179379e87ca\
eb85b179379e0b7e0042cfd6d3bed2c7abd9420b7b00fd0c4febd4273daeb2c2\
4febd4273daeb2c20b7e0042f9f3ddf199f699ab160b7e0042e3dcca95fccef2\
f5857f0b7e0042c5cfd9b2f1e5baea270b7b00fd0cd6b5c0adee27ea604febd4\
273daeb2c20b7b00fd0c00000000000000007935147a4e86c8610b073e060a6d\
61785f726f756e6473030008667265655f6d656d030308696e69745f6d6f6400\
0005726573657400020675706461746500030664696765737400050ae2050681\
0103017f017b017f2301210003402000230b2001fdce01fd0b0400200041106a\
230c2001fdce01fd0b0400200041206a21002001fd0c01000000000000000100\
000000000000fdce012101200241016a22022300470d000b2302210041002102\
034020002002ad230a7c370300200041086a2100200241016a22022300470d00\
0b0b12002000411ffdcb0120004121fdcd01fd500b0f0020012301200041206c\
fc0a00000b890103027f027b017f03402002fd0004002307fdd5012106200241\
106afd0004002307fdd50121074100210420012108034020082008fd00040020\
06fdce0110012305fdd501fd0b0400200841106a210820082008fd0004002007\
fdce0110012305fdd501fd0b0400200841106a2108200441016a22042000470d\
000b200241206a22022003490d000b0b8a0101027b2000fd0004002101200041\
106afd00040021022001fd1d004201892001fd1d014207892002fd1d00420c89\
2002fd1d014212897c7c7c20012307fdd50110012305fdd501210120022307fd\
d50110012305fdd50121022001fd1d008523047e23097c2001fd1d018523047e\
23097c2002fd1d008523047e23097c2002fd1d018523047e23097c0ba2020302\
0103070600010203040506a3010b7f0041080b7f0041c0020b7e00428795afaf\
98b6de9b9e7f0b7b00fd0c87caeb85b179379e87caeb85b179379e0b7e0042cf\
d6d3bed2c7abd9420b7b00fd0c4febd4273daeb2c24febd4273daeb2c20b7e00\
42f9f3ddf199f699ab160b7e0042e3dcca95fccef2f5857f0b7e0042c5cfd9b2\
f1e5baea270b7b00fd0cd6b5c0adee27ea604febd4273daeb2c20b7b00fd0c00\
000000000000007935147a4e86c8610b073e060a6d61785f726f756e64730300\
08667265655f6d656d030108696e69745f6d6f64000005726573657400020675\
706461746500030664696765737400050ace05067b03017f017b017f03402000\
23092001fdce01fd0b04002000230a2001fdce01fd0b0410200041206a210020\
01fd0c01000000000000000100000000000000fdce012101200241016a220223\
00470d000b418002210041002102034020002002ad23087c370300200041086a\
2100200241016a22022300470d000b0b12002000411ffdcb0120004121fdcd01\
fd500b0f0020014100200041206cfc0a00000b7f03027f027b017f03402002fd\
0004002305fdd50121062002fd0004102305fdd5012107410021042001210803\
4020082008fd0004002006fdce0110012303fdd501fd0b040020082008fd0004\
102007fdce0110012303fdd501fd0b0410200841206a2108200441016a220420\
00470d000b200241206a22022003490d000b0b870101027b2000fd0004002101\
2000fd00041021022001fd1d004201892001fd1d014207892002fd1d00420c89\
2002fd1d014212897c7c7c20012305fdd50110012303fdd501210120022305fd\
d50110012303fdd50121022001fd1d008523027e23077c2001fd1d018523027e\
23077c2002fd1d008523027e23077c2002fd1d018523027e23077c0ba3020302\
7f017e027f200241204e0440200521070340200720011004370300200741086a\
2107200141206a2101200641016a22062000470d000b0520052302200041086c\
fc0a00000b2005210741002106034020072903002002ad7c2108200321090340\
0240200941086a220a20044b0d00200929030023067e421f8923047e20088542\
1b8923047e23097c2108200a21090c010b0b0240200941046a220a20044b0d00\
200935020023047e20088542178923067e23087c2108200a21090b0340024020\
0920044f0d002009310000230a7e200885420b8923047e2108200941016a2109\
0c010b0b200820084221888523067e22082008421d888523087e220820084220\
8885210820072008370300200741086a2107200641016a22062000470d000b0b\
2107200141206a2101200641016a22062000470d000b05200541800220004108\
6cfc0a00000b2005210741002106034020072903002002ad7c21082003210903\
400240200941086a220a20044b0d00200929030023047e421f8923027e200885\
421b8923027e23077c2108200a21090c010b0b0240200941046a220a20044b0d\
00200935020023027e20088542178923047e23067c2108200a21090b03400240\
200920044f0d00200931000023087e200885420b8923027e2108200941016a21\
090c010b0b200820084221888523047e22082008421d888523067e2208200842\
208885210820072008370300200741086a2107200641016a22062000470d000b\
0b\
" as Hex,
);
31 changes: 14 additions & 17 deletions hashers/xxhash.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

(global $max_rounds i32 (i32.const 8))
(export "max_rounds" (global $max_rounds))
(global $init_state_adr i32 (i32.const 0))
(global $alt_digest_init_adr i32 (i32.const 256))
;; $init_state_adr = 0
;; $alt_digest_init_adr = 256
(global $free_mem i32 (i32.const 320))
(export "free_mem" (global $free_mem))

Expand All @@ -29,14 +29,14 @@
(local $ii v128)
(local $i i32)

(local.set $state_adr (global.get $init_state_adr))
;; $state_adr = $init_state_adr = 0
(loop $rounds
(v128.store
(local.get $state_adr)
(i64x2.add (global.get $i12) (local.get $ii))
)
(v128.store
(i32.add (local.get $state_adr) (i32.const 16))
(v128.store offset=16
(local.get $state_adr)
(i64x2.add (global.get $i34) (local.get $ii))
)

Expand All @@ -46,7 +46,7 @@
(br_if $rounds (i32.ne (global.get $max_rounds)))
)

(local.set $state_adr (global.get $alt_digest_init_adr))
(local.set $state_adr (i32.const 256)) ;; $alt_digest_init_adr = 256
(local.set $i (i32.const 0))
(loop $rounds
(i64.store (local.get $state_adr) (i64.add (i64.extend_i32_u (local.get $i)) (global.get $p5)))
Expand All @@ -67,7 +67,7 @@
(func (export "reset") (param $rounds i32) (param $state_adr i32)
(memory.copy
(local.get $state_adr)
(global.get $init_state_adr)
(i32.const 0) ;; $init_state_adr = 0
(i32.mul (local.get $rounds) (i32.const 32))
)
)
Expand All @@ -89,31 +89,28 @@
(i64x2.mul (global.get $p22))
(local.set $data01)

(v128.load (i32.add (local.get $pos) (i32.const 16)))
(v128.load offset=16 (local.get $pos))
(i64x2.mul (global.get $p22))
(local.set $data23)

(local.set $round_i (i32.const 0))
(local.set $state_cur (local.get $state_adr))
(loop $rounds
(v128.store
(local.get $state_cur)
(v128.store (local.get $state_cur)
(v128.load (local.get $state_cur))
(i64x2.add (local.get $data01))
(call $i64x2_rotl_31)
(i64x2.mul (global.get $p11))
)

(local.set $state_cur (i32.add (local.get $state_cur) (i32.const 16)))
(v128.store
(local.get $state_cur)
(v128.load (local.get $state_cur))
(v128.store offset=16 (local.get $state_cur)
(v128.load offset=16 (local.get $state_cur))
(i64x2.add (local.get $data23))
(call $i64x2_rotl_31)
(i64x2.mul (global.get $p11))
)

(local.set $state_cur (i32.add (local.get $state_cur) (i32.const 16)))
(local.set $state_cur (i32.add (local.get $state_cur) (i32.const 32)))
(local.tee $round_i (i32.add (local.get $round_i) (i32.const 1)))
(br_if $rounds (i32.ne (local.get $rounds)))
)
Expand All @@ -131,7 +128,7 @@
(local $state23 v128)

(local.set $state01 (v128.load (local.get $state_adr)))
(local.set $state23 (v128.load (i32.add (local.get $state_adr) (i32.const 16))))
(local.set $state23 (v128.load offset=16 (local.get $state_adr)))

(i64.rotl (i64x2.extract_lane 0 (local.get $state01)) (i64.const 1))
(i64.rotl (i64x2.extract_lane 1 (local.get $state01)) (i64.const 7))
Expand Down Expand Up @@ -199,7 +196,7 @@
(else
(memory.copy
(local.get $digest_adr)
(global.get $alt_digest_init_adr)
(i32.const 256) ;; $alt_digest_init_adr = 256
(i32.mul (local.get $rounds) (i32.const 8))
)
)
Expand Down