From 3279e322eb9239e7f6e98f0abb9421e4e7f37c25 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Wed, 9 Jan 2019 20:34:17 +0200 Subject: [PATCH] update to a new version of ff crate with serde support --- Cargo.toml | 12 +++++++++--- src/bn256/fr.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d3e885..fb62b8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,11 +2,12 @@ name = "pairing" # Remember to change version string in README.md. -version = "0.15.0" +version = "0.15.1" authors = [ "Sean Bowe ", "Jack Grigg ", - "Alex Vlasov " + "Alex Vlasov ", + "Alex Gluchowski " ] license = "MIT/Apache-2.0" @@ -18,7 +19,12 @@ repository = "https://github.com/matterinc/pairing" [dependencies] rand = "0.4" byteorder = "1" -ff = { version = "0.4", features = ["derive"] } +ff = { git = 'https://github.com/matterinc/ff', features = ["derive"] } +#ff = { path = "../ff", features = ["derive"] } +serde = "1.0.80" +serde_derive = "1.0.80" +serde_json = "1.0.33" +hex = "0.3.2" [features] unstable-features = ["expose-arith"] diff --git a/src/bn256/fr.rs b/src/bn256/fr.rs index e713ac6..97715d8 100644 --- a/src/bn256/fr.rs +++ b/src/bn256/fr.rs @@ -5,7 +5,49 @@ use ff::{Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr}; #[PrimeFieldGenerator = "7"] pub struct Fr(FrRepr); +#[test] +fn test_to_hex() { + assert_eq!(Fr::one().to_hex(), "0000000000000000000000000000000000000000000000000000000000000001"); +} + +#[test] +fn test_fr_from_hex() { + let fr = Fr::from_hex("0000000000000000000000000000000000000000000000000000000000000001").unwrap(); + assert_eq!(fr, Fr::one()); + + let fr = Fr::from_hex("0x0000000000000000000000000000000000000000000000000000000000000001").unwrap(); + assert_eq!(fr, Fr::one()); + + let fr = Fr::from_hex("0x01").unwrap(); + assert_eq!(fr, Fr::one()); + + let fr = Fr::from_hex("0x00").unwrap(); + assert_eq!(fr, Fr::zero()); + + let fr = Fr::from_hex("00").unwrap(); + assert_eq!(fr, Fr::zero()); +} + +#[test] +fn test_fr_serialize() { + assert_eq!( + serde_json::to_string(&Fr::one()).unwrap(), + r#""0x0000000000000000000000000000000000000000000000000000000000000001""#); +} + +#[test] +fn test_fr_deserialize() { + let json = r#""0x0000000000000000000000000000000000000000000000000000000000000001""#; + let fr: Fr = serde_json::from_str(json).unwrap(); + assert_eq!(fr, Fr::one()); +} + #[test] fn test_roots_of_unity() { assert_eq!(Fr::S, 28); +} + +#[test] +fn test_default() { + assert_eq!(Fr::default(), Fr::zero()); } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 4d2051f..96f3fa0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,11 @@ extern crate byteorder; extern crate ff; extern crate rand; +extern crate hex; +extern crate serde; +#[macro_use] +extern crate serde_derive; + #[cfg(test)] pub mod tests;