Inspiration

A master key is a small piece of data with a large responsibility. I wanted to understand what it takes to protect one beyond making its bytes look random: how the encryption reverses, how modified records are rejected, and where sensitive intermediate values can still leak.

That led to Prime-Field Feistel Cipher (PFFC), an experimental Rust cipher built around a construction whose equations, implementation, and weaknesses can all be inspected. The project focuses on cryptography; application-specific integrations are future work.

The problem

An encryption algorithm must do more than pass a roundtrip test. It needs a precise transformation, a defined input format, a clear response to tampering, and an honest account of its security assumptions.

PFFC investigates encrypting an existing 32-byte master value under a separate random 32-byte wrapping key. It also explores arithmetic sharing inside the round computation and checks before recovered bytes are released. It does not solve storage of the wrapping key or derive it from a password.

What it does

PFFC applies a custom 32-round vector Feistel composition over the prime field with 257 elements. The result is an 88-byte record:

Component Size
Format header 4 bytes
Public nonce 16 bytes
Encrypted field state 36 bytes
Authentication tag 32 bytes

Opening first validates the public format and authentication tag. It then decrypts, re-encrypts the recovered field state with fresh masks, compares the complete body, and checks that the recovered words are valid bytes before releasing them.

The local demo shows a generated master value, its encrypted record, successful recovery, and rejection after a ciphertext bit is changed. All displayed values are public synthetic fixtures.

The mathematics

Embed the input bytes in the field modulo 257. Add input-whitening values, then split the state into two 16-element vectors, L and R. Define the Cauchy matrix

$$ C_{ij}=(i-16-j)^{-1}\pmod{257},\qquad 0\leq i,j<16. $$

For round r, use this matrix when r is even and its transpose when r is odd. With round key k_r, compute

$$ F_r(R)=C_r(R+k_r)^{\circ 3},\qquad (L,R)\longleftarrow(R,L+F_r(R)). $$

The exponent denotes componentwise cubing. After 32 rounds, add output-whitening values. A round reverses exactly as

$$ (L_{\mathrm{old}},R_{\mathrm{old}})= (R_{\mathrm{new}}-F_r(L_{\mathrm{new}}),L_{\mathrm{new}}). $$

The prime field makes the arithmetic explicit, while the Cauchy matrix provides a linear mixing layer. The inverse establishes correctness, not confidentiality. The 32-round count is a provisional research parameter with no established security margin.

The review also found that the alternating matrices are equivalent, after public coordinate changes and corresponding key/whitening changes, to one fixed symmetric matrix. That simplification is documented rather than treating alternation as a security or originality claim.

How I built it

Rust implements both a direct reference and a version whose round values use two additive arithmetic shares. The shared implementation refreshes both operand encodings before each multiplication, including squaring. Tests compare the two implementations while varying the masks.

SHAKE256 expands the wrapping key and nonce into round and whitening values. HMAC-SHA3-256 authenticates the stored record. These are attributed existing supporting primitives; the supplied plaintext is transformed by the specified Feistel core.

The parser enforces exact lengths and canonical field values. Unsafe Rust is forbidden in the package, and designated sensitive buffers use best-effort zeroization. A separate Python implementation checks the complete ciphertext vector using different arithmetic and packing code. Cargo pins dependencies, and the evidence includes public seeds, raw observations, build metadata, and file hashes.

Development and documentation were AI-assisted. The separate reference implementation and internal review are not an independent external security certification.

Challenges

  • The byte/field boundary: The field contains 256 as well as every byte value. Ciphertext words need nine bits, and recovery must check the byte domain before conversion.
  • Masking correctly: Arithmetic can recombine correctly while an intermediate value still depends on the secret. A negative control demonstrates why reusing the same share encoding during squaring is problematic.
  • Learning from failures: Earlier toy lattice-encryption and key-encapsulation studies exposed limitations and remain labeled comparison controls. They are separate from PFFC's current encryption core.
  • Separating correctness from security: Successful recovery, authentication, masking, and resistance to cryptanalysis require different evidence.

Accomplishments

The audited 0.3.3 source passes 52 tests in debug and release builds, including 18 C2-specific tests. Clippy and formatting checks pass. The Python reference matches the complete 88-byte ciphertext vector, and the recorded evidence reproduces byte for byte.

The C2 evaluation with public seed 7 and 16 generated records reports:

Check Recorded result
Master-value recovery 16 / 16
Reference/shared comparisons 80 / 80
Selected record mutations rejected 1,408 / 1,408
Tested wrong wrapping keys rejected 16 / 16
Tested malformed lengths rejected 176 / 176

The mutation experiment flips bit zero in each of 88 bytes across 16 records; format checks or HMAC reject those changes. It is not exhaustive adversarial coverage or evidence of the custom cipher's confidentiality.

The attack study also includes an exhaustive local cube-difference calculation, reduced-round integral patterns, and an exact masking-intermediate counterexample. Reporting weaknesses is part of the result.

What I learned

A working cipher is not automatically a secure cipher. Reproducible negative controls, precise assumptions, and explicit limitations are essential. Authentication can reject a modified record without proving the confidentiality of the encryption core, and sharing the round state does not protect every part of a program.

Current limits and what's next

PFFC is a working research prototype, not a production key vault. Full-round confidentiality, an evidence-based round count, novelty, and complete side-channel protection remain unestablished. Key expansion, authentication, reconstruction, and other boundaries still process unshared values. No compiled timing or physical leakage evaluation establishes end-to-end protection.

The construction has no supplied reduction to LWE or another established hard problem. Feistel networks, field cubing, Cauchy diffusion, whitening, sharing, SHAKE, and HMAC have prior art. Whether the exact composition satisfies the competition's originality rule remains unconfirmed. The fault argument assumes the schedule, re-encryption, checks, and release remain correct; it does not cover common-mode faults or bypassed checks.

Next steps are stronger integral, algebraic, differential, and linear analysis; a justified round count; a compositional leakage model; and evaluation of generated machine code and the physical channel actually claimed.

Try it and review the work

With Rust/Cargo and Python 3 installed:

git clone https://github.com/jameslrivera/prime-field-feistel-cipher.git
cd prime-field-feistel-cipher
cargo build --locked
cargo run --release --offline -- demo-c2 --seed 7 --out review-evidence
cargo run --release --offline -- evaluate-c2 --seed 7 --trials 16 --out review-evidence
python3 scripts/c2_reference.py --check review-evidence/c2-report.json
cargo test --locked --offline

The first build may download dependencies. Open review-evidence/c2-report.md for readable results and its JSON companion for the observations. No account or external API is needed for the demo. All demo keys and values are deliberately public test data.

Related research: MiMC, Feistel Structures for MPC, and More / GMiMC, private-circuit masking, FIPS 202, and RFC 2104.

Built With

Share this project:

Updates

Submission history