From e0b5efab0e2fe0c3f82bb07483eeef9d6bc58f63 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Thu, 7 Sep 2023 14:20:25 -0700 Subject: [PATCH] wip: data commitment doesn't match --- Cargo.lock | 262 +++++++++++++++++++++++++++++++++++----------- Cargo.toml | 4 +- src/commitment.rs | 116 ++++++++++++++++++-- src/fixture.rs | 14 +-- src/inputs.rs | 50 ++++++++- src/signature.rs | 1 - 6 files changed, 363 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd3c7acf..0fd75e73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.5.0" @@ -151,7 +166,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -228,6 +243,15 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -315,9 +339,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" dependencies = [ "serde", ] @@ -420,11 +444,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.27" +version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f56b4c72906975ca04becb8a30e102dfecddd0c06181e3e95ddc444be28881f8" +checksum = "defd4e7873dbddba6c7c91e199c7fcb946abc4a6a4ac3195400bcfb01b5de877" dependencies = [ + "android-tzdata", + "iana-time-zone", "num-traits", + "serde", + "windows-targets", ] [[package]] @@ -439,20 +467,19 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.1" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c8d502cbaec4595d2e7d5f61e318f05417bd2b66fdc3809498f0d3fdf0bea27" +checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6" dependencies = [ "clap_builder", "clap_derive", - "once_cell", ] [[package]] name = "clap_builder" -version = "4.4.1" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5891c7bc0edb3e1c2204fc5e94009affabeb1821c9e5fdc3959536c5c0bb984d" +checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ "anstream", "anstyle", @@ -462,14 +489,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.4.0" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9fd1a5729c4548118d7d70ff234a44868d00489a4b6597b0b020918a0e91a1a" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -667,9 +694,9 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[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", @@ -699,9 +726,10 @@ dependencies = [ [[package]] name = "curta" version = "0.1.0" -source = "git+https://github.com/succinctlabs/curta.git#72155615ba80464f3524c48ce31ec2f931987214" +source = "git+https://github.com/succinctlabs/curta.git?branch=john/nightly#b64dab8c1db6a3d014eed5770f7e9a21b87d0c49" dependencies = [ "anyhow", + "bincode", "hex", "itertools 0.10.5", "log", @@ -735,7 +763,7 @@ source = "git+https://github.com/succinctlabs/curve25519-dalek.git?branch=featur dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -751,6 +779,41 @@ dependencies = [ "zeroize", ] +[[package]] +name = "darling" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.31", +] + +[[package]] +name = "darling_macro" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.31", +] + [[package]] name = "data-encoding" version = "2.4.0" @@ -772,6 +835,9 @@ name = "deranged" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946" +dependencies = [ + "serde", +] [[package]] name = "derive_more" @@ -1142,7 +1208,7 @@ dependencies = [ "reqwest", "serde", "serde_json", - "syn 2.0.29", + "syn 2.0.31", "toml", "walkdir", ] @@ -1160,7 +1226,7 @@ dependencies = [ "proc-macro2", "quote", "serde_json", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1186,7 +1252,7 @@ dependencies = [ "serde", "serde_json", "strum", - "syn 2.0.29", + "syn 2.0.31", "tempfile", "thiserror", "tiny-keccak", @@ -1514,7 +1580,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1797,6 +1863,35 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.4.0" @@ -1859,6 +1954,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown 0.12.3", + "serde", ] [[package]] @@ -1869,6 +1965,7 @@ checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" dependencies = [ "equivalent", "hashbrown 0.14.0", + "serde", ] [[package]] @@ -2074,9 +2171,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.2" +version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5486aed0026218e61b8a01d5fbd5a0a134649abb71a0e53b7bc088529dced86e" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "memoffset" @@ -2260,14 +2357,14 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] name = "object" -version = "0.32.0" +version = "0.32.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ac5bbd07aea88c60a577a1ce218075ffd59208b2d7ca97adf9bfc5aeb21ebe" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" dependencies = [ "memchr", ] @@ -2332,7 +2429,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2343,9 +2440,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.92" +version = "0.9.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db7e971c2c2bba161b2d2fdf37080177eff520b3bc044787c7f1f5f9e78d869b" +checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d" dependencies = [ "cc", "libc", @@ -2518,7 +2615,7 @@ dependencies = [ "phf_shared 0.11.2", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2556,7 +2653,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2596,7 +2693,7 @@ checksum = "4503fa043bf02cee09a9582e9554b4c6403b2ef55e4612e96561d294419429f8" [[package]] name = "plonky2" version = "0.1.4" -source = "git+https://github.com/mir-protocol/plonky2.git#760f09a8aa0d77a081dbcc987cdec5e008471232" +source = "git+https://github.com/mir-protocol/plonky2.git#e6ca46062384ee7f113f52e06c1c828cdc4b9106" dependencies = [ "ahash", "anyhow", @@ -2651,7 +2748,7 @@ checksum = "5696e2e2a6bb5c48a6e33fb0dd4d20d0a9472784b709964f337f224e99bd6d06" [[package]] name = "plonky2x" version = "0.1.0" -source = "git+ssh://git@github.com/succinctlabs/succinctx.git#40183060085ac14f0d0e60f9a5c641cfee255f8f" +source = "git+ssh://git@github.com/succinctlabs/succinctx.git#7253e81c69932409d2a50556c5c7ce7aa3839f3f" dependencies = [ "anyhow", "array-macro", @@ -2670,6 +2767,7 @@ dependencies = [ "reqwest", "serde", "serde_json", + "serde_with", "sha2 0.10.7", "tokio", ] @@ -2688,12 +2786,12 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" [[package]] name = "prettyplease" -version = "0.2.12" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c64d9ba0963cdcea2e1b2230fbae2bab30eb25a174be395c41e764bfb65dd62" +checksum = "8832c0f9be7e3cae60727e6256cfd2cd3c3e2b6cd5dad4190ecb2fd658c9030b" dependencies = [ "proc-macro2", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2899,9 +2997,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" dependencies = [ "aho-corasick", "memchr", @@ -2911,9 +3009,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" dependencies = [ "aho-corasick", "memchr", @@ -3047,9 +3145,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.10" +version = "0.38.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6248e1caa625eb708e266e06159f135e8c26f2bb7ceb72dc4b2766d0340964" +checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453" dependencies = [ "bitflags 2.4.0", "errno", @@ -3285,7 +3383,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -3307,7 +3405,7 @@ checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -3331,6 +3429,35 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ca3b16a3d82c4088f343b7480a93550b3eabe1a358569c2dfe38bbcead07237" +dependencies = [ + "base64 0.21.3", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.0.0", + "serde", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e6be15c453eb305019bfa438b1593c731f36a289a7853f7707ee29e870b3b3c" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.31", +] + [[package]] name = "sha1" version = "0.10.5" @@ -3531,7 +3658,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -3588,9 +3715,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.29" +version = "2.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398" dependencies = [ "proc-macro2", "quote", @@ -3618,9 +3745,9 @@ dependencies = [ [[package]] name = "tendermint" -version = "0.33.0" +version = "0.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6d550db02d6bec4ebcbbebc4301ec22181bc489c37fb3f167e64b14c1be8321" +checksum = "c293cdbb6e13bad728bce26584a3a89d7cb5ea0b4b9db796fded10bae4a0b06b" dependencies = [ "bytes", "digest 0.10.7", @@ -3647,9 +3774,9 @@ dependencies = [ [[package]] name = "tendermint-proto" -version = "0.33.0" +version = "0.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1834fa2eb884ba69b9c0eea55f0178270bed421217596ca4e54c19ef75dcb660" +checksum = "03dcd29922e5670af2307f3d8e58c9720ec94b15b92ef85fe9ed0f4b25fbde44" dependencies = [ "bytes", "flex-error", @@ -3685,22 +3812,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.47" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" +checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.47" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" +checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -3782,7 +3909,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -3894,7 +4021,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -4050,9 +4177,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", @@ -4094,7 +4221,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", "wasm-bindgen-shared", ] @@ -4128,7 +4255,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4195,6 +4322,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -4331,7 +4467,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 960ebc32..67eb0e2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.71" clap = { version = "4.3.18", features = ["derive"] } -curta = { git = "https://github.com/succinctlabs/curta.git" } +curta = { git = "https://github.com/succinctlabs/curta.git", branch = "john/nightly" } digest = "0.10.7" dotenv = "0.15.0" ed25519-consensus = "2.1.0" @@ -24,4 +24,4 @@ sha2 = "0.10.7" subtle-encoding = "0.5.1" tendermint = "0.33.0" tendermint-proto = "0.33.0" -tokio = { version="1.29.1", features = ["full"]} +tokio = { version = "1.29.1", features = ["full"] } diff --git a/src/commitment.rs b/src/commitment.rs index 50fb20dd..9b921166 100644 --- a/src/commitment.rs +++ b/src/commitment.rs @@ -13,6 +13,7 @@ use curta::math::extension::cubic::parameters::CubicParameters; use plonky2::field::extension::Extendable; use plonky2::iop::target::BoolTarget; use plonky2::iop::target::Target; +use plonky2::plonk::config::{AlgebraicHasher, GenericConfig}; use plonky2::{hash::hash_types::RichField, plonk::circuit_builder::CircuitBuilder}; use plonky2x::frontend::ecc::ed25519::curve::curve_types::Curve; use plonky2x::frontend::ecc::ed25519::curve::ed25519::Ed25519; @@ -96,13 +97,16 @@ pub trait CelestiaCommitment, const D: usize> { /// Compute the data commitment from the data hashes and block heights. WINDOW_RANGE is the number of blocks in the data commitment. NUM_LEAVES is the number of leaves in the tree for the data commitment. fn get_data_commitment< E: CubicParameters, + C: GenericConfig + 'static, const WINDOW_RANGE: usize, const NUM_LEAVES: usize, >( &mut self, data_hashes: &Vec, block_heights: &Vec, - ) -> TendermintHashTarget; + ) -> TendermintHashTarget + where + >::Hasher: AlgebraicHasher; } impl, const D: usize> CelestiaCommitment @@ -173,7 +177,7 @@ impl, const D: usize> CelestiaCommitment leaf: &[BoolTarget; LEAF_SIZE_BITS], ) -> TendermintHashTarget { // NUM_BYTES must be a multiple of 32 - assert_eq!(NUM_BYTES % 32, 0); + assert_eq!(NUM_BYTES % 64, 0); // Calculate the message for the leaf hash. let mut leaf_msg_bits = [self._false(); LEAF_SIZE_BITS_PLUS_8]; @@ -343,13 +347,17 @@ impl, const D: usize> CelestiaCommitment fn get_data_commitment< E: CubicParameters, + C: GenericConfig + 'static, const WINDOW_RANGE: usize, const NUM_LEAVES: usize, >( &mut self, data_hashes: &Vec, block_heights: &Vec, - ) -> TendermintHashTarget { + ) -> TendermintHashTarget + where + >::Hasher: AlgebraicHasher, + { let mut gadget: SHA256BuilderGadget = self.init_sha256(); let mut leaves = vec![TendermintHashTarget([self._false(); HASH_SIZE_BITS]); NUM_LEAVES]; @@ -358,7 +366,7 @@ impl, const D: usize> CelestiaCommitment // Encode the data hash and height into a tuple. let data_root_tuple = self.encode_data_root_tuple(&data_hashes[i], &block_heights[i]); - const DATA_TUPLE_ROOT_SIZE_BITS: usize = HASH_SIZE_BITS * 2; + const DATA_TUPLE_ROOT_SIZE_BITS: usize = 64 * 8; const DATA_TUPLE_ROOT_SIZE_BITS_PLUS_8: usize = DATA_TUPLE_ROOT_SIZE_BITS + 8; // Number of bytes in the padded message for SHA256. @@ -371,9 +379,16 @@ impl, const D: usize> CelestiaCommitment leaves[i] = leaf_hash; leaf_enabled[i] = self._true(); } - for i in WINDOW_RANGE..NUM_LEAVES { - leaf_enabled[i] = self._false(); - } + + // Fill out the first SHA256 gadget with empty leaves. + // First chunk is 800 SHA-chunks + // Fill out 1024 - 800 = 224 SHA-chunks + let num_chunks_left = 224; + fill_out_sha_gadget::(self, &mut gadget, num_chunks_left); + self.constrain_sha256_gadget::(gadget); + + let mut gadget: SHA256BuilderGadget = self.init_sha256(); + // Hash each of the validators to get their corresponding leaf hash. let mut current_nodes = leaves.clone(); @@ -393,14 +408,37 @@ impl, const D: usize> CelestiaCommitment merkle_layer_size /= 2; } + // If NUM_LEAVES=512, then we have 1024 - (511 * 2) = 2 SHA-chunks left. + // Each inner_hash_stark is 2 SHA chunks + let num_chunks_left = 2; + fill_out_sha_gadget::(self, &mut gadget, num_chunks_left); + self.constrain_sha256_gadget::(gadget); + // Return the root hash. current_nodes[0] } } +fn fill_out_sha_gadget, E: CubicParameters, const D: usize>( + builder: &mut CircuitBuilder, + gadget: &mut SHA256BuilderGadget, + num_chunks_left: usize, +) { + let zero = builder.zero(); + let bytes = CurtaBytes(builder.add_virtual_target_arr::<64>()); + for i in 0..64 { + builder.connect(bytes.0[i], zero); + } + + for _ in 0..num_chunks_left { + builder.sha256(&bytes, gadget); + } +} + #[cfg(test)] pub(crate) mod tests { use super::*; + use curta::math::goldilocks::cubic::GoldilocksCubicParameters; use plonky2::{ iop::witness::{PartialWitness, WitnessWrite}, plonk::{ @@ -409,10 +447,11 @@ pub(crate) mod tests { config::{GenericConfig, PoseidonGoldilocksConfig}, }, }; + use subtle_encoding::hex; use crate::{ commitment::CelestiaCommitment, - inputs::get_path_indices, + inputs::{generate_data_commitment_inputs, get_path_indices}, utils::{ f_bits_to_bytes, generate_proofs_from_header, hash_all_leaves, leaf_hash, to_be_bits, I64Target, MarshalledValidatorTarget, TendermintHashTarget, HASH_SIZE_BITS, @@ -423,9 +462,68 @@ pub(crate) mod tests { type C = PoseidonGoldilocksConfig; type F = >::F; + type E = GoldilocksCubicParameters; type Curve = Ed25519; const D: usize = 2; - const VALIDATOR_SET_SIZE_MAX: usize = 4; + + const WINDOW_SIZE: usize = 400; + const NUM_LEAVES: usize = 512; + + #[test] + fn test_data_commitment() { + let mut pw = PartialWitness::new(); + let config = CircuitConfig::standard_recursion_config(); + let mut builder = CircuitBuilder::::new(config); + + const START_BLOCK: usize = 3800; + const END_BLOCK: usize = START_BLOCK + WINDOW_SIZE; + + let inputs = generate_data_commitment_inputs(START_BLOCK, END_BLOCK); + + let mut data_hashes_targets = Vec::new(); + let mut block_heights_targets = Vec::new(); + for i in 0..WINDOW_SIZE { + let mut data_hash_target = TendermintHashTarget([builder._false(); HASH_SIZE_BITS]); + + let data_hash_bits = to_be_bits(inputs.data_hashes[i].into()); + for j in 0..HASH_SIZE_BITS { + data_hash_target.0[j] = builder.constant_bool(data_hash_bits[j]); + } + + let block_height = builder.constant_u32((START_BLOCK + i) as u32); + + data_hashes_targets.push(data_hash_target); + block_heights_targets.push(block_height); + } + + let root_hash_target = builder.get_data_commitment::( + &data_hashes_targets, + &block_heights_targets, + ); + + println!( + "Expected data commitment root: {:?}", + String::from_utf8(hex::encode(inputs.data_commitment_root)).unwrap() + ); + + let expected_data_commitment_bits = to_be_bits(inputs.data_commitment_root.into()); + + println!( + "Expected data commitment root bits: {:?}", + expected_data_commitment_bits + ); + + for i in 0..HASH_SIZE_BITS { + pw.set_bool_target(root_hash_target.0[i], expected_data_commitment_bits[i]); + } + + let data = builder.build::(); + let proof = data.prove(pw).unwrap(); + + data.verify(proof).unwrap(); + + println!("Verified proof"); + } #[test] fn test_encode_data_root_tuple() { diff --git a/src/fixture.rs b/src/fixture.rs index 0d8ca426..1a6ab67a 100644 --- a/src/fixture.rs +++ b/src/fixture.rs @@ -40,11 +40,11 @@ struct VerifySignatureData { } #[derive(Debug, Serialize, Deserialize)] -struct DataCommitmentFixture { - start_block: u64, - end_block: u64, - data_hashes: Vec, - data_commitment: Hash, +pub struct DataCommitmentFixture { + pub start_block: u64, + pub end_block: u64, + pub data_hashes: Vec, + pub data_commitment: Hash, } pub fn encode_block_height(block_height: u64) -> Vec { @@ -294,9 +294,9 @@ pub(crate) mod tests { use sha2::Sha256; #[tokio::test] - async fn test_data_commitment() { + async fn calculate_data_commitment() { // End exclusive range: https://github.com/celestiaorg/celestia-core/blob/main/rpc/core/blocks.go#L537-L538 - generate_data_commitment(4000, 4001).await + generate_data_commitment(3800, 4200).await } #[test] diff --git a/src/inputs.rs b/src/inputs.rs index 948e15cb..064c5ca2 100644 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -1,5 +1,6 @@ use std::fs; +use crate::fixture::DataCommitmentFixture; /// Source (tendermint-rs): https://github.com/informalsystems/tendermint-rs/blob/e930691a5639ef805c399743ac0ddbba0e9f53da/tendermint/src/merkle.rs#L32 use crate::utils::{ compute_hash_from_aunts, generate_proofs_from_header, leaf_hash, non_absent_vote, SignedBlock, @@ -8,7 +9,7 @@ use crate::utils::{ use ed25519_consensus::SigningKey; use sha2::Sha256; use tendermint::crypto::ed25519::VerificationKey; -use tendermint::{private_key, Signature}; +use tendermint::{private_key, Hash, Signature}; use tendermint::{validator::Set as ValidatorSet, vote::SignedVote, vote::ValidatorIndex}; use tendermint_proto::types::BlockId as RawBlockId; use tendermint_proto::Protobuf; @@ -69,6 +70,13 @@ pub struct CelestiaSkipBlockProof { pub base: CelestiaBaseBlockProof, } +#[derive(Debug, Clone)] +pub struct CelestiaDataCommitmentProofInputs { + pub data_hashes: Vec, + pub block_heights: Vec, + pub data_commitment_root: Hash, +} + // If hash_so_far is on the left, False, else True pub fn get_path_indices(index: u64, total: u64) -> Vec { let mut path_indices = vec![]; @@ -110,6 +118,43 @@ fn get_signed_block(block: usize) -> Box { block } +fn get_data_commitment_fixture(start_block: usize, end_block: usize) -> DataCommitmentFixture { + let mut file = String::new(); + file.push_str("./src/fixtures/mocha-4/"); + file.push_str(&start_block.to_string()); + file.push_str("-"); + file.push_str(&end_block.to_string()); + file.push_str("/data_commitment.json"); + + let file_content = fs::read_to_string(file.as_str()); + + DataCommitmentFixture::from( + serde_json::from_str::(&file_content.unwrap()) + .expect("failed to parse json"), + ) +} + +/// Generate the inputs for a skip proof from a trusted_block to block. +pub fn generate_data_commitment_inputs( + start_block: usize, + end_block: usize, +) -> CelestiaDataCommitmentProofInputs { + // Generate test cases from data commitment fixture + let fixture = get_data_commitment_fixture(start_block, end_block); + + let mut block_heights = Vec::new(); + for i in start_block..end_block { + block_heights.push(i as u32); + } + + CelestiaDataCommitmentProofInputs { + data_hashes: fixture.data_hashes, + block_heights, + data_commitment_root: fixture.data_commitment, + } +} + +/// Generate the base inputs for a proof of a Celestia block (to be used by the skip or step circuits). fn generate_base_inputs( block: &Box, ) -> CelestiaBaseBlockProof { @@ -240,6 +285,7 @@ fn generate_base_inputs( celestia_block_proof } +/// Generate the inputs for a step proof for consecutive Celestia blocks. pub fn generate_step_inputs( block_number: usize, ) -> CelestiaStepBlockProof { @@ -359,7 +405,7 @@ fn update_present_on_trusted_header( ); } -// Where block is the block we want to generate inputs for, and trusted_block is the block we're skipping from +/// Generate the inputs for a skip proof from a trusted_block to block. pub fn generate_skip_inputs( trusted_block: usize, block: usize, diff --git a/src/signature.rs b/src/signature.rs index b9db4f11..654141ed 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -25,7 +25,6 @@ use plonky2x::frontend::ecc::ed25519::gadgets::eddsa::{ verify_signatures_circuit, EDDSAPublicKeyTarget, EDDSASignatureTarget, }; use plonky2x::frontend::num::nonnative::nonnative::CircuitBuilderNonNative; -use plonky2x::prelude::GoldilocksField; use crate::utils::to_be_bits; use crate::utils::{