Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

Expand Down Expand Up @@ -54,22 +55,30 @@ lib/
<family>.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
test/ # one *_test.dart per algorithm + cross-cutting tests
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/<cipher>_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/<x>.dart` → family barrel
Expand All @@ -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/<file>.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'])`
Expand Down Expand Up @@ -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: `AESIn<MODE>Mode` (the pair), `AESIn<MODE>ModeEncrypt` /
`...Decrypt` (the sides), `...CipherCore` (bare engine). AEAD naming:
- Block-cipher mode naming: `<Cipher>In<MODE>Mode` (the pair),
`<Cipher>In<MODE>ModeEncrypt` / `...Decrypt` (the sides), or a single
`<Cipher>In<MODE>ModeCipher` 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:
`<Cipher>Poly1305`, attached via
`extension <Cipher>ExtensionForPoly1305 on <Cipher>`.
- 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"`.

Expand Down Expand Up @@ -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/<cipher>_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
Expand Down
4 changes: 3 additions & 1 deletion lib/src/algorithms/mlkem/arith.dart
Original file line number Diff line number Diff line change
@@ -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';
Loading