Skip to content

Commit

Permalink
refactor: switch hashing from blake2s to sha256 by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored Nov 8, 2019
1 parent c6a4dfa commit fc438a2
Show file tree
Hide file tree
Showing 24 changed files with 681 additions and 613 deletions.
32 changes: 31 additions & 1 deletion fil-proofs-tooling/src/bin/benchy/hash_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use storage_proofs::circuit::pedersen::{pedersen_compression_num, pedersen_md_no
use storage_proofs::circuit::test::TestConstraintSystem;
use storage_proofs::crypto;
use storage_proofs::crypto::pedersen::JJ_PARAMS;
use storage_proofs::util::{bits_to_bytes, bytes_into_boolean_vec};
use storage_proofs::util::{bits_to_bytes, bytes_into_boolean_vec, bytes_into_boolean_vec_be};

fn blake2s_count(bytes: usize) -> Result<Report, failure::Error> {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
Expand Down Expand Up @@ -44,6 +44,32 @@ fn blake2s_count(bytes: usize) -> Result<Report, failure::Error> {
})
}

fn sha256_count(bytes: usize) -> Result<Report, failure::Error> {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);

let mut cs = TestConstraintSystem::<Bls12>::new();
let mut data = vec![0u8; bytes];
rng.fill_bytes(&mut data);

let data_bits: Vec<Boolean> = {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec_be(&mut cs, Some(data.as_slice()), data.len()).unwrap()
};

let _out: Vec<bool> = scircuit::sha256::sha256(&mut cs, &data_bits)?
.into_iter()
.map(|b| b.get_value().unwrap())
.collect();

assert!(cs.is_satisfied(), "constraints not satisfied");

Ok(Report {
hash_fn: "sha256".into(),
bytes,
constraints: cs.num_constraints(),
})
}

fn pedersen_count(bytes: usize) -> Result<Report, failure::Error> {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);

Expand Down Expand Up @@ -103,6 +129,10 @@ pub fn run() -> Result<(), failure::Error> {
pedersen_count(64)?,
pedersen_count(128)?,
pedersen_count(256)?,
sha256_count(32)?,
sha256_count(64)?,
sha256_count(128)?,
sha256_count(256)?,
];

// print reports
Expand Down
24 changes: 12 additions & 12 deletions fil-proofs-tooling/src/bin/benchy/stacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn file_backed_mmap_from_zeroes(n: usize, use_tmp: bool) -> Result<MmapMut, fail
}

fn dump_proof_bytes<H: Hasher>(
all_partition_proofs: &[Vec<stacked::Proof<H, Blake2sHasher>>],
all_partition_proofs: &[Vec<stacked::Proof<H, Sha256Hasher>>],
) -> Result<(), failure::Error> {
let file = OpenOptions::new()
.write(true)
Expand Down Expand Up @@ -132,7 +132,7 @@ where
layer_challenges: layer_challenges.clone(),
};

let pp = StackedDrg::<H, Blake2sHasher>::setup(&sp)?;
let pp = StackedDrg::<H, Sha256Hasher>::setup(&sp)?;

let (pub_in, priv_in, d) = if *bench_only {
(None, None, None)
Expand All @@ -146,9 +146,9 @@ where
return_value: (pub_inputs, priv_inputs),
} = measure(|| {
let (tau, (p_aux, t_aux)) =
StackedDrg::<H, Blake2sHasher>::replicate(&pp, &replica_id, &mut data, None)?;
StackedDrg::<H, Sha256Hasher>::replicate(&pp, &replica_id, &mut data, None)?;

let pb = stacked::PublicInputs::<H::Domain, <Blake2sHasher as Hasher>::Domain> {
let pb = stacked::PublicInputs::<H::Domain, <Sha256Hasher as Hasher>::Domain> {
replica_id,
seed,
tau: Some(tau),
Expand Down Expand Up @@ -189,7 +189,7 @@ where
wall_time: vanilla_proving_wall_time,
return_value: all_partition_proofs,
} = measure(|| {
StackedDrg::<H, Blake2sHasher>::prove_all_partitions(
StackedDrg::<H, Sha256Hasher>::prove_all_partitions(
&pp,
&pub_inputs,
&priv_inputs,
Expand Down Expand Up @@ -218,7 +218,7 @@ where

for _ in 0..*samples {
let m = measure(|| {
let verified = StackedDrg::<H, Blake2sHasher>::verify_all_partitions(
let verified = StackedDrg::<H, Sha256Hasher>::verify_all_partitions(
&pp,
&pub_inputs,
&all_partition_proofs,
Expand Down Expand Up @@ -265,7 +265,7 @@ where
if let Some(data) = d {
if *extract {
let m = measure(|| {
StackedDrg::<H, Blake2sHasher>::extract_all(&pp, &replica_id, &data)
StackedDrg::<H, Sha256Hasher>::extract_all(&pp, &replica_id, &data)
.map_err(|err| err.into())
})?;

Expand Down Expand Up @@ -296,9 +296,9 @@ struct CircuitWorkMeasurement {
}

fn do_circuit_work<H: 'static + Hasher>(
pp: &<StackedDrg<H, Blake2sHasher> as ProofScheme>::PublicParams,
pub_in: Option<<StackedDrg<H, Blake2sHasher> as ProofScheme>::PublicInputs>,
priv_in: Option<<StackedDrg<H, Blake2sHasher> as ProofScheme>::PrivateInputs>,
pp: &<StackedDrg<H, Sha256Hasher> as ProofScheme>::PublicParams,
pub_in: Option<<StackedDrg<H, Sha256Hasher> as ProofScheme>::PublicInputs>,
priv_in: Option<<StackedDrg<H, Sha256Hasher> as ProofScheme>::PrivateInputs>,
params: &Params,
report: &mut Report,
) -> Result<CircuitWorkMeasurement, failure::Error> {
Expand All @@ -322,7 +322,7 @@ fn do_circuit_work<H: 'static + Hasher>(

if *bench || *circuit {
let mut cs = MetricCS::<Bls12>::new();
<StackedCompound as CompoundProof<_, StackedDrg<H, Blake2sHasher>, _>>::blank_circuit(
<StackedCompound as CompoundProof<_, StackedDrg<H, Sha256Hasher>, _>>::blank_circuit(
&pp, &JJ_PARAMS,
)
.synthesize(&mut cs)?;
Expand All @@ -342,7 +342,7 @@ fn do_circuit_work<H: 'static + Hasher>(
// We should also allow the serialized vanilla proofs to be passed (as a file) to the example
// and skip replication/vanilla-proving entirely.
let gparams =
<StackedCompound as CompoundProof<_, StackedDrg<H, Blake2sHasher>, _>>::groth_params(
<StackedCompound as CompoundProof<_, StackedDrg<H, Sha256Hasher>, _>>::groth_params(
&compound_public_params.vanilla_params,
&JJ_PARAMS,
)?;
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn get_unsealed_range<T: Into<PathBuf> + AsRef<Path>>(
ticket: Ticket,
offset: UnpaddedByteIndex,
num_bytes: UnpaddedBytesAmount,
) -> error::Result<(UnpaddedBytesAmount)> {
) -> error::Result<UnpaddedBytesAmount> {
let comm_d =
as_safe_commitment::<<DefaultPieceHasher as Hasher>::Domain, _>(&comm_d, "comm_d")?;

Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ pub const MINIMUM_RESERVED_BYTES_FOR_PIECE_IN_FULLY_ALIGNED_SECTOR: u64 =
pub const MIN_PIECE_SIZE: UnpaddedBytesAmount = UnpaddedBytesAmount(127);

/// The hasher used for creating comm_d.
pub type DefaultPieceHasher = storage_proofs::hasher::Blake2sHasher;
pub type DefaultPieceHasher = storage_proofs::hasher::Sha256Hasher;

pub use storage_proofs::drgraph::DefaultTreeHasher;
2 changes: 1 addition & 1 deletion filecoin-proofs/src/fr32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ the unique bit `0`, that just *started* at that position but doesn't
necessarily carry that value.)
**/
pub fn shift_bits(input: &[u8], amount: usize, is_left: bool) -> (Vec<u8>) {
pub fn shift_bits(input: &[u8], amount: usize, is_left: bool) -> Vec<u8> {
debug_assert!(amount >= 1);
debug_assert!(amount <= 7);

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2019-10-28
nightly-2019-11-06
Loading

0 comments on commit fc438a2

Please sign in to comment.