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

fix(maat): proofs #600

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
73 changes: 71 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", tag =
frame-benchmarking-cli = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2" }
frame-executive = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
frame-support-test = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
frame-system-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions cli/polka-storage-provider/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bls12_381 = { workspace = true }
cid = { workspace = true, features = ["std"] }
clap = { workspace = true, features = ["derive"] }
codec = { workspace = true }
hex = { workspace = true }
jsonrpsee = { workspace = true, features = ["http-client", "macros", "server", "ws-client"] }
sc-cli = { workspace = true }
serde = { workspace = true }
Expand Down
76 changes: 66 additions & 10 deletions cli/polka-storage-provider/client/src/commands/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
str::FromStr,
};

use codec::Encode;
use mater::CarV2Reader;
use polka_storage_proofs::{
porep::{self, sealer::Sealer},
Expand All @@ -16,7 +17,11 @@ use primitives_commitment::{
piece::{PaddedPieceSize, PieceInfo},
Commitment,
};
use primitives_proofs::{derive_prover_id, RegisteredPoStProof, RegisteredSealProof};
use primitives_proofs::{
derive_prover_id,
randomness::{draw_randomness, DomainSeparationTag},
RegisteredPoStProof, RegisteredSealProof, SectorNumber,
};
use storagext::multipair::{MultiPairArgs, MultiPairSigner};
use subxt::tx::Signer;

Expand Down Expand Up @@ -72,6 +77,15 @@ pub enum ProofsCommand {
/// Directory where the proof files and the sector will be put. Defaults to the current directory.
#[arg(short, long)]
output_path: Option<PathBuf>,
/// Sector number
#[arg(long)]
sector_id: u32,
/// The height at which we draw the randomness for deriving a sealed cid.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should propose the defaults that were hardcoded here, If I were a new user, I wouldn't know where to put it and which number needs to be after which one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that we will eventually remove those commands? The defaults were also specific for a single use case

#[arg(long)]
seal_randomness_height: u64,
/// Precommit block number
#[arg(long)]
pre_commit_block_number: u64,
},
/// Generates PoSt verifying key and proving parameters for zk-SNARK workflows (submit windowed PoSt)
#[clap(name = "post-params")]
Expand Down Expand Up @@ -203,17 +217,42 @@ impl ProofsCommand {
commp,
output_path,
cache_directory,
sector_id,
seal_randomness_height,
pre_commit_block_number,
} => {
let Some(signer) = Option::<MultiPairSigner>::from(signer_key) else {
return Err(UtilsCommandError::NoSigner)?;
};
let prover_id = derive_prover_id(signer.account_id());

// Those are hardcoded for the showcase only.
// They should come from Storage Provider Node, precommits and other information.
let sector_id = 77.into();
let ticket = [12u8; 32];
let seed = [13u8; 32];
let sector_number = SectorNumber::try_from(sector_id)
.map_err(|_| UtilsCommandError::InvalidSectorId)?;

let entropy = signer.account_id().encode();
println!("Entropy: {}", hex::encode(&entropy));

let ticket = get_randomness(
DomainSeparationTag::SealRandomness,
seal_randomness_height,
&entropy,
);
println!(
"[{seal_randomness_height}] Ticket randomness: {}",
hex::encode(ticket)
);

// The number added is configured in runtime:
// https://github.com/eigerco/polka-storage/blob/18207759d7c6c175916d5bed70246d94a8f028f4/runtime/src/configs/mod.rs#L360
let interactive_block_number = pre_commit_block_number + 10;
cernicc marked this conversation as resolved.
Show resolved Hide resolved
let seed = get_randomness(
DomainSeparationTag::InteractiveSealChallengeSeed,
interactive_block_number,
&entropy,
);
println!(
"[{interactive_block_number}] Seed randomness: {}",
hex::encode(seed)
);

let output_path = if let Some(output_path) = output_path {
output_path
Expand Down Expand Up @@ -273,14 +312,17 @@ impl ProofsCommand {
.create_sector(vec![(piece_file, piece_info)], unsealed_sector)
.map_err(|e| UtilsCommandError::GeneratePoRepError(e))?;

let prover_id = derive_prover_id(signer.account_id());
println!("Prover ID: {}", hex::encode(prover_id));

println!("Precommitting...");
let precommit = sealer
.precommit_sector(
&cache_directory,
unsealed_sector_path,
&sealed_sector_path,
prover_id,
sector_id,
sector_number,
ticket,
&piece_infos,
)
Expand All @@ -293,7 +335,7 @@ impl ProofsCommand {
&cache_directory,
&sealed_sector_path,
prover_id,
sector_id,
sector_number,
ticket,
Some(seed),
precommit,
Expand All @@ -310,8 +352,10 @@ impl ProofsCommand {
.clone()
.try_into()
.expect("converstion between rust-fil-proofs and polka-storage-proofs to work");
proof_scale_file.write_all(&codec::Encode::encode(&proof_scale))?;
let scale_encoded_proof = codec::Encode::encode(&proof_scale);
proof_scale_file.write_all(&scale_encoded_proof)?;

println!("Proof as HEX: {}", hex::encode(scale_encoded_proof));
println!("Wrote proof to {}", proof_scale_filename.display());
}
ProofsCommand::GeneratePoStParams {
Expand Down Expand Up @@ -446,6 +490,8 @@ pub enum UtilsCommandError {
InvalidPieceCommP(String, cid::Error),
#[error("invalid piece type")]
InvalidPieceType(String, &'static str),
#[error("invalid sector id")]
InvalidSectorId,
#[error("file {0} is invalid CARv2 file {1}")]
InvalidCARv2(PathBuf, mater::Error),
#[error("no signer key was provider")]
Expand All @@ -465,3 +511,13 @@ fn file_with_extension(
.map_err(|e| UtilsCommandError::FileCreateError(new_path.clone(), e))?;
Ok((new_path, file))
}

fn get_randomness(
personalization: DomainSeparationTag,
block_number: u64,
entropy: &[u8],
) -> [u8; 32] {
// This randomness digest is hardcoded because it's always same on testnet.
let digest = [0u8; 32];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's not [0u8; 32] on the testnet, right?

Copy link
Member Author

@cernicc cernicc Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered here. It is now :D

draw_randomness(&digest, personalization, block_number, &entropy)
}
4 changes: 2 additions & 2 deletions lib/polka-storage-proofs/src/porep/sealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Sealer {
unsealed_sector,
sealed_sector,
prover_id,
sector_id.into(),
storage_proofs_core::sector::SectorId::from(u64::from(sector_id)),
ticket,
&piece_infos,
)?;
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Sealer {
cache_path,
replica_path,
prover_id,
sector_id.into(),
storage_proofs_core::sector::SectorId::from(u64::from(sector_id)),
ticket,
seed,
pre_commit.into(),
Expand Down
2 changes: 1 addition & 1 deletion lib/polka-storage-proofs/src/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn generate_window_post<CacheDirectory: AsRef<Path>>(
let mut replicas = BTreeMap::new();
for replica in partition_replicas {
replicas.insert(
replica.sector_id.into(),
storage_proofs_core::sector::SectorId::from(u64::from(replica.sector_id)),
PrivateReplicaInfo::<Tree>::new(
replica.replica_path,
replica.comm_r,
Expand Down
22 changes: 22 additions & 0 deletions maat/generate_porep_proof.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Clear cache before run
CACHE_FOLDER="/tmp/psp-cache"
rm -r "$CACHE_FOLDER"
mkdir "$CACHE_FOLDER"

PROVIDER="//Charlie"
CAR_FILE="../examples/test-data-big.car"
SECTOR_CID="baga6ea4seaqbfhdvmk5qygevit25ztjwl7voyikb5k2fqcl2lsuefhaqtukuiii"
PARAMS_PATH="../2KiB.porep.params"
SECTOR_ID=1
SEAL_RANDOMNESS_HEIGHT=20
PRE_COMMIT_BLOCK_NUMBER=30

polka-storage-provider-client proofs porep \
--sr25519-key "$PROVIDER" \
--proof-parameters-path "$PARAMS_PATH" \
--cache-directory "$CACHE_FOLDER" \
--sector-id "$SECTOR_ID" \
--seal-randomness-height "$SEAL_RANDOMNESS_HEIGHT" \
--pre-commit-block-number "$PRE_COMMIT_BLOCK_NUMBER" \
"$CAR_FILE" \
"$SECTOR_CID"
Loading