add rand-bytes for cryptographically random tokens#519
Merged
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
a515121 to
97bcfcf
Compare
`rand-bytes n > t` returns n CSPRNG bytes from `getrandom`, encoded as base64url-no-pad. Distinct from `rnd` (seedable uniform float for simulations) and `rndn` (seedable Normal float): this is the path for JWT jti claims, CSRF tokens, session IDs, and nonces. Output is base64url-no-pad so it drops straight into headers, cookies, and query strings without further encoding. Encoded length is deterministic: ceil(n * 4 / 3) chars. Tree-bridge eligible (arity 1, no FnRef, no I/O wrap) - VM and Cranelift JIT inherit at zero opcode cost. Cap at 1 MiB; negative or non-finite n surfaces as ILO-R009. CSPRNG output is never seeded. The base64url-no-pad encoder is hand-rolled (no `base64` dep yet on main); when the crypto-primitives branch lands it can fold into the shared encoder without changing rand-bytes semantics.
97bcfcf to
714c9e8
Compare
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
rand-bytes n > t, a CSPRNG-backed bytes builtin distinct fromrnd/rndn. Output isncryptographically random bytes fromgetrandom, encoded as base64url-no-pad text. Targets the JWTjti/ CSRF token / session ID / nonce class of use cases, hit by the jwt-signer-and-verifier persona (pending #5ad).rndandrndnare seedable uniform / Normal floats - for simulations and Monte Carlo, NOT tokens.rand-bytesis the cryptographic path: platform CSPRNG, never seeded.=padding) so it drops straight into headers, cookies, and query strings.ceil(n * 4 / 3)chars.len (rand-bytes 16) == 22always.Repro before / after
Before this PR, agents reaching for a jti / CSRF token had three bad options:
rnd(seedable, wrong tool), build it fromrnd a b+chr(slow, biased, allocs), or shell out torun "openssl" ["rand" "-base64" "16"](heavy, fork cost, format mismatch).After:
What's in the diff
src/builtins.rs-Builtin::RandBytesvariant +from_name+name+ appended toALL(preserves on-wire tags).src/interpreter/mod.rs- tree-walker dispatch,eval_rand_bytes(#[inline(never)]from the start), and a hand-rolledb64url_no_pad_encode(nobase64dep needed on main; folds into the shared encoder when the crypto-primitives branch lands).src/verify.rs- signature("rand-bytes", &["n"], "t").src/vm/mod.rs- tree-bridge eligibility(Builtin::RandBytes, 1). VM and JIT inherit; no new opcodes.Cargo.toml-getrandom = "0.2"(small, std-adjacent, already in transitive deps).tests/regression_rand_bytes.rs(11 cross-engine integration tests) + 12 unit tests insrc/interpreter/mod.rspinning the encoder against canonical RFC 4648 §5 vectors (b"hello" -> "aGVsbG8",[0xfb, 0xff, 0xbf] -> "-_-_") and the err paths.examples/rand-bytes.iloexercised by the engine harness across VM + JIT.SPEC.md,ai.txt(regenerated),skills/ilo/SKILL.md,skills/ilo/ilo-builtins-math.md. Site docs updated in a parallel commit on/Users/dan/code/ilo-lang/site(crypto.md adds a Cryptographic random section).Test plan
cargo build --release --features craneliftcleancargo test --release --features cranelift- 7488 pass / 0 fail (was 7476; +12 unit tests, +11 regression, +1 example harness entry already counted)cargo fmt --checkcleancargo clippy --release --features cranelift --all-targets -- -D warningscleanrand-bytes 16+,/,=)nandn > 1 MiBcap surface as ILO-R009 on VM (JIT swallows tree-bridge errors as nil per known limitation - documented inline)rand-bytes.iloacross every engine and matches-- out:markersFollow-ups
b64u/b64u-dec/sha256/hmac-sha256) lands, foldb64url_no_pad_encodeinto the shared encoder.