From b4fa892ab04811876291690b77cbd692902371e5 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 11:08:31 +0800 Subject: [PATCH 01/16] Optimize bls precompile by arkworks lib --- Cargo.lock | 138 ++++++++++++++++++ Cargo.toml | 8 ++ precompile/ark-bls12-381/Cargo.toml | 56 ++++++++ precompile/ark-bls12-381/src/bls/mod.rs | 183 ++++++++++++++++++++++++ precompile/ark-bls12-381/src/lib.rs | 59 ++++++++ shell.nix | 26 ++++ 6 files changed, 470 insertions(+) create mode 100644 precompile/ark-bls12-381/Cargo.toml create mode 100644 precompile/ark-bls12-381/src/bls/mod.rs create mode 100644 precompile/ark-bls12-381/src/lib.rs create mode 100644 shell.nix diff --git a/Cargo.lock b/Cargo.lock index 17ef57f86..383dfd0c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -233,6 +233,124 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" +[[package]] +name = "ark-bls12-381" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c775f0d12169cba7aae4caeb547bb6a50781c7449a8aa53793827c9ec4abf488" +dependencies = [ + "ark-ec", + "ark-ff", + "ark-serialize", + "ark-std", +] + +[[package]] +name = "ark-ec" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c60370a92f8e1a5f053cad73a862e1b99bc642333cd676fa11c0c39f80f4ac2" +dependencies = [ + "ark-ff", + "ark-poly", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.13.2", + "itertools", + "num-traits", + "zeroize", +] + +[[package]] +name = "ark-ff" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2d42532524bee1da5a4f6f733eb4907301baa480829557adcff5dfaeee1d9a" +dependencies = [ + "ark-ff-asm", + "ark-ff-macros", + "ark-serialize", + "ark-std", + "derivative", + "digest 0.10.6", + "itertools", + "num-bigint", + "num-traits", + "paste", + "rustc_version", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d6873aaba7959593d89babed381d33e2329453368f1bf3c67e07686a1c1056f" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "ark-ff-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c2e7d0f2d67cc7fc925355c74d36e7eda19073639be4a0a233d4611b8c959d" +dependencies = [ + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ark-poly" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f6ec811462cabe265cfe1b102fcfe3df79d7d2929c2425673648ee9abfd0272" +dependencies = [ + "ark-ff", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.13.2", +] + +[[package]] +name = "ark-serialize" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e735959bc173ea4baf13327b19c22d452b8e9e8e8f7b7fc34e6bf0e316c33e" +dependencies = [ + "ark-serialize-derive", + "ark-std", + "digest 0.10.6", + "num-bigint", +] + +[[package]] +name = "ark-serialize-derive" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd34f0920d995d2c932f38861c416f70de89a6de9875876b012557079603e6cc" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ark-std" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" +dependencies = [ + "num-traits", + "rand 0.8.5", +] + [[package]] name = "array-bytes" version = "4.2.0" @@ -2506,6 +2624,23 @@ dependencies = [ "sp-std", ] +[[package]] +name = "darwinia-precompile-ark-bls12-381" +version = "6.0.0" +dependencies = [ + "ark-bls12-381", + "ark-ec", + "ark-ff", + "ark-serialize", + "ark-std", + "fp-evm", + "pallet-evm", + "precompile-utils", + "rand 0.8.5", + "sha2 0.10.6", + "sp-std", +] + [[package]] name = "darwinia-precompile-assets" version = "6.0.0" @@ -4424,6 +4559,9 @@ name = "hashbrown" version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash 0.8.3", +] [[package]] name = "heck" diff --git a/Cargo.toml b/Cargo.toml index 5a14eca28..ae20d54ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,12 +23,20 @@ repository = "https://github.com/darwinia-network/darwinia" version = "6.0.0" [workspace.dependencies] +# arkworks +ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } +ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} +ark-std = { default-features = false, version = "0.4.0" } +ark-ec = { default-features = false, version = "0.4.1" } +ark-ff = { default-features = false, version = "0.4.1" } + # crates.io array-bytes = { version = "6.0" } codec = { package = "parity-scale-codec", version = "3.3", default-features = false, features = ["derive"] } libsecp256k1 = { version = "0.7" } scale-info = { version = "2.3", default-features = false, features = ["derive"] } sha3 = { version = "0.9" } +sha2 = { version = "0.10.6", default-features = false } static_assertions = { version = "1.1" } # cumulus diff --git a/precompile/ark-bls12-381/Cargo.toml b/precompile/ark-bls12-381/Cargo.toml new file mode 100644 index 000000000..d0f5d76cb --- /dev/null +++ b/precompile/ark-bls12-381/Cargo.toml @@ -0,0 +1,56 @@ +[package] +authors.workspace = true +description = "Arkworks BLS12-381 based precompile for EVM pallet." +edition.workspace = true +name = "darwinia-precompile-ark-bls12-381" +readme = "README.md" +version.workspace = true + +[dependencies] +# crates.io +sha2 = { workspace = true } + +# arkworks +ark-bls12-381 = { workspace = true } +ark-serialize = { workspace = true } +ark-std = { workspace = true } +ark-ec = { workspace = true } +ark-ff = { workspace = true } + +# frontier +pallet-evm = { workspace = true } +fp-evm = { workspace = true } + +# moonbeam +precompile-utils = { workspace = true } + +# substrate +sp-std = { workspace = true } + +[dev-dependencies] +rand = "0.8.5" + +[features] +default = ["std"] +std = [ + # crates.io + "sha2/std", + + # arkworks + "ark-bls12-381/std", + "ark-serialize/std", + "ark-std/std", + "ark-ec/std", + "ark-ff/std", + + # frontier + "pallet-evm/std", + "fp-evm/std", + + # moonbeam + "precompile-utils/std", + + # substrate + "sp-std/std", +] + diff --git a/precompile/ark-bls12-381/src/bls/mod.rs b/precompile/ark-bls12-381/src/bls/mod.rs new file mode 100644 index 000000000..bf8c86483 --- /dev/null +++ b/precompile/ark-bls12-381/src/bls/mod.rs @@ -0,0 +1,183 @@ +// This file is part of Darwinia. +// +// Copyright (C) 2018-2023 Darwinia Network +// SPDX-License-Identifier: GPL-3.0 +// +// Darwinia is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Darwinia is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Darwinia. If not, see . +// +// Inspired from https://github.com/w3f/apk-proofs/blob/main/bw6/src/bls/mod.rs + +use ark_bls12_381::{ + g2::Config as G2Config, Bls12_381, G1Affine, G1Projective, G2Affine, G2Projective, +}; +use ark_ec::{ + hashing::{ + curve_maps::wb::WBMap, map_to_curve_hasher::MapToCurveBasedHasher, HashToCurve, + HashToCurveError, + }, + models::short_weierstrass::Projective, + pairing::Pairing, + AffineRepr, CurveGroup, +}; +use ark_ff::{field_hashers::DefaultFieldHasher, Zero}; +use ark_serialize::*; +use ark_std::vec::Vec; +use core::ops::Neg; +use sha2::Sha256; +use sp_std::borrow::Borrow; + +/// Domain Separation Tag for signatures on G2 +pub const DST_G2: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; + +#[derive(Clone, Debug)] +pub struct Signature(G2Projective); + +impl From for Signature { + fn from(sig: G2Projective) -> Signature { + Signature(sig) + } +} + +impl AsRef for Signature { + fn as_ref(&self) -> &G2Projective { + &self.0 + } +} + +impl Signature { + pub fn from_bytes(bytes: &[u8]) -> Result { + let p = G2Affine::deserialize_compressed(bytes)?; + Ok(Self(p.into())) + } + + #[allow(dead_code)] + pub fn aggregate>(signatures: impl IntoIterator) -> Signature { + signatures.into_iter().map(|s| s.borrow().0).sum::().into() + } +} + +#[derive(Clone, Debug, Eq, PartialEq, Hash, CanonicalSerialize, CanonicalDeserialize)] +pub struct PublicKey(pub G1Projective); + +impl From for PublicKey { + fn from(pk: G1Projective) -> PublicKey { + PublicKey(pk) + } +} + +impl PublicKey { + pub fn from_bytes(bytes: &[u8]) -> Result { + let p = G1Affine::deserialize_compressed(bytes)?; + Ok(Self(p.into())) + } + + pub fn aggregate>(public_keys: impl IntoIterator) -> PublicKey { + public_keys.into_iter().map(|s| s.borrow().0).sum::().into() + } + + pub fn verify(&self, signature: &Signature, message: &G2Projective) -> bool { + Bls12_381::multi_pairing( + [G1Affine::generator().neg(), self.0.into_affine()], + [signature.as_ref().into_affine(), message.into_affine()], + ) + .is_zero() + } +} + +pub fn hash_to_curve_g2(message: &[u8]) -> Result { + let wb_to_curve_hasher = MapToCurveBasedHasher::< + Projective, + DefaultFieldHasher, + WBMap, + >::new(DST_G2)?; + Ok(wb_to_curve_hasher.hash(message)?.into()) +} + +#[cfg(test)] +mod tests { + use ark_std::test_rng; + use rand::Rng; + + use super::*; + + use ark_bls12_381::Fr; + use ark_ec::Group; + use ark_ff::UniformRand; + + #[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)] + pub struct SecretKey(Fr); + + impl From for SecretKey { + fn from(sk: Fr) -> SecretKey { + SecretKey(sk) + } + } + + impl From<&SecretKey> for PublicKey { + fn from(sk: &SecretKey) -> PublicKey { + (G1Projective::generator() * sk.as_ref()).into() + } + } + + impl AsRef for SecretKey { + fn as_ref(&self) -> &Fr { + &self.0 + } + } + + impl SecretKey { + pub fn new(rng: &mut R) -> SecretKey { + SecretKey(Fr::rand(rng)) + } + + pub fn sign(&self, message: &G2Projective) -> Signature { + (*message * self.as_ref()).into() + } + } + + #[test] + fn test_apk() { + let rng = &mut test_rng(); + let message = G2Projective::rand(rng); + + let sks = (0..10).map(|_| SecretKey::new(rng)).collect::>(); + let pks = sks.iter().map(PublicKey::from).collect::>(); + let sigs = sks.iter().map(|sk| sk.sign(&message)).collect::>(); + pks.iter().zip(sigs.iter()).for_each(|(pk, sig)| assert!(pk.verify(sig, &message))); + + let apk = PublicKey::aggregate(pks); + let asig = Signature::aggregate(sigs); + assert!(apk.verify(&asig, &message)); + } + + #[test] + fn test_h2c() { + let message = vec![ + 58, 137, 108, 164, 181, 219, 16, 43, 157, 253, 71, 82, 139, 6, 34, 10, 145, 189, 18, + 70, 29, 204, 134, 121, 60, 226, 213, 145, 244, 30, 164, 248, + ]; + let e = vec![ + 178, 18, 44, 225, 215, 170, 68, 228, 52, 151, 40, 113, 171, 202, 76, 203, 156, 112, + 105, 249, 147, 210, 132, 79, 69, 117, 109, 151, 35, 71, 117, 21, 119, 179, 181, 81, 92, + 22, 22, 88, 190, 243, 147, 248, 3, 210, 87, 98, 0, 84, 201, 248, 182, 249, 99, 59, 86, + 60, 71, 244, 250, 189, 134, 232, 18, 82, 72, 76, 83, 155, 46, 113, 128, 107, 49, 67, + 174, 100, 244, 181, 33, 174, 14, 151, 112, 62, 141, 100, 173, 191, 103, 178, 205, 17, + 237, 147, + ]; + let p: G2Affine = hash_to_curve_g2(&message).unwrap().into(); + let mut c = Vec::new(); + p.serialize_compressed(&mut c).unwrap(); + assert_eq!(e, c); + } +} diff --git a/precompile/ark-bls12-381/src/lib.rs b/precompile/ark-bls12-381/src/lib.rs new file mode 100644 index 000000000..7ea2bf80a --- /dev/null +++ b/precompile/ark-bls12-381/src/lib.rs @@ -0,0 +1,59 @@ +// This file is part of Darwinia. +// +// Copyright (C) 2018-2023 Darwinia Network +// SPDX-License-Identifier: GPL-3.0 +// +// Darwinia is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Darwinia is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Darwinia. If not, see . + +#![cfg_attr(not(feature = "std"), no_std)] + +mod bls; + +// core +use core::marker::PhantomData; +// bls +use bls::{hash_to_curve_g2, PublicKey, Signature}; +// moonbeam +use precompile_utils::prelude::*; +// substrate +use sp_std::prelude::*; + +pub(crate) const BLS_ESTIMATED_COST: u64 = 100_000; +pub struct ARKBLS12381(PhantomData); + +#[precompile_utils::precompile] +impl ARKBLS12381 { + #[precompile::public("fast_aggregate_verify(bytes[],bytes,bytes)")] + #[precompile::view] + fn fast_aggregate_verify( + handle: &mut impl PrecompileHandle, + pubkeys: Vec, + message: UnboundedBytes, + signature: UnboundedBytes, + ) -> EvmResult { + handle.record_cost(BLS_ESTIMATED_COST)?; + + let asig = + Signature::from_bytes(signature.as_bytes()).map_err(|_| revert("Invalid signature"))?; + let public_keys: Result, _> = + pubkeys.into_iter().map(|k| PublicKey::from_bytes(k.as_bytes())).collect(); + let Ok(pks) = public_keys else { + return Err(revert("Invalid pubkeys")); + }; + + let apk = PublicKey::aggregate(pks); + let msg = hash_to_curve_g2(message.as_bytes()).map_err(|_| revert("Invalid message"))?; + Ok(apk.verify(&asig, &msg)) + } +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 000000000..e4f93645e --- /dev/null +++ b/shell.nix @@ -0,0 +1,26 @@ +let + mozillaOverlay = + import (builtins.fetchGit { + url = "https://github.com/mozilla/nixpkgs-mozilla.git"; + rev = "78e723925daf5c9e8d0a1837ec27059e61649cb6"; + }); + nixpkgs = import { overlays = [ mozillaOverlay ]; }; + rust-nightly = with nixpkgs; ((rustChannelOf { date = "2022-11-15"; channel = "nightly"; }).rust.override { + extensions = [ "rust-src" ]; + targets = [ "wasm32-unknown-unknown" ]; + }); +in +with nixpkgs; pkgs.mkShell { + nativeBuildInputs = [ + rust-nightly + ]; + buildInputs = [ + clang + rocksdb + ]; + + RUST_SRC_PATH = "${rust-nightly}/lib/rustlib/src/rust/src"; + LIBCLANG_PATH = "${llvmPackages.libclang.lib}/lib"; + PROTOC = "${protobuf}/bin/protoc"; + ROCKSDB_LIB_DIR = "${rocksdb}/lib"; +} From 6c222be312b7dae1ac28e8cb58ea796b325f46e6 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 11:13:14 +0800 Subject: [PATCH 02/16] Pin bls-test precompose at `address(2017)` --- Cargo.lock | 1 + Cargo.toml | 1 + runtime/darwinia/Cargo.toml | 1 + runtime/darwinia/src/pallets/evm.rs | 5 +++++ 4 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 383dfd0c1..b1387117e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2765,6 +2765,7 @@ dependencies = [ "darwinia-ecdsa-authority", "darwinia-message-gadget", "darwinia-message-transact", + "darwinia-precompile-ark-bls12-381", "darwinia-precompile-assets", "darwinia-precompile-bls12-381", "darwinia-precompile-deposit", diff --git a/Cargo.toml b/Cargo.toml index ae20d54ef..6ceb90e59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ darwinia-ecdsa-authority = { default-features = false, path = "pallet/e darwinia-message-gadget = { default-features = false, path = "pallet/message-gadget" } darwinia-message-transact = { default-features = false, path = "pallet/message-transact" } darwinia-precompile-assets = { default-features = false, path = "precompile/assets" } +darwinia-precompile-ark-bls12-381 = { default-features = false, path = "precompile/ark-bls12-381" } darwinia-precompile-bls12-381 = { default-features = false, path = "precompile/bls12-381" } darwinia-precompile-deposit = { default-features = false, path = "precompile/deposit" } darwinia-precompile-staking = { default-features = false, path = "precompile/staking" } diff --git a/runtime/darwinia/Cargo.toml b/runtime/darwinia/Cargo.toml index 80f461988..909f369ef 100644 --- a/runtime/darwinia/Cargo.toml +++ b/runtime/darwinia/Cargo.toml @@ -38,6 +38,7 @@ darwinia-ecdsa-authority = { workspace = true } darwinia-message-gadget = { workspace = true } darwinia-message-transact = { workspace = true } darwinia-precompile-assets = { workspace = true } +darwinia-precompile-ark-bls12-381 = { workspace = true } darwinia-precompile-bls12-381 = { workspace = true } darwinia-precompile-deposit = { workspace = true } darwinia-precompile-staking = { workspace = true } diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 49ba628dd..3aca8214c 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -66,6 +66,8 @@ where addr(1026), addr(1536), addr(1537), + // Test only + addr(2047), addr(2048), ] } @@ -117,6 +119,9 @@ where Some(>::execute(handle)), a if a == addr(1537) => Some(>::execute(handle)), + a if a == addr(2047) => + Some(>::execute(handle)), + _ => None, // [2048..) reserved for the experimental precompiles. a if a == addr(2048) => Some(>::execute(handle)), From 58bfdbccf0be6dee4b8fc21bf999262626aaf6e4 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 11:21:54 +0800 Subject: [PATCH 03/16] Fmt --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ceb90e59..abf8d74db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,9 @@ version = "6.0.0" # arkworks ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} -ark-std = { default-features = false, version = "0.4.0" } -ark-ec = { default-features = false, version = "0.4.1" } -ark-ff = { default-features = false, version = "0.4.1" } +ark-std = { default-features = false, version = "0.4.0" } +ark-ec = { default-features = false, version = "0.4.1" } +ark-ff = { default-features = false, version = "0.4.1" } # crates.io array-bytes = { version = "6.0" } From 039df8f2fe9883eaacf580df43ce4359a7233589 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 11:23:53 +0800 Subject: [PATCH 04/16] Update deps --- Cargo.toml | 8 -------- precompile/ark-bls12-381/Cargo.toml | 13 +++++++------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index abf8d74db..400cb05dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,20 +23,12 @@ repository = "https://github.com/darwinia-network/darwinia" version = "6.0.0" [workspace.dependencies] -# arkworks -ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } -ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} -ark-std = { default-features = false, version = "0.4.0" } -ark-ec = { default-features = false, version = "0.4.1" } -ark-ff = { default-features = false, version = "0.4.1" } - # crates.io array-bytes = { version = "6.0" } codec = { package = "parity-scale-codec", version = "3.3", default-features = false, features = ["derive"] } libsecp256k1 = { version = "0.7" } scale-info = { version = "2.3", default-features = false, features = ["derive"] } sha3 = { version = "0.9" } -sha2 = { version = "0.10.6", default-features = false } static_assertions = { version = "1.1" } # cumulus diff --git a/precompile/ark-bls12-381/Cargo.toml b/precompile/ark-bls12-381/Cargo.toml index d0f5d76cb..1c94103b6 100644 --- a/precompile/ark-bls12-381/Cargo.toml +++ b/precompile/ark-bls12-381/Cargo.toml @@ -8,14 +8,15 @@ version.workspace = true [dependencies] # crates.io -sha2 = { workspace = true } +sha2 = { version = "0.10.6", default-features = false } # arkworks -ark-bls12-381 = { workspace = true } -ark-serialize = { workspace = true } -ark-std = { workspace = true } -ark-ec = { workspace = true } -ark-ff = { workspace = true } +ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } +ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} +ark-std = { default-features = false, version = "0.4.0" } +ark-ec = { default-features = false, version = "0.4.1" } +ark-ff = { default-features = false, version = "0.4.1" } + # frontier pallet-evm = { workspace = true } From fb74aa40213c56753be3d022a43837e246340c83 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 11:36:37 +0800 Subject: [PATCH 05/16] Add openssl deps in nix --- shell.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shell.nix b/shell.nix index e4f93645e..5e2c4423f 100644 --- a/shell.nix +++ b/shell.nix @@ -17,6 +17,10 @@ with nixpkgs; pkgs.mkShell { buildInputs = [ clang rocksdb + pkg-config + openssl.dev + ] ++ lib.optionals stdenv.isDarwin [ + darwin.apple_sdk.frameworks.Security ]; RUST_SRC_PATH = "${rust-nightly}/lib/rustlib/src/rust/src"; From 807a0e6ccdf30e8ae6564654a6ea6ad8d1bb81a6 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Thu, 9 Mar 2023 11:40:02 +0800 Subject: [PATCH 06/16] Remove empty line --- precompile/ark-bls12-381/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/precompile/ark-bls12-381/Cargo.toml b/precompile/ark-bls12-381/Cargo.toml index 1c94103b6..288a0b552 100644 --- a/precompile/ark-bls12-381/Cargo.toml +++ b/precompile/ark-bls12-381/Cargo.toml @@ -17,7 +17,6 @@ ark-std = { default-features = false, version = "0.4.0" } ark-ec = { default-features = false, version = "0.4.1" } ark-ff = { default-features = false, version = "0.4.1" } - # frontier pallet-evm = { workspace = true } fp-evm = { workspace = true } From 979cfd3c159e268308cd041c2cd5a508adcc346e Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Thu, 9 Mar 2023 11:40:03 +0800 Subject: [PATCH 07/16] Format and fix compile --- precompile/ark-bls12-381/Cargo.toml | 19 ++++++++----------- precompile/ark-bls12-381/src/bls/mod.rs | 5 +++-- runtime/darwinia/src/pallets/evm.rs | 3 +-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/precompile/ark-bls12-381/Cargo.toml b/precompile/ark-bls12-381/Cargo.toml index 288a0b552..77c736f8a 100644 --- a/precompile/ark-bls12-381/Cargo.toml +++ b/precompile/ark-bls12-381/Cargo.toml @@ -8,18 +8,16 @@ version.workspace = true [dependencies] # crates.io -sha2 = { version = "0.10.6", default-features = false } - -# arkworks -ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } -ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} -ark-std = { default-features = false, version = "0.4.0" } -ark-ec = { default-features = false, version = "0.4.1" } -ark-ff = { default-features = false, version = "0.4.1" } +ark-bls12-381 = { version = "0.4.0", default-features = false, features = ["curve"] } +ark-ec = { version = "0.4.1", default-features = false } +ark-ff = { version = "0.4.1", default-features = false } +ark-serialize = { version = "0.4.1", default-features = false, features = ["derive"] } +ark-std = { version = "0.4.0", default-features = false } +sha2 = { version = "0.10.6", default-features = false } # frontier -pallet-evm = { workspace = true } fp-evm = { workspace = true } +pallet-evm = { workspace = true } # moonbeam precompile-utils = { workspace = true } @@ -28,7 +26,7 @@ precompile-utils = { workspace = true } sp-std = { workspace = true } [dev-dependencies] -rand = "0.8.5" +rand = { version = "0.8.5" } [features] default = ["std"] @@ -53,4 +51,3 @@ std = [ # substrate "sp-std/std", ] - diff --git a/precompile/ark-bls12-381/src/bls/mod.rs b/precompile/ark-bls12-381/src/bls/mod.rs index bf8c86483..1d78f8edd 100644 --- a/precompile/ark-bls12-381/src/bls/mod.rs +++ b/precompile/ark-bls12-381/src/bls/mod.rs @@ -18,6 +18,9 @@ // // Inspired from https://github.com/w3f/apk-proofs/blob/main/bw6/src/bls/mod.rs +// core +use core::{borrow::Borrow, ops::Neg}; +// crates.io use ark_bls12_381::{ g2::Config as G2Config, Bls12_381, G1Affine, G1Projective, G2Affine, G2Projective, }; @@ -33,9 +36,7 @@ use ark_ec::{ use ark_ff::{field_hashers::DefaultFieldHasher, Zero}; use ark_serialize::*; use ark_std::vec::Vec; -use core::ops::Neg; use sha2::Sha256; -use sp_std::borrow::Borrow; /// Domain Separation Tag for signatures on G2 pub const DST_G2: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 3aca8214c..17946f754 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -49,7 +49,7 @@ where Self(Default::default()) } - pub fn used_addresses() -> [sp_core::H160; 15] { + pub fn used_addresses() -> [sp_core::H160; 16] { [ addr(1), addr(2), @@ -121,7 +121,6 @@ where Some(>::execute(handle)), a if a == addr(2047) => Some(>::execute(handle)), - _ => None, // [2048..) reserved for the experimental precompiles. a if a == addr(2048) => Some(>::execute(handle)), From 2f26478d905f3357c60a8d27de52ace33256629c Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Thu, 9 Mar 2023 11:49:53 +0800 Subject: [PATCH 08/16] Fix --- runtime/darwinia/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/darwinia/Cargo.toml b/runtime/darwinia/Cargo.toml index 909f369ef..3f2595986 100644 --- a/runtime/darwinia/Cargo.toml +++ b/runtime/darwinia/Cargo.toml @@ -37,8 +37,8 @@ darwinia-deposit = { workspace = true } darwinia-ecdsa-authority = { workspace = true } darwinia-message-gadget = { workspace = true } darwinia-message-transact = { workspace = true } -darwinia-precompile-assets = { workspace = true } darwinia-precompile-ark-bls12-381 = { workspace = true } +darwinia-precompile-assets = { workspace = true } darwinia-precompile-bls12-381 = { workspace = true } darwinia-precompile-deposit = { workspace = true } darwinia-precompile-staking = { workspace = true } @@ -158,6 +158,7 @@ std = [ "darwinia-ecdsa-authority/std", "darwinia-message-gadget/std", "darwinia-message-transact/std", + "darwinia-precompile-ark-bls12-381/std", "darwinia-precompile-assets/std", "darwinia-precompile-bls12-381/std", "darwinia-precompile-staking/std", From 578e325d403a3314d7475d6dfa0a1d7f3f20723a Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 12:29:17 +0800 Subject: [PATCH 09/16] Resolve conv --- Cargo.lock | 33 +++-------- Cargo.toml | 1 - precompile/ark-bls12-381/Cargo.toml | 57 ------------------ precompile/ark-bls12-381/src/lib.rs | 59 ------------------- precompile/bls12-381/Cargo.toml | 43 ++++++++------ .../src/bls/mod.rs | 0 precompile/bls12-381/src/lib.rs | 33 ++++++++--- runtime/darwinia/Cargo.toml | 1 - runtime/darwinia/src/pallets/evm.rs | 5 -- shell.nix | 5 +- 10 files changed, 59 insertions(+), 178 deletions(-) delete mode 100644 precompile/ark-bls12-381/Cargo.toml delete mode 100644 precompile/ark-bls12-381/src/lib.rs rename precompile/{ark-bls12-381 => bls12-381}/src/bls/mod.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index b1387117e..85f860a7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2624,23 +2624,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "darwinia-precompile-ark-bls12-381" -version = "6.0.0" -dependencies = [ - "ark-bls12-381", - "ark-ec", - "ark-ff", - "ark-serialize", - "ark-std", - "fp-evm", - "pallet-evm", - "precompile-utils", - "rand 0.8.5", - "sha2 0.10.6", - "sp-std", -] - [[package]] name = "darwinia-precompile-assets" version = "6.0.0" @@ -2666,17 +2649,16 @@ dependencies = [ name = "darwinia-precompile-bls12-381" version = "6.0.0" dependencies = [ + "ark-bls12-381", + "ark-ec", + "ark-ff", + "ark-serialize", + "ark-std", "fp-evm", - "frame-system", - "pallet-balances", "pallet-evm", - "pallet-timestamp", - "parity-scale-codec", "precompile-utils", - "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "rand 0.8.5", + "sha2 0.10.6", "sp-std", ] @@ -2765,7 +2747,6 @@ dependencies = [ "darwinia-ecdsa-authority", "darwinia-message-gadget", "darwinia-message-transact", - "darwinia-precompile-ark-bls12-381", "darwinia-precompile-assets", "darwinia-precompile-bls12-381", "darwinia-precompile-deposit", diff --git a/Cargo.toml b/Cargo.toml index 400cb05dd..5a14eca28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,6 @@ darwinia-ecdsa-authority = { default-features = false, path = "pallet/e darwinia-message-gadget = { default-features = false, path = "pallet/message-gadget" } darwinia-message-transact = { default-features = false, path = "pallet/message-transact" } darwinia-precompile-assets = { default-features = false, path = "precompile/assets" } -darwinia-precompile-ark-bls12-381 = { default-features = false, path = "precompile/ark-bls12-381" } darwinia-precompile-bls12-381 = { default-features = false, path = "precompile/bls12-381" } darwinia-precompile-deposit = { default-features = false, path = "precompile/deposit" } darwinia-precompile-staking = { default-features = false, path = "precompile/staking" } diff --git a/precompile/ark-bls12-381/Cargo.toml b/precompile/ark-bls12-381/Cargo.toml deleted file mode 100644 index 1c94103b6..000000000 --- a/precompile/ark-bls12-381/Cargo.toml +++ /dev/null @@ -1,57 +0,0 @@ -[package] -authors.workspace = true -description = "Arkworks BLS12-381 based precompile for EVM pallet." -edition.workspace = true -name = "darwinia-precompile-ark-bls12-381" -readme = "README.md" -version.workspace = true - -[dependencies] -# crates.io -sha2 = { version = "0.10.6", default-features = false } - -# arkworks -ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } -ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} -ark-std = { default-features = false, version = "0.4.0" } -ark-ec = { default-features = false, version = "0.4.1" } -ark-ff = { default-features = false, version = "0.4.1" } - - -# frontier -pallet-evm = { workspace = true } -fp-evm = { workspace = true } - -# moonbeam -precompile-utils = { workspace = true } - -# substrate -sp-std = { workspace = true } - -[dev-dependencies] -rand = "0.8.5" - -[features] -default = ["std"] -std = [ - # crates.io - "sha2/std", - - # arkworks - "ark-bls12-381/std", - "ark-serialize/std", - "ark-std/std", - "ark-ec/std", - "ark-ff/std", - - # frontier - "pallet-evm/std", - "fp-evm/std", - - # moonbeam - "precompile-utils/std", - - # substrate - "sp-std/std", -] - diff --git a/precompile/ark-bls12-381/src/lib.rs b/precompile/ark-bls12-381/src/lib.rs deleted file mode 100644 index 7ea2bf80a..000000000 --- a/precompile/ark-bls12-381/src/lib.rs +++ /dev/null @@ -1,59 +0,0 @@ -// This file is part of Darwinia. -// -// Copyright (C) 2018-2023 Darwinia Network -// SPDX-License-Identifier: GPL-3.0 -// -// Darwinia is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Darwinia is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Darwinia. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -mod bls; - -// core -use core::marker::PhantomData; -// bls -use bls::{hash_to_curve_g2, PublicKey, Signature}; -// moonbeam -use precompile_utils::prelude::*; -// substrate -use sp_std::prelude::*; - -pub(crate) const BLS_ESTIMATED_COST: u64 = 100_000; -pub struct ARKBLS12381(PhantomData); - -#[precompile_utils::precompile] -impl ARKBLS12381 { - #[precompile::public("fast_aggregate_verify(bytes[],bytes,bytes)")] - #[precompile::view] - fn fast_aggregate_verify( - handle: &mut impl PrecompileHandle, - pubkeys: Vec, - message: UnboundedBytes, - signature: UnboundedBytes, - ) -> EvmResult { - handle.record_cost(BLS_ESTIMATED_COST)?; - - let asig = - Signature::from_bytes(signature.as_bytes()).map_err(|_| revert("Invalid signature"))?; - let public_keys: Result, _> = - pubkeys.into_iter().map(|k| PublicKey::from_bytes(k.as_bytes())).collect(); - let Ok(pks) = public_keys else { - return Err(revert("Invalid pubkeys")); - }; - - let apk = PublicKey::aggregate(pks); - let msg = hash_to_curve_g2(message.as_bytes()).map_err(|_| revert("Invalid message"))?; - Ok(apk.verify(&asig, &msg)) - } -} diff --git a/precompile/bls12-381/Cargo.toml b/precompile/bls12-381/Cargo.toml index 1bbb86b3e..5f2d4110a 100644 --- a/precompile/bls12-381/Cargo.toml +++ b/precompile/bls12-381/Cargo.toml @@ -1,15 +1,26 @@ [package] authors.workspace = true -description = "BLS12-381 implementation for EVM pallet." +description = "Arkworks BLS12-381 based precompile for EVM pallet." edition.workspace = true name = "darwinia-precompile-bls12-381" readme = "README.md" version.workspace = true [dependencies] +# crates.io +sha2 = { version = "0.10.6", default-features = false } + +# arkworks +ark-bls12-381 = { default-features = false, version = "0.4.0", features = ["curve"] } +ark-serialize = { default-features = false, version = "0.4.1", features = ["derive"]} +ark-std = { default-features = false, version = "0.4.0" } +ark-ec = { default-features = false, version = "0.4.1" } +ark-ff = { default-features = false, version = "0.4.1" } + + # frontier -fp-evm = { workspace = true } pallet-evm = { workspace = true } +fp-evm = { workspace = true } # moonbeam precompile-utils = { workspace = true } @@ -18,27 +29,24 @@ precompile-utils = { workspace = true } sp-std = { workspace = true } [dev-dependencies] -# crates.io -codec = { package = "parity-scale-codec", workspace = true } -scale-info = { workspace = true } - -# moonbeam -precompile-utils = { workspace = true, features = ["testing"] } - -# substrate -frame-system = { workspace = true } -pallet-balances = { workspace = true, features = ["std"] } -pallet-timestamp = { workspace = true, features = ["std"] } -sp-core = { workspace = true } -sp-io = { workspace = true } -sp-runtime = { workspace = true } +rand = "0.8.5" [features] default = ["std"] std = [ + # crates.io + "sha2/std", + + # arkworks + "ark-bls12-381/std", + "ark-serialize/std", + "ark-std/std", + "ark-ec/std", + "ark-ff/std", + # frontier - "fp-evm/std", "pallet-evm/std", + "fp-evm/std", # moonbeam "precompile-utils/std", @@ -46,3 +54,4 @@ std = [ # substrate "sp-std/std", ] + diff --git a/precompile/ark-bls12-381/src/bls/mod.rs b/precompile/bls12-381/src/bls/mod.rs similarity index 100% rename from precompile/ark-bls12-381/src/bls/mod.rs rename to precompile/bls12-381/src/bls/mod.rs diff --git a/precompile/bls12-381/src/lib.rs b/precompile/bls12-381/src/lib.rs index 04a03bad6..7ea2bf80a 100644 --- a/precompile/bls12-381/src/lib.rs +++ b/precompile/bls12-381/src/lib.rs @@ -18,25 +18,42 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod bls; + // core use core::marker::PhantomData; +// bls +use bls::{hash_to_curve_g2, PublicKey, Signature}; // moonbeam use precompile_utils::prelude::*; // substrate use sp_std::prelude::*; -pub struct BLS12381(PhantomData); +pub(crate) const BLS_ESTIMATED_COST: u64 = 100_000; +pub struct ARKBLS12381(PhantomData); #[precompile_utils::precompile] -impl BLS12381 { +impl ARKBLS12381 { #[precompile::public("fast_aggregate_verify(bytes[],bytes,bytes)")] #[precompile::view] - fn state_storage_at( - _handle: &mut impl PrecompileHandle, - _pubkeys: Vec, - _message: UnboundedBytes, - _signature: UnboundedBytes, + fn fast_aggregate_verify( + handle: &mut impl PrecompileHandle, + pubkeys: Vec, + message: UnboundedBytes, + signature: UnboundedBytes, ) -> EvmResult { - return Err(revert("Unavailable now")); + handle.record_cost(BLS_ESTIMATED_COST)?; + + let asig = + Signature::from_bytes(signature.as_bytes()).map_err(|_| revert("Invalid signature"))?; + let public_keys: Result, _> = + pubkeys.into_iter().map(|k| PublicKey::from_bytes(k.as_bytes())).collect(); + let Ok(pks) = public_keys else { + return Err(revert("Invalid pubkeys")); + }; + + let apk = PublicKey::aggregate(pks); + let msg = hash_to_curve_g2(message.as_bytes()).map_err(|_| revert("Invalid message"))?; + Ok(apk.verify(&asig, &msg)) } } diff --git a/runtime/darwinia/Cargo.toml b/runtime/darwinia/Cargo.toml index 909f369ef..80f461988 100644 --- a/runtime/darwinia/Cargo.toml +++ b/runtime/darwinia/Cargo.toml @@ -38,7 +38,6 @@ darwinia-ecdsa-authority = { workspace = true } darwinia-message-gadget = { workspace = true } darwinia-message-transact = { workspace = true } darwinia-precompile-assets = { workspace = true } -darwinia-precompile-ark-bls12-381 = { workspace = true } darwinia-precompile-bls12-381 = { workspace = true } darwinia-precompile-deposit = { workspace = true } darwinia-precompile-staking = { workspace = true } diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 3aca8214c..49ba628dd 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -66,8 +66,6 @@ where addr(1026), addr(1536), addr(1537), - // Test only - addr(2047), addr(2048), ] } @@ -119,9 +117,6 @@ where Some(>::execute(handle)), a if a == addr(1537) => Some(>::execute(handle)), - a if a == addr(2047) => - Some(>::execute(handle)), - _ => None, // [2048..) reserved for the experimental precompiles. a if a == addr(2048) => Some(>::execute(handle)), diff --git a/shell.nix b/shell.nix index 5e2c4423f..004f77499 100644 --- a/shell.nix +++ b/shell.nix @@ -5,10 +5,7 @@ let rev = "78e723925daf5c9e8d0a1837ec27059e61649cb6"; }); nixpkgs = import { overlays = [ mozillaOverlay ]; }; - rust-nightly = with nixpkgs; ((rustChannelOf { date = "2022-11-15"; channel = "nightly"; }).rust.override { - extensions = [ "rust-src" ]; - targets = [ "wasm32-unknown-unknown" ]; - }); + rust-nightly = with nixpkgs; (nixpkgs.latest.rustChannels.nightly.rust); in with nixpkgs; pkgs.mkShell { nativeBuildInputs = [ From 6a1a53456ee9d055168ce1cf2cb5fd2cf29bae27 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 13:01:06 +0800 Subject: [PATCH 10/16] Fix --- precompile/bls12-381/Cargo.toml | 5 ++--- precompile/bls12-381/src/bls/mod.rs | 5 ++++- precompile/bls12-381/src/lib.rs | 4 ++-- shell.nix | 7 +++++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/precompile/bls12-381/Cargo.toml b/precompile/bls12-381/Cargo.toml index 404b860ed..250534943 100644 --- a/precompile/bls12-381/Cargo.toml +++ b/precompile/bls12-381/Cargo.toml @@ -12,7 +12,6 @@ ark-bls12-381 = { version = "0.4.0", default-features = false, features = ["curv ark-ec = { version = "0.4.1", default-features = false } ark-ff = { version = "0.4.1", default-features = false } ark-serialize = { version = "0.4.1", default-features = false, features = ["derive"] } -ark-std = { version = "0.4.0", default-features = false } sha2 = { version = "0.10.6", default-features = false } # frontier @@ -26,7 +25,8 @@ precompile-utils = { workspace = true } sp-std = { workspace = true } [dev-dependencies] -rand = { version = "0.8.5" } +rand = { version = "0.8.5" } +ark-std = { version = "0.4.0", default-features = false } [features] default = ["std"] @@ -37,7 +37,6 @@ std = [ # arkworks "ark-bls12-381/std", "ark-serialize/std", - "ark-std/std", "ark-ec/std", "ark-ff/std", diff --git a/precompile/bls12-381/src/bls/mod.rs b/precompile/bls12-381/src/bls/mod.rs index 1d78f8edd..92defaebf 100644 --- a/precompile/bls12-381/src/bls/mod.rs +++ b/precompile/bls12-381/src/bls/mod.rs @@ -20,6 +20,10 @@ // core use core::{borrow::Borrow, ops::Neg}; + +// substrate +use sp_std::prelude::Vec; + // crates.io use ark_bls12_381::{ g2::Config as G2Config, Bls12_381, G1Affine, G1Projective, G2Affine, G2Projective, @@ -35,7 +39,6 @@ use ark_ec::{ }; use ark_ff::{field_hashers::DefaultFieldHasher, Zero}; use ark_serialize::*; -use ark_std::vec::Vec; use sha2::Sha256; /// Domain Separation Tag for signatures on G2 diff --git a/precompile/bls12-381/src/lib.rs b/precompile/bls12-381/src/lib.rs index 7ea2bf80a..2095966e8 100644 --- a/precompile/bls12-381/src/lib.rs +++ b/precompile/bls12-381/src/lib.rs @@ -30,10 +30,10 @@ use precompile_utils::prelude::*; use sp_std::prelude::*; pub(crate) const BLS_ESTIMATED_COST: u64 = 100_000; -pub struct ARKBLS12381(PhantomData); +pub struct BLS12381(PhantomData); #[precompile_utils::precompile] -impl ARKBLS12381 { +impl BLS12381 { #[precompile::public("fast_aggregate_verify(bytes[],bytes,bytes)")] #[precompile::view] fn fast_aggregate_verify( diff --git a/shell.nix b/shell.nix index 004f77499..ca8fdf59d 100644 --- a/shell.nix +++ b/shell.nix @@ -5,7 +5,10 @@ let rev = "78e723925daf5c9e8d0a1837ec27059e61649cb6"; }); nixpkgs = import { overlays = [ mozillaOverlay ]; }; - rust-nightly = with nixpkgs; (nixpkgs.latest.rustChannels.nightly.rust); + rust-nightly = with nixpkgs; ((rustChannelOf { date = "2022-11-15"; channel = "nightly"; }).rust.override { + extensions = [ "rust-src" ]; + targets = [ "wasm32-unknown-unknown" ]; + }); in with nixpkgs; pkgs.mkShell { nativeBuildInputs = [ @@ -14,7 +17,7 @@ with nixpkgs; pkgs.mkShell { buildInputs = [ clang rocksdb - pkg-config + pkg-config openssl.dev ] ++ lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security From 535e8f88dfda28bf972aa566bbf6c775b6cd8dd1 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 13:01:30 +0800 Subject: [PATCH 11/16] Use rustup on nixos --- shell.nix | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 shell.nix diff --git a/shell.nix b/shell.nix deleted file mode 100644 index ca8fdf59d..000000000 --- a/shell.nix +++ /dev/null @@ -1,30 +0,0 @@ -let - mozillaOverlay = - import (builtins.fetchGit { - url = "https://github.com/mozilla/nixpkgs-mozilla.git"; - rev = "78e723925daf5c9e8d0a1837ec27059e61649cb6"; - }); - nixpkgs = import { overlays = [ mozillaOverlay ]; }; - rust-nightly = with nixpkgs; ((rustChannelOf { date = "2022-11-15"; channel = "nightly"; }).rust.override { - extensions = [ "rust-src" ]; - targets = [ "wasm32-unknown-unknown" ]; - }); -in -with nixpkgs; pkgs.mkShell { - nativeBuildInputs = [ - rust-nightly - ]; - buildInputs = [ - clang - rocksdb - pkg-config - openssl.dev - ] ++ lib.optionals stdenv.isDarwin [ - darwin.apple_sdk.frameworks.Security - ]; - - RUST_SRC_PATH = "${rust-nightly}/lib/rustlib/src/rust/src"; - LIBCLANG_PATH = "${llvmPackages.libclang.lib}/lib"; - PROTOC = "${protobuf}/bin/protoc"; - ROCKSDB_LIB_DIR = "${rocksdb}/lib"; -} From ef27080f60fd9fb7e1f5eabd2451043594bcbfe0 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 13:07:48 +0800 Subject: [PATCH 12/16] Fix --- runtime/darwinia/src/pallets/evm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 61acbb784..49ba628dd 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -49,7 +49,7 @@ where Self(Default::default()) } - pub fn used_addresses() -> [sp_core::H160; 16] { + pub fn used_addresses() -> [sp_core::H160; 15] { [ addr(1), addr(2), From 064f68a80f75905f06322ff5150a83dfb1f8aff7 Mon Sep 17 00:00:00 2001 From: bear Date: Thu, 9 Mar 2023 15:00:12 +0800 Subject: [PATCH 13/16] Disable in the crab and darwinia network --- runtime/crab/src/pallets/evm.rs | 8 ++++---- runtime/darwinia/src/pallets/evm.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/runtime/crab/src/pallets/evm.rs b/runtime/crab/src/pallets/evm.rs index 2e28a9ff1..950984e3b 100644 --- a/runtime/crab/src/pallets/evm.rs +++ b/runtime/crab/src/pallets/evm.rs @@ -49,7 +49,7 @@ where Self(Default::default()) } - pub fn used_addresses() -> [sp_core::H160; 15] { + pub fn used_addresses() -> [sp_core::H160; 14] { [ addr(1), addr(2), @@ -66,7 +66,7 @@ where addr(1026), addr(1536), addr(1537), - addr(2048), + // addr(2048), ] } } @@ -118,8 +118,8 @@ where a if a == addr(1537) => Some(>::execute(handle)), // [2048..) reserved for the experimental precompiles. - a if a == addr(2048) => - Some(>::execute(handle)), + // a if a == addr(2048) => + // Some(>::execute(handle)), _ => None, } } diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 49ba628dd..258dde402 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -49,7 +49,7 @@ where Self(Default::default()) } - pub fn used_addresses() -> [sp_core::H160; 15] { + pub fn used_addresses() -> [sp_core::H160; 14] { [ addr(1), addr(2), @@ -66,7 +66,7 @@ where addr(1026), addr(1536), addr(1537), - addr(2048), + // addr(2048), ] } } @@ -118,8 +118,8 @@ where a if a == addr(1537) => Some(>::execute(handle)), // [2048..) reserved for the experimental precompiles. - a if a == addr(2048) => - Some(>::execute(handle)), + // a if a == addr(2048) => + // Some(>::execute(handle)), _ => None, } } From 9a492be26a65e75d5f3e955e6253fc5ec943110c Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 9 Mar 2023 20:36:34 +0800 Subject: [PATCH 14/16] Add `shell.nix` to .gitigore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index a9a20e83c..4ddd4ea93 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ node_modules # Test data test-data + +# Nix +shell.nix From 4b53b7204423076153b08827eb40bf5f126a4957 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 10 Mar 2023 11:23:28 +0800 Subject: [PATCH 15/16] Format --- precompile/bls12-381/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/precompile/bls12-381/src/lib.rs b/precompile/bls12-381/src/lib.rs index 2095966e8..12697e1b8 100644 --- a/precompile/bls12-381/src/lib.rs +++ b/precompile/bls12-381/src/lib.rs @@ -19,11 +19,10 @@ #![cfg_attr(not(feature = "std"), no_std)] mod bls; +use bls::{hash_to_curve_g2, PublicKey, Signature}; // core use core::marker::PhantomData; -// bls -use bls::{hash_to_curve_g2, PublicKey, Signature}; // moonbeam use precompile_utils::prelude::*; // substrate From f4584e70e42a18b8115df7805ff6ee9bf94f07ae Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 10 Mar 2023 11:24:17 +0800 Subject: [PATCH 16/16] Format --- .gitignore | 5 ++--- precompile/bls12-381/Cargo.toml | 6 ++---- precompile/bls12-381/src/bls/mod.rs | 19 ++++++------------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 4ddd4ea93..d2d8f18f7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,11 +8,10 @@ # Package Manager ## cargo target +# nix +shell.nix ## npm node_modules # Test data test-data - -# Nix -shell.nix diff --git a/precompile/bls12-381/Cargo.toml b/precompile/bls12-381/Cargo.toml index 250534943..423b45c1a 100644 --- a/precompile/bls12-381/Cargo.toml +++ b/precompile/bls12-381/Cargo.toml @@ -26,19 +26,17 @@ sp-std = { workspace = true } [dev-dependencies] rand = { version = "0.8.5" } -ark-std = { version = "0.4.0", default-features = false } +ark-std = { version = "0.4.0" } [features] default = ["std"] std = [ # crates.io - "sha2/std", - - # arkworks "ark-bls12-381/std", "ark-serialize/std", "ark-ec/std", "ark-ff/std", + "sha2/std", # frontier "pallet-evm/std", diff --git a/precompile/bls12-381/src/bls/mod.rs b/precompile/bls12-381/src/bls/mod.rs index 92defaebf..2566f3007 100644 --- a/precompile/bls12-381/src/bls/mod.rs +++ b/precompile/bls12-381/src/bls/mod.rs @@ -20,10 +20,6 @@ // core use core::{borrow::Borrow, ops::Neg}; - -// substrate -use sp_std::prelude::Vec; - // crates.io use ark_bls12_381::{ g2::Config as G2Config, Bls12_381, G1Affine, G1Projective, G2Affine, G2Projective, @@ -40,25 +36,24 @@ use ark_ec::{ use ark_ff::{field_hashers::DefaultFieldHasher, Zero}; use ark_serialize::*; use sha2::Sha256; +// substrate +use sp_std::prelude::Vec; /// Domain Separation Tag for signatures on G2 pub const DST_G2: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; #[derive(Clone, Debug)] pub struct Signature(G2Projective); - impl From for Signature { fn from(sig: G2Projective) -> Signature { Signature(sig) } } - impl AsRef for Signature { fn as_ref(&self) -> &G2Projective { &self.0 } } - impl Signature { pub fn from_bytes(bytes: &[u8]) -> Result { let p = G2Affine::deserialize_compressed(bytes)?; @@ -73,13 +68,11 @@ impl Signature { #[derive(Clone, Debug, Eq, PartialEq, Hash, CanonicalSerialize, CanonicalDeserialize)] pub struct PublicKey(pub G1Projective); - impl From for PublicKey { fn from(pk: G1Projective) -> PublicKey { PublicKey(pk) } } - impl PublicKey { pub fn from_bytes(bytes: &[u8]) -> Result { let p = G1Affine::deserialize_compressed(bytes)?; @@ -110,14 +103,14 @@ pub fn hash_to_curve_g2(message: &[u8]) -> Result