Skip to content

Commit

Permalink
Add serde_as regression test for pasta
Browse files Browse the repository at this point in the history
  • Loading branch information
volhovm committed Aug 22, 2024
1 parent 57b7f34 commit 7cd2acf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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();
(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:?}"
);
}
}

0 comments on commit 7cd2acf

Please sign in to comment.