From 3f656b4afdf38252054477c01f0f2c2c856e5f02 Mon Sep 17 00:00:00 2001 From: shekohex Date: Tue, 19 Dec 2023 12:13:03 +0000 Subject: [PATCH] Add ZkSaaS Pallet to Testnet Runtime (#354) --- Cargo.lock | 1 + Cargo.toml | 1 + primitives/Cargo.toml | 8 +++++++- primitives/src/verifier/mod.rs | 21 +++++++++++++++++++-- runtime/testnet/Cargo.toml | 4 +++- runtime/testnet/src/lib.rs | 20 +++++++++++++++----- 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f2765d927..2a115e92b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15335,6 +15335,7 @@ dependencies = [ "pallet-treasury", "pallet-utility", "pallet-vesting", + "pallet-zksaas", "parity-scale-codec", "precompile-utils", "rpc-primitives-debug", diff --git a/Cargo.toml b/Cargo.toml index f0b2589cf..d7d172634 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,6 +100,7 @@ pallet-airdrop-claims = { path = "pallets/claims", default-features = false } pallet-jobs = { path = "pallets/jobs", default-features = false } pallet-roles = { path = "pallets/roles", default-features = false } pallet-dkg = { path = "pallets/dkg", default-features = false } +pallet-zksaas = { path = "pallets/zksaas", default-features = false } # Substrate dependencies sp-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false } diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index b424b4845..6337fd3f2 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -36,7 +36,13 @@ hex = { workspace = true } [features] default = ["std", "verifying"] -std = ["frame-support/std", "sp-runtime/std", "sp-core/std", "sp-consensus-aura/std"] +std = [ + "frame-support/std", + "sp-runtime/std", + "sp-core/std", + "sp-consensus-aura/std", + "ark-std?/std", +] verifying = [ "ark-crypto-primitives", "ark-ec", diff --git a/primitives/src/verifier/mod.rs b/primitives/src/verifier/mod.rs index 3567b709d..93b727761 100644 --- a/primitives/src/verifier/mod.rs +++ b/primitives/src/verifier/mod.rs @@ -1,5 +1,6 @@ use ark_crypto_primitives::Error; use ark_ff::{BigInteger, PrimeField}; +use sp_std::vec::Vec; pub mod arkworks; pub mod circom; @@ -31,7 +32,7 @@ pub fn to_field_elements(bytes: &[u8]) -> Result, Error> { /// Convert a vector of field elements into a vector of bytes. pub fn from_field_elements(elts: &[F]) -> Result, Error> { - let res = elts.iter().fold(vec![], |mut acc, prev| { + let res = elts.iter().fold(Vec::with_capacity(elts.len() * 32), |mut acc, prev| { acc.extend_from_slice(&prev.into_bigint().to_bytes_be()); acc }); @@ -39,11 +40,27 @@ pub fn from_field_elements(elts: &[F]) -> Result, Error> Ok(res) } -// A trait meant to be implemented over a zero-knowledge verifier function. +/// A trait meant to be implemented over a zero-knowledge verifier function. pub trait InstanceVerifier { fn verify(pub_inps: &[u8], proof: &[u8], params: &[u8]) -> Result; } +/// A verifier that always returns true. +#[derive(Clone, Copy, Debug)] +pub struct PassThroughVerifier; + +impl InstanceVerifier for PassThroughVerifier { + fn verify(_pub_inps: &[u8], _proof: &[u8], _params: &[u8]) -> Result { + Ok(true) + } +} + +impl InstanceVerifier for () { + fn verify(_pub_inps: &[u8], _proof: &[u8], _params: &[u8]) -> Result { + Ok(false) + } +} + impl InstanceVerifier for (V1, V2) where V1: InstanceVerifier, diff --git a/runtime/testnet/Cargo.toml b/runtime/testnet/Cargo.toml index 57f3a2377..775a2a9a4 100644 --- a/runtime/testnet/Cargo.toml +++ b/runtime/testnet/Cargo.toml @@ -87,9 +87,10 @@ pallet-vesting = { workspace = true } # Webb dependencies pallet-dkg = { workspace = true } +pallet-zksaas = { workspace = true } pallet-jobs = { workspace = true } pallet-roles = { workspace = true } -tangle-primitives = { workspace = true } +tangle-primitives = { workspace = true, features = ["verifying"] } # Frontier dependencies fp-account = { workspace = true } @@ -209,6 +210,7 @@ std = [ "pallet-jobs/std", "pallet-roles/std", "pallet-dkg/std", + "pallet-zksaas/std", # ETH2 light-client "pallet-eth2-light-client/std", diff --git a/runtime/testnet/src/lib.rs b/runtime/testnet/src/lib.rs index 876dc6027..290624037 100644 --- a/runtime/testnet/src/lib.rs +++ b/runtime/testnet/src/lib.rs @@ -75,6 +75,7 @@ use tangle_primitives::{ jobs::{JobResult, JobSubmission, JobType, JobWithResult, ValidatorOffenceType}, roles::ValidatorRewardDistribution, traits::jobs::{JobToFee, MPCHandler}, + verifier::{arkworks::ArkworksVerifierGroth16Bn254, circom::CircomVerifierGroth16Bn254}, }; #[cfg(any(feature = "std", test))] @@ -1017,8 +1018,8 @@ impl JobToFee for MockJobToFeeHandler { match job.job_type { JobType::DKGTSSPhaseOne(_) => Dkg::job_to_fee(job), JobType::DKGTSSPhaseTwo(_) => Dkg::job_to_fee(job), - JobType::ZkSaaSPhaseOne(_) => todo!(), // TODO : Replace with zksaas pallet - JobType::ZkSaaSPhaseTwo(_) => todo!(), // TODO : Replace with zksaas pallet + JobType::ZkSaaSPhaseOne(_) => ZkSaaS::job_to_fee(job), + JobType::ZkSaaSPhaseTwo(_) => ZkSaaS::job_to_fee(job), } } } @@ -1030,8 +1031,8 @@ impl MPCHandler for MockMPCHandler { match data.result { JobResult::DKGPhaseOne(_) => Dkg::verify(data.result), JobResult::DKGPhaseTwo(_) => Dkg::verify(data.result), - JobResult::ZkSaaSPhaseOne(_) => todo!(), // TODO : Replace with zksaas pallet - JobResult::ZkSaaSPhaseTwo(_) => todo!(), // TODO : Replace with zksaas pallet + JobResult::ZkSaaSPhaseOne(_) => ZkSaaS::verify(data), + JobResult::ZkSaaSPhaseTwo(_) => ZkSaaS::verify(data), } } @@ -1098,6 +1099,14 @@ impl pallet_dkg::Config for Runtime { type WeightInfo = (); } +impl pallet_zksaas::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type UpdateOrigin = EnsureRootOrHalfCouncil; + type Verifier = (ArkworksVerifierGroth16Bn254, CircomVerifierGroth16Bn254); + type WeightInfo = (); +} + pub struct BaseFilter; impl Contains for BaseFilter { fn contains(call: &RuntimeCall) -> bool { @@ -1204,7 +1213,8 @@ construct_runtime!( Roles: pallet_roles, Jobs: pallet_jobs, - Dkg: pallet_dkg + Dkg: pallet_dkg, + ZkSaaS: pallet_zksaas, } );