solutions/pallets/zkp_verify/src/lib.rs

178 lines
5.6 KiB
Rust
Executable File

#![cfg_attr(not(feature = "std"), no_std)]
/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://docs.substrate.io/reference/frame-pallets/>
pub use pallet::*;
use dusk_plonk::prelude::{Proof, Verifier, BlsScalar};
use rkyv::Deserialize;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use ark_bn254::{Bn254, Fq, Fq2, Fr, G1Affine, G2Affine, Config as Bn254Config};
use ark_ec::bn::Bn;
use ark_gm17::{prepare_verifying_key as gm17_prepare_verification_key, Proof as gm17_proof, VerifyingKey as gm17_verification_key, verify_proof as gm17_verify_proof, GM17};
use ark_groth16::{prepare_verifying_key as g16_prepare_verification_key, Proof as g16_proof, VerifyingKey as g16_verification_key, Groth16};
use ark_ff::{Field, Zero, vec,Fp};
use ark_snark::SNARK;
use ark_bls12_381::{Bls12_381, Fr as BlsFr};
use ark_serialize::{CanonicalDeserialize, Compress, Validate};
use ark_std::{
io::{Cursor},
vec::Vec,
};
#[pallet::pallet]
pub struct Pallet<T>(_);
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;
}
// The pallet's runtime storage items.
// https://docs.substrate.io/main-docs/build/runtime-storage/
#[pallet::storage]
#[pallet::getter(fn something)]
// Learn more about declaring storage items:
// https://docs.substrate.io/main-docs/build/runtime-storage/#declaring-storage-items
pub type Something<T> = StorageValue<_, u32>;
// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/main-docs/build/events-errors/
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
ValidationSuccess { who: T::AccountId },
}
#[pallet::error]
pub enum Error<T> {
ValidationFailed,
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::cause_error())]
pub fn verify_proof(origin: OriginFor<T>, vk: Vec<u8>,inputs: Vec<u8>,proof: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
let cursor = Cursor::new(&vk);
let vk = <Groth16<Bls12_381> as SNARK<BlsFr>>::VerifyingKey::deserialize_with_mode(
cursor,
Compress::No,
Validate::No,
).unwrap();
let cursor = Cursor::new(&inputs);
let inputs: ark_ff::Fp<ark_ff::MontBackend<ark_bls12_381::FrConfig, 4>, 4> =
Fp::deserialize_with_mode(cursor, Compress::No, Validate::No).unwrap();
let cursor = Cursor::new(&proof);
let proof =
<Groth16<Bls12_381> as SNARK<BlsFr>>::Proof::deserialize_with_mode(
cursor,
Compress::No,
Validate::No,
).unwrap();
let result = Groth16::<Bls12_381>::verify(&vk, &[inputs], &proof).unwrap();
if(result){
Self::deposit_event(Event::ValidationSuccess { who });
Ok(())
}else{
Err(Error::<T>::ValidationFailed.into())
}
}
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::cause_error())]
pub fn verify_proof_plonk(origin: OriginFor<T>, vk: Vec<u8>, inputs: Vec<u8>,proof: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
let verifier: Verifier = Verifier::try_from_bytes(&vk[..]).expect("Failed to load verifier");
let archived = rkyv::check_archived_root::<Proof>(&proof[..]).unwrap();
let proof: Proof = archived.deserialize(&mut rkyv::Infallible).unwrap();
let archived = rkyv::check_archived_root::<Vec<BlsScalar>>(&inputs[..]).unwrap();
let inputs: Vec<BlsScalar> = archived.deserialize(&mut rkyv::Infallible).unwrap();
let verification =verifier.verify(&proof, &inputs);
let result = verification.is_ok();
if(result){
Self::deposit_event(Event::ValidationSuccess { who });
Ok(())
}else{
Err(Error::<T>::ValidationFailed.into())
}
}
/*#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::cause_error())]
pub fn verify_proof_gm17(origin: OriginFor<T>, vk: Vec<u8>, inputs: Vec<u8>,proof: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
let cursor = Cursor::new(&vk);
let pvk = <GM17<Bls12_381> as SNARK<BlsFr>>::VerifyingKey::deserialize_with_mode(
cursor,
Compress::No,
Validate::No,
).unwrap();
let cursor = Cursor::new(&inputs);
let inputs: ark_ff::Fp<ark_ff::MontBackend<ark_bls12_381::FrConfig, 4>, 4> =
Fp::deserialize_with_mode(cursor, Compress::No, Validate::No).unwrap();
let cursor = Cursor::new(&proof);
let proof =
<GM17<Bls12_381> as SNARK<BlsFr>>::Proof::deserialize_with_mode(
cursor,
Compress::No,
Validate::No,
)
.unwrap();
let result = gm17_verify_proof(&vk, &proof, &[inputs]).unwrap();
if(result){
Ok(())
}else{
Err(Error::<T>::NoneValue.into())
}
}*/
}
}