Skip to content

Commit

Permalink
Merge branch 'paul/fix/autogen' of https://github.com/0xProject/OpenZKP
Browse files Browse the repository at this point in the history
… into paul/fix/autogen
  • Loading branch information
Paul Vienhage committed Sep 29, 2020
2 parents d27e8f7 + 001c56f commit d51ed33
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 83 deletions.
3 changes: 2 additions & 1 deletion algebra/elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ lazy_static = { version = "1.3.0", features = [ "spin_no_std" ] } # TODO: When `
no-std-compat = { version = "0.4.0", features = [ "alloc" ] }
parity-scale-codec = { version = "1.3.0", default-features = false, optional = true }
proptest = { version = "0.9.4", optional = true }
serde = { version = "1.0", features = ["derive"], default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false, optional = true }
tiny-keccak = { version = "2.0.1", features = ["keccak"] }
zkp-macros-decl = { version = "0.1.0", path = "../../utils/macros-decl", default-features = false }
zkp-primefield = { version = "0.1.0", path = "../primefield", default-features = false }
Expand All @@ -39,6 +39,7 @@ harness = false
default = [
"inline",
"std",
"serde",
]
std = [
"serde/std",
Expand Down
4 changes: 3 additions & 1 deletion algebra/elliptic-curve/src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::prelude::v1::*;
use crate::{ScalarFieldElement, BETA};
#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use zkp_primefield::{FieldElement, NegInline, One, Zero};
use zkp_u256::{commutative_binop, noncommutative_binop};

#[derive(PartialEq, Eq, Clone, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "parity_codec", derive(Encode, Decode))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Affine {
Zero, // Neutral element, point at infinity, additive identity, etc.
Point { x: FieldElement, y: FieldElement },
Expand Down
3 changes: 0 additions & 3 deletions algebra/elliptic-curve/src/scalar_field.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};
use zkp_macros_decl::u256h;
use zkp_primefield::{Parameters, PrimeField};
use zkp_u256::U256;

pub type Element = PrimeField<Order>;

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[cfg_attr(feature = "parity_codec", derive(Encode, Decode))]
pub struct Order();

impl Parameters for Order {
Expand Down
3 changes: 2 additions & 1 deletion algebra/primefield/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ parity-scale-codec = { version = "1.3.0", default-features = false, optional = t
proptest = { version = "0.9.4", optional = true }
rand = { version = "0.7.2", optional = true }
rayon = { version = "1.3.0", optional = true }
serde = { version = "1.0", features = ["derive"], default_features = false }
serde = { version = "1.0", features = ["derive"], default_features = false, optional = true }
zkp-macros-decl = { version = "0.1.0", path = "../../utils/macros-decl", default_features = false }
zkp-u256 = { version = "0.1.0", path = "../u256", default_features = false }

Expand Down Expand Up @@ -53,6 +53,7 @@ default = [
"inline",
"rand",
"std",
"serde",
]
std = [
"crunchy/std",
Expand Down
3 changes: 3 additions & 0 deletions algebra/primefield/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ pub mod fft;
pub mod geometric_series;
mod invert_batch;
mod ops;
#[cfg(feature = "parity_codec")]
mod parity_codec;
mod prime_field;
#[cfg(any(test, feature = "proptest"))]
mod proptest;
mod proth_field;
#[cfg(feature = "rand")]
mod rand;
#[cfg(feature = "serde")]
mod serde;
mod traits;
mod uint;
Expand Down
59 changes: 59 additions & 0 deletions algebra/primefield/src/parity_codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Clippy false positive
#[allow(clippy::useless_attribute)]
// We want to import an alternative prelude.
#[allow(clippy::wildcard_imports)]
use std::prelude::v1::*;

use crate::{uint::UInt, Parameters, PrimeField};
use parity_scale_codec::{Decode, Encode, Error, Input, Output};

impl<U, P> Encode for PrimeField<P>
where
U: UInt + Encode,
P: Parameters<UInt = U>,
{
fn size_hint(&self) -> usize {
self.to_uint().size_hint()
}

fn encode_to<T: Output>(&self, dest: &mut T) {
self.to_uint().encode_to(dest);
}

fn encode(&self) -> Vec<u8> {
self.to_uint().encode()
}

fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.to_uint().using_encoded(f)
}
}

impl<U, P> Decode for PrimeField<P>
where
U: UInt + Decode,
P: Parameters<UInt = U>,
{
fn decode<I: Input>(value: &mut I) -> Result<Self, Error> {
Ok(Self::from_uint(&U::decode(value)?))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::proth_field::Proth;
use proptest::prelude::*;

#[test]
fn test_roundtrip() {
proptest!(|(x: PrimeField<Proth>)| {
let serialized = x.encode();
// Deserialize consumes a mutable slice reference.
let mut slice = serialized.as_slice();
let deserialized: PrimeField<Proth> = Decode::decode(&mut slice)?;
prop_assert_eq!(slice.len(), 0); // Consumes all
prop_assert_eq!(deserialized, x);
});
}
}
3 changes: 0 additions & 3 deletions algebra/primefield/src/prime_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use std::{fmt, prelude::v1::*};

use crate::{Root, SquareRoot, UInt as FieldUInt};
#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};
use std::{
hash::{Hash, Hasher},
marker::PhantomData,
Expand All @@ -32,7 +30,6 @@ use zkp_u256::{
/// `proptest` support `Parameters` needs to be `'static + Send` (which it
/// really should anyway).
// Derive fails for Clone, PartialEq, Eq, Hash
#[cfg_attr(feature = "parity_codec", derive(Encode, Decode))]
pub struct PrimeField<P: Parameters> {
// TODO: un-pub. They are pub so FieldElement can have const-fn constructors.
pub uint: P::UInt,
Expand Down
3 changes: 0 additions & 3 deletions algebra/primefield/src/proth_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{Parameters, PrimeField};
#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};
use std::marker::PhantomData;
use zkp_macros_decl::u256h;
use zkp_u256::{to_montgomery_const, U256};
Expand All @@ -10,7 +8,6 @@ use zkp_u256::{to_montgomery_const, U256};
pub type FieldElement = PrimeField<Proth>;

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[cfg_attr(feature = "parity_codec", derive(Encode, Decode))]
pub struct Proth();

impl Parameters for Proth {
Expand Down
3 changes: 2 additions & 1 deletion algebra/u256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ parity-scale-codec = { version = "1.3.0", default-features = false, optional = t
proptest = { version = "0.9.4", optional = true }
proptest-derive = { version = "0.1.2", optional = true }
rand = { version = "0.7.2", optional = true }
serde = { version = "1.0", default_features = false }
serde = { version = "1.0", default_features = false, optional = true }

[dev-dependencies]
bincode = "1.2.1"
Expand All @@ -42,6 +42,7 @@ default = [
"inline",
"rand",
"std",
"serde",
]
std = [
"crunchy/std",
Expand Down
130 changes: 69 additions & 61 deletions algebra/u256/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::prelude::v1::*;

use crate::U256;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::u64;

Expand All @@ -31,6 +32,7 @@ impl U256 {
}
}

#[cfg(feature = "serde")]
impl Serialize for U256 {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
Expand All @@ -41,6 +43,7 @@ impl Serialize for U256 {
}
}

#[cfg(feature = "serde")]
impl<'a> Deserialize<'a> for U256 {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
if deserializer.is_human_readable() {
Expand Down Expand Up @@ -168,73 +171,78 @@ mod tests {
use num_traits::identities::One;
use proptest::prelude::*;

#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};

#[test]
fn test_one() {
let one = U256::one();
let serialized = serde_json::to_string(&one).unwrap();
assert_eq!(
serialized,
"\"0x0000000000000000000000000000000000000000000000000000000000000001\""
);
}
#[cfg(feature = "serde")]
mod serde {
use super::*;

#[test]
fn test_one() {
let one = U256::one();
let serialized = serde_json::to_string(&one).unwrap();
assert_eq!(
serialized,
"\"0x0000000000000000000000000000000000000000000000000000000000000001\""
);
}

#[test]
fn test_serde_json() {
proptest!(|(x: U256)| {
let serialized = serde_json::to_string(&x)?;
let deserialized: U256 = serde_json::from_str(&serialized)?;
prop_assert_eq!(deserialized, x);
});
}
#[test]
fn test_json() {
proptest!(|(x: U256)| {
let serialized = serde_json::to_string(&x)?;
let deserialized: U256 = serde_json::from_str(&serialized)?;
prop_assert_eq!(deserialized, x);
});
}

#[test]
fn test_serde_bincode() {
proptest!(|(x: U256)| {
let serialized = bincode::serialize(&x)?;
let deserialized: U256 = bincode::deserialize(&serialized)?;
prop_assert_eq!(deserialized, x);
});
#[test]
fn test_bincode() {
proptest!(|(x: U256)| {
let serialized = bincode::serialize(&x)?;
let deserialized: U256 = bincode::deserialize(&serialized)?;
prop_assert_eq!(deserialized, x);
});
}
}

#[cfg(feature = "parity_codec")]
#[test]
fn test_parity_codec_one() {
let one = U256::one();
let serialized = one.encode();
assert_eq!(
hex::encode(serialized),
"0100000000000000000000000000000000000000000000000000000000000000"
);
}
mod parity_codec {
use super::*;
use parity_scale_codec::{Decode, Encode};

#[test]
fn test_one() {
let one = U256::one();
let serialized = one.encode();
assert_eq!(
hex::encode(serialized),
"0100000000000000000000000000000000000000000000000000000000000000"
);
}

#[cfg(feature = "parity_codec")]
#[test]
fn test_parity_codec() {
proptest!(|(x: U256)| {
let serialized = x.encode();
// Deserialize consumes a mutable slice reference.
let mut slice = serialized.as_slice();
let deserialized: U256 = U256::decode(&mut slice)?;
prop_assert_eq!(slice.len(), 0); // Consumes all
prop_assert_eq!(deserialized, x);
});
}
#[test]
fn test_roundtrip() {
proptest!(|(x: U256)| {
let serialized = x.encode();
// Deserialize consumes a mutable slice reference.
let mut slice = serialized.as_slice();
let deserialized: U256 = U256::decode(&mut slice)?;
prop_assert_eq!(slice.len(), 0); // Consumes all
prop_assert_eq!(deserialized, x);
});
}

#[cfg(feature = "parity_codec")]
#[test]
fn test_parity_little_endian() {
proptest!(|(x: U256)| {
let serialized = x.encode();
// Encoding is lsb first (little-endian order)
// We prefer big-endian in IO, but the actual memory layout is
// little-endian. Having the encoding be identical to the memory
// layout may give a performance advantage down the line, which
// seems to be the goal of the Parity Scale codec.
let little_endian: Vec<u8> = x.to_bytes_be().iter().rev().cloned().collect();
prop_assert_eq!(serialized, little_endian);
});
#[test]
fn test_little_endian() {
proptest!(|(x: U256)| {
let serialized = x.encode();
// Encoding is lsb first (little-endian order)
// We prefer big-endian in IO, but the actual memory layout is
// little-endian. Having the encoding be identical to the memory
// layout may give a performance advantage down the line, which
// seems to be the goal of the Parity Scale codec.
let little_endian: Vec<u8> = x.to_bytes_be().iter().rev().cloned().collect();
prop_assert_eq!(serialized, little_endian);
});
}
}
}
5 changes: 2 additions & 3 deletions algebra/u256/src/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
#[allow(clippy::wildcard_imports)]
use std::prelude::v1::*;

#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};
#[cfg(any(test, feature = "proptest"))]
use proptest_derive::Arbitrary;
use std::{cmp::Ordering, u64};

#[cfg(feature = "parity_codec")]
use parity_scale_codec::{Decode, Encode};

#[derive(PartialEq, Eq, Clone, Default, Hash)]
#[cfg_attr(feature = "parity_codec", derive(Encode, Decode))]
// TODO: Generate a quasi-random sequence.
Expand Down
4 changes: 2 additions & 2 deletions crypto/elliptic-curve-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ lazy_static = { version = "1.3.0", features = [ "spin_no_std" ] } # TODO: When `
parity-scale-codec = { version = "1.3.0", default-features = false, optional = true }
no-std-compat = { version = "0.4.0", features = [ "alloc" ] }
tiny-keccak = { version = "2.0.1", features = ["sha3"] }
serde = { version = "1.0", features = ["derive"], default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false, optional = true }
zkp-elliptic-curve = { version = "0.1.0", path = "../../algebra/elliptic-curve", default-features = false }
zkp-macros-decl = { version = "0.1.0", path = "../../utils/macros-decl", default-features = false }
zkp-primefield = { version = "0.1.0", path = "../../algebra/primefield", default-features = false }
Expand All @@ -37,7 +37,7 @@ name = "benchmark"
harness = false

[features]
default = [ "std" ]
default = [ "std", "serde" ]
std = [
"serde/std",
"itertools/use_std",
Expand Down
Loading

0 comments on commit d51ed33

Please sign in to comment.