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

Add serde_as regression test for pasta #2476

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hex = { version = "0.4", features = ["serde"] }
num-bigint = { version = "0.4.3", features = ["rand"]}
num-integer = "0.1.45"
num-traits = "0.2"
rmp-serde = "1.1.1"
sha2 = "0.10.2"
thiserror = "1.0.30"
rand = "0.8.0"
Expand Down
58 changes: 58 additions & 0 deletions utils/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,61 @@ where
T::deserialize(&mut &bytes[..]).map_err(serde::de::Error::custom)
}
}

#[cfg(test)]
mod tests {

use ark_ec::AffineCurve;
use ark_serialize::Write;
use mina_curves::pasta::{Pallas, Vesta};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::io::BufReader;

#[test]
pub fn serde_as_regression_pasta() {
#[serde_as]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
struct TestStruct {
#[serde_as(as = "crate::serialization::SerdeAs")]
pallas: Pallas,
#[serde_as(as = "crate::serialization::SerdeAs")]
vesta: Vesta,
}

let data_expected = TestStruct {
pallas: Pallas::prime_subgroup_generator(),
vesta: Vesta::prime_subgroup_generator(),
};

// reference serialized value
let buf_expected: Vec<u8> = vec![
146, 196, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

let mut buf_written: Vec<u8> = vec![0; buf_expected.len()];

let srs_bytes = rmp_serde::to_vec(&data_expected).unwrap();

Choose a reason for hiding this comment

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

Nit pick: don't use unwrap() but expects. Generally it's easier to find what is wrong if you have more than one unwrap() in the test

(buf_written.as_mut_slice())
.write_all(&srs_bytes)
.expect("failed to write file");
(buf_written.as_mut_slice())
.flush()
.expect("failed to flush file");

assert!(
buf_written == buf_expected,
"Serialized (written) representation {buf_written:?} does not match the expected one {buf_expected:?}"
);

let reader = BufReader::new(buf_expected.as_slice());
let data_read: TestStruct = rmp_serde::from_read(reader).unwrap();

assert!(
data_read == data_expected,
"Deserialized value {data_read:?} does not match the expected one {data_expected:?}"
);
}
}
Loading