SIMD128.md
The optional simd128 feature replaces only the polynomial arithmetic leaf.
It uses existing IC Wasm SIMD128 operations and needs no runtime, SDK or host
changes. Default builds retain the scalar implementation. Native tests execute
an exact lane model; they do not establish actual Wasm execution or instruction
savings. Provenance pins the primary sources and licenses.
The implementation follows FIPS 203 Algorithms 9--12 and the standard Kyber
Montgomery quotient (p-t*q)/2^16, with signed low16 t=p*QINV and QINV=-3327.
Public twiddles include a factor of R, and arbitrary canonical factors use the
standard R²=1353 conversion. The inverse's final factor is 512=3303*R mod3329.
Every coefficient is canonical before storage, so key encodings, ciphertexts,
prepared contexts and all validation rules remain byte-exact.
Eight 16-bit coefficients occupy each vector. Signed low/high widening products compute full 32-bit products; the reduction uses signed subtraction and shift, then signed saturation narrowing. Narrowing cannot saturate: both factors are in [0,3329), signed t is in [-32768,32767], the 32-bit difference cannot overflow, and the reduced quotient is in (-3329,3329). A mask-based addition makes it canonical. Modular addition/subtraction also remain within signed16 bounds. All indices, loop counts, shuffles and backend selection depend only on public constants or the compile-time feature. There is no secret-dependent branch.
All seven forward/inverse stages are vectorized. The length4/2 stages pack two or four independent butterfly groups with fixed shuffles. Pointwise products pack four degree-one polynomial products per vector and preserve the existing accumulation semantics. The implementation introduces no heap allocation and adds 2048 bytes of public twiddle tables, with bounded vector temporaries; actual compiler spills and peak Wasm stack/pages remain unmeasured.
Decapsulation still performs the full standard decrypt, G/J hashing, re-encryption, constant-time comparison and selection, including for corrupted ciphertexts. No ciphertext validity result, skip or successful-operation cache was added. The shared runtime and the key-import/memory contracts are unchanged.
Validation includes all120 NIST cases, encoded and expanded keys, comparison against the untouched registry implementation, arbitrary/corrupted ciphertexts, all3329² canonical Montgomery operand pairs, 516 basis/random forward/inverse polynomials and256 random full polynomial accumulations. The separate benchmark leaf delegates to the established complete cohort fixture, retaining and checking every output at actual100 operations. It separates independent checked key imports from one key imported and fully expanded inside the same call. The native checker runs both input populations and all decapsulation patterns twice.
Root integration is deliberately separate: register mlkem_simd::cases() from
benches/crypto/cases/src/mlkem_simd.rs and build with
--features cdk-rsx-crypto-mlkem/simd128. Run a matched scalar build from the same
source without that feature. The leaf rejects setup if the feature is absent.
Preparation, context drops, complete endpoint costs and all page charges belong
to the actual IC comparison. Browser/native speedups reported by the source
papers are not IC gas measurements.
The separately selectable simd128-q15 feature uses the same Montgomery
quotient with existing i16x8.q15mulr_sat_s operations. It implies simd128 and
uses the same inputs, tables, store bounds and benchmark leaf. Its proof is a
translation identity, not an approximate quotient. Put p=ab, take the signed
low16 t=pQINV, and let s=tq. The positive modular inverse ensures p-s=k65536.
Neither p nor s can saturate the Q15 operation: |p|<11,075,585 and |s|<=109,084,672,
well inside the Q15 saturation threshold. Therefore
Q15(a,b) - Q15(t,q)
= floor((p+16384)/32768) - floor((s+16384)/32768)
= 2*k.
This even difference lies in (-2q,2q), so signed16 subtraction cannot overflow; arithmetic shift by1 recovers exactly k. Canonical correction is unchanged. Both rounded terms are subtracted with the positive inverse; the odd-factor condition in the paper's negative-inverse/addition variant does not apply to this identity. The construction follows the doubled Montgomery view in Neon NTT, with rounding canceled explicitly. All3329² canonical operand pairs are checked against ordinary integer modular arithmetic independently for this feature as well.
Using the pinned IC catalog, the quotient and canonical correction require a
source-level20 vector gas units with widening products, versus10 with the Q15
mapping, excluding loads, public constants and loop/control instructions. These
are opcode-accounting estimates; compiler output and whole-call instruction/page
savings must be established by actual canonical Wasm qualification. No relaxed
SIMD instruction is used. Select it with
--features cdk-rsx-crypto-mlkem/simd128-q15 for a separate matched artifact.
The SIMD benchmark leaf now runs an independent official-vector gate before
ordinary fixture preparation. It checks every output byte through fixed SHA256
commitments generated offline from the pinned NIST files, without executing the
candidate arithmetic during generation. The target checks official keygen public
and secret encodings, encapsulation ciphertext/shared key, valid decapsulation
and a published modified-ciphertext rejection key for both raw and expanded APIs.
The generator independently confirms each rejection key as Python SHAKE256
J(z || c). Selected cases and hashes are recorded in
KAT-PROVENANCE.json.
The eight selected official cases use10304 bytes of static inputs/commitments;
SHA256 itself first checks the standard abc digest. These commitments cover full
encodings and all32 shared-key bytes; they are cryptographic commitments rather
than stored copies of every expected output byte.
The gate also rejects noncanonical public/secret coefficients. Every admitted
SIMD fixture reruns its parameter set's gate; no successful gate, key or expanded
context is cached between calls. Gate work and its contexts finish before the
ordinary fixture is created. Operation::run, logical work and retained-output
checks are unchanged. Target qualification must archive successful setup and
report the gate's setup instruction/page costs separately from the N100 body;
old artifact receipts remain frozen. The generator's --check validates both
the10304-byte payload and its source/case metadata without modifying them.