Skip to main content

Crate taceo_ark_serde_compat

Crate taceo_ark_serde_compat 

Source
Expand description

§ark-serde-compat

Various serialization helpers for serializing arkworks types using serde.

§Overview

This crate provides serde-compatible serialization and deserialization functions for arkworks-rs types, supporting both human-readable and non-human readable formats:

  • Human-readable formats (e.g., JSON): Field elements are serialized as decimal strings, and curve points are serialized as arrays of coordinate strings. This format is designed to work seamlessly with Circom’s JSON format expectations.
  • Non-human readable formats (e.g., bincode, CBOR): Uses ark-serialize with compressed mode for efficient binary serialization.

§Features

  • bn254: Enables serialization support for BN254 curve types
  • bls12-381: Enables serialization support for BLS12-381 curve types
  • babyjubjub: Enables serialization support for BabyJubJub curve types

None of the features is enabled by default.

§Usage

Use the provided functions with serde’s field attributes:

use serde::{Serialize, Deserialize};
use ark_bn254::{Fr, G1Affine};

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(serialize_with = "taceo_ark_serde_compat::bn254::serialize_fr")]
    #[serde(deserialize_with = "taceo_ark_serde_compat::bn254::deserialize_fr")]
    scalar: Fr,

    #[serde(serialize_with = "taceo_ark_serde_compat::bn254::serialize_g1")]
    #[serde(deserialize_with = "taceo_ark_serde_compat::bn254::deserialize_g1")]
    point: G1Affine,
}

§Serialization Formats

§Human-Readable Formats (JSON)

§Field Elements

Field elements (Fr, Fq) are serialized as decimal strings:

"12345678901234567890"
§BN254 G1 Points

G1 points are serialized in projective coordinates [x, y, z]:

["1", "2", "1"]

The point at infinity is represented as:

["0", "1", "0"]
§BN254 G2 Points

G2 points are serialized as [[x0, x1], [y0, y1], [z0, z1]]:

[["1", "2"], ["3", "4"], ["1", "0"]]
§BabyJubJub Points

BabyJubJub points are serialized in affine coordinates [x, y]:

["123", "456"]

§Non-Human Readable Formats (Binary)

For non-human readable serializers like bincode and CBOR, all arkworks types are serialized using ark-serialize in compressed mode. This provides efficient binary serialization that is significantly more compact than the JSON representation.

The same serde attributes work for both human-readable and non-human readable formats:

use serde::{Serialize, Deserialize};
use ark_bn254::Fr;

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(serialize_with = "taceo_ark_serde_compat::serialize_f")]
    #[serde(deserialize_with = "taceo_ark_serde_compat::deserialize_f")]
    field: Fr,
}

// Example usage
let my_struct = MyStruct { field: Fr::from(42u64) };

// Works with JSON
let json = serde_json::to_string(&my_struct)?;

// Also works with CBOR (uses compressed ark-serialize)
let mut b = Vec::new();
ciborium::into_writer(&my_struct, &mut b)?;

§License

See the repository LICENSE file for details.

Enums§

CheckElement
Indicates whether we should check if deserialized are valid points on the curves. No indicates to skip those checks, which is by orders of magnitude faster, but could potentially result in undefined behaviour. Use only with care.

Traits§

CanonicalJsonSerialize
Trait providing serialization for pairing-friendly elliptic curves.

Functions§

deserialize_f
Deserialize an unsigned prime field element. Returns an error if the value is negative or larger than modulus.
deserialize_f_array
Deserialize a fixed-size array of prime field elements.
deserialize_f_array_signed
Deserialize a fixed-size array of prime field elements. Allows negative values and is consistent with Circom’s negative value parsing. Will return an error if element is larger than modulus.
deserialize_f_seq
Deserialize a sequence of prime field elements.
deserialize_f_seq_signed
Deserialize a sequence of prime field elements. Allows negative values and is consistent with Circom’s negative value parsing. Will return an error if element is larger than modulus.
deserialize_f_signed
Deserialize a prime field element. Allows negative values and is consistent with Circom’s negative value parsing. Will return an error if element is larger than modulus.
deserialize_g1
Deserialize a G1 affine point with full validation.
deserialize_g2
Deserialize a G2 affine point with full validation.
deserialize_g1_seq
Deserialize a sequence of G1 affine points with full validation.
deserialize_g1_seq_unchecked
Deserialize a sequence of G1 affine points without validation.
deserialize_g1_unchecked
Deserialize a G1 affine point without validation.
deserialize_g2_unchecked
Deserialize a G2 affine point without validation.
deserialize_gt
Deserialize a target group (GT/Fq12) element.
serialize_f
Serialize a prime field element.
serialize_f_seq
Serialize a sequence of prime field elements.
serialize_g1
Serialize a G1 affine point.
serialize_g2
Serialize a G2 affine point.
serialize_g1_seq
Serialize a sequence of G1 affine points.
serialize_gt
Serialize a target group (GT/Fq12) element.