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:
@@ -15,25 +15,29 @@ exception Cbor_error of string
|
||||
|
||||
let write_head buf major v =
|
||||
let m = major lsl 5 in
|
||||
(* Width selection + big-endian byte emission via Int64, so the web targets
|
||||
compute identically to native: on js_of_ocaml [int] is 32-bit, so the
|
||||
literal 0x100000000 (2^32) truncates to 0 (sending small values to the
|
||||
8-byte branch) and [v lsr (8*i)] with i>=4 is shift-mod-32. Int64 has the
|
||||
full 64-bit width and well-defined shifts on every target. *)
|
||||
let v64 = Int64.of_int v in
|
||||
let put_be nbytes =
|
||||
for i = nbytes - 1 downto 0 do
|
||||
Buffer.add_char buf
|
||||
(Char.chr (Int64.to_int
|
||||
(Int64.logand (Int64.shift_right_logical v64 (8 * i)) 0xFFL)))
|
||||
done
|
||||
in
|
||||
if v < 24 then
|
||||
Buffer.add_char buf (Char.chr (m lor v))
|
||||
else if v < 0x100 then begin
|
||||
Buffer.add_char buf (Char.chr (m lor 24));
|
||||
Buffer.add_char buf (Char.chr v)
|
||||
Buffer.add_char buf (Char.chr (m lor 24)); put_be 1
|
||||
end else if v < 0x10000 then begin
|
||||
Buffer.add_char buf (Char.chr (m lor 25));
|
||||
Buffer.add_char buf (Char.chr ((v lsr 8) land 0xFF));
|
||||
Buffer.add_char buf (Char.chr (v land 0xFF))
|
||||
end else if v < 0x100000000 then begin
|
||||
Buffer.add_char buf (Char.chr (m lor 26));
|
||||
for i = 3 downto 0 do
|
||||
Buffer.add_char buf (Char.chr ((v lsr (8 * i)) land 0xFF))
|
||||
done
|
||||
Buffer.add_char buf (Char.chr (m lor 25)); put_be 2
|
||||
end else if Int64.compare v64 0x100000000L < 0 then begin
|
||||
Buffer.add_char buf (Char.chr (m lor 26)); put_be 4
|
||||
end else begin
|
||||
Buffer.add_char buf (Char.chr (m lor 27));
|
||||
for i = 7 downto 0 do
|
||||
Buffer.add_char buf (Char.chr ((v lsr (8 * i)) land 0xFF))
|
||||
done
|
||||
Buffer.add_char buf (Char.chr (m lor 27)); put_be 8
|
||||
end
|
||||
|
||||
(* dag-cbor map key order: shorter key first, then bytewise. *)
|
||||
|
||||
Reference in New Issue
Block a user