From 6232ff4e91ee2e55a8e5ab96b6e360bdd3037c67 Mon Sep 17 00:00:00 2001 From: z2trillion Date: Tue, 31 Oct 2023 09:56:56 +0800 Subject: [PATCH] Add n_rows_required method to MptCircuit (#93) * fix column annotation * Implement n_rows_required for internal gadgets * Add n_rows_required implementation for MptUpdateGadget * clippy and add 1 to account for disabled row in MptUpdateConfig * Start assignment from second row * Remove panic * Add comments explaining where +1 comes from * Add test for fixed vk * Dedup hash traces and add comment explaining +1 in n_rows_required * Fix comment * Fix build * Track Cargo.lock --------- Co-authored-by: Mason Liang --- .gitignore | 3 +- Cargo.lock.current => Cargo.lock | 278 ++++++++++-------------- Cargo.toml | 2 +- src/constraint_builder/column.rs | 2 +- src/gadgets/byte_bit.rs | 13 +- src/gadgets/byte_representation.rs | 14 +- src/gadgets/canonical_representation.rs | 52 ++++- src/gadgets/key_bit.rs | 11 +- src/gadgets/mpt_update.rs | 32 +++ src/gadgets/poseidon.rs | 12 + src/mpt.rs | 27 ++- src/tests.rs | 66 +++++- 12 files changed, 318 insertions(+), 194 deletions(-) rename Cargo.lock.current => Cargo.lock (91%) diff --git a/.gitignore b/.gitignore index 31a5ddfa..21ad0a9e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ /layouts .vscode .cargo -Cargo.lock -*.png \ No newline at end of file +*.png diff --git a/Cargo.lock.current b/Cargo.lock similarity index 91% rename from Cargo.lock.current rename to Cargo.lock index cf31c6e0..cab9efe6 100644 --- a/Cargo.lock.current +++ b/Cargo.lock @@ -21,9 +21,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.5" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c378d78423fdad8089616f827526ee33c19f2fddbd5de1629152c9593ba4783" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] @@ -73,13 +73,13 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" [[package]] name = "async-trait" -version = "0.1.73" +version = "0.1.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" +checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", ] [[package]] @@ -88,7 +88,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi 0.1.19", + "hermit-abi", "libc", "winapi", ] @@ -155,9 +155,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" @@ -213,15 +213,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f5353f36341f7451062466f0b755b96ac3a9547e4d7f6b70d603fc721a7d7896" dependencies = [ - "sha2 0.10.7", + "sha2 0.10.8", "tinyvec", ] [[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 = "byte-slice-cast" @@ -237,9 +237,9 @@ checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" [[package]] name = "byteorder" -version = "1.4.3" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" @@ -273,9 +273,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.30" +version = "0.4.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "defd4e7873dbddba6c7c91e199c7fcb946abc4a6a4ac3195400bcfb01b5de877" +checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" dependencies = [ "android-tzdata", "iana-time-zone", @@ -316,7 +316,7 @@ dependencies = [ "hmac 0.12.1", "k256", "serde", - "sha2 0.10.7", + "sha2 0.10.8", "thiserror", ] @@ -332,7 +332,7 @@ dependencies = [ "once_cell", "pbkdf2 0.12.2", "rand", - "sha2 0.10.7", + "sha2 0.10.8", "thiserror", ] @@ -351,7 +351,7 @@ dependencies = [ "ripemd", "serde", "serde_derive", - "sha2 0.10.7", + "sha2 0.10.8", "sha3 0.10.8", "thiserror", ] @@ -765,9 +765,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", @@ -803,25 +803,14 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.3" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" dependencies = [ - "errno-dragonfly", "libc", "windows-sys", ] -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "eth-keystore" version = "0.5.0" @@ -838,7 +827,7 @@ dependencies = [ "scrypt", "serde", "serde_json", - "sha2 0.10.7", + "sha2 0.10.8", "sha3 0.10.8", "thiserror", "uuid", @@ -847,7 +836,7 @@ dependencies = [ [[package]] name = "eth-types" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git#0afa0d9e148b914eda1d1a701547f62ee1844b24" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?rev=7d9bc181953cfc6e7baf82ff0ce651281fd70a8a#7d9bc181953cfc6e7baf82ff0ce651281fd70a8a" dependencies = [ "ethers-core", "ethers-signers", @@ -858,6 +847,7 @@ dependencies = [ "libsecp256k1", "num", "num-bigint", + "once_cell", "poseidon-circuit", "regex", "serde", @@ -958,16 +948,16 @@ dependencies = [ "ethers-core", "hex", "rand", - "sha2 0.10.7", + "sha2 0.10.8", "thiserror", "tracing", ] [[package]] name = "fastrand" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "fdeflate" @@ -1013,9 +1003,9 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.27" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6c98ee8095e9d1dcbf2fcc6d95acccb90d1c81db1e44725c6a984b1dbdfb010" +checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" dependencies = [ "crc32fast", "miniz_oxide", @@ -1192,7 +1182,7 @@ dependencies = [ [[package]] name = "halo2-mpt-circuits" version = "0.1.0" -source = "git+https://github.com/scroll-tech/mpt-circuit.git?tag=v0.6.3#a42263eeb38f48b3008abea95993423604497c6a" +source = "git+https://github.com/scroll-tech/mpt-circuit.git?tag=v0.7.0#578c210ceb88d3c143ee2a013ad836d19285d9c1" dependencies = [ "ethers-core", "halo2_proofs", @@ -1214,7 +1204,7 @@ dependencies = [ [[package]] name = "halo2_proofs" version = "0.2.0" -source = "git+https://github.com/scroll-tech/halo2.git?branch=develop#aa86c107aeb62282d81ebce5c4930ec0c0aa540b" +source = "git+https://github.com/scroll-tech/halo2.git?branch=develop#e3fe25eadd714fd991f35190d17ff0b8fb031188" dependencies = [ "ark-std", "blake2b_simd", @@ -1275,9 +1265,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" [[package]] name = "heck" @@ -1294,12 +1284,6 @@ dependencies = [ "libc", ] -[[package]] -name = "hermit-abi" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" - [[package]] name = "hex" version = "0.4.3" @@ -1426,9 +1410,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.0.0" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ "equivalent", "hashbrown", @@ -1483,7 +1467,7 @@ dependencies = [ "ecdsa", "elliptic-curve", "once_cell", - "sha2 0.10.7", + "sha2 0.10.8", "signature", ] @@ -1504,15 +1488,15 @@ 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 = "libloading" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d580318f95776505201b28cf98eb1fa5e4be3b689633ba6a3e6cd880ff22d8cb" +checksum = "c571b676ddfc9a8c12f1f3d3085a7b163966a8fd8098a90640953ce5f6170161" dependencies = [ "cfg-if 1.0.0", "windows-sys", @@ -1568,9 +1552,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.7" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" [[package]] name = "log" @@ -1580,9 +1564,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.6.3" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "memoffset" @@ -1606,10 +1590,10 @@ dependencies = [ [[package]] name = "mpt-zktrie" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git#0afa0d9e148b914eda1d1a701547f62ee1844b24" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?rev=7d9bc181953cfc6e7baf82ff0ce651281fd70a8a#7d9bc181953cfc6e7baf82ff0ce651281fd70a8a" dependencies = [ "eth-types", - "halo2-mpt-circuits 0.1.0 (git+https://github.com/scroll-tech/mpt-circuit.git?tag=v0.6.3)", + "halo2-mpt-circuits 0.1.0 (git+https://github.com/scroll-tech/mpt-circuit.git?tag=v0.7.0)", "halo2_proofs", "hex", "lazy_static", @@ -1689,23 +1673,13 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi 0.3.2", - "libc", -] - [[package]] name = "num_enum" version = "0.6.1" @@ -1724,7 +1698,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", ] [[package]] @@ -1823,9 +1797,9 @@ dependencies = [ [[package]] name = "pathfinder_simd" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39fe46acc5503595e5949c17b818714d26fdf9b4920eacf3b2947f0199f4a6ff" +checksum = "0444332826c70dc47be74a7c6a5fc44e23a7905ad6858d4162b658320455ef93" dependencies = [ "rustc_version", ] @@ -1849,17 +1823,6 @@ dependencies = [ "hmac 0.12.1", ] -[[package]] -name = "pest" -version = "2.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7a4d085fd991ac8d5b05a147b437791b4260b76326baf0fc60cf7c9c27ecd33" -dependencies = [ - "memchr", - "thiserror", - "ucd-trie", -] - [[package]] name = "pin-project-lite" version = "0.2.13" @@ -1973,9 +1936,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "primitive-types" -version = "0.12.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f3486ccba82358b11a77516035647c34ba167dfa53312630de83b12bd4f3d66" +checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" dependencies = [ "fixed-hash", "impl-codec", @@ -2021,9 +1984,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", ] @@ -2084,9 +2047,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", @@ -2094,14 +2057,12 @@ 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]] @@ -2135,9 +2096,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.5" +version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", @@ -2147,9 +2108,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.8" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", @@ -2158,9 +2119,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.5" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rfc6979" @@ -2211,20 +2172,20 @@ checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" [[package]] name = "rustc_version" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ "semver", ] [[package]] name = "rustix" -version = "0.38.13" +version = "0.38.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662" +checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" dependencies = [ - "bitflags 2.4.0", + "bitflags 2.4.1", "errno", "libc", "linux-raw-sys", @@ -2300,7 +2261,7 @@ dependencies = [ "hmac 0.12.1", "pbkdf2 0.11.0", "salsa20", - "sha2 0.10.7", + "sha2 0.10.8", ] [[package]] @@ -2319,47 +2280,35 @@ dependencies = [ [[package]] name = "semver" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver-parser" -version = "0.10.2" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" -dependencies = [ - "pest", -] +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "serde" -version = "1.0.188" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.188" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", ] [[package]] name = "serde_json" -version = "1.0.106" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ "itoa", "ryu", @@ -2403,9 +2352,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 1.0.0", "cpufeatures", @@ -2519,9 +2468,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.32" +version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", @@ -2560,31 +2509,31 @@ dependencies = [ [[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.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", ] [[package]] @@ -2630,11 +2579,10 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.37" +version = "0.1.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +checksum = "ee2ef2af84856a50c1d430afce2fdded0a4ec7eda868db86409b4543df0797f9" dependencies = [ - "cfg-if 1.0.0", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -2642,20 +2590,20 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.26" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", ] [[package]] name = "tracing-core" -version = "0.1.31" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", ] @@ -2668,15 +2616,9 @@ checksum = "375812fa44dab6df41c195cd2f7fecb488f6c09fbaafb62807488cefab642bff" [[package]] name = "typenum" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" - -[[package]] -name = "ucd-trie" -version = "0.1.6" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "uint" @@ -2692,9 +2634,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unicode-xid" @@ -2755,7 +2697,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", "wasm-bindgen-shared", ] @@ -2777,7 +2719,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.38", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2822,9 +2764,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", ] @@ -2912,9 +2854,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "winnow" -version = "0.5.15" +version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc" +checksum = "a3b801d0e0a6726477cc207f60162da452f3a95adb368399bef20a946e06f65c" dependencies = [ "memchr", ] @@ -2958,7 +2900,7 @@ checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" [[package]] name = "zktrie" version = "0.2.0" -source = "git+https://github.com/scroll-tech/zktrie.git?branch=v0.6#83318659773604fa565e2ebeb810a6d3746f0af4" +source = "git+https://github.com/scroll-tech/zktrie.git?branch=v0.7#a130ea543d291d4b71724f91cb8a49745c593a0c" dependencies = [ "gobuild", ] diff --git a/Cargo.toml b/Cargo.toml index 65f5f3e1..18cecdd1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = " print_layout = ["halo2_proofs/dev-graph"] [dev-dependencies] -mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git" } +mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", rev = "7d9bc181953cfc6e7baf82ff0ce651281fd70a8a" } # mpt-zktrie = { path = "../scroll-circuits/zktrie" } rand_chacha = "0.3.0" plotters = "0.3" diff --git a/src/constraint_builder/column.rs b/src/constraint_builder/column.rs index b3dbfa03..45351e16 100644 --- a/src/constraint_builder/column.rs +++ b/src/constraint_builder/column.rs @@ -51,7 +51,7 @@ impl FixedColumn { { region .assign_fixed( - || "asdfasdfawe", + || "fixed", self.0, offset, || Value::known(value.try_into().unwrap()), diff --git a/src/gadgets/byte_bit.rs b/src/gadgets/byte_bit.rs index 60474732..471fa2f8 100644 --- a/src/gadgets/byte_bit.rs +++ b/src/gadgets/byte_bit.rs @@ -31,7 +31,7 @@ impl ByteBitGadget { } pub fn assign(&self, region: &mut Region<'_, F>) { - let mut offset = 0; + let mut offset = 1; for byte in 0..256 { for index in 0..8 { self.byte.assign(region, offset, byte); @@ -40,6 +40,17 @@ impl ByteBitGadget { offset += 1; } } + + let expected_offset = Self::n_rows_required(); + debug_assert!( + offset == expected_offset, + "assign used {offset} rows but {expected_offset} rows expected from `n_rows_required`", + ); + } + + pub fn n_rows_required() -> usize { + // +1 because assigment starts on offset = 1 instead of offset = 0. + 256 * 8 + 1 } } diff --git a/src/gadgets/byte_representation.rs b/src/gadgets/byte_representation.rs index 4ce4a331..aa8b2904 100644 --- a/src/gadgets/byte_representation.rs +++ b/src/gadgets/byte_representation.rs @@ -93,7 +93,6 @@ impl ByteRepresentationConfig { } } - // can this we done with an Iterator instead? pub fn assign( &self, region: &mut Region<'_, F>, @@ -111,7 +110,7 @@ impl ByteRepresentationConfig { .chain(u128s.iter().map(u128_to_big_endian)) .chain(frs.iter().map(fr_to_big_endian)); - let mut offset = 0; + let mut offset = 1; for byte_representation in byte_representations { let mut value = F::zero(); let mut rlc = Value::known(F::zero()); @@ -132,6 +131,17 @@ impl ByteRepresentationConfig { offset += 1; } } + + let expected_offset = Self::n_rows_required(u32s, u64s, u128s, frs); + debug_assert!( + offset == expected_offset, + "assign used {offset} rows but {expected_offset} rows expected from `n_rows_required`", + ); + } + + pub fn n_rows_required(u32s: &[u32], u64s: &[u64], u128s: &[u128], frs: &[Fr]) -> usize { + // +1 because assigment starts on offset = 1 instead of offset = 0. + 1 + u32s.len() * 4 + u64s.len() * 8 + u128s.len() * 16 + frs.len() * 31 } } diff --git a/src/gadgets/canonical_representation.rs b/src/gadgets/canonical_representation.rs index 7e2ae9b5..e98f0132 100644 --- a/src/gadgets/canonical_representation.rs +++ b/src/gadgets/canonical_representation.rs @@ -130,19 +130,19 @@ impl CanonicalRepresentationConfig { } } - pub fn assign<'a>( + pub fn assign( &self, region: &mut Region<'_, Fr>, randomness: Value, - values: impl IntoIterator, + values: &[Fr], + n_rows: usize, ) { let modulus = U256::from_str_radix(Fr::MODULUS, 16).unwrap(); let mut modulus_bytes = [0u8; 32]; modulus.to_big_endian(&mut modulus_bytes); - let mut offset = 0; - // TODO: we add a final Fr::zero() to handle the always enabled selector. Add a default assignment instead? - for value in values.into_iter().copied().chain([Fr::zero()]) { + let mut offset = 1; + for value in values.iter() { let mut bytes = value.to_bytes(); bytes.reverse(); let mut differences_are_zero_so_far = true; @@ -171,7 +171,7 @@ impl CanonicalRepresentationConfig { ); differences_are_zero_so_far &= difference.is_zero_vartime(); - self.value.assign(region, offset, value); + self.value.assign(region, offset, *value); rlc = rlc * randomness + Value::known(Fr::from(u64::from(*byte))); self.rlc.assign(region, offset, rlc); @@ -179,6 +179,42 @@ impl CanonicalRepresentationConfig { offset += 1 } } + + let expected_offset = Self::n_rows_required(values); + debug_assert!( + offset == expected_offset, + "assign used {offset} rows but {expected_offset} rows expected from `n_rows_required`", + ); + + let n_padding_values = n_rows / 32 - values.len(); + for _ in 0..n_padding_values { + for (index, modulus_byte) in modulus_bytes.iter().enumerate() { + self.modulus_byte + .assign(region, offset, u64::from(*modulus_byte)); + + self.index + .assign(region, offset, u64::try_from(index).unwrap()); + if index.is_zero() { + self.index_is_zero.enable(region, offset); + } else if index == 31 { + self.index_is_31.enable(region, offset); + } + + let difference = Fr::from(u64::from(*modulus_byte)); + self.difference.assign(region, offset, difference); + self.difference_is_zero.assign(region, offset, difference); + + self.differences_are_zero_so_far + .assign(region, offset, index == 0); + + offset += 1 + } + } + } + + pub fn n_rows_required(values: &[Fr]) -> usize { + // +1 because assigment starts on offset = 1 instead of offset = 0. + values.len() * 32 + 1 } } @@ -250,11 +286,11 @@ mod test { layouter.assign_region( || "", |mut region| { - for offset in 0..(8 * 256) { + for offset in 1..(1 + 8 * 256) { selector.enable(&mut region, offset); } byte_bit.assign(&mut region); - canonical_representation.assign(&mut region, randomness, &self.values); + canonical_representation.assign(&mut region, randomness, &self.values, 256); Ok(()) }, ) diff --git a/src/gadgets/key_bit.rs b/src/gadgets/key_bit.rs index 8891665d..5d4b2581 100644 --- a/src/gadgets/key_bit.rs +++ b/src/gadgets/key_bit.rs @@ -89,6 +89,8 @@ impl KeyBitConfig { pub fn assign(&self, region: &mut Region<'_, Fr>, lookups: &[(Fr, usize, bool)]) { // TODO; dedup lookups for (offset, (value, index, bit)) in lookups.iter().enumerate() { + // TODO: either move the disabled row to the end of the assigment or get rid of it entirely. + let offset = offset + 1; // Start assigning at offet = 1 because the first row is disabled. let bytes = value.to_bytes(); let index_div_8 = index / 8; // index = (31 - index/8) * 8 @@ -108,6 +110,11 @@ impl KeyBitConfig { self.byte.assign(region, offset, u64::from(byte)); } } + + pub fn n_rows_required(lookups: &[(Fr, usize, bool)]) -> usize { + // +1 because assigment starts on offset = 1 instead of offset = 0. + 1 + lookups.len() + } } impl KeyBitLookup for KeyBitConfig { @@ -191,13 +198,13 @@ mod test { layouter.assign_region( || "", |mut region| { - for offset in 0..(8 * 256) { + for offset in 1..(1 + 8 * 256) { selector.enable(&mut region, offset); } key_bit.assign(&mut region, &self.lookups); byte_bit.assign(&mut region); - canonical_representation.assign(&mut region, randomness, &keys); + canonical_representation.assign(&mut region, randomness, &keys, 256); Ok(()) }, ) diff --git a/src/gadgets/mpt_update.rs b/src/gadgets/mpt_update.rs index 500537e7..cedd72c4 100644 --- a/src/gadgets/mpt_update.rs +++ b/src/gadgets/mpt_update.rs @@ -594,9 +594,21 @@ impl MptUpdateConfig { n_rows += proof.n_rows(); offset = 1 + n_rows; } + + let expected_offset = Self::n_rows_required(proofs); + debug_assert!( + offset == expected_offset, + "assign used {offset} rows but {expected_offset} rows expected from `n_rows_required`", + ); + n_rows } + pub fn n_rows_required(proofs: &[Proof]) -> usize { + // +1 because assigment starts on offset = 1 instead of offset = 0. + proofs.iter().map(Proof::n_rows).sum::() + 1 + } + fn assign_account_trie_rows( &self, region: &mut Region<'_, Fr>, @@ -2086,6 +2098,8 @@ pub fn hash_traces(proofs: &[Proof]) -> Vec<([Fr; 2], Fr, Fr)> { } } } + hash_traces.sort(); + hash_traces.dedup(); hash_traces } @@ -2118,6 +2132,9 @@ pub fn key_bit_lookups(proofs: &[Proof]) -> Vec<(Fr, usize, bool)> { } lookups.extend(proof.storage.key_bit_lookups()); } + + lookups.sort(); + lookups.dedup(); lookups } @@ -2199,6 +2216,19 @@ pub fn byte_representations(proofs: &[Proof]) -> (Vec, Vec, Vec, _ => {} } } + + u32s.sort(); + u32s.dedup(); + + u64s.sort(); + u64s.dedup(); + + u128s.sort(); + u128s.dedup(); + + frs.sort(); + frs.dedup(); + (u32s, u64s, u128s, frs) } @@ -2213,5 +2243,7 @@ pub fn mpt_update_keys(proofs: &[Proof]) -> Vec { keys.push(proof.claim.old_root); keys.push(proof.claim.new_root); } + keys.sort(); + keys.dedup(); keys } diff --git a/src/gadgets/poseidon.rs b/src/gadgets/poseidon.rs index 2586490e..c216643e 100644 --- a/src/gadgets/poseidon.rs +++ b/src/gadgets/poseidon.rs @@ -7,6 +7,9 @@ use halo2_proofs::{ #[cfg(test)] use hash_circuit::hash::Hashable; +#[cfg(test)] +const MAX_POSEIDON_ROWS: usize = 200; + /// Lookup represent the poseidon table in zkevm circuit pub trait PoseidonLookup { fn lookup_columns(&self) -> (FixedColumn, [AdviceColumn; 6]) { @@ -48,6 +51,9 @@ impl PoseidonTable { } pub fn load(&self, region: &mut Region<'_, Fr>, hash_traces: &[([Fr; 2], Fr, Fr)]) { + // The test poseidon table starts assigning from the first row, which has a disabled + // selector, but this is fine because the poseidon_lookup in the ConstraintBuilder + // doesn't include the mpt circuit's selector column. for (offset, hash_trace) in hash_traces.iter().enumerate() { assert!( Hashable::hash_with_domain([hash_trace.0[0], hash_trace.0[1]], hash_trace.1) @@ -67,6 +73,12 @@ impl PoseidonTable { } self.q_enable.assign(region, offset, Fr::one()); } + + // We need to do this so that the fixed columns in the tests will not depend on the + // number of poseidon hashes that are looked up. + for offset in hash_traces.len()..MAX_POSEIDON_ROWS { + self.q_enable.assign(region, offset, Fr::one()); + } } } diff --git a/src/mpt.rs b/src/mpt.rs index 08c8c50c..12ce206c 100644 --- a/src/mpt.rs +++ b/src/mpt.rs @@ -141,13 +141,8 @@ impl MptCircuitConfig { keys.len() ); - self.canonical_representation.assign( - &mut region, - randomness, - keys.iter() - .chain(std::iter::repeat(&Fr::zero())) - .take(total_rep_size), - ); + self.canonical_representation + .assign(&mut region, randomness, &keys, n_rows); self.key_bit.assign(&mut region, &key_bit_lookups(proofs)); self.byte_bit.assign(&mut region); self.byte_representation.assign( @@ -180,4 +175,22 @@ impl MptCircuitConfig { pub fn lookup_exprs(&self, meta: &mut VirtualCells<'_, F>) -> [Expression; 8] { self.mpt_update.lookup().map(|q| q.run(meta)) } + + /// The number of minimum number of rows required for the mpt circuit. + pub fn n_rows_required(proofs: &[Proof]) -> usize { + let (u32s, u64s, u128s, frs) = byte_representations(proofs); + + // +1 for the final padding row to satisfy the "final mpt update is padding" constraint. + 1 + *[ + MptUpdateConfig::n_rows_required(proofs), + CanonicalRepresentationConfig::n_rows_required(&mpt_update_keys(proofs)), + KeyBitConfig::n_rows_required(&key_bit_lookups(proofs)), + // TODO: move rlc lookup for frs into CanonicalRepresentationConfig. + ByteRepresentationConfig::n_rows_required(&u32s, &u64s, &u128s, &frs), + ByteBitGadget::n_rows_required(), + ] + .iter() + .max() + .unwrap() + } } diff --git a/src/tests.rs b/src/tests.rs index 91855616..0ea9df52 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -6,10 +6,12 @@ use ethers_core::types::{Address, U256}; use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner}, dev::MockProver, - halo2curves::bn256::Fr, - plonk::{Circuit, ConstraintSystem, Error, FirstPhase}, + halo2curves::bn256::{Bn256, Fr}, + plonk::{keygen_vk, Circuit, ConstraintSystem, Error, FirstPhase}, + poly::kzg::commitment::ParamsKZG, }; use mpt_zktrie::state::{builder::HASH_SCHEME_DONE, witness::WitnessGenerator, ZktrieState}; +use rand_chacha::rand_core::SeedableRng; const N_ROWS: usize = 8 * 256 + 1; const STORAGE_ADDRESS: Address = Address::repeat_byte(1); @@ -115,6 +117,34 @@ fn degree() { assert_eq!(meta.degree(), 9); } +#[test] +fn verifying_key_constant() { + let params = ParamsKZG::::setup(17, rand_chacha::ChaCha20Rng::seed_from_u64(2)); + + let no_updates = TestCircuit::new(N_ROWS, vec![]); + let one_update = TestCircuit::new( + N_ROWS, + vec![( + MPTProofType::BalanceChanged, + serde_json::from_str(&include_str!( + "traces/empty_account_type_1_balance_update.json" + )) + .unwrap(), + )], + ); + let vk_no_updates = keygen_vk(¶ms, &no_updates).unwrap(); + let vk_one_update = keygen_vk(¶ms, &one_update).unwrap(); + + assert_eq!( + vk_no_updates.fixed_commitments(), + vk_one_update.fixed_commitments() + ); + assert_eq!( + vk_no_updates.permutation().commitments(), + vk_one_update.permutation().commitments() + ); +} + #[test] fn all_padding() { mock_prove(vec![]); @@ -1046,3 +1076,35 @@ fn create_name_registrator_per_txs_not_enough_gas_d0_g0_v0() { .unwrap(), ); } + +#[test] +fn test_n_rows_required() { + assert!(*HASH_SCHEME_DONE); + let mut generator = WitnessGenerator::from(&ZktrieState::default()); + generator.handle_new_state( + mpt_zktrie::mpt_circuits::MPTProofType::BalanceChanged, + Address::repeat_byte(1), + U256::from(23), + U256::zero(), + None, + ); + + let trace = generator.handle_new_state( + mpt_zktrie::mpt_circuits::MPTProofType::AccountDoesNotExist, + Address::repeat_byte(2), + U256::zero(), + U256::zero(), + None, + ); + let json = serde_json::to_string_pretty(&trace).unwrap(); + let trace: SMTTrace = serde_json::from_str(&json).unwrap(); + + let witness = vec![(MPTProofType::AccountDoesNotExist, trace); 3000]; + let proofs: Vec<_> = witness.clone().into_iter().map(Proof::from).collect(); + + let n_rows_required = MptCircuitConfig::n_rows_required(&proofs); + + let circuit = TestCircuit::new(n_rows_required, witness); + let prover = MockProver::::run(14, &circuit, vec![]).unwrap(); + assert_eq!(prover.verify(), Ok(())); +}