From 69fe30c1e434858d1eb5fd996baf4c98f9c4568f Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 18 Jun 2018 13:15:04 +0200 Subject: [PATCH 1/2] wipg --- src/circuit/kdf.rs | 4 ++-- src/circuit/ppor/mod.rs | 2 +- src/circuit/xor.rs | 6 ++--- src/layered_drgporep.rs | 52 ++++++++++++++++++++--------------------- src/util.rs | 12 ++++++---- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/circuit/kdf.rs b/src/circuit/kdf.rs index f5edebe3..51463967 100644 --- a/src/circuit/kdf.rs +++ b/src/circuit/kdf.rs @@ -37,7 +37,7 @@ mod tests { use pairing::bls12_381::Bls12; use rand::{Rng, SeedableRng, XorShiftRng}; use sapling_crypto::circuit::boolean::Boolean; - use util::{bits_into_bytes, bytes_into_boolean_vec}; + use util::{bits_to_bytes, bytes_into_boolean_vec}; #[test] fn test_kdf_input_circut() { @@ -72,7 +72,7 @@ mod tests { }); // convert Vec to Vec - let actual = bits_into_bytes( + let actual = bits_to_bytes( out.iter() .map(|v| v.get_value().unwrap()) .collect::>() diff --git a/src/circuit/ppor/mod.rs b/src/circuit/ppor/mod.rs index 5babe8b9..497f067d 100644 --- a/src/circuit/ppor/mod.rs +++ b/src/circuit/ppor/mod.rs @@ -53,7 +53,7 @@ impl<'a, E: JubjubEngine> Circuit for ParallelProofOfRetrievability<'a, E> { mod tests { use super::*; use circuit::test::*; - use drgraph::{self, proof_into_options}; + use drgraph; use merklepor; use pairing::bls12_381::*; use pairing::Field; diff --git a/src/circuit/xor.rs b/src/circuit/xor.rs index e4e2c91b..5d6a7025 100644 --- a/src/circuit/xor.rs +++ b/src/circuit/xor.rs @@ -36,7 +36,7 @@ mod tests { use pairing::bls12_381::Bls12; use rand::{Rng, SeedableRng, XorShiftRng}; use sapling_crypto::circuit::boolean::Boolean; - use util::{bits_into_bytes, bytes_into_boolean_vec}; + use util::{bits_to_bytes, bytes_into_boolean_vec}; #[test] fn test_xor_input_circut() { @@ -64,7 +64,7 @@ mod tests { assert_eq!(out_bits.len(), data_bits.len(), "invalid output length"); // convert Vec to Vec - let actual = bits_into_bytes( + let actual = bits_to_bytes( out_bits .iter() .map(|v| v.get_value().unwrap()) @@ -82,7 +82,7 @@ mod tests { xor(&mut cs, key_bits.as_slice(), out_bits.as_slice()).unwrap() }; - let roundtrip = bits_into_bytes( + let roundtrip = bits_to_bytes( roundtrip_bits .iter() .map(|v| v.get_value().unwrap()) diff --git a/src/layered_drgporep.rs b/src/layered_drgporep.rs index ba08820e..532bfa68 100644 --- a/src/layered_drgporep.rs +++ b/src/layered_drgporep.rs @@ -59,12 +59,12 @@ impl<'a> Into for drgporep::DataProof<'a> { pub struct PublicInputs<'a> { pub prover_id: &'a [u8], pub challenge: usize, - pub tau: Tau, + pub tau: Vec, } pub struct PrivateInputs<'a> { pub replica: &'a [u8], - pub aux: &'a ProverAux, + pub aux: &'a [porep::ProverAux], } #[derive(Debug, Clone)] @@ -193,7 +193,7 @@ fn prove_layers( pp: &drgporep::PublicParams, pub_inputs: &PublicInputs, priv_inputs: &drgporep::PrivateInputs, - aux: &ProverAux, + aux: &[porep::ProverAux], layers: usize, mut proofs: Vec, ) -> Result> { @@ -225,29 +225,27 @@ fn prove_layers( prove_layers(pp, pub_inputs, &new_priv_inputs, aux, layers - 1, proofs) } } - -type Tau = Vec; -type ProverAux = Vec; - impl<'a, 'c> PoRep<'a> for LayeredDrgPoRep { - type Tau = Tau; - type ProverAux = ProverAux; + type Tau = Vec; + type ProverAux = Vec; fn replicate( pp: &'a PublicParams, prover_id: &[u8], data: &mut [u8], ) -> Result<(Self::Tau, Self::ProverAux)> { - let taus = Vec::new(); - let auxs = Vec::new(); + let mut taus = Vec::new(); + let mut auxs = Vec::new(); permute_and_replicate_layers( &pp.drg_porep_public_params, pp.layers, prover_id, data, - taus, - auxs, - ) + &mut taus, + &mut auxs, + )?; + + Ok((taus, auxs)) } fn extract_all<'b>( @@ -305,20 +303,20 @@ fn permute_and_replicate_layers( layers: usize, prover_id: &[u8], data: &mut [u8], - mut taus: Vec, - mut auxs: Vec, -) -> Result<(Vec, Vec)> { + taus: &mut Vec, + auxs: &mut Vec, +) -> Result<()> { assert!(layers > 0); let (tau, aux) = DrgPoRep::replicate(&drgpp, prover_id, data).unwrap(); taus.push(tau); auxs.push(aux); - if layers == 1 { - Ok((taus, auxs)) - } else { - permute_and_replicate_layers(&permute(&drgpp), layers - 1, prover_id, data, taus, auxs) + if layers != 1 { + permute_and_replicate_layers(&permute(&drgpp), layers - 1, prover_id, data, taus, auxs)?; } + + Ok(()) } #[cfg(test)] @@ -334,7 +332,7 @@ mod tests { } #[test] - fn test_layered_extract_all() { + fn layered_extract_all() { let lambda = 16; let prover_id = vec![1u8; 16]; let data = vec![2u8; 16 * 3]; @@ -414,17 +412,17 @@ mod tests { } #[test] - fn test_layered_prove_verify_16_2() { + fn layered_prove_verify_16_2() { layered_prove_verify(16, 4); } #[test] - fn test_layered_prove_verify_16_3() { + fn layered_prove_verify_16_3() { layered_prove_verify(16, 3); } #[test] - fn test_layered_prove_verify_16_10() { + fn layered_prove_verify_16_10() { layered_prove_verify(16, 10); } @@ -435,12 +433,12 @@ mod tests { // } #[test] - fn test_layered_prove_verify_32_3() { + fn layered_prove_verify_32_3() { layered_prove_verify(32, 3); } #[test] - fn test_layered_prove_verify_32_10() { + fn layered_prove_verify_32_10() { layered_prove_verify(32, 10); } } diff --git a/src/util.rs b/src/util.rs index 74613b7c..9ac6dc8b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -55,6 +55,7 @@ pub fn bytes_into_boolean_vec>( Ok(bits) } +#[allow(dead_code)] #[inline] fn bool_to_u8(bit: bool, offset: usize) -> u8 { if bit { @@ -65,7 +66,8 @@ fn bool_to_u8(bit: bool, offset: usize) -> u8 { } /// Converts a slice of bools into their byte representation, in little endian. -pub fn bits_into_bytes(bits: &[bool]) -> Vec { +#[allow(dead_code)] +pub fn bits_to_bytes(bits: &[bool]) -> Vec { bits.chunks(8) .map(|bits| { bool_to_u8(bits[7], 7) @@ -99,7 +101,7 @@ mod tests { bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), 8).unwrap() }; - let bytes_actual: Vec = bits_into_bytes( + let bytes_actual: Vec = bits_to_bytes( bools .iter() .map(|b| b.get_value().unwrap()) @@ -122,11 +124,11 @@ mod tests { #[test] fn test_bits_into_bytes() { assert_eq!( - bits_into_bytes(&[true, false, false, false, false, false, false, false]), + bits_to_bytes(&[true, false, false, false, false, false, false, false]), vec![1] ); assert_eq!( - bits_into_bytes(&[true, true, true, true, true, true, true, true]), + bits_to_bytes(&[true, true, true, true, true, true, true, true]), vec![255] ); } @@ -144,7 +146,7 @@ mod tests { let bytes: Vec = (0..i).map(|_| rng.gen()).collect(); let bits = bytes_into_bits(bytes.as_slice()); - assert_eq!(bits_into_bytes(bits.as_slice()), bytes); + assert_eq!(bits_to_bytes(bits.as_slice()), bytes); } } } From 8b3fec627cacfa7d8492acd05b9d90d7bd449fe8 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 18 Jun 2018 13:33:34 +0200 Subject: [PATCH 2/2] refactor: follow clippy a little better --- src/circuit/drgporep.rs | 12 ++++----- src/circuit/ppor/mod.rs | 4 +-- src/circuit/sloth.rs | 1 - src/drgraph.rs | 3 +++ src/layered_drgporep.rs | 57 ++++++++++++++++++++++------------------- 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/circuit/drgporep.rs b/src/circuit/drgporep.rs index cc699102..ba0b6db0 100644 --- a/src/circuit/drgporep.rs +++ b/src/circuit/drgporep.rs @@ -46,10 +46,10 @@ pub fn drgporep( params: &E::Params, lambda: usize, replica_node: Option<&[u8]>, - replica_node_path: Vec>, + replica_node_path: &[Option<(E::Fr, bool)>], replica_root: Option, replica_parents: Vec>, - replica_parents_paths: Vec>>, + replica_parents_paths: &[Vec>], data_node: Option<&[u8]>, data_node_path: Vec>, data_root: Option, @@ -80,7 +80,7 @@ where params, replica_node, lambda, - replica_node_path.clone(), + replica_node_path.to_owned(), replica_root, )?; @@ -247,7 +247,7 @@ mod tests { .into_iter() .map(|(_, parent)| Some(parent.data)) .collect(); - let replica_parents_paths = proof_nc + let replica_parents_paths: Vec<_> = proof_nc .replica_parents .iter() .map(|(_, parent)| parent.proof.as_options()) @@ -273,10 +273,10 @@ mod tests { params, lambda, replica_node, - replica_node_path, + &replica_node_path, replica_root, replica_parents, - replica_parents_paths, + &replica_parents_paths, data_node, data_node_path, data_root, diff --git a/src/circuit/ppor/mod.rs b/src/circuit/ppor/mod.rs index 497f067d..912f9c6c 100644 --- a/src/circuit/ppor/mod.rs +++ b/src/circuit/ppor/mod.rs @@ -24,7 +24,7 @@ pub struct ParallelProofOfRetrievability<'a, E: JubjubEngine> { pub lambda: usize, /// The authentication path of the commitment in the tree. - pub auth_paths: Vec>>, + pub auth_paths: &'a [Vec>], /// The root of the underyling merkle tree. pub root: Option, @@ -118,7 +118,7 @@ mod tests { params, lambda: pub_params.lambda, values: values, - auth_paths: auth_paths, + auth_paths: &auth_paths, root: Some(tree.root().into()), }; diff --git a/src/circuit/sloth.rs b/src/circuit/sloth.rs index 7e65f41e..5e463d1c 100644 --- a/src/circuit/sloth.rs +++ b/src/circuit/sloth.rs @@ -68,7 +68,6 @@ mod tests { use circuit::test::TestConstraintSystem; use crypto::sloth; use pairing::bls12_381::{Bls12, Fr}; - use pairing::PrimeField; use rand::{Rng, SeedableRng, XorShiftRng}; #[test] diff --git a/src/drgraph.rs b/src/drgraph.rs index 853df31f..0c9f0864 100644 --- a/src/drgraph.rs +++ b/src/drgraph.rs @@ -1,3 +1,5 @@ +#![cfg_attr(feature = "cargo-clippy", allow(len_without_is_empty))] + use crypto::feistel; use error::Result; use hasher::pedersen; @@ -143,6 +145,7 @@ pub enum Sampling { impl Graph { /// Creates a new graph. If no sampling is passed, it does not contain any edges. + #[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] pub fn new(nodes: usize, sampling: Option) -> Graph { match sampling { Some(Sampling::DR) => dr_sample(nodes), diff --git a/src/layered_drgporep.rs b/src/layered_drgporep.rs index 532bfa68..058fa3ab 100644 --- a/src/layered_drgporep.rs +++ b/src/layered_drgporep.rs @@ -129,14 +129,18 @@ impl<'a> ProofScheme<'a> for LayeredDrgPoRep { replica: priv_inputs.replica, }; + let mut proofs = Vec::with_capacity(pub_params.layers); + prove_layers( &pub_params.drg_porep_public_params, pub_inputs, &drg_priv_inputs, priv_inputs.aux, pub_params.layers, - Vec::new(), - ) + &mut proofs, + )?; + + Ok(proofs) } fn verify( @@ -195,8 +199,8 @@ fn prove_layers( priv_inputs: &drgporep::PrivateInputs, aux: &[porep::ProverAux], layers: usize, - mut proofs: Vec, -) -> Result> { + proofs: &mut Vec, +) -> Result<()> { assert!(layers > 0); let mut scratch = priv_inputs.replica.to_vec().clone(); @@ -219,12 +223,13 @@ fn prove_layers( let pp = &permute(pp); - if layers == 1 { - Ok(proofs) - } else { - prove_layers(pp, pub_inputs, &new_priv_inputs, aux, layers - 1, proofs) + if layers != 1 { + prove_layers(pp, pub_inputs, &new_priv_inputs, aux, layers - 1, proofs)?; } + + Ok(()) } + impl<'a, 'c> PoRep<'a> for LayeredDrgPoRep { type Tau = Vec; type ProverAux = Vec; @@ -234,8 +239,9 @@ impl<'a, 'c> PoRep<'a> for LayeredDrgPoRep { prover_id: &[u8], data: &mut [u8], ) -> Result<(Self::Tau, Self::ProverAux)> { - let mut taus = Vec::new(); - let mut auxs = Vec::new(); + let mut taus = Vec::with_capacity(pp.layers); + let mut auxs = Vec::with_capacity(pp.layers); + permute_and_replicate_layers( &pp.drg_porep_public_params, pp.layers, @@ -253,17 +259,16 @@ impl<'a, 'c> PoRep<'a> for LayeredDrgPoRep { prover_id: &'b [u8], data: &'b [u8], ) -> Result> { - let mut d: Vec = data.to_vec(); - let dd = d.as_mut_slice(); - - Ok( - extract_and_invert_permute_layers( - &pp.drg_porep_public_params, - pp.layers, - prover_id, - dd, - )?.to_vec(), - ) + let mut data = data.to_vec(); + + extract_and_invert_permute_layers( + &pp.drg_porep_public_params, + pp.layers, + prover_id, + &mut data, + )?; + + Ok(data) } fn extract( @@ -281,7 +286,7 @@ fn extract_and_invert_permute_layers<'a>( layers: usize, prover_id: &[u8], data: &'a mut [u8], -) -> Result<&'a [u8]> { +) -> Result<()> { assert!(layers > 0); let inverted = &invert_permute(&drgpp); @@ -291,11 +296,11 @@ fn extract_and_invert_permute_layers<'a>( data[i] = *r; } - if layers == 1 { - Ok(data) - } else { - extract_and_invert_permute_layers(inverted, layers - 1, prover_id, data) + if layers != 1 { + extract_and_invert_permute_layers(inverted, layers - 1, prover_id, data)?; } + + Ok(()) } fn permute_and_replicate_layers(