From 87082373def3b41edea0312608e7c959d9981f1f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 28 Nov 2026 20:17:59 +0000 Subject: [PATCH 1/2] Update `AGENTS.md` for Blowfish, Twofish, ML-KEM, and wasm CI Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KmuCbukmnH9zJJPoxX3tGo --- AGENTS.md | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 741b8bb..f4e833a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -23,8 +23,9 @@ asymmetries between similar algorithms. Almost all of them are load-bearing. ## 1. Orientation -**What**: symmetric ciphers (AES in 9 modes, ChaCha20, XChaCha20, Salsa20, -XSalsa20, XOR) plus AEAD variants with Poly1305, and the ML-KEM post-quantum +**What**: symmetric ciphers (AES in 10 modes; Blowfish and Twofish in +ECB/CBC/CTR; ChaCha20, XChaCha20, Salsa20, XSalsa20, XOR) plus AEAD variants +with Poly1305, and the ML-KEM post-quantum key encapsulation mechanism (FIPS 203). Published to pub.dev as `cipherlib` (repo: `bitanon/cipherlib`, default branch `main`). @@ -54,8 +55,15 @@ lib/ .dart # family barrel: `export ... show X` + top-level fn core/cipher.dart # CipherBase, Cipher, StreamCipher, CipherPair, SaltedCipher core/aes.dart # AESCore: S-boxes, T-tables (GENERATED, see §4.F4), key expansion + core/blowfish.dart # BlowfishCore (P-array/S-box init tables GENERATED, §4.F4) + core/twofish.dart # TwofishCore (lookup tables GENERATED, §4.F4) + core/kem.dart # KEMBase, KEMKeyPair, KEMSecret + stream_extensions.dart # StreamCipherExtension (byte-level `.stream()`) algorithms/ # concrete implementations (chacha20.dart, salsa20.dart, ...) - aes_modes/ # one file per AES mode (cbc, ctr, gcm, ige, xts, ...) + aes_modes/ # one file per AES mode (cbc, ccm, cfb, ctr, ecb, gcm, ige, ofb, pcbc, xts) + blowfish_modes/ # cbc, ctr, ecb + twofish_modes/ # cbc, ctr, ecb + mlkem/ # MLKEM, K-PKE, arith.dart → arith_64bit/_32bit twins (conditional export) aead_cipher.dart # AEADCipher / AEADStreamCipher / AEADResult(+WithIV) padding.dart # Padding.none/zero/byte/ansi/pkcs7/pkcs5 utils/ # nonce.dart (Nonce/Nonce64/Nonce128), typed_data.dart, chunk_stream.dart @@ -63,13 +71,14 @@ test/ # one *_test.dart per algorithm + cross-cutting te fixtures/ # bulk KAT vectors as Dart data files test_integration/ # SEPARATE pub package; parity vs pointycastle & cryptography benchmark/ # hand-rolled harness (_base.dart) → generates BENCHMARK.md -example/ # 10 runnable examples; cipherlib_example.dart is embedded in README +example/ # 11 runnable examples; cipherlib_example.dart is embedded in README scripts/ # paired .sh/.bat: benchmark, coverage, compiled, globals -tool/aes_cache_gen.dart # generator for the AES T-tables in core/aes.dart +tool/_cache_gen.dart # generators for the tables in core/{aes,blowfish,twofish}.dart ``` Generated / never hand-edit: `build/`, `coverage/`, `doc/`, `.dart_tool/`, -the T-table block in `core/aes.dart`, and `BENCHMARK.md` (regenerate it). +the `Output from ...cache_gen.dart` table blocks in `core/aes.dart`, +`core/blowfish.dart` and `core/twofish.dart`, and `BENCHMARK.md` (regenerate it). **Public API surface** = what `lib/cipherlib.dart` reaches. New symbols become public in three steps: `algorithms/.dart` → family barrel @@ -90,18 +99,25 @@ Run from the repo root. | One test file | `dart test -p vm test/chacha20_test.dart` | | Tests matching a name | `dart test -p vm -N "pattern"` | | Web/JS tests (only when needed) | `dart test -p node` | +| dart2wasm tests (needs Chrome) | `dart test -P wasm` | | Integration parity suite | `cd test_integration && dart pub get && dart run main.dart` | | Coverage (LCOV+Cobertura+JUnit) | `sh scripts/coverage.sh` | | Benchmarks (regenerates md) | `sh scripts/benchmark.sh` (M3 Pro only, §4.F5) | | One compiled benchmark | `sh scripts/compiled.sh benchmark/.dart` | | Run the main example | `dart run example/cipherlib_example.dart` | -| Regenerate AES T-tables | `dart run tool/aes_cache_gen.dart` (prints to stdout) | +| Regenerate cipher tables | `dart run tool/{aes,blowfish,twofish}_cache_gen.dart` (prints to stdout) | | Package score (release only) | `dart pub global run pana --exit-code-threshold 0` | Notes: - `dart test` **without** `-p vm` runs the `node` platform too (slow, needs - Node.js). Default to `-p vm`; add `node` only for web-relevant changes. + Node.js). Default to `-p vm`; add `node` only for web-relevant changes — + always when touching `algorithms/mlkem/arith_*bit.dart` (platform twins). +- CI runs `dart test -P wasm` (dart2wasm on Chrome) on every push; the + AES-GCM counter bug of 0.7.2 only showed up there. Run it when touching + typed-data views or counter/carry code. +- Tests that only pass on the stable SDK get `@Tags(['stable-only'])`; CI + excludes them on 2.19 with `-x stable-only`. - `dart analyze` alone is NOT CI parity — CI uses `--fatal-infos`, so info-level findings fail the build. - Tests needing `dart:io` or other non-web APIs: tag with `@Tags(['vm-only'])` @@ -169,13 +185,16 @@ House idioms (copy them, don't improvise): - Block functions are hand-unrolled with 16 locals (`s0..s15`) and C-style loops with pre-declared `int i, j, n;`. Match this style in hot code; do not refactor unrolled rounds into loops "for readability". -- AES mode naming: `AESInMode` (the pair), `AESInModeEncrypt` / - `...Decrypt` (the sides), `...CipherCore` (bare engine). AEAD naming: +- Block-cipher mode naming: `InMode` (the pair), + `InModeEncrypt` / `...Decrypt` (the sides), or a single + `InModeCipher` when both directions are the same keystream + transform (CTR/CFB/OFB); `...CipherCore` is the shared engine of the AEAD + modes (GCM, CCM). Same scheme for `AES`, `Blowfish`, `Twofish`. AEAD naming: `Poly1305`, attached via `extension ExtensionForPoly1305 on `. - Top-level one-shot functions are lowercase-run-together: `chacha20`, - `xchacha20poly1305`. AES has none — its API is the builder - `AES(key).cbc(iv)`, `AES(key).gcm(iv)`, etc. + `xchacha20poly1305`. Block ciphers have none — their API is the builder + `AES(key).cbc(iv)`, `AES(key).gcm(iv)`, `Blowfish(key).ctr(iv)`, etc. - `name` getters follow the spec-string format: `"ChaCha20"`, `"AES/CBC/${padding.name}"`, `"XSalsa20/Poly1305"`. @@ -352,9 +371,10 @@ Do not change an existing error type or message — tests match them. - **F2. `dart analyze` passing ≠ CI passing.** Use `--fatal-infos`. - **F3. Plain `dart test` runs the node platform** — slow and environment- dependent. Use `-p vm` unless testing web behavior. -- **F4. The AES T-tables are generated.** Never hand-edit the constant blocks - under the `Output from 'dart run tool/aes_cache_gen.dart'` marker in - `core/aes.dart`; change the generator, re-run it, paste its stdout. +- **F4. The cipher tables are generated.** Never hand-edit the constant + blocks under the `Output from 'dart run tool/_cache_gen.dart'` + markers in `core/aes.dart`, `core/blowfish.dart` and `core/twofish.dart`; + change the generator, re-run it, paste its stdout. - **F5. Benchmark numbers are machine-locked.** `BENCHMARK.md` only carries numbers from the reference 36GB Apple M3 Pro (the footer says so). On any other machine: run for relative comparison, never commit. And BENCHMARK.md From 170a449d05c1ab634926a4f610ce65cf1c59db92 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 28 Nov 2026 20:26:05 +0000 Subject: [PATCH 2/2] Select ML-KEM 32-bit arithmetic via `dart.library.js_interop` Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KmuCbukmnH9zJJPoxX3tGo --- lib/src/algorithms/mlkem/arith.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/algorithms/mlkem/arith.dart b/lib/src/algorithms/mlkem/arith.dart index 967e078..9979c8d 100644 --- a/lib/src/algorithms/mlkem/arith.dart +++ b/lib/src/algorithms/mlkem/arith.dart @@ -1,4 +1,6 @@ // Copyright (c) 2026, Sudipto Chandra // All rights reserved. Check LICENSE file for details. -export 'arith_64bit.dart' if (dart.library.js) 'arith_32bit.dart'; +export 'arith_64bit.dart' + if (dart.library.js_interop) 'arith_32bit.dart' + if (dart.library.js) 'arith_32bit.dart';