Skip to content

Commit

Permalink
Merge pull request #2476 from o1-labs/volhovm/add-regression-for-past…
Browse files Browse the repository at this point in the history
…a-serialization

Add serde_as regression test for pasta
  • Loading branch information
volhovm authored Aug 22, 2024
2 parents 57b7f34 + 34aa857 commit f3acf46
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
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: 0 additions & 1 deletion kimchi/src/tests/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ where
}

/// creates the indexes
#[must_use]
pub(crate) fn setup_with_custom_srs<F: FnMut(D<G::ScalarField>, usize) -> OpeningProof::SRS>(
mut self,
get_srs: F,
Expand Down
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
60 changes: 60 additions & 0 deletions utils/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,63 @@ 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 serialized_bytes =
rmp_serde::to_vec(&data_expected).expect("TestStruct could not be serialized");
(buf_written.as_mut_slice())
.write_all(&serialized_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).expect("Could not deseralize TestStruct");

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

0 comments on commit f3acf46

Please sign in to comment.