Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clarify h and h_select #1696

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion filecoin-proofs/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub use util::*;
pub use window_post::*;
pub use winning_post::*;

pub use storage_proofs_update::constants::{hs, partition_count};
pub use storage_proofs_update::constants::partition_count;

/// Unseals the sector at `sealed_path` and returns the bytes for a piece
/// whose first (unpadded) byte begins at `offset` and ends at `offset` plus
Expand Down
20 changes: 10 additions & 10 deletions filecoin-proofs/src/api/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn encode_into<Tree: 'static + MerkleTreeTrait<Hasher = TreeRHasher>>(
sector_key_path,
sector_key_cache_path,
staged_data_path,
usize::from(config.h_select),
config.h,
)?;

let mut comm_d = [0; 32];
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn decode_from<Tree: 'static + MerkleTreeTrait<Hasher = TreeRHasher>>(
<Tree::Hasher as Hasher>::Domain::try_from_bytes(&p_aux.comm_c.into_bytes())?,
comm_d_new.into(),
<Tree::Hasher as Hasher>::Domain::try_from_bytes(&p_aux.comm_r_last.into_bytes())?,
usize::from(config.h_select),
config.h,
)?;

info!("decode_from:finish");
Expand Down Expand Up @@ -268,7 +268,7 @@ pub fn remove_encoded_data<Tree: 'static + MerkleTreeTrait<Hasher = TreeRHasher>
<Tree::Hasher as Hasher>::Domain::try_from_bytes(&p_aux.comm_c.into_bytes())?,
comm_d_new.into(),
<Tree::Hasher as Hasher>::Domain::try_from_bytes(&p_aux.comm_r_last.into_bytes())?,
usize::from(config.h_select),
config.h,
)?;

// Persist p_aux and t_aux into the sector_key_cache_path here
Expand Down Expand Up @@ -314,7 +314,7 @@ pub fn generate_single_partition_proof<Tree: 'static + MerkleTreeTrait<Hasher =
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};

let t_aux_old = get_t_aux::<Tree>(sector_key_cache_path)?;
Expand Down Expand Up @@ -367,7 +367,7 @@ pub fn verify_single_partition_proof<Tree: 'static + MerkleTreeTrait<Hasher = Tr
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};

let valid = EmptySectorUpdate::<Tree>::verify(&public_params, &public_inputs, &proof)?;
Expand Down Expand Up @@ -406,7 +406,7 @@ pub fn generate_partition_proofs<Tree: 'static + MerkleTreeTrait<Hasher = TreeRH
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};

let t_aux_old = get_t_aux::<Tree>(sector_key_cache_path)?;
Expand Down Expand Up @@ -458,7 +458,7 @@ pub fn verify_partition_proofs<Tree: 'static + MerkleTreeTrait<Hasher = TreeRHas
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};

let valid =
Expand Down Expand Up @@ -494,7 +494,7 @@ pub fn generate_empty_sector_update_proof_with_vanilla<
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};

let setup_params_compound = compound_proof::SetupParams {
Expand Down Expand Up @@ -547,7 +547,7 @@ pub fn generate_empty_sector_update_proof<Tree: 'static + MerkleTreeTrait<Hasher
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};

let t_aux_old = get_t_aux::<Tree>(sector_key_cache_path)?;
Expand Down Expand Up @@ -607,7 +607,7 @@ pub fn verify_empty_sector_update_proof<Tree: 'static + MerkleTreeTrait<Hasher =
comm_r_old: comm_r_old_safe,
comm_d_new: comm_d_new_safe,
comm_r_new: comm_r_new_safe,
h: usize::from(config.h_select),
h: config.h,
};
let setup_params_compound = compound_proof::SetupParams {
vanilla_params: SetupParams {
Expand Down
32 changes: 0 additions & 32 deletions filecoin-proofs/src/types/hselect.rs

This file was deleted.

2 changes: 0 additions & 2 deletions filecoin-proofs/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use storage_proofs_post::fallback;
use crate::constants::DefaultPieceHasher;

mod bytes_amount;
mod hselect;
mod piece_info;
mod porep_config;
mod porep_proof_partitions;
Expand All @@ -25,7 +24,6 @@ mod sector_update_config;
mod update_proof_partitions;

pub use bytes_amount::*;
pub use hselect::*;
pub use piece_info::*;
pub use porep_config::*;
pub use porep_proof_partitions::*;
Expand Down
8 changes: 4 additions & 4 deletions filecoin-proofs/src/types/sector_update_config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use storage_proofs_core::util::NODE_SIZE;
use storage_proofs_update::constants::partition_count;
use storage_proofs_update::constants::{h_default, partition_count};

use crate::types::{HSelect, PoRepConfig, SectorSize, UpdateProofPartitions};
use crate::types::{PoRepConfig, SectorSize, UpdateProofPartitions};

#[derive(Clone, Copy, Debug)]
pub struct SectorUpdateConfig {
pub sector_size: SectorSize,
pub nodes_count: usize,
pub update_partitions: UpdateProofPartitions,
pub h_select: HSelect,
pub h: usize,
}

impl SectorUpdateConfig {
Expand All @@ -19,7 +19,7 @@ impl SectorUpdateConfig {
sector_size: porep_config.sector_size,
nodes_count,
update_partitions: UpdateProofPartitions::from(partition_count(nodes_count)),
h_select: HSelect::from_nodes(nodes_count),
h: h_default(nodes_count),
}
}
}
11 changes: 3 additions & 8 deletions storage-proofs-update/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use storage_proofs_core::{

use crate::{
constants::{
apex_leaf_count, challenge_count, hs, partition_count, validate_tree_r_shape, TreeD,
TreeDArity, TreeDDomain, TreeDHasher, TreeRDomain, TreeRHasher,
apex_leaf_count, challenge_count, h_select, hs, partition_count, validate_tree_r_shape,
TreeD, TreeDArity, TreeDDomain, TreeDHasher, TreeRDomain, TreeRHasher,
POSEIDON_CONSTANTS_GEN_RANDOMNESS,
},
gadgets::{apex_por, gen_challenge_bits, get_challenge_high_bits, label_r_new},
Expand Down Expand Up @@ -58,12 +58,7 @@ impl PublicInputs {
"partition-index `k` exceeds partition-count for sector-size"
);

let hs_index = hs(sector_nodes)
.iter()
.position(|h_allowed| *h_allowed == h)
.expect("invalid `h` for sector-size");

let h_select = 1u64 << hs_index;
let h_select = h_select(sector_nodes, h);

let partition_bit_len = partition_count.trailing_zeros() as usize;
let k_and_h_select = (k as u64) | (h_select << partition_bit_len);
Expand Down
28 changes: 25 additions & 3 deletions storage-proofs-update/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ pub const fn challenge_count_poseidon(sector_nodes: usize) -> usize {
challenge_count(sector_nodes) * partition_count(sector_nodes)
}

// Returns the `h` values allowed for the given sector-size. Each `h` value is a possible number of
// high bits taken from each challenge `c`. A single value of `h = hs[i]` is taken from `hs` for
// each proof; the circuit takes `h_select = 2^i` as a public input.
/// Returns the `h` values allowed for the given sector-size. Each `h` value is a possible number
/// of high bits taken from each challenge `c`. A single value of `h = hs[i]` is taken from `hs`
/// for each proof; the circuit takes `h_select = 2^i` as a public input.
///
/// Those values are hard-coded for the circuit and cannot be changed without another trusted
/// setup.
pub const fn hs(sector_nodes: usize) -> [usize; 6] {
if sector_nodes <= SECTOR_SIZE_32_KIB {
[1; 6]
Expand All @@ -100,6 +103,25 @@ pub const fn hs(sector_nodes: usize) -> [usize; 6] {
}
}

/// Returns the `h` for the given sector-size. The `h` value is the number of high bits taken from
/// each challenge `c`. For production use, it was determined to use the value at index 3, which
/// translates to a value of 10 for production sector sizes.
pub const fn h_default(sector_nodes: usize) -> usize {
hs(sector_nodes)[3]
}

/// Returns the bit pattern to select the `h` value from the list of hard-coded `h` values within
/// the circuit.
pub fn h_select(sector_nodes: usize, h: usize) -> u64 {
// The list of possible `h` values is hard-coded in the circuit, hence their index instead of
// the actual values can be used within the circuit.
let h_index = hs(sector_nodes)
.iter()
.position(|h_allowed| *h_allowed == h)
.expect("invalid `h` for sector-size");
1u64 << h_index
}

// The number of leafs in each partition's apex-tree.
pub const fn apex_leaf_count(sector_nodes: usize) -> usize {
if sector_nodes <= SECTOR_SIZE_8_KIB {
Expand Down
9 changes: 2 additions & 7 deletions storage-proofs-update/src/poseidon/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use storage_proofs_core::{

use crate::{
constants::{
challenge_count_poseidon, hs, validate_tree_r_shape, TreeRDomain, TreeRHasher,
challenge_count_poseidon, h_select, hs, validate_tree_r_shape, TreeRDomain, TreeRHasher,
POSEIDON_CONSTANTS_GEN_RANDOMNESS,
},
gadgets::{gen_challenge_bits, get_challenge_high_bits, label_r_new},
Expand Down Expand Up @@ -48,12 +48,7 @@ impl PublicInputs {
comm_d_new: TreeRDomain,
comm_r_new: TreeRDomain,
) -> Self {
let hs_index = hs(sector_nodes)
.iter()
.position(|h_allowed| *h_allowed == h)
.expect("invalid `h` for sector-size");

let h_select = 1u64 << hs_index;
let h_select = h_select(sector_nodes, h);

PublicInputs {
h_select: Some(Fr::from(h_select)),
Expand Down