Skip to content

Commit fc438a2

Browse files
refactor: switch hashing from blake2s to sha256 by default
1 parent c6a4dfa commit fc438a2

File tree

24 files changed

+681
-613
lines changed

24 files changed

+681
-613
lines changed

fil-proofs-tooling/src/bin/benchy/hash_fns.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use storage_proofs::circuit::pedersen::{pedersen_compression_num, pedersen_md_no
88
use storage_proofs::circuit::test::TestConstraintSystem;
99
use storage_proofs::crypto;
1010
use storage_proofs::crypto::pedersen::JJ_PARAMS;
11-
use storage_proofs::util::{bits_to_bytes, bytes_into_boolean_vec};
11+
use storage_proofs::util::{bits_to_bytes, bytes_into_boolean_vec, bytes_into_boolean_vec_be};
1212

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

47+
fn sha256_count(bytes: usize) -> Result<Report, failure::Error> {
48+
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
49+
50+
let mut cs = TestConstraintSystem::<Bls12>::new();
51+
let mut data = vec![0u8; bytes];
52+
rng.fill_bytes(&mut data);
53+
54+
let data_bits: Vec<Boolean> = {
55+
let mut cs = cs.namespace(|| "data");
56+
bytes_into_boolean_vec_be(&mut cs, Some(data.as_slice()), data.len()).unwrap()
57+
};
58+
59+
let _out: Vec<bool> = scircuit::sha256::sha256(&mut cs, &data_bits)?
60+
.into_iter()
61+
.map(|b| b.get_value().unwrap())
62+
.collect();
63+
64+
assert!(cs.is_satisfied(), "constraints not satisfied");
65+
66+
Ok(Report {
67+
hash_fn: "sha256".into(),
68+
bytes,
69+
constraints: cs.num_constraints(),
70+
})
71+
}
72+
4773
fn pedersen_count(bytes: usize) -> Result<Report, failure::Error> {
4874
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
4975

@@ -103,6 +129,10 @@ pub fn run() -> Result<(), failure::Error> {
103129
pedersen_count(64)?,
104130
pedersen_count(128)?,
105131
pedersen_count(256)?,
132+
sha256_count(32)?,
133+
sha256_count(64)?,
134+
sha256_count(128)?,
135+
sha256_count(256)?,
106136
];
107137

108138
// print reports

fil-proofs-tooling/src/bin/benchy/stacked.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn file_backed_mmap_from_zeroes(n: usize, use_tmp: bool) -> Result<MmapMut, fail
4444
}
4545

4646
fn dump_proof_bytes<H: Hasher>(
47-
all_partition_proofs: &[Vec<stacked::Proof<H, Blake2sHasher>>],
47+
all_partition_proofs: &[Vec<stacked::Proof<H, Sha256Hasher>>],
4848
) -> Result<(), failure::Error> {
4949
let file = OpenOptions::new()
5050
.write(true)
@@ -132,7 +132,7 @@ where
132132
layer_challenges: layer_challenges.clone(),
133133
};
134134

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

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

151-
let pb = stacked::PublicInputs::<H::Domain, <Blake2sHasher as Hasher>::Domain> {
151+
let pb = stacked::PublicInputs::<H::Domain, <Sha256Hasher as Hasher>::Domain> {
152152
replica_id,
153153
seed,
154154
tau: Some(tau),
@@ -189,7 +189,7 @@ where
189189
wall_time: vanilla_proving_wall_time,
190190
return_value: all_partition_proofs,
191191
} = measure(|| {
192-
StackedDrg::<H, Blake2sHasher>::prove_all_partitions(
192+
StackedDrg::<H, Sha256Hasher>::prove_all_partitions(
193193
&pp,
194194
&pub_inputs,
195195
&priv_inputs,
@@ -218,7 +218,7 @@ where
218218

219219
for _ in 0..*samples {
220220
let m = measure(|| {
221-
let verified = StackedDrg::<H, Blake2sHasher>::verify_all_partitions(
221+
let verified = StackedDrg::<H, Sha256Hasher>::verify_all_partitions(
222222
&pp,
223223
&pub_inputs,
224224
&all_partition_proofs,
@@ -265,7 +265,7 @@ where
265265
if let Some(data) = d {
266266
if *extract {
267267
let m = measure(|| {
268-
StackedDrg::<H, Blake2sHasher>::extract_all(&pp, &replica_id, &data)
268+
StackedDrg::<H, Sha256Hasher>::extract_all(&pp, &replica_id, &data)
269269
.map_err(|err| err.into())
270270
})?;
271271

@@ -296,9 +296,9 @@ struct CircuitWorkMeasurement {
296296
}
297297

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

323323
if *bench || *circuit {
324324
let mut cs = MetricCS::<Bls12>::new();
325-
<StackedCompound as CompoundProof<_, StackedDrg<H, Blake2sHasher>, _>>::blank_circuit(
325+
<StackedCompound as CompoundProof<_, StackedDrg<H, Sha256Hasher>, _>>::blank_circuit(
326326
&pp, &JJ_PARAMS,
327327
)
328328
.synthesize(&mut cs)?;
@@ -342,7 +342,7 @@ fn do_circuit_work<H: 'static + Hasher>(
342342
// We should also allow the serialized vanilla proofs to be passed (as a file) to the example
343343
// and skip replication/vanilla-proving entirely.
344344
let gparams =
345-
<StackedCompound as CompoundProof<_, StackedDrg<H, Blake2sHasher>, _>>::groth_params(
345+
<StackedCompound as CompoundProof<_, StackedDrg<H, Sha256Hasher>, _>>::groth_params(
346346
&compound_public_params.vanilla_params,
347347
&JJ_PARAMS,
348348
)?;

filecoin-proofs/src/api/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn get_unsealed_range<T: Into<PathBuf> + AsRef<Path>>(
4747
ticket: Ticket,
4848
offset: UnpaddedByteIndex,
4949
num_bytes: UnpaddedBytesAmount,
50-
) -> error::Result<(UnpaddedBytesAmount)> {
50+
) -> error::Result<UnpaddedBytesAmount> {
5151
let comm_d =
5252
as_safe_commitment::<<DefaultPieceHasher as Hasher>::Domain, _>(&comm_d, "comm_d")?;
5353

filecoin-proofs/src/constants.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ pub const MINIMUM_RESERVED_BYTES_FOR_PIECE_IN_FULLY_ALIGNED_SECTOR: u64 =
2323
pub const MIN_PIECE_SIZE: UnpaddedBytesAmount = UnpaddedBytesAmount(127);
2424

2525
/// The hasher used for creating comm_d.
26-
pub type DefaultPieceHasher = storage_proofs::hasher::Blake2sHasher;
26+
pub type DefaultPieceHasher = storage_proofs::hasher::Sha256Hasher;
2727

2828
pub use storage_proofs::drgraph::DefaultTreeHasher;

filecoin-proofs/src/fr32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ the unique bit `0`, that just *started* at that position but doesn't
441441
necessarily carry that value.)
442442
443443
**/
444-
pub fn shift_bits(input: &[u8], amount: usize, is_left: bool) -> (Vec<u8>) {
444+
pub fn shift_bits(input: &[u8], amount: usize, is_left: bool) -> Vec<u8> {
445445
debug_assert!(amount >= 1);
446446
debug_assert!(amount <= 7);
447447

rust-toolchain

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2019-10-28
1+
nightly-2019-11-06

0 commit comments

Comments
 (0)