upgrade num, derive serialize
This commit is contained in:
parent
44caf701b0
commit
a5290fab39
4 changed files with 69 additions and 49 deletions
17
Cargo.lock
generated
17
Cargo.lock
generated
|
@ -288,6 +288,16 @@ dependencies = [
|
|||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.39"
|
||||
|
@ -664,10 +674,12 @@ version = "0.3.0"
|
|||
dependencies = [
|
||||
"bincode 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-bigint 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -713,6 +725,7 @@ version = "0.3.2"
|
|||
"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
|
||||
"checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
|
||||
"checksum num-bigint 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1"
|
||||
"checksum num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "57450397855d951f1a41305e54851b1a7b8f5d2e349543a02a2effe25459f718"
|
||||
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
|
||||
"checksum num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "af3fdbbc3291a5464dc57b03860ec37ca6bf915ed6ee385e7c6c052c422b2124"
|
||||
"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
|
||||
|
|
|
@ -9,6 +9,11 @@ edition = "2018"
|
|||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
lazy_static = "0.1.*"
|
||||
num = {version = "0.1.36", default-features = false}
|
||||
num-bigint = {version = "0.1.36", default-features = false}
|
||||
bincode = "0.8.0"
|
||||
bincode = "0.8.0"
|
||||
serde_json = "1.0"
|
||||
num-traits = "0.2"
|
||||
num-integer = "0.1"
|
||||
|
||||
[dependencies.num-bigint]
|
||||
version = "0.2"
|
||||
features = ["serde"]
|
|
@ -4,10 +4,11 @@
|
|||
// @author Jacob Eberhardt <jacob.eberhardt@tu-berlin.de>
|
||||
// @date 2017
|
||||
|
||||
use num::{Integer, Num, One, Zero};
|
||||
use lazy_static::lazy_static;
|
||||
use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
|
||||
use serde::de::{Deserialize, Deserializer, Visitor};
|
||||
use serde::{Serialize, Serializer};
|
||||
use num_integer::Integer;
|
||||
use num_traits::{Num, One, Zero};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::convert::From;
|
||||
use std::fmt;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
@ -71,7 +72,7 @@ pub trait Field:
|
|||
fn get_required_bits() -> usize;
|
||||
}
|
||||
|
||||
#[derive(PartialEq, PartialOrd, Clone, Eq, Ord, Hash)]
|
||||
#[derive(PartialEq, PartialOrd, Clone, Eq, Ord, Hash, Serialize, Deserialize)]
|
||||
pub struct FieldPrime {
|
||||
value: BigInt,
|
||||
}
|
||||
|
@ -323,48 +324,48 @@ impl<'a> Pow<&'a FieldPrime> for FieldPrime {
|
|||
}
|
||||
}
|
||||
|
||||
// custom serde serialization
|
||||
impl Serialize for FieldPrime {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
// serializer.serialize_bytes(&(*self.value.to_biguint().to_bytes_le().as_slice()))
|
||||
serializer.serialize_bytes(&(*self.into_byte_vector().as_slice()))
|
||||
}
|
||||
}
|
||||
// // custom serde serialization
|
||||
// impl Serialize for FieldPrime {
|
||||
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
// where
|
||||
// S: Serializer,
|
||||
// {
|
||||
// // serializer.serialize_bytes(&(*self.value.to_biguint().to_bytes_le().as_slice()))
|
||||
// serializer.serialize_bytes(&(*self.into_byte_vector().as_slice()))
|
||||
// }
|
||||
// }
|
||||
|
||||
// custom serde deserialization
|
||||
|
||||
struct FieldPrimeVisitor;
|
||||
// struct FieldPrimeVisitor;
|
||||
|
||||
impl FieldPrimeVisitor {
|
||||
fn new() -> Self {
|
||||
FieldPrimeVisitor {}
|
||||
}
|
||||
}
|
||||
// impl FieldPrimeVisitor {
|
||||
// fn new() -> Self {
|
||||
// FieldPrimeVisitor {}
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'de> Visitor<'de> for FieldPrimeVisitor {
|
||||
type Value = FieldPrime;
|
||||
// impl<'de> Visitor<'de> for FieldPrimeVisitor {
|
||||
// type Value = FieldPrime;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("struct FieldPrime")
|
||||
}
|
||||
// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
// formatter.write_str("struct FieldPrime")
|
||||
// }
|
||||
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> {
|
||||
let val = BigUint::from_bytes_le(v).to_bigint().unwrap();
|
||||
Ok(FieldPrime { value: val })
|
||||
}
|
||||
}
|
||||
// fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> {
|
||||
// let val = BigUint::from_bytes_le(v).to_bigint().unwrap();
|
||||
// Ok(FieldPrime { value: val })
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'de> Deserialize<'de> for FieldPrime {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_bytes(FieldPrimeVisitor::new())
|
||||
}
|
||||
}
|
||||
// impl<'de> Deserialize<'de> for FieldPrime {
|
||||
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
// where
|
||||
// D: Deserializer<'de>,
|
||||
// {
|
||||
// deserializer.deserialize_bytes(FieldPrimeVisitor::new())
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Calculates the gcd using an iterative implementation of the extended euclidian algorithm.
|
||||
/// Returning `(d, s, t)` so that `d = s * a + t * b`
|
||||
|
@ -633,6 +634,13 @@ mod tests {
|
|||
assert_eq!(FieldPrime::from("11"), deserialized);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serde_json_ser_deser() {
|
||||
let serialized = serde_json::to_string(&FieldPrime::from("11")).unwrap();
|
||||
let deserialized = serde_json::from_str(&serialized).unwrap();
|
||||
assert_eq!(FieldPrime::from("11"), deserialized);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_ser_deser() {
|
||||
let fp = FieldPrime::from("101");
|
||||
|
|
|
@ -1,7 +1 @@
|
|||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate num;
|
||||
extern crate num_bigint;
|
||||
extern crate serde;
|
||||
|
||||
pub mod field;
|
||||
|
|
Loading…
Reference in a new issue