Serialization for Fe

One struct.
Every wire.

Fe Codec encodes your types as JSON, deterministic CBOR or a format you write, and hashes them as EIP-712 typed data. A generator written in Fe writes the serializers, and the same code runs in contracts, native programs and WebAssembly.

Install the generator (Linux x86-64/ARM64 · macOS Apple silicon)

curl -fsSL https://github.com/emeieiron/fe-codec/releases/latest/download/installer.sh | sh
Formats
JSON · CBOR · EIP-712 · yours
Targets
EVM · native · WebAssembly
Toolchain
Fe 26.7.0
order · inspect
src/models.feFe
#[serializable]pub struct Order {1    pub id: u64,2    pub buyer: Address,3    pub total: u256,4    pub status: OrderStatus,5    pub note: Option<Text<32>>,}

valueid: 7, buyer: 0xd8da…6045, total: 10¹⁸, status: Paid, note: "gift"

fe_codec_json119 bytes

{"id":7,"buyer":"0xd8da6bf26964af9d7eed9e03e53415d37aa96045","total":1000000000000000000,"status":"Paid","note":"gift"}

fe_codec_cbor43 bytes
a5map · 50107key 1 · uint 70254d8da6bf26964af9d7eed9e03e53415d37aa96045key 2 · bytes(20)031b0de0b6b3a7640000key 3 · uint640401key 4 · variant 1056467696674key 5 · text(4)
Encoded by fe_codec_json and fe_codec_cbor when this page was built.Hover or focus a field

§01 · What you can do

Describe a type once.
Use it everywhere it travels.

Encode once, in every format

The serializer never names a format. Switch ingots, or write code generic overFormat, and the same value comes out as text or bytes.

  • fe_codec_json::encode(order)119 bytes
  • fe_codec_cbor::encode(order)43 bytes
  • format.encode<Order, 192>(order)any Format

Sign typed data

Mark a struct #[eip712] and its type hash becomes a compile-time constant. Gas for the EIP's example mail, digest included:

  • Generated13,900
  • Hand-written14,600
  • Generic70,900

Same types, three targets

Contracts, native programs and WebAssembly share the models and the serializers.

EVMnativewasm

Property tests require the targets to agree on every encoded byte and every decode outcome.

Bring your own format

Implement Encoder, Decoder and Format againstfe_codec_core alone. Every serializable type works with it at once.

Writing a format →

Errors that point at the byte

Every error carries its kind, the offset and the field it was decoding.

UnknownVariantoffset 17field "status"

Change the schema without breaking data

Stable #[codec_id]s give fields their wire identity. Add new fields asOptions, and old data still decodes.

Schema evolution →
#[serializable]
pub struct Account {
    #[codec_id = 1]
    pub owner: u64,
    #[codec_id = 2]
    pub balance: u64,
    #[codec_id = 3]
    pub label: Option<Text<8>>,   // added later
}

§02 · How it works

Plain Fe in, plain Fe out.

The generator reads your declarations and writes ordinary Fe that implements the codec traits. There is no runtime reflection and no plugin to load: the compiler checks the result like any other module.

  1. Describe

    Mark structs and enums #[serializable]. They stay ordinary Fe types.

    #[serializable]
    pub struct Order {
        pub id: u64,
        pub buyer: Address,
        pub total: u256,
        pub status: OrderStatus,
        pub note: Option<Text<32>>,
    }
  2. Generate

    Write the impls beside your models, or let FeMP run the generator on each build.

    fe_codec_gen --source src/models.fe --out src/serializers.fe
    // Generated by fe_codec_gen. Do not edit.
    impl fe_codec_core::Serialize for Order {
        fn serialize<CodecEncoder: fe_codec_core::Encoder>(self, _ encoder: mut CodecEncoder) -> fe_codec_core::CodecResult<()> {
            var present: usize = 4
            if !CodecEncoder::OMIT_NONE || self.note.is_some() {
                present += 1
            }
            var record = encoder.begin_record<Self>(present)?
            encoder.field<Self>(mut record, 0)?
            self.id.serialize(mut encoder)?
            // … one visit per field, in ID order
            encoder.end_record(record)
        }
    }
  3. Use

    Encode and decode with any format. Bindings choose the type and the buffer.

    fn round_trip(_ order: Order) -> CodecResult<Order> {
        let bytes: BoundedBuffer<192> = fe_codec_json::encode(order)?
        let back: Order = fe_codec_json::decode(bytes)?
        Result::Ok(back)
    }

§03 · EIP-712

Typed data, hashed at compile time.

The generator computes each struct's encodeType and emits its type hash as acore::keccak constant. Hashing a struct is then one keccak_wordsover its members, as hand-written code would be.

#[serializable]
pub struct Person { pub name: Text<16>, pub wallet: Address }

#[serializable]
#[eip712]
pub struct Mail { pub from: Person, pub to: Person, pub contents: Text<32> }
let digest = domain.digest(mail.hash_struct())
let signer = std::evm::crypto::ecrecover(hash: digest, v, r, s)
Signing typed data →
encodeType
Mail(Person from,Person to,string contents)Person(string name,address wallet)
typeHash
0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2
hashStruct
0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e
digest
0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2

Computed by fe_codec_eip712 when this page was built: the EIP's own example values, through the generated and the generic path. The tests also check them against an independent transcription of the specification and viem.

§04 · Evidence

Proved, property-tested, example-tested.

Three kinds of evidence, kept apart because they are not interchangeable.

Proved

femp prove, for every input, on the EVM, natively and in WebAssembly

  • UTF-8 validation of a character follows RFC 3629 §4 exactly
  • The CBOR encoder's head for any 64-bit argument is the only head the decoder accepts
  • The 8–64-bit range checks accept exactly the values of their width

Property-tested

femp prop, with generated inputs on all three targets

  • Every integer width round-trips in JSON, CBOR and the compact format
  • No input of up to 64 arbitrary bytes makes a decoder panic
  • The targets agree on every decode outcome and every encoded byte

Example-tested

Golden vectors, conformance and the generator's own suites

  • Vectors from an independent reference encoder, and RFC 8949 Appendix A
  • A conformance check every format runs, with exact error kinds and offsets
  • Random models checked against the reference encoders and the EIP-712 specification

What is not established, such as proofs for whole encoders, is listed in the design's guarantees.

Start with one struct.