diff --git a/Cargo.lock b/Cargo.lock index 860b4d7..5f36db9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -941,6 +941,20 @@ name = "bytemuck" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8334215b81e418a0a7bdb8ef0849474f40bb10c8b71f1c4ed315cff49f32494d" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] [[package]] name = "byteorder" @@ -1923,6 +1937,12 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + [[package]] name = "dtoa" version = "1.0.9" @@ -2016,6 +2036,12 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "elf" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b" + [[package]] name = "elliptic-curve" version = "0.13.8" @@ -5053,6 +5079,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "num-format" version = "0.4.4" @@ -5192,7 +5229,9 @@ dependencies = [ "frame-support", "frame-system", "parity-scale-codec", + "risc0-zkvm", "scale-info", + "serde_json", "sp-core", "sp-io", "sp-runtime", @@ -6788,6 +6827,116 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "risc0-binfmt" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923c85a23cb9a9475b8cd4479ad3a06252604a361626e9ae7dc0dc635af22c22" +dependencies = [ + "anyhow", + "elf", + "log", + "risc0-zkp", + "risc0-zkvm-platform", + "serde", +] + +[[package]] +name = "risc0-circuit-recursion" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97547e10e9fdaaab8b64ffb45dc158b31f023b1a68015c6ce9f12fe3e403012a" +dependencies = [ + "anyhow", + "bytemuck", + "log", + "risc0-core", + "risc0-zkp", + "tracing", +] + +[[package]] +name = "risc0-circuit-rv32im" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a269d01b18cba24ee1a08f68726fc3623e8705ed79d158377d12e9129dcde2e" +dependencies = [ + "anyhow", + "log", + "risc0-core", + "risc0-zkp", + "risc0-zkvm-platform", + "tracing", +] + +[[package]] +name = "risc0-core" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "477e0bb8d2ec0b7955088b521eb596901e652d0faa2ea73bda0b77e05af5c07d" +dependencies = [ + "bytemuck", + "rand_core 0.6.4", +] + +[[package]] +name = "risc0-zkp" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5abb1a0cf847d3f9aed1e563b76c358107e7ba66dbfab28f7144252c990bd82" +dependencies = [ + "anyhow", + "blake2 0.10.6", + "bytemuck", + "digest 0.10.7", + "hex", + "log", + "paste", + "rand_core 0.6.4", + "risc0-core", + "risc0-zkvm-platform", + "serde", + "sha2 0.10.8", + "tracing", +] + +[[package]] +name = "risc0-zkvm" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cf80df202c038efc2199be34fda8114b38bfc5b2b51c60cbbdf1f425b07b384" +dependencies = [ + "anyhow", + "bytemuck", + "cfg-if", + "getrandom 0.2.15", + "hex", + "log", + "num-derive", + "num-traits", + "risc0-binfmt", + "risc0-circuit-recursion", + "risc0-circuit-rv32im", + "risc0-core", + "risc0-zkp", + "risc0-zkvm-platform", + "rrs-lib", + "semver 1.0.23", + "serde", + "tracing", +] + +[[package]] +name = "risc0-zkvm-platform" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dcd6b66f7a4972001db0acf3f06d99b7851c8d9f0de1f7e0fb4496c66c5cd02" +dependencies = [ + "bytemuck", + "getrandom 0.2.15", + "libm", +] + [[package]] name = "rocksdb" version = "0.21.0" @@ -6815,6 +6964,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "rrs-lib" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4382d3af3a4ebdae7f64ba6edd9114fff92c89808004c4943b393377a25d001" +dependencies = [ + "downcast-rs", + "paste", +] + [[package]] name = "rtnetlink" version = "0.10.1" diff --git a/Cargo.toml b/Cargo.toml index 5bf5a06..317e064 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,6 +110,9 @@ jsonrpsee = { version = "0.22", features = ["client-core", "macros", "server"] } sc-rpc = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false } sp-blockchain = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false } hex-literal = { version = " 0.4.1" } + +risc0-zkvm = { version = "0.19.1", default-features = false } +serde_json = { version = "1", default-features = false, features = ["alloc"] } ## New end parity-scale-codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [ diff --git a/custom-pallets/anonymous-account/Cargo.toml b/custom-pallets/anonymous-account/Cargo.toml index 1f30c22..f4fbd42 100644 --- a/custom-pallets/anonymous-account/Cargo.toml +++ b/custom-pallets/anonymous-account/Cargo.toml @@ -21,6 +21,8 @@ frame-system = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } +risc0-zkvm = { workspace = true, default-features = false } +serde_json = { workspace = true, default-features = false } [dev-dependencies] @@ -34,6 +36,8 @@ std = [ "frame-support/std", "frame-system/std", "scale-info/std", + "risc0-zkvm/std", + "serde_json/std", ] runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] try-runtime = ["frame-support/try-runtime"] diff --git a/custom-pallets/anonymous-account/src/lib.rs b/custom-pallets/anonymous-account/src/lib.rs index 708612d..4a70fc8 100644 --- a/custom-pallets/anonymous-account/src/lib.rs +++ b/custom-pallets/anonymous-account/src/lib.rs @@ -17,6 +17,8 @@ pub use weights::*; pub type DepartmentId = u64; use frame_support::pallet_prelude::DispatchError; +use risc0_zkvm::Receipt; +use scale_info::prelude::string::String; use sp_std::collections::btree_set::BTreeSet; #[frame_support::pallet(dev_mode)] @@ -37,6 +39,9 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Type representing the weight of this pallet type WeightInfo: WeightInfo; + + #[pallet::constant] + type AnonymousAccountImageId: Get<[u32; 8]>; } // The pallet's runtime storage items. @@ -87,6 +92,7 @@ pub mod pallet { hash: [u8; 32], account_id: T::AccountId, }, + ProofVerified, } // Errors inform users that something went wrong. @@ -101,6 +107,7 @@ pub mod pallet { NoAccounts, IncompleteSlice, HashNotFound, + ProofNotVerified, } // Dispatchable functions allows users to interact with the pallet and invoke state changes. @@ -176,6 +183,21 @@ pub mod pallet { Ok(()) } + #[pallet::call_index(2)] + #[pallet::weight(0)] + pub fn verify_proof(origin: OriginFor, receipt_bytes: Vec) -> DispatchResult { + let who = ensure_signed(origin)?; + let image_id = T::AnonymousAccountImageId::get(); + let receipt_json: String = Decode::decode(&mut &receipt_bytes[..]).unwrap(); + let receipt: Receipt = serde_json::from_str(&receipt_json).unwrap(); + let (output, password_hash): ([u8; 32], [u8; 32]) = receipt.journal.decode().unwrap(); + + receipt.verify(image_id).map_err(|_| Error::::ProofNotVerified)?; + + Self::deposit_event(Event::ProofVerified); + Ok(()) + } + #[pallet::call_index(50)] #[pallet::weight(0)] pub fn calculate_hash(origin: OriginFor) -> DispatchResult { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index cb50db3..142d370 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -149,6 +149,7 @@ parameter_types! { pub BlockLength: frame_system::limits::BlockLength = frame_system::limits::BlockLength ::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); pub const SS58Prefix: u8 = 42; + pub const AnonymousAccountImageId: [u32; 8] = [1512492500, 2753161227, 4049970770, 2674496521, 3333553514, 2059402670, 1701049823, 2725882521]; } /// The default types are being injected by [`derive_impl`](`frame_support::derive_impl`) from @@ -302,6 +303,7 @@ impl pallet_departments::Config for Runtime { impl pallet_anonymous_account::Config for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = pallet_anonymous_account::weights::SubstrateWeight; + type AnonymousAccountImageId = AnonymousAccountImageId; } impl pallet_department_funding::Config for Runtime {