kernel: make the crypto/content-addressing stack actually WASM-safe (32-bit ints)
The kernel's sha2/cbor/cid/ed25519 modules were labelled 'WASM-safe' but assumed 63-bit native int. On the web targets — js_of_ocaml (32-bit int) and wasm_of_ocaml (31-bit int) — they truncated, producing wrong digests/CIDs and a Char.chr crash at kernel INIT (ed25519 precomputes sqrtm1 + base_point at module load, driving the base-2^26 bignum). This is why a freshly-built browser kernel crashed on boot while the stale committed artifact (older toolchain) still ran. Fixes (all verified bit-identical to the 63-bit native build, conformance 271/271): - sx_sha2: SHA-256 round words via Int32 (were native int + land 0xFFFFFFFF, which is a no-op on 31-bit and overflows the constants); both SHA-256/512 length-encoding via Int64 shifts (native "lsr 32" is shift-mod-32 on js, which leaked the length byte into a higher word). NIST vectors pass native/js/wasm. - sx_cbor: write_head width selection + byte emission via Int64 (the 0x100000000 literal truncated to 0 on js, sending small ints to the 8-byte branch; and "v lsr (8*i)" with i>=4 was shift-mod-32). - sx_cid: base32_lower keeps acc bounded to the unconsumed low bits (it grew 8 bits/byte and overflowed). cid_from_sx now matches native<->js exactly. - sx_ed25519: bignum mul accumulates in Int64 (26x26=52-bit products overflow); div_small running remainder in Int64 (rem<<26 ~= 2^34). This was the boot gate — the browser kernel now boots (SxKernel live, crypto-sha256 correct on js). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -68,15 +68,22 @@ let sub (a : bn) (b : bn) : bn =
|
||||
norm r
|
||||
|
||||
let mul (a : bn) (b : bn) : bn =
|
||||
(* Accumulate in Int64: a limb product is 26+26 = 52 bits, which overflows the
|
||||
web targets' int (32-bit js_of_ocaml / 31-bit wasm_of_ocaml). Int64 is a
|
||||
real 64-bit type on every target, so the carries are exact. *)
|
||||
let la = Array.length a and lb = Array.length b in
|
||||
let r = Array.make (la + lb) 0 in
|
||||
let maskL = Int64.of_int mask in
|
||||
for i = 0 to la - 1 do
|
||||
let carry = ref 0 in
|
||||
let carry = ref 0L in
|
||||
let ai = Int64.of_int a.(i) in
|
||||
for j = 0 to lb - 1 do
|
||||
let s = r.(i + j) + a.(i) * b.(j) + !carry in
|
||||
r.(i + j) <- s land mask; carry := s lsr bits
|
||||
let s = Int64.add (Int64.add (Int64.of_int r.(i + j))
|
||||
(Int64.mul ai (Int64.of_int b.(j)))) !carry in
|
||||
r.(i + j) <- Int64.to_int (Int64.logand s maskL);
|
||||
carry := Int64.shift_right_logical s bits
|
||||
done;
|
||||
r.(i + lb) <- r.(i + lb) + !carry
|
||||
r.(i + lb) <- r.(i + lb) + Int64.to_int !carry
|
||||
done;
|
||||
norm r
|
||||
|
||||
@@ -109,12 +116,16 @@ let bn_mod (a : bn) (m : bn) : bn =
|
||||
end
|
||||
|
||||
let div_small (a : bn) (d : int) : bn =
|
||||
(* [rem lsl bits] reaches ~2^34 (rem < d <= 256, bits = 26), past the web
|
||||
targets' int width — accumulate the running remainder in Int64. *)
|
||||
let la = Array.length a in
|
||||
let q = Array.make la 0 in
|
||||
let rem = ref 0 in
|
||||
let rem = ref 0L in
|
||||
let dL = Int64.of_int d in
|
||||
for i = la - 1 downto 0 do
|
||||
let cur = (!rem lsl bits) lor a.(i) in
|
||||
q.(i) <- cur / d; rem := cur mod d
|
||||
let cur = Int64.logor (Int64.shift_left !rem bits) (Int64.of_int a.(i)) in
|
||||
q.(i) <- Int64.to_int (Int64.div cur dL);
|
||||
rem := Int64.rem cur dL
|
||||
done;
|
||||
norm q
|
||||
|
||||
|
||||
Reference in New Issue
Block a user