|
| 1 | +# AGENTS.md - AI Coding Agent Guidelines |
| 2 | + |
| 3 | +Guidelines for AI coding agents working on the fiber-sphinx codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +fiber-sphinx is a Rust implementation of the Sphinx mix network protocol for Fiber. |
| 8 | +It implements onion message encryption/decryption for anonymous message routing. |
| 9 | + |
| 10 | +## Build, Test, and Lint Commands |
| 11 | + |
| 12 | +### Prerequisites |
| 13 | +- Rust toolchain 1.76.0 (specified in `rust-toolchain.toml`) |
| 14 | +- Components: rustfmt, clippy |
| 15 | + |
| 16 | +### Build |
| 17 | +```bash |
| 18 | +cargo build # Build the project |
| 19 | +RUSTFLAGS="-Dwarnings" cargo build # Build with warnings as errors (CI mode) |
| 20 | +``` |
| 21 | + |
| 22 | +### Test |
| 23 | +```bash |
| 24 | +cargo test # Run all tests |
| 25 | +cargo test test_onion_packet_from_bytes # Run a specific test by name |
| 26 | +cargo test test_derive # Run tests matching a pattern |
| 27 | +cargo test test_create_onion_packet -- --nocapture # Run with output visible |
| 28 | +``` |
| 29 | + |
| 30 | +### Lint and Format |
| 31 | +```bash |
| 32 | +cargo fmt # Format code (required before committing) |
| 33 | +cargo fmt --check # Check formatting without changes |
| 34 | +cargo clippy # Run clippy linter |
| 35 | +RUSTFLAGS="-Dwarnings" cargo clippy # Clippy with warnings as errors (CI mode) |
| 36 | +``` |
| 37 | + |
| 38 | +### Documentation |
| 39 | +```bash |
| 40 | +cargo doc --open # Generate and open documentation |
| 41 | +``` |
| 42 | + |
| 43 | +## Code Style Guidelines |
| 44 | + |
| 45 | +### Rust Edition and Version |
| 46 | +- **Edition**: 2021 |
| 47 | +- **Minimum Rust Version**: 1.76.0 |
| 48 | + |
| 49 | +### Formatting |
| 50 | +- Always run `cargo fmt` before committing |
| 51 | +- Use rustfmt defaults (no custom configuration) |
| 52 | + |
| 53 | +### Imports |
| 54 | +- Group: 1) Standard library 2) External crates (alphabetically) 3) Internal modules |
| 55 | +- Use specific imports, not glob imports |
| 56 | +- Nested imports are acceptable: `use secp256k1::{PublicKey, SecretKey}` |
| 57 | + |
| 58 | +### Naming Conventions |
| 59 | +- **Types/Structs/Enums**: PascalCase (`OnionPacket`, `SphinxError`) |
| 60 | +- **Functions/Methods**: snake_case (`derive_key`, `peel`) |
| 61 | +- **Constants**: SCREAMING_SNAKE_CASE (`HMAC_KEY_RHO`, `CHACHA_NONCE`) |
| 62 | +- **Variables**: snake_case (`session_key`, `hops_path`) |
| 63 | +- **Generic Type Parameters**: Single uppercase letters (`C`, `F`, `T`) |
| 64 | + |
| 65 | +### Type Annotations |
| 66 | +- Use explicit types for public APIs |
| 67 | +- Use `[u8; 32]` for fixed-size byte arrays (keys, HMACs) |
| 68 | +- Use `Vec<u8>` for variable-length byte data |
| 69 | + |
| 70 | +### Error Handling |
| 71 | +- Use `thiserror` for error definitions |
| 72 | +- Define errors with `#[derive(Error, Debug, Eq, PartialEq)]` |
| 73 | +- Return `Result<T, SphinxError>` for fallible operations |
| 74 | +- Use `Option<T>` for values that may not exist |
| 75 | + |
| 76 | +Example: |
| 77 | +```rust |
| 78 | +#[derive(Error, Debug, Eq, PartialEq)] |
| 79 | +pub enum SphinxError { |
| 80 | + #[error("The hops path does not match the hops data length")] |
| 81 | + HopsLenMismatch, |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Documentation |
| 86 | +- Use `//!` for module-level documentation |
| 87 | +- Use `///` for function/struct documentation |
| 88 | +- Include code examples in doc comments |
| 89 | +- Document public APIs thoroughly |
| 90 | + |
| 91 | +### Function Patterns |
| 92 | +- Use `&self` for methods that don't consume the struct |
| 93 | +- Use `self` for consuming transforms (`into_bytes`, `peel`) |
| 94 | +- Use generic constraints: `C: Signing` or `C: Verification` |
| 95 | + |
| 96 | +### Testing |
| 97 | +- Tests in `src/tests.rs` module |
| 98 | +- Naming: `test_<function_name>` or `test_<feature_description>` |
| 99 | +- Use `hex-conservative` for hex encoding/decoding |
| 100 | +- Test both success and error cases |
| 101 | + |
| 102 | +### Cryptographic Patterns |
| 103 | +- Use `Secp256k1::new()` for secp256k1 context |
| 104 | +- Use `SharedSecret::new()` for ECDH operations |
| 105 | +- Use ChaCha20 with 12-byte zero nonce: `[0u8; 12]` |
| 106 | +- Use HMAC-SHA256 for key derivation and MAC computation |
| 107 | + |
| 108 | +### Memory and Safety |
| 109 | +- Use `expect()` with descriptive messages for infallible operations |
| 110 | +- Avoid `unwrap()` in library code; use `?` or proper error handling |
| 111 | +- Use `#[inline]` for small, frequently-called functions |
| 112 | + |
| 113 | +## Project Structure |
| 114 | + |
| 115 | +``` |
| 116 | +fiber-sphinx/ |
| 117 | +├── Cargo.toml # Project manifest and dependencies |
| 118 | +├── rust-toolchain.toml # Rust version specification |
| 119 | +├── src/ |
| 120 | +│ ├── lib.rs # Main library code and public API |
| 121 | +│ └── tests.rs # Test module |
| 122 | +├── docs/ |
| 123 | +│ ├── spec.md # Protocol specification |
| 124 | +│ ├── development.md # Development guide |
| 125 | +│ └── CONTRIBUTING.md # Contribution guidelines |
| 126 | +└── .github/workflows/ |
| 127 | + ├── ci.yml # CI workflow (build, test, clippy) |
| 128 | + └── cov.yml # Coverage workflow |
| 129 | +``` |
| 130 | + |
| 131 | +## CI Requirements |
| 132 | + |
| 133 | +The CI runs on Rust 1.76.0, stable, beta, and nightly. All must pass: |
| 134 | +- `cargo build` - Compilation with no errors |
| 135 | +- `cargo test` - All tests pass |
| 136 | +- `cargo clippy` - No warnings (with `-Dwarnings`) |
| 137 | + |
| 138 | +## Contributing Workflow |
| 139 | + |
| 140 | +1. Create branch from `develop` |
| 141 | +2. Add tests for new code |
| 142 | +3. Update documentation for API changes |
| 143 | +4. Ensure tests pass and code lints |
| 144 | +5. Run `cargo fmt` before committing |
| 145 | + |
| 146 | +## Dependencies |
| 147 | + |
| 148 | +### Runtime |
| 149 | +- `secp256k1` (0.28.0) - Elliptic curve cryptography |
| 150 | +- `sha2` (0.10.8) - SHA-256 hashing |
| 151 | +- `hmac` (0.12.1) - HMAC implementation |
| 152 | +- `chacha20` (0.9.1) - ChaCha20 stream cipher |
| 153 | +- `thiserror` (1.0) - Error derive macro |
| 154 | + |
| 155 | +### Dev |
| 156 | +- `hex-conservative` (0.2.1) - Hex encoding/decoding for tests |
0 commit comments