Skip to content

Commit

Permalink
feat(storage-proofs): add pedersen hash precompute for x86_64 (#967)
Browse files Browse the repository at this point in the history
feat(storage-proofs): add pedersen hash precompute for x86_64
  • Loading branch information
dignifiedquire committed Dec 3, 2019
2 parents f5a2e0a + cb0e3a9 commit e5fdeb9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion fil-proofs-tooling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ chrono = { version = "0.4.7", features = ["serde"] }
memmap = "0.7.0"
bellperson = "0.4.1"
paired = "0.16.0"
fil-sapling-crypto = "0.2.0"
fil-sapling-crypto = "0.2.1"
rand = "0.7"
storage-proofs = { path = "../storage-proofs"}
filecoin-proofs = { path = "../filecoin-proofs"}
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ff = "0.5.0"
blake2b_simd = "0.5"
bellperson = "0.4.1"
paired = "0.16.0"
fil-sapling-crypto = "0.2.0"
fil-sapling-crypto = "0.2.1"
clap = "2"
log = "0.4.7"
fil_logger = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion storage-proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ toml = "0.5"
ff = "0.5.0"
bellperson = "0.4.1"
paired = { version = "0.16.0", features = ["serde"] }
fil-sapling-crypto = "0.2.0"
fil-sapling-crypto = "0.2.1"
serde_json = "1.0"
log = "0.4.7"
pretty_assertions = "0.6.1"
Expand Down
25 changes: 19 additions & 6 deletions storage-proofs/src/crypto/pedersen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ff::PrimeFieldRepr;
use fil_sapling_crypto::jubjub::JubjubBls12;
use fil_sapling_crypto::pedersen_hash::{pedersen_hash, Personalization};
use fil_sapling_crypto::pedersen_hash::Personalization;
use paired::bls12_381::{Bls12, Fr, FrRepr};

use crate::error::Result;
Expand All @@ -24,9 +24,15 @@ pub fn pedersen(data: &[u8]) -> Fr {
}

pub fn pedersen_bits<'a, S: Iterator<Item = &'a [u8]>>(data: Bits<&'a [u8], S>) -> Fr {
pedersen_hash::<Bls12, _>(Personalization::None, data, &JJ_PARAMS)
.into_xy()
.0
let digest = if cfg!(target_arch = "x86_64") {
use fil_sapling_crypto::pedersen_hash::pedersen_hash_bls12_381_with_precomp;
pedersen_hash_bls12_381_with_precomp::<_>(Personalization::None, data, &JJ_PARAMS)
} else {
use fil_sapling_crypto::pedersen_hash::pedersen_hash;
pedersen_hash::<Bls12, _>(Personalization::None, data, &JJ_PARAMS)
};

digest.into_xy().0
}

/// Pedersen hashing for inputs that have length mulitple of the block size `256`. Based on pedersen hashes and a Merkle-Damgard construction.
Expand Down Expand Up @@ -62,8 +68,15 @@ fn pedersen_compression_bits<T>(bits: T) -> FrRepr
where
T: IntoIterator<Item = bool>,
{
let (x, _) = pedersen_hash::<Bls12, _>(Personalization::None, bits, &JJ_PARAMS).into_xy();
x.into()
let digest = if cfg!(target_arch = "x86_64") {
use fil_sapling_crypto::pedersen_hash::pedersen_hash_bls12_381_with_precomp;
pedersen_hash_bls12_381_with_precomp::<_>(Personalization::None, bits, &JJ_PARAMS)
} else {
use fil_sapling_crypto::pedersen_hash::pedersen_hash;
pedersen_hash::<Bls12, _>(Personalization::None, bits, &JJ_PARAMS)
};

digest.into_xy().0.into()
}

#[derive(Debug, Clone)]
Expand Down
19 changes: 14 additions & 5 deletions storage-proofs/src/hasher/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bellperson::{ConstraintSystem, SynthesisError};
use ff::{Field, PrimeField, PrimeFieldRepr};
use fil_sapling_crypto::circuit::pedersen_hash as pedersen_hash_circuit;
use fil_sapling_crypto::jubjub::JubjubEngine;
use fil_sapling_crypto::pedersen_hash::{pedersen_hash, Personalization};
use fil_sapling_crypto::pedersen_hash::Personalization;
use merkletree::hash::{Algorithm as LightAlgorithm, Hashable};
use merkletree::merkle::Element;
use paired::bls12_381::{Bls12, Fr, FrRepr};
Expand Down Expand Up @@ -260,10 +260,19 @@ impl LightAlgorithm<PedersenDomain> for PedersenFunction {
) -> PedersenDomain {
let node_bits = NodeBits::new(&(left.0).0[..], &(right.0).0[..]);

pedersen_hash::<Bls12, _>(Personalization::None, node_bits, &pedersen::JJ_PARAMS)
.into_xy()
.0
.into()
let digest = if cfg!(target_arch = "x86_64") {
use fil_sapling_crypto::pedersen_hash::pedersen_hash_bls12_381_with_precomp;
pedersen_hash_bls12_381_with_precomp::<_>(
Personalization::None,
node_bits,
&pedersen::JJ_PARAMS,
)
} else {
use fil_sapling_crypto::pedersen_hash::pedersen_hash;
pedersen_hash::<Bls12, _>(Personalization::None, node_bits, &pedersen::JJ_PARAMS)
};

digest.into_xy().0.into()
}
}

Expand Down
34 changes: 24 additions & 10 deletions storage-proofs/src/test_helper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr};
use fil_sapling_crypto::pedersen_hash;
use fil_sapling_crypto::pedersen_hash::Personalization;
use paired::bls12_381::{Bls12, Fr};
use rand::Rng;

Expand Down Expand Up @@ -161,15 +161,29 @@ pub fn random_merkle_path_with_value<R: Rng>(
lhs.reverse();
rhs.reverse();

cur = pedersen_hash::pedersen_hash::<Bls12, _>(
pedersen_hash::Personalization::None,
lhs.into_iter()
.take(Fr::NUM_BITS as usize)
.chain(rhs.into_iter().take(Fr::NUM_BITS as usize)),
&crypto::pedersen::JJ_PARAMS,
)
.into_xy()
.0;
cur = if cfg!(target_arch = "x86_64") {
use fil_sapling_crypto::pedersen_hash::pedersen_hash_bls12_381_with_precomp;
pedersen_hash_bls12_381_with_precomp::<_>(
Personalization::None,
lhs.into_iter()
.take(Fr::NUM_BITS as usize)
.chain(rhs.into_iter().take(Fr::NUM_BITS as usize)),
&crypto::pedersen::JJ_PARAMS,
)
.into_xy()
.0
} else {
use fil_sapling_crypto::pedersen_hash::pedersen_hash;
pedersen_hash::<Bls12, _>(
Personalization::None,
lhs.into_iter()
.take(Fr::NUM_BITS as usize)
.chain(rhs.into_iter().take(Fr::NUM_BITS as usize)),
&crypto::pedersen::JJ_PARAMS,
)
.into_xy()
.0
};
}

(auth_path, cur)
Expand Down

0 comments on commit e5fdeb9

Please sign in to comment.