diff --git a/Cargo.lock b/Cargo.lock index 662d93d3..426912e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -319,7 +319,7 @@ dependencies = [ "alloy-rlp", "alloy-serde", "alloy-sol-types", - "itertools 0.13.0", + "itertools 0.14.0", "serde", "serde_json", "serde_with", @@ -580,19 +580,31 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3df4dcc01ff89867cd86b0da835f23c3f02738353aaee7dde7495af71363b8d5" dependencies = [ - "ark-ec", + "ark-ec 0.5.0", "ark-ff 0.5.0", "ark-serialize 0.5.0", "ark-std 0.5.0", ] +[[package]] +name = "ark-bls12-381" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be2ede2c0c96fa37d5d3484e8a59fec566c4a52b8c84bf993eaa6c67d7225a4c" +dependencies = [ + "ark-ec 0.6.0", + "ark-ff 0.6.0", + "ark-serialize 0.6.0", + "ark-std 0.6.0", +] + [[package]] name = "ark-bn254" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d69eab57e8d2663efa5c63135b2af4f396d66424f88954c21104125ab6b3e6bc" dependencies = [ - "ark-ec", + "ark-ec 0.5.0", "ark-ff 0.5.0", "ark-std 0.5.0", ] @@ -605,7 +617,7 @@ checksum = "43d68f2d516162846c1238e755a7c4d131b892b70cc70c471a8e3ca3ed818fce" dependencies = [ "ahash", "ark-ff 0.5.0", - "ark-poly", + "ark-poly 0.5.0", "ark-serialize 0.5.0", "ark-std 0.5.0", "educe", @@ -618,6 +630,27 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ark-ec" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8352a2b2aedf6ba2cc38f7520fc51191d518dde96175c729af19f2d059f191c4" +dependencies = [ + "ahash", + "ark-ff 0.6.0", + "ark-poly 0.6.0", + "ark-serialize 0.6.0", + "ark-std 0.6.0", + "educe", + "fnv", + "hashbrown 0.17.1", + "itertools 0.14.0", + "num-bigint 0.4.6", + "num-integer", + "num-traits", + "zeroize", +] + [[package]] name = "ark-ff" version = "0.3.0" @@ -676,6 +709,23 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ark-ff" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7a806ac6c8307b929df4645776290a50ee2aac754ad09d8bdf73391309e43af" +dependencies = [ + "ark-ff-asm 0.6.0", + "ark-ff-macros 0.6.0", + "ark-serialize 0.6.0", + "ark-std 0.6.0", + "digest 0.10.7", + "educe", + "num-bigint 0.4.6", + "num-traits", + "zeroize", +] + [[package]] name = "ark-ff-asm" version = "0.3.0" @@ -706,6 +756,16 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "ark-ff-asm" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1479009684adc073dff49a1025d3a7065b317a9ead25aaaca38cdc70058ba8a2" +dependencies = [ + "quote", + "syn 2.0.117", +] + [[package]] name = "ark-ff-macros" version = "0.3.0" @@ -744,6 +804,19 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "ark-ff-macros" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a0691ed21ef00ef89c1e9bda832eba493dda3ec2f8d892fb25b705f73f06bb8" +dependencies = [ + "num-bigint 0.4.6", + "num-traits", + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "ark-poly" version = "0.5.0" @@ -759,13 +832,28 @@ dependencies = [ "hashbrown 0.15.5", ] +[[package]] +name = "ark-poly" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75f55af10b672002b8d953e230282c51206842e20e5791a94432219b4201de5c" +dependencies = [ + "ahash", + "ark-ff 0.6.0", + "ark-serialize 0.6.0", + "ark-std 0.6.0", + "educe", + "fnv", + "hashbrown 0.17.1", +] + [[package]] name = "ark-secp256k1" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8bd211c48debd3037b48873a7aa22c3aba034e83388aa4124795c9f220b88c7" dependencies = [ - "ark-ec", + "ark-ec 0.5.0", "ark-ff 0.5.0", "ark-std 0.5.0", ] @@ -776,7 +864,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cf8be5820de567729bfa73a410ddd07cec8ad102d9a4bf61fd6b2e60db264e8" dependencies = [ - "ark-ec", + "ark-ec 0.5.0", "ark-ff 0.5.0", "ark-std 0.5.0", ] @@ -808,13 +896,25 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f4d068aaf107ebcd7dfb52bc748f8030e0fc930ac8e360146ca54c1203088f7" dependencies = [ - "ark-serialize-derive", + "ark-serialize-derive 0.5.0", "ark-std 0.5.0", "arrayvec", "digest 0.10.7", "num-bigint 0.4.6", ] +[[package]] +name = "ark-serialize" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a74dd304fd536fb95d0a328e72be759209cc496a9da094c5bc56e5fea4f9e86b" +dependencies = [ + "ark-serialize-derive 0.6.0", + "ark-std 0.6.0", + "digest 0.10.7", + "num-bigint 0.4.6", +] + [[package]] name = "ark-serialize-derive" version = "0.5.0" @@ -826,6 +926,17 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "ark-serialize-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f153690697a2b91e5e1251ff98411ee5371500a111a0fd317a70e588eb300f9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "ark-std" version = "0.3.0" @@ -856,6 +967,16 @@ dependencies = [ "rand 0.8.6", ] +[[package]] +name = "ark-std" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "367c9c827ed431bff6868b7aa926e05b16eb46603cc8b6e768e4a5553fa1d155" +dependencies = [ + "num-traits", + "rand 0.8.6", +] + [[package]] name = "arrayref" version = "0.3.9" @@ -1726,6 +1847,15 @@ dependencies = [ "generic-array 0.14.9", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "bls12_381" version = "0.7.1" @@ -1768,18 +1898,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "bls12_381" -version = "0.8.0" -source = "git+https://github.com/zkcrypto/bls12_381?rev=6bb96951d5c2035caf4989b6e4a018435379590f#6bb96951d5c2035caf4989b6e4a018435379590f" -dependencies = [ - "digest 0.10.7", - "ff 0.13.1", - "group 0.13.0", - "rand_core 0.6.4", - "subtle", -] - [[package]] name = "blst" version = "0.3.16" @@ -1824,7 +1942,7 @@ version = "3.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "519bd3116aeeb42d5372c29d982d16d0170d3d4a5ed85fc7dd91642ffff3c67c" dependencies = [ - "darling 0.20.11", + "darling 0.23.0", "ident_case", "prettyplease", "proc-macro2", @@ -2182,7 +2300,7 @@ version = "3.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.61.2", ] [[package]] @@ -2483,6 +2601,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + [[package]] name = "csv" version = "1.4.0" @@ -2957,12 +3084,22 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", + "block-buffer 0.10.4", "const-oid", - "crypto-common", + "crypto-common 0.1.6", "subtle", ] +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "crypto-common 0.2.2", +] + [[package]] name = "dirs" version = "5.0.1" @@ -3002,7 +3139,7 @@ dependencies = [ "libc", "option-ext", "redox_users 0.5.2", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3024,7 +3161,7 @@ checksum = "9f5b01c17f85ee988d832c40e549a64bd89ab2c9f8d8a613bdf5122ae507e294" dependencies = [ "cfg-if", "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -3378,8 +3515,10 @@ version = "0.14.0" name = "ere-platform-openvm" version = "0.14.0" dependencies = [ + "ark-bls12-381 0.6.0", + "ark-ec 0.6.0", + "ark-serialize 0.6.0", "aurora-engine-modexp", - "bls12_381 0.8.0 (git+https://github.com/zkcrypto/bls12_381?rev=6bb96951d5c2035caf4989b6e4a018435379590f)", "ere-platform-core", "k256 0.13.4 (git+https://github.com/openvm-org/openvm.git?tag=v2.1.0-preview)", "openvm 2.0.0 (git+https://github.com/openvm-org/openvm.git?tag=v2.1.0-preview)", @@ -3391,7 +3530,7 @@ dependencies = [ "openvm-pairing", "openvm-sha2", "p256 0.13.2 (git+https://github.com/openvm-org/openvm.git?tag=v2.1.0-preview)", - "ripemd", + "ripemd 0.2.0", "zkvm-interface 0.1.0", ] @@ -3680,7 +3819,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4327,6 +4466,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "allocator-api2", +] + [[package]] name = "heck" version = "0.5.0" @@ -4457,6 +4605,15 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "hybrid-array" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3944cf8cf766b40e2a1a333ee5e9b563f854d5fa49d6a8ca2764e97c6eddb214" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.8.1" @@ -4546,7 +4703,7 @@ dependencies = [ "js-sys", "log", "wasm-bindgen", - "windows-core 0.61.2", + "windows-core 0.62.2", ] [[package]] @@ -5060,7 +5217,7 @@ dependencies = [ "kzg-rs", "num-bigint-dig", "p256 0.13.2 (git+https://github.com/sp1-patches/elliptic-curves?tag=patch-p256-13.2-sp1-6.2.0)", - "ripemd", + "ripemd 0.1.3", "sha2 0.10.9 (git+https://github.com/sp1-patches/RustCrypto-hashes?tag=patch-sha2-0.10.9-sp1-6.2.0)", "sp1-zkvm", "substrate-bn", @@ -5492,7 +5649,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -8184,7 +8341,7 @@ name = "precomp-arith-eq-384" version = "1.0.0-alpha" source = "git+https://github.com/0xPolygonHermez/zisk.git?tag=v1.0.0-alpha#4b9f758fabc4955cac20af837019ccc31b803a46" dependencies = [ - "ark-bls12-381", + "ark-bls12-381 0.5.0", "ark-ff 0.5.0", "fields", "lazy_static", @@ -8330,7 +8487,7 @@ name = "precompiles-helpers" version = "1.0.0-alpha" source = "git+https://github.com/0xPolygonHermez/zisk.git?tag=v1.0.0-alpha#4b9f758fabc4955cac20af837019ccc31b803a46" dependencies = [ - "ark-bls12-381", + "ark-bls12-381 0.5.0", "ark-bn254", "ark-ff 0.5.0", "ark-secp256k1", @@ -8613,7 +8770,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be769465445e8c1474e9c5dac2018218498557af32d9ed057325ec9a41ae81bf" dependencies = [ "heck", - "itertools 0.12.1", + "itertools 0.14.0", "log", "multimap", "once_cell", @@ -8633,7 +8790,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "343d3bd7056eda839b03204e68deff7d1b13aba7af2b2fd16890697274262ee7" dependencies = [ "heck", - "itertools 0.12.1", + "itertools 0.14.0", "log", "multimap", "petgraph 0.8.3", @@ -8654,7 +8811,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.12.1", + "itertools 0.14.0", "proc-macro2", "quote", "syn 2.0.117", @@ -8667,7 +8824,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27c6023962132f4b30eb4c172c91ce92d933da334c59c23cddee82358ddafb0b" dependencies = [ "anyhow", - "itertools 0.12.1", + "itertools 0.14.0", "proc-macro2", "quote", "syn 2.0.117", @@ -9179,6 +9336,15 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "ripemd" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dd4211456b4172d7e44261920c25acf07367c4f04bb5f5d54fc21b090d9b159" +dependencies = [ + "digest 0.11.3", +] + [[package]] name = "riscv" version = "1.0.0-alpha" @@ -9352,7 +9518,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -9420,7 +9586,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -11731,7 +11897,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -12927,7 +13093,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.61.2", ] [[package]] @@ -13854,7 +14020,7 @@ source = "git+https://github.com/0xPolygonHermez/zisk.git?tag=v1.0.0-alpha#4b9f7 dependencies = [ "anyhow", "ark-bn254", - "ark-ec", + "ark-ec 0.5.0", "ark-ff 0.5.0", "ark-serialize 0.5.0", "aurora-engine-modexp", @@ -13878,7 +14044,7 @@ dependencies = [ "paste", "precompiles-helpers", "rand 0.8.6", - "ripemd", + "ripemd 0.1.3", "secp256k1 0.31.1", "serde", "sha2 0.10.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -13909,7 +14075,7 @@ dependencies = [ "paste", "precompiles-helpers", "rand 0.8.6", - "ripemd", + "ripemd 0.1.3", "serde", "sha2 0.10.9 (registry+https://github.com/rust-lang/crates.io-index)", "tiny-keccak 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 6c12c9e3..6c09ab98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,12 +55,14 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(openvm_intrinsics)', 'cfg( [workspace.dependencies] anyhow = "1.0.98" +ark-bls12-381 = { version = "0.6", default-features = false, features = ["curve"] } +ark-ec = { version = "0.6", default-features = false } +ark-serialize = { version = "0.6", default-features = false } aurora-engine-modexp = { version = "1.2.0", default-features = false } auto_impl = "1.3.0" bincode = { version = "2.0.1", default-features = false } bitcode = { version = "0.6", default-features = false } blake3 = "1.8" -bls12_381 = { git = "https://github.com/zkcrypto/bls12_381", rev = "6bb96951d5c2035caf4989b6e4a018435379590f", default-features = false } bytemuck = "1.25.0" cargo_metadata = "0.19.0" cbindgen = "0.29" @@ -91,7 +93,7 @@ prost-build = "0.14" prost-types = "0.14" rand = "0.9.2" reqwest = { version = "0.12", default-features = false } -ripemd = { version = "0.1.3", default-features = false } +ripemd = { version = "0.2", default-features = false } serde = { version = "1.0.219", default-features = false } serde-big-array = "0.5.1" serde_bytes = { version = "0.11.19", default-features = false } diff --git a/crates/platform/openvm/Cargo.toml b/crates/platform/openvm/Cargo.toml index 364ab9c8..2d4993d6 100644 --- a/crates/platform/openvm/Cargo.toml +++ b/crates/platform/openvm/Cargo.toml @@ -19,8 +19,10 @@ openvm-pairing = { workspace = true, optional = true } openvm-sha2 = { workspace = true, optional = true } # zkvm-accelerator dependencies +ark-bls12-381 = { workspace = true, optional = true } +ark-ec = { workspace = true, optional = true } +ark-serialize = { workspace = true, optional = true } aurora-engine-modexp = { workspace = true, optional = true } -bls12_381 = { workspace = true, features = ["experimental"], optional = true } ripemd = { workspace = true, optional = true } zkvm-interface = { workspace = true, optional = true } @@ -42,8 +44,10 @@ zkvm-accelerator = [ "dep:openvm-p256", "dep:openvm-pairing", "dep:openvm-sha2", + "dep:ark-bls12-381", + "dep:ark-ec", + "dep:ark-serialize", "dep:aurora-engine-modexp", - "dep:bls12_381", "dep:ripemd", "dep:zkvm-interface", ] diff --git a/crates/platform/openvm/src/zkvm_accelerator.rs b/crates/platform/openvm/src/zkvm_accelerator.rs index b46df6f8..d7e0b3e4 100644 --- a/crates/platform/openvm/src/zkvm_accelerator.rs +++ b/crates/platform/openvm/src/zkvm_accelerator.rs @@ -29,7 +29,12 @@ use alloc::{vec, vec::Vec}; use core::slice; -use bls12_381::hash_to_curve::MapToCurve; +use ark_bls12_381::{Fq, Fq2, G1Affine, G2Affine}; +use ark_ec::{ + AffineRepr, + hashing::{curve_maps::wb::WBMap, map_to_curve_hasher::MapToCurve}, +}; +use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use openvm_curve_utils::SubgroupCheck; use openvm_ecc_guest::{ AffinePoint, Group, @@ -49,7 +54,7 @@ use openvm_pairing::{ bn254::{self as bn, Bn254}, }; use openvm_sha2::{Digest, Sha256}; -use ripemd::Ripemd160; +use ripemd::{Digest as _, Ripemd160}; use zkvm_interface::{ zkvm_blake2f_message, zkvm_blake2f_offset, zkvm_blake2f_state, zkvm_bls12_381_fp, zkvm_bls12_381_fp2, zkvm_bls12_381_g1_msm_pair, zkvm_bls12_381_g1_point, @@ -79,22 +84,6 @@ const BLS_G1_LEN: usize = 96; const BLS_G2_LEN: usize = 192; const BLS_SCALAR_LEN: usize = 32; -/// EIP-2537 image of `map_fp2_to_g2(0)`, in the C interface G2 encoding. -const MAP_FP2_TO_G2_ZERO: [u8; BLS_G2_LEN] = [ - 0x01, 0x83, 0x20, 0x89, 0x6e, 0xc9, 0xee, 0xf9, 0xd5, 0xe6, 0x19, 0x84, 0x8d, 0xc2, 0x9c, 0xe2, - 0x66, 0xf4, 0x13, 0xd0, 0x2d, 0xd3, 0x1d, 0x9b, 0x9d, 0x44, 0xec, 0x0c, 0x79, 0xcd, 0x61, 0xf1, - 0x8b, 0x07, 0x5d, 0xdb, 0xa6, 0xd7, 0xbd, 0x20, 0xb7, 0xff, 0x27, 0xa4, 0xb3, 0x24, 0xbf, 0xce, - 0x0a, 0x67, 0xd1, 0x21, 0x18, 0xb5, 0xa3, 0x5b, 0xb0, 0x2d, 0x2e, 0x86, 0xb3, 0xeb, 0xfa, 0x7e, - 0x23, 0x41, 0x0d, 0xb9, 0x3d, 0xe3, 0x9f, 0xb0, 0x6d, 0x70, 0x25, 0xfa, 0x95, 0xe9, 0x6f, 0xfa, - 0x42, 0x8a, 0x7a, 0x27, 0xc3, 0xae, 0x4d, 0xd4, 0xb4, 0x0b, 0xd2, 0x51, 0xac, 0x65, 0x88, 0x92, - 0x02, 0x60, 0xe0, 0x36, 0x44, 0xd1, 0xa2, 0xc3, 0x21, 0x25, 0x6b, 0x32, 0x46, 0xba, 0xd2, 0xb8, - 0x95, 0xca, 0xd1, 0x38, 0x90, 0xcb, 0xe6, 0xf8, 0x5d, 0xf5, 0x51, 0x06, 0xa0, 0xd3, 0x34, 0x60, - 0x4f, 0xb1, 0x43, 0xc7, 0xa0, 0x42, 0xd8, 0x78, 0x00, 0x62, 0x71, 0x86, 0x5b, 0xc3, 0x59, 0x41, - 0x04, 0xc6, 0x97, 0x77, 0xa4, 0x3f, 0x0b, 0xda, 0x07, 0x67, 0x9d, 0x58, 0x05, 0xe6, 0x3f, 0x18, - 0xcf, 0x4e, 0x0e, 0x7c, 0x61, 0x12, 0xac, 0x7f, 0x70, 0x26, 0x6d, 0x19, 0x9b, 0x4f, 0x76, 0xae, - 0x27, 0xc6, 0x26, 0x9a, 0x3c, 0xee, 0xbd, 0xae, 0x30, 0x80, 0x6e, 0x9a, 0x76, 0xaa, 0xdf, 0x5c, -]; - #[unsafe(no_mangle)] unsafe extern "C" fn zkvm_keccak256( data: *const u8, @@ -436,15 +425,11 @@ unsafe extern "C" fn zkvm_bls12_map_fp_to_g1( field_element: *const zkvm_bls12_381_fp, result: *mut zkvm_bls12_381_g1_point, ) -> zkvm_status { - type Fp = ::Field; - - let Some(field_element) = Fp::from_bytes(unsafe { &(*field_element).data }).into_option() - else { + let Some(field_element) = read_bls12_fp(unsafe { &(*field_element).data }) else { return ZKVM_EFAIL; }; - let point = bls12_381::G1Projective::map_to_curve(&field_element).clear_h(); - unsafe { (*result).data = serialize_bls12_g1(&bls12_381::G1Affine::from(point)) }; + unsafe { (*result).data = serialize_bls12_g1(&map_bls12_fp_to_g1(field_element)) }; ZKVM_EOK } @@ -453,28 +438,15 @@ unsafe extern "C" fn zkvm_bls12_map_fp2_to_g2( field_element: *const zkvm_bls12_381_fp2, result: *mut zkvm_bls12_381_g2_point, ) -> zkvm_status { - type Fp = ::Field; - type Fp2 = ::Field; - let field_element = unsafe { &(*field_element).data }; - let Some(c0) = Fp::from_bytes(field_element[..BLS_FP_LEN].try_into().unwrap()).into_option() - else { + let Some(c0) = read_bls12_fp(field_element[..BLS_FP_LEN].try_into().unwrap()) else { return ZKVM_EFAIL; }; - let Some(c1) = Fp::from_bytes(field_element[BLS_FP_LEN..].try_into().unwrap()).into_option() - else { + let Some(c1) = read_bls12_fp(field_element[BLS_FP_LEN..].try_into().unwrap()) else { return ZKVM_EFAIL; }; - // The `bls12_381` SWU map sends `u = 0`, and only `u = 0`, to infinity, so the finite image - // EIP-2537 expects is hardcoded. - if bool::from(c0.is_zero()) && bool::from(c1.is_zero()) { - unsafe { (*result).data = MAP_FP2_TO_G2_ZERO }; - return ZKVM_EOK; - } - - let point = bls12_381::G2Projective::map_to_curve(&Fp2 { c0, c1 }).clear_h(); - unsafe { (*result).data = serialize_bls12_g2(&bls12_381::G2Affine::from(point)) }; + unsafe { (*result).data = serialize_bls12_g2(&map_bls12_fp2_to_g2(Fq2::new(c0, c1))) }; ZKVM_EOK } @@ -695,28 +667,59 @@ fn encode_bls_g2_point(point: &bls::G2Affine) -> [u8; BLS_G2_LEN] { output } -/// Serializes a `bls12_381` G1 point as `x || y`, each 48 big-endian bytes. -fn serialize_bls12_g1(point: &bls12_381::G1Affine) -> [u8; BLS_G1_LEN] { - if bool::from(point.is_identity()) { - return [0u8; BLS_G1_LEN]; - } - point.to_uncompressed() +/// Reads a big-endian `Fp`, rejecting an encoding that is not the canonical representative. +fn read_bls12_fp(input: &[u8; BLS_FP_LEN]) -> Option { + let mut input_le = *input; + input_le.reverse(); + Fq::deserialize_uncompressed(&input_le[..]).ok() } -/// Serializes a `bls12_381` G2 point as `x_c0 || x_c1 || y_c0 || y_c1`, each 48 big-endian bytes. -/// The crate emits the two Fp2 limbs in the opposite order, so each pair is swapped back to the -/// EIP-2537 convention. -fn serialize_bls12_g2(point: &bls12_381::G2Affine) -> [u8; BLS_G2_LEN] { - if bool::from(point.is_identity()) { - return [0u8; BLS_G2_LEN]; - } +/// Serializes an `Fp` as 48 big-endian bytes. +fn encode_bls12_fp(fp: &Fq) -> [u8; BLS_FP_LEN] { + let mut output = [0u8; BLS_FP_LEN]; + fp.serialize_uncompressed(&mut output[..]) + .expect("a field element always serializes"); + output.reverse(); + output +} + +/// Maps an `Fp` onto G1 following EIP-2537, which is the simplified SWU map composed with the +/// isogeny back to the curve, then cofactor clearing. +fn map_bls12_fp_to_g1(fp: Fq) -> G1Affine { + WBMap::map_to_curve(fp) + .expect("map_to_curve is infallible") + .clear_cofactor() +} + +/// Maps an `Fp2` onto G2 following EIP-2537. +fn map_bls12_fp2_to_g2(fp2: Fq2) -> G2Affine { + WBMap::map_to_curve(fp2) + .expect("map_to_curve is infallible") + .clear_cofactor() +} - let raw = point.to_uncompressed(); +/// Serializes a G1 point as `x || y`, each 48 big-endian bytes, with infinity as all zeros. +fn serialize_bls12_g1(point: &G1Affine) -> [u8; BLS_G1_LEN] { + let mut output = [0u8; BLS_G1_LEN]; + let Some((x, y)) = point.xy() else { + return output; + }; + output[..BLS_FP_LEN].copy_from_slice(&encode_bls12_fp(&x)); + output[BLS_FP_LEN..].copy_from_slice(&encode_bls12_fp(&y)); + output +} + +/// Serializes a G2 point as `x_c0 || x_c1 || y_c0 || y_c1`, each 48 big-endian bytes, with +/// infinity as all zeros. +fn serialize_bls12_g2(point: &G2Affine) -> [u8; BLS_G2_LEN] { let mut output = [0u8; BLS_G2_LEN]; - output[..BLS_FP_LEN].copy_from_slice(&raw[BLS_FP_LEN..2 * BLS_FP_LEN]); - output[BLS_FP_LEN..2 * BLS_FP_LEN].copy_from_slice(&raw[..BLS_FP_LEN]); - output[2 * BLS_FP_LEN..3 * BLS_FP_LEN].copy_from_slice(&raw[3 * BLS_FP_LEN..]); - output[3 * BLS_FP_LEN..].copy_from_slice(&raw[2 * BLS_FP_LEN..3 * BLS_FP_LEN]); + let Some((x, y)) = point.xy() else { + return output; + }; + output[..BLS_FP_LEN].copy_from_slice(&encode_bls12_fp(&x.c0)); + output[BLS_FP_LEN..2 * BLS_FP_LEN].copy_from_slice(&encode_bls12_fp(&x.c1)); + output[2 * BLS_FP_LEN..3 * BLS_FP_LEN].copy_from_slice(&encode_bls12_fp(&y.c0)); + output[3 * BLS_FP_LEN..].copy_from_slice(&encode_bls12_fp(&y.c1)); output }