From 0995a0aaec8d661ddbc704501827859f74342002 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 2 Oct 2023 09:31:18 +0200 Subject: [PATCH] port BLS operators to use chia-bls (and BLST) --- Cargo.lock | 554 ++++++++++++++++++++++++-------- Cargo.toml | 5 +- src/allocator.rs | 76 ++--- src/bls_ops.rs | 115 +++---- src/more_ops.rs | 13 +- src/op_utils.rs | 13 - tools/Cargo.toml | 3 +- tools/src/bin/verify-zksnark.rs | 77 ++--- 8 files changed, 522 insertions(+), 334 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3897e752..574a1d74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "aho-corasick" -version = "1.0.4" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] @@ -17,6 +17,12 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "arbitrary" version = "1.3.0" @@ -49,9 +55,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" [[package]] name = "bitvec" @@ -97,11 +103,23 @@ dependencies = [ "subtle", ] +[[package]] +name = "blst" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c94087b935a822949d3291a9989ad2b2051ea141eda0fd4e478a75f6aa3e604b" +dependencies = [ + "cc", + "glob", + "threadpool", + "zeroize", +] + [[package]] name = "bumpalo" -version = "3.13.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "cast" @@ -125,6 +143,50 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chia-bls" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a0fec2be9bdf1cac41bf1972faf09d354f7738beb419574241cee1c00e97559" +dependencies = [ + "anyhow", + "arbitrary", + "blst", + "chia-traits", + "clvm-traits", + "clvmr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hex", + "hkdf", + "sha2 0.9.9", + "thiserror", + "tiny-bip39", +] + +[[package]] +name = "chia-traits" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54ac593d0fe509f7f5154e31c41c175aa17e91dcbfe368815eb61111ad59ddb4" +dependencies = [ + "chia_streamable_macro", + "clvmr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hex", + "sha2 0.9.9", + "thiserror", +] + +[[package]] +name = "chia_streamable_macro" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dc973091d99d0e71cff30bf8e44f4d2f773fbf779de603ca34cd23ed3e0963a" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "ciborium" version = "0.2.1" @@ -189,14 +251,24 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "clvm-derive" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea20158301302a7a7d73bc1021c23d74e24e9a5e0f907e4d34b22ea0a5f11a31" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "clvm-rs-test-tools" version = "0.1.0" dependencies = [ - "bls12_381", + "chia-bls", "clap", - "clvmr", - "group", + "clvmr 0.3.0", "hex", "hex-literal", "linreg", @@ -207,11 +279,23 @@ dependencies = [ "sha1", ] +[[package]] +name = "clvm-traits" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bad113e3534ca1e2f79d353a46d23de94887036ef627ae67114f51ba5a3964e7" +dependencies = [ + "clvm-derive", + "clvmr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-bigint", + "thiserror", +] + [[package]] name = "clvm_rs" version = "0.3.0" dependencies = [ - "clvmr", + "clvmr 0.3.0", "pyo3", ] @@ -219,7 +303,7 @@ dependencies = [ name = "clvm_rs-fuzz" version = "1.0.0" dependencies = [ - "clvmr", + "clvmr 0.3.0", "libfuzzer-sys", ] @@ -227,7 +311,7 @@ dependencies = [ name = "clvm_wasm" version = "0.3.0" dependencies = [ - "clvmr", + "clvmr 0.3.0", "js-sys", "wasm-bindgen", "wasm-bindgen-test", @@ -237,10 +321,9 @@ dependencies = [ name = "clvmr" version = "0.3.0" dependencies = [ - "bls12_381", + "chia-bls", "criterion", "getrandom", - "group", "hex", "k256", "lazy_static", @@ -253,6 +336,25 @@ dependencies = [ "sha2 0.9.9", ] +[[package]] +name = "clvmr" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cd344b6dc76235f446025fe9ebe54aa6131e2e59acb49e16be48a3bb3492491" +dependencies = [ + "bls12_381", + "getrandom", + "group", + "hex", + "k256", + "lazy_static", + "num-bigint", + "num-integer", + "num-traits", + "p256", + "sha2 0.9.9", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -271,9 +373,9 @@ checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" [[package]] name = "cpufeatures" -version = "0.2.9" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1" +checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" dependencies = [ "libc", ] @@ -314,16 +416,6 @@ dependencies = [ "itertools", ] -[[package]] -name = "crossbeam-channel" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -359,9 +451,9 @@ dependencies = [ [[package]] name = "crypto-bigint" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4c2f4e1afd912bc40bfd6fed5d9dc1f288e0ba01bfcc835cc5bc3eb13efe15" +checksum = "740fe28e594155f10cfc383984cbefd529d7396050557148f79cb0f621204124" dependencies = [ "generic-array", "rand_core", @@ -379,6 +471,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-mac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "der" version = "0.7.8" @@ -444,9 +546,9 @@ checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "elliptic-curve" -version = "0.13.5" +version = "0.13.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "968405c8fdc9b3bf4df0a6638858cc0b52462836ab6b1c87377785dd09cf1c0b" +checksum = "d97ca172ae9dc9f9b779a6e3a65d308f2af74e5b8c921299075bdb4a0370e914" dependencies = [ "base16ct", "crypto-bigint", @@ -463,24 +565,19 @@ dependencies = [ ] [[package]] -name = "errno" -version = "0.3.2" +name = "equivalent" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", -] +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] -name = "errno-dragonfly" -version = "0.1.2" +name = "errno" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" dependencies = [ - "cc", "libc", + "windows-sys", ] [[package]] @@ -517,9 +614,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" dependencies = [ "futures-channel", "futures-core", @@ -532,9 +629,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" dependencies = [ "futures-core", "futures-sink", @@ -542,15 +639,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" [[package]] name = "futures-executor" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" dependencies = [ "futures-core", "futures-task", @@ -559,32 +656,32 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" [[package]] name = "futures-macro" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.38", ] [[package]] name = "futures-sink" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" +checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" [[package]] name = "futures-task" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" [[package]] name = "futures-timer" @@ -594,9 +691,9 @@ checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" [[package]] name = "futures-util" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" dependencies = [ "futures-channel", "futures-core", @@ -634,6 +731,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "group" version = "0.13.0" @@ -651,6 +754,12 @@ version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" +[[package]] +name = "hashbrown" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" + [[package]] name = "heck" version = "0.4.1" @@ -659,9 +768,9 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" [[package]] name = "hex" @@ -675,6 +784,26 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" +[[package]] +name = "hkdf" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01706d578d5c281058480e673ae4086a9f4710d8df1ad80a5b03e39ece5f886b" +dependencies = [ + "digest 0.9.0", + "hmac 0.11.0", +] + +[[package]] +name = "hmac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" +dependencies = [ + "crypto-mac", + "digest 0.9.0", +] + [[package]] name = "hmac" version = "0.12.1" @@ -684,6 +813,16 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "indexmap" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "indoc" version = "1.0.9" @@ -718,9 +857,9 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "jobserver" -version = "0.1.26" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2" +checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d" dependencies = [ "libc", ] @@ -744,7 +883,7 @@ dependencies = [ "ecdsa", "elliptic-curve", "once_cell", - "sha2 0.10.7", + "sha2 0.10.8", "signature", ] @@ -756,9 +895,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.147" +version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" [[package]] name = "libfuzzer-sys" @@ -783,15 +922,15 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.5" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" [[package]] name = "lock_api" -version = "0.4.10" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" dependencies = [ "autocfg", "scopeguard", @@ -805,9 +944,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.5.0" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "memoffset" @@ -887,11 +1026,11 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "openssl" -version = "0.10.56" +version = "0.10.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "729b745ad4a5575dd06a3e1af1414bd330ee561c01b3899eb584baeaa8def17e" +checksum = "bac25ee399abb46215765b1cb35bc0212377e58a061560d8b29b024fd0430e7c" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.4.1", "cfg-if", "foreign-types", "libc", @@ -908,23 +1047,23 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.38", ] [[package]] name = "openssl-src" -version = "111.27.0+1.1.1v" +version = "300.1.6+3.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06e8f197c82d7511c5b014030c9b1efeda40d7d5f99d23b4ceed3524a5e63f02" +checksum = "439fac53e092cd7442a3660c85dde4643ab3b5bd39040912388dcdabf6b88085" dependencies = [ "cc", ] [[package]] name = "openssl-sys" -version = "0.9.91" +version = "0.9.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "866b5f16f90776b9bb8dc1e1802ac6f0513de3a7a7465867bfbc563dc737faac" +checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d" dependencies = [ "cc", "libc", @@ -935,9 +1074,9 @@ dependencies = [ [[package]] name = "os_str_bytes" -version = "6.5.1" +version = "6.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" [[package]] name = "p256" @@ -948,7 +1087,7 @@ dependencies = [ "ecdsa", "elliptic-curve", "primeorder", - "sha2 0.10.7", + "sha2 0.10.8", ] [[package]] @@ -972,9 +1111,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.8" +version = "0.9.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" dependencies = [ "cfg-if", "libc", @@ -983,6 +1122,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "pbkdf2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" +dependencies = [ + "digest 0.10.7", +] + [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -994,9 +1142,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12cc1b0bf1727a77a54b6654e7b5f1af8604923edc8b81885f8ec92f9e3f0a05" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -1063,6 +1211,16 @@ dependencies = [ "elliptic-curve", ] +[[package]] +name = "proc-macro-crate" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +dependencies = [ + "once_cell", + "toml_edit", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1089,9 +1247,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] @@ -1203,9 +1361,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" dependencies = [ "either", "rayon-core", @@ -1213,30 +1371,28 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" dependencies = [ - "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "num_cpus", ] [[package]] name = "redox_syscall" -version = "0.3.5" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" dependencies = [ "bitflags 1.3.2", ] [[package]] name = "regex" -version = "1.9.3" +version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", @@ -1246,9 +1402,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.6" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", @@ -1257,9 +1413,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rfc6979" @@ -1267,7 +1423,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" dependencies = [ - "hmac", + "hmac 0.12.1", "subtle", ] @@ -1297,6 +1453,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.4.0" @@ -1308,11 +1470,11 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.8" +version = "0.38.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19ed4fa021d81c8392ce04db050a3da9a60299050b7ae1cf482d862b54a7218f" +checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" dependencies = [ - "bitflags 2.4.0", + "bitflags 2.4.1", "errno", "libc", "linux-raw-sys", @@ -1362,35 +1524,35 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.18" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "serde" -version = "1.0.185" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be9b6f69f1dfd54c3b568ffa45c310d6973a5e5148fd40cf515acaf38cf5bc31" +checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.185" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc59dfdcbad1437773485e0367fea4b090a2e0a16d9ffc46af47764536a298ec" +checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.38", ] [[package]] name = "serde_json" -version = "1.0.105" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ "itoa", "ryu", @@ -1423,9 +1585,9 @@ dependencies = [ [[package]] name = "sha2" -version = "0.10.7" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ "cfg-if", "cpufeatures", @@ -1453,9 +1615,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" [[package]] name = "spki" @@ -1475,9 +1637,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "subtle" -version = "2.5.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" @@ -1492,9 +1654,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.29" +version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", @@ -1509,19 +1671,67 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "target-lexicon" -version = "0.12.11" +version = "0.12.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" +checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" [[package]] name = "termcolor" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" dependencies = [ "winapi-util", ] +[[package]] +name = "thiserror" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + +[[package]] +name = "tiny-bip39" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62cc94d358b5a1e84a5cb9109f559aa3c4d634d2b1b4de3d0fa4adc7c78e2861" +dependencies = [ + "anyhow", + "hmac 0.12.1", + "once_cell", + "pbkdf2", + "rand", + "rustc-hash", + "sha2 0.10.8", + "thiserror", + "unicode-normalization", + "wasm-bindgen", + "zeroize", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -1532,17 +1742,58 @@ dependencies = [ "serde_json", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + [[package]] name = "typenum" -version = "1.16.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "unicode-ident" -version = "1.0.11" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-normalization" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] [[package]] name = "unindent" @@ -1564,9 +1815,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "walkdir" -version = "2.3.3" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", "winapi-util", @@ -1599,7 +1850,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.38", "wasm-bindgen-shared", ] @@ -1633,7 +1884,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.38", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1696,9 +1947,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] @@ -1775,6 +2026,15 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "winnow" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "176b6138793677221d420fd2f0aeeced263f197688b36484660da767bca2fa32" +dependencies = [ + "memchr", +] + [[package]] name = "wyz" version = "0.5.1" @@ -1789,3 +2049,17 @@ name = "zeroize" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] diff --git a/Cargo.toml b/Cargo.toml index b4fbb7fa..041c482c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,10 +34,7 @@ lazy_static = "=1.4.0" num-bigint = "=0.4.3" num-traits = "=0.2.15" num-integer = "=0.1.45" -# the experimental feature enables hashing to curves -bls12_381 = { version = "=0.8.0", features = ["experimental"] } -# the newer sha2 crate doesn't implement the digest traits required by HKDF -group = "=0.13.0" +chia-bls = ">=0.2.13" sha2 = "=0.9.9" openssl = { version = "=0.10.55", features = ["vendored"], optional = true } getrandom = { version = "=0.2.9", features = ["js" ]} diff --git a/src/allocator.rs b/src/allocator.rs index 2e1bc930..eb9fd03d 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -1,7 +1,7 @@ use crate::err_utils::err; use crate::number::{node_from_number, number_from_u8, Number}; use crate::reduction::EvalErr; -use bls12_381::{G1Affine, G1Projective, G2Affine, G2Projective}; +use chia_bls::{G1Element, G2Element}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct NodePtr(pub i32); @@ -148,14 +148,12 @@ impl Allocator { node_from_number(self, &v) } - pub fn new_g1(&mut self, g1: G1Projective) -> Result { - let g1: G1Affine = g1.into(); - self.new_atom(&g1.to_compressed()) + pub fn new_g1(&mut self, g1: G1Element) -> Result { + self.new_atom(&g1.to_bytes()) } - pub fn new_g2(&mut self, g2: G2Projective) -> Result { - let g2: G2Affine = g2.into(); - self.new_atom(&g2.to_compressed()) + pub fn new_g2(&mut self, g2: G2Element) -> Result { + self.new_atom(&g2.to_bytes()) } pub fn new_pair(&mut self, first: NodePtr, rest: NodePtr) -> Result { @@ -251,42 +249,32 @@ impl Allocator { number_from_u8(self.atom(node)) } - pub fn g1(&self, node: NodePtr) -> Result { + pub fn g1(&self, node: NodePtr) -> Result { let blob = match self.sexp(node) { SExp::Atom => self.atom(node), _ => { return err(node, "pair found, expected G1 point"); } }; - if blob.len() != 48 { - return err(node, "atom is not G1 size, 48 bytes"); - } - - let affine: Option = - G1Affine::from_compressed(blob.try_into().expect("G1 slice is not 48 bytes")).into(); - match affine { - Some(point) => Ok(G1Projective::from(point)), - None => err(node, "atom is not a G1 point"), - } + let array: [u8; 48] = blob + .try_into() + .map_err(|_| EvalErr(node, "atom is not G1 size, 48 bytes".to_string()))?; + G1Element::from_bytes(&array) + .map_err(|_| EvalErr(node, "atom is not a G1 point".to_string())) } - pub fn g2(&self, node: NodePtr) -> Result { + pub fn g2(&self, node: NodePtr) -> Result { let blob = match self.sexp(node) { SExp::Atom => self.atom(node), _ => { return err(node, "pair found, expected G2 point"); } }; - if blob.len() != 96 { - return err(node, "atom is not G2 size, 96 bytes"); - } - - let affine: Option = - G2Affine::from_compressed(blob.try_into().expect("G2 slice is not 96 bytes")).into(); - match affine { - Some(point) => Ok(G2Projective::from(point)), - None => err(node, "atom is not a G2 point"), - } + let array = blob + .try_into() + .map_err(|_| EvalErr(node, "atom is not G2 size, 96 bytes".to_string()))?; + G2Element::from_bytes(&array) + .map_err(|_| EvalErr(node, "atom is not a G2 point".to_string())) } pub fn sexp(&self, node: NodePtr) -> SExp { @@ -719,7 +707,7 @@ fn test_g1_roundtrip(#[case] atom: &str) { let mut a = Allocator::new(); let n = a.new_atom(&hex::decode(atom).unwrap()).unwrap(); let g1 = a.g1(n).unwrap(); - assert_eq!(hex::encode(G1Affine::from(g1).to_compressed()), atom); + assert_eq!(hex::encode(g1.to_bytes()), atom); let g1_copy = a.new_g1(g1).unwrap(); let g1_atom = a.atom(g1_copy); @@ -764,7 +752,7 @@ fn test_g2_roundtrip(#[case] atom: &str) { let mut a = Allocator::new(); let n = a.new_atom(&hex::decode(atom).unwrap()).unwrap(); let g2 = a.g2(n).unwrap(); - assert_eq!(hex::encode(G2Affine::from(g2).to_compressed()), atom); + assert_eq!(hex::encode(g2.to_bytes()), atom); let g2_copy = a.new_g2(g2).unwrap(); let g2_atom = a.atom(g2_copy); @@ -804,31 +792,25 @@ fn make_number(a: &mut Allocator, bytes: &[u8]) -> NodePtr { #[cfg(test)] fn make_g1(a: &mut Allocator, bytes: &[u8]) -> NodePtr { - let v: G1Projective = G1Affine::from_compressed(bytes.try_into().unwrap()) - .unwrap() - .into(); + let v = G1Element::from_bytes(bytes.try_into().unwrap()).unwrap(); a.new_g1(v).unwrap() } #[cfg(test)] fn make_g2(a: &mut Allocator, bytes: &[u8]) -> NodePtr { - let v: G2Projective = G2Affine::from_compressed(bytes.try_into().unwrap()) - .unwrap() - .into(); + let v = G2Element::from_bytes(bytes.try_into().unwrap()).unwrap(); a.new_g2(v).unwrap() } #[cfg(test)] fn make_g1_fail(a: &mut Allocator, bytes: &[u8]) -> NodePtr { assert!(<[u8; 48]>::try_from(bytes).is_err()); - //assert!(G1Affine::from_compressed(bytes.try_into().unwrap()).is_none().unwrap_u8() != 0); a.new_atom(bytes).unwrap() } #[cfg(test)] fn make_g2_fail(a: &mut Allocator, bytes: &[u8]) -> NodePtr { assert!(<[u8; 96]>::try_from(bytes).is_err()); - //assert!(G2Affine::from_compressed(bytes.try_into().unwrap()).is_none().unwrap_u8() != 0); a.new_atom(bytes).unwrap() } @@ -851,32 +833,26 @@ fn check_number(a: &Allocator, n: NodePtr, bytes: &[u8]) { #[cfg(test)] fn check_g1(a: &Allocator, n: NodePtr, bytes: &[u8]) { let num = a.g1(n).unwrap(); - let v: G1Projective = G1Affine::from_compressed(bytes.try_into().unwrap()) - .unwrap() - .into(); + let v = G1Element::from_bytes(bytes.try_into().unwrap()).unwrap(); assert_eq!(num, v); } #[cfg(test)] fn check_g2(a: &Allocator, n: NodePtr, bytes: &[u8]) { let num = a.g2(n).unwrap(); - let v: G2Projective = G2Affine::from_compressed(bytes.try_into().unwrap()) - .unwrap() - .into(); + let v = G2Element::from_bytes(bytes.try_into().unwrap()).unwrap(); assert_eq!(num, v); } #[cfg(test)] fn check_g1_fail(a: &Allocator, n: NodePtr, bytes: &[u8]) { assert_eq!(a.g1(n).unwrap_err().0, n); - //assert!(G1Affine::from_compressed(bytes.try_into().unwrap()).is_none().unwrap_u8() != 0); assert!(<[u8; 48]>::try_from(bytes).is_err()); } #[cfg(test)] fn check_g2_fail(a: &Allocator, n: NodePtr, bytes: &[u8]) { assert_eq!(a.g2(n).unwrap_err().0, n); - //assert!(G2Affine::from_compressed(bytes.try_into().unwrap()).is_none().unwrap_u8() != 0); assert!(<[u8; 96]>::try_from(bytes).is_err()); } @@ -1042,8 +1018,7 @@ e28f75bb8f1c7c42c39a8c5529bf0f4e", fn test_atom_len_g1(#[case] buffer_hex: &str, #[case] expected: usize) { let mut a = Allocator::new(); let buffer = &hex::decode(buffer_hex).unwrap(); - let g1 = - G1Projective::from(G1Affine::from_compressed(&buffer[..].try_into().unwrap()).unwrap()); + let g1 = G1Element::from_bytes(&buffer[..].try_into().unwrap()).expect("invalid G1 point"); let atom = a.new_g1(g1).unwrap(); assert_eq!(a.atom_len(atom), expected); } @@ -1074,8 +1049,7 @@ fn test_atom_len_g2(#[case] buffer_hex: &str, #[case] expected: usize) { let mut a = Allocator::new(); let buffer = &hex::decode(buffer_hex).unwrap(); - let g2 = - G2Projective::from(G2Affine::from_compressed(&buffer[..].try_into().unwrap()).unwrap()); + let g2 = G2Element::from_bytes(&buffer[..].try_into().unwrap()).expect("invalid G2 point"); let atom = a.new_g2(g2).unwrap(); assert_eq!(a.atom_len(atom), expected); } diff --git a/src/bls_ops.rs b/src/bls_ops.rs index 0271441b..2142377e 100644 --- a/src/bls_ops.rs +++ b/src/bls_ops.rs @@ -2,14 +2,14 @@ use crate::allocator::{Allocator, NodePtr}; use crate::cost::{check_cost, Cost}; use crate::err_utils::err; use crate::op_utils::{ - atom, first, get_args, get_varargs, int_atom, mod_group_order, new_atom_and_cost, nullp, - number_to_scalar, rest, MALLOC_COST_PER_BYTE, + atom, first, get_args, get_varargs, int_atom, mod_group_order, new_atom_and_cost, nullp, rest, + MALLOC_COST_PER_BYTE, +}; +use crate::reduction::{EvalErr, Reduction, Response}; +use chia_bls::{ + aggregate_pairing, aggregate_verify, hash_to_g1_with_dst, hash_to_g2_with_dst, G1Element, + G2Element, PublicKey, }; -use crate::reduction::{Reduction, Response}; -use bls12_381::hash_to_curve::{ExpandMsgXmd, HashToCurve}; -use bls12_381::{multi_miller_loop, G1Affine, G1Projective, G2Affine, G2Prepared, G2Projective}; -use group::Group; -use std::ops::Neg; // the same cost as point_add (aka g1_add) const BLS_G1_SUBTRACT_BASE_COST: Cost = 101094; @@ -51,7 +51,7 @@ const DST_G2: &[u8; 43] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_"; pub fn op_bls_g1_subtract(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Response { let mut cost = BLS_G1_SUBTRACT_BASE_COST; check_cost(a, cost, max_cost)?; - let mut total: G1Projective = G1Projective::identity(); + let mut total = G1Element::default(); let mut is_first = true; while let Some((arg, rest)) = a.next(input) { input = rest; @@ -61,7 +61,7 @@ pub fn op_bls_g1_subtract(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) if is_first { total = point; } else { - total -= point; + total -= &point; }; is_first = false; } @@ -82,7 +82,8 @@ pub fn op_bls_g1_multiply(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> cost += scalar_len as Cost * BLS_G1_MULTIPLY_COST_PER_BYTE; check_cost(a, cost, max_cost)?; - total *= number_to_scalar(mod_group_order(scalar)); + let scalar = mod_group_order(scalar); + total.scalar_multiply(scalar.to_bytes_be().1.as_slice()); Ok(Reduction( cost + 48 * MALLOC_COST_PER_BYTE, @@ -93,18 +94,14 @@ pub fn op_bls_g1_multiply(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> pub fn op_bls_g1_negate(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response { let [point] = get_args::<1>(a, input, "g1_negate")?; - // we don't validate the point. We may want to soft fork-in validating the - // point once the allocator preserves native representation of points let blob = atom(a, point, "G1 atom")?; - if blob.len() != 48 { - return err(point, "atom is not G1 size, 48 bytes"); - } - if G1Affine::from_compressed(blob.try_into().expect("G1 slice is not 48 bytes")) - .is_none() - .into() - { - return err(point, "atom is not a valid G1 point"); - } + // this is here to validate the point + let _g1 = G1Element::from_bytes( + blob.try_into() + .map_err(|_| EvalErr(point, "atom is not G1 size, 48 bytes".to_string()))?, + ) + .map_err(|_| EvalErr(point, "atom is not a valid G1 point".to_string()))?; + if (blob[0] & 0xe0) == 0xc0 { // This is compressed infinity. negating it is a no-op // we can just pass through the same atom as we received. We'll charge @@ -123,7 +120,7 @@ pub fn op_bls_g1_negate(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> R pub fn op_bls_g2_add(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Response { let mut cost = BLS_G2_ADD_BASE_COST; check_cost(a, cost, max_cost)?; - let mut total: G2Projective = G2Projective::identity(); + let mut total = G2Element::default(); while let Some((arg, rest)) = a.next(input) { input = rest; let point = a.g2(arg)?; @@ -140,7 +137,7 @@ pub fn op_bls_g2_add(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> R pub fn op_bls_g2_subtract(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Response { let mut cost = BLS_G2_SUBTRACT_BASE_COST; check_cost(a, cost, max_cost)?; - let mut total: G2Projective = G2Projective::identity(); + let mut total = G2Element::default(); let mut is_first = true; while let Some((arg, rest)) = a.next(input) { input = rest; @@ -150,7 +147,7 @@ pub fn op_bls_g2_subtract(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) if is_first { total = point; } else { - total -= point; + total -= &point; }; is_first = false; } @@ -171,7 +168,8 @@ pub fn op_bls_g2_multiply(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> cost += scalar_len as Cost * BLS_G2_MULTIPLY_COST_PER_BYTE; check_cost(a, cost, max_cost)?; - total *= number_to_scalar(mod_group_order(scalar)); + let scalar = mod_group_order(scalar); + total.scalar_multiply(scalar.to_bytes_be().1.as_slice()); Ok(Reduction( cost + 96 * MALLOC_COST_PER_BYTE, @@ -185,16 +183,14 @@ pub fn op_bls_g2_negate(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> R // we don't validate the point. We may want to soft fork-in validating the // point once the allocator preserves native representation of points let blob = atom(a, point, "G2 atom")?; - if blob.len() != 96 { - return err(point, "atom is not G2 size, 96 bytes"); - } - if G2Affine::from_compressed(blob.try_into().expect("G2 slice is not 96 bytes")) - .is_none() - .into() - { - return err(point, "atom is not a valid G2 point"); - } + // this is here to validate the point + let _g2 = G2Element::from_bytes( + blob.try_into() + .map_err(|_| EvalErr(point, "atom is not G2 size, 96 bytes".to_string()))?, + ) + .map_err(|_| EvalErr(point, "atom is not a valid G2 point".to_string()))?; + if (blob[0] & 0xe0) == 0xc0 { // This is compressed infinity. negating it is a no-op // we can just pass through the same atom as we received. We'll charge @@ -231,7 +227,7 @@ pub fn op_bls_map_to_g1(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Re cost += dst.len() as Cost * BLS_MAP_TO_G1_COST_PER_DST_BYTE; check_cost(a, cost, max_cost)?; - let point = >>::hash_to_curve(msg, dst); + let point = hash_to_g1_with_dst(msg, dst); Ok(Reduction( cost + 48 * MALLOC_COST_PER_BYTE, a.new_g1(point)?, @@ -258,7 +254,7 @@ pub fn op_bls_map_to_g2(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Re cost += dst.len() as Cost * BLS_MAP_TO_G2_COST_PER_DST_BYTE; check_cost(a, cost, max_cost)?; - let point = >>::hash_to_curve(msg, dst); + let point = hash_to_g2_with_dst(msg, dst); Ok(Reduction( cost + 96 * MALLOC_COST_PER_BYTE, a.new_g2(point)?, @@ -268,13 +264,12 @@ pub fn op_bls_map_to_g2(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Re // This operator takes a variable number of G1 and G2 points. The points must // come in pairs (as a "flat" argument list). // It performs a low-level pairing operation of the (G1, G2)-pairs -// and returns a boolean indicating whether the resulting Gt point is the -// identity or not. True means identity False otherwise. This is a building -// block for signature verification. +// and returns if the resulting Gt point is the +// identity, otherwise terminates the program with a validation error. pub fn op_bls_pairing_identity(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response { let mut cost = BLS_PAIRING_BASE_COST; check_cost(a, cost, max_cost)?; - let mut items = Vec::<(G1Affine, G2Prepared)>::new(); + let mut items = Vec::<(G1Element, G2Element)>::new(); let mut args = input; while !nullp(a, args) { @@ -284,18 +279,10 @@ pub fn op_bls_pairing_identity(a: &mut Allocator, input: NodePtr, max_cost: Cost args = rest(a, args)?; let g2 = a.g2(first(a, args)?)?; args = rest(a, args)?; - items.push((g1.into(), G2Prepared::from(G2Affine::from(g2)))); + items.push((g1, g2)); } - let mut item_refs = Vec::<(&G1Affine, &G2Prepared)>::new(); - for (p, q) in &items { - item_refs.push((p, q)); - } - let identity: bool = multi_miller_loop(&item_refs) - .final_exponentiation() - .is_identity() - .into(); - if !identity { + if !aggregate_pairing(items) { err(input, "bls_pairing_identity failed") } else { Ok(Reduction(cost, a.null())) @@ -318,7 +305,7 @@ pub fn op_bls_verify(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respo // followed by a variable number of (G1, msg)-pairs (as a flat list) args = rest(a, args)?; - let mut items = Vec::<(G1Affine, G2Prepared)>::new(); + let mut items = Vec::<(PublicKey, &[u8])>::new(); while !nullp(a, args) { let pk = a.g1(first(a, args)?)?; args = rest(a, args)?; @@ -330,32 +317,10 @@ pub fn op_bls_verify(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respo cost += DST_G2.len() as Cost * BLS_MAP_TO_G2_COST_PER_DST_BYTE; check_cost(a, cost, max_cost)?; - // The AUG scheme requires prepending the public key to the signed - // message - let mut prepended_msg = G1Affine::from(pk).to_compressed().to_vec(); - prepended_msg.extend_from_slice(msg); - - let point = >>::hash_to_curve( - prepended_msg, - DST_G2, - ); - items.push((pk.into(), G2Prepared::from(G2Affine::from(point)))); + items.push((pk, msg)); } - items.push(( - G1Affine::generator().neg(), - G2Prepared::from(G2Affine::from(signature)), - )); - - let mut item_refs = Vec::<(&G1Affine, &G2Prepared)>::new(); - for (p, q) in &items { - item_refs.push((p, q)); - } - let identity: bool = multi_miller_loop(&item_refs) - .final_exponentiation() - .is_identity() - .into(); - if !identity { + if !aggregate_verify(&signature, items) { err(input, "bls_verify failed") } else { Ok(Reduction(cost, a.null())) diff --git a/src/more_ops.rs b/src/more_ops.rs index ffea2185..b09d75ca 100644 --- a/src/more_ops.rs +++ b/src/more_ops.rs @@ -1,4 +1,3 @@ -use bls12_381::{G1Affine, G1Projective, Scalar}; use num_bigint::{BigUint, Sign}; use num_integer::Integer; use std::ops::BitAndAssign; @@ -11,10 +10,11 @@ use crate::err_utils::err; use crate::number::Number; use crate::op_utils::{ atom, atom_len, get_args, get_varargs, i32_atom, int_atom, mod_group_order, new_atom_and_cost, - nullp, number_to_scalar, u32_from_u8, MALLOC_COST_PER_BYTE, + nullp, u32_from_u8, MALLOC_COST_PER_BYTE, }; use crate::reduction::{Reduction, Response}; use crate::sha2::{Digest, Sha256}; +use chia_bls::G1Element; const ARITH_BASE_COST: Cost = 99; const ARITH_COST_PER_ARG: Cost = 320; @@ -790,10 +790,11 @@ pub fn op_all(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Response pub fn op_pubkey_for_exp(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response { let [n] = get_args::<1>(a, input, "pubkey_for_exp")?; let (v0, v0_len) = int_atom(a, n, "pubkey_for_exp")?; - let exp: Number = mod_group_order(v0); + let bytes = mod_group_order(v0).to_bytes_be().1; + + let point = G1Element::from_integer(&bytes); + let cost = PUBKEY_BASE_COST + (v0_len as Cost) * PUBKEY_COST_PER_BYTE; - let exp: Scalar = number_to_scalar(exp); - let point: G1Projective = G1Affine::generator() * exp; Ok(Reduction( cost + 48 * MALLOC_COST_PER_BYTE, a.new_g1(point)?, @@ -802,7 +803,7 @@ pub fn op_pubkey_for_exp(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> pub fn op_point_add(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Response { let mut cost = POINT_ADD_BASE_COST; - let mut total: G1Projective = G1Projective::identity(); + let mut total = G1Element::default(); while let Some((arg, rest)) = a.next(input) { input = rest; let point = a.g1(arg)?; diff --git a/src/op_utils.rs b/src/op_utils.rs index c057685c..d3f2cda7 100644 --- a/src/op_utils.rs +++ b/src/op_utils.rs @@ -4,7 +4,6 @@ use crate::err_utils::err; use crate::number::Number; use crate::reduction::EvalErr; use crate::reduction::{Reduction, Response}; -use bls12_381::Scalar; use lazy_static::lazy_static; use num_bigint::{BigUint, Sign}; use num_integer::Integer; @@ -575,18 +574,6 @@ fn test_i32_atom() { assert_eq!(r.1, "test requires int32 args (with no leading zeros)"); } -pub fn number_to_scalar(n: Number) -> Scalar { - let (sign, as_u8): (Sign, Vec) = n.to_bytes_le(); - let mut scalar_array: [u8; 32] = [0; 32]; - scalar_array[..as_u8.len()].clone_from_slice(&as_u8[..]); - let exp: Scalar = Scalar::from_bytes(&scalar_array).unwrap(); - if sign == Sign::Minus { - exp.neg() - } else { - exp - } -} - pub fn new_atom_and_cost(a: &mut Allocator, cost: Cost, buf: &[u8]) -> Response { let c = buf.len() as Cost * MALLOC_COST_PER_BYTE; Ok(Reduction(cost + c, a.new_atom(buf)?)) diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 7c242bb4..21f783d2 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -19,11 +19,10 @@ rand = "0.8.4" sha1 = "=0.10.5" linreg = "=0.2.0" clvmr = { path = ".." } -bls12_381 = { version = "=0.8.0", features = ["experimental"] } +chia-bls = ">=0.2.13" num-bigint = "0.4.3" serde = { version ="1.0.163", features = ["derive"] } serde_json = "1.0.96" -group = "=0.13.0" clap = { version = "=4.0.29", features = ["derive"] } [[bin]] diff --git a/tools/src/bin/verify-zksnark.rs b/tools/src/bin/verify-zksnark.rs index 3d4d11f8..63330f55 100644 --- a/tools/src/bin/verify-zksnark.rs +++ b/tools/src/bin/verify-zksnark.rs @@ -1,6 +1,4 @@ -use bls12_381::{multi_miller_loop, G1Affine, G1Projective, G2Affine, G2Prepared, Scalar}; -use group::Group; - +use chia_bls::{aggregate_pairing, G1Element, G2Element}; use num_bigint::BigInt; use serde::Deserialize; @@ -52,18 +50,23 @@ fn vec_pair(arr: &[String]) -> ([u8; 48], [u8; 48]) { ) } -fn vec_pair_g1(arr: &[String]) -> G1Affine { +fn vec_pair_g1(arr: &[String]) -> G1Element { let (fp_1, fp_2) = vec_pair(arr); let data: [u8; 96] = [fp_1, fp_2].concat().try_into().unwrap(); - G1Affine::from_uncompressed(&data).unwrap() + println!("G1 uncompressed: {}", hex::encode(data)); + let ret = G1Element::from_uncompressed(&data).unwrap(); + println!("G1 compressed: {}", hex::encode(ret.to_bytes())); + ret } -fn vec_pair_g2(arr: &[Vec]) -> G2Affine { +fn vec_pair_g2(arr: &[Vec]) -> G2Element { let (fp_1, fp_2) = vec_pair(&arr[0]); let (fp_3, fp_4) = vec_pair(&arr[1]); let data: [u8; 192] = [fp_2, fp_1, fp_4, fp_3].concat().try_into().unwrap(); - let p = G2Affine::from_uncompressed(&data); - p.unwrap() + println!("G2 uncompressed: {}", hex::encode(data)); + let ret = G2Element::from_uncompressed(&data).unwrap(); + println!("G2 compressed: {}", hex::encode(ret.to_bytes())); + ret } pub fn main() { @@ -91,22 +94,17 @@ pub fn main() { let ic0 = vec_pair_g1(&verification_key.ic[0]); - let mut cpub = G1Affine::identity(); + let mut cpub = G1Element::default(); for (i, public_i) in public.iter().enumerate() { - let ic = vec_pair_g1(&verification_key.ic[i + 1]); - let scalar: [u8; 32] = public_i - .parse::() - .unwrap() - .to_bytes_le() - .1 - .try_into() - .unwrap(); - let scalar = Scalar::from_bytes(&scalar).unwrap(); - cpub = (cpub + ic * scalar).into(); + let mut ic = vec_pair_g1(&verification_key.ic[i + 1]); + let scalar = public_i.parse::().unwrap().to_bytes_be().1; + ic.scalar_multiply(&scalar); + cpub += ⁣ } - cpub = (cpub + G1Projective::from(ic0)).into(); + cpub += &ic0; - let pi_a = vec_pair_g1(&proof.pi_a); + let mut pi_a = vec_pair_g1(&proof.pi_a); + pi_a.negate(); let pi_b = vec_pair_g2(&proof.pi_b); let pi_c = vec_pair_g1(&proof.pi_c); @@ -118,30 +116,23 @@ pub fn main() { // output the compressed values println!( "bls_pairing_identity 0x{} 0x{} 0x{} 0x{} 0x{} 0x{} 0x{} 0x{} => 0 | 7800000", - hex::encode((-pi_a).to_compressed()), - hex::encode(pi_b.to_compressed()), - hex::encode(cpub.to_compressed()), - hex::encode(vk_gamma_2.to_compressed()), - hex::encode(pi_c.to_compressed()), - hex::encode(vk_delta_2.to_compressed()), - hex::encode(vk_alpha_1.to_compressed()), - hex::encode(vk_beta_2.to_compressed()) + hex::encode(pi_a.to_bytes()), + hex::encode(pi_b.to_bytes()), + hex::encode(cpub.to_bytes()), + hex::encode(vk_gamma_2.to_bytes()), + hex::encode(pi_c.to_bytes()), + hex::encode(vk_delta_2.to_bytes()), + hex::encode(vk_alpha_1.to_bytes()), + hex::encode(vk_beta_2.to_bytes()) ); // run the miller loop - let mut item_refs = Vec::<(&G1Affine, &G2Prepared)>::new(); - let pi_a = -pi_a; - let pi_b = G2Prepared::from(pi_b); - let vk_gamma_2 = G2Prepared::from(vk_gamma_2); - let vk_delta_2 = G2Prepared::from(vk_delta_2); - let vk_beta_2 = G2Prepared::from(vk_beta_2); - item_refs.push((&pi_a, &pi_b)); - item_refs.push((&cpub, &vk_gamma_2)); - item_refs.push((&pi_c, &vk_delta_2)); - item_refs.push((&vk_alpha_1, &vk_beta_2)); - let identity: bool = multi_miller_loop(&item_refs) - .final_exponentiation() - .is_identity() - .into(); + let item_refs: Vec<(&G1Element, &G2Element)> = vec![ + (&pi_a, &pi_b), + (&cpub, &vk_gamma_2), + (&pi_c, &vk_delta_2), + (&vk_alpha_1, &vk_beta_2), + ]; + let identity: bool = aggregate_pairing(item_refs); assert!(identity); }