Skip to content

Commit

Permalink
Implement serde traits for Fq2, Fq6, Fq12.
Browse files Browse the repository at this point in the history
  • Loading branch information
afck committed Aug 24, 2018
1 parent 54d8735 commit b0f3a88
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repository = "https://github.com/ebfull/pairing"
rand = "0.4"
byteorder = "1"
ff = { version = "0.4", features = ["derive"] }
serde = { version = "1.0", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
unstable-features = ["expose-arith"]
Expand Down
1 change: 1 addition & 0 deletions src/bls12_381/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rand::{Rand, Rng};

/// An element of Fq12, represented by c0 + c1 * w.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Fq12 {
pub c0: Fq6,
pub c1: Fq6,
Expand Down
1 change: 1 addition & 0 deletions src/bls12_381/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::cmp::Ordering;

/// An element of Fq2, represented by c0 + c1 * u.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Fq2 {
pub c0: Fq,
pub c1: Fq,
Expand Down
1 change: 1 addition & 0 deletions src/bls12_381/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rand::{Rand, Rng};

/// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2).
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Fq6 {
pub c0: Fq2,
pub c1: Fq2,
Expand Down
10 changes: 9 additions & 1 deletion src/bls12_381/serde_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Fq, FqRepr, Fr, FrRepr, G1, G1Affine, G2, G2Affine};
use super::{Fq, FqRepr, Fr, FrRepr, G1Affine, G2Affine, G1, G2};
use {CurveAffine, CurveProjective, EncodedPoint, PrimeField};

use serde::de::Error as DeserializeError;
Expand Down Expand Up @@ -125,6 +125,7 @@ mod tests {
extern crate serde_json;

use super::*;
use bls12_381::Fq12;

use std::fmt::Debug;

Expand Down Expand Up @@ -166,4 +167,11 @@ mod tests {
test_roundtrip(&f);
test_roundtrip(&f.into_repr());
}

#[test]
fn serde_fq12() {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let f: Fq12 = rng.gen();
test_roundtrip(&f);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern crate byteorder;
extern crate ff;
extern crate rand;
#[cfg(feature = "serde")]
#[macro_use(Serialize, Deserialize)]
extern crate serde;

#[cfg(test)]
Expand Down

0 comments on commit b0f3a88

Please sign in to comment.