Skip to content

Commit

Permalink
Add support for sp1 v4.0.0 (#713)
Browse files Browse the repository at this point in the history
Add support for sp1 v4.
  • Loading branch information
0xSasaPrsic authored Jan 23, 2025
1 parent 9319c61 commit 305e82e
Show file tree
Hide file tree
Showing 8 changed files with 1,435 additions and 834 deletions.
2,185 changes: 1,386 additions & 799 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pallets/vector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sp-runtime = { workspace = true, default-features = false }
sp-std = { workspace = true, default-features = false }
sp-core = { workspace = true, default-features = false }
frame-benchmarking = { workspace = true, default-features = false, optional = true }
sp1-verifier = { version = "3.4.0", default-features = false }
sp1-verifier = { version = "4.0.0", default-features = false }
alloy-sol-types = { version = "0.8.12", default-features = false }

ark-bn254.workspace = true
Expand All @@ -40,7 +40,7 @@ ethabi.workspace = true
[dev-dependencies]
pallet-balances = { workspace = true, default-features = false, features = ["std"] }
pallet-timestamp = { workspace = true, default-features = false }
sp1-sdk = { version = "3.4.0", default-features = false }
sp1-sdk = { version = "4.0.0", default-features = false }

[features]
default = [ "std" ]
Expand Down
23 changes: 12 additions & 11 deletions pallets/vector/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub const ROTATE_VK: &str = r#"{"vk_json":{
// Generated with SP1 Helios https://github.com/succinctlabs/sp1-helios/blob/main/README.md
// cargo prove key —-elf (sp1 helios elf) in SP1 Helios
const SP1_VERIFICATION_KEY: [u8; 32] =
hex!("00788ce8dc2970920a3d3c072c8c07843d15f1307a53b3dd31b113c3e71c28e8");
hex!("00525ee0ed579ba497061070aea9d4918808790c2aa2b5dcb8fe94bdde4ddbb6");

#[benchmarks(where
[u8; 32]: From << T as frame_system::Config >::AccountId >,
Expand Down Expand Up @@ -502,20 +502,21 @@ mod benchmarks {

let slots_per_period = 8192;
let finality_threshold = 342u16;
let slot = 6178816u64;
ConfigurationStorage::<T>::set(Configuration {
slots_per_period,
finality_threshold,
});
let slot = 6724864u64;
let current_period = slot / slots_per_period;

let period = slot / slots_per_period;
SyncCommitteeHashes::<T>::set(
period,
current_period,
H256(hex!(
"c00e7928895533bafa24fffba54cc7660664eb0134bffe39932fb28a3ddd4b46"
"cfde56afdd0992f3fc0a576c2b8959f66a88d47f2886046c104fe6e14da386ac"
)),
);

ConfigurationStorage::<T>::set(Configuration {
slots_per_period,
finality_threshold,
});

Updater::<T>::set(H256(ACCOUNT1));
let account = T::AccountId::from(ACCOUNT1);
let origin = RawOrigin::Signed(account.clone());
Expand Down Expand Up @@ -633,9 +634,9 @@ fn get_valid_rotate_proof() -> FunctionProof {
}

fn get_valid_sp1_proof() -> ProofInput {
BoundedVec::truncate_from(hex!("0906909023b57ea329344437d01eaa453caa08c01bad6662a15eaf5b1a4f8772e4ce5c180655f20b52a401653cc55ef33311435238f5aae57d5295093b2fafa8ec1949a52d618596b003c3ec290f33f87032a9662fd03c5fae18d93adaf921a2a133e7620fa4cde186d77895b90765ffada8862171398d6d8ec81ac0418c77832fbfdcfd0316ceb5d7a6b371a6438865ad8b66f737a4f702f4ba9487ec3e75ad2bfc68a828cc30d8bb3c035c48ceae35091e41a5417704ef220f1555a2dfd1b72433637c16bb5ed83108bb7d0ba2cce357639d2595c032e991a8a7ebb59737a906ba2abd17e4aa255f659dc7f22c520ea74f62377d6a5342490b3d84c423e573998c7924").to_vec())
BoundedVec::truncate_from(hex!("11b6a09d118ac1f4d588d81a97770ff506c49ec0a7819e2cee1d043f76b381b16b4795ea249ad3f7aaccbffcf499a91d1bce5851383c9bf7a75bb95408e5c6bebc3cb8fc2413e6b5c527d5907e89a81cd8a17987208164cc88250db8720d8375a13291fc24f583f49d19d2f40afffd59fb65c40ad32e96fe37ef151150881d7992e039ff0d51180710dbfa3d2dfe87de458cba382ff125da0c3f1855bef9d29488b41c312897b742b907b9c14c04359e8b874fa260a27dc4237e48b0cbe7749b76723813011208a6a9cc0e33d4f027af7a3bfd22622816a4497748da7f7a1a1cc286abcc00e228f365edd0770d67ca3b0d5e0f5f536fff6ebb207fcdb8688af762aff9db").to_vec())
}

fn get_valid_public_values() -> PublicValuesInput {
BoundedVec::truncate_from(hex!("009c4d92c7f0d1a15c9e62d578ee917c8aded8d9d5ab9de1ffa278c00d282f8095eb3a41a42b59787608d52c6aada0b590902283a91144ef47ad6860b92a5c08f4887c7e675fa7c166c1d17e03d0dd746aa595756b66a8fb8d8fad1215d4caaf00000000000000000000000000000000000000000000000000000000005e4800707bf6bc332f8c8dfffa93ea511e2342c5ace8839d68afecc1fc12a116aabda300000000000000000000000000000000000000000000000000000000005d3300c00e7928895533bafa24fffba54cc7660664eb0134bffe39932fb28a3ddd4b46").to_vec())
BoundedVec::truncate_from(hex!("711c632bedcfb9b85e6dc1bd53ef7c69753f5836761c69be029b55b8097fd6145a2d22ca66c5c60b6a181d25ea9f54b8669440ef59f71a8caf818b00bbd34c995aaad4aeaf4c830b38bfe4f9f5fe3cfcdad919639b871ea20cf1e7eff30a8a360000000000000000000000000000000000000000000000000000000000669d00d7bd59d9998ade89e28efdd86630633bf71117417aa107163526b8d748d332840000000000000000000000000000000000000000000000000000000000669ce0cfde56afdd0992f3fc0a576c2b8959f66a88d47f2886046c104fe6e14da386ac").to_vec())
}
2 changes: 1 addition & 1 deletion pallets/vector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ pub mod pallet {
if sync_committee_hash != next_sync_committee_hash {
ensure!(
sync_committee_hash == H256::zero(),
Error::<T>::SyncCommitteeAlreadySet
Error::<T>::SyncCommitteeHashAlreadySet
);
SyncCommitteeHashes::<T>::set(next_period, next_sync_committee_hash);
Self::deposit_event(Event::SyncCommitteeHashUpdated {
Expand Down
46 changes: 29 additions & 17 deletions pallets/vector/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
RotateVerificationKey, SP1VerificationKey, SourceChainFrozen, StepVerificationKey,
SyncCommitteeHashes, SyncCommitteePoseidons, Updater, ValidProof, WhitelistedDomains,
};
use alloy_sol_types::private::primitives::hex::{ToHex, ToHexExt};
use alloy_sol_types::SolValue;
use avail_core::data_proof::Message::FungibleToken;
use avail_core::data_proof::{tx_uid, AddressedMessage, Message};
Expand All @@ -20,6 +21,7 @@ use frame_support::{
};
use frame_system::RawOrigin;
use hex_literal::hex;
use log::info;
use primitive_types::U256;
use sp1_sdk::SP1ProofWithPublicValues;
use sp_core::{crypto::AccountId32, keccak_256, ByteArray};
Expand All @@ -32,7 +34,7 @@ const TEST_SENDER_ACCOUNT: AccountId32 = AccountId32::new(TEST_SENDER_VEC);
// Generated with SP1 Helios https://github.com/succinctlabs/sp1-helios/blob/main/README.md
// cargo prove key —-elf (sp1 helios elf) in SP1 Helios
const SP1_VERIFICATION_KEY: [u8; 32] =
hex!("00788ce8dc2970920a3d3c072c8c07843d15f1307a53b3dd31b113c3e71c28e8");
hex!("00525ee0ed579ba497061070aea9d4918808790c2aa2b5dcb8fe94bdde4ddbb6");

pub const PROOF_FILE: &str = "test/proof.bin";

Expand Down Expand Up @@ -1422,7 +1424,7 @@ fn test_fulfill_successfully() {
let proof_outputs: ProofOutputs = SolValue::abi_decode(&public_inputs, true).unwrap();
let slots_per_period = 8192;
let finality_threshold = 342u16;
let slot = 6178816u64;
let slot = 6724864u64;
let current_period = slot / slots_per_period;

ConfigurationStorage::<Test>::set(Configuration {
Expand All @@ -1445,27 +1447,36 @@ fn test_fulfill_successfully() {

assert_ok!(ok);

let header = Headers::<Test>::get(6178816);
let header = Headers::<Test>::get(slot);
assert_eq!(
H256(hex!(
"95eb3a41a42b59787608d52c6aada0b590902283a91144ef47ad6860b92a5c08"
"5a2d22ca66c5c60b6a181d25ea9f54b8669440ef59f71a8caf818b00bbd34c99"
)),
header
);
let execution_state_root = ExecutionStateRoots::<Test>::get(6178816);
let execution_state_root = ExecutionStateRoots::<Test>::get(slot);
assert_eq!(
H256(hex!(
"009c4d92c7f0d1a15c9e62d578ee917c8aded8d9d5ab9de1ffa278c00d282f80"
"711c632bedcfb9b85e6dc1bd53ef7c69753f5836761c69be029b55b8097fd614"
)),
execution_state_root
);
let sync_committee_hash = SyncCommitteeHashes::<Test>::get((6178816 / 8192) + 1);

let sync_committee_hash = SyncCommitteeHashes::<Test>::get(slot / 8192);
assert_eq!(
H256(hex!(
"f4887c7e675fa7c166c1d17e03d0dd746aa595756b66a8fb8d8fad1215d4caaf"
"cfde56afdd0992f3fc0a576c2b8959f66a88d47f2886046c104fe6e14da386ac"
)),
sync_committee_hash
);

let next_sync_committee_hash = SyncCommitteeHashes::<Test>::get((slot / 8192) + 1);
assert_eq!(
H256(hex!(
"5aaad4aeaf4c830b38bfe4f9f5fe3cfcdad919639b871ea20cf1e7eff30a8a36"
)),
next_sync_committee_hash
);
});
}

Expand Down Expand Up @@ -1497,12 +1508,12 @@ fn test_fulfill_successfully_sync_committee_not_set() {

assert_ok!(ok);

let header = Headers::<Test>::get(6178816);
let header = Headers::<Test>::get(6724864);

// assert proof outputs
let period = 6178816 / 8192;
let period = 6724864 / 8192;
assert_eq!(H256(proof_outputs.newHeader.0), header);
let execution_state_root = ExecutionStateRoots::<Test>::get(6178816);
let execution_state_root = ExecutionStateRoots::<Test>::get(6724864);
assert_eq!(
H256(proof_outputs.executionStateRoot.0),
execution_state_root
Expand All @@ -1518,7 +1529,7 @@ fn test_fulfill_successfully_sync_committee_not_set() {
});
}

// TODO this panics
// TODO verify_groth16_raw is not part of the sp1 v4.0.0 release which will fix panics
// #[test]
// fn test_fulfill_incorrect_proof() {
// new_test_ext().execute_with(|| {
Expand All @@ -1533,7 +1544,7 @@ fn test_fulfill_successfully_sync_committee_not_set() {
// let proof_outputs: ProofOutputs = SolValue::abi_decode(&public_inputs, true).unwrap();
// let slots_per_period = 8192;
// let finality_threshold = 342;
// let slot = 6178816u64;
// let slot = 6724864u64;
// let current_period = slot / slots_per_period;
//
// ConfigurationStorage::<Test>::set(Configuration {
Expand All @@ -1557,6 +1568,7 @@ fn test_fulfill_successfully_sync_committee_not_set() {
// assert_err!(err, Error::<Test>::VerificationFailed);
// });
// }

#[test]
fn test_fulfill_incorrect_proof_output() {
new_test_ext().execute_with(|| {
Expand All @@ -1575,7 +1587,7 @@ fn test_fulfill_incorrect_proof_output() {
let proof_outputs: ProofOutputs = SolValue::abi_decode(&proof_outputs_vec, true).unwrap();
let slots_per_period = 8192;
let finality_threshold = 342;
let slot = 6178816u64;
let slot = 6724864u64;
let current_period = slot / slots_per_period;

ConfigurationStorage::<Test>::set(Configuration {
Expand Down Expand Up @@ -1611,7 +1623,7 @@ fn test_fulfill_head_not_greater() {
let proof_outputs: ProofOutputs = SolValue::abi_decode(&public_inputs, true).unwrap();
let slots_per_period = 8192;
let finality_threshold = 342;
let slot = 6178816u64;
let slot = 6724864u64;
let current_period = slot / slots_per_period;

ConfigurationStorage::<Test>::set(Configuration {
Expand Down Expand Up @@ -1650,7 +1662,7 @@ fn test_fulfill_next_period_already_set() {
let proof_outputs: ProofOutputs = SolValue::abi_decode(&public_inputs, true).unwrap();
let slots_per_period = 8192;
let finality_threshold = 342;
let slot = 6178816u64;
let slot = 6724864u64;
let current_period = slot / slots_per_period;

ConfigurationStorage::<Test>::set(Configuration {
Expand Down Expand Up @@ -1688,7 +1700,7 @@ fn test_fulfill_configuration_not_set() {

let proof_outputs: ProofOutputs = SolValue::abi_decode(&public_inputs, true).unwrap();
let slots_per_period = 8192;
let slot = 6178816u64;
let slot = 6724864u64;
let current_period = slot / slots_per_period;

ConfigurationStorage::<Test>::set(Configuration {
Expand Down
4 changes: 2 additions & 2 deletions pallets/vector/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `614`
// Estimated: `6020`
// Minimum execution time: 532_131_000_000 picoseconds.
Weight::from_parts(537_965_000_000, 6020)
// Minimum execution time: 519_421_000_000 picoseconds.
Weight::from_parts(527_224_000_000, 6020)
.saturating_add(RocksDbWeight::get().reads(9_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
Expand Down
Binary file modified pallets/vector/test/proof.bin
Binary file not shown.
5 changes: 3 additions & 2 deletions runtime/src/weights/pallet_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,11 @@ impl<T: frame_system::Config> pallet_vector::WeightInfo for WeightInfo<T> {
// Proof Size summary in bytes:
// Measured: `614`
// Estimated: `6020`
// Minimum execution time: 533_498_000_000 picoseconds.
Weight::from_parts(537_142_000_000, 0)
// Minimum execution time: 521_131_000_000 picoseconds.
Weight::from_parts(523_158_000_000, 0)
.saturating_add(Weight::from_parts(0, 6020))
.saturating_add(T::DbWeight::get().reads(9))
.saturating_add(T::DbWeight::get().writes(5))
}
}

0 comments on commit 305e82e

Please sign in to comment.