Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add conditional support for serde #209

Merged

Conversation

hackaugusto
Copy link
Contributor

This adds conditional support for serde. It allow serializing/deserializing the field elements, which is useful for downstream applications, for instance, to save test data.

The change is behind a feature flag to avoid impacting compilation times.

@hackaugusto
Copy link
Contributor Author

hackaugusto commented Aug 8, 2023

Simple test for the output format:

use serde::{Deserialize, Serialize};
use serde_json::json;
use winter_math::fields;

#[derive(Serialize, Deserialize)]
struct Data {
    d0: fields::f64::BaseElement,
    d1: fields::f62::BaseElement,
    d2: fields::f128::BaseElement,
}

fn main() {
    let v = Data {
        d0: 1234123u64.into(),
        d1: 1234123u64.into(),
        d2: 1234123u64.into(),
    };

    println!(
        "{}",
        serde_json::to_string(&v).expect("json serialization failed")
    );
}

The code using into and from annotations outputs the values are converted from montgomery form to human readable numbers:

{"d0":1234123,"d1":1234123,"d2":1234123}

In contrast, without them:

{"d0":5300517923007285,"d1":1483409960899751059,"d2":1234123}

@hackaugusto hackaugusto force-pushed the hacka-conditional-support-for-serde branch from 0f59504 to d2c2d6d Compare August 8, 2023 17:55
@irakliyk irakliyk self-requested a review August 9, 2023 04:51
Copy link
Collaborator

@irakliyk irakliyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you!

@irakliyk irakliyk merged commit d97dc57 into facebook:main Aug 9, 2023
9 checks passed
@hackaugusto hackaugusto deleted the hacka-conditional-support-for-serde branch August 9, 2023 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants