Skip to content

Commit

Permalink
fix: add support for multiple proof types at the network level (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte authored Feb 18, 2025
1 parent 176455a commit 7f298e8
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 108 deletions.
8 changes: 4 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ generate-proof-params sector-size:
cargo r -r -p polka-storage-provider-client -- proofs post-params --post-type "{{sector-size}}"

bench-test pallet:
cargo test --profile ci --locked -p pallet-{{pallet}} --features runtime-benchmarks -- benchmark --nocapture
cargo test --profile ci --locked -p "pallet-{{pallet}}" --features runtime-benchmarks -- benchmark --nocapture

bench-node pallet:
bench-node pallet steps="5" repeat="1":
cargo run \
-p polka-storage-node -r -F runtime-benchmarks -F testnet -- \
benchmark pallet \
--wasm-execution=compiled \
--pallet "pallet_{{pallet}}" \
--extrinsic "*" \
--steps 5 \
--repeat 1 \
--steps "{{steps}}" \
--repeat "{{repeat}}" \
--template node/benchmark_template.hbs
4 changes: 2 additions & 2 deletions examples/start_sp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ wait
# Each of the keys is different, because the processes are running in parallel.
# If they were running in parallel on the same account, they'd conflict with each other on the transaction nonce.
RUST_LOG=debug target/release/storagext-cli --sr25519-key "//Charlie" storage-provider register --post-proof "8MiB" "$P2P_SP_PEER_ID" &
RUST_LOG=debug target/release/storagext-cli --sr25519-key "//Alice" proofs set-porep-verifying-key @8MiB.porep.vk.scale &
RUST_LOG=debug target/release/storagext-cli --sr25519-key "//Bob" proofs set-post-verifying-key @8MiB.post.vk.scale &
RUST_LOG=debug target/release/storagext-cli --sr25519-key "//Alice" proofs set-porep-verifying-key --registered-proof 8MiB @8MiB.porep.vk.scale &
RUST_LOG=debug target/release/storagext-cli --sr25519-key "//Bob" proofs set-post-verifying-key --registered-proof 8MiB @8MiB.post.vk.scale &
wait

echo "seal_proof = '8MiB'
Expand Down
2 changes: 1 addition & 1 deletion lib/polka-storage-proofs/src/groth16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<E> VerifyingKey<E>
where
E: Engine<G1Affine = G1Affine, G2Affine = G2Affine>,
{
/// Serialises the `VerifiyingKey` into a byte stream and writes it to the given buffer.
/// Serialises the `VerifyingKey` into a byte stream and writes it to the given buffer.
pub fn into_bytes(&self, buf: &mut [u8]) -> Result<(), IntoBytesError> {
if buf.len() < self.serialised_bytes() {
return Err(IntoBytesError::InsufficientBufferLength);
Expand Down
13 changes: 10 additions & 3 deletions maat/tests/real_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use libp2p::PeerId;
use maat::*;
use polka_storage_proofs::{porep, post};
use polka_storage_provider_common::{commp::commp, deadline::Deadline, sector::UnsealedSector};
use primitives::{proofs::RegisteredPoStProof, sector::SectorNumber};
use primitives::{
proofs::{RegisteredPoStProof, RegisteredSealProof},
sector::SectorNumber,
};
use storagext::{
clients::ProofsClientExt,
multipair::MultiPairSigner,
Expand Down Expand Up @@ -92,12 +95,13 @@ where
async fn set_porep_verifying_key<Keypair>(
client: &storagext::Client,
charlie: &Keypair,
seal_proof: RegisteredSealProof,
vk: VerifyingKey,
) where
Keypair: subxt::tx::Signer<PolkaStorageConfig>,
{
let result = client
.set_porep_verifying_key(charlie, vk, true)
.set_porep_verifying_key(charlie, seal_proof, vk, true)
.await
.unwrap()
.unwrap();
Expand All @@ -114,12 +118,13 @@ async fn set_porep_verifying_key<Keypair>(
async fn set_post_verifying_key<Keypair>(
client: &storagext::Client,
charlie: &Keypair,
post_proof: RegisteredPoStProof,
vk: VerifyingKey,
) where
Keypair: subxt::tx::Signer<PolkaStorageConfig>,
{
let result = client
.set_post_verifying_key(charlie, vk, true)
.set_post_verifying_key(charlie, post_proof, vk, true)
.await
.unwrap()
.unwrap();
Expand Down Expand Up @@ -336,6 +341,7 @@ async fn real_world_use_case() {
set_porep_verifying_key(
&client,
&charlie_kp,
seal_proof,
VerifyingKey::from_raw_bytes(porep_vk_scale),
)
.await;
Expand All @@ -348,6 +354,7 @@ async fn real_world_use_case() {
set_post_verifying_key(
&client,
&charlie_kp,
post_proof,
VerifyingKey::from_raw_bytes(post_vk_scale),
)
.await;
Expand Down
19 changes: 15 additions & 4 deletions pallets/proofs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::Pallet as ProofsPallet;
where T: crate::Config
)]
mod benchmarks {
use primitives::proofs::{RegisteredPoStProof, RegisteredSealProof};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

Expand All @@ -33,9 +34,13 @@ mod benchmarks {
.unwrap();

#[extrinsic_call]
_(RawOrigin::Signed(whitelisted_caller()), vkey_bytes);
_(
RawOrigin::Signed(whitelisted_caller()),
RegisteredSealProof::StackedDRG2KiBV1P1,
vkey_bytes,
);

assert!(PoRepVerifyingKey::<T>::get().is_some());
assert!(PoRepVerifyingKeys::<T>::get(&RegisteredSealProof::StackedDRG2KiBV1P1).is_some());
}

#[benchmark]
Expand All @@ -48,9 +53,15 @@ mod benchmarks {
.unwrap();

#[extrinsic_call]
_(RawOrigin::Signed(whitelisted_caller()), vkey_bytes);
_(
RawOrigin::Signed(whitelisted_caller()),
RegisteredPoStProof::StackedDRGWindow2KiBV1P1,
vkey_bytes,
);

assert!(PoStVerifyingKey::<T>::get().is_some());
assert!(
PoStVerifyingKeys::<T>::get(&RegisteredPoStProof::StackedDRGWindow2KiBV1P1).is_some()
);
}

impl_benchmark_test_suite! {
Expand Down
58 changes: 31 additions & 27 deletions pallets/proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,27 @@ pub mod pallet {
#[pallet::pallet]
pub struct Pallet<T>(_);

/// Verifying Key for verifying all of the PoRep proofs generated for 2KiB sectors.
/// One per runtime.
///
/// It should be set via some kind of trusted setup procedure.
/// /// Test key can be generated via `polka-storage-provider-client utils porep-params`.
/// To support more sector sizes for proofs, this data structure would need to be a Map from Sector Size to a Verifying Key.
/// [`VerifyingKey`]s for Proofs of Replication.
#[pallet::storage]
pub type PoRepVerifyingKey<T: Config> = StorageValue<_, VerifyingKey<Bls12>, OptionQuery>;

/// Verifying Key for verifying all of the PoSt proofs generated for 2KiB sectors.
/// One per runtime.
///
/// It should be set via some kind of trusted setup procedure.
/// Test key can be generated via `polka-storage-provider-client utils post-params`.
/// To support more sector sizes for proofs, this data structure would need to be a Map from Sector Size to a Verifying Key.
pub type PoRepVerifyingKeys<T: Config> =
StorageMap<_, Blake2_128Concat, RegisteredSealProof, VerifyingKey<Bls12>>;

/// [`VerifyingKey`]s for Proofs of Spacetime.
#[pallet::storage]
pub type PoStVerifyingKey<T: Config> = StorageValue<_, VerifyingKey<Bls12>, OptionQuery>;
pub type PoStVerifyingKeys<T: Config> =
StorageMap<_, Blake2_128Concat, RegisteredPoStProof, VerifyingKey<Bls12>>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
PoRepVerifyingKeyChanged { who: T::AccountId },
PoStVerifyingKeyChanged { who: T::AccountId },
PoRepVerifyingKeyChanged {
who: T::AccountId,
proof: RegisteredSealProof,
},
PoStVerifyingKeyChanged {
who: T::AccountId,
proof: RegisteredPoStProof,
},
}

#[pallet::error]
Expand All @@ -95,6 +93,7 @@ pub mod pallet {
#[pallet::weight((T::WeightInfo::set_porep_verifying_key(), DispatchClass::Operational))]
pub fn set_porep_verifying_key(
origin: OriginFor<T>,
registered_seal_proof: RegisteredSealProof,
verifying_key: crate::Vec<u8>,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Expand All @@ -104,17 +103,19 @@ pub mod pallet {
Error::<T>::Conversion
})?;

PoRepVerifyingKey::<T>::set(Some(vkey));

Self::deposit_event(Event::PoRepVerifyingKeyChanged { who: caller });

PoRepVerifyingKeys::<T>::insert(registered_seal_proof, vkey);
Self::deposit_event(Event::PoRepVerifyingKeyChanged {
who: caller,
proof: registered_seal_proof,
});
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight((T::WeightInfo::set_post_verifying_key(), DispatchClass::Operational))]
pub fn set_post_verifying_key(
origin: OriginFor<T>,
registered_post_proof: RegisteredPoStProof,
verifying_key: crate::Vec<u8>,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Expand All @@ -124,10 +125,11 @@ pub mod pallet {
Error::<T>::Conversion
})?;

PoStVerifyingKey::<T>::set(Some(vkey));

Self::deposit_event(Event::PoStVerifyingKeyChanged { who: caller });

PoStVerifyingKeys::<T>::insert(registered_post_proof, vkey);
Self::deposit_event(Event::PoStVerifyingKeyChanged {
who: caller,
proof: registered_post_proof,
});
Ok(())
}
}
Expand Down Expand Up @@ -166,7 +168,8 @@ pub mod pallet {
}
let proof_scheme = porep::ProofScheme::setup(seal_proof);

let vkey = PoRepVerifyingKey::<T>::get().ok_or(Error::<T>::MissingPoRepVerifyingKey)?;
let vkey = PoRepVerifyingKeys::<T>::get(seal_proof)
.ok_or(Error::<T>::MissingPoRepVerifyingKey)?;
log::info!(target: LOG_TARGET, "Verifying PoRep proof for sector: {}...", sector);
proof_scheme
.verify(
Expand Down Expand Up @@ -220,7 +223,8 @@ pub mod pallet {

let proof_scheme = post::ProofScheme::setup(post_type);

let vkey = PoStVerifyingKey::<T>::get().ok_or(Error::<T>::MissingPoStVerifyingKey)?;
let vkey = PoStVerifyingKeys::<T>::get(post_type)
.ok_or(Error::<T>::MissingPoStVerifyingKey)?;
proof_scheme
.verify(randomness, replicas.clone(), vkey, parsed_proofs)
.map_err(|e| {
Expand Down
12 changes: 9 additions & 3 deletions pallets/proofs/src/tests/porep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@ use rand_xorshift::XorShiftRng;
use sp_core::bounded_vec;
use sp_runtime::BoundedVec;

use crate::{mock::*, tests::TEST_SEED, Error, PoRepVerifyingKey};
use crate::{mock::*, tests::TEST_SEED, Error, PoRepVerifyingKeys};

#[test]
fn sets_porep_verifying_key() {
new_test_ext().execute_with(|| {
assert_eq!(None, PoRepVerifyingKey::<Test>::get());
let proof = RegisteredSealProof::StackedDRG2KiBV1P1;
assert_eq!(None, PoRepVerifyingKeys::<Test>::get(proof));
let vk = default_porep_verifyingkey();

assert_ok!(ProofsModule::set_porep_verifying_key(
RuntimeOrigin::signed(1),
proof,
vk.clone()
));
let scale_vk: VerifyingKey<Bls12> = Decode::decode(&mut vk.as_slice()).unwrap();
assert_eq!(Some(scale_vk), PoRepVerifyingKey::<Test>::get());
assert_eq!(Some(scale_vk), PoRepVerifyingKeys::<Test>::get(proof));
});
}

#[test]
fn verification_invalid_verifyingkey() {
new_test_ext().execute_with(|| {
let proof = RegisteredSealProof::StackedDRG2KiBV1P1;
let mut rng = XorShiftRng::from_seed(TEST_SEED);
let vkey = Encode::encode(&VerifyingKey::<Bls12>::random(&mut rng));

assert_ok!(ProofsModule::set_porep_verifying_key(
RuntimeOrigin::signed(1),
proof,
vkey
));

Expand Down Expand Up @@ -72,9 +76,11 @@ fn porep_verification_succeeds() {
let comm_d = default_porep_comm_d();
let proof_bytes = default_porep_proof();
let vkey_bytes = default_porep_verifyingkey();
let proof = RegisteredSealProof::StackedDRG2KiBV1P1;

assert_ok!(ProofsModule::set_porep_verifying_key(
RuntimeOrigin::signed(1),
proof,
vkey_bytes
));
assert_ok!(<ProofsModule as ProofVerification>::verify_porep(
Expand Down
10 changes: 7 additions & 3 deletions pallets/proofs/src/tests/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ use sp_core::bounded_vec;
use sp_runtime::{BoundedBTreeMap, BoundedVec};
use sp_std::collections::btree_map::BTreeMap;

use crate::{mock::*, tests::TEST_SEED, Error, PoRepVerifyingKey, PoStVerifyingKey};
use crate::{mock::*, tests::TEST_SEED, Error, PoStVerifyingKeys};

#[test]
fn sets_post_verifying_key() {
new_test_ext().execute_with(|| {
assert_eq!(None, PoRepVerifyingKey::<Test>::get());
let proof = RegisteredPoStProof::StackedDRGWindow2KiBV1P1;
assert_eq!(None, PoStVerifyingKeys::<Test>::get(proof));
let vk = default_post_verifyingkey();

assert_ok!(ProofsModule::set_post_verifying_key(
RuntimeOrigin::signed(1),
proof,
vk.clone()
));
let scale_vk: VerifyingKey<Bls12> = Decode::decode(&mut vk.as_slice()).unwrap();
assert_eq!(Some(scale_vk), PoStVerifyingKey::<Test>::get());
assert_eq!(Some(scale_vk), PoStVerifyingKeys::<Test>::get(proof));
});
}

Expand All @@ -38,6 +40,7 @@ fn post_verification_succeeds() {

assert_ok!(ProofsModule::set_post_verifying_key(
RuntimeOrigin::signed(1),
RegisteredPoStProof::StackedDRGWindow2KiBV1P1,
vkey_bytes
));

Expand All @@ -59,6 +62,7 @@ fn post_verification_fails() {

assert_ok!(ProofsModule::set_post_verifying_key(
RuntimeOrigin::signed(1),
RegisteredPoStProof::StackedDRGWindow2KiBV1P1,
vkey
));

Expand Down
Loading

0 comments on commit 7f298e8

Please sign in to comment.