|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is a C reference implementation of Mina Protocol's Schnorr signature scheme. It provides cryptographic operations for signing and verifying Mina blockchain transactions, including payments and stake delegations. |
| 8 | + |
| 9 | +## Build Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Build everything (reference_signer and unit_tests, then run tests) |
| 13 | +make |
| 14 | + |
| 15 | +# Build and run unit tests |
| 16 | +make unit_tests |
| 17 | + |
| 18 | +# Build the reference signer demo |
| 19 | +make reference_signer |
| 20 | + |
| 21 | +# Clean build artifacts |
| 22 | +make clean |
| 23 | +``` |
| 24 | + |
| 25 | +The Makefile automatically runs unit tests after building them. |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +### Cryptographic Primitives |
| 30 | + |
| 31 | +- **Elliptic Curve**: Pasta.Pallas curve (`y^2 = x^3 + 5`) from the Zcash Pasta curves |
| 32 | + - Field operations in `pasta_fp.c` (base field Fp) |
| 33 | + - Scalar operations in `pasta_fq.c` (scalar field Fq) |
| 34 | + - Uses fiat-crypto generated Montgomery form arithmetic |
| 35 | + |
| 36 | +- **Group Operations** (`crypto.c`): |
| 37 | + - Projective (Jacobian) coordinates internally for efficiency |
| 38 | + - Affine coordinates for external API |
| 39 | + - `group_add`, `group_dbl`, `group_scalar_mul` for internal ops |
| 40 | + - `affine_*` functions wrap group operations |
| 41 | + |
| 42 | +### Signature Scheme |
| 43 | + |
| 44 | +The signing follows Mina's Schnorr specification: |
| 45 | +1. `message_derive()` - Derives deterministic nonce `k` using Blake2b |
| 46 | +2. `message_hash()` - Hashes message with Poseidon for challenge `e` |
| 47 | +3. `sign()` - Produces signature `(rx, s)` where `s = k + e*sk` |
| 48 | +4. `verify()` - Verifies using `s*G - e*pk` and checking x-coordinate |
| 49 | + |
| 50 | +### Poseidon Hash (`poseidon.c`, `poseidon.h`) |
| 51 | + |
| 52 | +Two variants supported: |
| 53 | +- `POSEIDON_LEGACY` (0x00) - Original Mina parameters |
| 54 | +- `POSEIDON_KIMCHI` (0x01) - Updated kimchi version |
| 55 | + |
| 56 | +Parameters are in `poseidon_params_legacy.h` and `poseidon_params_kimchi.h`. |
| 57 | + |
| 58 | +### Transaction Structure |
| 59 | + |
| 60 | +`Transaction` struct in `crypto.h` contains: |
| 61 | +- Common fields: fee, fee_token, fee_payer_pk, nonce, valid_until, memo |
| 62 | +- Body fields: tag (payment/delegation), source_pk, receiver_pk, token_id, amount |
| 63 | + |
| 64 | +### Key Data Types |
| 65 | + |
| 66 | +- `Field` / `Scalar`: 4x uint64_t limbs (256-bit in Montgomery form) |
| 67 | +- `Affine`: Point as (x, y) field elements |
| 68 | +- `Group`: Point as (X, Y, Z) projective coordinates |
| 69 | +- `Compressed`: Point as x-coordinate + y parity bit |
| 70 | +- `ROInput`: Random oracle input for hashing (fields + packed bits) |
| 71 | + |
| 72 | +### Network IDs |
| 73 | + |
| 74 | +- `TESTNET_ID` (0x00) |
| 75 | +- `MAINNET_ID` (0x01) |
| 76 | +- `NULLNET_ID` (0xff) - For testing without network prefix |
| 77 | + |
| 78 | +## Key Files |
| 79 | + |
| 80 | +- `crypto.c/h` - Main cryptographic operations and type definitions |
| 81 | +- `unit_tests.c` - Comprehensive test suite with known test vectors |
| 82 | +- `reference_signer.c` - Demo showing payment and delegation signing |
| 83 | +- `curve_checks.c/h` - Generated elliptic curve verification tests |
| 84 | + |
| 85 | +## Development Guidelines |
| 86 | + |
| 87 | +### Commit Guidelines |
| 88 | + |
| 89 | +- **Never** add Claude as a co-author in commit messages |
| 90 | +- Avoid emoji usage in commit messages |
| 91 | +- Wrap commit titles and body text at 80 characters maximum |
| 92 | + |
| 93 | +### Code Style |
| 94 | + |
| 95 | +- Use `-Wall -Werror` flags (enforced by Makefile) |
| 96 | +- Follow existing naming conventions: `snake_case` for functions and variables |
| 97 | +- Montgomery form is used internally for field/scalar arithmetic |
| 98 | +- Always run `make unit_tests` before committing to verify changes |
0 commit comments