From bbbac5aec9520052c3921fb272ee0fe79fd4b73d Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 29 Oct 2025 12:49:54 +0900 Subject: [PATCH 01/15] Use nested crt --- Cargo.lock | 321 +++++++----------- Cargo.toml | 2 +- abe/Cargo.toml | 2 +- ...l => secpar_120_height_1_l1_6.params.toml} | 9 +- .../secpar_120_height_1_limb_10.params.toml | 11 - .../secpar_120_height_1_limb_11.params.toml | 11 - .../secpar_120_height_1_limb_12.params.toml | 11 - .../secpar_120_height_1_limb_13.params.toml | 11 - .../secpar_120_height_1_limb_14.params.toml | 11 - .../secpar_120_height_1_limb_15.params.toml | 11 - .../secpar_120_height_1_limb_16.params.toml | 11 - .../secpar_120_height_1_limb_2.params.toml | 11 - .../secpar_120_height_1_limb_3.params.toml | 11 - .../secpar_120_height_1_limb_4.params.toml | 11 - .../secpar_120_height_1_limb_5.params.toml | 11 - .../secpar_120_height_1_limb_6.params.toml | 11 - .../secpar_120_height_1_limb_7.params.toml | 11 - .../secpar_120_height_1_limb_8.params.toml | 11 - .../secpar_120_height_1_limb_9.params.toml | 11 - abe/src/config.rs | 6 +- abe/src/main.rs | 66 ++-- src/abe.rs | 101 ++++-- src/simulator.rs | 109 ++++++ 23 files changed, 336 insertions(+), 445 deletions(-) rename abe/sim_configs/{secpar_120_height_1_limb_1.params.toml => secpar_120_height_1_l1_6.params.toml} (60%) delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_10.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_11.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_12.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_13.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_14.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_15.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_16.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_2.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_3.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_4.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_5.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_6.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_7.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_8.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_limb_9.params.toml diff --git a/Cargo.lock b/Cargo.lock index b84c811..0cb94a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,26 +20,11 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "addr2line" -version = "0.25.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler2" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" - [[package]] name = "aho-corasick" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" dependencies = [ "memchr", ] @@ -129,26 +114,11 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" -[[package]] -name = "backtrace" -version = "0.3.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-link", -] - [[package]] name = "bigdecimal" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a22f228ab7a1b23027ccc6c350b72868017af7ea8356fbdf19f8d991c690013" +checksum = "560f42649de9fa436b73517378a147ec21f6c997a546581df4b4b31677828934" dependencies = [ "autocfg", "libm", @@ -179,9 +149,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.9.4" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "bitvec" @@ -212,9 +182,9 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "cc" -version = "1.2.41" +version = "1.2.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac9fe6cdbb24b6ade63616c0a0688e45bb56732262c158df3c0c4bea4ca47cb7" +checksum = "739eb0f94557554b3ca9a86d2d37bebd49c5e6d0c1d2bda35ba5bdac830befc2" dependencies = [ "find-msvc-tools", "shlex", @@ -222,9 +192,9 @@ dependencies = [ [[package]] name = "cfg-if" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "chrono" @@ -241,9 +211,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.48" +version = "4.5.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2134bb3ea021b78629caa971416385309e0131b351b25e01dc16fb54e1b5fae" +checksum = "0c2cfd7bf8a6017ddaa4e32ffe7403d547790db06bd171c1c53926faab501623" dependencies = [ "clap_builder", "clap_derive", @@ -251,9 +221,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.48" +version = "4.5.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2ba64afa3c0a6df7fa517765e31314e983f51dda798ffba27b988194fb65dc9" +checksum = "0a4c05b9e80c5ccd3a7ef080ad7b6ba7d6fc00a985b8b157197075677c82c7a0" dependencies = [ "anstream", "anstyle", @@ -263,9 +233,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.47" +version = "4.5.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbfd7eae0b0f1a6e63d4b13c9c478de77c2eb546fba158ad50b4203dc24b9f9c" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" dependencies = [ "heck", "proc-macro2", @@ -275,15 +245,15 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.5" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" [[package]] name = "codespan-reporting" -version = "0.12.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe6d2e5af09e8c8ad56c969f2157a3d4238cebc7c55f0a517728c38f7b200f81" +checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" dependencies = [ "serde", "termcolor", @@ -339,9 +309,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.186" +version = "1.0.187" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e9c4fe7f2f5dc5c62871a1b43992d197da6fa1394656a94276ac2894a90a6fe" +checksum = "d8465678d499296e2cbf9d3acf14307458fd69b471a31b65b3c519efe8b5e187" dependencies = [ "cc", "cxx-build", @@ -354,9 +324,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.186" +version = "1.0.187" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5cf2909d37d80633ddd208676fc27c2608a7f035fff69c882421168038b26dd" +checksum = "d74b6bcf49ebbd91f1b1875b706ea46545032a14003b5557b7dfa4bbeba6766e" dependencies = [ "cc", "codespan-reporting", @@ -369,9 +339,9 @@ dependencies = [ [[package]] name = "cxxbridge-cmd" -version = "1.0.186" +version = "1.0.187" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "077f5ee3d3bfd8d27f83208fdaa96ddd50af7f096c77077cc4b94da10bfacefd" +checksum = "94ca2ad69673c4b35585edfa379617ac364bccd0ba0adf319811ba3a74ffa48a" dependencies = [ "clap", "codespan-reporting", @@ -383,20 +353,19 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.186" +version = "1.0.187" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0108748615125b9f2e915dfafdffcbdabbca9b15102834f6d7e9a768f2f2864" +checksum = "d29b52102aa395386d77d322b3a0522f2035e716171c2c60aa87cc5e9466e523" [[package]] name = "cxxbridge-macro" -version = "1.0.186" +version = "1.0.187" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e896681ef9b8dc462cfa6961d61909704bde0984b30bcb4082fe102b478890" +checksum = "2a8ebf0b6138325af3ec73324cb3a48b64d57721f17291b151206782e61f66cd" dependencies = [ "indexmap", "proc-macro2", "quote", - "rustversion", "syn", ] @@ -432,9 +401,9 @@ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "env_filter" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2" dependencies = [ "log", "regex", @@ -495,9 +464,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "generic-array" -version = "0.14.7" +version = "0.14.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +checksum = "4bb6743198531e02858aeaea5398fcc883e71851fcbcb5a2f773e2fb6cb1edf2" dependencies = [ "typenum", "version_check", @@ -505,21 +474,21 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", "r-efi", - "wasi 0.14.7+wasi-0.2.4", + "wasip2", ] [[package]] -name = "gimli" -version = "0.32.3" +name = "hamming" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" +checksum = "65043da274378d68241eb9a8f8f8aa54e349136f7b8e12f63e3ef44043cc30e1" [[package]] name = "hashbrown" @@ -565,30 +534,19 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.11.4" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5" +checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" dependencies = [ "equivalent", "hashbrown 0.16.0", ] -[[package]] -name = "io-uring" -version = "0.7.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "046fa2d4d00aea763528b4950358d0ead425372445dc8ff86312b3c69ff7727b" -dependencies = [ - "bitflags", - "cfg-if", - "libc", -] - [[package]] name = "is_terminal_polyfill" -version = "1.70.1" +version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" [[package]] name = "itertools" @@ -631,9 +589,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.81" +version = "0.3.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305" +checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65" dependencies = [ "once_cell", "wasm-bindgen", @@ -722,30 +680,10 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "miniz_oxide" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" -dependencies = [ - "adler2", -] - -[[package]] -name = "mio" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" -dependencies = [ - "libc", - "wasi 0.11.1+wasi-snapshot-preview1", - "windows-sys 0.59.0", -] - [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git#d81081fee29d1853e38f714ed725c545ebf45952" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fnested_crt#c2e32d8fb25288a13b7d712cb357a486d5f435ca" dependencies = [ "bigdecimal", "bincode", @@ -758,6 +696,7 @@ dependencies = [ "num-bigint", "num-traits", "openfhe", + "primal", "rand 0.9.2", "rayon", "serde", @@ -808,15 +747,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "object" -version = "0.37.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.21.3" @@ -825,9 +755,9 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "once_cell_polyfill" -version = "1.70.1" +version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "openfhe" @@ -883,11 +813,57 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "primal" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e5f354948532e6017fc91f9a5ff5ba1be0dabd3a0c9e9c417969cd4c1ad6e8" +dependencies = [ + "primal-check", + "primal-estimate", + "primal-sieve", +] + +[[package]] +name = "primal-bit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "252429dbb8aeacc3233df500dc3a6a367bf28eb3a711272884d7540a7b636055" +dependencies = [ + "hamming", +] + +[[package]] +name = "primal-check" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08" +dependencies = [ + "num-integer", +] + +[[package]] +name = "primal-estimate" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a432100a0b3a61085e75b5f89e9f42de73c0acb7dea5038b893697918105d822" + +[[package]] +name = "primal-sieve" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e982796d82203351983d3602a8d6372d1d7894e86960047ba0d4b7426a5edd3" +dependencies = [ + "primal-bit", + "primal-estimate", + "smallvec", +] + [[package]] name = "proc-macro2" -version = "1.0.101" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] @@ -988,9 +964,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.12.1" +version = "1.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a52d8d02cacdb176ef4678de6c052efb4b3da14b78e4db683a4252762be5433" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" dependencies = [ "aho-corasick", "memchr", @@ -1000,9 +976,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "722166aa0d7438abbaa4d5cc2c649dac844e8c56d82fb3d33e9c34b5cd268fc6" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" dependencies = [ "aho-corasick", "memchr", @@ -1011,15 +987,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.7" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3160422bbd54dd5ecfdca71e5fd59b7b8fe2b1697ab2baf64f6d05dcc66d298" - -[[package]] -name = "rustc-demangle" -version = "0.1.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" [[package]] name = "rustix" @@ -1135,12 +1105,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "slab" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589" - [[package]] name = "smallvec" version = "1.15.1" @@ -1155,9 +1119,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.106" +version = "2.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917" dependencies = [ "proc-macro2", "quote", @@ -1243,24 +1207,19 @@ dependencies = [ [[package]] name = "tokio" -version = "1.47.1" +version = "1.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" +checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" dependencies = [ - "backtrace", - "io-uring", - "libc", - "mio", "pin-project-lite", - "slab", "tokio-macros", ] [[package]] name = "tokio-macros" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" dependencies = [ "proc-macro2", "quote", @@ -1375,9 +1334,9 @@ checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] name = "unicode-ident" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06" [[package]] name = "unicode-width" @@ -1415,21 +1374,6 @@ version = "0.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1" -[[package]] -name = "wasi" -version = "0.11.1+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" - -[[package]] -name = "wasi" -version = "0.14.7+wasi-0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "883478de20367e224c0090af9cf5f9fa85bed63a95c1abf3afc5c083ebc06e8c" -dependencies = [ - "wasip2", -] - [[package]] name = "wasip2" version = "1.0.1+wasi-0.2.4" @@ -1441,9 +1385,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.104" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d" +checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60" dependencies = [ "cfg-if", "once_cell", @@ -1452,25 +1396,11 @@ dependencies = [ "wasm-bindgen-shared", ] -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - [[package]] name = "wasm-bindgen-macro" -version = "0.2.104" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119" +checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1478,22 +1408,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.104" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7" +checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc" dependencies = [ + "bumpalo", "proc-macro2", "quote", "syn", - "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.104" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1" +checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76" dependencies = [ "unicode-ident", ] @@ -1575,15 +1505,6 @@ dependencies = [ "windows-targets 0.52.6", ] -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets 0.52.6", -] - [[package]] name = "windows-sys" version = "0.60.2" diff --git a/Cargo.toml b/Cargo.toml index acb3e7c..7cca793 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/nested_crt" } rand = { version = "0.9.0", features = ["std_rng"] } num-bigint = { version = "0.4", features = ["serde"] } thiserror = "2.0.16" diff --git a/abe/Cargo.toml b/abe/Cargo.toml index 6e91046..cd112b7 100644 --- a/abe/Cargo.toml +++ b/abe/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/nested_crt" } arithmetic-abe = { path = "../" } anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } diff --git a/abe/sim_configs/secpar_120_height_1_limb_1.params.toml b/abe/sim_configs/secpar_120_height_1_l1_6.params.toml similarity index 60% rename from abe/sim_configs/secpar_120_height_1_limb_1.params.toml rename to abe/sim_configs/secpar_120_height_1_l1_6.params.toml index 6aba0b5..ef25956 100644 --- a/abe/sim_configs/secpar_120_height_1_limb_1.params.toml +++ b/abe/sim_configs/secpar_120_height_1_l1_6.params.toml @@ -1,11 +1,12 @@ target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 +crt_bits = 24 +crt_depth_min = 20 +crt_depth_max = 60 base_bits_min = 17 base_bits_max = 17 log_dim_min = 15 log_dim_max = 15 num_eval_slots = 1 -limb_bit_size = 1 +l1_moduli_bits = 8 +scale = 8 height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_10.params.toml b/abe/sim_configs/secpar_120_height_1_limb_10.params.toml deleted file mode 100644 index 2e4fbfe..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_10.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 10 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_11.params.toml b/abe/sim_configs/secpar_120_height_1_limb_11.params.toml deleted file mode 100644 index 1fa5c6d..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_11.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 11 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_12.params.toml b/abe/sim_configs/secpar_120_height_1_limb_12.params.toml deleted file mode 100644 index 113cb81..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_12.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 12 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_13.params.toml b/abe/sim_configs/secpar_120_height_1_limb_13.params.toml deleted file mode 100644 index c3dcf4a..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_13.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 13 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_14.params.toml b/abe/sim_configs/secpar_120_height_1_limb_14.params.toml deleted file mode 100644 index fc55843..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_14.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 14 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_15.params.toml b/abe/sim_configs/secpar_120_height_1_limb_15.params.toml deleted file mode 100644 index 7d6f991..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_15.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 15 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_16.params.toml b/abe/sim_configs/secpar_120_height_1_limb_16.params.toml deleted file mode 100644 index 1808d85..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_16.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 16 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_2.params.toml b/abe/sim_configs/secpar_120_height_1_limb_2.params.toml deleted file mode 100644 index 5bc2711..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_2.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 2 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_3.params.toml b/abe/sim_configs/secpar_120_height_1_limb_3.params.toml deleted file mode 100644 index 6a1b62b..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_3.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 3 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_4.params.toml b/abe/sim_configs/secpar_120_height_1_limb_4.params.toml deleted file mode 100644 index 3b61d4f..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_4.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 4 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_5.params.toml b/abe/sim_configs/secpar_120_height_1_limb_5.params.toml deleted file mode 100644 index 97de0d5..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_5.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 5 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_6.params.toml b/abe/sim_configs/secpar_120_height_1_limb_6.params.toml deleted file mode 100644 index ab94fd9..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_6.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 6 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_7.params.toml b/abe/sim_configs/secpar_120_height_1_limb_7.params.toml deleted file mode 100644 index 6122203..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_7.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 7 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_8.params.toml b/abe/sim_configs/secpar_120_height_1_limb_8.params.toml deleted file mode 100644 index b59bc7f..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_8.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 8 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_limb_9.params.toml b/abe/sim_configs/secpar_120_height_1_limb_9.params.toml deleted file mode 100644 index 5ba92e3..0000000 --- a/abe/sim_configs/secpar_120_height_1_limb_9.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 1 -crt_depth_max = 1 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 -num_eval_slots = 1 -limb_bit_size = 9 -height = 1 diff --git a/abe/src/config.rs b/abe/src/config.rs index ce163aa..9c2da27 100644 --- a/abe/src/config.rs +++ b/abe/src/config.rs @@ -18,7 +18,8 @@ pub struct RunConfig { pub trapdoor_sigma: Option, /// bit size of the base for the gadget vector and decomposition pub base_bits: u32, - pub limb_bit_size: usize, + pub l1_moduli_bits: usize, + pub scale: u64, pub arith_input_size: usize, pub arith_height: u32, // #[serde( @@ -39,6 +40,7 @@ pub struct SimConfig { pub log_dim_min: u32, pub log_dim_max: u32, pub num_eval_slots: Option, - pub limb_bit_size: usize, + pub l1_moduli_bits: usize, + pub scale: u64, pub height: usize, } diff --git a/abe/src/main.rs b/abe/src/main.rs index 9ae2f02..d2815a0 100644 --- a/abe/src/main.rs +++ b/abe/src/main.rs @@ -5,18 +5,14 @@ use arithmetic_abe::{ abe::KeyPolicyABE, ciphertext::Ciphertext, keys::{FuncSK, MasterPK, MasterSK}, - simulator::bruteforce_params_for_bench_arith_circuit, + simulator::bruteforce_params_for_bench_nested_crt_circuit, }; use chrono::Local; use clap::{Parser, Subcommand}; use keccak_asm::Keccak256; use mxx::{ - arithmetic::circuit::ArithmeticCircuit, matrix::dcrt_poly::DCRTPolyMatrix, - poly::{ - PolyParams, - dcrt::{params::DCRTPolyParams, poly::DCRTPoly}, - }, + poly::{PolyParams, dcrt::params::DCRTPolyParams}, sampler::{ PolyTrapdoorSampler, hash::DCRTPolyHashSampler, trapdoor::DCRTPolyTrapdoorSampler, uniform::DCRTPolyUniformSampler, @@ -122,12 +118,13 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { log_dim_min, log_dim_max, num_eval_slots, - limb_bit_size, + l1_moduli_bits, + scale, height, } = config; log_mem(format!( - "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), num_eval_slots={:?}, limb_bit_size={}, height={}, config_prefix={}", + "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), num_eval_slots={:?}, l1_moduli_bits={}, scale = {}, height={}, config_prefix={}", target_secpar, crt_bits, crt_depth_min, @@ -137,19 +134,21 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { log_dim_min, log_dim_max, num_eval_slots, - limb_bit_size, + l1_moduli_bits, + scale, height, config_prefix )); - let params = bruteforce_params_for_bench_arith_circuit( + let params = bruteforce_params_for_bench_nested_crt_circuit( target_secpar, crt_bits, (crt_depth_min, crt_depth_max), (base_bits_min, base_bits_max), (log_dim_min, log_dim_max), config.num_eval_slots, - limb_bit_size, + l1_moduli_bits, + scale, height, ) .context("unable to find parameters for benchmark arithmetic circuit")?; @@ -180,7 +179,8 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { trapdoor_sigma: Some(4.578), base_bits, num_eval_slots: config.num_eval_slots, - limb_bit_size, + l1_moduli_bits, + scale, arith_input_size, arith_height, }; @@ -215,7 +215,8 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { DCRTPolyTrapdoorSampler, DCRTPolyUniformSampler, >::new( - config.limb_bit_size, + config.l1_moduli_bits, + config.scale, ¶ms, config.num_eval_slots, config.knapsack_size, @@ -224,18 +225,8 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { ); let mut t_setup = Duration::ZERO; let mut t_keygen = Duration::ZERO; - let num_eval_slots = config.num_eval_slots.unwrap_or(params.ring_dimension() as usize); log_mem("starting KeyPolicy ABE"); - log_mem("start building arithmetic circuit"); - let arith_circuit = ArithmeticCircuit::::benchmark_multiplication_tree( - ¶ms, - config.limb_bit_size, - num_eval_slots, - config.arith_height as usize, - false, - ); - log_mem("finished building arithmetic circuit"); // 1) setup log_mem("starting setup"); @@ -253,7 +244,15 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { log_mem("starting keygen"); let fsk: FuncSK = timed_read_async( "keygen", - || abe.keygen(params.clone(), mpk.clone(), msk.clone(), arith_circuit, dir_path.clone()), + || { + abe.keygen( + params.clone(), + mpk.clone(), + msk.clone(), + config.arith_height, + dir_path.clone(), + ) + }, &mut t_keygen, ) .await; @@ -282,7 +281,8 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { DCRTPolyTrapdoorSampler, DCRTPolyUniformSampler, >::new( - config.limb_bit_size, + config.l1_moduli_bits, + config.scale, ¶ms, config.num_eval_slots, config.knapsack_size, @@ -296,15 +296,6 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { let num_eval_slots = config.num_eval_slots.unwrap_or(params.ring_dimension() as usize); log_mem("starting KeyPolicy ABE"); - log_mem("start building arithmetic circuit"); - let arith_circuit = ArithmeticCircuit::::benchmark_multiplication_tree( - ¶ms, - config.limb_bit_size, - num_eval_slots, - config.arith_height as usize, - false, - ); - log_mem("finished building arithmetic circuit"); // 3) enc log_mem("starting enc"); @@ -357,8 +348,11 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { }, &mut t_read_fsk, ); - let bit: bool = - timed_read("dec", || abe.dec(params.clone(), ct, mpk, fsk, arith_circuit), &mut t_dec); + let bit: bool = timed_read( + "dec", + || abe.dec(params.clone(), ct, mpk, fsk, config.arith_height), + &mut t_dec, + ); log_mem(format!("finished decryption: result={}", bit)); Ok(()) } diff --git a/src/abe.rs b/src/abe.rs index 3cffb25..6995b36 100644 --- a/src/abe.rs +++ b/src/abe.rs @@ -2,18 +2,21 @@ use crate::{ ciphertext::Ciphertext, keys::{FuncSK, MasterPK, MasterSK}, }; +use log::info; use mxx::{ - arithmetic::circuit::ArithmeticCircuit, + // arithmetic::circuit::ArithmeticCircuit, bgg::{ encoding::BggEncoding, sampler::{BGGEncodingSampler, BGGPublicKeySampler}, }, + circuit::PolyCircuit, element::PolyElem, - gadgets::crt::encode_modulo_poly, - lookup::lwe_eval::LweBggEncodingPltEvaluator, + gadgets::arith::nested_crt::{NestedCrtPoly, NestedCrtPolyContext, encode_nested_crt_poly}, + lookup::lwe_eval::{LweBggEncodingPltEvaluator, LweBggPubKeyEvaluator}, matrix::PolyMatrix, poly::{Poly, PolyParams}, sampler::{DistType, PolyHashSampler, PolyTrapdoorSampler, PolyUniformSampler}, + storage::write::{init_storage_system, wait_for_all_writes}, }; use num_bigint::BigUint; use std::{marker::PhantomData, path::PathBuf, sync::Arc}; @@ -27,8 +30,9 @@ pub struct KeyPolicyABE< SU: PolyUniformSampler + Send + Sync, > { pub e_b_sigma: f64, - pub limb_bit_size: usize, - pub num_crt_limbs: usize, + pub l1_moduli_bits: usize, + pub l1_moduli_depth: usize, + pub scale: u64, pub crt_depth: usize, pub num_eval_slots: usize, pub knapsack_size: Option, @@ -45,7 +49,8 @@ impl< > KeyPolicyABE { pub fn new( - limb_bit_size: usize, + l1_moduli_bits: usize, + scale: u64, params: &::Params, num_eval_slots: Option, knapsack_size: Option, @@ -53,11 +58,12 @@ impl< trapdoor_sampler: ST, ) -> Self { let (_, crt_bits, crt_depth) = params.to_crt(); - let num_crt_limbs = crt_bits.div_ceil(limb_bit_size); + let l1_moduli_depth = (2 * crt_bits).div_ceil(l1_moduli_bits); let num_eval_slots = num_eval_slots.unwrap_or(params.ring_dimension() as usize); Self { - limb_bit_size, - num_crt_limbs, + l1_moduli_bits, + l1_moduli_depth, + scale, crt_depth, num_eval_slots, knapsack_size, @@ -118,11 +124,10 @@ impl< .iter() .flat_map(|input| { assert_eq!(input.len(), self.num_eval_slots); - encode_modulo_poly(self.limb_bit_size, ¶ms, input) + encode_nested_crt_poly(self.l1_moduli_bits, ¶ms, input) }) .collect::>(); - let num_given_input_polys = - num_modulo_poly::(self.limb_bit_size, ¶ms, num_inputs); + let num_given_input_polys = num_inputs * self.l1_moduli_depth; let reveal_plaintexts = vec![true; num_given_input_polys + 1]; let bgg_pubkey_sampler = BGGPublicKeySampler::<_, SH>::new(mpk.seed, 1); let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); @@ -188,20 +193,41 @@ impl< params: ::Params, mpk: MasterPK, msk: MasterSK, - arith_circuit: ArithmeticCircuit, + height: u32, dir_path: PathBuf, ) -> FuncSK { - let result = arith_circuit - .evaluate_with_bgg_pubkey::( + init_storage_system(); + let circuit = { + let mut circuit = PolyCircuit::::new(); + let ctx = Arc::new(NestedCrtPolyContext::setup( + &mut circuit, ¶ms, - mpk.seed, - dir_path.clone(), - 1, - mpk.b_matrix.clone(), - msk.b_trapdoor.clone(), - self.trapdoor_sampler.clone(), - ) - .await; + self.l1_moduli_bits, + self.scale, + self.num_eval_slots, + false, + )); + NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height as usize); + circuit + }; + let plt_evaluator = LweBggPubKeyEvaluator::::new( + mpk.seed, + self.trapdoor_sampler.clone(), + mpk.b_matrix.clone(), + msk.b_trapdoor.clone(), + dir_path.clone(), + ); + let num_inputs = + 1usize.checked_shl(height as u32).expect("height is too large to represent 2^h inputs"); + let num_given_input_polys = num_inputs * self.l1_moduli_depth; + let reveal_plaintexts = vec![true; num_given_input_polys + 1]; + let bgg_pubkey_sampler = BGGPublicKeySampler::<_, SH>::new(mpk.seed, 1); + let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); + let result = circuit.eval(¶ms, &pubkeys[0], &pubkeys[1..], Some(plt_evaluator)); + info!("finished evaluation of pubkeys"); + wait_for_all_writes(dir_path.clone()).await.unwrap(); + info!("finished write files"); + let a_f = result[0].clone().matrix; let u_f = self.trapdoor_sampler.preimage_extend( ¶ms, @@ -220,18 +246,27 @@ impl< ct: Ciphertext, mpk: MasterPK, fsk: FuncSK, - arith_circuit: ArithmeticCircuit, + height: u32, ) -> bool { + init_storage_system(); + let circuit = { + let mut circuit = PolyCircuit::::new(); + let ctx = Arc::new(NestedCrtPolyContext::setup( + &mut circuit, + ¶ms, + self.l1_moduli_bits, + self.scale, + self.num_eval_slots, + false, + )); + NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height as usize); + circuit + }; let encodings = &ct.bgg_encodings[..]; let dir_path: PathBuf = fsk.dir_path; let bgg_evaluator = LweBggEncodingPltEvaluator::::new(mpk.seed, dir_path, ct.c_b.clone()); - let result = arith_circuit.poly_circuit.eval( - ¶ms, - &encodings[0], - &encodings[1..], - Some(bgg_evaluator), - ); + let result = circuit.eval(¶ms, &encodings[0], &encodings[1..], Some(bgg_evaluator)); // 5. Let `c_f := s^T*A_f + e_{c_f}` in $\mathcal{R}_{q}^{1 \times m}$ // be the BGG+ encoding corresponding to the output wire of `poly_circuit`. let v = ct.c_b.concat_columns(&[&result[0].vector]) * fsk.u_f; @@ -239,9 +274,3 @@ impl< z.extract_bits_with_threshold(¶ms)[0] } } - -fn num_modulo_poly(limb_bit_size: usize, params: &P::Params, num_inputs: usize) -> usize { - let (_, crt_bits, _) = params.to_crt(); - let num_limbs_per_slot = crt_bits.div_ceil(limb_bit_size); - num_inputs * num_limbs_per_slot -} diff --git a/src/simulator.rs b/src/simulator.rs index 3ea87e4..3449fb6 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -3,6 +3,7 @@ pub use mxx::simulator::lattice_estimator::run_lattice_estimator_cli; use mxx::{ arithmetic::circuit::ArithmeticCircuit, circuit::PolyCircuit, + gadgets::arith::nested_crt::{NestedCrtPoly, NestedCrtPolyContext}, poly::dcrt::{params::DCRTPolyParams, poly::DCRTPoly}, simulator::{ SimulatorContext, @@ -154,6 +155,114 @@ pub fn bruteforce_params_for_bench_arith_circuit( .map(|outs| (outs.1, outs.2, outs.3, outs.4, outs.5)) } +// Output (crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) or None +pub fn bruteforce_params_for_bench_nested_crt_circuit( + target_secpar: u32, + crt_bits: u32, + crt_depth_range: (u32, u32), + base_bits_range: (u32, u32), + log_dim_range: (u32, u32), + num_eval_slots: Option, + l1_moduli_bits: usize, + scale: u64, + height: usize, + // circuit: PolyCircuit, +) -> Option<(u32, u32, u32, f64, u32)> { + // (cost, crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) + let outputs: Vec<(u32, u32, u32, u32, f64, u32)> = + (base_bits_range.0..=base_bits_range.1) + .into_par_iter() + .flat_map(|base_bits| { + let mut local = Vec::<(u32, u32, u32, u32, f64, u32)>::new(); + let mut lo = crt_depth_range.0; + let mut hi = crt_depth_range.1; + while lo <= hi { + let crt_depth = lo + ((hi - lo) / 2); + log::info!("base_bits {base_bits} crt_depth {crt_depth}"); + let (log_dim, e_b_log_alpha, knapsack_size) = match find_min_ring_dim( + target_secpar, + crt_bits, + crt_depth, + base_bits, + log_dim_range, + ) { + Ok(result) => result, + Err(e) => { + log::info!( + "Security error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, l1_moduli_bits = {}, height = {}: {}", + target_secpar, crt_bits, base_bits, crt_depth, l1_moduli_bits, height, e + ); + // try smaller crt_depth + if crt_depth == 0 { break; } + hi = crt_depth - 1; + continue; + } + }; + log::info!( + "Found log_dim = {}, e_b_log_alpha = {}, knapsack_size = {}", + log_dim, + e_b_log_alpha, + knapsack_size + ); + let ring_dim = (1 << log_dim) as u32; + let params = DCRTPolyParams::new(ring_dim, crt_depth as usize, crt_bits as usize, base_bits); + let circuit = { + let mut circuit = PolyCircuit::::new(); + let ctx = Arc::new(NestedCrtPolyContext::setup(&mut circuit, ¶ms, l1_moduli_bits, scale, num_eval_slots.unwrap_or(ring_dim as usize), true)); + NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height); + circuit + }; + log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); + log::info!("circuit size {:?}", circuit.count_gates_by_type_vec()); + log::info!("poly circuit non_free_depth {}",circuit.non_free_depth()); + match check_correctness( + target_secpar, + log_dim, + crt_bits, + crt_depth, + base_bits, + knapsack_size, + e_b_log_alpha, + &circuit, + ) { + Ok(cost) => { + log::info!( + "Found with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, cost = {}", + target_secpar, crt_bits, base_bits, crt_depth, cost + ); + local.push(( + cost, + crt_depth, + base_bits, + log_dim, + 2.0f64.powf( + crt_bits as f64 * crt_depth as f64 + e_b_log_alpha as f64, + ), + knapsack_size, + )); + // search smaller crt_depth to continue binary search + if crt_depth == 0 { break; } + hi = crt_depth - 1; + } + Err(e) => { + log::info!( + "Correctness error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}: {}", + target_secpar, crt_bits, base_bits, crt_depth, e + ); + // try larger crt_depth + lo = crt_depth + 1; + } + } + } + local + }) + .collect(); + outputs + .into_iter() + .min_by(|x, y| x.0.cmp(&y.0)) + .map(|outs| (outs.1, outs.2, outs.3, outs.4, outs.5)) +} + fn find_min_ring_dim( target_secpar: u32, crt_bits: u32, From 24c24eacf5616bc2e6d83af755f161ed5a39f92a Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 15:37:23 +0900 Subject: [PATCH 02/15] push the current changes --- Cargo.lock | 236 +++++---------- Cargo.toml | 4 + .../secpar_120_height_1_l1_6.params.toml | 8 +- src/simulator.rs | 284 ++++++++++-------- 4 files changed, 247 insertions(+), 285 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0cb94a6..0519c34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,22 +70,22 @@ dependencies = [ [[package]] name = "anstyle-query" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e231f6134f61b71076a3eab506c379d4f36122f2af15a9ff04415ea4c3339e2" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] name = "anstyle-wincon" -version = "3.0.10" +version = "3.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e0633414522a32ffaac8ac6cc8f748e090c5717661fddeea04219e2344f5f2a" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -182,9 +182,9 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "cc" -version = "1.2.43" +version = "1.2.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "739eb0f94557554b3ca9a86d2d37bebd49c5e6d0c1d2bda35ba5bdac830befc2" +checksum = "c481bdbf0ed3b892f6f806287d72acd515b352a4ec27a208489b8c1bc839633a" dependencies = [ "find-msvc-tools", "shlex", @@ -211,9 +211,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.50" +version = "4.5.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2cfd7bf8a6017ddaa4e32ffe7403d547790db06bd171c1c53926faab501623" +checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" dependencies = [ "clap_builder", "clap_derive", @@ -221,9 +221,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.50" +version = "4.5.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a4c05b9e80c5ccd3a7ef080ad7b6ba7d6fc00a985b8b157197075677c82c7a0" +checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" dependencies = [ "anstream", "anstyle", @@ -299,9 +299,9 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "crypto-common" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", "typenum", @@ -309,9 +309,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.187" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8465678d499296e2cbf9d3acf14307458fd69b471a31b65b3c519efe8b5e187" +checksum = "a7620f6cfc4dcca21f2b085b7a890e16c60fd66f560cd69ee60594908dc72ab1" dependencies = [ "cc", "cxx-build", @@ -324,9 +324,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.187" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d74b6bcf49ebbd91f1b1875b706ea46545032a14003b5557b7dfa4bbeba6766e" +checksum = "7a9bc1a22964ff6a355fbec24cf68266a0ed28f8b84c0864c386474ea3d0e479" dependencies = [ "cc", "codespan-reporting", @@ -339,9 +339,9 @@ dependencies = [ [[package]] name = "cxxbridge-cmd" -version = "1.0.187" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94ca2ad69673c4b35585edfa379617ac364bccd0ba0adf319811ba3a74ffa48a" +checksum = "b1f29a879d35f7906e3c9b77d7a1005a6a0787d330c09dfe4ffb5f617728cb44" dependencies = [ "clap", "codespan-reporting", @@ -353,15 +353,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.187" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29b52102aa395386d77d322b3a0522f2035e716171c2c60aa87cc5e9466e523" +checksum = "d67109015f93f683e364085aa6489a5b2118b4a40058482101d699936a7836d6" [[package]] name = "cxxbridge-macro" -version = "1.0.187" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a8ebf0b6138325af3ec73324cb3a48b64d57721f17291b151206782e61f66cd" +checksum = "d187e019e7b05a1f3e69a8396b70800ee867aa9fc2ab972761173ccee03742df" dependencies = [ "indexmap", "proc-macro2", @@ -446,9 +446,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "find-msvc-tools" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127" +checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" [[package]] name = "foldhash" @@ -464,9 +464,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "generic-array" -version = "0.14.9" +version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bb6743198531e02858aeaea5398fcc883e71851fcbcb5a2f773e2fb6cb1edf2" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", @@ -498,9 +498,9 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "hashbrown" -version = "0.16.0" +version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" [[package]] name = "heck" @@ -534,12 +534,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.12.0" +version = "2.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" +checksum = "0ad4bb2b565bca0645f4d68c5c9af97fba094e9791da685bf83cb5f3ce74acf2" dependencies = [ "equivalent", - "hashbrown 0.16.0", + "hashbrown 0.16.1", ] [[package]] @@ -565,22 +565,22 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "jiff" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be1f93b8b1eb69c77f24bbb0afdf66f54b632ee39af40ca21c4365a1d7347e49" +checksum = "49cce2b81f2098e7e3efc35bc2e0a6b7abec9d34128283d7a26fa8f32a6dbb35" dependencies = [ "jiff-static", "log", "portable-atomic", "portable-atomic-util", - "serde", + "serde_core", ] [[package]] name = "jiff-static" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03343451ff899767262ec32146f6d559dd759fdadf42ff0e227c7c48f72594b4" +checksum = "980af8b43c3ad5d8d349ace167ec8170839f753a42d233ba19e08afe1850fa69" dependencies = [ "proc-macro2", "quote", @@ -589,9 +589,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.82" +version = "0.3.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65" +checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8" dependencies = [ "once_cell", "wasm-bindgen", @@ -615,9 +615,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.177" +version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" +checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" [[package]] name = "libm" @@ -683,7 +683,7 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fnested_crt#c2e32d8fb25288a13b7d712cb357a486d5f435ca" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fnested_crt#83fd0de276b00125b4ff39ea79beacdcaba87394" dependencies = [ "bigdecimal", "bincode", @@ -762,7 +762,7 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "openfhe" version = "0.3.2" -source = "git+https://github.com/MachinaIO/openfhe-rs.git#1845c23031b7c95040456003aafc892c3228741b" +source = "git+https://github.com/MachinaIO//openfhe-rs.git?branch=feat%2Fnew_funcs_for_prf#940d480bb6a1781a0ceba69b62798172c146c0e5" dependencies = [ "cxx", "cxx-build", @@ -870,9 +870,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.41" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" dependencies = [ "proc-macro2", ] @@ -1119,9 +1119,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.108" +version = "2.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917" +checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" dependencies = [ "proc-macro2", "quote", @@ -1267,9 +1267,9 @@ checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2" [[package]] name = "tracing" -version = "0.1.41" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +checksum = "2d15d90a0b5c19378952d479dc858407149d7bb45a14de0142f6c534b16fc647" dependencies = [ "pin-project-lite", "tracing-attributes", @@ -1278,9 +1278,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.30" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" dependencies = [ "proc-macro2", "quote", @@ -1289,9 +1289,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.34" +version = "0.1.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d12581f227e93f094d3af2ae690a574abb8a2b9b7a96e7cfe9647b2b617678" +checksum = "7a04e24fab5c89c6a36eb8558c9656f30d81de51dfa4d3b45f26b21d61fa0a6c" dependencies = [ "once_cell", "valuable", @@ -1310,9 +1310,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.20" +version = "0.3.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2054a14f5307d601f88daf0553e1cbf472acc4f2c51afab632431cdcd72124d5" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" dependencies = [ "matchers", "nu-ansi-term", @@ -1334,9 +1334,9 @@ checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] name = "unicode-ident" -version = "1.0.20" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "unicode-width" @@ -1385,9 +1385,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.105" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60" +checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd" dependencies = [ "cfg-if", "once_cell", @@ -1398,9 +1398,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.105" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2" +checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1408,9 +1408,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.105" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc" +checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40" dependencies = [ "bumpalo", "proc-macro2", @@ -1421,9 +1421,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.105" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76" +checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4" dependencies = [ "unicode-ident", ] @@ -1502,16 +1502,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets 0.53.5", + "windows-targets", ] [[package]] @@ -1529,31 +1520,14 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.53.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm 0.53.1", - "windows_aarch64_msvc 0.53.1", - "windows_i686_gnu 0.53.1", - "windows_i686_gnullvm 0.53.1", - "windows_i686_msvc 0.53.1", - "windows_x86_64_gnu 0.53.1", - "windows_x86_64_gnullvm 0.53.1", - "windows_x86_64_msvc 0.53.1", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] @@ -1562,101 +1536,53 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" - [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" -[[package]] -name = "windows_i686_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" - [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_i686_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" - [[package]] name = "winnow" -version = "0.7.13" +version = "0.7.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf" +checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" [[package]] name = "wit-bindgen" @@ -1675,18 +1601,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.27" +version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c" +checksum = "fd74ec98b9250adb3ca554bdde269adf631549f51d8a8f8f0a10b50f1cb298c3" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.27" +version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831" +checksum = "d8a8d209fdf45cf5138cbb5a506f6b52522a25afccc534d1475dad8e31105c6a" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 7cca793..25dbf4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,10 @@ bigdecimal = { version = "0.4" } rayon = "1.10" log = "0.4" +[patch."https://github.com/MachinaIO/openfhe-rs.git"] +openfhe = { git = "https://github.com/MachinaIO//openfhe-rs.git", branch = "feat/new_funcs_for_prf" } + + [profile.dev] debug = "line-tables-only" split-debuginfo = "unpacked" diff --git a/abe/sim_configs/secpar_120_height_1_l1_6.params.toml b/abe/sim_configs/secpar_120_height_1_l1_6.params.toml index ef25956..b7c5f5c 100644 --- a/abe/sim_configs/secpar_120_height_1_l1_6.params.toml +++ b/abe/sim_configs/secpar_120_height_1_l1_6.params.toml @@ -1,11 +1,11 @@ target_secpar = 120 -crt_bits = 24 +crt_bits = 32 crt_depth_min = 20 -crt_depth_max = 60 +crt_depth_max = 20 base_bits_min = 17 base_bits_max = 17 -log_dim_min = 15 -log_dim_max = 15 +log_dim_min = 16 +log_dim_max = 16 num_eval_slots = 1 l1_moduli_bits = 8 scale = 8 diff --git a/src/simulator.rs b/src/simulator.rs index 3449fb6..4462e78 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -14,11 +14,8 @@ use mxx::{ utils::log_mem, }; use num_bigint::BigUint; -use rayon::{join, prelude::*}; -use std::sync::{ - Arc, - atomic::{AtomicU32, Ordering}, -}; +use rayon::prelude::*; +use std::sync::Arc; use thiserror::Error; // Logging (replaces println!) // Configure a logger (e.g., env_logger) in the binary/tests to see output. @@ -49,8 +46,8 @@ pub enum SimulatorError { "good log_alpha not found for target_secpar={target_secpar}, ring_dim={ring_dim}, log_q={log_q}, m={m}" )] LogAlphaNotFound { target_secpar: u32, ring_dim: BigUint, log_q: u32, m: BigUint }, - #[error("correctness does not hold: error={e}, q_over_4={q_over_4}")] - NotCorrect { e: BigDecimal, q_over_4: BigDecimal }, + #[error("correctness does not hold: error_bits={e_bits}, q_over_4_bits={q_over_4_bits}")] + NotCorrect { e_bits: usize, q_over_4_bits: usize }, } // Output (crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) or None @@ -103,6 +100,7 @@ pub fn bruteforce_params_for_bench_arith_circuit( ); let ring_dim = (1 << log_dim) as u32; let params = DCRTPolyParams::new(ring_dim, crt_depth as usize, crt_bits as usize, base_bits); + log::info!("params constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); let circuit = ArithmeticCircuit::benchmark_multiplication_tree(¶ms, limb_bit_size, num_eval_slots.unwrap_or(ring_dim as usize), height,true); log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); log::info!("circuit size {:?}", circuit.poly_circuit.count_gates_by_type_vec()); @@ -206,9 +204,11 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( ); let ring_dim = (1 << log_dim) as u32; let params = DCRTPolyParams::new(ring_dim, crt_depth as usize, crt_bits as usize, base_bits); + log::info!("params constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); let circuit = { let mut circuit = PolyCircuit::::new(); let ctx = Arc::new(NestedCrtPolyContext::setup(&mut circuit, ¶ms, l1_moduli_bits, scale, num_eval_slots.unwrap_or(ring_dim as usize), true)); + log::info!("ctx constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height); circuit }; @@ -315,104 +315,25 @@ fn check_security( base_bits: u32, ) -> Result<(i64, u32), SimulatorError> { let log_q = crt_bits * crt_depth; - let q = BigUint::from(2u32).pow(log_q); let m_g = crt_bits.div_ceil(base_bits) * crt_depth; let m_b = m_g + 2; // The column size of the matrix B (sampled with a trapdoor) is m_b; however, one column is an // identity polynomial, so we need to ignore one column. Additionally, one more uniformly // random matrix is used for encrypting a message in ABE; thus the total column size for // ring-LWE is m_b - 1 + 1 = m_b. - let (log_alpha_res, knapsack_res) = join( - || find_log_alpha_for_ring_lwe(target_secpar, ring_dim, log_q, &BigUint::from(m_b)), - || find_knapsack_size(target_secpar, ring_dim, &q, m_b - 1), - ); + let log_alpha_res = + find_log_alpha_for_ring_lwe(target_secpar, ring_dim, log_q, &BigUint::from(m_b)); + // join( + // || find_log_alpha_for_ring_lwe(target_secpar, ring_dim, log_q, &BigUint::from(m_b)), + // // || find_knapsack_size(target_secpar, ring_dim, &q, m_b - 1), + // ); let log_alpha = log_alpha_res?; log::debug!("found log_alpha_res = {log_alpha}"); - let knapsack_size = knapsack_res?; + let knapsack_size = m_b - 1; log::debug!("found knapsack_size = {knapsack_size}"); Ok((log_alpha, knapsack_size)) } -/// Returns the smallest `knapsack_size` in [1, max_knapsack_size] whose estimated -/// security is at least `target_secpar`, or an error if estimation fails or none found. -/// - `target_secpar`: required minimum security parameter. -/// - `ring_dim`: base ring dimension. -/// - `q`: modulus (as BigUint). -/// - `max_knapsack_size`: upper bound to search (inclusive). -fn find_knapsack_size( - target_secpar: u32, - ring_dim: &BigUint, - q: &BigUint, - max_knapsack_size: u32, -) -> Result { - if max_knapsack_size < 2 { - return Err(SimulatorError::KnapsackNotFound { - target_secpar, - ring_dim: ring_dim.clone(), - max_knapsack_size, - q: q.clone(), - }); - } - - let best = AtomicU32::new(0); - - (2..=max_knapsack_size).into_par_iter().try_for_each( - |knapsack_size| -> Result<(), SimulatorError> { - let current_best = best.load(Ordering::Relaxed); - if current_best != 0 && knapsack_size >= current_best { - return Ok(()); - } - - // Effective LWE dimension n = ring_dim * knapsack_size - ring_dim - let n = ring_dim * BigUint::from(knapsack_size) - ring_dim; - // s_dist = Ternary, e_dist = Ternary, m = n, exact = false (rough) - let secpar = run_lattice_estimator_cli( - &n, - q, - &Distribution::Ternary, - &Distribution::Ternary, - Some(&n), - false, - )?; - log::debug!("called estimator {secpar} in find_knapsack_size for {knapsack_size}"); - - if secpar as u32 >= target_secpar { - let mut observed = best.load(Ordering::Acquire); - while observed == 0 || knapsack_size < observed { - match best.compare_exchange( - observed, - knapsack_size, - Ordering::AcqRel, - Ordering::Acquire, - ) { - Ok(_) => break, - Err(actual) => { - if actual != 0 && knapsack_size >= actual { - break; - } - observed = actual; - } - } - } - } - - Ok(()) - }, - )?; - - let best_value = best.load(Ordering::Relaxed); - if best_value != 0 { - Ok(best_value) - } else { - Err(SimulatorError::KnapsackNotFound { - target_secpar, - ring_dim: ring_dim.clone(), - max_knapsack_size, - q: q.clone(), - }) - } -} - /// Binary-search for the smallest integer `log_alpha` in [-log_q, -1] such that /// the estimated security for ring-LWE with parameters (ring_dim, q=2^log_q, /// s_dist=Ternary, e_dist=DiscreteGaussianAlpha(alpha=2^{-log_alpha}), m) is at @@ -428,7 +349,7 @@ fn find_log_alpha_for_ring_lwe( ) -> Result { // q = 2^{log_q} let q = BigUint::from(1u8) << (log_q as usize); - + let q_dec = BigDecimal::from_biguint(q.clone(), 0); // Search bounds (inclusive) over integer log_alpha. let mut lo: i64 = -(log_q as i64); let mut hi: i64 = 5 - (log_q as i64); @@ -440,7 +361,10 @@ fn find_log_alpha_for_ring_lwe( // alpha = sigma/q = 2^{log_alpha} let alpha = 2f64.powi(mid as i32); // safe for practical parameter sizes - let e_dist = Distribution::DiscreteGaussianAlpha { alpha, mean: None, n: None }; + let stddev = &q_dec * BigDecimal::from_f64(alpha).unwrap(); + let e_dist = + Distribution::DiscreteGaussian { stddev: stddev.to_string(), mean: None, n: None }; + // DiscreteGaussianAlpha { alpha, mean: None, n: None }; // s_dist = Ternary, m = provided, rough estimation let secpar = run_lattice_estimator_cli( @@ -471,6 +395,26 @@ fn find_log_alpha_for_ring_lwe( }) } +// Compute 2^exponent exactly as a BigDecimal to avoid intermediate f64 overflow. +fn pow_two_bigdecimal(exponent: i64) -> BigDecimal { + if exponent == 0 { + return BigDecimal::one(); + } + let mut result = BigDecimal::one(); + let mut base = BigDecimal::from(2u32); + let mut exp = exponent.unsigned_abs(); + while exp > 0 { + if exp & 1 == 1 { + result = result * &base; + } + exp >>= 1; + if exp > 0 { + base = &base * &base; + } + } + if exponent >= 0 { result } else { BigDecimal::one() / result } +} + fn check_correctness( target_secpar: u32, log_dim: u32, @@ -487,7 +431,7 @@ fn check_correctness( let q = BigUint::from(2u32).pow(log_q); let m_g = (crt_bits.div_ceil(base_bits) * crt_depth) as usize; let m_b = m_g + 2; - let e_b_sigma = BigDecimal::from_f64(2f64.powf((log_q as i64 - e_b_log_alpha) as f64)).unwrap(); + let e_b_sigma = pow_two_bigdecimal(i64::from(log_q) - e_b_log_alpha); let secpar_sqrt = BigDecimal::from_u32(target_secpar).unwrap().sqrt().unwrap(); let ring_dim_sqrt = BigDecimal::from_biguint(ring_dim.clone(), 0).sqrt().unwrap(); let base = BigDecimal::from_biguint((BigUint::from(1u32)) << base_bits, 0); @@ -514,7 +458,63 @@ fn check_correctness( .max_by(|a, b| a.h_norm.poly_norm.norm.cmp(&b.h_norm.poly_norm.norm)) .unwrap(); let (max_h_top, max_h_bottom) = max_out_wire.h_norm.split_rows(m_b); + let max_h_top_bits = { + let s = max_h_top + .poly_norm + .norm + .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + .to_string(); + if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + let bytes = n.to_bytes_be(); + if bytes.is_empty() { + 0usize + } else { + (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + } + } else { + 0usize + } + }; + log::info!("max_h_top_bits bits {}", max_h_top_bits); + + let max_h_bottom_bits = { + let s = max_h_bottom + .poly_norm + .norm + .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + .to_string(); + if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + let bytes = n.to_bytes_be(); + if bytes.is_empty() { + 0usize + } else { + (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + } + } else { + 0usize + } + }; + log::info!("max_h_bottom_bits bits {}", max_h_bottom_bits); let e_after_eval = &e_b * max_h_top + e_a * max_h_bottom; + let e_after_eval_bits = { + let s = e_after_eval + .poly_norm + .norm + .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + .to_string(); + if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + let bytes = n.to_bytes_be(); + if bytes.is_empty() { + 0usize + } else { + (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + } + } else { + 0usize + } + }; + log::info!("e_after_eval_bits bits {}", e_after_eval_bits); + let plt_eval = NormPltLweEvaluator::new(sim_ctx.clone(), input_size); let mut preimage_norm_top = plt_eval.preimage1_norm.clone(); preimage_norm_top.nrow = m_b; @@ -523,44 +523,76 @@ fn check_correctness( preimage_norm_bottom.ncol = 1; let e_u = PolyMatrixNorm::sample_gauss(sim_ctx.clone(), 1, 1, e_b_sigma); let e_final = &e_b * preimage_norm_top + e_after_eval * preimage_norm_bottom + e_u; - let q_over_4 = BigDecimal::from_biguint(q, 0) / BigDecimal::from_u32(4).unwrap(); - if q_over_4 > e_final.poly_norm.norm { - // Compute bit lengths of q_over_4 and e_final (after rounding up to integer) - let q_over_4_bits = { - let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } - } else { + let e_final_bits = { + let s = e_final + .poly_norm + .norm + .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + .to_string(); + if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + let bytes = n.to_bytes_be(); + if bytes.is_empty() { 0usize - } - }; - let e_final_bits = { - let s = e_final - .poly_norm - .norm - .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - .to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } } else { + (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + } + } else { + 0usize + } + }; + log::info!("e_final_bits bits {}", e_final_bits); + + let q_over_4 = BigDecimal::from_biguint(q, 0) / BigDecimal::from_u32(4).unwrap(); + let q_over_4_bits = { + let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); + if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + let bytes = n.to_bytes_be(); + if bytes.is_empty() { 0usize + } else { + (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) } - }; + } else { + 0usize + } + }; + if q_over_4 > e_final.poly_norm.norm { + // Compute bit lengths of q_over_4 and e_final (after rounding up to integer) + // let q_over_4_bits = { + // let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; + // let e_final_bits = { + // let s = e_final + // .poly_norm + // .norm + // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + // .to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; log_mem(format!("q_over_4_bits: {}, e_final_bits: {}", q_over_4_bits, e_final_bits)); Ok(log_dim * m_g as u32) } else { - Err(SimulatorError::NotCorrect { e: e_final.poly_norm.norm, q_over_4 }) + Err(SimulatorError::NotCorrect { e_bits: e_final_bits, q_over_4_bits }) } } From 7e5539a6af4d52f3c1ebc95c84f6842fb0ed337d Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 17:29:02 +0900 Subject: [PATCH 03/15] simulation succeeded --- Cargo.lock | 36 +-- Cargo.toml | 5 +- abe/Cargo.toml | 2 +- abe/run_configs/secpar_0_height_2.params.toml | 15 +- ..._6.params.toml_20251217-172456.params.toml | 13 ++ .../secpar_120_height_1_l1_6.params.toml | 12 - .../secpar_120_height_1_p_6.params.toml | 11 + abe/src/config.rs | 6 +- abe/src/main.rs | 33 +-- src/abe.rs | 98 ++++---- src/simulator.rs | 220 +++++++++--------- 11 files changed, 237 insertions(+), 214 deletions(-) create mode 100644 abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml delete mode 100644 abe/sim_configs/secpar_120_height_1_l1_6.params.toml create mode 100644 abe/sim_configs/secpar_120_height_1_p_6.params.toml diff --git a/Cargo.lock b/Cargo.lock index 0519c34..1a93247 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,15 +176,15 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.19.0" +version = "3.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" [[package]] name = "cc" -version = "1.2.48" +version = "1.2.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c481bdbf0ed3b892f6f806287d72acd515b352a4ec27a208489b8c1bc839633a" +checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" dependencies = [ "find-msvc-tools", "shlex", @@ -309,9 +309,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7620f6cfc4dcca21f2b085b7a890e16c60fd66f560cd69ee60594908dc72ab1" +checksum = "bbda285ba6e5866529faf76352bdf73801d9b44a6308d7cd58ca2379f378e994" dependencies = [ "cc", "cxx-build", @@ -324,9 +324,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9bc1a22964ff6a355fbec24cf68266a0ed28f8b84c0864c386474ea3d0e479" +checksum = "af9efde466c5d532d57efd92f861da3bdb7f61e369128ce8b4c3fe0c9de4fa4d" dependencies = [ "cc", "codespan-reporting", @@ -339,9 +339,9 @@ dependencies = [ [[package]] name = "cxxbridge-cmd" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f29a879d35f7906e3c9b77d7a1005a6a0787d330c09dfe4ffb5f617728cb44" +checksum = "3efb93799095bccd4f763ca07997dc39a69e5e61ab52d2c407d4988d21ce144d" dependencies = [ "clap", "codespan-reporting", @@ -353,15 +353,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d67109015f93f683e364085aa6489a5b2118b4a40058482101d699936a7836d6" +checksum = "3092010228026e143b32a4463ed9fa8f86dca266af4bf5f3b2a26e113dbe4e45" [[package]] name = "cxxbridge-macro" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d187e019e7b05a1f3e69a8396b70800ee867aa9fc2ab972761173ccee03742df" +checksum = "31d72ebfcd351ae404fb00ff378dfc9571827a00722c9e735c9181aec320ba0a" dependencies = [ "indexmap", "proc-macro2", @@ -651,9 +651,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.28" +version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" [[package]] name = "matchers" @@ -683,7 +683,7 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fnested_crt#83fd0de276b00125b4ff39ea79beacdcaba87394" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fggh_lut_2512#b01bccbadd2d1e2488280d7d9d0b6a68bba8586c" dependencies = [ "bigdecimal", "bincode", @@ -762,7 +762,7 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "openfhe" version = "0.3.2" -source = "git+https://github.com/MachinaIO//openfhe-rs.git?branch=feat%2Fnew_funcs_for_prf#940d480bb6a1781a0ceba69b62798172c146c0e5" +source = "git+https://github.com/MachinaIO/openfhe-rs.git#7654f31b6ff666e08cbe07a26c21d1bc011e872b" dependencies = [ "cxx", "cxx-build", diff --git a/Cargo.toml b/Cargo.toml index 25dbf4e..94e4964 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/nested_crt" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/ggh_lut_2512" } rand = { version = "0.9.0", features = ["std_rng"] } num-bigint = { version = "0.4", features = ["serde"] } thiserror = "2.0.16" @@ -16,9 +16,6 @@ bigdecimal = { version = "0.4" } rayon = "1.10" log = "0.4" -[patch."https://github.com/MachinaIO/openfhe-rs.git"] -openfhe = { git = "https://github.com/MachinaIO//openfhe-rs.git", branch = "feat/new_funcs_for_prf" } - [profile.dev] debug = "line-tables-only" diff --git a/abe/Cargo.toml b/abe/Cargo.toml index cd112b7..d48b862 100644 --- a/abe/Cargo.toml +++ b/abe/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/nested_crt" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/ggh_lut_2512" } arithmetic-abe = { path = "../" } anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } diff --git a/abe/run_configs/secpar_0_height_2.params.toml b/abe/run_configs/secpar_0_height_2.params.toml index e80ec59..3ba8b82 100644 --- a/abe/run_configs/secpar_0_height_2.params.toml +++ b/abe/run_configs/secpar_0_height_2.params.toml @@ -1,12 +1,13 @@ config_id = "secpar_0_height_2.params.toml" -target_secpar = 0 +target_secpar = 120 crt_depth = 2 -crt_bits = 10 +crt_bits = 17 ring_dimension = 4 -num_eval_slots = 1 +knapsack_size = 31 e_b_sigma = 0.0 trapdoor_sigma = 4.578 -base_bits = 7 -limb_bit_size = 2 -arith_input_size = 4 -arith_height = 2 +base_bits = 10 +p_moduli_bits = 6 +scale = 4 +arith_input_size = 2 +arith_height = 1 diff --git a/abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml b/abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml new file mode 100644 index 0000000..4e6e3ba --- /dev/null +++ b/abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml @@ -0,0 +1,13 @@ +config_id = "secpar_120_height_1_p_6.params.toml_20251217-172456" +target_secpar = 120 +crt_depth = 15 +crt_bits = 25 +ring_dimension = 32768 +knapsack_size = 31 +e_b_sigma = 4.0 +trapdoor_sigma = 4.578 +base_bits = 17 +p_moduli_bits = 6 +scale = 10 +arith_input_size = 2 +arith_height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_l1_6.params.toml b/abe/sim_configs/secpar_120_height_1_l1_6.params.toml deleted file mode 100644 index b7c5f5c..0000000 --- a/abe/sim_configs/secpar_120_height_1_l1_6.params.toml +++ /dev/null @@ -1,12 +0,0 @@ -target_secpar = 120 -crt_bits = 32 -crt_depth_min = 20 -crt_depth_max = 20 -base_bits_min = 17 -base_bits_max = 17 -log_dim_min = 16 -log_dim_max = 16 -num_eval_slots = 1 -l1_moduli_bits = 8 -scale = 8 -height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_p_6.params.toml b/abe/sim_configs/secpar_120_height_1_p_6.params.toml new file mode 100644 index 0000000..ec77b70 --- /dev/null +++ b/abe/sim_configs/secpar_120_height_1_p_6.params.toml @@ -0,0 +1,11 @@ +target_secpar = 120 +crt_bits = 25 +crt_depth_min = 15 +crt_depth_max = 20 +base_bits_min = 17 +base_bits_max = 19 +log_dim_min = 14 +log_dim_max = 16 +p_moduli_bits = 6 +scale = 10 +height = 1 diff --git a/abe/src/config.rs b/abe/src/config.rs index 9c2da27..cab0e4f 100644 --- a/abe/src/config.rs +++ b/abe/src/config.rs @@ -11,14 +11,13 @@ pub struct RunConfig { pub crt_depth: u32, pub crt_bits: u32, pub ring_dimension: u32, - pub num_eval_slots: Option, pub knapsack_size: Option, pub e_b_sigma: f64, #[serde(default = "default_trapdoor_sigma")] pub trapdoor_sigma: Option, /// bit size of the base for the gadget vector and decomposition pub base_bits: u32, - pub l1_moduli_bits: usize, + pub p_moduli_bits: usize, pub scale: u64, pub arith_input_size: usize, pub arith_height: u32, @@ -39,8 +38,7 @@ pub struct SimConfig { pub base_bits_max: u32, pub log_dim_min: u32, pub log_dim_max: u32, - pub num_eval_slots: Option, - pub l1_moduli_bits: usize, + pub p_moduli_bits: usize, pub scale: u64, pub height: usize, } diff --git a/abe/src/main.rs b/abe/src/main.rs index d2815a0..cb0af37 100644 --- a/abe/src/main.rs +++ b/abe/src/main.rs @@ -12,7 +12,7 @@ use clap::{Parser, Subcommand}; use keccak_asm::Keccak256; use mxx::{ matrix::dcrt_poly::DCRTPolyMatrix, - poly::{PolyParams, dcrt::params::DCRTPolyParams}, + poly::dcrt::params::DCRTPolyParams, sampler::{ PolyTrapdoorSampler, hash::DCRTPolyHashSampler, trapdoor::DCRTPolyTrapdoorSampler, uniform::DCRTPolyUniformSampler, @@ -117,14 +117,13 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { base_bits_max, log_dim_min, log_dim_max, - num_eval_slots, - l1_moduli_bits, + p_moduli_bits, scale, height, } = config; log_mem(format!( - "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), num_eval_slots={:?}, l1_moduli_bits={}, scale = {}, height={}, config_prefix={}", + "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), p_moduli_bits={}, scale = {}, height={}, config_prefix={}", target_secpar, crt_bits, crt_depth_min, @@ -133,8 +132,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { base_bits_max, log_dim_min, log_dim_max, - num_eval_slots, - l1_moduli_bits, + p_moduli_bits, scale, height, config_prefix @@ -146,8 +144,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { (crt_depth_min, crt_depth_max), (base_bits_min, base_bits_max), (log_dim_min, log_dim_max), - config.num_eval_slots, - l1_moduli_bits, + p_moduli_bits, scale, height, ) @@ -178,8 +175,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { e_b_sigma, trapdoor_sigma: Some(4.578), base_bits, - num_eval_slots: config.num_eval_slots, - l1_moduli_bits, + p_moduli_bits, scale, arith_input_size, arith_height, @@ -215,10 +211,9 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { DCRTPolyTrapdoorSampler, DCRTPolyUniformSampler, >::new( - config.l1_moduli_bits, + config.p_moduli_bits, config.scale, ¶ms, - config.num_eval_slots, config.knapsack_size, config.e_b_sigma, trapdoor_sampler, @@ -281,10 +276,9 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { DCRTPolyTrapdoorSampler, DCRTPolyUniformSampler, >::new( - config.l1_moduli_bits, + config.p_moduli_bits, config.scale, ¶ms, - config.num_eval_slots, config.knapsack_size, config.e_b_sigma, trapdoor_sampler, @@ -293,8 +287,6 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { let mut t_enc = Duration::ZERO; let mut t_read_fsk = Duration::ZERO; let mut t_dec = Duration::ZERO; - let num_eval_slots = config.num_eval_slots.unwrap_or(params.ring_dimension() as usize); - log_mem("starting KeyPolicy ABE"); // 3) enc @@ -312,14 +304,7 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { ); let ct: Ciphertext = timed_read( "enc", - || { - abe.enc( - params.clone(), - mpk, - &vec![vec![BigUint::ZERO; num_eval_slots]; config.arith_input_size], - &vec![true; num_eval_slots], - ) - }, + || abe.enc(params.clone(), mpk, &vec![BigUint::ZERO; config.arith_input_size], true), &mut t_enc, ); log_mem("finished enc"); diff --git a/src/abe.rs b/src/abe.rs index 6995b36..f4916f8 100644 --- a/src/abe.rs +++ b/src/abe.rs @@ -11,7 +11,7 @@ use mxx::{ }, circuit::PolyCircuit, element::PolyElem, - gadgets::arith::nested_crt::{NestedCrtPoly, NestedCrtPolyContext, encode_nested_crt_poly}, + gadgets::arith::nested_rns::{NestedRnsPoly, NestedRnsPolyContext, encode_nested_rns_poly}, lookup::lwe_eval::{LweBggEncodingPltEvaluator, LweBggPubKeyEvaluator}, matrix::PolyMatrix, poly::{Poly, PolyParams}, @@ -30,11 +30,10 @@ pub struct KeyPolicyABE< SU: PolyUniformSampler + Send + Sync, > { pub e_b_sigma: f64, - pub l1_moduli_bits: usize, - pub l1_moduli_depth: usize, + pub p_moduli_bits: usize, + pub p_moduli_depth: usize, pub scale: u64, pub crt_depth: usize, - pub num_eval_slots: usize, pub knapsack_size: Option, pub trapdoor_sampler: ST, _sh: PhantomData, @@ -49,23 +48,22 @@ impl< > KeyPolicyABE { pub fn new( - l1_moduli_bits: usize, + p_moduli_bits: usize, scale: u64, params: &::Params, - num_eval_slots: Option, knapsack_size: Option, e_b_sigma: f64, trapdoor_sampler: ST, ) -> Self { + assert!(p_moduli_bits > 1, "p_moduli_bits must be at least 2 for NestedRns"); let (_, crt_bits, crt_depth) = params.to_crt(); - let l1_moduli_depth = (2 * crt_bits).div_ceil(l1_moduli_bits); - let num_eval_slots = num_eval_slots.unwrap_or(params.ring_dimension() as usize); + // NestedRns chooses the number of level-1 moduli as ceil(2 * |q_bits| / (p_bits - 1)) + let p_moduli_depth = (2 * crt_bits).div_ceil(p_moduli_bits - 1); Self { - l1_moduli_bits, - l1_moduli_depth, + p_moduli_bits, + p_moduli_depth, scale, crt_depth, - num_eval_slots, knapsack_size, e_b_sigma, trapdoor_sampler, @@ -94,14 +92,19 @@ impl< &self, params: ::Params, mpk: MasterPK, - inputs: &[Vec], - message: &[bool], + inputs: &[BigUint], + message: bool, ) -> Ciphertext { let num_inputs = inputs.len(); + assert_eq!( + num_inputs, mpk.num_inputs, + "provided inputs ({num_inputs}) must match mpk.num_inputs ({})", + mpk.num_inputs + ); let uniform_sampler = SU::new(); let s = uniform_sampler.sample_uniform(¶ms, 1, 1, DistType::TernaryDist); let b_col_size = 2 + params.modulus_digits(); - let c_b_error = { + let c_b_error: M = { let first_part = uniform_sampler.sample_uniform( ¶ms, 1, @@ -120,15 +123,21 @@ impl< }; let c_b = s.clone() * mpk.b_matrix.as_ref() + &c_b_error; let bgg_encoding_sampler = BGGEncodingSampler::::new(¶ms, &s.get_row(0), None); + // let (_, _, crt_depth) = params.to_crt(); + // let p_moduli_depth = (2 * crt_bits).div_ceil(self.p_moduli_bits - 1); let plaintexts = inputs .iter() - .flat_map(|input| { - assert_eq!(input.len(), self.num_eval_slots); - encode_nested_crt_poly(self.l1_moduli_bits, ¶ms, input) - }) + .flat_map(|input| encode_nested_rns_poly(self.p_moduli_bits, ¶ms, input)) .collect::>(); - let num_given_input_polys = num_inputs * self.l1_moduli_depth; - let reveal_plaintexts = vec![true; num_given_input_polys + 1]; + // let expected_plaintexts = mpk.num_inputs * crt_depth * self.p_moduli_depth; + // assert_eq!( + // plaintexts.len(), + // expected_plaintexts, + // "plaintext count ({}) must equal num_inputs * crt_depth * p_moduli_depth ({})", + // plaintexts.len(), + // expected_plaintexts + // ); + let reveal_plaintexts = vec![true; plaintexts.len()]; let bgg_pubkey_sampler = BGGPublicKeySampler::<_, SH>::new(mpk.seed, 1); let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); let bgg_encodings_no_error = bgg_encoding_sampler.sample(¶ms, &pubkeys, &plaintexts); @@ -166,14 +175,13 @@ impl< } }) .collect::>(); - let ring_dim = params.ring_dimension() as usize; - assert_eq!(message.len(), self.num_eval_slots, "message length must match num_eval_slots",); - let mut message_coeffs: Vec = - message.iter().map(|bit| BigUint::from(*bit as u8)).collect(); - if message_coeffs.len() < ring_dim { - message_coeffs.resize(ring_dim, BigUint::from(0u8)); - } - let message_poly = M::P::from_biguints(¶ms, &message_coeffs); + // let ring_dim = params.ring_dimension() as usize; + // let mut message_coeffs: Vec = + // message.iter().map(|bit| BigUint::from(*bit as u8)).collect(); + // if message_coeffs.len() < ring_dim { + // message_coeffs.resize(ring_dim, BigUint::from(0u8)); + // } + let message_poly = M::P::from_usize_to_constant(¶ms, message as usize); let half_q = ::Elem::half_q(¶ms.modulus()); let half_const = M::P::from_elem_to_constant(¶ms, &half_q); let scaled_message = message_poly * half_const; @@ -199,15 +207,19 @@ impl< init_storage_system(); let circuit = { let mut circuit = PolyCircuit::::new(); - let ctx = Arc::new(NestedCrtPolyContext::setup( + let ctx = Arc::new(NestedRnsPolyContext::setup( &mut circuit, ¶ms, - self.l1_moduli_bits, + self.p_moduli_bits, self.scale, - self.num_eval_slots, false, )); - NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height as usize); + NestedRnsPoly::benchmark_multiplication_tree( + ctx, + ¶ms, + &mut circuit, + height as usize, + ); circuit }; let plt_evaluator = LweBggPubKeyEvaluator::::new( @@ -217,10 +229,7 @@ impl< msk.b_trapdoor.clone(), dir_path.clone(), ); - let num_inputs = - 1usize.checked_shl(height as u32).expect("height is too large to represent 2^h inputs"); - let num_given_input_polys = num_inputs * self.l1_moduli_depth; - let reveal_plaintexts = vec![true; num_given_input_polys + 1]; + let reveal_plaintexts = vec![true; circuit.num_input()]; let bgg_pubkey_sampler = BGGPublicKeySampler::<_, SH>::new(mpk.seed, 1); let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); let result = circuit.eval(¶ms, &pubkeys[0], &pubkeys[1..], Some(plt_evaluator)); @@ -251,18 +260,27 @@ impl< init_storage_system(); let circuit = { let mut circuit = PolyCircuit::::new(); - let ctx = Arc::new(NestedCrtPolyContext::setup( + let ctx = Arc::new(NestedRnsPolyContext::setup( &mut circuit, ¶ms, - self.l1_moduli_bits, + self.p_moduli_bits, self.scale, - self.num_eval_slots, false, )); - NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height as usize); + NestedRnsPoly::benchmark_multiplication_tree( + ctx, + ¶ms, + &mut circuit, + height as usize, + ); circuit }; let encodings = &ct.bgg_encodings[..]; + assert_eq!( + encodings.len(), + circuit.num_input() + 1, + "ciphertext must contain exactly 1 + circuit.num_input() encodings" + ); let dir_path: PathBuf = fsk.dir_path; let bgg_evaluator = LweBggEncodingPltEvaluator::::new(mpk.seed, dir_path, ct.c_b.clone()); diff --git a/src/simulator.rs b/src/simulator.rs index 4462e78..9a77251 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -1,9 +1,8 @@ use bigdecimal::{BigDecimal, FromPrimitive, One}; pub use mxx::simulator::lattice_estimator::run_lattice_estimator_cli; use mxx::{ - arithmetic::circuit::ArithmeticCircuit, circuit::PolyCircuit, - gadgets::arith::nested_crt::{NestedCrtPoly, NestedCrtPolyContext}, + gadgets::arith::nested_rns::{NestedRnsPoly, NestedRnsPolyContext}, poly::dcrt::{params::DCRTPolyParams, poly::DCRTPoly}, simulator::{ SimulatorContext, @@ -11,7 +10,7 @@ use mxx::{ poly_matrix_norm::PolyMatrixNorm, wire_norm::NormPltLweEvaluator, }, - utils::log_mem, + utils::{bigdecimal_bits_ceil, log_mem}, }; use num_bigint::BigUint; use rayon::prelude::*; @@ -47,7 +46,7 @@ pub enum SimulatorError { )] LogAlphaNotFound { target_secpar: u32, ring_dim: BigUint, log_q: u32, m: BigUint }, #[error("correctness does not hold: error_bits={e_bits}, q_over_4_bits={q_over_4_bits}")] - NotCorrect { e_bits: usize, q_over_4_bits: usize }, + NotCorrect { e_bits: u64, q_over_4_bits: u64 }, } // Output (crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) or None @@ -57,10 +56,9 @@ pub fn bruteforce_params_for_bench_arith_circuit( crt_depth_range: (u32, u32), base_bits_range: (u32, u32), log_dim_range: (u32, u32), - num_eval_slots: Option, - limb_bit_size: usize, + p_moduli_bits: usize, + scale: u64, height: usize, - // circuit: PolyCircuit, ) -> Option<(u32, u32, u32, f64, u32)> { // (cost, crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) let outputs: Vec<(u32, u32, u32, u32, f64, u32)> = @@ -83,8 +81,8 @@ pub fn bruteforce_params_for_bench_arith_circuit( Ok(result) => result, Err(e) => { log::info!( - "Security error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, limb_bit_size = {}, height = {}: {}", - target_secpar, crt_bits, base_bits, crt_depth, limb_bit_size, height, e + "Security error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, p_moduli_bits = {}, scale = {}, height = {}: {}", + target_secpar, crt_bits, base_bits, crt_depth, p_moduli_bits, scale, height, e ); // try smaller crt_depth if crt_depth == 0 { break; } @@ -101,10 +99,13 @@ pub fn bruteforce_params_for_bench_arith_circuit( let ring_dim = (1 << log_dim) as u32; let params = DCRTPolyParams::new(ring_dim, crt_depth as usize, crt_bits as usize, base_bits); log::info!("params constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); - let circuit = ArithmeticCircuit::benchmark_multiplication_tree(¶ms, limb_bit_size, num_eval_slots.unwrap_or(ring_dim as usize), height,true); + let mut circuit = PolyCircuit::::new(); + let nested_rns_ctx = Arc::new(NestedRnsPolyContext::setup(&mut circuit, ¶ms, p_moduli_bits, scale, true)); + NestedRnsPoly::benchmark_multiplication_tree(nested_rns_ctx, ¶ms, &mut circuit, height); + // ArithmeticCircuit::benchmark_multiplication_tree(¶ms, limb_bit_size, num_eval_slots.unwrap_or(ring_dim as usize), height,true); log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); - log::info!("circuit size {:?}", circuit.poly_circuit.count_gates_by_type_vec()); - log::info!("poly circuit non_free_depth {}",circuit.poly_circuit.non_free_depth()); + log::info!("circuit size {:?}", circuit.count_gates_by_type_vec()); + log::info!("poly circuit non_free_depth {}",circuit.non_free_depth()); match check_correctness( target_secpar, log_dim, @@ -113,7 +114,7 @@ pub fn bruteforce_params_for_bench_arith_circuit( base_bits, knapsack_size, e_b_log_alpha, - &circuit.poly_circuit, + &circuit, ) { Ok(cost) => { log::info!( @@ -160,8 +161,7 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( crt_depth_range: (u32, u32), base_bits_range: (u32, u32), log_dim_range: (u32, u32), - num_eval_slots: Option, - l1_moduli_bits: usize, + p_moduli_bits: usize, scale: u64, height: usize, // circuit: PolyCircuit, @@ -187,8 +187,8 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( Ok(result) => result, Err(e) => { log::info!( - "Security error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, l1_moduli_bits = {}, height = {}: {}", - target_secpar, crt_bits, base_bits, crt_depth, l1_moduli_bits, height, e + "Security error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, p_moduli_bits = {}, height = {}: {}", + target_secpar, crt_bits, base_bits, crt_depth, p_moduli_bits, height, e ); // try smaller crt_depth if crt_depth == 0 { break; } @@ -207,9 +207,9 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( log::info!("params constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); let circuit = { let mut circuit = PolyCircuit::::new(); - let ctx = Arc::new(NestedCrtPolyContext::setup(&mut circuit, ¶ms, l1_moduli_bits, scale, num_eval_slots.unwrap_or(ring_dim as usize), true)); + let ctx = Arc::new(NestedRnsPolyContext::setup(&mut circuit, ¶ms, p_moduli_bits, scale, true)); log::info!("ctx constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); - NestedCrtPoly::benchmark_multiplication_tree(ctx, &mut circuit, height); + NestedRnsPoly::benchmark_multiplication_tree(ctx, ¶ms,&mut circuit, height); circuit }; log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); @@ -377,7 +377,10 @@ fn find_log_alpha_for_ring_lwe( )?; log::debug!("called estimator {secpar} in find_log_alpha_for_ring_lwe"); - if secpar as u32 >= target_secpar { + if log_q as i64 + mid <= 0 { + // try smaller (more conservative) log_alpha + hi = mid - 1; + } else if secpar as u32 >= target_secpar { found = Some(found.map_or(mid, |cur| cur.min(mid))); // try smaller (more conservative) log_alpha hi = mid - 1; @@ -431,7 +434,9 @@ fn check_correctness( let q = BigUint::from(2u32).pow(log_q); let m_g = (crt_bits.div_ceil(base_bits) * crt_depth) as usize; let m_b = m_g + 2; - let e_b_sigma = pow_two_bigdecimal(i64::from(log_q) - e_b_log_alpha); + log::info!("e_b_log_alpha {}", e_b_log_alpha); + let e_b_sigma = pow_two_bigdecimal(i64::from(log_q) + e_b_log_alpha); + log::info!("e_b_sigma {}", e_b_sigma); let secpar_sqrt = BigDecimal::from_u32(target_secpar).unwrap().sqrt().unwrap(); let ring_dim_sqrt = BigDecimal::from_biguint(ring_dim.clone(), 0).sqrt().unwrap(); let base = BigDecimal::from_biguint((BigUint::from(1u32)) << base_bits, 0); @@ -458,61 +463,66 @@ fn check_correctness( .max_by(|a, b| a.h_norm.poly_norm.norm.cmp(&b.h_norm.poly_norm.norm)) .unwrap(); let (max_h_top, max_h_bottom) = max_out_wire.h_norm.split_rows(m_b); - let max_h_top_bits = { - let s = max_h_top - .poly_norm - .norm - .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - .to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } - } else { - 0usize - } - }; + let max_h_top_bits = bigdecimal_bits_ceil(&max_h_top.poly_norm.norm); + // { + // let s = max_h_top + // .poly_norm + // .norm + // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + // .to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; log::info!("max_h_top_bits bits {}", max_h_top_bits); - let max_h_bottom_bits = { - let s = max_h_bottom - .poly_norm - .norm - .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - .to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } - } else { - 0usize - } - }; + let max_h_bottom_bits = bigdecimal_bits_ceil(&max_h_bottom.poly_norm.norm); + // { + // let s = max_h_bottom + // .poly_norm + // .norm + // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + // .to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; log::info!("max_h_bottom_bits bits {}", max_h_bottom_bits); + log::info!("e_b bits {}", bigdecimal_bits_ceil(&e_b.poly_norm.norm)); + log::info!("e_a bits {}", bigdecimal_bits_ceil(&e_a.poly_norm.norm)); let e_after_eval = &e_b * max_h_top + e_a * max_h_bottom; - let e_after_eval_bits = { - let s = e_after_eval - .poly_norm - .norm - .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - .to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } - } else { - 0usize - } - }; + let e_after_eval_bits = bigdecimal_bits_ceil(&e_after_eval.poly_norm.norm); + // { + // let s = e_after_eval + // .poly_norm + // .norm + // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + // .to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; log::info!("e_after_eval_bits bits {}", e_after_eval_bits); let plt_eval = NormPltLweEvaluator::new(sim_ctx.clone(), input_size); @@ -523,39 +533,41 @@ fn check_correctness( preimage_norm_bottom.ncol = 1; let e_u = PolyMatrixNorm::sample_gauss(sim_ctx.clone(), 1, 1, e_b_sigma); let e_final = &e_b * preimage_norm_top + e_after_eval * preimage_norm_bottom + e_u; - let e_final_bits = { - let s = e_final - .poly_norm - .norm - .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - .to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } - } else { - 0usize - } - }; + let e_final_bits = bigdecimal_bits_ceil(&e_final.poly_norm.norm); + // { + // let s = e_final + // .poly_norm + // .norm + // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) + // .to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; log::info!("e_final_bits bits {}", e_final_bits); let q_over_4 = BigDecimal::from_biguint(q, 0) / BigDecimal::from_u32(4).unwrap(); - let q_over_4_bits = { - let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); - if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - let bytes = n.to_bytes_be(); - if bytes.is_empty() { - 0usize - } else { - (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - } - } else { - 0usize - } - }; + let q_over_4_bits = bigdecimal_bits_ceil(&q_over_4); + // { + // let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); + // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { + // let bytes = n.to_bytes_be(); + // if bytes.is_empty() { + // 0usize + // } else { + // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) + // } + // } else { + // 0usize + // } + // }; if q_over_4 > e_final.poly_norm.norm { // Compute bit lengths of q_over_4 and e_final (after rounding up to integer) // let q_over_4_bits = { @@ -612,8 +624,8 @@ mod tests { (2, 4), (15, 18), (13, 16), - Some(2), - 2, + 6, + 1 << 8, 3, ); assert!(params.is_some()); From 1b36cb39efec6287ef6e2daf8ee5983bf789d08e Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 17:38:12 +0900 Subject: [PATCH 04/15] update ci --- .github/workflows/benchmark-self-hosted.yml | 25 ++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/.github/workflows/benchmark-self-hosted.yml b/.github/workflows/benchmark-self-hosted.yml index 94b37ca..558a8cd 100644 --- a/.github/workflows/benchmark-self-hosted.yml +++ b/.github/workflows/benchmark-self-hosted.yml @@ -92,7 +92,7 @@ jobs: uses: actions/checkout@v4 with: repository: MachinaIO/openfhe-development - ref: feat/improve_determinant + ref: main path: openfhe - name: Build & install OpenFHE @@ -122,16 +122,21 @@ jobs: run: | mkdir -p logs + LOGFILE=logs/data_${{ github.event.inputs.data_id }}.log + # Run abe for offline computation with hardcoded config path as requested - ( - abe bench-run-offline \ - --config abe/run_configs/${{ github.event.inputs.config_path }} \ - --data-dir data_${{ github.event.inputs.data_id }} \ - 2>&1 | tee logs/data_${{ github.event.inputs.data_id }}.log - ) & - pid=$! - echo "Benchmark PID: $pid" - wait $pid + echo "=== bench-run-offline ===" | tee "$LOGFILE" + abe bench-run-offline \ + --config abe/run_configs/${{ github.event.inputs.config_path }} \ + --data-dir data_${{ github.event.inputs.data_id }} \ + 2>&1 | tee -a "$LOGFILE" + + # Run abe for online computation reusing the generated keys/data + echo "=== bench-run-online ===" | tee -a "$LOGFILE" + abe bench-run-online \ + --config abe/run_configs/${{ github.event.inputs.config_path }} \ + --data-dir data_${{ github.event.inputs.data_id }} \ + 2>&1 | tee -a "$LOGFILE" - name: Install uv uses: astral-sh/setup-uv@v5 From 490f0387f21ea1314bdb23502a9e200273c0f8be Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 18:08:53 +0900 Subject: [PATCH 05/15] Fix ci --- .github/workflows/benchmark-self-hosted.yml | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/.github/workflows/benchmark-self-hosted.yml b/.github/workflows/benchmark-self-hosted.yml index c383e5c..f2da5d0 100644 --- a/.github/workflows/benchmark-self-hosted.yml +++ b/.github/workflows/benchmark-self-hosted.yml @@ -86,19 +86,14 @@ jobs: LOGFILE=logs/data_${{ github.event.inputs.data_id }}.log - # Run abe for offline computation with hardcoded config path as requested - echo "=== bench-run-offline ===" | tee "$LOGFILE" - abe bench-run-offline \ - --config abe/run_configs/${{ github.event.inputs.config_path }} \ - --data-dir data_${{ github.event.inputs.data_id }} \ - 2>&1 | tee -a "$LOGFILE" - - # Run abe for online computation reusing the generated keys/data - echo "=== bench-run-online ===" | tee -a "$LOGFILE" - abe bench-run-online \ - --config abe/run_configs/${{ github.event.inputs.config_path }} \ - --data-dir data_${{ github.event.inputs.data_id }} \ - 2>&1 | tee -a "$LOGFILE" + { + abe bench-run-offline \ + --config abe/run_configs/${{ github.event.inputs.config_path }} \ + --data-dir data_${{ github.event.inputs.data_id }} && + abe bench-run-online \ + --config abe/run_configs/${{ github.event.inputs.config_path }} \ + --data-dir data_${{ github.event.inputs.data_id }} + } 2>&1 | tee -a "$LOGFILE" - name: Install uv uses: astral-sh/setup-uv@v5 From 4b6b82c9ac2fb06322c3f5872c5d870e3f39c972 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 18:26:18 +0900 Subject: [PATCH 06/15] update ci --- .github/workflows/benchmark-self-hosted.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/benchmark-self-hosted.yml b/.github/workflows/benchmark-self-hosted.yml index f2da5d0..dd49b2b 100644 --- a/.github/workflows/benchmark-self-hosted.yml +++ b/.github/workflows/benchmark-self-hosted.yml @@ -81,6 +81,7 @@ jobs: env: LD_LIBRARY_PATH: /usr/local/lib RUST_LOG: info + RUST_BACKTRACE: 1 run: | mkdir -p logs From a779d037e5788d3790ede31eab65bc6a3c6245a3 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 19:15:23 +0900 Subject: [PATCH 07/15] find smaller params --- ...56.params.toml => secpar_120_height_1_p_6.params.toml} | 8 ++++---- abe/sim_configs/secpar_120_height_1_p_6.params.toml | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) rename abe/run_configs/{secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml => secpar_120_height_1_p_6.params.toml} (55%) diff --git a/abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml b/abe/run_configs/secpar_120_height_1_p_6.params.toml similarity index 55% rename from abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml rename to abe/run_configs/secpar_120_height_1_p_6.params.toml index 4e6e3ba..20cb4bd 100644 --- a/abe/run_configs/secpar_120_height_1_p_6.params.toml_20251217-172456.params.toml +++ b/abe/run_configs/secpar_120_height_1_p_6.params.toml @@ -1,9 +1,9 @@ -config_id = "secpar_120_height_1_p_6.params.toml_20251217-172456" +config_id = "secpar_120_height_1_p_6.params.toml" target_secpar = 120 -crt_depth = 15 +crt_depth = 11 crt_bits = 25 -ring_dimension = 32768 -knapsack_size = 31 +ring_dimension = 16384 +knapsack_size = 23 e_b_sigma = 4.0 trapdoor_sigma = 4.578 base_bits = 17 diff --git a/abe/sim_configs/secpar_120_height_1_p_6.params.toml b/abe/sim_configs/secpar_120_height_1_p_6.params.toml index ec77b70..e3568b8 100644 --- a/abe/sim_configs/secpar_120_height_1_p_6.params.toml +++ b/abe/sim_configs/secpar_120_height_1_p_6.params.toml @@ -1,11 +1,11 @@ target_secpar = 120 crt_bits = 25 -crt_depth_min = 15 -crt_depth_max = 20 +crt_depth_min = 8 +crt_depth_max = 11 base_bits_min = 17 base_bits_max = 19 -log_dim_min = 14 -log_dim_max = 16 +log_dim_min = 13 +log_dim_max = 14 p_moduli_bits = 6 scale = 10 height = 1 From 28904e3e680f8c8373cc34ffac1895309b1c9f68 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 19:35:44 +0900 Subject: [PATCH 08/15] update mxx --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1a93247..46f0b8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -683,7 +683,7 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fggh_lut_2512#b01bccbadd2d1e2488280d7d9d0b6a68bba8586c" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fggh_lut_2512#966bc47b41caaa579f3d40f78421c3a553aa210b" dependencies = [ "bigdecimal", "bincode", From 3ad8f870e2fdc61a05f7b427bb4aaba28b2d0ebd Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 17 Dec 2025 20:05:06 +0900 Subject: [PATCH 09/15] scale -> scale_bits --- .github/workflows/benchmark-self-hosted.yml | 2 +- abe/run_configs/secpar_0_height_1_p_3.params.toml | 13 +++++++++++++ abe/run_configs/secpar_0_height_2.params.toml | 13 ------------- abe/run_configs/secpar_120_height_1_p_6.params.toml | 2 +- abe/sim_configs/secpar_0_height_1_p_3.params.toml | 11 +++++++++++ abe/sim_configs/secpar_120_height_1_p_6.params.toml | 2 +- abe/src/config.rs | 2 +- abe/src/main.rs | 10 +++++----- src/simulator.rs | 3 ++- 9 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 abe/run_configs/secpar_0_height_1_p_3.params.toml delete mode 100644 abe/run_configs/secpar_0_height_2.params.toml create mode 100644 abe/sim_configs/secpar_0_height_1_p_3.params.toml diff --git a/.github/workflows/benchmark-self-hosted.yml b/.github/workflows/benchmark-self-hosted.yml index dd49b2b..a6faa9c 100644 --- a/.github/workflows/benchmark-self-hosted.yml +++ b/.github/workflows/benchmark-self-hosted.yml @@ -6,7 +6,7 @@ on: config_path: description: "Path to Configuration File" required: true - default: "secpar_0_height_2.params.toml" + default: "secpar_0_height_1_p_3.params.toml" data_id: description: "Data ID (e.g. 53)" diff --git a/abe/run_configs/secpar_0_height_1_p_3.params.toml b/abe/run_configs/secpar_0_height_1_p_3.params.toml new file mode 100644 index 0000000..64eb95c --- /dev/null +++ b/abe/run_configs/secpar_0_height_1_p_3.params.toml @@ -0,0 +1,13 @@ +config_id = "secpar_0_height_1_p_3.params.toml" +target_secpar = 0 +crt_depth = 3 +crt_bits = 16 +ring_dimension = 16 +knapsack_size = 7 +e_b_sigma = 4.0 +trapdoor_sigma = 4.578 +base_bits = 8 +p_moduli_bits = 6 +scale = 64 +arith_input_size = 2 +arith_height = 1 diff --git a/abe/run_configs/secpar_0_height_2.params.toml b/abe/run_configs/secpar_0_height_2.params.toml deleted file mode 100644 index 3ba8b82..0000000 --- a/abe/run_configs/secpar_0_height_2.params.toml +++ /dev/null @@ -1,13 +0,0 @@ -config_id = "secpar_0_height_2.params.toml" -target_secpar = 120 -crt_depth = 2 -crt_bits = 17 -ring_dimension = 4 -knapsack_size = 31 -e_b_sigma = 0.0 -trapdoor_sigma = 4.578 -base_bits = 10 -p_moduli_bits = 6 -scale = 4 -arith_input_size = 2 -arith_height = 1 diff --git a/abe/run_configs/secpar_120_height_1_p_6.params.toml b/abe/run_configs/secpar_120_height_1_p_6.params.toml index 20cb4bd..27cd06c 100644 --- a/abe/run_configs/secpar_120_height_1_p_6.params.toml +++ b/abe/run_configs/secpar_120_height_1_p_6.params.toml @@ -8,6 +8,6 @@ e_b_sigma = 4.0 trapdoor_sigma = 4.578 base_bits = 17 p_moduli_bits = 6 -scale = 10 +scale = 256 arith_input_size = 2 arith_height = 1 diff --git a/abe/sim_configs/secpar_0_height_1_p_3.params.toml b/abe/sim_configs/secpar_0_height_1_p_3.params.toml new file mode 100644 index 0000000..0672b38 --- /dev/null +++ b/abe/sim_configs/secpar_0_height_1_p_3.params.toml @@ -0,0 +1,11 @@ +target_secpar = 0 +crt_bits = 16 +crt_depth_min = 3 +crt_depth_max = 3 +base_bits_min = 8 +base_bits_max = 8 +log_dim_min = 4 +log_dim_max = 4 +p_moduli_bits = 6 +scale_bits = 6 +height = 1 diff --git a/abe/sim_configs/secpar_120_height_1_p_6.params.toml b/abe/sim_configs/secpar_120_height_1_p_6.params.toml index e3568b8..fa2bd78 100644 --- a/abe/sim_configs/secpar_120_height_1_p_6.params.toml +++ b/abe/sim_configs/secpar_120_height_1_p_6.params.toml @@ -7,5 +7,5 @@ base_bits_max = 19 log_dim_min = 13 log_dim_max = 14 p_moduli_bits = 6 -scale = 10 +scale_bits = 8 height = 1 diff --git a/abe/src/config.rs b/abe/src/config.rs index cab0e4f..f11f67b 100644 --- a/abe/src/config.rs +++ b/abe/src/config.rs @@ -39,6 +39,6 @@ pub struct SimConfig { pub log_dim_min: u32, pub log_dim_max: u32, pub p_moduli_bits: usize, - pub scale: u64, + pub scale_bits: u32, pub height: usize, } diff --git a/abe/src/main.rs b/abe/src/main.rs index cb0af37..42e361c 100644 --- a/abe/src/main.rs +++ b/abe/src/main.rs @@ -118,12 +118,12 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { log_dim_min, log_dim_max, p_moduli_bits, - scale, + scale_bits, height, } = config; log_mem(format!( - "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), p_moduli_bits={}, scale = {}, height={}, config_prefix={}", + "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), p_moduli_bits={}, scale_bits = {}, height={}, config_prefix={}", target_secpar, crt_bits, crt_depth_min, @@ -133,7 +133,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { log_dim_min, log_dim_max, p_moduli_bits, - scale, + scale_bits, height, config_prefix )); @@ -145,7 +145,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { (base_bits_min, base_bits_max), (log_dim_min, log_dim_max), p_moduli_bits, - scale, + scale_bits, height, ) .context("unable to find parameters for benchmark arithmetic circuit")?; @@ -176,7 +176,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { trapdoor_sigma: Some(4.578), base_bits, p_moduli_bits, - scale, + scale: 1 << scale_bits, arith_input_size, arith_height, }; diff --git a/src/simulator.rs b/src/simulator.rs index 9a77251..3665ff5 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -162,7 +162,7 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( base_bits_range: (u32, u32), log_dim_range: (u32, u32), p_moduli_bits: usize, - scale: u64, + scale_bits: u32, height: usize, // circuit: PolyCircuit, ) -> Option<(u32, u32, u32, f64, u32)> { @@ -207,6 +207,7 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( log::info!("params constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); let circuit = { let mut circuit = PolyCircuit::::new(); + let scale = 1< Date: Wed, 17 Dec 2025 21:06:05 +0900 Subject: [PATCH 10/15] update configs --- .github/workflows/benchmark-self-hosted.yml | 2 +- abe/run_configs/secpar_0_height_1_p_3.params.toml | 13 ------------- abe/run_configs/secpar_20_height_1_p_6.params.toml | 13 +++++++++++++ abe/sim_configs/secpar_0_height_1_p_3.params.toml | 11 ----------- abe/sim_configs/secpar_20_height_1_p_6.params.toml | 11 +++++++++++ 5 files changed, 25 insertions(+), 25 deletions(-) delete mode 100644 abe/run_configs/secpar_0_height_1_p_3.params.toml create mode 100644 abe/run_configs/secpar_20_height_1_p_6.params.toml delete mode 100644 abe/sim_configs/secpar_0_height_1_p_3.params.toml create mode 100644 abe/sim_configs/secpar_20_height_1_p_6.params.toml diff --git a/.github/workflows/benchmark-self-hosted.yml b/.github/workflows/benchmark-self-hosted.yml index a6faa9c..9888be1 100644 --- a/.github/workflows/benchmark-self-hosted.yml +++ b/.github/workflows/benchmark-self-hosted.yml @@ -6,7 +6,7 @@ on: config_path: description: "Path to Configuration File" required: true - default: "secpar_0_height_1_p_3.params.toml" + default: "secpar_20_height_1_p_6.params.toml" data_id: description: "Data ID (e.g. 53)" diff --git a/abe/run_configs/secpar_0_height_1_p_3.params.toml b/abe/run_configs/secpar_0_height_1_p_3.params.toml deleted file mode 100644 index 64eb95c..0000000 --- a/abe/run_configs/secpar_0_height_1_p_3.params.toml +++ /dev/null @@ -1,13 +0,0 @@ -config_id = "secpar_0_height_1_p_3.params.toml" -target_secpar = 0 -crt_depth = 3 -crt_bits = 16 -ring_dimension = 16 -knapsack_size = 7 -e_b_sigma = 4.0 -trapdoor_sigma = 4.578 -base_bits = 8 -p_moduli_bits = 6 -scale = 64 -arith_input_size = 2 -arith_height = 1 diff --git a/abe/run_configs/secpar_20_height_1_p_6.params.toml b/abe/run_configs/secpar_20_height_1_p_6.params.toml new file mode 100644 index 0000000..da9ee82 --- /dev/null +++ b/abe/run_configs/secpar_20_height_1_p_6.params.toml @@ -0,0 +1,13 @@ +config_id = "secpar_20_height_1_p_6.params.toml" +target_secpar = 20 +crt_depth = 9 +crt_bits = 24 +ring_dimension = 4096 +knapsack_size = 28 +e_b_sigma = 4.0 +trapdoor_sigma = 4.578 +base_bits = 8 +p_moduli_bits = 6 +scale = 64 +arith_input_size = 2 +arith_height = 1 diff --git a/abe/sim_configs/secpar_0_height_1_p_3.params.toml b/abe/sim_configs/secpar_0_height_1_p_3.params.toml deleted file mode 100644 index 0672b38..0000000 --- a/abe/sim_configs/secpar_0_height_1_p_3.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 0 -crt_bits = 16 -crt_depth_min = 3 -crt_depth_max = 3 -base_bits_min = 8 -base_bits_max = 8 -log_dim_min = 4 -log_dim_max = 4 -p_moduli_bits = 6 -scale_bits = 6 -height = 1 diff --git a/abe/sim_configs/secpar_20_height_1_p_6.params.toml b/abe/sim_configs/secpar_20_height_1_p_6.params.toml new file mode 100644 index 0000000..9f51af0 --- /dev/null +++ b/abe/sim_configs/secpar_20_height_1_p_6.params.toml @@ -0,0 +1,11 @@ +target_secpar = 20 +crt_bits = 24 +crt_depth_min = 8 +crt_depth_max = 10 +base_bits_min = 8 +base_bits_max = 8 +log_dim_min = 11 +log_dim_max = 13 +p_moduli_bits = 6 +scale_bits = 6 +height = 1 From dbe7ee1c65646c0214ab9bbb0fc6eac50601f2f4 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Thu, 25 Dec 2025 22:02:31 +0900 Subject: [PATCH 11/15] update for ggh15 plt evaluator --- Cargo.lock | 78 ++-- .../secpar_0_height_1_p_6.params.toml | 13 + .../secpar_20_height_1_p_6.params.toml | 13 - .../secpar_0_height_1_p_6.params.toml | 11 + .../secpar_20_height_1_p_6.params.toml | 11 - abe/src/main.rs | 33 +- src/abe.rs | 108 ++--- src/simulator.rs | 398 +++++++----------- 8 files changed, 281 insertions(+), 384 deletions(-) create mode 100644 abe/run_configs/secpar_0_height_1_p_6.params.toml delete mode 100644 abe/run_configs/secpar_20_height_1_p_6.params.toml create mode 100644 abe/sim_configs/secpar_0_height_1_p_6.params.toml delete mode 100644 abe/sim_configs/secpar_20_height_1_p_6.params.toml diff --git a/Cargo.lock b/Cargo.lock index 46f0b8a..14b02ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -182,9 +182,9 @@ checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" [[package]] name = "cc" -version = "1.2.49" +version = "1.2.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" +checksum = "9f50d563227a1c37cc0a263f64eca3334388c01c5e4c4861a9def205c614383c" dependencies = [ "find-msvc-tools", "shlex", @@ -559,15 +559,15 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +checksum = "7ee5b5339afb4c41626dde77b7a611bd4f2c202b897852b4bcf5d03eddc61010" [[package]] name = "jiff" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49cce2b81f2098e7e3efc35bc2e0a6b7abec9d34128283d7a26fa8f32a6dbb35" +checksum = "a87d9b8105c23642f50cbbae03d1f75d8422c5cb98ce7ee9271f7ff7505be6b8" dependencies = [ "jiff-static", "log", @@ -578,9 +578,9 @@ dependencies = [ [[package]] name = "jiff-static" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "980af8b43c3ad5d8d349ace167ec8170839f753a42d233ba19e08afe1850fa69" +checksum = "b787bebb543f8969132630c51fd0afab173a86c6abae56ff3b9e5e3e3f9f6e58" dependencies = [ "proc-macro2", "quote", @@ -683,7 +683,7 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fggh_lut_2512#966bc47b41caaa579f3d40f78421c3a553aa210b" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fggh_lut_2512#50ef06f11ef231d79afdef27e6f31c30aa47c932" dependencies = [ "bigdecimal", "bincode", @@ -762,7 +762,7 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "openfhe" version = "0.3.2" -source = "git+https://github.com/MachinaIO/openfhe-rs.git#7654f31b6ff666e08cbe07a26c21d1bc011e872b" +source = "git+https://github.com/MachinaIO/openfhe-rs.git#9cc3775e2be28012469a4dc89b7d2a750444ce93" dependencies = [ "cxx", "cxx-build", @@ -791,9 +791,9 @@ checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" [[package]] name = "portable-atomic" -version = "1.11.1" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" +checksum = "f59e70c4aef1e55797c2e8fd94a4f2a973fc972cfde0e0b05f683667b0cd39dd" [[package]] name = "portable-atomic-util" @@ -993,9 +993,9 @@ checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" [[package]] name = "rustix" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" dependencies = [ "bitflags", "errno", @@ -1010,12 +1010,6 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - [[package]] name = "scopeguard" version = "1.2.0" @@ -1060,22 +1054,22 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.145" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" +checksum = "6af14725505314343e673e9ecb7cd7e8a36aa9791eb936235a3567cc31447ae4" dependencies = [ "itoa", "memchr", - "ryu", "serde", "serde_core", + "zmij", ] [[package]] name = "serde_spanned" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e24345aa0fe688594e73770a5f6d1b216508b4f93484c0026d521acd30134392" +checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" dependencies = [ "serde_core", ] @@ -1136,9 +1130,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "tempfile" -version = "3.23.0" +version = "3.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" +checksum = "655da9c7eb6305c55742045d5a8d2037996d61d8de95806335c7c86ce0f82e9c" dependencies = [ "fastrand", "getrandom", @@ -1228,9 +1222,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.9.8" +version = "0.9.10+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0dc8b1fb61449e27716ec0e1bdf0f6b8f3e8f6b05391e8497b8b6d7804ea6d8" +checksum = "0825052159284a1a8b4d6c0c86cbc801f2da5afd2b225fa548c72f2e74002f48" dependencies = [ "indexmap", "serde_core", @@ -1243,33 +1237,33 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.7.3" +version = "0.7.5+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2cdb639ebbc97961c51720f858597f7f24c4fc295327923af55b74c3c724533" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" dependencies = [ "serde_core", ] [[package]] name = "toml_parser" -version = "1.0.4" +version = "1.0.6+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0cbe268d35bdb4bb5a56a2de88d0ad0eb70af5384a99d648cd4b3d04039800e" +checksum = "a3198b4b0a8e11f09dd03e133c0280504d0801269e9afa46362ffde1cbeebf44" dependencies = [ "winnow", ] [[package]] name = "toml_writer" -version = "1.0.4" +version = "1.0.6+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2" +checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607" [[package]] name = "tracing" -version = "0.1.43" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d15d90a0b5c19378952d479dc858407149d7bb45a14de0142f6c534b16fc647" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ "pin-project-lite", "tracing-attributes", @@ -1289,9 +1283,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.35" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a04e24fab5c89c6a36eb8558c9656f30d81de51dfa4d3b45f26b21d61fa0a6c" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", "valuable", @@ -1618,3 +1612,9 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zmij" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dccf46b25b205e4bebe1d5258a991df1cc17801017a845cb5b3fe0269781aa" diff --git a/abe/run_configs/secpar_0_height_1_p_6.params.toml b/abe/run_configs/secpar_0_height_1_p_6.params.toml new file mode 100644 index 0000000..69a2523 --- /dev/null +++ b/abe/run_configs/secpar_0_height_1_p_6.params.toml @@ -0,0 +1,13 @@ +config_id = "secpar_0_height_1_p_6.params.toml" +target_secpar = 0 +crt_depth = 15 +crt_bits = 24 +ring_dimension = 16 +knapsack_size = 46 +e_b_sigma = 4.0 +trapdoor_sigma = 4.578 +base_bits = 8 +p_moduli_bits = 6 +scale = 256 +arith_input_size = 2 +arith_height = 1 diff --git a/abe/run_configs/secpar_20_height_1_p_6.params.toml b/abe/run_configs/secpar_20_height_1_p_6.params.toml deleted file mode 100644 index da9ee82..0000000 --- a/abe/run_configs/secpar_20_height_1_p_6.params.toml +++ /dev/null @@ -1,13 +0,0 @@ -config_id = "secpar_20_height_1_p_6.params.toml" -target_secpar = 20 -crt_depth = 9 -crt_bits = 24 -ring_dimension = 4096 -knapsack_size = 28 -e_b_sigma = 4.0 -trapdoor_sigma = 4.578 -base_bits = 8 -p_moduli_bits = 6 -scale = 64 -arith_input_size = 2 -arith_height = 1 diff --git a/abe/sim_configs/secpar_0_height_1_p_6.params.toml b/abe/sim_configs/secpar_0_height_1_p_6.params.toml new file mode 100644 index 0000000..a30637b --- /dev/null +++ b/abe/sim_configs/secpar_0_height_1_p_6.params.toml @@ -0,0 +1,11 @@ +target_secpar = 0 +crt_bits = 24 +crt_depth_min = 11 +crt_depth_max = 15 +base_bits_min = 8 +base_bits_max = 8 +log_dim_min = 4 +log_dim_max = 4 +p_moduli_bits = 6 +scale_bits = 8 +height = 1 diff --git a/abe/sim_configs/secpar_20_height_1_p_6.params.toml b/abe/sim_configs/secpar_20_height_1_p_6.params.toml deleted file mode 100644 index 9f51af0..0000000 --- a/abe/sim_configs/secpar_20_height_1_p_6.params.toml +++ /dev/null @@ -1,11 +0,0 @@ -target_secpar = 20 -crt_bits = 24 -crt_depth_min = 8 -crt_depth_max = 10 -base_bits_min = 8 -base_bits_max = 8 -log_dim_min = 11 -log_dim_max = 13 -p_moduli_bits = 6 -scale_bits = 6 -height = 1 diff --git a/abe/src/main.rs b/abe/src/main.rs index 42e361c..56128b6 100644 --- a/abe/src/main.rs +++ b/abe/src/main.rs @@ -14,7 +14,7 @@ use mxx::{ matrix::dcrt_poly::DCRTPolyMatrix, poly::dcrt::params::DCRTPolyParams, sampler::{ - PolyTrapdoorSampler, hash::DCRTPolyHashSampler, trapdoor::DCRTPolyTrapdoorSampler, + hash::DCRTPolyHashSampler, trapdoor::DCRTPolyTrapdoorSampler, uniform::DCRTPolyUniformSampler, }, utils::{log_mem, timed_read, timed_read_async}, @@ -203,8 +203,7 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { config.crt_bits as usize, config.base_bits, ); - let trapdoor_sampler = - DCRTPolyTrapdoorSampler::new(¶ms, config.trapdoor_sigma.expect("trapdoor sigma exist")); + let trapdoor_sigma = config.trapdoor_sigma.expect("trapdoor sigma exist"); let abe = KeyPolicyABE::< DCRTPolyMatrix, DCRTPolyHashSampler, @@ -216,7 +215,7 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { ¶ms, config.knapsack_size, config.e_b_sigma, - trapdoor_sampler, + trapdoor_sigma, ); let mut t_setup = Duration::ZERO; let mut t_keygen = Duration::ZERO; @@ -226,7 +225,7 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { // 1) setup log_mem("starting setup"); let (mpk, msk): (MasterPK, MasterSK) = - timed_read("setup", || abe.setup(params.clone(), config.arith_input_size), &mut t_setup); + timed_read("setup", || abe.setup(¶ms, config.arith_input_size), &mut t_setup); log_mem("finished setup"); let dir_path = if data_dir.exists() { @@ -239,15 +238,7 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { log_mem("starting keygen"); let fsk: FuncSK = timed_read_async( "keygen", - || { - abe.keygen( - params.clone(), - mpk.clone(), - msk.clone(), - config.arith_height, - dir_path.clone(), - ) - }, + || abe.keygen(¶ms, mpk.clone(), msk.clone(), config.arith_height, dir_path.clone()), &mut t_keygen, ) .await; @@ -268,8 +259,7 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { config.crt_bits as usize, config.base_bits, ); - let trapdoor_sampler = - DCRTPolyTrapdoorSampler::new(¶ms, config.trapdoor_sigma.expect("trapdoor sigma exist")); + let trapdoor_sigma = config.trapdoor_sigma.expect("trapdoor sigma exist"); let abe = KeyPolicyABE::< DCRTPolyMatrix, DCRTPolyHashSampler, @@ -281,7 +271,7 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { ¶ms, config.knapsack_size, config.e_b_sigma, - trapdoor_sampler, + trapdoor_sigma, ); let mut t_read_mpk = Duration::ZERO; let mut t_enc = Duration::ZERO; @@ -304,7 +294,7 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { ); let ct: Ciphertext = timed_read( "enc", - || abe.enc(params.clone(), mpk, &vec![BigUint::ZERO; config.arith_input_size], true), + || abe.enc(¶ms, mpk, &vec![BigUint::ZERO; config.arith_input_size], true), &mut t_enc, ); log_mem("finished enc"); @@ -333,11 +323,8 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { }, &mut t_read_fsk, ); - let bit: bool = timed_read( - "dec", - || abe.dec(params.clone(), ct, mpk, fsk, config.arith_height), - &mut t_dec, - ); + let bit: bool = + timed_read("dec", || abe.dec(¶ms, ct, mpk, fsk, config.arith_height), &mut t_dec); log_mem(format!("finished decryption: result={}", bit)); Ok(()) } diff --git a/src/abe.rs b/src/abe.rs index f4916f8..125446b 100644 --- a/src/abe.rs +++ b/src/abe.rs @@ -12,7 +12,7 @@ use mxx::{ circuit::PolyCircuit, element::PolyElem, gadgets::arith::nested_rns::{NestedRnsPoly, NestedRnsPolyContext, encode_nested_rns_poly}, - lookup::lwe_eval::{LweBggEncodingPltEvaluator, LweBggPubKeyEvaluator}, + lookup::ggh15_eval::{GGH15BGGEncodingPltEvaluator, GGH15BGGPubKeyPltEvaluator}, matrix::PolyMatrix, poly::{Poly, PolyParams}, sampler::{DistType, PolyHashSampler, PolyTrapdoorSampler, PolyUniformSampler}, @@ -25,64 +25,68 @@ const TAG_BGG_PUBKEY: &[u8] = b"BGG_PUBKEY"; pub struct KeyPolicyABE< M: PolyMatrix + 'static, - SH: PolyHashSampler<[u8; 32], M = M> + Send + Sync, - ST: PolyTrapdoorSampler + Clone + Send + Sync, - SU: PolyUniformSampler + Send + Sync, + HS: PolyHashSampler<[u8; 32], M = M> + Send + Sync, + TS: PolyTrapdoorSampler + Clone + Send + Sync, + US: PolyUniformSampler + Send + Sync, > { - pub e_b_sigma: f64, pub p_moduli_bits: usize, - pub p_moduli_depth: usize, + // pub p_moduli_depth: usize, pub scale: u64, pub crt_depth: usize, pub knapsack_size: Option, - pub trapdoor_sampler: ST, - _sh: PhantomData, - _su: PhantomData, + pub trapdoor_sigma: f64, + pub e_b_sigma: f64, + // pub trapdoor_sampler: TS, + _hs: PhantomData, + _us: PhantomData, + _ts: PhantomData, } impl< M: PolyMatrix + 'static, - SH: PolyHashSampler<[u8; 32], M = M> + Send + Sync, - ST: PolyTrapdoorSampler + Clone + Send + Sync, - SU: PolyUniformSampler + Send + Sync, -> KeyPolicyABE + HS: PolyHashSampler<[u8; 32], M = M> + Send + Sync, + TS: PolyTrapdoorSampler + Clone + Send + Sync, + US: PolyUniformSampler + Send + Sync, +> KeyPolicyABE { pub fn new( p_moduli_bits: usize, scale: u64, params: &::Params, knapsack_size: Option, + trapdoor_sigma: f64, e_b_sigma: f64, - trapdoor_sampler: ST, ) -> Self { assert!(p_moduli_bits > 1, "p_moduli_bits must be at least 2 for NestedRns"); - let (_, crt_bits, crt_depth) = params.to_crt(); + let (_, _, crt_depth) = params.to_crt(); // NestedRns chooses the number of level-1 moduli as ceil(2 * |q_bits| / (p_bits - 1)) - let p_moduli_depth = (2 * crt_bits).div_ceil(p_moduli_bits - 1); + // let p_moduli_depth = (2 * crt_bits).div_ceil(p_moduli_bits - 1); Self { p_moduli_bits, - p_moduli_depth, + // p_moduli_depth, scale, crt_depth, knapsack_size, + trapdoor_sigma, e_b_sigma, - trapdoor_sampler, - _sh: PhantomData, - _su: PhantomData, + _hs: PhantomData, + _us: PhantomData, + _ts: PhantomData, } } pub fn setup( &self, - params: ::Params, + params: &::Params, num_inputs: usize, - ) -> (MasterPK, MasterSK) { + ) -> (MasterPK, MasterSK) { let seed: [u8; 32] = rand::random(); - let (b_trapdoor, b_matrix) = self.trapdoor_sampler.trapdoor(¶ms, 1); + let trapdoor_sampler = TS::new(params, self.trapdoor_sigma); + let (b_trapdoor, b_matrix) = trapdoor_sampler.trapdoor(params, 1); let b_trapdoor = Arc::new(b_trapdoor); let b_matrix = Arc::new(b_matrix); - let uniform_sampler = SU::new(); - let u = uniform_sampler.sample_uniform(¶ms, 1, 1, DistType::FinRingDist); + let uniform_sampler = US::new(); + let u = uniform_sampler.sample_uniform(params, 1, 1, DistType::FinRingDist); let mpk = MasterPK::new(num_inputs, seed, b_matrix, u); let msk = MasterSK::new(b_trapdoor); (mpk, msk) @@ -90,7 +94,7 @@ impl< pub fn enc( &self, - params: ::Params, + params: &::Params, mpk: MasterPK, inputs: &[BigUint], message: bool, @@ -101,7 +105,7 @@ impl< "provided inputs ({num_inputs}) must match mpk.num_inputs ({})", mpk.num_inputs ); - let uniform_sampler = SU::new(); + let uniform_sampler = US::new(); let s = uniform_sampler.sample_uniform(¶ms, 1, 1, DistType::TernaryDist); let b_col_size = 2 + params.modulus_digits(); let c_b_error: M = { @@ -122,12 +126,12 @@ impl< first_part.concat_columns(&[&second_part, &third_part]) }; let c_b = s.clone() * mpk.b_matrix.as_ref() + &c_b_error; - let bgg_encoding_sampler = BGGEncodingSampler::::new(¶ms, &s.get_row(0), None); + let bgg_encoding_sampler = BGGEncodingSampler::::new(params, &s.get_row(0), None); // let (_, _, crt_depth) = params.to_crt(); // let p_moduli_depth = (2 * crt_bits).div_ceil(self.p_moduli_bits - 1); let plaintexts = inputs .iter() - .flat_map(|input| encode_nested_rns_poly(self.p_moduli_bits, ¶ms, input)) + .flat_map(|input| encode_nested_rns_poly(self.p_moduli_bits, params, input)) .collect::>(); // let expected_plaintexts = mpk.num_inputs * crt_depth * self.p_moduli_depth; // assert_eq!( @@ -138,7 +142,7 @@ impl< // expected_plaintexts // ); let reveal_plaintexts = vec![true; plaintexts.len()]; - let bgg_pubkey_sampler = BGGPublicKeySampler::<_, SH>::new(mpk.seed, 1); + let bgg_pubkey_sampler = BGGPublicKeySampler::<_, HS>::new(mpk.seed, 1); let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); let bgg_encodings_no_error = bgg_encoding_sampler.sample(¶ms, &pubkeys, &plaintexts); let encode_col_size = params.modulus_digits(); @@ -198,9 +202,9 @@ impl< pub async fn keygen( &self, - params: ::Params, + params: &::Params, mpk: MasterPK, - msk: MasterSK, + msk: MasterSK, height: u32, dir_path: PathBuf, ) -> FuncSK { @@ -216,42 +220,41 @@ impl< )); NestedRnsPoly::benchmark_multiplication_tree( ctx, - ¶ms, + params, &mut circuit, height as usize, ); circuit }; - let plt_evaluator = LweBggPubKeyEvaluator::::new( + let plt_evaluator = GGH15BGGPubKeyPltEvaluator::::new( mpk.seed, - self.trapdoor_sampler.clone(), + self.trapdoor_sigma, + self.e_b_sigma, + ¶ms, mpk.b_matrix.clone(), msk.b_trapdoor.clone(), dir_path.clone(), + false, ); let reveal_plaintexts = vec![true; circuit.num_input()]; - let bgg_pubkey_sampler = BGGPublicKeySampler::<_, SH>::new(mpk.seed, 1); + let bgg_pubkey_sampler = BGGPublicKeySampler::<_, HS>::new(mpk.seed, 1); let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); - let result = circuit.eval(¶ms, &pubkeys[0], &pubkeys[1..], Some(plt_evaluator)); + let result = circuit.eval(params, &pubkeys[0], &pubkeys[1..], Some(plt_evaluator)); info!("finished evaluation of pubkeys"); wait_for_all_writes(dir_path.clone()).await.unwrap(); info!("finished write files"); let a_f = result[0].clone().matrix; - let u_f = self.trapdoor_sampler.preimage_extend( - ¶ms, - &msk.b_trapdoor, - &mpk.b_matrix, - &a_f, - &mpk.u, - ); + let trapdoor_sampler = TS::new(params, self.trapdoor_sigma); + let u_f = + trapdoor_sampler.preimage_extend(¶ms, &msk.b_trapdoor, &mpk.b_matrix, &a_f, &mpk.u); assert_eq!(result.len(), 1); FuncSK { a_f, u_f, dir_path } } pub fn dec( &self, - params: ::Params, + params: &::Params, ct: Ciphertext, mpk: MasterPK, fsk: FuncSK, @@ -269,7 +272,7 @@ impl< )); NestedRnsPoly::benchmark_multiplication_tree( ctx, - ¶ms, + params, &mut circuit, height as usize, ); @@ -282,13 +285,18 @@ impl< "ciphertext must contain exactly 1 + circuit.num_input() encodings" ); let dir_path: PathBuf = fsk.dir_path; - let bgg_evaluator = - LweBggEncodingPltEvaluator::::new(mpk.seed, dir_path, ct.c_b.clone()); - let result = circuit.eval(¶ms, &encodings[0], &encodings[1..], Some(bgg_evaluator)); + let bgg_evaluator = GGH15BGGEncodingPltEvaluator::::new( + mpk.seed, + ¶ms, + dir_path, + mpk.b_matrix.row_size(), + ct.c_b.clone(), + ); + let result = circuit.eval(params, &encodings[0], &encodings[1..], Some(bgg_evaluator)); // 5. Let `c_f := s^T*A_f + e_{c_f}` in $\mathcal{R}_{q}^{1 \times m}$ // be the BGG+ encoding corresponding to the output wire of `poly_circuit`. let v = ct.c_b.concat_columns(&[&result[0].vector]) * fsk.u_f; let z = ct.c_u - &v.get_row(0)[0]; - z.extract_bits_with_threshold(¶ms)[0] + z.extract_bits_with_threshold(params)[0] } } diff --git a/src/simulator.rs b/src/simulator.rs index 3665ff5..48823ec 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -3,12 +3,15 @@ pub use mxx::simulator::lattice_estimator::run_lattice_estimator_cli; use mxx::{ circuit::PolyCircuit, gadgets::arith::nested_rns::{NestedRnsPoly, NestedRnsPolyContext}, - poly::dcrt::{params::DCRTPolyParams, poly::DCRTPoly}, + poly::{ + PolyParams, + dcrt::{params::DCRTPolyParams, poly::DCRTPoly}, + }, simulator::{ SimulatorContext, + error_norm::*, lattice_estimator::{Distribution, EstimatorCliError}, poly_matrix_norm::PolyMatrixNorm, - wire_norm::NormPltLweEvaluator, }, utils::{bigdecimal_bits_ceil, log_mem}, }; @@ -50,109 +53,112 @@ pub enum SimulatorError { } // Output (crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) or None -pub fn bruteforce_params_for_bench_arith_circuit( - target_secpar: u32, - crt_bits: u32, - crt_depth_range: (u32, u32), - base_bits_range: (u32, u32), - log_dim_range: (u32, u32), - p_moduli_bits: usize, - scale: u64, - height: usize, -) -> Option<(u32, u32, u32, f64, u32)> { - // (cost, crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) - let outputs: Vec<(u32, u32, u32, u32, f64, u32)> = - (base_bits_range.0..=base_bits_range.1) - .into_par_iter() - .flat_map(|base_bits| { - let mut local = Vec::<(u32, u32, u32, u32, f64, u32)>::new(); - let mut lo = crt_depth_range.0; - let mut hi = crt_depth_range.1; - while lo <= hi { - let crt_depth = lo + ((hi - lo) / 2); - log::info!("base_bits {base_bits} crt_depth {crt_depth}"); - let (log_dim, e_b_log_alpha, knapsack_size) = match find_min_ring_dim( - target_secpar, - crt_bits, - crt_depth, - base_bits, - log_dim_range, - ) { - Ok(result) => result, - Err(e) => { - log::info!( - "Security error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, p_moduli_bits = {}, scale = {}, height = {}: {}", - target_secpar, crt_bits, base_bits, crt_depth, p_moduli_bits, scale, height, e - ); - // try smaller crt_depth - if crt_depth == 0 { break; } - hi = crt_depth - 1; - continue; - } - }; - log::info!( - "Found log_dim = {}, e_b_log_alpha = {}, knapsack_size = {}", - log_dim, - e_b_log_alpha, - knapsack_size - ); - let ring_dim = (1 << log_dim) as u32; - let params = DCRTPolyParams::new(ring_dim, crt_depth as usize, crt_bits as usize, base_bits); - log::info!("params constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); - let mut circuit = PolyCircuit::::new(); - let nested_rns_ctx = Arc::new(NestedRnsPolyContext::setup(&mut circuit, ¶ms, p_moduli_bits, scale, true)); - NestedRnsPoly::benchmark_multiplication_tree(nested_rns_ctx, ¶ms, &mut circuit, height); - // ArithmeticCircuit::benchmark_multiplication_tree(¶ms, limb_bit_size, num_eval_slots.unwrap_or(ring_dim as usize), height,true); - log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); - log::info!("circuit size {:?}", circuit.count_gates_by_type_vec()); - log::info!("poly circuit non_free_depth {}",circuit.non_free_depth()); - match check_correctness( - target_secpar, - log_dim, - crt_bits, - crt_depth, - base_bits, - knapsack_size, - e_b_log_alpha, - &circuit, - ) { - Ok(cost) => { - log::info!( - "Found with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}, cost = {}", - target_secpar, crt_bits, base_bits, crt_depth, cost - ); - local.push(( - cost, - crt_depth, - base_bits, - log_dim, - 2.0f64.powf( - crt_bits as f64 * crt_depth as f64 + e_b_log_alpha as f64, - ), - knapsack_size, - )); - // search smaller crt_depth to continue binary search - if crt_depth == 0 { break; } - hi = crt_depth - 1; - } - Err(e) => { - log::info!( - "Correctness error with target_secpar = {}, crt_bits = {}, base_bits = {}, crt_depth = {}: {}", - target_secpar, crt_bits, base_bits, crt_depth, e - ); - // try larger crt_depth - lo = crt_depth + 1; - } - } - } - local - }) - .collect(); - outputs - .into_iter() - .min_by(|x, y| x.0.cmp(&y.0)) - .map(|outs| (outs.1, outs.2, outs.3, outs.4, outs.5)) -} +// pub fn bruteforce_params_for_bench_arith_circuit( +// target_secpar: u32, +// crt_bits: u32, +// crt_depth_range: (u32, u32), +// base_bits_range: (u32, u32), +// log_dim_range: (u32, u32), +// p_moduli_bits: usize, +// scale: u64, +// height: usize, +// ) -> Option<(u32, u32, u32, f64, u32)> { +// // (cost, crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) +// let outputs: Vec<(u32, u32, u32, u32, f64, u32)> = +// (base_bits_range.0..=base_bits_range.1) +// .into_par_iter() +// .flat_map(|base_bits| { +// let mut local = Vec::<(u32, u32, u32, u32, f64, u32)>::new(); +// let mut lo = crt_depth_range.0; +// let mut hi = crt_depth_range.1; +// while lo <= hi { +// let crt_depth = lo + ((hi - lo) / 2); +// log::info!("base_bits {base_bits} crt_depth {crt_depth}"); +// let (log_dim, e_b_log_alpha, knapsack_size) = match find_min_ring_dim( +// target_secpar, +// crt_bits, +// crt_depth, +// base_bits, +// log_dim_range, +// ) { +// Ok(result) => result, +// Err(e) => { +// log::info!( +// "Security error with target_secpar = {}, crt_bits = {}, base_bits +// = {}, crt_depth = {}, p_moduli_bits = {}, scale = {}, height = {}: {}", +// target_secpar, crt_bits, base_bits, crt_depth, p_moduli_bits, scale, height, e +// ); // try smaller crt_depth +// if crt_depth == 0 { break; } +// hi = crt_depth - 1; +// continue; +// } +// }; +// log::info!( +// "Found log_dim = {}, e_b_log_alpha = {}, knapsack_size = {}", +// log_dim, +// e_b_log_alpha, +// knapsack_size +// ); +// let ring_dim = (1 << log_dim) as u32; +// let params = DCRTPolyParams::new(ring_dim, crt_depth as usize, crt_bits as +// usize, base_bits); log::info!("params constructed with crt_depth = {}, +// log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, +// base_bits, knapsack_size, e_b_log_alpha); let mut circuit = +// PolyCircuit::::new(); let nested_rns_ctx = +// Arc::new(NestedRnsPolyContext::setup(&mut circuit, ¶ms, p_moduli_bits, scale, true)); +// NestedRnsPoly::benchmark_multiplication_tree(nested_rns_ctx, ¶ms, &mut +// circuit, height); // +// ArithmeticCircuit::benchmark_multiplication_tree(¶ms, limb_bit_size, +// num_eval_slots.unwrap_or(ring_dim as usize), height,true); +// log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size +// = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); +// log::info!("circuit size {:?}", circuit.count_gates_by_type_vec()); +// log::info!("poly circuit non_free_depth {}",circuit.non_free_depth()); +// match check_correctness( +// target_secpar, +// params, +// base_bits, +// knapsack_size, +// e_b_log_alpha, +// &circuit, +// ) { +// Ok(cost) => { +// log::info!( +// "Found with target_secpar = {}, crt_bits = {}, base_bits = {}, +// crt_depth = {}, cost = {}", target_secpar, crt_bits, base_bits, +// crt_depth, cost ); +// local.push(( +// cost, +// crt_depth, +// base_bits, +// log_dim, +// 2.0f64.powf( +// crt_bits as f64 * crt_depth as f64 + e_b_log_alpha as f64, +// ), +// knapsack_size, +// )); +// // search smaller crt_depth to continue binary search +// if crt_depth == 0 { break; } +// hi = crt_depth - 1; +// } +// Err(e) => { +// log::info!( +// "Correctness error with target_secpar = {}, crt_bits = {}, +// base_bits = {}, crt_depth = {}: {}", target_secpar, crt_bits, +// base_bits, crt_depth, e ); +// // try larger crt_depth +// lo = crt_depth + 1; +// } +// } +// } +// local +// }) +// .collect(); +// outputs +// .into_iter() +// .min_by(|x, y| x.0.cmp(&y.0)) +// .map(|outs| (outs.1, outs.2, outs.3, outs.4, outs.5)) +// } // Output (crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size) or None pub fn bruteforce_params_for_bench_nested_crt_circuit( @@ -216,14 +222,12 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( log::info!("circuit constructed with crt_depth = {}, log_dim = {}, base_bits = {}, knapsack_size = {}, e_b_log_alpha = {}", crt_depth, log_dim, base_bits, knapsack_size, e_b_log_alpha); log::info!("circuit size {:?}", circuit.count_gates_by_type_vec()); log::info!("poly circuit non_free_depth {}",circuit.non_free_depth()); + let input_norm_bound = BigDecimal::from_u64(1<<(2*p_moduli_bits)).unwrap(); match check_correctness( - target_secpar, - log_dim, - crt_bits, - crt_depth, - base_bits, + ¶ms, knapsack_size, e_b_log_alpha, + input_norm_bound, &circuit, ) { Ok(cost) => { @@ -420,28 +424,24 @@ fn pow_two_bigdecimal(exponent: i64) -> BigDecimal { } fn check_correctness( - target_secpar: u32, - log_dim: u32, - crt_bits: u32, - crt_depth: u32, - base_bits: u32, + params: &DCRTPolyParams, knapsack_size: u32, e_b_log_alpha: i64, + input_norm_bound: BigDecimal, circuit: &PolyCircuit, ) -> Result { let input_size = circuit.num_input(); - let ring_dim = BigUint::from(2u32).pow(log_dim); - let log_q = crt_bits * crt_depth; - let q = BigUint::from(2u32).pow(log_q); - let m_g = (crt_bits.div_ceil(base_bits) * crt_depth) as usize; + let ring_dim = params.ring_dimension(); + let log_q = params.modulus_bits() as u32; + let m_g = params.modulus_digits(); + let base_bits = params.base_bits(); let m_b = m_g + 2; log::info!("e_b_log_alpha {}", e_b_log_alpha); let e_b_sigma = pow_two_bigdecimal(i64::from(log_q) + e_b_log_alpha); log::info!("e_b_sigma {}", e_b_sigma); - let secpar_sqrt = BigDecimal::from_u32(target_secpar).unwrap().sqrt().unwrap(); - let ring_dim_sqrt = BigDecimal::from_biguint(ring_dim.clone(), 0).sqrt().unwrap(); + let ring_dim_sqrt = BigDecimal::from_u32(ring_dim).unwrap().sqrt().unwrap(); let base = BigDecimal::from_biguint((BigUint::from(1u32)) << base_bits, 0); - let sim_ctx = Arc::new(SimulatorContext::new(secpar_sqrt, ring_dim_sqrt, base, m_g)); + let sim_ctx = Arc::new(SimulatorContext::new(ring_dim_sqrt, base, 1, m_g)); let e_b = PolyMatrixNorm::sample_gauss(sim_ctx.clone(), 1, m_b, e_b_sigma.clone()); let r_mat = PolyMatrixNorm::new( @@ -453,18 +453,22 @@ fn check_correctness( ); let e_a = &e_b * &r_mat; log::info!("before simulation: e_b = {:?}, e_a = {:?}", e_b, e_a); - let out_wire_norms = circuit.simulate_max_h_norm( + let plt_evaluator = NormPltGGH15Evaluator::new(sim_ctx.clone(), &e_b_sigma); + let preimage_norm = plt_evaluator.k_g.poly_norm.norm.clone(); + let out_errors = circuit.simulate_max_error_norm( sim_ctx.clone(), - BigDecimal::from_u32(crt_bits).unwrap(), + input_norm_bound, input_size, + &e_a.poly_norm.norm, + Some(plt_evaluator), ); log::info!("after simulation"); - let max_out_wire = out_wire_norms - .into_iter() - .max_by(|a, b| a.h_norm.poly_norm.norm.cmp(&b.h_norm.poly_norm.norm)) - .unwrap(); - let (max_h_top, max_h_bottom) = max_out_wire.h_norm.split_rows(m_b); - let max_h_top_bits = bigdecimal_bits_ceil(&max_h_top.poly_norm.norm); + // let max_out_error = out_errors + // .into_iter() + // .max_by(|a, b| a.matrix_norm.poly_norm.norm.cmp(&b.matrix_norm.poly_norm.norm)) + // .unwrap(); + // let (max_h_top, max_h_bottom) = max_out_error.matrix_norm.poly_norm.split_rows(m_b); + // let max_h_top_bits = bigdecimal_bits_ceil(&max_h_top.poly_norm.norm); // { // let s = max_h_top // .poly_norm @@ -482,9 +486,9 @@ fn check_correctness( // 0usize // } // }; - log::info!("max_h_top_bits bits {}", max_h_top_bits); + // log::info!("max_h_top_bits bits {}", max_h_top_bits); - let max_h_bottom_bits = bigdecimal_bits_ceil(&max_h_bottom.poly_norm.norm); + // let max_h_bottom_bits = bigdecimal_bits_ceil(&max_h_bottom.poly_norm.norm); // { // let s = max_h_bottom // .poly_norm @@ -502,134 +506,32 @@ fn check_correctness( // 0usize // } // }; - log::info!("max_h_bottom_bits bits {}", max_h_bottom_bits); + // log::info!("max_h_bottom_bits bits {}", max_h_bottom_bits); log::info!("e_b bits {}", bigdecimal_bits_ceil(&e_b.poly_norm.norm)); log::info!("e_a bits {}", bigdecimal_bits_ceil(&e_a.poly_norm.norm)); - let e_after_eval = &e_b * max_h_top + e_a * max_h_bottom; - let e_after_eval_bits = bigdecimal_bits_ceil(&e_after_eval.poly_norm.norm); - // { - // let s = e_after_eval - // .poly_norm - // .norm - // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - // .to_string(); - // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - // let bytes = n.to_bytes_be(); - // if bytes.is_empty() { - // 0usize - // } else { - // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - // } - // } else { - // 0usize - // } - // }; + let e_after_eval = out_errors + .into_iter() + .max_by(|a, b| a.matrix_norm.poly_norm.norm.cmp(&b.matrix_norm.poly_norm.norm)) + .unwrap(); + let e_after_eval_bits = bigdecimal_bits_ceil(&e_after_eval.matrix_norm.poly_norm.norm); log::info!("e_after_eval_bits bits {}", e_after_eval_bits); - let plt_eval = NormPltLweEvaluator::new(sim_ctx.clone(), input_size); - let mut preimage_norm_top = plt_eval.preimage1_norm.clone(); - preimage_norm_top.nrow = m_b; - preimage_norm_top.ncol = 1; - let mut preimage_norm_bottom = plt_eval.preimage2_norm.clone(); - preimage_norm_bottom.ncol = 1; + let preimage_norm_top = + PolyMatrixNorm::new(sim_ctx.clone(), m_b, 1, preimage_norm.clone(), None); + let preimage_norm_bottom = + PolyMatrixNorm::new(sim_ctx.clone(), m_g, 1, preimage_norm.clone(), None); let e_u = PolyMatrixNorm::sample_gauss(sim_ctx.clone(), 1, 1, e_b_sigma); - let e_final = &e_b * preimage_norm_top + e_after_eval * preimage_norm_bottom + e_u; + let e_final = &e_b * preimage_norm_top + e_after_eval.matrix_norm * preimage_norm_bottom + e_u; let e_final_bits = bigdecimal_bits_ceil(&e_final.poly_norm.norm); - // { - // let s = e_final - // .poly_norm - // .norm - // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - // .to_string(); - // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - // let bytes = n.to_bytes_be(); - // if bytes.is_empty() { - // 0usize - // } else { - // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - // } - // } else { - // 0usize - // } - // }; log::info!("e_final_bits bits {}", e_final_bits); - let q_over_4 = BigDecimal::from_biguint(q, 0) / BigDecimal::from_u32(4).unwrap(); + let q_over_4 = BigDecimal::from_biguint(params.modulus().as_ref().clone(), 0) / + BigDecimal::from_u32(4).unwrap(); let q_over_4_bits = bigdecimal_bits_ceil(&q_over_4); - // { - // let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); - // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - // let bytes = n.to_bytes_be(); - // if bytes.is_empty() { - // 0usize - // } else { - // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - // } - // } else { - // 0usize - // } - // }; if q_over_4 > e_final.poly_norm.norm { - // Compute bit lengths of q_over_4 and e_final (after rounding up to integer) - // let q_over_4_bits = { - // let s = q_over_4.with_scale_round(0, bigdecimal::RoundingMode::Ceiling).to_string(); - // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - // let bytes = n.to_bytes_be(); - // if bytes.is_empty() { - // 0usize - // } else { - // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - // } - // } else { - // 0usize - // } - // }; - // let e_final_bits = { - // let s = e_final - // .poly_norm - // .norm - // .with_scale_round(0, bigdecimal::RoundingMode::Ceiling) - // .to_string(); - // if let Some(n) = BigUint::parse_bytes(s.as_bytes(), 10) { - // let bytes = n.to_bytes_be(); - // if bytes.is_empty() { - // 0usize - // } else { - // (bytes.len() - 1) * 8 + (8 - bytes[0].leading_zeros() as usize) - // } - // } else { - // 0usize - // } - // }; - log_mem(format!("q_over_4_bits: {}, e_final_bits: {}", q_over_4_bits, e_final_bits)); - Ok(log_dim * m_g as u32) + Ok(log_q * m_g as u32) } else { Err(SimulatorError::NotCorrect { e_bits: e_final_bits, q_over_4_bits }) } } - -#[cfg(test)] -mod tests { - use super::*; - // Initialize logger for test output - use env_logger; - - #[test] - fn test_bruteforce_params() { - // Initialize env_logger once for tests; ignore if already set. - let _ = env_logger::builder().is_test(true).try_init(); - let params = bruteforce_params_for_bench_arith_circuit( - 100, - 41, - (2, 4), - (15, 18), - (13, 16), - 6, - 1 << 8, - 3, - ); - assert!(params.is_some()); - println!("params: {:?}", params); - } -} From 0101523d2924dc02bab699f901600b567a49c125 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Thu, 8 Jan 2026 21:55:32 +0900 Subject: [PATCH 12/15] use the latest branch of mxx --- Cargo.lock | 19 ++++++++++++++++++- Cargo.toml | 2 +- abe/Cargo.toml | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14b02ec..57da5cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -683,11 +683,12 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fggh_lut_2512#50ef06f11ef231d79afdef27e6f31c30aa47c932" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fgpu#16d3697fab3aa663054b2e18b585a930d020c9f5" dependencies = [ "bigdecimal", "bincode", "bitvec", + "cc", "dashmap", "digest", "itertools", @@ -699,6 +700,7 @@ dependencies = [ "primal", "rand 0.9.2", "rayon", + "sequential-test", "serde", "serde_json", "tempfile", @@ -1022,6 +1024,21 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d68f2ec51b097e4c1a75b681a8bec621909b5e91f15bb7b840c4f2f7b01148b2" +[[package]] +name = "sequential-macro" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb5facc5f409a55d25bf271c853402a00e1187097d326757043f5dd711944d07" + +[[package]] +name = "sequential-test" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d9c0d773bc7e7733264f460e5dfa00b2510421ddd6284db0749eef8dfb79e9" +dependencies = [ + "sequential-macro", +] + [[package]] name = "serde" version = "1.0.228" diff --git a/Cargo.toml b/Cargo.toml index 94e4964..a80fe0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/ggh_lut_2512" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/gpu" } rand = { version = "0.9.0", features = ["std_rng"] } num-bigint = { version = "0.4", features = ["serde"] } thiserror = "2.0.16" diff --git a/abe/Cargo.toml b/abe/Cargo.toml index d48b862..29dc2d3 100644 --- a/abe/Cargo.toml +++ b/abe/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/ggh_lut_2512" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/gpu" } arithmetic-abe = { path = "../" } anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } From b046e8070802cb8efcc923a0efc7e740e99b7db3 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Thu, 8 Jan 2026 21:57:56 +0900 Subject: [PATCH 13/15] disable OpenOMP in ci --- .github/workflows/benchmark-self-hosted.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/benchmark-self-hosted.yml b/.github/workflows/benchmark-self-hosted.yml index 9888be1..50873f2 100644 --- a/.github/workflows/benchmark-self-hosted.yml +++ b/.github/workflows/benchmark-self-hosted.yml @@ -62,7 +62,7 @@ jobs: run: | cd openfhe mkdir -p build && cd build - cmake .. + cmake .. -DWITH_OPENMP=OFF make -j"$(nproc)" sudo make install echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/openfhe.conf From 681fb56e96542c1b4c4dbfacc2803bb05d36d5cb Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Tue, 3 Feb 2026 18:18:23 +0900 Subject: [PATCH 14/15] Support NestedRNS --- Cargo.lock | 155 +++++++++++++---------------------------------- Cargo.toml | 2 +- abe/Cargo.toml | 3 +- abe/src/main.rs | 59 +++++++++--------- src/abe.rs | 39 ++++++------ src/simulator.rs | 13 ++-- 6 files changed, 107 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57da5cd..4a1d705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "keccak-asm", "mxx", "num-bigint", + "rand 0.9.2", "serde", "tokio", "toml", @@ -116,9 +117,9 @@ checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "bigdecimal" -version = "0.4.9" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "560f42649de9fa436b73517378a147ec21f6c997a546581df4b4b31677828934" +checksum = "4d6867f1565b3aad85681f1015055b087fcfd840d6aeee6eee7f2da317603695" dependencies = [ "autocfg", "libm", @@ -182,9 +183,9 @@ checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" [[package]] name = "cc" -version = "1.2.50" +version = "1.2.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f50d563227a1c37cc0a263f64eca3334388c01c5e4c4861a9def205c614383c" +checksum = "cd4932aefd12402b36c60956a4fe0035421f544799057659ff86f923657aada3" dependencies = [ "find-msvc-tools", "shlex", @@ -211,9 +212,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.53" +version = "4.5.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" +checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394" dependencies = [ "clap_builder", "clap_derive", @@ -221,9 +222,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.53" +version = "4.5.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" +checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00" dependencies = [ "anstream", "anstyle", @@ -446,9 +447,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "find-msvc-tools" -version = "0.1.5" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" +checksum = "f449e6c6c08c865631d4890cfacf252b3d396c9bcc83adb6623cdb02a8336c41" [[package]] name = "foldhash" @@ -484,12 +485,6 @@ dependencies = [ "wasip2", ] -[[package]] -name = "hamming" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65043da274378d68241eb9a8f8f8aa54e349136f7b8e12f63e3ef44043cc30e1" - [[package]] name = "hashbrown" version = "0.14.5" @@ -534,9 +529,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.12.1" +version = "2.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ad4bb2b565bca0645f4d68c5c9af97fba094e9791da685bf83cb5f3ce74acf2" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" dependencies = [ "equivalent", "hashbrown 0.16.1", @@ -559,15 +554,15 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ee5b5339afb4c41626dde77b7a611bd4f2c202b897852b4bcf5d03eddc61010" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" [[package]] name = "jiff" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a87d9b8105c23642f50cbbae03d1f75d8422c5cb98ce7ee9271f7ff7505be6b8" +checksum = "e67e8da4c49d6d9909fe03361f9b620f58898859f5c7aded68351e85e71ecf50" dependencies = [ "jiff-static", "log", @@ -578,9 +573,9 @@ dependencies = [ [[package]] name = "jiff-static" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b787bebb543f8969132630c51fd0afab173a86c6abae56ff3b9e5e3e3f9f6e58" +checksum = "e0c84ee7f197eca9a86c6fd6cb771e55eb991632f15f2bc3ca6ec838929e6e78" dependencies = [ "proc-macro2", "quote", @@ -615,9 +610,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.178" +version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" [[package]] name = "libm" @@ -683,12 +678,11 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fgpu#16d3697fab3aa663054b2e18b585a930d020c9f5" +source = "git+https://github.com/MachinaIO/mxx.git?branch=main#e53d03029298982a3e2cc5931571a403eb988be2" dependencies = [ "bigdecimal", "bincode", "bitvec", - "cc", "dashmap", "digest", "itertools", @@ -697,10 +691,8 @@ dependencies = [ "num-bigint", "num-traits", "openfhe", - "primal", "rand 0.9.2", "rayon", - "sequential-test", "serde", "serde_json", "tempfile", @@ -764,7 +756,7 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "openfhe" version = "0.3.2" -source = "git+https://github.com/MachinaIO/openfhe-rs.git#9cc3775e2be28012469a4dc89b7d2a750444ce93" +source = "git+https://github.com/MachinaIO/openfhe-rs.git#aaea3779c29559153b7018c05b9e9153e54e7c0d" dependencies = [ "cxx", "cxx-build", @@ -793,9 +785,9 @@ checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" [[package]] name = "portable-atomic" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f59e70c4aef1e55797c2e8fd94a4f2a973fc972cfde0e0b05f683667b0cd39dd" +checksum = "f89776e4d69bb58bc6993e99ffa1d11f228b839984854c7daeb5d37f87cbe950" [[package]] name = "portable-atomic-util" @@ -815,66 +807,20 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "primal" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1e5f354948532e6017fc91f9a5ff5ba1be0dabd3a0c9e9c417969cd4c1ad6e8" -dependencies = [ - "primal-check", - "primal-estimate", - "primal-sieve", -] - -[[package]] -name = "primal-bit" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "252429dbb8aeacc3233df500dc3a6a367bf28eb3a711272884d7540a7b636055" -dependencies = [ - "hamming", -] - -[[package]] -name = "primal-check" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08" -dependencies = [ - "num-integer", -] - -[[package]] -name = "primal-estimate" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a432100a0b3a61085e75b5f89e9f42de73c0acb7dea5038b893697918105d822" - -[[package]] -name = "primal-sieve" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e982796d82203351983d3602a8d6372d1d7894e86960047ba0d4b7426a5edd3" -dependencies = [ - "primal-bit", - "primal-estimate", - "smallvec", -] - [[package]] name = "proc-macro2" -version = "1.0.103" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.42" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" dependencies = [ "proc-macro2", ] @@ -1024,21 +970,6 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d68f2ec51b097e4c1a75b681a8bec621909b5e91f15bb7b840c4f2f7b01148b2" -[[package]] -name = "sequential-macro" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb5facc5f409a55d25bf271c853402a00e1187097d326757043f5dd711944d07" - -[[package]] -name = "sequential-test" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d9c0d773bc7e7733264f460e5dfa00b2510421ddd6284db0749eef8dfb79e9" -dependencies = [ - "sequential-macro", -] - [[package]] name = "serde" version = "1.0.228" @@ -1071,9 +1002,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.147" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6af14725505314343e673e9ecb7cd7e8a36aa9791eb936235a3567cc31447ae4" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "itoa", "memchr", @@ -1130,9 +1061,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.111" +version = "2.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" dependencies = [ "proc-macro2", "quote", @@ -1218,9 +1149,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.48.0" +version = "1.49.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" +checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" dependencies = [ "pin-project-lite", "tokio-macros", @@ -1239,9 +1170,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.9.10+spec-1.1.0" +version = "0.9.11+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0825052159284a1a8b4d6c0c86cbc801f2da5afd2b225fa548c72f2e74002f48" +checksum = "f3afc9a848309fe1aaffaed6e1546a7a14de1f935dc9d89d32afd9a44bab7c46" dependencies = [ "indexmap", "serde_core", @@ -1612,18 +1543,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.31" +version = "0.8.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd74ec98b9250adb3ca554bdde269adf631549f51d8a8f8f0a10b50f1cb298c3" +checksum = "668f5168d10b9ee831de31933dc111a459c97ec93225beb307aed970d1372dfd" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.31" +version = "0.8.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a8d209fdf45cf5138cbb5a506f6b52522a25afccc534d1475dad8e31105c6a" +checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1" dependencies = [ "proc-macro2", "quote", @@ -1632,6 +1563,6 @@ dependencies = [ [[package]] name = "zmij" -version = "0.1.8" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1dccf46b25b205e4bebe1d5258a991df1cc17801017a845cb5b3fe0269781aa" +checksum = "2fc5a66a20078bf1251bde995aa2fdcc4b800c70b5d92dd2c62abc5c60f679f8" diff --git a/Cargo.toml b/Cargo.toml index a80fe0f..a1ce2d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/gpu" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "main" } rand = { version = "0.9.0", features = ["std_rng"] } num-bigint = { version = "0.4", features = ["serde"] } thiserror = "2.0.16" diff --git a/abe/Cargo.toml b/abe/Cargo.toml index 29dc2d3..117b107 100644 --- a/abe/Cargo.toml +++ b/abe/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/gpu" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "main" } arithmetic-abe = { path = "../" } anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } @@ -16,3 +16,4 @@ serde = { version = "1.0", features = ["derive"] } toml = "0.9.5" tokio = { version = "1", features = ["fs", "rt-multi-thread", "macros"] } chrono = { version = "0.4", features = ["clock"] } +rand = { version = "0.9.0", features = ["std_rng"] } diff --git a/abe/src/main.rs b/abe/src/main.rs index 56128b6..5bbe90a 100644 --- a/abe/src/main.rs +++ b/abe/src/main.rs @@ -12,15 +12,16 @@ use clap::{Parser, Subcommand}; use keccak_asm::Keccak256; use mxx::{ matrix::dcrt_poly::DCRTPolyMatrix, - poly::dcrt::params::DCRTPolyParams, + poly::{PolyParams, dcrt::params::DCRTPolyParams}, sampler::{ hash::DCRTPolyHashSampler, trapdoor::DCRTPolyTrapdoorSampler, uniform::DCRTPolyUniformSampler, }, - utils::{log_mem, timed_read, timed_read_async}, + utils::{gen_biguint_for_modulus, timed_read, timed_read_async}, }; use num_bigint::BigUint; use std::{env, fs, path::PathBuf, time::Duration}; +use tracing::info; use tracing_subscriber::{EnvFilter, fmt}; #[derive(Parser, Debug)] @@ -70,7 +71,7 @@ async fn main() -> Result<()> { match cli.command { Commands::BenchSim { config } => { - log_mem(format!("Loading simulator config: path={}", config.display())); + info!("Loading simulator config: path={}", config.display()); let config_prefix = config .file_name() .map(|os| os.to_string_lossy().into_owned()) @@ -83,7 +84,7 @@ async fn main() -> Result<()> { run_bench_sim(sim_config, config_prefix)?; } Commands::BenchRunOffline { config, data_dir } => { - log_mem(format!("Loading run config: path={}", config.display())); + info!("Loading run config: path={}", config.display()); let contents = fs::read_to_string(&config) .with_context(|| format!("failed to read run config from {}", config.display()))?; let run_config: RunConfig = toml::from_str(&contents) @@ -91,7 +92,7 @@ async fn main() -> Result<()> { run_bench_offline(run_config, data_dir).await?; } Commands::BenchRunOnline { config, data_dir } => { - log_mem(format!("Loading run config: path={}", config.display())); + info!("Loading run config: path={}", config.display()); let contents = fs::read_to_string(&config) .with_context(|| format!("failed to read run config from {}", config.display()))?; let run_config: RunConfig = toml::from_str(&contents) @@ -122,7 +123,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { height, } = config; - log_mem(format!( + info!( "Starting benchmark parameter search: target_secpar={}, crt_bits={}, crt_depth_range=({}-{}), base_bits_range=({}-{}), log_dim_range=({}-{}), p_moduli_bits={}, scale_bits = {}, height={}, config_prefix={}", target_secpar, crt_bits, @@ -136,7 +137,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { scale_bits, height, config_prefix - )); + ); let params = bruteforce_params_for_bench_nested_crt_circuit( target_secpar, @@ -159,10 +160,10 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { .checked_shl(arith_height) .context("arith_height is too large for usize input size")?; - log_mem(format!( + info!( "Benchmark parameter search succeeded: crt_depth={}, base_bits={}, log_dim={}, e_b_sigma={}, knapsack_size={}, ring_dimension={}, arith_input_size={}", crt_depth, base_bits, log_dim, e_b_sigma, knapsack_size, ring_dimension, arith_input_size - )); + ); let config_id = format!("{}_{}", config_prefix, Local::now().format("%Y%m%d-%H%M%S")); let run_config = RunConfig { @@ -191,7 +192,7 @@ fn run_bench_sim(config: SimConfig, config_prefix: String) -> Result<()> { fs::write(&output_path, toml) .with_context(|| format!("failed to write config file to {}", output_path.display()))?; - log_mem(format!("Wrote benchmark config: path={}", output_path.display())); + info!("Wrote benchmark config: path={}", output_path.display()); Ok(()) } @@ -220,13 +221,13 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { let mut t_setup = Duration::ZERO; let mut t_keygen = Duration::ZERO; - log_mem("starting KeyPolicy ABE"); + info!("starting KeyPolicy ABE"); // 1) setup - log_mem("starting setup"); + info!("starting setup"); let (mpk, msk): (MasterPK, MasterSK) = timed_read("setup", || abe.setup(¶ms, config.arith_input_size), &mut t_setup); - log_mem("finished setup"); + info!("finished setup"); let dir_path = if data_dir.exists() { data_dir @@ -235,19 +236,19 @@ async fn run_bench_offline(config: RunConfig, data_dir: PathBuf) -> Result<()> { data_dir }; // 2) keygen - log_mem("starting keygen"); + info!("starting keygen"); let fsk: FuncSK = timed_read_async( "keygen", || abe.keygen(¶ms, mpk.clone(), msk.clone(), config.arith_height, dir_path.clone()), &mut t_keygen, ) .await; - log_mem("finished keygen"); + info!("finished keygen"); - log_mem("starting writing mpk and fsk files"); + info!("starting writing mpk and fsk files"); mpk.write(dir_path.join(format!("{}.mpk", config.config_id)))?; fsk.write(dir_path.join(format!("{}.fsk", config.config_id)))?; - log_mem("finished writing mpk and fsk files"); + info!("finished writing mpk and fsk files"); Ok(()) } @@ -277,10 +278,17 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { let mut t_enc = Duration::ZERO; let mut t_read_fsk = Duration::ZERO; let mut t_dec = Duration::ZERO; - log_mem("starting KeyPolicy ABE"); + + let mut inputs = vec![BigUint::ZERO; config.arith_input_size]; + let mut rng = rand::rng(); + for i in 1..inputs.len() { + inputs[i] = gen_biguint_for_modulus(&mut rng, ¶ms.modulus()); + } + + info!("starting KeyPolicy ABE"); // 3) enc - log_mem("starting enc"); + info!("starting enc"); let mpk = timed_read( "read mpk", || { @@ -292,14 +300,11 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { }, &mut t_read_mpk, ); - let ct: Ciphertext = timed_read( - "enc", - || abe.enc(¶ms, mpk, &vec![BigUint::ZERO; config.arith_input_size], true), - &mut t_enc, - ); - log_mem("finished enc"); + let ct: Ciphertext = + timed_read("enc", || abe.enc(¶ms, mpk, &inputs, true), &mut t_enc); + info!("finished enc"); // 4) dec - log_mem("starting dec"); + info!("starting dec"); t_read_mpk = Duration::ZERO; let mpk = timed_read( "read mpk", @@ -325,6 +330,6 @@ async fn run_bench_online(config: RunConfig, data_dir: PathBuf) -> Result<()> { ); let bit: bool = timed_read("dec", || abe.dec(¶ms, ct, mpk, fsk, config.arith_height), &mut t_dec); - log_mem(format!("finished decryption: result={}", bit)); + info!("finished decryption: result={}", bit); Ok(()) } diff --git a/src/abe.rs b/src/abe.rs index 125446b..7449f2e 100644 --- a/src/abe.rs +++ b/src/abe.rs @@ -208,7 +208,7 @@ impl< height: u32, dir_path: PathBuf, ) -> FuncSK { - init_storage_system(); + init_storage_system(dir_path.clone()); let circuit = { let mut circuit = PolyCircuit::::new(); let ctx = Arc::new(NestedRnsPolyContext::setup( @@ -218,14 +218,16 @@ impl< self.scale, false, )); - NestedRnsPoly::benchmark_multiplication_tree( - ctx, - params, - &mut circuit, - height as usize, - ); + info!("constructed NestedRnsPolyContext"); + NestedRnsPoly::benchmark_multiplication_tree(ctx, &mut circuit, height as usize, None); circuit }; + info!( + "constructed circuit with {} inputs, {} gates, and {} non-free depth", + circuit.num_input(), + circuit.num_gates(), + circuit.non_free_depth() + ); let plt_evaluator = GGH15BGGPubKeyPltEvaluator::::new( mpk.seed, self.trapdoor_sigma, @@ -236,13 +238,21 @@ impl< dir_path.clone(), false, ); + info!("constructed plt_evaluator"); let reveal_plaintexts = vec![true; circuit.num_input()]; let bgg_pubkey_sampler = BGGPublicKeySampler::<_, HS>::new(mpk.seed, 1); let pubkeys = bgg_pubkey_sampler.sample(¶ms, TAG_BGG_PUBKEY, &reveal_plaintexts); - let result = circuit.eval(params, &pubkeys[0], &pubkeys[1..], Some(plt_evaluator)); + info!("sampled pubkeys"); + info!("starting evaluation of pubkeys"); + let result = circuit.eval(params, &pubkeys[0], &pubkeys[1..], Some(&plt_evaluator)); info!("finished evaluation of pubkeys"); + info!("starting sample_all_preimages"); + let sample_all_start = std::time::Instant::now(); + plt_evaluator.sample_aux_matrices(¶ms); + info!("finished sample_all_preimages in {:?}", sample_all_start.elapsed()); + info!("starting wait_for_all_writes"); wait_for_all_writes(dir_path.clone()).await.unwrap(); - info!("finished write files"); + info!("finished wait_for_all_writes"); let a_f = result[0].clone().matrix; let trapdoor_sampler = TS::new(params, self.trapdoor_sigma); @@ -260,7 +270,7 @@ impl< fsk: FuncSK, height: u32, ) -> bool { - init_storage_system(); + init_storage_system(fsk.dir_path.clone()); let circuit = { let mut circuit = PolyCircuit::::new(); let ctx = Arc::new(NestedRnsPolyContext::setup( @@ -270,12 +280,7 @@ impl< self.scale, false, )); - NestedRnsPoly::benchmark_multiplication_tree( - ctx, - params, - &mut circuit, - height as usize, - ); + NestedRnsPoly::benchmark_multiplication_tree(ctx, &mut circuit, height as usize, None); circuit }; let encodings = &ct.bgg_encodings[..]; @@ -292,7 +297,7 @@ impl< mpk.b_matrix.row_size(), ct.c_b.clone(), ); - let result = circuit.eval(params, &encodings[0], &encodings[1..], Some(bgg_evaluator)); + let result = circuit.eval(params, &encodings[0], &encodings[1..], Some(&bgg_evaluator)); // 5. Let `c_f := s^T*A_f + e_{c_f}` in $\mathcal{R}_{q}^{1 \times m}$ // be the BGG+ encoding corresponding to the output wire of `poly_circuit`. let v = ct.c_b.concat_columns(&[&result[0].vector]) * fsk.u_f; diff --git a/src/simulator.rs b/src/simulator.rs index 48823ec..b5fd437 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -1,4 +1,5 @@ use bigdecimal::{BigDecimal, FromPrimitive, One}; +use log::info; pub use mxx::simulator::lattice_estimator::run_lattice_estimator_cli; use mxx::{ circuit::PolyCircuit, @@ -13,7 +14,7 @@ use mxx::{ lattice_estimator::{Distribution, EstimatorCliError}, poly_matrix_norm::PolyMatrixNorm, }, - utils::{bigdecimal_bits_ceil, log_mem}, + utils::bigdecimal_bits_ceil, }; use num_bigint::BigUint; use rayon::prelude::*; @@ -216,7 +217,7 @@ pub fn bruteforce_params_for_bench_nested_crt_circuit( let scale = 1< e_final.poly_norm.norm { - log_mem(format!("q_over_4_bits: {}, e_final_bits: {}", q_over_4_bits, e_final_bits)); + info!("q_over_4_bits: {}, e_final_bits: {}", q_over_4_bits, e_final_bits); Ok(log_q * m_g as u32) } else { Err(SimulatorError::NotCorrect { e_bits: e_final_bits, q_over_4_bits }) From 2c375fdb30f4f6ea3badf92e806cb7b1806070f7 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Wed, 4 Feb 2026 09:21:05 +0900 Subject: [PATCH 15/15] Use commit PLT evaluator --- Cargo.lock | 2 +- Cargo.toml | 2 +- abe/Cargo.toml | 2 +- .../secpar_0_height_1_p_6.params.toml | 4 +- .../secpar_120_height_1_p_6.params.toml | 8 +-- .../secpar_120_height_1_p_6.params.toml | 12 ++--- src/abe.rs | 51 +++++++++++-------- src/simulator.rs | 4 +- 8 files changed, 49 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a1d705..f1a4867 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -678,7 +678,7 @@ dependencies = [ [[package]] name = "mxx" version = "0.1.0" -source = "git+https://github.com/MachinaIO/mxx.git?branch=main#e53d03029298982a3e2cc5931571a403eb988be2" +source = "git+https://github.com/MachinaIO/mxx.git?branch=feat%2Fcommit_lookup_eval_simu#5bcb3866947a1563e4c6fac627ba18d56127fa46" dependencies = [ "bigdecimal", "bincode", diff --git a/Cargo.toml b/Cargo.toml index a1ce2d4..33d5a4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "main" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/commit_lookup_eval_simu" } rand = { version = "0.9.0", features = ["std_rng"] } num-bigint = { version = "0.4", features = ["serde"] } thiserror = "2.0.16" diff --git a/abe/Cargo.toml b/abe/Cargo.toml index 117b107..8135e3e 100644 --- a/abe/Cargo.toml +++ b/abe/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "main" } +mxx = { git = "https://github.com/MachinaIO/mxx.git", branch = "feat/commit_lookup_eval_simu" } arithmetic-abe = { path = "../" } anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } diff --git a/abe/run_configs/secpar_0_height_1_p_6.params.toml b/abe/run_configs/secpar_0_height_1_p_6.params.toml index 69a2523..392b61f 100644 --- a/abe/run_configs/secpar_0_height_1_p_6.params.toml +++ b/abe/run_configs/secpar_0_height_1_p_6.params.toml @@ -1,9 +1,9 @@ config_id = "secpar_0_height_1_p_6.params.toml" target_secpar = 0 -crt_depth = 15 +crt_depth = 11 crt_bits = 24 ring_dimension = 16 -knapsack_size = 46 +knapsack_size = 34 e_b_sigma = 4.0 trapdoor_sigma = 4.578 base_bits = 8 diff --git a/abe/run_configs/secpar_120_height_1_p_6.params.toml b/abe/run_configs/secpar_120_height_1_p_6.params.toml index 27cd06c..a7408bb 100644 --- a/abe/run_configs/secpar_120_height_1_p_6.params.toml +++ b/abe/run_configs/secpar_120_height_1_p_6.params.toml @@ -1,12 +1,12 @@ config_id = "secpar_120_height_1_p_6.params.toml" target_secpar = 120 -crt_depth = 11 -crt_bits = 25 +crt_depth = 15 +crt_bits = 24 ring_dimension = 16384 -knapsack_size = 23 +knapsack_size = 31 e_b_sigma = 4.0 trapdoor_sigma = 4.578 -base_bits = 17 +base_bits = 12 p_moduli_bits = 6 scale = 256 arith_input_size = 2 diff --git a/abe/sim_configs/secpar_120_height_1_p_6.params.toml b/abe/sim_configs/secpar_120_height_1_p_6.params.toml index fa2bd78..cad7c1a 100644 --- a/abe/sim_configs/secpar_120_height_1_p_6.params.toml +++ b/abe/sim_configs/secpar_120_height_1_p_6.params.toml @@ -1,11 +1,11 @@ target_secpar = 120 -crt_bits = 25 -crt_depth_min = 8 -crt_depth_max = 11 -base_bits_min = 17 -base_bits_max = 19 +crt_bits = 24 +crt_depth_min = 10 +crt_depth_max = 30 +base_bits_min = 12 +base_bits_max = 12 log_dim_min = 13 -log_dim_max = 14 +log_dim_max = 16 p_moduli_bits = 6 scale_bits = 8 height = 1 diff --git a/src/abe.rs b/src/abe.rs index 7449f2e..f781b58 100644 --- a/src/abe.rs +++ b/src/abe.rs @@ -12,7 +12,7 @@ use mxx::{ circuit::PolyCircuit, element::PolyElem, gadgets::arith::nested_rns::{NestedRnsPoly, NestedRnsPolyContext, encode_nested_rns_poly}, - lookup::ggh15_eval::{GGH15BGGEncodingPltEvaluator, GGH15BGGPubKeyPltEvaluator}, + lookup::commit_eval::{CommitBGGEncodingPltEvaluator, CommitBGGPubKeyPltEvaluator}, matrix::PolyMatrix, poly::{Poly, PolyParams}, sampler::{DistType, PolyHashSampler, PolyTrapdoorSampler, PolyUniformSampler}, @@ -223,20 +223,19 @@ impl< circuit }; info!( - "constructed circuit with {} inputs, {} gates, and {} non-free depth", + "constructed circuit with {} inputs, {:?} gates, and {} non-free depth", circuit.num_input(), - circuit.num_gates(), + circuit.count_gates_by_type_vec(), circuit.non_free_depth() ); - let plt_evaluator = GGH15BGGPubKeyPltEvaluator::::new( - mpk.seed, - self.trapdoor_sigma, - self.e_b_sigma, + let tree_base = 2; + let secret_size = mpk.b_matrix.row_size(); + let plt_evaluator = CommitBGGPubKeyPltEvaluator::::setup::( ¶ms, - mpk.b_matrix.clone(), - msk.b_trapdoor.clone(), - dir_path.clone(), - false, + secret_size, + self.trapdoor_sigma, + tree_base, + mpk.seed, ); info!("constructed plt_evaluator"); let reveal_plaintexts = vec![true; circuit.num_input()]; @@ -246,10 +245,14 @@ impl< info!("starting evaluation of pubkeys"); let result = circuit.eval(params, &pubkeys[0], &pubkeys[1..], Some(&plt_evaluator)); info!("finished evaluation of pubkeys"); - info!("starting sample_all_preimages"); - let sample_all_start = std::time::Instant::now(); - plt_evaluator.sample_aux_matrices(¶ms); - info!("finished sample_all_preimages in {:?}", sample_all_start.elapsed()); + info!("starting commit_all_lut_matrices"); + let commit_all_start = std::time::Instant::now(); + plt_evaluator.commit_all_lut_matrices::( + ¶ms, + mpk.b_matrix.as_ref(), + msk.b_trapdoor.as_ref(), + ); + info!("finished commit_all_lut_matrices in {:?}", commit_all_start.elapsed()); info!("starting wait_for_all_writes"); wait_for_all_writes(dir_path.clone()).await.unwrap(); info!("finished wait_for_all_writes"); @@ -290,12 +293,20 @@ impl< "ciphertext must contain exactly 1 + circuit.num_input() encodings" ); let dir_path: PathBuf = fsk.dir_path; - let bgg_evaluator = GGH15BGGEncodingPltEvaluator::::new( - mpk.seed, + let tree_base = 2; + let one_pubkey = encodings[0].pubkey.clone(); + let input_pubkeys = + encodings[1..].iter().map(|encoding| encoding.pubkey.clone()).collect::>(); + let bgg_evaluator = CommitBGGEncodingPltEvaluator::::setup( ¶ms, - dir_path, - mpk.b_matrix.row_size(), - ct.c_b.clone(), + tree_base, + mpk.seed, + &circuit, + &one_pubkey, + &input_pubkeys, + &ct.c_b, + &ct.c_b, + &dir_path, ); let result = circuit.eval(params, &encodings[0], &encodings[1..], Some(&bgg_evaluator)); // 5. Let `c_f := s^T*A_f + e_{c_f}` in $\mathcal{R}_{q}^{1 \times m}$ diff --git a/src/simulator.rs b/src/simulator.rs index b5fd437..78f8b36 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -454,7 +454,9 @@ fn check_correctness( ); let e_a = &e_b * &r_mat; log::info!("before simulation: e_b = {:?}, e_a = {:?}", e_b, e_a); - let plt_evaluator = NormPltGGH15Evaluator::new(sim_ctx.clone(), &e_b_sigma, &e_b_sigma, None); + let tree_base = 2; + let plt_evaluator = + NormPltCommitEvaluator::new(sim_ctx.clone(), &e_b_sigma, tree_base, circuit); let preimage_norm = compute_preimage_norm(&sim_ctx.ring_dim_sqrt, m_g as u64, &sim_ctx.base); let out_errors = circuit.simulate_max_error_norm( sim_ctx.clone(),