diff --git a/Cargo.lock b/Cargo.lock index 4cf4ec8b0ad..b52b3b2b003 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,6 +131,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a" dependencies = [ "cfg-if", + "const-random", "getrandom 0.2.10", "once_cell", "version_check", @@ -246,7 +247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d1da02abba9f9063d786eab1509833ebb2fac0f966862ca59439c76b9c566760" dependencies = [ "include_dir", - "itertools", + "itertools 0.10.5", "proc-macro-error", "proc-macro2", "quote", @@ -348,7 +349,7 @@ dependencies = [ "ark-std", "derivative", "hashbrown 0.13.2", - "itertools", + "itertools 0.10.5", "num-traits", "rayon", "zeroize", @@ -416,7 +417,7 @@ dependencies = [ "ark-std", "derivative", "digest 0.10.7", - "itertools", + "itertools 0.10.5", "num-bigint", "num-traits", "paste", @@ -1879,7 +1880,7 @@ dependencies = [ "cranelift-codegen 0.95.1", "cranelift-entity 0.95.1", "cranelift-frontend 0.95.1", - "itertools", + "itertools 0.10.5", "log", "smallvec", "wasmparser 0.102.0", @@ -3539,6 +3540,15 @@ dependencies = [ "scale-info", ] +[[package]] +name = "fixed-hash" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" +dependencies = [ + "static_assertions", +] + [[package]] name = "fixed-hash" version = "0.8.0" @@ -3667,7 +3677,7 @@ dependencies = [ "frame-system", "gethostname", "handlebars", - "itertools", + "itertools 0.10.5", "lazy_static", "linked-hash-map", "log", @@ -3842,7 +3852,7 @@ dependencies = [ "derive-syn-parse", "expander", "frame-support-procedural-tools", - "itertools", + "itertools 0.10.5", "macro_magic", "proc-macro-warning", "proc-macro2", @@ -4373,7 +4383,7 @@ dependencies = [ "gsys", "hex-literal", "log", - "primitive-types", + "primitive-types 0.12.2", "proptest", "sp-arithmetic", "sp-core", @@ -4543,7 +4553,7 @@ dependencies = [ "gsdk", "names 0.14.0", "parking_lot 0.12.1", - "primitive-types", + "primitive-types 0.12.2", "rand 0.8.5", "reqwest", "subxt", @@ -4669,6 +4679,7 @@ dependencies = [ "gear-sandbox-host", "log", "parity-scale-codec", + "plonky2", "sp-io", "sp-runtime-interface", "sp-std 8.0.0 (git+https://github.com/gear-tech/polkadot-sdk.git?branch=gear-v1.1.0)", @@ -4991,8 +5002,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -5209,7 +5222,7 @@ dependencies = [ "hashbrown 0.14.3", "hex", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", ] @@ -5347,6 +5360,7 @@ checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ "ahash 0.8.6", "allocator-api2", + "rayon", "serde", ] @@ -5830,6 +5844,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" @@ -6115,6 +6138,16 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "keccak-hash" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2bd4c29270e724d3eaadf7bdc8700af4221fc0ed771b855eadcd1b98d52851" +dependencies = [ + "primitive-types 0.10.1", + "tiny-keccak", +] + [[package]] name = "keyring" version = "1.2.1" @@ -7471,6 +7504,7 @@ dependencies = [ "autocfg", "num-integer", "num-traits", + "rand 0.8.5", ] [[package]] @@ -7480,6 +7514,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" dependencies = [ "num-traits", + "rand 0.8.5", ] [[package]] @@ -7986,7 +8021,7 @@ dependencies = [ "pallet-gear-voucher", "pallet-timestamp", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "rand 0.8.5", "rand_pcg", "scale-info", @@ -8015,7 +8050,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-io", "sp-runtime", @@ -8057,7 +8092,7 @@ dependencies = [ "pallet-gear-scheduler", "pallet-timestamp", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-core", "sp-crypto-ec-utils", @@ -8112,7 +8147,7 @@ dependencies = [ "pallet-gear-scheduler", "pallet-timestamp", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-core", "sp-io", @@ -8142,7 +8177,7 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "parity-wasm", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-core", "sp-io", @@ -8167,7 +8202,7 @@ dependencies = [ "pallet-gear-gas", "pallet-timestamp", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "rand 0.8.5", "scale-info", "sp-io", @@ -8200,7 +8235,7 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "parity-wasm", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-io", "sp-runtime", @@ -8233,7 +8268,7 @@ dependencies = [ "pallet-gear-scheduler", "pallet-timestamp", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-core", "sp-io", @@ -8291,7 +8326,7 @@ dependencies = [ "pallet-gear-program", "pallet-timestamp", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-core", "sp-io", @@ -8323,7 +8358,7 @@ dependencies = [ "pallet-treasury", "pallet-utility", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "serde", "sp-authority-discovery", @@ -8369,7 +8404,7 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "parity-wasm", - "primitive-types", + "primitive-types 0.12.2", "scale-info", "sp-core", "sp-io", @@ -9110,6 +9145,59 @@ version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3d7ddaed09e0eb771a79ab0fd64609ba0afb0a8366421957936ad14cbd13630" +[[package]] +name = "plonky2" +version = "0.1.4" +source = "git+https://github.com/0xPolygonZero/plonky2.git?rev=1a08e783da3d3cedcc7ae3dc9c06e018ab90da71#1a08e783da3d3cedcc7ae3dc9c06e018ab90da71" +dependencies = [ + "ahash 0.8.6", + "anyhow", + "getrandom 0.2.10", + "hashbrown 0.14.3", + "itertools 0.11.0", + "keccak-hash", + "log", + "num", + "plonky2_field", + "plonky2_maybe_rayon", + "plonky2_util", + "rand 0.8.5", + "rand_chacha 0.3.1", + "serde", + "serde_json", + "static_assertions", + "unroll", + "web-time", +] + +[[package]] +name = "plonky2_field" +version = "0.1.1" +source = "git+https://github.com/0xPolygonZero/plonky2.git?rev=1a08e783da3d3cedcc7ae3dc9c06e018ab90da71#1a08e783da3d3cedcc7ae3dc9c06e018ab90da71" +dependencies = [ + "anyhow", + "itertools 0.11.0", + "num", + "plonky2_util", + "rand 0.8.5", + "serde", + "static_assertions", + "unroll", +] + +[[package]] +name = "plonky2_maybe_rayon" +version = "0.1.1" +source = "git+https://github.com/0xPolygonZero/plonky2.git?rev=1a08e783da3d3cedcc7ae3dc9c06e018ab90da71#1a08e783da3d3cedcc7ae3dc9c06e018ab90da71" +dependencies = [ + "rayon", +] + +[[package]] +name = "plonky2_util" +version = "0.1.1" +source = "git+https://github.com/0xPolygonZero/plonky2.git?rev=1a08e783da3d3cedcc7ae3dc9c06e018ab90da71#1a08e783da3d3cedcc7ae3dc9c06e018ab90da71" + [[package]] name = "polling" version = "2.8.0" @@ -9181,7 +9269,7 @@ checksum = "59230a63c37f3e18569bdb90e4a89cbf5bf8b06fea0b84e65ea10cc4df47addd" dependencies = [ "difflib", "float-cmp", - "itertools", + "itertools 0.10.5", "normalize-line-endings", "predicates-core", "regex", @@ -9233,13 +9321,23 @@ dependencies = [ "syn 2.0.53", ] +[[package]] +name = "primitive-types" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05e4722c697a58a99d5d06a08c30821d7c082a4632198de1eaa5a6c22ef42373" +dependencies = [ + "fixed-hash 0.7.0", + "uint", +] + [[package]] name = "primitive-types" version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" dependencies = [ - "fixed-hash", + "fixed-hash 0.8.0", "impl-codec", "impl-serde", "scale-info", @@ -9384,7 +9482,7 @@ checksum = "119533552c9a7ffacc21e099c24a0ac8bb19c2a2a3f363de84cd9b844feab270" dependencies = [ "bytes", "heck 0.4.1", - "itertools", + "itertools 0.10.5", "lazy_static", "log", "multimap", @@ -9405,7 +9503,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "syn 1.0.109", @@ -11225,7 +11323,7 @@ checksum = "7789f5728e4e954aaa20cadcc370b99096fb8645fca3c9333ace44bb18f30095" dependencies = [ "derive_more", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-bits", "scale-decode-derive", "scale-info", @@ -11253,7 +11351,7 @@ checksum = "6d70cb4b29360105483fac1ed567ff95d65224a14dd275b6303ed0a654c78de5" dependencies = [ "derive_more", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-bits", "scale-encode-derive", "scale-info", @@ -11801,7 +11899,7 @@ dependencies = [ "hashbrown 0.14.3", "hex", "hmac 0.12.1", - "itertools", + "itertools 0.10.5", "libsecp256k1", "merlin 3.0.0", "no-std-net", @@ -11845,7 +11943,7 @@ dependencies = [ "futures-util", "hashbrown 0.14.3", "hex", - "itertools", + "itertools 0.10.5", "log", "lru", "parking_lot 0.12.1", @@ -12139,7 +12237,7 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.12.1", "paste", - "primitive-types", + "primitive-types 0.12.2", "rand 0.8.5", "regex", "scale-info", @@ -12417,7 +12515,7 @@ dependencies = [ "bytes", "impl-trait-for-tuples", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "sp-externalities", "sp-runtime-interface-proc-macro", "sp-std 8.0.0 (git+https://github.com/gear-tech/polkadot-sdk.git?branch=gear-v1.1.0)", @@ -12989,7 +13087,7 @@ dependencies = [ "impl-serde", "jsonrpsee 0.20.1", "parity-scale-codec", - "primitive-types", + "primitive-types 0.12.2", "scale-bits", "scale-decode", "scale-encode", @@ -13940,6 +14038,16 @@ dependencies = [ "subtle", ] +[[package]] +name = "unroll" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ad948c1cb799b1a70f836077721a92a35ac177d4daddf4c20a633786d4cf618" +dependencies = [ + "quote", + "syn 1.0.109", +] + [[package]] name = "unsigned-varint" version = "0.7.1" @@ -15015,6 +15123,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki" version = "0.22.0" diff --git a/node/service/src/client.rs b/node/service/src/client.rs index b398dca40c1..deea2d58416 100644 --- a/node/service/src/client.rs +++ b/node/service/src/client.rs @@ -56,6 +56,7 @@ impl sc_executor::NativeExecutionDispatch for VaraExecutorDispatch { gear_ri::gear_ri::HostFunctions, gear_ri::sandbox::HostFunctions, sp_crypto_ec_utils::bls12_381::host_calls::HostFunctions, + gear_ri::specific_plonky_2::HostFunctions, ); /// Otherwise we only use the default Substrate host functions. #[cfg(not(feature = "runtime-benchmarks"))] @@ -63,6 +64,7 @@ impl sc_executor::NativeExecutionDispatch for VaraExecutorDispatch { gear_ri::gear_ri::HostFunctions, gear_ri::sandbox::HostFunctions, sp_crypto_ec_utils::bls12_381::host_calls::HostFunctions, + gear_ri::specific_plonky_2::HostFunctions, ); fn dispatch(method: &str, data: &[u8]) -> Option> { diff --git a/node/testing/src/client.rs b/node/testing/src/client.rs index b37daea9135..ed1d0266962 100644 --- a/node/testing/src/client.rs +++ b/node/testing/src/client.rs @@ -32,6 +32,7 @@ impl sc_executor::NativeExecutionDispatch for LocalExecutorDispatch { gear_runtime_interface::gear_ri::HostFunctions, gear_runtime_interface::sandbox::HostFunctions, sp_crypto_ec_utils::bls12_381::host_calls::HostFunctions, + gear_runtime_interface::specific_plonky_2::HostFunctions, ); fn dispatch(method: &str, data: &[u8]) -> Option> { diff --git a/runtime-interface/Cargo.toml b/runtime-interface/Cargo.toml index 20ebcae809c..b027789ebb3 100644 --- a/runtime-interface/Cargo.toml +++ b/runtime-interface/Cargo.toml @@ -23,6 +23,8 @@ byteorder.workspace = true codec = { workspace = true } log = { workspace = true, optional = true } +plonky2 = { git = "https://github.com/0xPolygonZero/plonky2.git", rev = "1a08e783da3d3cedcc7ae3dc9c06e018ab90da71", optional = true } + [target.'cfg(windows)'.dependencies] winapi = { workspace = true, features = ["memoryapi"] } @@ -41,4 +43,6 @@ std = [ "byteorder/std", "codec/std", "log", + + "plonky2", ] diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index 05ad45b6a29..3e0b93fa116 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -301,3 +301,65 @@ pub trait GearDebug { .as_nanos() } } + +#[repr(u32)] +pub enum Plonky2VerifyResult { + Verified, + Rejected, + FailedToDecodeCommonData, + FailedToDecodeVerifierData, + FailedToDecodeProof, +} + +impl From for u32 { + fn from(value: Plonky2VerifyResult) -> Self { + value as u32 + } +} + +#[runtime_interface] +pub trait SpecificPlonky2 { + fn verify( + common_curcuit_data: Vec, + verifier_circuit_data: Vec, + proof: Vec, + ) -> u32 { + use plonky2::plonk::{ + self, + config::{GenericConfig, PoseidonGoldilocksConfig}, + }; + use plonky2::util::serialization::DefaultGateSerializer; + + pub const DIMENSION: usize = 2; + pub type Config = PoseidonGoldilocksConfig; + pub type Field = >::F; + // pub type CircuitData = plonk::circuit_data::CircuitData; + pub type CommonCircuitData = plonk::circuit_data::CommonCircuitData; + pub type VerifierOnlyCircuitData = plonk::circuit_data::VerifierOnlyCircuitData; + pub type VerifierCircuitData = plonk::circuit_data::VerifierCircuitData; + // pub type ProverOnlyCircuitData = plonk::circuit_data::ProverOnlyCircuitData; + pub type ProofWithPublicInputs = plonk::proof::ProofWithPublicInputs; + + let Ok(common) = CommonCircuitData::from_bytes(common_curcuit_data, &DefaultGateSerializer) else { + return Plonky2VerifyResult::FailedToDecodeCommonData.into(); + }; + + let Ok(verifier_only) = VerifierOnlyCircuitData::from_bytes(verifier_circuit_data) else { + return Plonky2VerifyResult::FailedToDecodeVerifierData.into(); + }; + + let Ok(proof_with_pis) = ProofWithPublicInputs::from_bytes(proof, &common) else { + return Plonky2VerifyResult::FailedToDecodeProof.into(); + }; + + let verifier_circuit_data = VerifierCircuitData { verifier_only, common }; + match verifier_circuit_data.verify(proof_with_pis) { + Ok(()) => Plonky2VerifyResult::Verified, + Err(e) => { + log::debug!("VerifierCircuitData::verify failed: {e:?}"); + + Plonky2VerifyResult::Rejected + } + }.into() + } +}