diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6ff32625..5cb84a12 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -148,6 +148,7 @@ jobs: repository: iden3/circom path: circom-repo + - name: Build and install Circom working-directory: circom-repo run: | @@ -158,7 +159,7 @@ jobs: uses: actions/checkout@v4 with: repository: 0xPolygonHermez/pil2-proofman-js - ref: feature/setup + ref: feature/minor_clean path: pil2-proofman-js - name: Install pil2-proofman-js dependencies @@ -169,7 +170,7 @@ jobs: uses: actions/checkout@v4 with: repository: 0xPolygonHermez/pil2-compiler - ref: feature/on-final-priority + ref: feature/proofval-stage path: pil2-compiler - name: Install pil2-compiler dependencies @@ -181,6 +182,7 @@ jobs: sudo apt-get update sudo apt-get install -y protobuf-compiler sudo apt install -y build-essential libbenchmark-dev libomp-dev libgmp-dev nlohmann-json3-dev nasm libsodium-dev cmake + sudo apt-get install -y openmpi-bin libopenmpi-dev - name: Install pil2-stark working-directory: pil2-stark @@ -224,6 +226,21 @@ jobs: - name: Verify proof Fibonacci Square run: | node pil2-proofman-js/src/main_verify -k examples/fibonacci-square/build/provingKey -p examples/fibonacci-square/build/proofs -t vadcop_final + + - name: Clean proof dir + run: | + rm -rf examples/fibonacci-square/build/proofs + + - name: Cargo build distributed + run: RUST_BACKTRACE=1 cargo build --features distributed -v + + - name: Generate proof Fibonacci Square distributed + run: | + mpirun --bind-to none -np 3 -x OMP_NUM_THREADS=32 target/debug/proofman-cli prove --witness-lib ./target/debug/libfibonacci_square.so --public-inputs examples/fibonacci-square/src/inputs.json --proving-key examples/fibonacci-square/build/provingKey/ --output-dir examples/fibonacci-square/build/proofs -a + + - name: Verify proof Fibonacci Square distributed + run: | + node pil2-proofman-js/src/main_verify -k examples/fibonacci-square/build/provingKey -p examples/fibonacci-square/build/proofs -t vadcop_final test-std: name: Test STD runs-on: ubuntu-latest @@ -260,7 +277,7 @@ jobs: uses: actions/checkout@v4 with: repository: 0xPolygonHermez/pil2-proofman-js - ref: feature/setup + ref: feature/minor_clean path: pil2-proofman-js - name: Install pil2-proofman-js dependencies @@ -271,7 +288,7 @@ jobs: uses: actions/checkout@v4 with: repository: 0xPolygonHermez/pil2-compiler - ref: feature/on-final-priority + ref: feature/proofval-stage path: pil2-compiler - name: Install pil2-compiler dependencies diff --git a/Cargo.lock b/Cargo.lock index e2f95caf..1eeafb43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.15" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" dependencies = [ "anstyle", "anstyle-parse", @@ -28,43 +28,43 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.8" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "anstyle-parse" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] name = "anstyle-wincon" -version = "3.0.4" +version = "3.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] name = "anyhow" -version = "1.0.91" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "autocfg" @@ -119,15 +119,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "cc" -version = "1.1.31" +version = "1.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" +checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e" dependencies = [ "shlex", ] @@ -160,9 +160,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.20" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -170,9 +170,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.20" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", @@ -194,24 +194,24 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "colorchoice" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" [[package]] name = "colored" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" dependencies = [ "lazy_static", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] @@ -240,9 +240,9 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "crossbeam-deque" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" dependencies = [ "crossbeam-epoch", "crossbeam-utils", @@ -259,9 +259,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.20" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "custom_derive" @@ -306,19 +306,19 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.9" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] name = "fastrand" -version = "2.1.1" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "fibonacci-square" @@ -330,11 +330,12 @@ dependencies = [ "p3-field", "p3-goldilocks", "pil-std-lib", - "proofman", "proofman-common", "proofman-macros", + "rayon", "serde", "serde_json", + "witness", ] [[package]] @@ -380,11 +381,11 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "home" -version = "0.5.9" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -395,9 +396,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "indexmap" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", "hashbrown", @@ -429,9 +430,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.11" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "lazy_static" @@ -447,9 +448,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.161" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "libffi" @@ -472,12 +473,12 @@ dependencies = [ [[package]] name = "libloading" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" +checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -728,12 +729,11 @@ dependencies = [ "num-traits", "p3-field", "p3-goldilocks", - "pilout", - "proofman", "proofman-common", "proofman-hints", "proofman-util", "rayon", + "witness", ] [[package]] @@ -742,16 +742,15 @@ version = "0.1.0" dependencies = [ "bytes", "log", - "proofman-util", "prost", "prost-build", ] [[package]] name = "pin-project-lite" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" [[package]] name = "pkg-config" @@ -770,9 +769,9 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.24" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "910d41a655dac3b764f1ade94821093d3610248694320cd072303a8eedcf221d" +checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", "syn", @@ -780,9 +779,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -798,7 +797,6 @@ dependencies = [ "mpi", "p3-field", "p3-goldilocks", - "pilout", "proofman-common", "proofman-hints", "proofman-starks-lib-c", @@ -806,6 +804,7 @@ dependencies = [ "serde_json", "stark", "transcript", + "witness", ] [[package]] @@ -836,7 +835,6 @@ dependencies = [ "mpi", "p3-field", "p3-goldilocks", - "pilout", "proofman-macros", "proofman-starks-lib-c", "proofman-util", @@ -850,9 +848,11 @@ dependencies = [ name = "proofman-hints" version = "0.1.0" dependencies = [ + "log", "p3-field", "proofman-common", "proofman-starks-lib-c", + "proofman-util", ] [[package]] @@ -881,9 +881,9 @@ dependencies = [ [[package]] name = "prost" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b0487d90e047de87f984913713b85c601c05609aad5b0df4b4573fbf69aa13f" +checksum = "2c0fef6c4230e4ccf618a35c59d7ede15dea37de8427500f50aff708806e42ec" dependencies = [ "bytes", "prost-derive", @@ -891,11 +891,10 @@ dependencies = [ [[package]] name = "prost-build" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c1318b19085f08681016926435853bbf7858f9c082d0999b80550ff5d9abe15" +checksum = "d0f3e5beed80eb580c68e2c600937ac2c4eedabdfd5ef1e5b7ea4f3fba84497b" dependencies = [ - "bytes", "heck", "itertools 0.13.0", "log", @@ -912,9 +911,9 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" +checksum = "157c5a9d7ea5c2ed2d9fb8f495b64759f7816c7eaea54ba3978f0d63000162e3" dependencies = [ "anyhow", "itertools 0.13.0", @@ -925,9 +924,9 @@ dependencies = [ [[package]] name = "prost-types" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4759aa0d3a6232fb8dbdb97b61de2c20047c68aca932c7ed76da9d788508d670" +checksum = "cc2f1e56baa61e93533aebc21af4d2134b70f66275e0fcdf3cbe43d77ff7e8fc" dependencies = [ "prost", ] @@ -993,9 +992,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", @@ -1005,9 +1004,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", @@ -1028,15 +1027,15 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustix" -version = "0.38.37" +version = "0.38.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1047,18 +1046,18 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.213" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.213" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", @@ -1067,9 +1066,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.132" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa", "memchr", @@ -1103,7 +1102,6 @@ dependencies = [ "p3-field", "p3-goldilocks", "proofman-common", - "proofman-hints", "proofman-starks-lib-c", "proofman-util", "serde", @@ -1126,9 +1124,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.82" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -1137,9 +1135,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.31.4" +version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "355dbe4f8799b304b05e1b0f05fc59b2a18d36645cf169607da45bde2f69a1be" +checksum = "948512566b1895f93b1592c7574baeb2de842f224f2aab158799ecadb8ebbb46" dependencies = [ "core-foundation-sys", "libc", @@ -1151,31 +1149,31 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.13.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" dependencies = [ "cfg-if", "fastrand", "once_cell", "rustix", - "windows-sys 0.59.0", + "windows-sys", ] [[package]] name = "thiserror" -version = "1.0.65" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.65" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", @@ -1194,9 +1192,9 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.40" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "pin-project-lite", "tracing-attributes", @@ -1205,9 +1203,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.27" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", @@ -1216,9 +1214,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", ] @@ -1242,9 +1240,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "unicode-segmentation" @@ -1305,7 +1303,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" dependencies = [ "windows-core", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1317,7 +1315,7 @@ dependencies = [ "windows-implement", "windows-interface", "windows-result", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1348,25 +1346,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1375,22 +1355,7 @@ 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-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", + "windows-targets", ] [[package]] @@ -1399,46 +1364,28 @@ 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_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", "windows_i686_gnullvm", - "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", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1451,54 +1398,45 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - [[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_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - [[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_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "witness" +version = "0.1.0" +dependencies = [ + "colored", + "env_logger", + "log", + "mpi", + "p3-field", + "p3-goldilocks", + "proofman-common", + "proofman-util", + "serde_json", +] + [[package]] name = "zerocopy" version = "0.7.35" diff --git a/Cargo.toml b/Cargo.toml index 3c798dc6..45e188aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,13 +9,14 @@ members = [ "provers/stark", "provers/starks-lib-c", "transcript", + "witness", "util", "pil2-components/lib/std/rs", - #"pil2-components/test/std/range_check/rs", - #"pil2-components/test/std/lookup/rs", - #"pil2-components/test/std/connection/rs", - #"pil2-components/test/std/permutation/rs", - #"pil2-components/test/simple/rs", + # "pil2-components/test/std/range_check/rs", + # "pil2-components/test/std/lookup/rs", + # "pil2-components/test/std/connection/rs", + # "pil2-components/test/std/permutation/rs", + # "pil2-components/test/simple/rs", # whoever re-enables this, it has to work out of # the box with `cargo check --workspace` or CI will # break and dev experience will be bad since repo @@ -32,6 +33,8 @@ proofman-common = { path = "common", version = "0.1.0" } proofman-hints = { path = "hints", version = "0.1.0" } proofman-util = { path = "util", version = "0.1.0" } proofman-cli = { path = "cli", version = "0.1.0" } +pil-std-lib = { path = "pil2-components/lib/std/rs", version = "0.1.0" } +witness = { path = "witness", version = "0.1.0" } pilout = { path = "pilout", version = "0.1.0" } log = { version = "0.4", default-features = false } env_logger = "0.11" diff --git a/book/quickstart.md b/book/quickstart.md index fafadee5..d4c04404 100644 --- a/book/quickstart.md +++ b/book/quickstart.md @@ -127,7 +127,7 @@ impl Executor for FibonacciExecutor { let num_rows = proof_ctx.pilout.air_groups[airgroup_id].airs[air_id].num_rows.unwrap() as usize; trace!(Fibonacci { a: Goldilocks, b: Goldilocks }); - let mut fib = Fibonacci::new(num_rows); + let mut fib = Fibonacci::new(); fib.a[0] = Goldilocks::one(); fib.b[0] = Goldilocks::one(); diff --git a/cli/assets/templates/pil_helpers_mod.rs.tt b/cli/assets/templates/pil_helpers_mod.rs.tt index 8273843a..8e9f77af 100644 --- a/cli/assets/templates/pil_helpers_mod.rs.tt +++ b/cli/assets/templates/pil_helpers_mod.rs.tt @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/cli/assets/templates/pil_helpers_pilout.rs.tt b/cli/assets/templates/pil_helpers_pilout.rs.tt deleted file mode 100644 index fb85c143..00000000 --- a/cli/assets/templates/pil_helpers_pilout.rs.tt +++ /dev/null @@ -1,27 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"{ project_name }-hash"; - -//AIRGROUP CONSTANTS -{{ for constant in constant_airgroups }} -pub const { constant.0 }_AIRGROUP_ID: usize = { constant.1 }; -{{ endfor }} -//AIR CONSTANTS -{{ for constant in constant_airs }} -pub const { constant.0 }_AIR_IDS: &[usize] = &[{ constant.3 }]; -{{ endfor }} -pub struct Pilout; - -impl Pilout \{ - pub fn pilout() -> WitnessPilout \{ - let mut pilout = WitnessPilout::new("{ project_name }", { num_stages }, PILOUT_HASH.to_vec()); -{{ for air_group in air_groups }} - let air_group = pilout.add_air_group(Some("{ air_group.name }")); -{{ for air in air_group.airs }} - air_group.add_air(Some("{ air.name }"), { air.num_rows });{{ endfor }} -{{ endfor }} - pilout - } -} diff --git a/cli/assets/templates/pil_helpers_trace.rs.tt b/cli/assets/templates/pil_helpers_trace.rs.tt index 709e0ebe..ee6e7147 100644 --- a/cli/assets/templates/pil_helpers_trace.rs.tt +++ b/cli/assets/templates/pil_helpers_trace.rs.tt @@ -2,12 +2,55 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; -{{ for air_group in air_groups }}{{ for air in air_group.airs }} -trace!({ air.name }Row, { air.name }Trace \{ -{{ for column in air.columns }} { column.name }: { column.type },{{ endfor }} +pub use proofman_macros::values; + +use std::fmt; + +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"{ project_name }-hash"; + +//AIRGROUP CONSTANTS +{{ for constant in constant_airgroups }} +pub const { constant.0 }_AIRGROUP_ID: usize = { constant.1 }; +{{ endfor }} +//AIR CONSTANTS +{{ for constant in constant_airs }} +pub const { constant.0 }_AIR_IDS: &[usize] = &[{ constant.3 }]; +{{ endfor }} +{{ for public_values in publics }} +//PUBLICS +use serde::Deserialize; +use serde::Serialize; +#[derive(Default, Debug, Serialize, Deserialize)] +pub struct {project_name}Publics \{ + {{ for column in public_values.values_u64 }}#[serde(default)] + pub {column.name}: {column.type}, + {{ endfor }} +} + +values!({ project_name }PublicValues \{ +{{ for column in public_values.values }} { column.name }: { column.type },{{ endfor }} +}); +{{ endfor }} {{ for proof_vals in proof_values }} +values!({ project_name }ProofValues \{ +{{ for column in proof_vals.values }} { column.name }: { column.type },{{ endfor }} }); +{{ endfor }} {{ for air_group in air_groups }}{{ for air in air_group.airs }} +trace!({ air.name }Trace \{ +{{ for column in air.columns }} { column.name }: { column.type },{{ endfor }} +}, { air_group.airgroup_id }, { air.id }, { air.num_rows } ); {{ endfor }}{{ endfor }}{{ for air_group in air_groups }}{{ for air in air_group.airs }}{{ for custom_commit in air.custom_columns }} -trace!({ air.name }{custom_commit.name}Row, { air.name }{custom_commit.name}Trace \{ +trace!({ air.name }{custom_commit.name}Trace \{ {{ for column in custom_commit.custom_columns }} { column.name }: { column.type },{{ endfor }} +}, { air_group.airgroup_id }, { air.id }, { air.num_rows }, { custom_commit.commit_id } ); +{{ endfor }}{{ endfor }}{{ endfor }}{{ for air_group in air_groups }}{{ for air in air_group.airs }}{{ for air_values in air.air_values }} +values!({ air.name }AirValues \{ +{{ for column in air_values.values }} { column.name }: { column.type },{{ endfor }} +}); +{{ endfor }}{{ endfor }}{{ endfor }}{{ for air_group in air_groups }}{{ for air in air_group.airs }}{{ for air_groupvalues in air.airgroup_values }} +values!({ air.name }AirGroupValues \{ +{{ for column in air_groupvalues.values }} { column.name }: { column.type },{{ endfor }} }); {{ endfor }}{{ endfor }}{{ endfor }} \ No newline at end of file diff --git a/cli/assets/templates/pil_helpers_trace_new.rs.tt b/cli/assets/templates/pil_helpers_trace_new.rs.tt deleted file mode 100644 index 4ccd72e5..00000000 --- a/cli/assets/templates/pil_helpers_trace_new.rs.tt +++ /dev/null @@ -1,31 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common as common; -pub use proofman_macros::trace; -{{ for public_values in publics }} -values!({ project_name }PublicValues \{ -{{ for column in public_values.values }} { column.name }: { column.type },{{ endfor }} -}); -{{ endfor }} -{{ for proof_vals in proof_values }} -values!({ project_name }ProofValues \{ -{{ for column in proof_vals.values }} { column.name }: { column.type },{{ endfor }} -}); -{{ endfor }} -{{ for air_group in air_groups }}{{ for air in air_group.airs }} -trace!({ air.name }Row, { air.name }Trace \{ -{{ for column in air.columns }} { column.name }: { column.type },{{ endfor }} -}); -{{ endfor }}{{ endfor }}{{ for air_group in air_groups }}{{ for air in air_group.airs }}{{ for custom_commit in air.custom_columns }} -trace!({ air.name }{custom_commit.name}Row, { air.name }{custom_commit.name}Trace \{ -{{ for column in custom_commit.custom_columns }} { column.name }: { column.type },{{ endfor }} -}); -{{ endfor }}{{ endfor }}{{ endfor }}{{ for air_group in air_groups }}{{ for air in air_group.airs }}{{ for air_values in air.air_values }} -values!({ air.name }AirValues \{ -{{ for column in air_values.values }} { column.name }: { column.type },{{ endfor }} -}); -{{ endfor }}{{ endfor }}{{ endfor }}{{ for air_group in air_groups }}{{ for air in air_group.airs }}{{ for airgroup_values in air.airgroup_values }} -values!({ air.name }AirGroupValues \{ -{{ for column in airgroup_values.values }} { column.name }: { column.type },{{ endfor }} -}); -{{ endfor }}{{ endfor }}{{ endfor }} \ No newline at end of file diff --git a/cli/src/commands/get_constraints.rs b/cli/src/commands/get_constraints.rs new file mode 100644 index 00000000..a7fc198f --- /dev/null +++ b/cli/src/commands/get_constraints.rs @@ -0,0 +1,61 @@ +// extern crate env_logger; +use clap::Parser; +use proofman_common::initialize_logger; +use std::path::PathBuf; +use colored::Colorize; +use std::sync::Arc; + +use proofman_common::{get_global_constraints_lines_str, get_constraints_lines_str, GlobalInfo, SetupsVadcop, SetupCtx}; + +#[derive(Parser)] +#[command(version, about, long_about = None)] +#[command(propagate_version = true)] +pub struct GetConstraintsCmd { + /// Setup folder path + #[clap(long)] + pub proving_key: PathBuf, +} + +impl GetConstraintsCmd { + const MY_NAME: &str = "Cnstrnts"; + + pub fn run(&self) -> Result<(), Box> { + println!("{} GetConstraints", format!("{: >12}", "Command").bright_green().bold()); + println!(); + + let global_info = GlobalInfo::new(&self.proving_key); + let setups = Arc::new(SetupsVadcop::new(&global_info, false, false)); + let sctx: Arc = setups.sctx.clone(); + + initialize_logger(proofman_common::VerboseMode::Info); + + for airgroup_id in 0..global_info.air_groups.len() { + for air_id in 0..global_info.airs[airgroup_id].len() { + log::info!( + "{}", + format!( + "{}: ► Constraints of {} - {}", + Self::MY_NAME, + global_info.air_groups[airgroup_id], + global_info.airs[airgroup_id][air_id].name, + ) + .bright_white() + .bold() + ); + let constraints_lines: Vec = get_constraints_lines_str(sctx.clone(), airgroup_id, air_id); + for (idx, line) in constraints_lines.iter().enumerate() { + log::info!("{}: · Constraint #{} : {}", Self::MY_NAME, idx, line); + } + } + } + + let global_constraints_lines = get_global_constraints_lines_str(sctx.clone()); + + log::info!("{}", format!("{}: ► Global Constraints", Self::MY_NAME,).bright_white().bold()); + for (idx, line) in global_constraints_lines.iter().enumerate() { + log::info!("{}: · Global Constraint #{} -> {}", Self::MY_NAME, idx, line); + } + + Ok(()) + } +} diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 5b13463b..22e08004 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -5,3 +5,4 @@ pub mod new; pub mod pil_helpers; pub mod prove; pub mod verify_constraints; +pub mod get_constraints; diff --git a/cli/src/commands/pil_helpers.rs b/cli/src/commands/pil_helpers.rs index 5e0fd0d6..460b6c48 100644 --- a/cli/src/commands/pil_helpers.rs +++ b/cli/src/commands/pil_helpers.rs @@ -26,7 +26,7 @@ pub struct PilHelpersCmd { pub verbose: u8, // Using u8 to hold the number of `-v` } -#[derive(Serialize)] +#[derive(Clone, Serialize)] struct ProofCtx { project_name: String, num_stages: u32, @@ -38,7 +38,7 @@ struct ProofCtx { publics: Vec, } -#[derive(Debug, Serialize)] +#[derive(Clone, Debug, Serialize)] struct AirGroupsCtx { airgroup_id: usize, name: String, @@ -46,33 +46,42 @@ struct AirGroupsCtx { airs: Vec, } -#[derive(Debug, Serialize)] +#[derive(Clone, Debug, Serialize)] struct AirCtx { id: usize, name: String, num_rows: u32, columns: Vec, + stages_columns: Vec, custom_columns: Vec, air_values: Vec, airgroup_values: Vec, } -#[derive(Debug, Serialize)] +#[derive(Clone, Debug, Serialize)] struct ValuesCtx { values: Vec, + values_u64: Vec, } -#[derive(Debug, Serialize)] +#[derive(Clone, Debug, Serialize)] struct CustomCommitsCtx { name: String, + commit_id: usize, custom_columns: Vec, } -#[derive(Debug, Serialize)] +#[derive(Clone, Debug, Serialize)] struct ColumnCtx { name: String, r#type: String, } +#[derive(Default, Clone, Debug, Serialize)] +struct StageColumnCtx { + stage_id: usize, + columns: Vec, +} + impl PilHelpersCmd { pub fn run(&self) -> Result<(), Box> { println!("{} Pil-helpers", format!("{: >12}", "Command").bright_green().bold()); @@ -126,6 +135,7 @@ impl PilHelpersCmd { name: air.name.as_ref().unwrap().clone(), num_rows: air.num_rows.unwrap(), columns: Vec::new(), + stages_columns: vec![StageColumnCtx::default(); pilout.num_challenges.len() - 1], custom_columns: Vec::new(), air_values: Vec::new(), airgroup_values: Vec::new(), @@ -171,16 +181,41 @@ impl PilHelpersCmd { // Start with "F" and apply each length in reverse order symbol.lengths.iter().rev().fold("F".to_string(), |acc, &length| format!("[{}; {}]", acc, length)) }; + let ext_type = if symbol.lengths.is_empty() { + "FieldExtension".to_string() // Case when lengths.len() == 0 + } else { + // Start with "F" and apply each length in reverse order + symbol + .lengths + .iter() + .rev() + .fold("FieldExtension".to_string(), |acc, &length| format!("[{}; {}]", acc, length)) + }; if symbol.r#type == SymbolType::ProofValue as i32 { if proof_values.is_empty() { - proof_values.push(ValuesCtx { values: Vec::new() }); + proof_values.push(ValuesCtx { values: Vec::new(), values_u64: Vec::new() }); + } + if symbol.stage == Some(1) { + proof_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type }); + } else { + proof_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type: ext_type }); } - proof_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type }); } else { if publics.is_empty() { - publics.push(ValuesCtx { values: Vec::new() }); + publics.push(ValuesCtx { values: Vec::new(), values_u64: Vec::new() }); } publics[0].values.push(ColumnCtx { name: name.to_owned(), r#type }); + let r#type_64 = if symbol.lengths.is_empty() { + "u64".to_string() // Case when lengths.len() == 0 + } else { + // Start with "u64" and apply each length in reverse order + symbol + .lengths + .iter() + .rev() + .fold("u64".to_string(), |acc, &length| format!("[{}; {}]", acc, length)) + }; + publics[0].values_u64.push(ColumnCtx { name: name.to_owned(), r#type: r#type_64 }); } }); @@ -191,8 +226,10 @@ impl PilHelpersCmd { air.custom_columns = pilout.air_groups[airgroup_id].airs[air_id] .custom_commits .iter() - .map(|commit| CustomCommitsCtx { + .enumerate() + .map(|(index, commit)| CustomCommitsCtx { name: commit.name.clone().unwrap().to_case(Case::Pascal), + commit_id: index, custom_columns: Vec::new(), }) .collect(); @@ -207,7 +244,7 @@ impl PilHelpersCmd { && ((symbol.air_id.is_some() && symbol.air_id.unwrap() == air_id as u32) || symbol.r#type == SymbolType::AirGroupValue as i32) && symbol.stage.is_some() - && ((symbol.r#type == SymbolType::WitnessCol as i32 && symbol.stage.unwrap() == 1) + && ((symbol.r#type == SymbolType::WitnessCol as i32) || (symbol.r#type == SymbolType::AirValue as i32) || (symbol.r#type == SymbolType::AirGroupValue as i32) || (symbol.r#type == SymbolType::CustomCol as i32 && symbol.stage.unwrap() == 0)) @@ -225,18 +262,39 @@ impl PilHelpersCmd { .rev() .fold("F".to_string(), |acc, &length| format!("[{}; {}]", acc, length)) }; + let ext_type = + if symbol.lengths.is_empty() { + "FieldExtension".to_string() // Case when lengths.len() == 0 + } else { + // Start with "F" and apply each length in reverse order + symbol.lengths.iter().rev().fold("FieldExtension".to_string(), |acc, &length| { + format!("[{}; {}]", acc, length) + }) + }; if symbol.r#type == SymbolType::WitnessCol as i32 { - air.columns.push(ColumnCtx { name: name.to_owned(), r#type }); + if symbol.stage.unwrap() == 1 { + air.columns.push(ColumnCtx { name: name.to_owned(), r#type }); + } else { + air.stages_columns[symbol.stage.unwrap() as usize - 2].stage_id = + symbol.stage.unwrap() as usize; + air.stages_columns[symbol.stage.unwrap() as usize - 2] + .columns + .push(ColumnCtx { name: name.to_owned(), r#type: ext_type }); + } } else if symbol.r#type == SymbolType::AirValue as i32 { if air.air_values.is_empty() { - air.air_values.push(ValuesCtx { values: Vec::new() }); + air.air_values.push(ValuesCtx { values: Vec::new(), values_u64: Vec::new() }); + } + if symbol.stage == Some(1) { + air.air_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type }); + } else { + air.air_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type: ext_type }); } - air.air_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type }); } else if symbol.r#type == SymbolType::AirGroupValue as i32 { if air.airgroup_values.is_empty() { - air.airgroup_values.push(ValuesCtx { values: Vec::new() }); + air.airgroup_values.push(ValuesCtx { values: Vec::new(), values_u64: Vec::new() }); } - air.airgroup_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type }); + air.airgroup_values[0].values.push(ColumnCtx { name: name.to_owned(), r#type: ext_type }); } else { air.custom_columns[symbol.commit_id.unwrap() as usize] .custom_columns @@ -261,7 +319,6 @@ impl PilHelpersCmd { let mut tt = TinyTemplate::new(); tt.add_template("mod.rs", MOD_RS)?; - tt.add_template("pilout.rs", include_str!("../../assets/templates/pil_helpers_pilout.rs.tt"))?; tt.add_template("traces.rs", include_str!("../../assets/templates/pil_helpers_trace.rs.tt"))?; // Write the files @@ -269,11 +326,11 @@ impl PilHelpersCmd { // Write mod.rs fs::write(pil_helpers_path.join("mod.rs"), MOD_RS)?; - // Write pilout.rs - fs::write(pil_helpers_path.join("pilout.rs"), tt.render("pilout.rs", &context)?)?; - // Write traces.rs - fs::write(pil_helpers_path.join("traces.rs"), tt.render("traces.rs", &context)?)?; + fs::write( + pil_helpers_path.join("traces.rs"), + tt.render("traces.rs", &context)?.replace("<", "<").replace(">", ">"), + )?; Ok(()) } diff --git a/cli/src/commands/prove.rs b/cli/src/commands/prove.rs index bc8ede47..a2e368b2 100644 --- a/cli/src/commands/prove.rs +++ b/cli/src/commands/prove.rs @@ -1,8 +1,7 @@ // extern crate env_logger; use clap::Parser; -use proofman_common::{initialize_logger, parse_cached_buffers, StdMode, DEFAULT_PRINT_VALS}; +use proofman_common::{initialize_logger, json_to_debug_instances_map, DebugInfo}; use std::path::PathBuf; -use std::collections::HashMap; use colored::Colorize; use crate::commands::field::Field; @@ -31,10 +30,6 @@ pub struct ProveCmd { #[clap(short = 'i', long)] pub public_inputs: Option, - /// Cached buffer path - #[clap(short = 'c', long, value_parser = parse_cached_buffers)] - pub cached_buffers: Option>, - /// Setup folder path #[clap(long)] pub proving_key: PathBuf, @@ -56,17 +51,8 @@ pub struct ProveCmd { #[arg(short, long, action = clap::ArgAction::Count, help = "Increase verbosity level")] pub verbose: u8, // Using u8 to hold the number of `-v` - #[clap(short = 'd', long, action = clap::ArgAction::Count)] - pub debug: u8, - - #[clap(long)] - pub print: Option, - - #[clap(long, action = clap::ArgAction::SetTrue)] - pub print_to_file: bool, - - #[clap(long, action = clap::ArgAction::Append)] - pub opids: Option>, + #[clap(short = 'd', long)] + pub debug: Option, } impl ProveCmd { @@ -82,23 +68,14 @@ impl ProveCmd { fs::create_dir_all(self.output_dir.join("proofs")).expect("Failed to create the proofs directory"); - let std_mode: StdMode = if self.debug == 1 { - let op_ids = self.opids.as_ref().map(|ids| { - ids.iter() - .flat_map(|id| { - id.split(',') - .map(|s| s.trim()) // Trim any surrounding whitespace - .filter_map(|s| s.parse::().ok()) // Try parsing as u64 - .collect::>() // Collect into a Vec - }) - .collect::>() // Collect the entire iterator into a Vec - }); - - let n_values = self.print.unwrap_or(DEFAULT_PRINT_VALS); - let print_to_file = self.print_to_file; - StdMode::new(proofman_common::ModeName::Debug, op_ids, n_values, print_to_file) + let debug_info = if let Some(debug_value) = &self.debug { + if debug_value.is_empty() || debug_value == "false" { + DebugInfo::new() + } else { + json_to_debug_instances_map(debug_value.clone()) + } } else { - self.debug.into() + DebugInfo::new() }; match self.field { @@ -106,10 +83,9 @@ impl ProveCmd { self.witness_lib.clone(), self.rom.clone(), self.public_inputs.clone(), - self.cached_buffers.clone(), self.proving_key.clone(), self.output_dir.clone(), - ProofOptions::new(false, self.verbose.into(), std_mode, self.aggregation, self.final_snark), + ProofOptions::new(false, self.verbose.into(), self.aggregation, self.final_snark, debug_info), )?, }; diff --git a/cli/src/commands/verify_constraints.rs b/cli/src/commands/verify_constraints.rs index 02495b76..14cadee1 100644 --- a/cli/src/commands/verify_constraints.rs +++ b/cli/src/commands/verify_constraints.rs @@ -1,7 +1,6 @@ // extern crate env_logger; use clap::Parser; -use std::collections::HashMap; -use proofman_common::{initialize_logger, parse_cached_buffers, StdMode, DEFAULT_PRINT_VALS}; +use proofman_common::{initialize_logger, json_to_debug_instances_map, DebugInfo}; use std::path::PathBuf; use colored::Colorize; use crate::commands::field::Field; @@ -29,10 +28,6 @@ pub struct VerifyConstraintsCmd { #[clap(short = 'i', long)] pub public_inputs: Option, - /// Cached buffer path - #[clap(short = 'c', long, value_parser = parse_cached_buffers)] - pub cached_buffers: Option>, - /// Setup folder path #[clap(long)] pub proving_key: PathBuf, @@ -44,17 +39,8 @@ pub struct VerifyConstraintsCmd { #[arg(short, long, action = clap::ArgAction::Count, help = "Increase verbosity level")] pub verbose: u8, // Using u8 to hold the number of `-v` - #[clap(short = 'd', long, action = clap::ArgAction::Count)] - pub debug: u8, - - #[clap(long)] - pub print: Option, - - #[clap(long, action = clap::ArgAction::SetTrue)] - pub print_to_file: bool, - - #[clap(long, action = clap::ArgAction::Append)] - pub opids: Option>, + #[clap(short = 'd', long)] + pub debug: Option, } impl VerifyConstraintsCmd { @@ -64,23 +50,14 @@ impl VerifyConstraintsCmd { initialize_logger(self.verbose.into()); - let std_mode: StdMode = if self.debug == 1 { - let op_ids = self.opids.as_ref().map(|ids| { - ids.iter() - .flat_map(|id| { - id.split(',') - .map(|s| s.trim()) // Trim any surrounding whitespace - .filter_map(|s| s.parse::().ok()) // Try parsing as u64 - .collect::>() // Collect into a Vec - }) - .collect::>() // Collect the entire iterator into a Vec - }); - - let n_values = self.print.unwrap_or(DEFAULT_PRINT_VALS); - let print_to_file = self.print_to_file; - StdMode::new(proofman_common::ModeName::Debug, op_ids, n_values, print_to_file) + let debug_info = if let Some(debug_value) = &self.debug { + if debug_value.is_empty() || debug_value == "false" { + DebugInfo::new() + } else { + json_to_debug_instances_map(debug_value.clone()) + } } else { - self.debug.into() + DebugInfo::new() }; match self.field { @@ -88,10 +65,15 @@ impl VerifyConstraintsCmd { self.witness_lib.clone(), self.rom.clone(), self.public_inputs.clone(), - self.cached_buffers.clone(), self.proving_key.clone(), PathBuf::new(), - ProofOptions::new(true, self.verbose.into(), std_mode, false, false), + ProofOptions::new( + true, + self.verbose.into(), + false, + false, + debug_info + ), )?, }; diff --git a/cli/src/main.rs b/cli/src/main.rs index e05d7991..1a7ff34d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -2,6 +2,7 @@ use std::process::exit; use clap::{Parser, Subcommand}; mod commands; +use commands::get_constraints::GetConstraintsCmd; use commands::pil_helpers::PilHelpersCmd; // use commands::new::NewCmd; use commands::prove::ProveCmd; @@ -26,6 +27,7 @@ pub enum Commands { Prove(ProveCmd), PilHelpers(PilHelpersCmd), VerifyConstraints(VerifyConstraintsCmd), + GetConstraints(GetConstraintsCmd), } fn main() { @@ -48,6 +50,7 @@ fn main() { Commands::Prove(args) => args.run(), Commands::PilHelpers(args) => args.run(), Commands::VerifyConstraints(args) => args.run(), + Commands::GetConstraints(args) => args.run(), }; if let Err(e) = result { diff --git a/common/Cargo.toml b/common/Cargo.toml index b78db2db..7c02c067 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" serde = { version = "1.0.130", features = ["derive"] } serde_json = "1.0.68" serde_derive = "1.0.196" -pilout = { path = "../pilout" } log.workspace = true transcript = { path = "../transcript" } p3-field.workspace = true diff --git a/common/src/air_instance.rs b/common/src/air_instance.rs index 00ded4ea..4fe803bb 100644 --- a/common/src/air_instance.rs +++ b/common/src/air_instance.rs @@ -1,31 +1,29 @@ -use std::mem::MaybeUninit; use std::ptr; -use std::{collections::HashMap, os::raw::c_void, sync::Arc}; -use std::path::PathBuf; use p3_field::Field; -use proofman_starks_lib_c::{get_custom_commit_map_ids_c, get_map_totaln_custom_commits_c}; +use proofman_util::create_buffer_fast; -use crate::{SetupCtx, StarkInfo}; +use crate::{trace::Trace, trace::Values}; #[repr(C)] pub struct StepsParams { - pub trace: *mut c_void, - pub pols: *mut c_void, - pub public_inputs: *mut c_void, - pub proof_values: *mut c_void, - pub challenges: *mut c_void, - pub airgroup_values: *mut c_void, - pub airvalues: *mut c_void, - pub evals: *mut c_void, - pub xdivxsub: *mut c_void, - pub p_const_pols: *mut c_void, - pub p_const_tree: *mut c_void, - pub custom_commits: [*mut c_void; 10], + pub trace: *mut u8, + pub aux_trace: *mut u8, + pub public_inputs: *mut u8, + pub proof_values: *mut u8, + pub challenges: *mut u8, + pub airgroup_values: *mut u8, + pub airvalues: *mut u8, + pub evals: *mut u8, + pub xdivxsub: *mut u8, + pub p_const_pols: *mut u8, + pub p_const_tree: *mut u8, + pub custom_commits: [*mut u8; 10], + pub custom_commits_extended: [*mut u8; 10], } -impl From<&StepsParams> for *mut c_void { - fn from(params: &StepsParams) -> *mut c_void { - params as *const StepsParams as *mut c_void +impl From<&StepsParams> for *mut u8 { + fn from(params: &StepsParams) -> *mut u8 { + params as *const StepsParams as *mut u8 } } @@ -33,7 +31,7 @@ impl Default for StepsParams { fn default() -> Self { StepsParams { trace: ptr::null_mut(), - pols: ptr::null_mut(), + aux_trace: ptr::null_mut(), public_inputs: ptr::null_mut(), proof_values: ptr::null_mut(), challenges: ptr::null_mut(), @@ -44,253 +42,237 @@ impl Default for StepsParams { p_const_pols: ptr::null_mut(), p_const_tree: ptr::null_mut(), custom_commits: [ptr::null_mut(); 10], + custom_commits_extended: [ptr::null_mut(); 10], } } } -#[derive(Default)] -pub struct CustomCommitsInfo { - pub buffer: Vec, - pub cached_file: PathBuf, +pub struct CustomCommitInfo { + pub trace: Vec, + pub commit_id: usize, +} + +pub struct TraceInfo { + airgroup_id: usize, + air_id: usize, + trace: Vec, + custom_traces: Option>>, + air_values: Option>, + airgroup_values: Option>, } -impl CustomCommitsInfo { - pub fn new(buffer: Vec, cached_file: PathBuf) -> Self { - Self { buffer, cached_file } +impl TraceInfo { + pub fn new(airgroup_id: usize, air_id: usize, trace: Vec) -> Self { + Self { airgroup_id, air_id, trace, custom_traces: None, air_values: None, airgroup_values: None } + } + + pub fn with_custom_traces(mut self, custom_traces: Vec>) -> Self { + self.custom_traces = Some(custom_traces); + self + } + + pub fn with_air_values(mut self, air_values: Vec) -> Self { + self.air_values = Some(air_values); + self + } + + pub fn with_airgroup_values(mut self, airgroup_values: Vec) -> Self { + self.air_values = Some(airgroup_values); + self + } +} + +pub struct FromTrace<'a, F> { + pub trace: &'a mut dyn Trace, + pub custom_traces: Option>>, + pub air_values: Option<&'a mut dyn Values>, + pub airgroup_values: Option<&'a mut dyn Values>, +} + +impl<'a, F> FromTrace<'a, F> { + pub fn new(trace: &'a mut dyn Trace) -> Self { + Self { trace, custom_traces: None, air_values: None, airgroup_values: None } + } + + pub fn with_custom_traces(mut self, custom_traces: Vec<&'a mut dyn Trace>) -> Self { + self.custom_traces = Some(custom_traces); + self + } + + pub fn with_air_values(mut self, air_values: &'a mut dyn Values) -> Self { + self.air_values = Some(air_values); + self + } + + pub fn with_airgroup_values(mut self, airgroup_values: &'a mut dyn Values) -> Self { + self.airgroup_values = Some(airgroup_values); + self } } /// Air instance context for managing air instances (traces) #[allow(dead_code)] #[repr(C)] +#[derive(Clone)] pub struct AirInstance { pub airgroup_id: usize, pub air_id: usize, - pub air_segment_id: Option, pub air_instance_id: Option, pub idx: Option, pub global_idx: Option, pub trace: Vec, - pub buffer: Option>>, - pub custom_commits: Vec>, + pub aux_trace: Vec, + pub custom_commits: Vec>, + pub custom_commits_extended: Vec>, pub airgroup_values: Vec, pub airvalues: Vec, pub evals: Vec, - pub commits_calculated: HashMap, - pub airgroupvalue_calculated: HashMap, - pub airvalue_calculated: HashMap, - pub custom_commits_calculated: Vec>, - pub stark_info: StarkInfo, + pub prover_initialized: bool, } impl AirInstance { - pub fn new( - setup_ctx: Arc, - airgroup_id: usize, - air_id: usize, - air_segment_id: Option, - trace: Vec, - ) -> Self { - let ps = setup_ctx.get_setup(airgroup_id, air_id); - - let n_custom_commits = ps.stark_info.custom_commits.len(); - let custom_commits_calculated = vec![HashMap::new(); n_custom_commits]; - - let mut custom_commits = Vec::new(); - - for _ in 0..n_custom_commits { - custom_commits.push(CustomCommitsInfo::default()); - } + pub fn new(trace_info: TraceInfo) -> Self { + let airgroup_id = trace_info.airgroup_id; + let air_id = trace_info.air_id; + + let custom_commits = Self::init_custom_commits(trace_info.custom_traces); + + let airvalues = trace_info.air_values.unwrap_or_default(); + + let airgroup_values = trace_info.airgroup_values.unwrap_or_default(); AirInstance { airgroup_id, air_id, - air_segment_id, air_instance_id: None, idx: None, global_idx: None, - trace, - buffer: None, + trace: trace_info.trace, + aux_trace: Vec::new(), custom_commits, - airgroup_values: vec![F::zero(); ps.stark_info.airgroupvalues_map.as_ref().unwrap().len() * 3], - airvalues: vec![F::zero(); ps.stark_info.airvalues_map.as_ref().unwrap().len() * 3], - evals: vec![F::zero(); ps.stark_info.ev_map.len() * 3], - commits_calculated: HashMap::new(), - airgroupvalue_calculated: HashMap::new(), - airvalue_calculated: HashMap::new(), - custom_commits_calculated, - stark_info: ps.stark_info.clone(), + custom_commits_extended: vec![Vec::new(); 10], + airgroup_values, + airvalues, + evals: Vec::new(), + prover_initialized: false, } } - pub fn get_trace_ptr(&self) -> *mut u8 { - self.trace.as_ptr() as *mut u8 - } - - pub fn set_buffer(&mut self, buffer: Vec>) { - self.buffer = Some(buffer); - } - - pub fn get_buffer_ptr(&self) -> *mut u8 { - match &self.buffer { - Some(buffer) => buffer.as_ptr() as *mut u8, - None => std::ptr::null_mut(), // Return null if `buffer` is `None` + pub fn new_from_trace(mut traces: FromTrace<'_, F>) -> Self { + let mut trace_info = + TraceInfo::new(traces.trace.airgroup_id(), traces.trace.air_id(), traces.trace.get_buffer()); + + if let Some(custom_traces) = traces.custom_traces.as_mut() { + let mut traces = Vec::new(); + for custom_trace in custom_traces.iter_mut() { + traces.push(CustomCommitInfo { + trace: custom_trace.get_buffer(), + commit_id: custom_trace.commit_id().unwrap(), + }); + } + trace_info = trace_info.with_custom_traces(traces); } - } - pub fn get_custom_commits_ptr(&self) -> [*mut c_void; 10] { - let mut ptrs = [std::ptr::null_mut(); 10]; - for (i, custom_commit) in self.custom_commits.iter().enumerate() { - ptrs[i] = custom_commit.buffer.as_ptr() as *mut c_void; + if let Some(air_values) = traces.air_values.as_mut() { + trace_info = trace_info.with_air_values(air_values.get_buffer()); } - ptrs - } - - pub fn set_custom_commit_cached_file(&mut self, setup_ctx: &SetupCtx, commit_id: u64, cached_file: PathBuf) { - let ps = setup_ctx.get_setup(self.airgroup_id, self.air_id); - - let buffer_size = get_map_totaln_custom_commits_c(ps.p_setup.p_stark_info, commit_id); - let buffer = vec![F::zero(); buffer_size as usize]; - self.custom_commits[commit_id as usize] = CustomCommitsInfo::new(buffer, cached_file); - - let ids = get_custom_commit_map_ids_c(ps.p_setup.p_stark_info, commit_id, 0); - for idx in ids { - self.set_custom_commit_calculated(commit_id as usize, idx as usize); - } + AirInstance::new(trace_info) } - pub fn set_custom_commit_id_buffer(&mut self, setup_ctx: &SetupCtx, buffer: Vec, commit_id: u64) { - self.custom_commits[commit_id as usize] = CustomCommitsInfo::new(buffer, PathBuf::new()); - - let ps = setup_ctx.get_setup(self.airgroup_id, self.air_id); - - let ids = get_custom_commit_map_ids_c(ps.p_setup.p_stark_info, commit_id, 0); - for idx in ids { - self.set_custom_commit_calculated(commit_id as usize, idx as usize); + pub fn init_custom_commits(traces_custom: Option>>) -> Vec> { + if let Some(traces_custom) = traces_custom { + let mut custom_commits = vec![Vec::new(); traces_custom.len()]; + for trace in traces_custom { + custom_commits[trace.commit_id] = trace.trace; + } + custom_commits + } else { + vec![Vec::new(); 10] } } - pub fn set_airvalue(&mut self, name: &str, lengths: Option>, value: F) { - let airvalues_map = self.stark_info.airvalues_map.as_ref().unwrap(); - let airvalue_id = (0..airvalues_map.len()) - .find(|&i| { - let airvalue = airvalues_map.get(i).unwrap(); - - // Check if name matches - let name_matches = airvalues_map[i].name == name; - - // If lengths is provided, check that it matches airvalue.lengths - let lengths_match = if let Some(ref provided_lengths) = lengths { - Some(&airvalue.lengths) == Some(provided_lengths) - } else { - true // If lengths is None, skip the lengths check - }; - - name_matches && lengths_match - }) - .unwrap_or_else(|| panic!("Name {} with specified lengths {:?} not found in airvalues", name, lengths)); - - self.airvalues[airvalue_id * 3] = value; - self.set_airvalue_calculated(airvalue_id); + pub fn get_trace(&self) -> Vec { + self.trace.clone() } - pub fn set_airvalue_ext(&mut self, name: &str, lengths: Option>, value: Vec) { - let airvalues_map = self.stark_info.airvalues_map.as_ref().unwrap(); - let airvalue_id = (0..airvalues_map.len()) - .find(|&i| { - let airvalue = airvalues_map.get(i).unwrap(); - - // Check if name matches - let name_matches = airvalues_map[i].name == name; - - // If lengths is provided, check that it matches airvalue.lengths - let lengths_match = if let Some(ref provided_lengths) = lengths { - Some(&airvalue.lengths) == Some(provided_lengths) - } else { - true // If lengths is None, skip the lengths check - }; - - name_matches && lengths_match - }) - .unwrap_or_else(|| panic!("Name {} with specified lengths {:?} not found in airvalues", name, lengths)); - - assert!(value.len() == 3, "Value vector must have exactly 3 elements"); - - let mut value_iter = value.into_iter(); - - self.airvalues[airvalue_id * 3] = value_iter.next().unwrap(); - self.airvalues[airvalue_id * 3 + 1] = value_iter.next().unwrap(); - self.airvalues[airvalue_id * 3 + 2] = value_iter.next().unwrap(); + pub fn get_trace_stage(&self, stage: usize) -> Vec { + if stage < 2 { + panic!("Stage must be 2 or higher"); + } - self.set_airvalue_calculated(airvalue_id); + Vec::new() } - pub fn set_airgroupvalue(&mut self, name: &str, lengths: Option>, value: F) { - let airgroupvalues_map = self.stark_info.airgroupvalues_map.as_ref().unwrap(); - let airgroupvalue_id = (0..airgroupvalues_map.len()) - .find(|&i| { - let airgroupvalue = airgroupvalues_map.get(i).unwrap(); + pub fn get_trace_ptr(&self) -> *mut u8 { + self.trace.as_ptr() as *mut u8 + } - // Check if name matches - let name_matches = airgroupvalues_map[i].name == name; + pub fn get_evals_ptr(&self) -> *mut u8 { + self.evals.as_ptr() as *mut u8 + } - // If lengths is provided, check that it matches airgroupvalues.lengths - let lengths_match = if let Some(ref provided_lengths) = lengths { - Some(&airgroupvalue.lengths) == Some(provided_lengths) - } else { - true // If lengths is None, skip the lengths check - }; + pub fn get_airgroup_values_ptr(&self) -> *mut u8 { + self.airgroup_values.as_ptr() as *mut u8 + } - name_matches && lengths_match - }) - .unwrap_or_else(|| { - panic!("Name {} with specified lengths {:?} not found in airgroupvalues", name, lengths) - }); + pub fn get_air_values(&self) -> Vec { + self.airvalues.clone() + } - self.airgroup_values[airgroupvalue_id * 3] = value; - self.set_airgroupvalue_calculated(airgroupvalue_id); + pub fn get_airgroup_values(&self) -> Vec { + self.airgroup_values.clone() } - pub fn set_airgroupvalue_ext(&mut self, name: &str, lengths: Option>, value: Vec) { - let airgroupvalues_map = self.stark_info.airgroupvalues_map.as_ref().unwrap(); - let airgroupvalue_id = (0..airgroupvalues_map.len()) - .find(|&i| { - let airgroupvalue = airgroupvalues_map.get(i).unwrap(); + pub fn get_airvalues_ptr(&self) -> *mut u8 { + self.airvalues.as_ptr() as *mut u8 + } - // Check if name matches - let name_matches = airgroupvalues_map[i].name == name; + pub fn init_evals(&mut self, size: usize) { + self.evals = vec![F::zero(); size]; + } - // If lengths is provided, check that it matches airgroupvalues.lengths - let lengths_match = if let Some(ref provided_lengths) = lengths { - Some(&airgroupvalue.lengths) == Some(provided_lengths) - } else { - true // If lengths is None, skip the lengths check - }; + pub fn init_aux_trace(&mut self, size: usize) { + self.aux_trace = create_buffer_fast(size); + } - name_matches && lengths_match - }) - .unwrap_or_else(|| { - panic!("Name {} with specified lengths {:?} not found in airgroupvalues", name, lengths) - }); + pub fn init_airvalues(&mut self, size: usize) { + self.airvalues = vec![F::zero(); size]; + } - assert!(value.len() == 3, "Value vector must have exactly 3 elements"); + pub fn init_airgroup_values(&mut self, size: usize) { + self.airgroup_values = vec![F::zero(); size]; + } - let mut value_iter = value.into_iter(); + pub fn init_custom_commit(&mut self, commit_id: usize, size: usize) { + self.custom_commits[commit_id] = create_buffer_fast(size); + } - self.airgroup_values[airgroupvalue_id * 3] = value_iter.next().unwrap(); - self.airgroup_values[airgroupvalue_id * 3 + 1] = value_iter.next().unwrap(); - self.airgroup_values[airgroupvalue_id * 3 + 2] = value_iter.next().unwrap(); + pub fn init_custom_commit_extended(&mut self, commit_id: usize, size: usize) { + self.custom_commits_extended[commit_id] = create_buffer_fast(size); + } - self.set_airgroupvalue_calculated(airgroupvalue_id); + pub fn get_aux_trace_ptr(&self) -> *mut u8 { + match &self.aux_trace.is_empty() { + false => self.aux_trace.as_ptr() as *mut u8, + true => std::ptr::null_mut(), // Return null if `trace` is `None` + } } - pub fn set_commit_calculated(&mut self, id: usize) { - self.commits_calculated.insert(id, true); + pub fn get_custom_commits_ptr(&self) -> [*mut u8; 10] { + let mut ptrs = [std::ptr::null_mut(); 10]; + for (i, custom_commit) in self.custom_commits.iter().enumerate() { + ptrs[i] = custom_commit.as_ptr() as *mut u8; + } + ptrs } - pub fn set_custom_commit_calculated(&mut self, commit_id: usize, id: usize) { - self.custom_commits_calculated[commit_id].insert(id, true); + pub fn get_custom_commits_extended_ptr(&self) -> [*mut u8; 10] { + let mut ptrs = [std::ptr::null_mut(); 10]; + for (i, custom_commit) in self.custom_commits_extended.iter().enumerate() { + ptrs[i] = custom_commit.as_ptr() as *mut u8; + } + ptrs } pub fn set_air_instance_id(&mut self, air_instance_id: usize, idx: usize) { @@ -298,11 +280,11 @@ impl AirInstance { self.idx = Some(idx); } - pub fn set_airgroupvalue_calculated(&mut self, id: usize) { - self.airgroupvalue_calculated.insert(id, true); + pub fn set_prover_initialized(&mut self) { + self.prover_initialized = true; } - pub fn set_airvalue_calculated(&mut self, id: usize) { - self.airvalue_calculated.insert(id, true); + pub fn clear_trace(&mut self) { + self.trace = Vec::new(); } } diff --git a/common/src/air_instances_repository.rs b/common/src/air_instances_repository.rs index 421d313e..0af8e749 100644 --- a/common/src/air_instances_repository.rs +++ b/common/src/air_instances_repository.rs @@ -30,7 +30,7 @@ impl AirInstancesRepository { } } - pub fn add_air_instance(&self, mut air_instance: AirInstance, global_idx: Option) { + pub fn add_air_instance(&self, mut air_instance: AirInstance, global_idx: Option) -> usize { let mut air_instances = self.air_instances.write().unwrap(); let n_air_instances = air_instances.len(); @@ -44,6 +44,7 @@ impl AirInstancesRepository { } *instance_id += 1; air_instances.push(air_instance); + air_instances.len() - 1 } pub fn find_airgroup_instances(&self, airgroup_id: usize) -> Vec { @@ -71,6 +72,21 @@ impl AirInstancesRepository { indices } + pub fn find_instance(&self, airgroup_id: usize, air_id: usize, air_instance_id: usize) -> Option { + let air_instances = self.air_instances.read().unwrap(); + + for (index, air_instance) in air_instances.iter().enumerate() { + if air_instance.airgroup_id == airgroup_id + && air_instance.air_id == air_id + && air_instance.air_instance_id.unwrap() == air_instance_id + { + return Some(index); + } + } + + None + } + // pub fn calculate_my_groups(&self) { // let mut group_indices: BTreeMap> = BTreeMap::new(); @@ -104,18 +120,4 @@ impl AirInstancesRepository { // println!("// MY AIR GROUPS {:?} // MY GROUPS {:?}", instances_info.my_air_groups, instances_info.my_groups); // } - - pub fn find_last_segment(&self, airgroup_id: usize, air_id: usize) -> Option { - let air_instances = self.air_instances.read().unwrap(); - - air_instances - .iter() - .filter(|air_instance| { - air_instance.airgroup_id == airgroup_id - && air_instance.air_id == air_id - && air_instance.air_segment_id.is_some() - }) - .map(|air_instance| air_instance.air_segment_id.unwrap_or(0)) - .max() - } } diff --git a/common/src/buffer_allocator.rs b/common/src/buffer_allocator.rs deleted file mode 100644 index 46c31a83..00000000 --- a/common/src/buffer_allocator.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::error::Error; - -use crate::SetupCtx; - -pub trait BufferAllocator: Send + Sync { - // Returns the size of the buffer and the offsets for each stage - fn get_buffer_info_custom_commit( - &self, - sctx: &SetupCtx, - airgroup_id: usize, - air_id: usize, - custom_commit_name: &str, - ) -> Result<(u64, Vec, u64), Box>; -} diff --git a/common/src/constraints.rs b/common/src/constraints.rs new file mode 100644 index 00000000..31520811 --- /dev/null +++ b/common/src/constraints.rs @@ -0,0 +1,90 @@ +use std::sync::Arc; + +use proofman_starks_lib_c::{ + get_n_constraints_c, get_constraints_lines_sizes_c, get_constraints_lines_c, get_n_global_constraints_c, + get_global_constraints_lines_sizes_c, get_global_constraints_lines_c, +}; + +use crate::SetupCtx; + +#[derive(Default, Debug, Clone, Copy)] +#[repr(C)] +pub struct ConstraintRowInfo { + pub row: u64, + pub dim: u64, + pub value: [u64; 3usize], +} + +#[derive(Default, Debug, Clone, Copy)] +#[repr(C)] +pub struct ConstraintInfo { + pub id: u64, + pub stage: u64, + pub im_pol: bool, + pub n_rows: u64, + pub skip: bool, + pub rows: [ConstraintRowInfo; 10usize], +} + +#[derive(Default, Debug, Clone, Copy)] +#[repr(C)] +pub struct GlobalConstraintInfo { + pub id: u64, + pub dim: u64, + pub valid: bool, + pub skip: bool, + pub value: [u64; 3usize], +} + +pub fn get_constraints_lines_str(sctx: Arc, airgroup_id: usize, air_id: usize) -> Vec { + let setup = sctx.get_setup(airgroup_id, air_id); + + let p_setup = (&setup.p_setup).into(); + let n_constraints = get_n_constraints_c(p_setup); + + let mut constraints_sizes = vec![0u64; n_constraints as usize]; + + get_constraints_lines_sizes_c(p_setup, constraints_sizes.as_mut_ptr()); + + let mut constraints_lines = vec![Vec::new(); n_constraints as usize]; + for i in 0..n_constraints as usize { + constraints_lines[i] = vec![0u8; constraints_sizes[i] as usize]; + } + + get_constraints_lines_c( + p_setup, + constraints_lines.iter_mut().map(|v| v.as_mut_ptr()).collect::>().as_mut_ptr(), + ); + + let mut constraints_lines_str = Vec::new(); + for constraint_line in constraints_lines { + constraints_lines_str.push(std::str::from_utf8(&constraint_line).unwrap().to_string()); + } + + constraints_lines_str +} + +pub fn get_global_constraints_lines_str(sctx: Arc) -> Vec { + let n_global_constraints = get_n_global_constraints_c(sctx.get_global_bin()); + + let mut global_constraints_sizes = vec![0u64; n_global_constraints as usize]; + + get_global_constraints_lines_sizes_c(sctx.get_global_bin(), global_constraints_sizes.as_mut_ptr()); + + let mut global_constraints_lines = vec![Vec::new(); n_global_constraints as usize]; + for i in 0..n_global_constraints as usize { + global_constraints_lines[i] = vec![0u8; global_constraints_sizes[i] as usize]; + } + + get_global_constraints_lines_c( + sctx.get_global_bin(), + global_constraints_lines.iter_mut().map(|v| v.as_mut_ptr()).collect::>().as_mut_ptr(), + ); + + let mut global_constraints_lines_str = Vec::new(); + for global_constraint_line in global_constraints_lines { + global_constraints_lines_str.push(std::str::from_utf8(&global_constraint_line).unwrap().to_string()); + } + + global_constraints_lines_str +} diff --git a/common/src/custom_commits.rs b/common/src/custom_commits.rs index 1a2f2646..471f300a 100644 --- a/common/src/custom_commits.rs +++ b/common/src/custom_commits.rs @@ -1,37 +1,22 @@ use proofman_starks_lib_c::{extend_and_merkelize_custom_commit_c, fri_proof_new_c, starks_new_c}; use p3_goldilocks::Goldilocks; -use serde_json::Value; -use std::collections::HashMap; -use std::os::raw::c_void; -use std::path::PathBuf; use crate::Setup; -pub fn parse_cached_buffers(s: &str) -> Result, String> { - let json_data: Value = serde_json::from_str(s).map_err(|e| format!("Invalid JSON: {}", e))?; - let mut map = HashMap::new(); - - if let Some(obj) = json_data.as_object() { - for (key, value) in obj { - if let Some(path_str) = value.as_str() { - map.insert(key.clone(), PathBuf::from(path_str)); - } else { - return Err(format!("Expected string for path in key '{}'", key)); - } - } - } else { - return Err("Expected JSON object for cached_buffers".to_string()); - } - - Ok(map) -} - -pub fn get_custom_commit_trace(commit_id: u64, step: u64, setup: &Setup, buffer: Vec, buffer_str: &str) { +pub fn get_custom_commit_trace( + commit_id: u64, + step: u64, + setup: &Setup, + buffer: Vec, + buffer_ext: Vec, + buffer_str: &str, +) { extend_and_merkelize_custom_commit_c( starks_new_c((&setup.p_setup).into(), std::ptr::null_mut()), commit_id, step, - buffer.as_ptr() as *mut c_void, + buffer.as_ptr() as *mut u8, + buffer_ext.as_ptr() as *mut u8, fri_proof_new_c((&setup.p_setup).into()), std::ptr::null_mut(), buffer_str, diff --git a/common/src/distribution_ctx.rs b/common/src/distribution_ctx.rs index cf333622..31c4ef09 100644 --- a/common/src/distribution_ctx.rs +++ b/common/src/distribution_ctx.rs @@ -107,6 +107,15 @@ impl DistributionCtx { self.instances_owner[instance_idx].0 } + #[inline] + pub fn find_instance(&self, airgroup_id: usize, air_id: usize) -> (bool, usize) { + if let Some(index) = self.instances.iter().position(|&(x, y)| x == airgroup_id && y == air_id) { + (true, index) + } else { + (false, 0) + } + } + #[inline] pub fn add_instance(&mut self, airgroup_id: usize, air_id: usize, weight: usize) -> (bool, usize) { let mut is_mine = false; @@ -197,19 +206,21 @@ impl DistributionCtx { } } + pub fn distribute_airgroupvalues(&self, _airgroupvalues: &mut [u64], _owner: usize) {} + pub fn distribute_multiplicity(&self, _multiplicity: &mut [u64], _owner: usize) { #[cfg(feature = "distributed")] { //assert that I can operate with u32 - assert!(_multiplicity.len() < std::u32::MAX as usize); + assert!(_multiplicity.len() < u32::MAX as usize); if _owner != self.rank as usize { //pack multiplicities in a sparce vector let mut packed_multiplicity = Vec::new(); - packed_multiplicity.push(0 as u32); //this will be the counter + packed_multiplicity.push(0u32); //this will be the counter for (idx, &m) in _multiplicity.iter().enumerate() { if m != 0 { - assert!(m < std::u32::MAX as u64); + assert!(m < u32::MAX as u64); packed_multiplicity.push(idx as u32); packed_multiplicity.push(m as u32); packed_multiplicity[0] += 2; @@ -238,7 +249,7 @@ impl DistributionCtx { // Ensure that each multiplicity vector can be operated with u32 let mut buff_size = 0; for multiplicity in _multiplicities.iter() { - assert!(multiplicity.len() < std::u32::MAX as usize); + assert!(multiplicity.len() < u32::MAX as usize); buff_size += multiplicity.len() + 1; } @@ -249,7 +260,7 @@ impl DistributionCtx { for (col_idx, multiplicity) in _multiplicities.iter().enumerate() { for (idx, &m) in multiplicity.iter().enumerate() { if m != 0 { - assert!(m < std::u32::MAX as u64); + assert!(m < u32::MAX as u64); packed_multiplicities[col_idx] += 1; packed_multiplicities.push(idx as u32); packed_multiplicities.push(m as u32); diff --git a/common/src/execution_ctx.rs b/common/src/execution_ctx.rs deleted file mode 100644 index 85b2e559..00000000 --- a/common/src/execution_ctx.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::{path::PathBuf, sync::Arc}; -use crate::{BufferAllocator, DistributionCtx, StdMode, VerboseMode}; -use std::sync::RwLock; -use std::collections::HashMap; -#[allow(dead_code)] -/// Represents the context when executing a witness computer plugin -pub struct ExecutionCtx { - pub rom_path: Option, - pub cached_buffers_path: Option>, - /// If true, the plugin must generate the public outputs - pub public_output: bool, - pub buffer_allocator: Arc, - pub verbose_mode: VerboseMode, - pub dctx: RwLock, - pub std_mode: StdMode, -} - -impl ExecutionCtx { - pub fn builder() -> ExecutionCtxBuilder { - ExecutionCtxBuilder::new() - } -} - -pub struct ExecutionCtxBuilder { - rom_path: Option, - cached_buffers_path: Option>, - public_output: bool, - buffer_allocator: Option>, - verbose_mode: VerboseMode, - std_mode: StdMode, -} - -impl Default for ExecutionCtxBuilder { - fn default() -> Self { - Self::new() - } -} - -impl ExecutionCtxBuilder { - pub fn new() -> Self { - ExecutionCtxBuilder { - rom_path: None, - cached_buffers_path: None, - public_output: true, - buffer_allocator: None, - verbose_mode: VerboseMode::Info, - std_mode: StdMode::default(), - } - } - - pub fn with_rom_path(mut self, rom_path: Option) -> Self { - self.rom_path = rom_path; - self - } - - pub fn with_cached_buffers_path(mut self, cached_buffers_path: Option>) -> Self { - self.cached_buffers_path = cached_buffers_path; - self - } - - pub fn with_buffer_allocator(mut self, buffer_allocator: Arc) -> Self { - self.buffer_allocator = Some(buffer_allocator); - self - } - - pub fn with_verbose_mode(mut self, verbose_mode: VerboseMode) -> Self { - self.verbose_mode = verbose_mode; - self - } - - pub fn with_std_mode(mut self, std_mode: StdMode) -> Self { - self.std_mode = std_mode; - self - } - - pub fn build(self) -> ExecutionCtx { - if self.buffer_allocator.is_none() { - panic!("Buffer allocator is required"); - } - - ExecutionCtx { - rom_path: self.rom_path, - cached_buffers_path: self.cached_buffers_path, - public_output: self.public_output, - buffer_allocator: self.buffer_allocator.unwrap(), - verbose_mode: self.verbose_mode, - dctx: RwLock::new(DistributionCtx::new()), - std_mode: self.std_mode, - } - } -} diff --git a/common/src/global_info.rs b/common/src/global_info.rs index 8c6469f2..8445a944 100644 --- a/common/src/global_info.rs +++ b/common/src/global_info.rs @@ -11,6 +11,8 @@ pub struct ProofValueMap { pub name: String, #[serde(default)] pub id: u64, + #[serde(default)] + pub stage: u64, } #[derive(Clone, Deserialize)] pub struct PublicMap { @@ -38,8 +40,6 @@ pub struct GlobalInfo { pub n_publics: usize, #[serde(rename = "numChallenges")] pub n_challenges: Vec, - #[serde(rename = "numProofValues")] - pub n_proof_values: usize, #[serde(rename = "proofValuesMap")] pub proof_values_map: Option>, diff --git a/common/src/lib.rs b/common/src/lib.rs index 5b10c1fe..14d27fb5 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,45 +1,34 @@ mod air_instance; mod air_instances_repository; -mod buffer_allocator; mod verbose_mode; -mod execution_ctx; mod distribution_ctx; -mod lib_pilout; mod proof_ctx; mod prover; mod extended_field; mod setup; mod setup_ctx; mod std_mode; -mod custom_commits; +mod publics; pub mod trace; pub mod global_info; pub mod stark_info; +pub mod utils; +pub mod custom_commits; +pub mod constraints; pub use air_instance::*; pub use air_instances_repository::*; -pub use buffer_allocator::*; -use proofman_starks_lib_c::set_log_level_c; pub use verbose_mode::*; -pub use execution_ctx::*; -pub use distribution_ctx::*; -pub use lib_pilout::*; pub use proof_ctx::*; pub use prover::*; pub use extended_field::*; pub use global_info::*; pub use setup::*; pub use setup_ctx::*; -pub use custom_commits::*; pub use stark_info::*; pub use std_mode::*; - -pub fn initialize_logger(verbose_mode: VerboseMode) { - env_logger::builder() - .format_timestamp(None) - .format_level(true) - .format_target(false) - .filter_level(verbose_mode.into()) - .init(); - set_log_level_c(verbose_mode.into()); -} +pub use publics::*; +pub use utils::*; +pub use distribution_ctx::*; +pub use custom_commits::*; +pub use constraints::*; diff --git a/common/src/lib_pilout.rs b/common/src/lib_pilout.rs deleted file mode 100644 index 45943344..00000000 --- a/common/src/lib_pilout.rs +++ /dev/null @@ -1,124 +0,0 @@ -#[derive(Debug)] -pub struct WitnessPilout { - name: String, - hash: Vec, - num_stages: u32, - air_groups: Vec, -} - -impl WitnessPilout { - pub fn new(name: &str, num_stages: u32, hash: Vec) -> Self { - WitnessPilout { name: name.to_string(), num_stages, air_groups: Vec::new(), hash } - } - - pub fn add_air_group(&mut self, air_group_name: Option<&str>) -> &mut AirGroup { - let airgroup_id = self.air_groups.len(); - let air_group = AirGroup::new(airgroup_id, air_group_name); - self.air_groups.push(air_group); - &mut self.air_groups[airgroup_id] - } - - pub fn find_air(&self, airgroup_name: &str, air_name: &str) -> Option<&Air> { - for airgroup in &self.air_groups { - if let Some(name) = &airgroup.name { - if name == airgroup_name { - for air in &airgroup.airs { - if let Some(name) = &air.name { - if name == air_name { - return Some(air); - } - } - } - } - } - } - None - } - - pub fn get_air_group(&self, airgroup_id: usize) -> &AirGroup { - &self.air_groups[airgroup_id] - } - - pub fn get_air(&self, airgroup_id: usize, air_id: usize) -> &Air { - &self.air_groups[airgroup_id].airs[air_id] - } - - pub fn name(&self) -> &str { - &self.name - } - - pub fn hash(&self) -> &[u8] { - &self.hash - } - - pub fn num_stages(&self) -> u32 { - self.num_stages - } - - pub fn air_groups(&self) -> &[AirGroup] { - &self.air_groups - } -} - -#[derive(Debug)] -pub struct AirGroup { - airgroup_id: usize, - name: Option, - airs: Vec, -} - -impl AirGroup { - pub fn new(airgroup_id: usize, name: Option<&str>) -> Self { - AirGroup { airgroup_id, name: name.map(|s| s.to_string()), airs: Vec::new() } - } - - pub fn add_air(&mut self, air_name: Option<&str>, num_rows: usize) -> &Air { - let air_id = self.airs.len(); - let air = Air::new(self.airgroup_id, air_id, air_name, num_rows); - self.airs.push(air); - &self.airs[air_id] - } - - pub fn airgroup_id(&self) -> usize { - self.airgroup_id - } - - pub fn name(&self) -> Option<&str> { - self.name.as_deref() - } - - pub fn airs(&self) -> &[Air] { - &self.airs - } -} - -#[derive(Debug)] -pub struct Air { - pub airgroup_id: usize, - pub air_id: usize, - pub name: Option, - /// log2(n), where n is the number of rows - num_rows: usize, -} - -impl Air { - pub fn new(airgroup_id: usize, air_id: usize, name: Option<&str>, num_rows: usize) -> Self { - Air { airgroup_id, air_id, name: name.map(|s| s.to_string()), num_rows } - } - - pub fn airgroup_id(&self) -> usize { - self.airgroup_id - } - - pub fn air_id(&self) -> usize { - self.air_id - } - - pub fn name(&self) -> Option<&str> { - self.name.as_deref() - } - - pub fn num_rows(&self) -> usize { - self.num_rows - } -} diff --git a/common/src/proof_ctx.rs b/common/src/proof_ctx.rs index 73047d42..54e45ffd 100644 --- a/common/src/proof_ctx.rs +++ b/common/src/proof_ctx.rs @@ -1,214 +1,217 @@ -use std::collections::HashMap; -use std::{mem::MaybeUninit, sync::RwLock}; +use std::{collections::HashMap, sync::RwLock}; use std::path::PathBuf; use p3_field::Field; -use crate::{AirInstancesRepository, GlobalInfo, StdMode, VerboseMode, WitnessPilout}; +use crate::{ModeName, distribution_ctx::DistributionCtx, AirInstancesRepository, GlobalInfo, StdMode, VerboseMode}; -pub struct PublicInputs { - pub inputs: RwLock>, - pub inputs_set: RwLock>, -} - -impl Default for PublicInputs { - fn default() -> Self { - Self { inputs: RwLock::new(Vec::new()), inputs_set: RwLock::new(Vec::new()) } - } -} - -impl PublicInputs { - pub fn new(n_publics: usize) -> Self { - Self { - inputs: RwLock::new(vec![0; n_publics * std::mem::size_of::()]), - inputs_set: RwLock::new(vec![false; n_publics]), - } - } -} - -pub struct ProofValues { +pub struct Values { pub values: RwLock>, - pub values_set: RwLock>, } -impl Default for ProofValues { - fn default() -> Self { - Self { values: RwLock::new(Vec::new()), values_set: RwLock::new(HashMap::new()) } +impl Values { + pub fn new(n_values: usize) -> Self { + Self { values: RwLock::new(vec![F::zero(); n_values]) } } } -pub struct Challenges { - pub challenges: RwLock>, -} - -impl Default for Challenges { +impl Default for Values { fn default() -> Self { - Self { challenges: RwLock::new(Vec::new()) } + Self { values: RwLock::new(Vec::new()) } } } -pub struct BuffHelper { - pub buff_helper: RwLock>>, -} - -impl Default for BuffHelper { - fn default() -> Self { - Self { buff_helper: RwLock::new(Vec::new()) } - } -} +pub type AirGroupMap = HashMap; +pub type AirIdMap = HashMap; +pub type InstanceMap = HashMap>; +#[derive(Clone)] pub struct ProofOptions { pub verify_constraints: bool, pub verbose_mode: VerboseMode, - pub std_mode: StdMode, pub aggregation: bool, pub final_snark: bool, + pub debug_info: DebugInfo, +} + +#[derive(Clone)] +pub struct DebugInfo { + pub debug_instances: AirGroupMap, + pub debug_global_instances: Vec, + pub std_mode: StdMode, +} + +impl DebugInfo { + pub fn new() -> Self { + Self { + std_mode: StdMode::new(ModeName::Standard, Vec::new(), 0, false), + debug_instances: HashMap::new(), + debug_global_instances: Vec::new(), + } + } } impl ProofOptions { pub fn new( verify_constraints: bool, verbose_mode: VerboseMode, - std_mode: StdMode, aggregation: bool, final_snark: bool, + debug_info: DebugInfo, ) -> Self { - Self { verify_constraints, verbose_mode, std_mode, aggregation, final_snark } + Self { verify_constraints, verbose_mode, aggregation, final_snark, debug_info } } } #[allow(dead_code)] pub struct ProofCtx { - pub pilout: WitnessPilout, - pub public_inputs: PublicInputs, - pub proof_values: ProofValues, - pub challenges: Challenges, - pub buff_helper: BuffHelper, + pub public_inputs: Values, + pub proof_values: Values, + pub challenges: Values, + pub buff_helper: Values, pub global_info: GlobalInfo, pub air_instance_repo: AirInstancesRepository, + pub options: ProofOptions, + pub dctx: RwLock, } impl ProofCtx { const MY_NAME: &'static str = "ProofCtx"; - pub fn create_ctx(pilout: WitnessPilout, proving_key_path: PathBuf) -> Self { + pub fn create_ctx(proving_key_path: PathBuf, options: ProofOptions) -> Self { log::info!("{}: Creating proof context", Self::MY_NAME); let global_info: GlobalInfo = GlobalInfo::new(&proving_key_path); - - let proof_values = ProofValues { - values: RwLock::new(vec![F::zero(); global_info.n_proof_values * 3]), - values_set: RwLock::new(HashMap::new()), - }; let n_publics = global_info.n_publics; + let n_proof_values = global_info.proof_values_map.as_ref().unwrap().len(); + let n_challenges = 4 + global_info.n_challenges.iter().sum::(); + Self { - pilout, global_info, - public_inputs: PublicInputs::new(n_publics), - proof_values, - challenges: Challenges::default(), - buff_helper: BuffHelper::default(), + public_inputs: Values::new(n_publics), + proof_values: Values::new(n_proof_values * 3), + challenges: Values::new(n_challenges * 3), + buff_helper: Values::default(), air_instance_repo: AirInstancesRepository::new(), + dctx: RwLock::new(DistributionCtx::new()), + options, } } - pub fn set_proof_value(&self, name: &str, value: F) { - let id = (0..self.global_info.n_proof_values) - .find(|&i| { - if let Some(proof_value) = self - .global_info - .proof_values_map - .as_ref() - .expect("global_info.proof_values_map is not initialized") - .get(i) - { - proof_value.name == name - } else { - false - } - }) - .unwrap_or_else(|| panic!("No proof value found with name {}", name)); - - self.proof_values.values.write().unwrap()[3 * id] = value; - self.proof_values.values.write().unwrap()[3 * id + 1] = F::zero(); - self.proof_values.values.write().unwrap()[3 * id + 2] = F::zero(); - - self.set_proof_value_calculated(id); - } - - pub fn set_proof_value_ext(&self, name: &str, value: Vec) { - let id = (0..self.global_info.n_proof_values) - .find(|&i| { - if let Some(proof_value) = self - .global_info - .proof_values_map - .as_ref() - .expect("global_info.proof_values_map is not initialized") - .get(i) - { - proof_value.name == name - } else { - false - } - }) - .unwrap_or_else(|| panic!("No proof value found with name {}", name)); + pub fn dctx_add_instance(&self, airgroup_id: usize, air_id: usize, weight: usize) -> (bool, usize) { + let mut dctx = self.dctx.write().unwrap(); + dctx.add_instance(airgroup_id, air_id, weight) + } + + pub fn dctx_distribute_multiplicity(&self, multiplicity: &mut [u64], instance_idx: usize) { + let dctx = self.dctx.read().unwrap(); + let owner = dctx.owner(instance_idx); + dctx.distribute_multiplicity(multiplicity, owner); + } - self.proof_values.values.write().unwrap()[3 * id] = value[0]; - self.proof_values.values.write().unwrap()[3 * id + 1] = value[1]; - self.proof_values.values.write().unwrap()[3 * id + 2] = value[2]; + pub fn dctx_distribute_airgroupvalues(&self, airgroup_values: &mut [u64], instance_idx: usize) { + let dctx = self.dctx.read().unwrap(); + let owner = dctx.owner(instance_idx); + dctx.distribute_airgroupvalues(airgroup_values, owner); + } - self.set_proof_value_calculated(id); + pub fn get_proof_values_ptr(&self) -> *mut u8 { + let guard = &self.proof_values.values.read().unwrap(); + guard.as_ptr() as *mut u8 } - pub fn set_proof_value_calculated(&self, id: usize) { - self.proof_values.values_set.write().unwrap().insert(id, true); + pub fn set_public_value(&self, value: F, public_id: usize) { + self.public_inputs.values.write().unwrap()[public_id] = value; } - pub fn set_public_value(&self, value: u64, public_id: u64) { - self.public_inputs.inputs.write().unwrap()[(public_id as usize) * 8..(public_id as usize + 1) * 8] - .copy_from_slice(&value.to_le_bytes()); + pub fn get_publics(&self) -> std::sync::RwLockWriteGuard> { + self.public_inputs.values.write().unwrap() + } - self.public_inputs.inputs_set.write().unwrap()[public_id as usize] = true; + pub fn get_proof_values(&self) -> std::sync::RwLockWriteGuard> { + self.proof_values.values.write().unwrap() } - pub fn set_public_value_by_name(&self, value: u64, public_name: &str, lengths: Option>) { - let n_publics: usize = self.global_info.publics_map.as_ref().expect("REASON").len(); - let public_id = (0..n_publics) - .find(|&i| { - let public = self.global_info.publics_map.as_ref().expect("REASON").get(i).unwrap(); + pub fn get_proof_values_by_stage(&self, stage: u32) -> Vec { + let proof_vals = self.proof_values.values.read().unwrap(); + + let mut values = Vec::new(); + let mut p = 0; + for proof_value in self.global_info.proof_values_map.as_ref().unwrap() { + if proof_value.stage > stage as u64 { + break; + } + if proof_value.stage == 1 { + if stage == 1 { + values.push(proof_vals[p]); + } + p += 1; + } else { + if proof_value.stage == stage as u64 { + values.push(proof_vals[p]); + values.push(proof_vals[p + 1]); + values.push(proof_vals[p + 2]); + } + p += 3; + } + } - // Check if name matches - let name_matches = public.name == public_name; + values + } + + pub fn get_publics_ptr(&self) -> *mut u8 { + let guard = &self.public_inputs.values.read().unwrap(); + guard.as_ptr() as *mut u8 + } - // If lengths is provided, check that it matches public.lengths - let lengths_match = if let Some(ref provided_lengths) = lengths { - Some(&public.lengths) == Some(provided_lengths) - } else { - true // If lengths is None, skip the lengths check - }; + pub fn get_challenges_ptr(&self) -> *mut u8 { + let guard = &self.challenges.values.read().unwrap(); + guard.as_ptr() as *mut u8 + } - name_matches && lengths_match - }) - .unwrap_or_else(|| { - panic!("Name {} with specified lengths {:?} not found in publics_map", public_name, lengths) - }); + pub fn get_buff_helper_ptr(&self) -> *mut u8 { + let guard = &self.buff_helper.values.read().unwrap(); + guard.as_ptr() as *mut u8 + } - self.set_public_value(value, public_id as u64); + pub fn get_air_instance_trace(&self, airgroup_id: usize, air_id: usize, air_instance_id: usize) -> Vec { + let index = self.air_instance_repo.find_instance(airgroup_id, air_id, air_instance_id); + if let Some(index) = index { + return self.air_instance_repo.air_instances.read().unwrap()[index].get_trace(); + } else { + panic!( + "Air Instance with id {} for airgroup {} and air {} not found", + air_instance_id, airgroup_id, air_id + ); + } } - pub fn get_public_value(&self, public_name: &str) -> u64 { - let n_publics: usize = self.global_info.publics_map.as_ref().expect("REASON").len(); - let public_id = (0..n_publics) - .find(|&i| { - let public = self.global_info.publics_map.as_ref().expect("REASON").get(i).unwrap(); - public.name == public_name - }) - .unwrap_or_else(|| panic!("Name {} not found in publics_map", public_name)); + pub fn get_air_instance_air_values(&self, airgroup_id: usize, air_id: usize, air_instance_id: usize) -> Vec { + let index = self.air_instance_repo.find_instance(airgroup_id, air_id, air_instance_id); + if let Some(index) = index { + return self.air_instance_repo.air_instances.read().unwrap()[index].get_air_values(); + } else { + panic!( + "Air Instance with id {} for airgroup {} and air {} not found", + air_instance_id, airgroup_id, air_id + ); + } + } - u64::from_le_bytes( - self.public_inputs.inputs.read().unwrap()[public_id * 8..(public_id + 1) * 8] - .try_into() - .expect("Expected 8 bytes for u64"), - ) + pub fn get_air_instance_airgroup_values( + &self, + airgroup_id: usize, + air_id: usize, + air_instance_id: usize, + ) -> Vec { + let index = self.air_instance_repo.find_instance(airgroup_id, air_id, air_instance_id); + if let Some(index) = index { + return self.air_instance_repo.air_instances.read().unwrap()[index].get_airgroup_values(); + } else { + panic!( + "Air Instance with id {} for airgroup {} and air {} not found", + air_instance_id, airgroup_id, air_id + ); + } } } diff --git a/common/src/prover.rs b/common/src/prover.rs index 10f09a17..ea08f47d 100644 --- a/common/src/prover.rs +++ b/common/src/prover.rs @@ -1,10 +1,10 @@ use std::os::raw::c_void; -use std::os::raw::c_char; use std::sync::Arc; use p3_field::Field; use transcript::FFITranscript; +use crate::ConstraintInfo; use crate::ProofCtx; use crate::SetupCtx; @@ -31,32 +31,6 @@ pub struct ProverInfo { pub instance_id: usize, } -#[derive(Debug, Clone, Copy)] -#[repr(C)] -pub struct ConstraintRowInfo { - pub row: u64, - pub dim: u64, - pub value: [u64; 3usize], -} - -#[derive(Debug, Clone, Copy)] -#[repr(C)] -pub struct ConstraintInfo { - pub id: u64, - pub stage: u64, - pub im_pol: bool, - pub line: *const c_char, - pub n_rows: u64, - pub rows: [ConstraintRowInfo; 10usize], -} - -#[derive(Debug, Clone, Copy)] -#[repr(C)] -pub struct ConstraintsResults { - pub n_constraints: u64, - pub constraints_info: *mut ConstraintInfo, -} - pub trait Prover { fn build(&mut self, proof_ctx: Arc>); fn free(&mut self); @@ -64,14 +38,13 @@ pub trait Prover { fn num_stages(&self) -> u32; fn get_challenges(&self, stage_id: u32, proof_ctx: Arc>, transcript: &FFITranscript); fn calculate_stage(&mut self, stage_id: u32, setup_ctx: Arc, proof_ctx: Arc>); - fn check_stage(&self, stage_id: u32, proof_ctx: Arc>); fn commit_stage(&mut self, stage_id: u32, proof_ctx: Arc>) -> ProverStatus; fn calculate_xdivxsub(&mut self, proof_ctx: Arc>); fn calculate_lev(&mut self, proof_ctx: Arc>); fn opening_stage(&mut self, opening_id: u32, setup_ctx: Arc, proof_ctx: Arc>) -> ProverStatus; - fn get_buff_helper_size(&self) -> usize; + fn get_buff_helper_size(&self, proof_ctx: Arc>) -> usize; fn get_proof(&self) -> *mut c_void; fn get_prover_info(&self) -> ProverInfo; fn get_zkin_proof(&self, proof_ctx: Arc>, output_dir: &str) -> *mut c_void; diff --git a/common/src/publics.rs b/common/src/publics.rs new file mode 100644 index 00000000..b0bbb1f9 --- /dev/null +++ b/common/src/publics.rs @@ -0,0 +1,24 @@ +use serde::de::DeserializeOwned; +use std::fs::File; +use std::io::Read; +use std::path::PathBuf; + +pub fn load_from_json(path: &Option) -> T { + if let Some(path) = &path { + // Open the file if the path is Some + let mut file = File::open(path).unwrap_or_else(|_| panic!("File not found at {:?}", path)); + + if !file.metadata().unwrap().is_file() { + panic!("{:?} is not a valid file", path); + } + + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap_or_else(|err| panic!("Failed to read file: {}", err)); + + // Deserialize the contents into the expected struct + serde_json::from_str(&contents).unwrap_or_else(|err| panic!("Failed to parse JSON: {}", err)) + } else { + // Return the default value if path is None + T::default() + } +} diff --git a/common/src/setup.rs b/common/src/setup.rs index 4b119061..86317d65 100644 --- a/common/src/setup.rs +++ b/common/src/setup.rs @@ -1,13 +1,14 @@ -use std::mem::MaybeUninit; use std::os::raw::c_void; use std::path::PathBuf; use std::sync::RwLock; +use proofman_starks_lib_c::get_map_totaln_c; use proofman_starks_lib_c::{ get_const_tree_size_c, get_const_size_c, prover_helpers_new_c, expressions_bin_new_c, stark_info_new_c, load_const_tree_c, load_const_pols_c, calculate_const_tree_c, stark_info_free_c, expressions_bin_free_c, prover_helpers_free_c, }; +use proofman_util::create_buffer_fast; use crate::GlobalInfo; use crate::ProofType; @@ -32,7 +33,7 @@ impl From<&SetupC> for *mut c_void { #[derive(Debug)] pub struct Pols { - pub values: RwLock>>, + pub values: RwLock>, } impl Default for Pols { @@ -51,6 +52,7 @@ pub struct Setup { pub stark_info: StarkInfo, pub const_pols: Pols, pub const_tree: Pols, + pub prover_buffer_size: u64, } impl Setup { @@ -66,20 +68,21 @@ impl Setup { let stark_info_path = setup_path.display().to_string() + ".starkinfo.json"; let expressions_bin_path = setup_path.display().to_string() + ".bin"; - let (stark_info, p_stark_info, p_expressions_bin, p_prover_helpers) = + let (stark_info, p_stark_info, p_expressions_bin, p_prover_helpers, prover_buffer_size) = if setup_type == &ProofType::Compressor && !global_info.get_air_has_compressor(airgroup_id, air_id) { // If the condition is met, use None for each pointer - (StarkInfo::default(), std::ptr::null_mut(), std::ptr::null_mut(), std::ptr::null_mut()) + (StarkInfo::default(), std::ptr::null_mut(), std::ptr::null_mut(), std::ptr::null_mut(), 0) } else { // Otherwise, initialize the pointers with their respective values let stark_info_json = std::fs::read_to_string(&stark_info_path) .unwrap_or_else(|_| panic!("Failed to read file {}", &stark_info_path)); let stark_info = StarkInfo::from_json(&stark_info_json); let p_stark_info = stark_info_new_c(stark_info_path.as_str()); + let prover_buffer_size = get_map_totaln_c(p_stark_info); let expressions_bin = expressions_bin_new_c(expressions_bin_path.as_str(), false); let prover_helpers = prover_helpers_new_c(p_stark_info); - (stark_info, p_stark_info, expressions_bin, prover_helpers) + (stark_info, p_stark_info, expressions_bin, prover_helpers, prover_buffer_size) }; Self { @@ -89,6 +92,7 @@ impl Setup { p_setup: SetupC { p_stark_info, p_expressions_bin, p_prover_helpers }, const_pols: Pols::default(), const_tree: Pols::default(), + prover_buffer_size, } } @@ -113,10 +117,9 @@ impl Setup { let p_stark_info = self.p_setup.p_stark_info; let const_size = get_const_size_c(p_stark_info) as usize; - let const_pols: Vec> = Vec::with_capacity(const_size); + let const_pols: Vec = create_buffer_fast(const_size); - let p_const_pols_address = const_pols.as_ptr() as *mut c_void; - load_const_pols_c(p_const_pols_address, const_pols_path.as_str(), const_size as u64); + load_const_pols_c(const_pols.as_ptr() as *mut u8, const_pols_path.as_str(), const_size as u64); *self.const_pols.values.write().unwrap() = const_pols; } @@ -136,17 +139,30 @@ impl Setup { let const_tree_size = get_const_tree_size_c(p_stark_info) as usize; - let const_tree: Vec> = Vec::with_capacity(const_tree_size); + let const_tree: Vec = create_buffer_fast(const_tree_size); - let p_const_tree_address = const_tree.as_ptr() as *mut c_void; if PathBuf::from(&const_pols_tree_path).exists() { - load_const_tree_c(p_const_tree_address, const_pols_tree_path.as_str(), const_tree_size as u64); + load_const_tree_c(const_tree.as_ptr() as *mut u8, const_pols_tree_path.as_str(), const_tree_size as u64); } else { let const_pols = self.const_pols.values.read().unwrap(); - let p_const_pols_address = (*const_pols).as_ptr() as *mut c_void; let tree_filename = if save_file { const_pols_tree_path.as_str() } else { "" }; - calculate_const_tree_c(p_stark_info, p_const_pols_address, p_const_tree_address, tree_filename); + calculate_const_tree_c( + p_stark_info, + (*const_pols).as_ptr() as *mut u8, + const_tree.as_ptr() as *mut u8, + tree_filename, + ); }; *self.const_tree.values.write().unwrap() = const_tree; } + + pub fn get_const_ptr(&self) -> *mut u8 { + let guard = &self.const_pols.values.read().unwrap(); + guard.as_ptr() as *mut u8 + } + + pub fn get_const_tree_ptr(&self) -> *mut u8 { + let guard = &self.const_tree.values.read().unwrap(); + guard.as_ptr() as *mut u8 + } } diff --git a/common/src/setup_ctx.rs b/common/src/setup_ctx.rs index 7db99ccb..6647aa82 100644 --- a/common/src/setup_ctx.rs +++ b/common/src/setup_ctx.rs @@ -85,6 +85,7 @@ impl SetupsVadcop { pub struct SetupRepository { setups: HashMap<(usize, usize), Setup>, global_bin: Option<*mut c_void>, + global_info_file: String, } unsafe impl Send for SetupRepository {} @@ -104,6 +105,9 @@ impl SetupRepository { false => None, }; + let global_info_path = &global_info.get_proving_key_path().join("pilout.globalInfo.json"); + let global_info_file = global_info_path.to_str().unwrap().to_string(); + // Initialize Hashmap for each airgroup_id, air_id if setup_type != &ProofType::VadcopFinal { for (airgroup_id, air_group) in global_info.airs.iter().enumerate() { @@ -117,7 +121,7 @@ impl SetupRepository { timer_stop_and_log_debug!(INITIALIZE_SETUPS); - Self { setups, global_bin } + Self { setups, global_bin, global_info_file } } pub fn free(&self) { @@ -153,7 +157,15 @@ impl SetupCtx { } } + pub fn get_setups_list(&self) -> Vec<(usize, usize)> { + self.setup_repository.setups.keys().cloned().collect() + } + pub fn get_global_bin(&self) -> *mut c_void { self.setup_repository.global_bin.unwrap() } + + pub fn get_global_info_file(&self) -> String { + self.setup_repository.global_info_file.clone() + } } diff --git a/common/src/stark_info.rs b/common/src/stark_info.rs index 4e08115e..48589632 100644 --- a/common/src/stark_info.rs +++ b/common/src/stark_info.rs @@ -221,4 +221,24 @@ impl StarkInfo { pub fn from_json(stark_info_json: &str) -> Self { serde_json::from_str(stark_info_json).expect("Failed to parse JSON file") } + + pub fn get_buff_helper_size(&self) -> usize { + let mut max_cols = 0; + for stage in 1..=self.n_stages + 1 { + let n_cols = *self.map_sections_n.get(&format!("cm{}", stage)).unwrap() as usize; + if n_cols > max_cols { + max_cols = n_cols; + } + } + + let n_extended = (1 << self.stark_struct.n_bits_ext) as usize; + let buff_size_stages = max_cols * n_extended; + + let buff_size_xdivxsub = self.opening_points.len() * 3 * n_extended; + + match buff_size_stages > buff_size_xdivxsub { + true => buff_size_stages, + false => buff_size_xdivxsub, + } + } } diff --git a/common/src/std_mode.rs b/common/src/std_mode.rs index 0c11e9f5..e0f71829 100644 --- a/common/src/std_mode.rs +++ b/common/src/std_mode.rs @@ -7,14 +7,14 @@ pub const DEFAULT_PRINT_VALS: usize = 10; #[derive(Clone)] pub struct StdMode { pub name: ModeName, - pub opids: Option>, + pub opids: Vec, pub n_vals: usize, pub print_to_file: bool, } impl StdMode { - pub const fn new(name: ModeName, opids: Option>, n_vals: usize, print_to_file: bool) -> Self { - if n_vals == 0 { + pub const fn new(name: ModeName, opids: Vec, n_vals: usize, print_to_file: bool) -> Self { + if name.as_usize() != ModeName::Standard.as_usize() && n_vals == 0 { panic!("n_vals must be greater than 0"); } @@ -25,8 +25,8 @@ impl StdMode { impl From for StdMode { fn from(v: u8) -> Self { match v { - 0 => StdMode::new(ModeName::Standard, None, DEFAULT_PRINT_VALS, false), - 1 => StdMode::new(ModeName::Debug, None, DEFAULT_PRINT_VALS, false), + 0 => StdMode::new(ModeName::Standard, Vec::new(), DEFAULT_PRINT_VALS, false), + 1 => StdMode::new(ModeName::Debug, Vec::new(), DEFAULT_PRINT_VALS, false), _ => panic!("Invalid mode"), } } @@ -34,7 +34,7 @@ impl From for StdMode { impl Default for StdMode { fn default() -> Self { - StdMode::new(ModeName::Standard, None, DEFAULT_PRINT_VALS, false) + StdMode::new(ModeName::Standard, Vec::new(), DEFAULT_PRINT_VALS, false) } } @@ -65,3 +65,9 @@ impl PartialEq for usize { *self == (*other as usize) } } + +impl ModeName { + const fn as_usize(&self) -> usize { + *self as usize + } +} diff --git a/common/src/trace.rs b/common/src/trace.rs index 1ca6e89a..9a3bc9f4 100644 --- a/common/src/trace.rs +++ b/common/src/trace.rs @@ -1,55 +1,31 @@ -pub trait Trace: Send { +pub trait Trace: Send { fn num_rows(&self) -> usize; - fn get_buffer_ptr(&mut self) -> *mut u8; + fn n_cols(&self) -> usize; + fn airgroup_id(&self) -> usize; + fn air_id(&self) -> usize; + fn commit_id(&self) -> Option; + fn get_buffer(&mut self) -> Vec; } -pub use proofman_macros::trace; - -#[cfg(test)] -use crate as common; - -#[test] -#[should_panic] -fn test_errors_are_launched_when_num_rows_is_invalid_1() { - let mut buffer = vec![0u8; 3]; - trace!(SimpleRow, Simple { a: F }); - let _ = Simple::map_buffer(&mut buffer, 1, 0); +pub trait Values: Send { + fn get_buffer(&mut self) -> Vec; } -#[test] -#[should_panic] -fn test_errors_are_launched_when_num_rows_is_invalid_2() { - let mut buffer = vec![0u8; 3]; - trace!(SimpleRow, Simple { a: F }); - let _ = Simple::map_buffer(&mut buffer, 3, 0); -} +pub use proofman_macros::trace; -#[test] -#[should_panic] -fn test_errors_are_launched_when_num_rows_is_invalid_3() { - trace!(SimpleRow, Simple { a: F }); - let _ = Simple::::new(1); -} +pub use proofman_macros::values; -#[test] -#[should_panic] -fn test_errors_are_launched_when_num_rows_is_invalid_4() { - trace!(SimpleRow, Simple { a: F }); - let _ = Simple::::new(3); -} +#[cfg(test)] +use crate as common; #[test] fn check() { - const OFFSET: usize = 1; - let num_rows = 8; - - trace!(TraceRow, MyTrace { a: F, b:F}); + trace!(TraceRow, MyTrace { a: F, b:F}, 0, 0, 8, 0); assert_eq!(TraceRow::::ROW_SIZE, 2); - let mut buffer = vec![0usize; num_rows * TraceRow::::ROW_SIZE + OFFSET]; - let trace = MyTrace::map_buffer(&mut buffer, num_rows, OFFSET); - let mut trace = trace.unwrap(); + let mut trace = MyTrace::new(); + let num_rows = trace.num_rows(); // Set values for i in 0..num_rows { @@ -66,14 +42,11 @@ fn check() { #[test] fn check_array() { - let num_rows = 8; - - trace!(TraceRow, MyTrace { a: F, b: [F; 3], c: F }); + trace!(TraceRow, MyTrace { a: F, b: [F; 3], c: F }, 0, 0, 8, 0); assert_eq!(TraceRow::::ROW_SIZE, 5); - let mut buffer = vec![0usize; num_rows * TraceRow::::ROW_SIZE]; - let trace = MyTrace::map_buffer(&mut buffer, num_rows, 0); - let mut trace = trace.unwrap(); + let mut trace = MyTrace::new(); + let num_rows = trace.num_rows(); // Set values for i in 0..num_rows { @@ -84,6 +57,8 @@ fn check_array() { trace[i].c = i * 40; } + let buffer = trace.get_buffer(); + // Check values for i in 0..num_rows { assert_eq!(buffer[i * TraceRow::::ROW_SIZE], i); @@ -96,14 +71,12 @@ fn check_array() { #[test] fn check_multi_array() { - let num_rows = 8; - - trace!(TraceRow, MyTrace { a: [[F;3]; 2], b: F }); + trace!(TraceRow, MyTrace { a: [[F;3]; 2], b: F }, 0, 0, 8, 0); assert_eq!(TraceRow::::ROW_SIZE, 7); - let mut buffer = vec![0usize; num_rows * TraceRow::::ROW_SIZE]; - let trace = MyTrace::map_buffer(&mut buffer, num_rows, 0); - let mut trace = trace.unwrap(); + + let mut trace = MyTrace::new(); + let num_rows = trace.num_rows(); // Set values for i in 0..num_rows { @@ -116,6 +89,8 @@ fn check_multi_array() { trace[i].b = i + 3; } + let buffer = trace.get_buffer(); + // Check values for i in 0..num_rows { assert_eq!(buffer[i * TraceRow::::ROW_SIZE], i); @@ -130,14 +105,12 @@ fn check_multi_array() { #[test] fn check_multi_array_2() { - let num_rows = 8; - - trace!(TraceRow, MyTrace { a: [[F;3]; 2], b: F, c: [F; 2] }); + trace!(TraceRow, MyTrace { a: [[F;3]; 2], b: F, c: [F; 2] }, 0, 0, 8, 0); assert_eq!(TraceRow::::ROW_SIZE, 9); - let mut buffer = vec![0usize; num_rows * TraceRow::::ROW_SIZE]; - let trace = MyTrace::map_buffer(&mut buffer, num_rows, 0); - let mut trace = trace.unwrap(); + + let mut trace = MyTrace::new(); + let num_rows = trace.num_rows(); // Set values for i in 0..num_rows { @@ -152,6 +125,8 @@ fn check_multi_array_2() { trace[i].c[1] = i + 2; } + let buffer = trace.get_buffer(); + // Check values for i in 0..num_rows { assert_eq!(buffer[i * TraceRow::::ROW_SIZE], i); diff --git a/common/src/utils.rs b/common/src/utils.rs new file mode 100644 index 00000000..d9f60f74 --- /dev/null +++ b/common/src/utils.rs @@ -0,0 +1,165 @@ +use crate::{ + AirGroupMap, AirIdMap, AirInstance, DebugInfo, InstanceMap, ModeName, ProofCtx, ProofOptions, StdMode, VerboseMode, + DEFAULT_PRINT_VALS, +}; +use proofman_starks_lib_c::set_log_level_c; +use std::sync::Arc; +use std::collections::HashMap; +use p3_field::Field; +use serde::Deserialize; +use std::fs; + +pub fn add_air_instance(air_instance: AirInstance, pctx: Arc>) -> Option { + let (is_mine, gid) = pctx.dctx.write().unwrap().add_instance(air_instance.airgroup_id, air_instance.air_id, 1); + + if is_mine { + return Some(pctx.air_instance_repo.add_air_instance(air_instance, Some(gid))); + } + + None +} + +pub fn initialize_logger(verbose_mode: VerboseMode) { + env_logger::builder() + .format_timestamp(None) + .format_level(true) + .format_target(false) + .filter_level(verbose_mode.into()) + .init(); + set_log_level_c(verbose_mode.into()); +} + +pub fn format_bytes(mut num_bytes: f64) -> String { + let units = ["Bytes", "KB", "MB", "GB"]; + let mut unit_index = 0; + + while num_bytes >= 0.01 && unit_index < units.len() - 1 { + if num_bytes < 1024.0 { + break; + } + num_bytes /= 1024.0; + unit_index += 1; + } + + format!("{:.2} {}", num_bytes, units[unit_index]) +} + +pub fn skip_prover_instance( + options: ProofOptions, + airgroup_id: usize, + air_id: usize, + instance_id: usize, +) -> (bool, Vec) { + if options.debug_info.debug_instances.is_empty() { + return (false, Vec::new()); + } else if let Some(airgroup_id_map) = options.debug_info.debug_instances.get(&airgroup_id) { + if airgroup_id_map.is_empty() { + return (false, Vec::new()); + } else if let Some(air_id_map) = airgroup_id_map.get(&air_id) { + if air_id_map.is_empty() { + return (false, Vec::new()); + } else if let Some(instance_id_map) = air_id_map.get(&instance_id) { + return (false, instance_id_map.clone()); + } + } + } + + (true, Vec::new()) +} + +#[derive(Default, Deserialize)] +struct StdDebugMode { + #[serde(default)] + opids: Option>, + #[serde(default)] + n_print: Option, + #[serde(default)] + print_to_file: bool, +} + +#[derive(Deserialize)] +struct DebugJson { + #[serde(default)] + constraints: Option>, + #[serde(default)] + global_constraints: Option>, + #[serde(default)] + std_mode: Option, +} + +#[derive(Deserialize)] +struct AirGroupJson { + #[serde(default)] + airgroup_id: Option, + #[serde(default)] + air_ids: Option>, +} + +#[derive(Deserialize)] +struct AirIdJson { + #[serde(default)] + air_id: Option, + #[serde(default)] + instances_ids: Option>, +} + +#[derive(Deserialize)] +struct InstanceJson { + #[serde(default)] + instance_id: Option, + #[serde(default)] + constraints: Option>, +} + +pub fn json_to_debug_instances_map(json_path: String) -> DebugInfo { + // Read the file contents + let debug_json = fs::read_to_string(&json_path).unwrap_or_else(|_| panic!("Failed to read file {}", json_path)); + + // Deserialize the JSON into the `DebugJson` struct + let json: DebugJson = serde_json::from_str(&debug_json) + .unwrap_or_else(|err| panic!("Failed to parse JSON file: {}: {}", json_path, err)); + + // Initialize the airgroup map + let mut airgroup_map: AirGroupMap = HashMap::new(); + + // Populate the airgroup map using the deserialized data + if let Some(constraints) = json.constraints { + for airgroup in constraints { + let mut air_id_map: AirIdMap = HashMap::new(); + + if let Some(air_ids) = airgroup.air_ids { + for air_id in air_ids { + let mut instance_map: InstanceMap = HashMap::new(); + + if let Some(instances) = air_id.instances_ids { + for instance in instances { + let instance_constraints = instance.constraints.unwrap_or_default(); + instance_map.insert(instance.instance_id.unwrap_or_default(), instance_constraints); + } + } + + air_id_map.insert(air_id.air_id.unwrap_or_default(), instance_map); + } + } + + airgroup_map.insert(airgroup.airgroup_id.unwrap_or_default(), air_id_map); + } + } + + // Default global_constraints to an empty Vec if None + let global_constraints = json.global_constraints.unwrap_or_default(); + + let std_mode = if !airgroup_map.is_empty() { + StdMode::new(ModeName::Standard, Vec::new(), 0, false) + } else { + let mode = json.std_mode.unwrap_or_default(); + StdMode::new( + ModeName::Debug, + mode.opids.unwrap_or_default(), + mode.n_print.unwrap_or(DEFAULT_PRINT_VALS), + mode.print_to_file, + ) + }; + + DebugInfo { debug_instances: airgroup_map.clone(), debug_global_instances: global_constraints, std_mode } +} diff --git a/examples/fibonacci-square/Cargo.toml b/examples/fibonacci-square/Cargo.toml index e6ffb65b..a23033da 100644 --- a/examples/fibonacci-square/Cargo.toml +++ b/examples/fibonacci-square/Cargo.toml @@ -9,13 +9,13 @@ crate-type = ["dylib"] [dependencies] proofman-common = { path = "../../common" } proofman-macros.workspace = true -proofman = { path = "../../proofman" } -pil-std-lib = {path = "../../pil2-components/lib/std/rs" } - +witness.workspace = true +pil-std-lib.workspace = true env_logger.workspace = true log.workspace = true p3-goldilocks.workspace = true p3-field.workspace = true +rayon = "1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.68" diff --git a/examples/fibonacci-square/pil/fibonaccisq.pil b/examples/fibonacci-square/pil/fibonaccisq.pil index 2d342463..0fd75514 100644 --- a/examples/fibonacci-square/pil/fibonaccisq.pil +++ b/examples/fibonacci-square/pil/fibonaccisq.pil @@ -6,12 +6,12 @@ public in2; public out; public rom_root[4]; -proofval value1; -proofval value2; +proofval stage(1) value1; +proofval stage(1) value2; private function checkProofValues() { - mod * value1 - value2 === 0; + module * value1 - value2 === 0; std_alpha - std_alpha === 0; } @@ -36,7 +36,7 @@ airtemplate FibonacciSquare(const int N = 2**8) { (a' - b) * (1 - L1') === 0; - mod * value1 - value2 === 0; + module * value1 - value2 === 0; 2*fibo1[0] - fibo1[1] === 0; line - (flags + 1) === 0; diff --git a/examples/fibonacci-square/pil/module.pil b/examples/fibonacci-square/pil/module.pil index b4de0c2d..31ec3986 100644 --- a/examples/fibonacci-square/pil/module.pil +++ b/examples/fibonacci-square/pil/module.pil @@ -2,18 +2,18 @@ require "std_range_check.pil"; const int MODULE_ID = 1; -public mod; +public module; airtemplate Module(const int N = 2**8) { col fixed LN = [0...,1]; col witness x, q, x_mod; - x === q * mod + x_mod; + x === q * module + x_mod; - // Ensure that 0 <= x_mod < mod < PRIME <--> 0 < mod - x_mod <--> mod - x_mod <= mod - range_check(mod - x_mod, 1, 2**8-1); - // NOTE: The 2**8-1 is used for simplicity. Moreover, for the shake of security, mod should be limited to 2**8-1 by the verifier + // Ensure that 0 <= x_mod < module < PRIME <--> 0 < module - x_mod <--> module - x_mod <= module + range_check(module - x_mod, 1, 2**8-1); + // NOTE: The 2**8-1 is used for simplicity. Moreover, for the shake of security, module should be limited to 2**8-1 by the verifier permutation_proves(MODULE_ID, [x, x_mod], 1 - LN); } \ No newline at end of file diff --git a/examples/fibonacci-square/src/debug.json b/examples/fibonacci-square/src/debug.json new file mode 100644 index 00000000..446065a2 --- /dev/null +++ b/examples/fibonacci-square/src/debug.json @@ -0,0 +1,3 @@ +{ + "global_constraints": [0] +} \ No newline at end of file diff --git a/examples/fibonacci-square/src/fibonacci.rs b/examples/fibonacci-square/src/fibonacci.rs index 1331d2c5..0db093fa 100644 --- a/examples/fibonacci-square/src/fibonacci.rs +++ b/examples/fibonacci-square/src/fibonacci.rs @@ -1,57 +1,43 @@ use std::sync::Arc; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; -use proofman::{WitnessManager, WitnessComponent}; +use proofman_common::{add_air_instance, AirInstance, FromTrace, ProofCtx}; +use witness::WitnessComponent; -use p3_field::PrimeField; +use p3_field::PrimeField64; -use crate::{FibonacciSquareTrace, FibonacciSquareRomTrace, Module, FIBONACCI_SQUARE_AIRGROUP_ID, FIBONACCI_SQUARE_AIR_IDS}; +use crate::{ + FibonacciSquareRomTrace, BuildPublicValues, BuildProofValues, FibonacciSquareAirValues, FibonacciSquareTrace, + FibonacciSquareAirGroupValues, Module, +}; -pub struct FibonacciSquare { +pub struct FibonacciSquare { module: Arc>, } -impl FibonacciSquare { +impl FibonacciSquare { const MY_NAME: &'static str = "FiboSqre"; - pub fn new(wcm: Arc>, module: Arc>) -> Arc { - let fibonacci = Arc::new(Self { module }); - - wcm.register_component(fibonacci.clone(), Some(FIBONACCI_SQUARE_AIRGROUP_ID), Some(FIBONACCI_SQUARE_AIR_IDS)); - - fibonacci - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // TODO: We should create the instance here and fill the trace in calculate witness!!! - if let Err(e) = - Self::calculate_trace(self, FIBONACCI_SQUARE_AIRGROUP_ID, FIBONACCI_SQUARE_AIR_IDS[0], pctx, ectx, sctx) - { - panic!("Failed to calculate fibonacci: {:?}", e); - } + pub fn new(module: Arc>) -> Arc { + Arc::new(Self { module }) } +} - fn calculate_trace( - &self, - airgroup_id: usize, - air_id: usize, - pctx: Arc>, - ectx: Arc, - sctx: Arc, - ) -> Result> { +impl WitnessComponent for FibonacciSquare { + fn execute(&self, pctx: Arc>) { log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let module = pctx.get_public_value("mod"); - let mut a = pctx.get_public_value("in1"); - let mut b = pctx.get_public_value("in2"); + let mut publics = BuildPublicValues::from_vec_guard(pctx.get_publics()); + + let module = F::as_canonical_u64(&publics.module); + let mut a = F::as_canonical_u64(&publics.in1); + let mut b = F::as_canonical_u64(&publics.in2); - let num_rows = pctx.global_info.airs[airgroup_id][air_id].num_rows; - let mut trace = FibonacciSquareTrace::new(num_rows); + let mut trace = FibonacciSquareTrace::new_zeroes(); trace[0].a = F::from_canonical_u64(a); trace[0].b = F::from_canonical_u64(b); - for i in 1..num_rows { + for i in 1..trace.num_rows() { let tmp = b; let result = self.module.calculate_module(a.pow(2) + b.pow(2), module); (a, b) = (tmp, result); @@ -60,66 +46,42 @@ impl FibonacciSquare { trace[i].b = F::from_canonical_u64(b); } - let (buffer_size_rom, offsets_rom, commit_id) = ectx.buffer_allocator.as_ref().get_buffer_info_custom_commit( - &sctx, - FIBONACCI_SQUARE_AIRGROUP_ID, - FIBONACCI_SQUARE_AIR_IDS[0], - "rom", - )?; + publics.out = trace[trace.num_rows() - 1].b; - let mut buffer_rom = vec![F::zero(); buffer_size_rom as usize]; + let mut trace_rom = FibonacciSquareRomTrace::new_zeroes(); - let mut trace_custom_commits = - FibonacciSquareRomTrace::map_buffer(&mut buffer_rom, num_rows, offsets_rom[0] as usize)?; - for i in 0..num_rows { - trace_custom_commits[i].line = F::from_canonical_u64(3 + i as u64); - trace_custom_commits[i].flags = F::from_canonical_u64(2 + i as u64); + for i in 0..trace_rom.num_rows() { + trace_rom[i].line = F::from_canonical_u64(3 + i as u64); + trace_rom[i].flags = F::from_canonical_u64(2 + i as u64); } - pctx.set_public_value_by_name(b, "out", None); + let mut proof_values = BuildProofValues::from_vec_guard(pctx.get_proof_values()); + proof_values.value1 = F::from_canonical_u64(5); + proof_values.value2 = F::from_canonical_u64(125); - pctx.set_proof_value("value1", F::from_canonical_u64(5)); - pctx.set_proof_value("value2", F::from_canonical_u64(125)); + let mut air_values = FibonacciSquareAirValues::::new(); + air_values.fibo1[0] = F::from_canonical_u64(1); + air_values.fibo1[1] = F::from_canonical_u64(2); + air_values.fibo3 = [F::from_canonical_u64(5), F::from_canonical_u64(5), F::from_canonical_u64(5)]; - // Not needed, for debugging! - // let mut result = F::zero(); - // for (i, _) in buffer.iter().enumerate() { - // result += buffer[i] * F::from_canonical_u64(i as u64); - // } - // log::info!("Result Fibonacci buffer: {:?}", result); - - let mut air_instance = AirInstance::new( - sctx.clone(), - FIBONACCI_SQUARE_AIRGROUP_ID, - FIBONACCI_SQUARE_AIR_IDS[0], - Some(0), - trace.buffer.unwrap(), + let air_instance = AirInstance::new_from_trace( + FromTrace::new(&mut trace).with_custom_traces(vec![&mut trace_rom]).with_air_values(&mut air_values), ); - air_instance.set_airvalue("FibonacciSquare.fibo1", Some(vec![0]), F::from_canonical_u64(1)); - air_instance.set_airvalue("FibonacciSquare.fibo1", Some(vec![1]), F::from_canonical_u64(2)); - air_instance.set_airvalue_ext("FibonacciSquare.fibo3", None, vec![F::from_canonical_u64(5); 3]); - match ectx.cached_buffers_path.as_ref().and_then(|cached_buffers| cached_buffers.get("rom").cloned()) { - Some(buffer_path) => air_instance.set_custom_commit_cached_file(&sctx, commit_id, buffer_path), - None => air_instance.set_custom_commit_id_buffer(&sctx, buffer_rom, commit_id), - } - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(FIBONACCI_SQUARE_AIRGROUP_ID, FIBONACCI_SQUARE_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } - - Ok(b) + add_air_instance::(air_instance, pctx.clone()); } -} -impl WitnessComponent for FibonacciSquare { - fn calculate_witness( - &self, - _stage: u32, - _air_instance_id: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn debug(&self, pctx: Arc>) { + let trace = FibonacciSquareTrace::from_vec(pctx.get_air_instance_trace(0, 0, 0)); + let air_values = FibonacciSquareAirValues::from_vec(pctx.get_air_instance_air_values(0, 0, 0)); + let airgroup_values = FibonacciSquareAirGroupValues::from_vec(pctx.get_air_instance_airgroup_values(0, 0, 0)); + + let publics = BuildPublicValues::from_vec_guard(pctx.get_publics()); + let proof_values = BuildProofValues::from_vec_guard(pctx.get_proof_values()); + + log::info!("{} First row 1: {:?}", Self::MY_NAME, trace[1]); + log::info!("{} Air values: {:?}", Self::MY_NAME, air_values); + log::info!("{} Airgroup values: {:?}", Self::MY_NAME, airgroup_values); + log::info!("{} Publics: {:?}", Self::MY_NAME, publics); + log::info!("{} Proof values: {:?}", Self::MY_NAME, proof_values); } } diff --git a/examples/fibonacci-square/src/fibonacci_lib.rs b/examples/fibonacci-square/src/fibonacci_lib.rs index 3804edb6..d8cf0992 100644 --- a/examples/fibonacci-square/src/fibonacci_lib.rs +++ b/examples/fibonacci-square/src/fibonacci_lib.rs @@ -1,95 +1,29 @@ -use std::io::Read; -use std::{fs::File, sync::Arc}; - -use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; -use proofman::{WitnessLibrary, WitnessManager}; +use std::sync::Arc; +use proofman_common::load_from_json; +use witness::{witness_library, WitnessLibrary, WitnessManager}; use pil_std_lib::Std; -use p3_field::PrimeField; +use p3_field::PrimeField64; use p3_goldilocks::Goldilocks; -use std::error::Error; -use std::path::PathBuf; -use crate::FibonacciSquarePublics; - -use crate::{FibonacciSquare, Pilout, Module}; - -pub struct FibonacciWitness { - public_inputs_path: Option, - wcm: Option>>, - fibonacci: Option>>, - module: Option>>, - std_lib: Option>>, -} +use crate::{BuildPublics, BuildPublicValues, FibonacciSquare, Module}; -impl FibonacciWitness { - pub fn new(public_inputs_path: Option) -> Self { - Self { public_inputs_path, wcm: None, fibonacci: None, module: None, std_lib: None } - } -} - -impl WitnessLibrary for FibonacciWitness { - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let wcm = Arc::new(WitnessManager::new(pctx.clone(), ectx.clone(), sctx.clone())); +witness_library!(WitnessLib, Goldilocks); +impl WitnessLibrary for WitnessLib { + fn register_witness(&mut self, wcm: Arc>) { let std_lib = Std::new(wcm.clone()); - let module = Module::new(wcm.clone(), std_lib.clone()); - let fibonacci = FibonacciSquare::new(wcm.clone(), module.clone()); - - self.wcm = Some(wcm.clone()); - self.fibonacci = Some(fibonacci); - self.module = Some(module); - self.std_lib = Some(std_lib); - - let public_inputs: FibonacciSquarePublics = if let Some(path) = &self.public_inputs_path { - let mut file = File::open(path).unwrap(); - - if !file.metadata().unwrap().is_file() { - panic!("Public inputs file not found"); - } - - let mut contents = String::new(); + let module = Module::new(std_lib.clone()); + let fibonacci = FibonacciSquare::new(module.clone()); - let _ = - file.read_to_string(&mut contents).map_err(|err| format!("Failed to read public inputs file: {}", err)); + wcm.register_component(fibonacci.clone()); + wcm.register_component(module.clone()); - serde_json::from_str(&contents).unwrap() - } else { - FibonacciSquarePublics::default() - }; + let public_inputs: BuildPublics = load_from_json(&wcm.get_public_inputs_path()); - pctx.set_public_value_by_name(public_inputs.module, "mod", None); - pctx.set_public_value_by_name(public_inputs.a, "in1", None); - pctx.set_public_value_by_name(public_inputs.b, "in2", None); + let mut publics = BuildPublicValues::from_vec_guard(wcm.get_pctx().get_publics()); - wcm.start_proof(pctx, ectx, sctx); + publics.module = F::from_canonical_u64(public_inputs.module); + publics.in1 = F::from_canonical_u64(public_inputs.in1); + publics.in2 = F::from_canonical_u64(public_inputs.in2); } - - fn end_proof(&mut self) { - self.wcm.as_ref().unwrap().end_proof(); - } - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.fibonacci.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.module.as_ref().unwrap().execute(pctx, ectx, sctx); - } - - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); - } - - fn pilout(&self) -> WitnessPilout { - Pilout::pilout() - } -} - -#[no_mangle] -pub extern "Rust" fn init_library( - _: Option, - public_inputs_path: Option, - verbose_mode: VerboseMode, -) -> Result>, Box> { - initialize_logger(verbose_mode); - - let fibonacci_witness = FibonacciWitness::new(public_inputs_path); - Ok(Box::new(fibonacci_witness)) } diff --git a/examples/fibonacci-square/src/inputs.json b/examples/fibonacci-square/src/inputs.json index 02ed73de..e21c9f87 100644 --- a/examples/fibonacci-square/src/inputs.json +++ b/examples/fibonacci-square/src/inputs.json @@ -1,5 +1,5 @@ { - "a": 0, - "b": 2, + "in1": 0, + "in2": 2, "module": 25 } \ No newline at end of file diff --git a/examples/fibonacci-square/src/lib.rs b/examples/fibonacci-square/src/lib.rs index c13b75c2..0badce4a 100644 --- a/examples/fibonacci-square/src/lib.rs +++ b/examples/fibonacci-square/src/lib.rs @@ -2,10 +2,8 @@ mod pil_helpers; mod fibonacci_lib; mod fibonacci; mod module; -mod public_inputs; pub use pil_helpers::*; pub use fibonacci_lib::*; pub use fibonacci::*; pub use module::*; -pub use public_inputs::*; diff --git a/examples/fibonacci-square/src/module.rs b/examples/fibonacci-square/src/module.rs index 6b5e2e03..facc79cb 100644 --- a/examples/fibonacci-square/src/module.rs +++ b/examples/fibonacci-square/src/module.rs @@ -1,30 +1,23 @@ use std::sync::{Arc, Mutex}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; -use proofman::{WitnessManager, WitnessComponent}; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; +use witness::WitnessComponent; use pil_std_lib::Std; -use p3_field::{AbstractField, PrimeField}; +use p3_field::{AbstractField, PrimeField64}; use num_bigint::BigInt; -use crate::{ModuleTrace, FIBONACCI_SQUARE_AIRGROUP_ID, MODULE_AIR_IDS}; +use crate::{BuildPublicValues, ModuleTrace}; -pub struct Module { +pub struct Module { inputs: Mutex>, std_lib: Arc>, } -impl Module { +impl Module { const MY_NAME: &'static str = "ModuleSM"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let module = Arc::new(Module { inputs: Mutex::new(Vec::new()), std_lib }); - - wcm.register_component(module.clone(), Some(FIBONACCI_SQUARE_AIRGROUP_ID), Some(MODULE_AIR_IDS)); - - // Register dependency relations - module.std_lib.register_predecessor(); - - module + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Module { inputs: Mutex::new(Vec::new()), std_lib }) } pub fn calculate_module(&self, x: u64, module: u64) -> u64 { @@ -36,18 +29,16 @@ impl Module x_mod } +} - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.calculate_trace(pctx, ectx, sctx); - } - - fn calculate_trace(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { +impl WitnessComponent for Module { + fn execute(&self, pctx: Arc>) { log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let module = pctx.get_public_value("mod"); + let publics = BuildPublicValues::from_vec_guard(pctx.get_publics()); + let module = F::as_canonical_u64(&publics.module); - let num_rows = pctx.global_info.airs[FIBONACCI_SQUARE_AIRGROUP_ID][MODULE_AIR_IDS[0]].num_rows; - let mut trace = ModuleTrace::new(num_rows); + let mut trace = ModuleTrace::new_zeroes(); //range_check(colu: mod - x_mod, min: 1, max: 2**8-1); let range = self.std_lib.get_range(BigInt::from(1), BigInt::from((1 << 8) - 1), None); @@ -68,36 +59,11 @@ impl Module } // Trivial range check for the remaining rows - for _ in inputs.len()..num_rows { + for _ in inputs.len()..trace.num_rows() { self.std_lib.range_check(F::from_canonical_u64(module), F::one(), range); } - let air_instance = AirInstance::new( - sctx.clone(), - FIBONACCI_SQUARE_AIRGROUP_ID, - MODULE_AIR_IDS[0], - Some(0), - trace.buffer.unwrap(), - ); - - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(FIBONACCI_SQUARE_AIRGROUP_ID, MODULE_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } - - self.std_lib.unregister_predecessor(pctx, None); - } -} - -impl WitnessComponent for Module { - fn calculate_witness( - &self, - _stage: u32, - _air_instance_id: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/examples/fibonacci-square/src/pil_helpers/mod.rs b/examples/fibonacci-square/src/pil_helpers/mod.rs index 8273843a..8e9f77af 100644 --- a/examples/fibonacci-square/src/pil_helpers/mod.rs +++ b/examples/fibonacci-square/src/pil_helpers/mod.rs @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/examples/fibonacci-square/src/pil_helpers/pilout.rs b/examples/fibonacci-square/src/pil_helpers/pilout.rs deleted file mode 100644 index 9ed22685..00000000 --- a/examples/fibonacci-square/src/pil_helpers/pilout.rs +++ /dev/null @@ -1,33 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"Build-hash"; - -//AIRGROUP CONSTANTS - -pub const FIBONACCI_SQUARE_AIRGROUP_ID: usize = 0; - -//AIR CONSTANTS - -pub const FIBONACCI_SQUARE_AIR_IDS: &[usize] = &[0]; - -pub const MODULE_AIR_IDS: &[usize] = &[1]; - -pub const U_8_AIR_AIR_IDS: &[usize] = &[2]; - -pub struct Pilout; - -impl Pilout { - pub fn pilout() -> WitnessPilout { - let mut pilout = WitnessPilout::new("Build", 2, PILOUT_HASH.to_vec()); - - let air_group = pilout.add_air_group(Some("FibonacciSquare")); - - air_group.add_air(Some("FibonacciSquare"), 1024); - air_group.add_air(Some("Module"), 1024); - air_group.add_air(Some("U8Air"), 256); - - pilout - } -} diff --git a/examples/fibonacci-square/src/pil_helpers/traces.rs b/examples/fibonacci-square/src/pil_helpers/traces.rs index 920d9f9f..56b69df7 100644 --- a/examples/fibonacci-square/src/pil_helpers/traces.rs +++ b/examples/fibonacci-square/src/pil_helpers/traces.rs @@ -2,19 +2,82 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; +pub use proofman_macros::values; -trace!(FibonacciSquareRow, FibonacciSquareTrace { - a: F, b: F, +use std::fmt; + +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"Build-hash"; + +//AIRGROUP CONSTANTS + +pub const FIBONACCI_SQUARE_AIRGROUP_ID: usize = 0; + +//AIR CONSTANTS + +pub const FIBONACCI_SQUARE_AIR_IDS: &[usize] = &[0]; + +pub const MODULE_AIR_IDS: &[usize] = &[1]; + +pub const U_8_AIR_AIR_IDS: &[usize] = &[2]; + + +//PUBLICS +use serde::Deserialize; +use serde::Serialize; +#[derive(Default, Debug, Serialize, Deserialize)] +pub struct BuildPublics { + #[serde(default)] + pub module: u64, + #[serde(default)] + pub in1: u64, + #[serde(default)] + pub in2: u64, + #[serde(default)] + pub out: u64, + #[serde(default)] + pub rom_root: [u64; 4], + +} + +values!(BuildPublicValues { + module: F, in1: F, in2: F, out: F, rom_root: [F; 4], }); + +values!(BuildProofValues { + value1: F, value2: F, +}); + +trace!(FibonacciSquareTrace { + a: F, b: F, +}, 0, 0, 1024 ); -trace!(ModuleRow, ModuleTrace { +trace!(ModuleTrace { x: F, q: F, x_mod: F, -}); +}, 0, 1, 1024 ); -trace!(U8AirRow, U8AirTrace { +trace!(U8AirTrace { mul: F, -}); +}, 0, 2, 256 ); -trace!(FibonacciSquareRomRow, FibonacciSquareRomTrace { +trace!(FibonacciSquareRomTrace { line: F, flags: F, +}, 0, 0, 1024, 0 ); + +values!(FibonacciSquareAirValues { + fibo1: [F; 2], fibo3: FieldExtension, +}); + +values!(FibonacciSquareAirGroupValues { + gsum_result: FieldExtension, +}); + +values!(ModuleAirGroupValues { + gsum_result: FieldExtension, +}); + +values!(U8AirAirGroupValues { + gsum_result: FieldExtension, }); diff --git a/examples/fibonacci-square/src/public_inputs.rs b/examples/fibonacci-square/src/public_inputs.rs deleted file mode 100644 index 60d7b5c4..00000000 --- a/examples/fibonacci-square/src/public_inputs.rs +++ /dev/null @@ -1,8 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Default, Debug, Serialize, Deserialize)] -pub struct FibonacciSquarePublics { - pub module: u64, - pub a: u64, - pub b: u64, -} diff --git a/examples/fibonacci-square/src/publics_info.json b/examples/fibonacci-square/src/publics_info.json index 381d3491..4534ac9a 100644 --- a/examples/fibonacci-square/src/publics_info.json +++ b/examples/fibonacci-square/src/publics_info.json @@ -1,7 +1,7 @@ { "nPublics": 8, "definitions": [ - { "name": "mod", "initialPos": 0, "chunks": [1, 64] }, + { "name": "module", "initialPos": 0, "chunks": [1, 64] }, { "name": "in1", "initialPos": 1, "chunks": [1, 64] }, { "name": "in2", "initialPos": 2, "chunks": [1, 64] }, { "name": "out", "initialPos": 3, "chunks": [1, 64] }, diff --git a/hints/Cargo.toml b/hints/Cargo.toml index cef188a7..8da7a9ea 100644 --- a/hints/Cargo.toml +++ b/hints/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" [dependencies] proofman-starks-lib-c = { path = "../provers/starks-lib-c" } proofman-common = { path = "../common" } +proofman-util = { path = "../util" } p3-field.workspace = true +log.workspace = true \ No newline at end of file diff --git a/hints/src/global_hints.rs b/hints/src/global_hints.rs new file mode 100644 index 00000000..e1522475 --- /dev/null +++ b/hints/src/global_hints.rs @@ -0,0 +1,291 @@ +use p3_field::Field; +use crate::{HintCol, HintFieldInfoC, HintFieldInfo, HintFieldOutput, HintFieldValue, HintFieldValues, HintFieldValuesVec}; +use proofman_starks_lib_c::{ + get_hint_field_global_constraints_values_c, get_hint_field_global_constraints_sizes_c, + get_hint_field_global_constraints_c, set_hint_field_global_constraints_c, +}; +use std::ffi::c_void; + +use std::{collections::HashMap, sync::Arc}; + +use proofman_common::{ExtensionField, ProofCtx, SetupCtx}; + +pub fn aggregate_airgroupvals(pctx: Arc>) -> Vec> { + const FIELD_EXTENSION: usize = 3; + + let mut airgroupvalues: Vec> = Vec::new(); + for agg_types in pctx.global_info.agg_types.iter() { + let mut values = vec![F::zero(); agg_types.len() * FIELD_EXTENSION]; + for (idx, agg_type) in agg_types.iter().enumerate() { + if agg_type.agg_type == 1 { + values[idx * FIELD_EXTENSION] = F::one(); + } + } + airgroupvalues.push(values); + } + + for air_instance in pctx.air_instance_repo.air_instances.write().unwrap().iter() { + for (idx, agg_type) in pctx.global_info.agg_types[air_instance.airgroup_id].iter().enumerate() { + let mut acc = ExtensionField { + value: [ + airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION], + airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 1], + airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 2], + ], + }; + if !air_instance.airgroup_values.is_empty() { + let instance_airgroup_val = ExtensionField { + value: [ + air_instance.airgroup_values[idx * FIELD_EXTENSION], + air_instance.airgroup_values[idx * FIELD_EXTENSION + 1], + air_instance.airgroup_values[idx * FIELD_EXTENSION + 2], + ], + }; + if agg_type.agg_type == 0 { + acc += instance_airgroup_val; + } else { + acc *= instance_airgroup_val; + } + airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION] = acc.value[0]; + airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 1] = acc.value[1]; + airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 2] = acc.value[2]; + } + } + } + + airgroupvalues +} + +fn get_global_hint_f( + pctx: Option>>, + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> Vec> { + let n_hints_values = get_hint_field_global_constraints_values_c(sctx.get_global_bin(), hint_id, hint_field_name); + + let mut hint_field_values: Vec> = vec![HintFieldInfo::default(); n_hints_values as usize]; + + let mut hint_field_values_c = HintFieldInfoC::::from_hint_field_info_vec(&mut hint_field_values); + let mut hint_field_values_c_ptr = hint_field_values_c.as_mut_ptr() as *mut c_void; + + get_hint_field_global_constraints_sizes_c( + sctx.get_global_info_file().as_str(), + sctx.get_global_bin(), + hint_field_values_c_ptr, + hint_id, + hint_field_name, + print_expression, + ); + + HintFieldInfoC::::sync_to_hint_field_info(&mut hint_field_values, &hint_field_values_c); + + for hint_field_value in hint_field_values.iter_mut() { + hint_field_value.init_buffers(true); + } + + hint_field_values_c = HintFieldInfoC::::from_hint_field_info_vec(&mut hint_field_values); + hint_field_values_c_ptr = hint_field_values_c.as_mut_ptr() as *mut c_void; + + let publics = if let Some(ref pctx) = pctx { pctx.get_publics_ptr() } else { std::ptr::null_mut() }; + let challenges = if let Some(ref pctx) = pctx { pctx.get_challenges_ptr() } else { std::ptr::null_mut() }; + let proof_values = if let Some(ref pctx) = pctx { pctx.get_proof_values_ptr() } else { std::ptr::null_mut() }; + let airgroup_values = if let Some(ref pctx) = pctx { + let mut airgroupvals = aggregate_airgroupvals(pctx.clone()); + let mut airgroup_values_ptrs: Vec<*mut F> = airgroupvals + .iter_mut() // Iterate mutably over the inner Vecs + .map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec + .collect(); + airgroup_values_ptrs.as_mut_ptr() as *mut *mut u8 + } else { + std::ptr::null_mut() + }; + + get_hint_field_global_constraints_c( + sctx.get_global_info_file().as_str(), + sctx.get_global_bin(), + hint_field_values_c_ptr, + publics, + challenges, + proof_values, + airgroup_values, + hint_id, + hint_field_name, + print_expression, + ); + + hint_field_values +} +pub fn get_hint_field_constant_gc( + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> HintFieldValue { + let hint_info = get_global_hint_f(None, sctx, hint_id, hint_field_name, print_expression); + + if hint_info[0].matrix_size != 0 { + panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); + } + + if print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info[0].expression_line).unwrap()); + } + + HintCol::from_hint_field(&hint_info[0]) +} + +pub fn get_hint_field_gc_constant_a( + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> HintFieldValuesVec { + let hint_infos: Vec> = get_global_hint_f(None, sctx, hint_id, hint_field_name, print_expression); + + let mut hint_field_values = Vec::new(); + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size != 1 { + panic!("get_hint_field_m can only be called with an array of expressions!"); + } + if print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + let hint_value = HintCol::from_hint_field(hint_info); + hint_field_values.push(hint_value); + } + + HintFieldValuesVec { values: hint_field_values } +} + +pub fn get_hint_field_constant_gc_m( + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> HintFieldValues { + let hint_infos = get_global_hint_f(None, sctx, hint_id, hint_field_name, print_expression); + + let mut hint_field_values = HashMap::with_capacity(hint_infos.len() as usize); + + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size > 2 { + panic!("get_hint_field_m can only be called with a matrix of expressions!",); + } + let hint_value = HintCol::from_hint_field(hint_info); + let mut pos = Vec::new(); + for p in 0..hint_info.matrix_size { + pos.push(hint_info.pos[p as usize]); + } + if print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + hint_field_values.insert(pos, hint_value); + } + + HintFieldValues { values: hint_field_values } +} + +pub fn get_hint_field_gc( + pctx: Arc>, + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> HintFieldValue { + let hint_info = get_global_hint_f(Some(pctx), sctx, hint_id, hint_field_name, print_expression); + + if hint_info[0].matrix_size != 0 { + panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); + } + + if print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info[0].expression_line).unwrap()); + } + + HintCol::from_hint_field(&hint_info[0]) +} + +pub fn get_hint_field_gc_a( + pctx: Arc>, + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> HintFieldValuesVec { + let hint_infos: Vec> = + get_global_hint_f(Some(pctx), sctx, hint_id, hint_field_name, print_expression); + + let mut hint_field_values = Vec::new(); + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size != 1 { + panic!("get_hint_field_m can only be called with an array of expressions!"); + } + if print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + let hint_value = HintCol::from_hint_field(hint_info); + hint_field_values.push(hint_value); + } + HintFieldValuesVec { values: hint_field_values } +} + +pub fn get_hint_field_gc_m( + pctx: Arc>, + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, +) -> HintFieldValues { + let hint_infos = get_global_hint_f(Some(pctx), sctx, hint_id, hint_field_name, print_expression); + + let mut hint_field_values = HashMap::with_capacity(hint_infos.len() as usize); + + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size > 2 { + panic!("get_hint_field_m can only be called with a matrix of expressions!",); + } + let hint_value = HintCol::from_hint_field(hint_info); + let mut pos = Vec::new(); + for p in 0..hint_info.matrix_size { + pos.push(hint_info.pos[p as usize]); + } + if print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + hint_field_values.insert(pos, hint_value); + } + + HintFieldValues { values: hint_field_values } +} + +pub fn set_hint_field_gc( + pctx: Arc>, + sctx: Arc, + hint_id: u64, + hint_field_name: &str, + value: HintFieldOutput, +) { + let mut value_array: Vec = Vec::new(); + + match value { + HintFieldOutput::Field(val) => { + value_array.push(val); + } + HintFieldOutput::FieldExtended(val) => { + value_array.push(val.value[0]); + value_array.push(val.value[1]); + value_array.push(val.value[2]); + } + }; + + set_hint_field_global_constraints_c( + sctx.get_global_info_file().as_str(), + sctx.get_global_bin(), + pctx.get_proof_values_ptr(), + value_array.as_ptr() as *mut u8, + hint_id, + hint_field_name, + ); +} diff --git a/hints/src/hints.rs b/hints/src/hints.rs index c2467161..d2c8e9e4 100644 --- a/hints/src/hints.rs +++ b/hints/src/hints.rs @@ -1,14 +1,15 @@ use proofman_starks_lib_c::{ - acc_hint_field_c, acc_mul_hint_fields_c, get_hint_field_c, get_hint_ids_by_name_c, mul_hint_fields_c, - print_expression_c, print_row_c, set_hint_field_c, update_airgroupvalue_c, VecU64Result, + get_hint_field_id_c, acc_hint_field_c, acc_mul_hint_fields_c, get_hint_field_c, get_hint_field_sizes_c, + get_hint_field_values_c, get_hint_ids_by_name_c, mul_hint_fields_c, print_row_c, set_hint_field_c, + update_airgroupvalue_c, n_hint_ids_by_name_c, }; use std::collections::HashMap; +use std::ffi::c_void; use p3_field::Field; use proofman_common::{AirInstance, ExtensionField, ProofCtx, SetupCtx, StepsParams}; - -use std::os::raw::c_void; +use proofman_util::create_buffer_fast; use std::ops::{Add, Div, Mul, Sub, AddAssign, DivAssign, MulAssign, SubAssign}; @@ -25,15 +26,108 @@ pub enum HintFieldType { } #[repr(C)] -#[allow(dead_code)] -pub struct HintFieldInfo { +#[derive(Debug)] +pub struct HintFieldInfoC { size: u64, + string_size: u64, offset: u8, // 1 or 3cd field_type: HintFieldType, values: *mut F, string_value: *mut u8, pub matrix_size: u64, pub pos: *mut u64, + pub expression_line: *mut u8, + expression_line_size: u64, +} + +impl HintFieldInfoC { + pub fn from_hint_field_info_vec(hint_field_values: &mut [HintFieldInfo]) -> Vec> { + hint_field_values + .iter_mut() + .map(|info| HintFieldInfoC { + size: info.size, + string_size: info.string_size, + offset: info.offset, + field_type: info.field_type, + values: info.values.as_mut_ptr(), + string_value: info.string_value.as_mut_ptr(), + matrix_size: info.matrix_size, + pos: info.pos.as_mut_ptr(), + expression_line: info.expression_line.as_mut_ptr(), + expression_line_size: info.expression_line_size, + }) + .collect() + } + + pub fn sync_to_hint_field_info( + hint_field_values: &mut [HintFieldInfo], + hint_field_values_c: &Vec>, + ) { + for (original, updated) in hint_field_values.iter_mut().zip(hint_field_values_c) { + original.size = updated.size; + original.string_size = updated.string_size; + original.matrix_size = updated.matrix_size; + original.expression_line_size = updated.expression_line_size; + original.offset = updated.offset; + original.field_type = updated.field_type; + } + } +} + +#[derive(Clone, Debug)] +#[repr(C)] +pub struct HintFieldInfo { + size: u64, + string_size: u64, + offset: u8, // 1 or 3cd + field_type: HintFieldType, + values: Vec, + string_value: Vec, + pub matrix_size: u64, + pub pos: Vec, + pub expression_line: Vec, + expression_line_size: u64, +} + +impl Default for HintFieldInfo { + fn default() -> Self { + HintFieldInfo { + size: 0, + string_size: 0, + offset: 0, + field_type: HintFieldType::Field, + values: Vec::new(), + string_value: Vec::new(), + matrix_size: 0, + pos: Vec::new(), + expression_line: Vec::new(), + expression_line_size: 0, + } + } +} + +impl HintFieldInfo { + pub fn init_buffers(&mut self, initialize_zeros: bool) { + if self.size > 0 { + if initialize_zeros { + self.values = vec![F::zero(); self.size as usize]; + } else { + self.values = create_buffer_fast(self.size as usize); + } + } + + if self.matrix_size > 0 { + self.pos = vec![0; self.matrix_size as usize]; + } + + if self.string_size > 0 { + self.string_value = vec![0; self.string_size as usize]; + } + + if self.expression_line_size > 0 { + self.expression_line = vec![0; self.expression_line_size as usize] + } + } } #[repr(C)] @@ -43,7 +137,7 @@ pub struct HintFieldInfoValues { } #[repr(C)] -#[derive(Default)] +#[derive(Clone, Default)] pub struct HintFieldOptions { pub dest: bool, pub inverse: bool, @@ -52,9 +146,9 @@ pub struct HintFieldOptions { pub compilation_time: bool, } -impl From<&HintFieldOptions> for *mut c_void { - fn from(options: &HintFieldOptions) -> *mut c_void { - options as *const HintFieldOptions as *mut c_void +impl From<&HintFieldOptions> for *mut u8 { + fn from(options: &HintFieldOptions) -> *mut u8 { + options as *const HintFieldOptions as *mut u8 } } @@ -622,48 +716,36 @@ pub struct HintCol; impl HintCol { pub fn from_hint_field(hint_field: &HintFieldInfo) -> HintFieldValue { - let values_slice = match hint_field.field_type { - HintFieldType::String => &[], - _ => unsafe { std::slice::from_raw_parts(hint_field.values, hint_field.size as usize) }, - }; - match hint_field.field_type { - HintFieldType::Field => HintFieldValue::Field(values_slice[0]), + HintFieldType::Field => HintFieldValue::Field(hint_field.values[0]), HintFieldType::FieldExtended => { - let array = [values_slice[0], values_slice[1], values_slice[2]]; + let array = [hint_field.values[0], hint_field.values[1], hint_field.values[2]]; HintFieldValue::FieldExtended(ExtensionField { value: array }) } - HintFieldType::Column => HintFieldValue::Column(values_slice.to_vec()), + HintFieldType::Column => HintFieldValue::Column(hint_field.values.to_vec()), HintFieldType::ColumnExtended => { let mut extended_vec: Vec> = Vec::with_capacity(hint_field.size as usize / 3); - for chunk in values_slice.chunks(3) { + for chunk in hint_field.values.chunks(3) { extended_vec.push(ExtensionField { value: [chunk[0], chunk[1], chunk[2]] }); } HintFieldValue::ColumnExtended(extended_vec) } - HintFieldType::String => { - let str_slice = - unsafe { std::slice::from_raw_parts(hint_field.string_value, hint_field.size as usize) }; - - match std::str::from_utf8(str_slice) { - Ok(value) => HintFieldValue::String(value.to_string()), - Err(_) => HintFieldValue::String(String::new()), - } - } + HintFieldType::String => match std::str::from_utf8(&hint_field.string_value) { + Ok(value) => HintFieldValue::String(value.to_string()), + Err(_) => HintFieldValue::String(String::new()), + }, } } } -pub fn get_hint_ids_by_name(p_expressions_bin: *mut c_void, name: &str) -> Vec { - let raw_ptr = get_hint_ids_by_name_c(p_expressions_bin, name); +pub fn get_hint_ids_by_name(p_expressions_bin: *mut std::os::raw::c_void, name: &str) -> Vec { + let n_hints = n_hint_ids_by_name_c(p_expressions_bin, name); - let hint_ids_result = unsafe { Box::from_raw(raw_ptr as *mut VecU64Result) }; + let mut hint_ids = vec![0; n_hints as usize]; - let slice = unsafe { std::slice::from_raw_parts(hint_ids_result.values, hint_ids_result.n_values as usize) }; + get_hint_ids_by_name_c(p_expressions_bin, hint_ids.as_mut_ptr(), name); - // Copy the contents of the slice into a Vec - - slice.to_vec() + hint_ids } #[allow(clippy::too_many_arguments)] @@ -680,26 +762,20 @@ pub fn mul_hint_fields( ) -> u64 { let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; mul_hint_fields_c( @@ -727,29 +803,23 @@ pub fn acc_hint_field( ) -> (u64, u64) { let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; - let raw_ptr = acc_hint_field_c( + acc_hint_field_c( (&setup.p_setup).into(), (&steps_params).into(), hint_id as u64, @@ -759,11 +829,10 @@ pub fn acc_hint_field( add, ); - let hint_ids_result = unsafe { Box::from_raw(raw_ptr as *mut VecU64Result) }; - - let slice = unsafe { std::slice::from_raw_parts(hint_ids_result.values, hint_ids_result.n_values as usize) }; + let dest_id = get_hint_field_id_c((&setup.p_setup).into(), hint_id as u64, hint_field_dest); + let airgroup_value_id = get_hint_field_id_c((&setup.p_setup).into(), hint_id as u64, hint_field_airgroupvalue); - (slice[0], slice[1]) + (dest_id, airgroup_value_id) } #[allow(clippy::too_many_arguments)] @@ -782,29 +851,23 @@ pub fn acc_mul_hint_fields( ) -> (u64, u64) { let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; - let raw_ptr = acc_mul_hint_fields_c( + acc_mul_hint_fields_c( (&setup.p_setup).into(), (&steps_params).into(), hint_id as u64, @@ -817,11 +880,10 @@ pub fn acc_mul_hint_fields( add, ); - let hint_ids_result = unsafe { Box::from_raw(raw_ptr as *mut VecU64Result) }; + let dest_id = get_hint_field_id_c((&setup.p_setup).into(), hint_id as u64, hint_field_dest); + let airgroup_value_id = get_hint_field_id_c((&setup.p_setup).into(), hint_id as u64, hint_field_airgroupvalue); - let slice = unsafe { std::slice::from_raw_parts(hint_ids_result.values, hint_ids_result.n_values as usize) }; - - (slice[0], slice[1]) + (dest_id, airgroup_value_id) } #[allow(clippy::too_many_arguments)] @@ -839,29 +901,23 @@ pub fn update_airgroupvalue( ) -> u64 { let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; - let raw_ptr = update_airgroupvalue_c( + update_airgroupvalue_c( (&setup.p_setup).into(), (&steps_params).into(), hint_id as u64, @@ -871,119 +927,139 @@ pub fn update_airgroupvalue( (&options1).into(), (&options2).into(), add, - ); - - let hint_ids_result = unsafe { Box::from_raw(raw_ptr as *mut VecU64Result) }; - - let slice = unsafe { std::slice::from_raw_parts(hint_ids_result.values, hint_ids_result.n_values as usize) }; - - slice[0] + ) } -pub fn get_hint_field( +#[allow(clippy::too_many_arguments)] +fn get_hint_f( setup_ctx: &SetupCtx, - proof_ctx: &ProofCtx, - air_instance: &mut AirInstance, + proof_ctx: Option<&ProofCtx>, + airgroup_id: usize, + air_id: usize, + air_instance: Option<&mut AirInstance>, hint_id: usize, hint_field_name: &str, options: HintFieldOptions, -) -> HintFieldValue { - let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); +) -> Vec> { + let setup = setup_ctx.get_setup(airgroup_id, air_id); + + let steps_params = if let Some(air_instance) = air_instance { + StepsParams { + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.unwrap().get_publics_ptr(), + proof_values: proof_ctx.unwrap().get_proof_values_ptr(), + challenges: proof_ctx.unwrap().get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), + xdivxsub: std::ptr::null_mut(), + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), + custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), + } + } else { + StepsParams::default() + }; - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; + let n_hints_values = get_hint_field_values_c((&setup.p_setup).into(), hint_id as u64, hint_field_name); - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; + let mut hint_field_values: Vec> = vec![HintFieldInfo::default(); n_hints_values as usize]; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, - xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, - custom_commits: air_instance.get_custom_commits_ptr(), - }; + let mut hint_field_values_c = HintFieldInfoC::::from_hint_field_info_vec(&mut hint_field_values); + let mut hint_field_values_c_ptr = hint_field_values_c.as_mut_ptr() as *mut c_void; - let raw_ptr = get_hint_field_c( + get_hint_field_sizes_c( (&setup.p_setup).into(), - (&steps_params).into(), + hint_field_values_c_ptr, hint_id as u64, hint_field_name, (&options).into(), ); - unsafe { - let hint_field_values = &*(raw_ptr as *mut HintFieldInfoValues); - let value = &*(hint_field_values.hint_field_values.add(0)); - if value.matrix_size != 0 { - panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); - } - HintCol::from_hint_field(value) + HintFieldInfoC::::sync_to_hint_field_info(&mut hint_field_values, &hint_field_values_c); + + for hint_field_value in hint_field_values.iter_mut() { + hint_field_value.init_buffers(options.initialize_zeros); } -} -pub fn get_hint_field_a( + hint_field_values_c = HintFieldInfoC::::from_hint_field_info_vec(&mut hint_field_values); + hint_field_values_c_ptr = hint_field_values_c.as_mut_ptr() as *mut c_void; + + get_hint_field_c( + (&setup.p_setup).into(), + (&steps_params).into(), + hint_field_values_c_ptr, + hint_id as u64, + hint_field_name, + (&options).into(), + ); + + hint_field_values +} +pub fn get_hint_field( setup_ctx: &SetupCtx, proof_ctx: &ProofCtx, air_instance: &mut AirInstance, hint_id: usize, hint_field_name: &str, options: HintFieldOptions, -) -> HintFieldValuesVec { - let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); +) -> HintFieldValue { + let hint_info = get_hint_f( + setup_ctx, + Some(proof_ctx), + air_instance.airgroup_id, + air_instance.air_id, + Some(air_instance), + hint_id, + hint_field_name, + options.clone(), + ); - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; + if hint_info[0].matrix_size != 0 { + panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); + } - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; + if options.print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info[0].expression_line).unwrap()); + } - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, - xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, - custom_commits: air_instance.get_custom_commits_ptr(), - }; + HintCol::from_hint_field(&hint_info[0]) +} - let raw_ptr = get_hint_field_c( - (&setup.p_setup).into(), - (&steps_params).into(), - hint_id as u64, +pub fn get_hint_field_a( + setup_ctx: &SetupCtx, + proof_ctx: &ProofCtx, + air_instance: &mut AirInstance, + hint_id: usize, + hint_field_name: &str, + options: HintFieldOptions, +) -> HintFieldValuesVec { + let hint_infos: Vec> = get_hint_f( + setup_ctx, + Some(proof_ctx), + air_instance.airgroup_id, + air_instance.air_id, + Some(air_instance), + hint_id, hint_field_name, - (&options).into(), + options.clone(), ); - unsafe { - let mut hint_field_values = Vec::new(); - let hint_field = &*(raw_ptr as *mut HintFieldInfoValues); - for v in 0..hint_field.n_values { - let h = &*(hint_field.hint_field_values.add(v as usize)); - if v == 0 && h.matrix_size != 1 { - panic!("get_hint_field_m can only be called with an array of expressions!"); - } - let hint_value = HintCol::from_hint_field(h); - hint_field_values.push(hint_value); + let mut hint_field_values = Vec::new(); + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size != 1 { + panic!("get_hint_field_m can only be called with an array of expressions!"); } - - HintFieldValuesVec { values: hint_field_values } + if options.print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + let hint_value = HintCol::from_hint_field(hint_info); + hint_field_values.push(hint_value); } + + HintFieldValuesVec { values: hint_field_values } } pub fn get_hint_field_m( @@ -994,57 +1070,35 @@ pub fn get_hint_field_m( hint_field_name: &str, options: HintFieldOptions, ) -> HintFieldValues { - let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - - let public_inputs_ptr = (*proof_ctx.public_inputs.inputs.read().unwrap()).as_ptr() as *mut c_void; - let proof_values_ptr = (*proof_ctx.proof_values.values.read().unwrap()).as_ptr() as *mut c_void; - let challenges_ptr = (*proof_ctx.challenges.challenges.read().unwrap()).as_ptr() as *mut c_void; - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: public_inputs_ptr, - proof_values: proof_values_ptr, - challenges: challenges_ptr, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, - xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, - custom_commits: air_instance.get_custom_commits_ptr(), - }; - - let raw_ptr = get_hint_field_c( - (&setup.p_setup).into(), - (&steps_params).into(), - hint_id as u64, + let hint_infos: Vec> = get_hint_f( + setup_ctx, + Some(proof_ctx), + air_instance.airgroup_id, + air_instance.air_id, + Some(air_instance), + hint_id, hint_field_name, - (&options).into(), + options.clone(), ); - unsafe { - let hint_field = &*(raw_ptr as *mut HintFieldInfoValues); - let mut hint_field_values = HashMap::with_capacity(hint_field.n_values as usize); + let mut hint_field_values = HashMap::with_capacity(hint_infos.len() as usize); - for v in 0..hint_field.n_values { - let h = &*(hint_field.hint_field_values.add(v as usize)); - if v == 0 && h.matrix_size > 2 { - panic!("get_hint_field_m can only be called with a matrix of expressions!",); - } - let hint_value = HintCol::from_hint_field(h); - let mut pos = Vec::new(); - for p in 0..h.matrix_size { - pos.push(h.pos.wrapping_add(p as usize) as u64); - } - hint_field_values.insert(pos, hint_value); + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size > 2 { + panic!("get_hint_field_m can only be called with a matrix of expressions!",); } - - HintFieldValues { values: hint_field_values } + let hint_value = HintCol::from_hint_field(hint_info); + let mut pos = Vec::new(); + for p in 0..hint_info.matrix_size { + pos.push(hint_info.pos[p as usize]); + } + if options.print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + hint_field_values.insert(pos, hint_value); } + + HintFieldValues { values: hint_field_values } } pub fn get_hint_field_constant( @@ -1057,26 +1111,18 @@ pub fn get_hint_field_constant( ) -> HintFieldValue { options.compilation_time = true; - let setup = setup_ctx.get_setup(airgroup_id, air_id); - - let steps_params = StepsParams::default(); + let hint_info: Vec> = + get_hint_f(setup_ctx, None, airgroup_id, air_id, None, hint_id, hint_field_name, options.clone()); - let raw_ptr = get_hint_field_c( - (&setup.p_setup).into(), - (&steps_params).into(), - hint_id as u64, - hint_field_name, - (&options).into(), - ); + if hint_info[0].matrix_size != 0 { + panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); + } - unsafe { - let hint_field_values = &*(raw_ptr as *mut HintFieldInfoValues); - let value = &*(hint_field_values.hint_field_values.add(0)); - if value.matrix_size != 0 { - panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); - } - HintCol::from_hint_field(value) + if options.print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info[0].expression_line).unwrap()); } + + HintCol::from_hint_field(&hint_info[0]) } pub fn get_hint_field_constant_a( @@ -1086,35 +1132,25 @@ pub fn get_hint_field_constant_a( hint_id: usize, hint_field_name: &str, mut options: HintFieldOptions, -) -> Vec> { +) -> HintFieldValuesVec { options.compilation_time = true; - let setup = setup_ctx.get_setup(airgroup_id, air_id); - - let steps_params = StepsParams::default(); + let hint_infos: Vec> = + get_hint_f(setup_ctx, None, airgroup_id, air_id, None, hint_id, hint_field_name, options.clone()); - let raw_ptr = get_hint_field_c( - (&setup.p_setup).into(), - (&steps_params).into(), - hint_id as u64, - hint_field_name, - (&options).into(), - ); - - unsafe { - let mut hint_field_values = Vec::new(); - let hint_field = &*(raw_ptr as *mut HintFieldInfoValues); - for v in 0..hint_field.n_values { - let h = &*(hint_field.hint_field_values.add(v as usize)); - if v == 0 && h.matrix_size != 1 { - panic!("get_hint_field_m can only be called with an array of expressions!"); - } - let hint_value = HintCol::from_hint_field(h); - hint_field_values.push(hint_value); + let mut hint_field_values = Vec::new(); + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size != 1 { + panic!("get_hint_field_m can only be called with an array of expressions!"); } - - hint_field_values + if options.print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + let hint_value = HintCol::from_hint_field(hint_info); + hint_field_values.push(hint_value); } + + HintFieldValuesVec { values: hint_field_values } } pub fn get_hint_field_constant_m( @@ -1127,40 +1163,27 @@ pub fn get_hint_field_constant_m( ) -> HintFieldValues { options.compilation_time = true; - let setup = setup_ctx.get_setup(airgroup_id, air_id); - - let steps_params = StepsParams::default(); + let hint_infos: Vec> = + get_hint_f(setup_ctx, None, airgroup_id, air_id, None, hint_id, hint_field_name, options.clone()); - let raw_ptr = get_hint_field_c( - (&setup.p_setup).into(), - (&steps_params).into(), - hint_id as u64, - hint_field_name, - (&options).into(), - ); + let mut hint_field_values = HashMap::with_capacity(hint_infos.len() as usize); - unsafe { - let hint_field = &*(raw_ptr as *mut HintFieldInfoValues); - let mut hint_field_values = HashMap::with_capacity(hint_field.n_values as usize); - - for v in 0..hint_field.n_values { - let h = &*(hint_field.hint_field_values.add(v as usize)); - if v == 0 && h.matrix_size != 0 { - panic!( - "get_hint_field_m can only be called with arrays of expressions, but {} is a single one", - hint_field_name - ); - } - let hint_value = HintCol::from_hint_field(h); - let mut pos = Vec::new(); - for p in 0..h.matrix_size { - pos.push(h.pos.wrapping_add(p as usize) as u64); - } - hint_field_values.insert(pos, hint_value); + for (v, hint_info) in hint_infos.iter().enumerate() { + if v == 0 && hint_info.matrix_size > 2 { + panic!("get_hint_field_m can only be called with a matrix of expressions!",); } - - HintFieldValues { values: hint_field_values } + let hint_value = HintCol::from_hint_field(hint_info); + let mut pos = Vec::new(); + for p in 0..hint_info.matrix_size { + pos.push(hint_info.pos[p as usize]); + } + if options.print_expression { + log::info!("HintsInf: {}", std::str::from_utf8(&hint_info.expression_line).unwrap()); + } + hint_field_values.insert(pos, hint_value); } + + HintFieldValues { values: hint_field_values } } pub fn set_hint_field( @@ -1171,8 +1194,8 @@ pub fn set_hint_field( values: &HintFieldValue, ) { let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), public_inputs: std::ptr::null_mut(), proof_values: std::ptr::null_mut(), challenges: std::ptr::null_mut(), @@ -1183,19 +1206,18 @@ pub fn set_hint_field( p_const_pols: std::ptr::null_mut(), p_const_tree: std::ptr::null_mut(), custom_commits: [std::ptr::null_mut(); 10], + custom_commits_extended: [std::ptr::null_mut(); 10], }; let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - let values_ptr: *mut c_void = match values { - HintFieldValue::Column(vec) => vec.as_ptr() as *mut c_void, - HintFieldValue::ColumnExtended(vec) => vec.as_ptr() as *mut c_void, + let values_ptr: *mut u8 = match values { + HintFieldValue::Column(vec) => vec.as_ptr() as *mut u8, + HintFieldValue::ColumnExtended(vec) => vec.as_ptr() as *mut u8, _ => panic!("Only column and column extended are accepted"), }; - let id = set_hint_field_c((&setup.p_setup).into(), (&steps_params).into(), values_ptr, hint_id, hint_field_name); - - air_instance.set_commit_calculated(id as usize); + set_hint_field_c((&setup.p_setup).into(), (&steps_params).into(), values_ptr, hint_id, hint_field_name); } pub fn set_hint_field_val( @@ -1207,17 +1229,18 @@ pub fn set_hint_field_val( ) { let steps_params = StepsParams { trace: std::ptr::null_mut(), - pols: std::ptr::null_mut(), + aux_trace: std::ptr::null_mut(), public_inputs: std::ptr::null_mut(), proof_values: std::ptr::null_mut(), challenges: std::ptr::null_mut(), - airgroup_values: air_instance.airgroup_values.as_mut_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_mut_ptr() as *mut c_void, + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), evals: std::ptr::null_mut(), xdivxsub: std::ptr::null_mut(), p_const_pols: std::ptr::null_mut(), p_const_tree: std::ptr::null_mut(), custom_commits: [std::ptr::null_mut(); 10], + custom_commits_extended: [std::ptr::null_mut(); 10], }; let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); @@ -1235,57 +1258,17 @@ pub fn set_hint_field_val( } }; - let values_ptr = value_array.as_ptr() as *mut c_void; - - let id = set_hint_field_c((&setup.p_setup).into(), (&steps_params).into(), values_ptr, hint_id, hint_field_name); + let values_ptr = value_array.as_ptr() as *mut u8; - air_instance.set_airgroupvalue_calculated(id as usize); -} - -pub fn print_expression( - setup_ctx: &SetupCtx, - air_instance: &mut AirInstance, - expr: &HintFieldValue, - first_print_value: u64, - last_print_value: u64, -) { - let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); - - match expr { - HintFieldValue::Column(vec) => { - print_expression_c( - (&setup.p_setup).into(), - vec.as_ptr() as *mut c_void, - 1, - first_print_value, - last_print_value, - ); - } - HintFieldValue::ColumnExtended(vec) => { - print_expression_c( - (&setup.p_setup).into(), - vec.as_ptr() as *mut c_void, - 3, - first_print_value, - last_print_value, - ); - } - HintFieldValue::Field(val) => { - println!("Field value: {:?}", val); - } - HintFieldValue::FieldExtended(val) => { - println!("FieldExtended values: {:?}", val); - } - HintFieldValue::String(_str) => panic!(), - } + set_hint_field_c((&setup.p_setup).into(), (&steps_params).into(), values_ptr, hint_id, hint_field_name); } pub fn print_row(setup_ctx: &SetupCtx, air_instance: &AirInstance, stage: u64, row: u64) { let setup = setup_ctx.get_setup(air_instance.airgroup_id, air_instance.air_id); let buffer = match stage == 1 { - true => air_instance.get_trace_ptr() as *mut c_void, - false => air_instance.get_buffer_ptr() as *mut c_void, + true => air_instance.get_trace_ptr(), + false => air_instance.get_aux_trace_ptr(), }; print_row_c((&setup.p_setup).into(), buffer, stage, row); diff --git a/hints/src/lib.rs b/hints/src/lib.rs index 0fe5e2a4..719cb48a 100644 --- a/hints/src/lib.rs +++ b/hints/src/lib.rs @@ -1,3 +1,5 @@ mod hints; +mod global_hints; pub use hints::*; +pub use global_hints::*; diff --git a/macros/src/lib.rs b/macros/src/lib.rs index ede1ec17..32289ce9 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -2,9 +2,8 @@ use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, format_ident, ToTokens}; use syn::{ - parse2, parse::{Parse, ParseStream}, - Ident, Generics, FieldsNamed, Result, Field, Token, + parse2, Field, FieldsNamed, Generics, Ident, LitInt, Result, Token, }; #[proc_macro] @@ -22,6 +21,10 @@ fn trace_impl(input: TokenStream2) -> Result { let trace_struct_name = parsed_input.struct_name; let generics = parsed_input.generics.params; let fields = parsed_input.fields; + let airgroup_id = parsed_input.airgroup_id; + let air_id = parsed_input.air_id; + let num_rows = parsed_input.num_rows; + let commit_id = parsed_input.commit_id; // Calculate ROW_SIZE based on the field types let row_size = fields @@ -47,134 +50,154 @@ fn trace_impl(input: TokenStream2) -> Result { impl<#generics: Copy> #row_struct_name<#generics> { pub const ROW_SIZE: usize = #row_size; - - pub fn as_slice(&self) -> &[#generics] { - unsafe { - std::slice::from_raw_parts( - self as *const #row_struct_name<#generics> as *const #generics, - #row_size, - ) - } - } } }; // Generate trace struct let trace_struct = quote! { - pub struct #trace_struct_name<'a, #generics> { - pub buffer: Option>, - pub slice_trace: &'a mut [#row_struct_name<#generics>], - num_rows: usize, + pub struct #trace_struct_name<#generics> { + pub buffer: Vec<#row_struct_name<#generics>>, + pub num_rows: usize, + pub row_size: usize, + pub airgroup_id: usize, + pub air_id: usize, + pub commit_id: Option, } - impl<'a, #generics: Default + Clone + Copy> #trace_struct_name<'a, #generics> { - pub fn new(num_rows: usize) -> Self { + impl<#generics: Default + Clone + Copy> #trace_struct_name<#generics> { + pub const NUM_ROWS: usize = #num_rows; + pub const AIRGROUP_ID: usize = #airgroup_id; + pub const AIR_ID: usize = #air_id; + + pub fn new() -> Self { + #trace_struct_name::with_capacity(Self::NUM_ROWS) + } + + pub fn with_capacity(num_rows: usize) -> Self { assert!(num_rows >= 2); assert!(num_rows & (num_rows - 1) == 0); - let buffer = vec![#generics::default(); num_rows * #row_struct_name::<#generics>::ROW_SIZE]; - let slice_trace = unsafe { - std::slice::from_raw_parts_mut( - buffer.as_ptr() as *mut #row_struct_name<#generics>, - num_rows, - ) - }; + let mut buff_uninit: Vec>> = Vec::with_capacity(num_rows); + + unsafe { + buff_uninit.set_len(num_rows); + } + let buffer: Vec<#row_struct_name::<#generics>> = unsafe { std::mem::transmute(buff_uninit) }; #trace_struct_name { - buffer: Some(buffer), - slice_trace, + buffer, num_rows, + row_size: #row_struct_name::<#generics>::ROW_SIZE, + airgroup_id: Self::AIRGROUP_ID, + air_id: Self::AIR_ID, + commit_id: #commit_id, } } - pub fn map_buffer( - external_buffer: &'a mut [#generics], - num_rows: usize, - offset: usize, - ) -> Result> { + pub fn new_zeroes() -> Self { + let num_rows = Self::NUM_ROWS; assert!(num_rows >= 2); assert!(num_rows & (num_rows - 1) == 0); - let start = offset; - let end = start + num_rows * #row_struct_name::<#generics>::ROW_SIZE; + let buffer = vec![#row_struct_name::<#generics>::default(); num_rows]; - if end > external_buffer.len() { - return Err("Buffer is too small to fit the trace".into()); + #trace_struct_name { + buffer, + num_rows, + row_size: #row_struct_name::<#generics>::ROW_SIZE, + airgroup_id: Self::AIRGROUP_ID, + air_id: Self::AIR_ID, + commit_id: #commit_id, } + } - let slice_trace = unsafe { - std::slice::from_raw_parts_mut( - external_buffer[start..end].as_ptr() as *mut #row_struct_name<#generics>, - num_rows, - ) - }; - - Ok(#trace_struct_name { - buffer: None, - slice_trace, + pub fn from_vec( + mut external_buffer: Vec<#generics>, + ) -> Self { + let num_rows = Self::NUM_ROWS; + let buffer: Vec<#row_struct_name::<#generics>> = unsafe { std::mem::transmute(external_buffer) }; + #trace_struct_name { + buffer, num_rows, - }) + row_size: #row_struct_name::<#generics>::ROW_SIZE, + airgroup_id: Self::AIRGROUP_ID, + air_id: Self::AIR_ID, + commit_id: #commit_id, + } } - pub fn from_row_vec( - external_buffer: Vec<#row_struct_name<#generics>>, - ) -> Result> { - let mut num_f = external_buffer.len() * #row_struct_name::<#generics>::ROW_SIZE; - - let slice_trace = unsafe { - let ptr = external_buffer.as_ptr() as *mut #row_struct_name<#generics>; - std::slice::from_raw_parts_mut( - ptr, - external_buffer.len(), - ) - }; - - let buffer_f = unsafe { - let mut vec = Vec::from_raw_parts( - external_buffer.as_ptr() as *mut #generics, - num_f, - num_f, - ); - vec.resize(num_f, #generics::default()); - vec - }; + pub fn num_rows(&self) -> usize { + self.num_rows + } - std::mem::forget(external_buffer); + pub fn airgroup_id(&self) -> usize { + self.airgroup_id + } - Ok(#trace_struct_name { - buffer: Some(buffer_f), - slice_trace, - num_rows: num_f / #row_struct_name::<#generics>::ROW_SIZE, - }) + pub fn air_id(&self) -> usize { + self.air_id } - pub fn num_rows(&self) -> usize { - self.num_rows + pub fn get_buffer(&mut self) -> Vec<#generics> { + let mut buffer = std::mem::take(&mut self.buffer); + unsafe { + let ptr = buffer.as_mut_ptr(); + let capacity = buffer.capacity() * self.row_size; + let len = buffer.len() * self.row_size; + + std::mem::forget(buffer); + + Vec::from_raw_parts(ptr.cast(), len, capacity) + } } } - impl<'a, #generics> std::ops::Index for #trace_struct_name<'a, #generics> { + impl<#generics> std::ops::Index for #trace_struct_name<#generics> { type Output = #row_struct_name<#generics>; fn index(&self, index: usize) -> &Self::Output { - &self.slice_trace[index] + &self.buffer[index] } } - impl<'a, #generics> std::ops::IndexMut for #trace_struct_name<'a, #generics> { + impl<#generics> std::ops::IndexMut for #trace_struct_name<#generics> { fn index_mut(&mut self, index: usize) -> &mut Self::Output { - &mut self.slice_trace[index] + &mut self.buffer[index] } } - impl<'a, #generics: Send> common::trace::Trace for #trace_struct_name<'a, #generics> { + impl<#generics: Send> common::trace::Trace<#generics> for #trace_struct_name<#generics> { fn num_rows(&self) -> usize { self.num_rows } - fn get_buffer_ptr(&mut self) -> *mut u8 { - let buffer = self.buffer.as_mut().expect("Buffer is not available"); - buffer.as_mut_ptr() as *mut u8 + fn n_cols(&self) -> usize { + self.row_size + } + + fn airgroup_id(&self) -> usize { + self.airgroup_id + } + + fn air_id(&self) -> usize { + self.air_id + } + + fn commit_id(&self) -> Option { + self.commit_id + } + + fn get_buffer(&mut self) -> Vec<#generics> { + let mut buffer = std::mem::take(&mut self.buffer); + unsafe { + let ptr = buffer.as_mut_ptr(); + let capacity = buffer.capacity() * self.row_size; + let len = buffer.len() * self.row_size; + + std::mem::forget(buffer); + + Vec::from_raw_parts(ptr.cast(), len, capacity) + } } } }; @@ -191,6 +214,10 @@ struct ParsedTraceInput { struct_name: Ident, generics: Generics, fields: FieldsNamed, + airgroup_id: LitInt, + air_id: LitInt, + num_rows: LitInt, + commit_id: TokenStream2, } impl Parse for ParsedTraceInput { @@ -212,7 +239,33 @@ impl Parse for ParsedTraceInput { let generics: Generics = input.parse()?; let fields: FieldsNamed = input.parse()?; - Ok(ParsedTraceInput { row_struct_name, struct_name, generics, fields }) + input.parse::()?; + let airgroup_id = input.parse::()?; + + input.parse::()?; + let air_id = input.parse::()?; + + input.parse::()?; + let num_rows = input.parse::()?; + + let commit_id: TokenStream2 = if input.peek(Token![,]) { + input.parse::()?; + let commit_id = input.parse::()?; + quote!(Some(#commit_id)) + } else { + quote!(None) + }; + + Ok(ParsedTraceInput { + row_struct_name, + struct_name, + generics, + fields, + airgroup_id, + air_id, + num_rows, + commit_id, + }) } } @@ -232,278 +285,246 @@ fn calculate_field_size_literal(field_type: &syn::Type) -> Result { } } -#[test] -fn test_trace_macro_generates_default_row_struct() { - let input = quote! { - Simple { a: F, b: F } - }; +#[proc_macro] +pub fn values(input: TokenStream) -> TokenStream { + match values_impl(input.into()) { + Ok(tokens) => tokens.into(), + Err(e) => e.to_compile_error().into(), + } +} - let _expected = quote! { +fn values_impl(input: TokenStream2) -> Result { + let parsed_input: ParsedValuesInput = parse2(input)?; + + let row_struct_name = parsed_input.row_struct_name; + let values_struct_name = parsed_input.struct_name; + let generics = parsed_input.generics.params; + let fields = parsed_input.fields; + + // Calculate TOTAL_SIZE based on the field types + let total_size = fields + .named + .iter() + .map(|field| calculate_field_slots(&field.ty)) + .collect::>>()? + .into_iter() + .sum::(); + + // Generate row struct + let field_definitions = fields.named.iter().map(|field| { + let Field { ident, ty, .. } = field; + quote! { pub #ident: #ty, } + }); + + let row_struct = quote! { #[repr(C)] #[derive(Debug, Clone, Copy, Default)] - pub struct SimpleRow { - pub a: F, - pub b: F, + pub struct #row_struct_name<#generics> { + #(#field_definitions)* } - impl SimpleRow { - pub const ROW_SIZE: usize = 2usize; + + impl<#generics: Copy> #row_struct_name<#generics> { + pub const TOTAL_SIZE: usize = #total_size; } - pub struct Simple<'a, F> { - pub buffer: Option>, - pub slice_trace: &'a mut [SimpleRow], - num_rows: usize, + }; + + let values_struct = quote! { + pub struct #values_struct_name<'a, #generics> { + pub buffer: Vec<#generics>, + pub slice_values: &'a mut #row_struct_name<#generics>, } - impl<'a, F: Default + Clone + Copy> Simple<'a, F> { - pub fn new(num_rows: usize) -> Self { - assert!(num_rows >= 2); - assert!(num_rows & (num_rows - 1) == 0); - let buffer = vec![F::default(); num_rows * SimpleRow::::ROW_SIZE]; - let slice_trace = unsafe { - std::slice::from_raw_parts_mut( - buffer.as_ptr() as *mut SimpleRow, - num_rows, - ) + + impl<'a, #generics: Default + Clone + Copy> #values_struct_name<'a, #generics> { + pub fn new() -> Self { + let mut buffer = vec![#generics::default(); #row_struct_name::<#generics>::TOTAL_SIZE]; // Interpolate here as well + + let slice_values = unsafe { + let ptr = buffer.as_mut_ptr() as *mut #row_struct_name<#generics>; + &mut *ptr }; - Simple { - buffer: Some(buffer), - slice_trace, - num_rows, + + #values_struct_name { + buffer: buffer, + slice_values, } } - pub fn map_buffer( - external_buffer: &'a mut [F], - num_rows: usize, - offset: usize, - ) -> Result> { - assert!(num_rows >= 2); - assert!(num_rows & (num_rows - 1) == 0); - let start = offset; - let end = start + num_rows * SimpleRow::::ROW_SIZE; - if end > external_buffer.len() { - return Err("Buffer is too small to fit the trace".into()); - } - let slice_trace = unsafe { - std::slice::from_raw_parts_mut( - external_buffer[start..end].as_ptr() as *mut SimpleRow, - num_rows, - ) + + pub fn from_vec( + mut external_buffer: Vec<#generics>, + ) -> Self { + let slice_values = unsafe { + // Create a mutable slice from the raw pointer of external_buffer + let ptr = external_buffer.as_mut_ptr() as *mut #row_struct_name<#generics>; + &mut *ptr }; - Ok(Simple { - buffer: None, - slice_trace, - num_rows, - }) + + // Return the struct with the owned buffers and borrowed slices + #values_struct_name { + buffer: external_buffer, + slice_values, + } } - pub fn from_row_vec( - external_buffer: Vec>, - ) -> Result> { - let num_rows = external_buffer.len().next_power_of_two(); - assert!(num_rows >= 2); - assert!(num_rows & (num_rows - 1) == 0); - let slice_trace = unsafe { - let ptr = external_buffer.as_ptr() as *mut SimpleRow; - std::slice::from_raw_parts_mut( - ptr, - num_rows, - ) - }; - let buffer_f = unsafe { - Vec::from_raw_parts( - external_buffer.as_ptr() as *mut F, - num_rows * SimpleRow::::ROW_SIZE, - num_rows * SimpleRow::::ROW_SIZE, - ) + + pub fn from_vec_guard( + mut external_buffer_rw: std::sync::RwLockWriteGuard>, + ) -> Self { + let slice_values = unsafe { + let ptr = external_buffer_rw.as_mut_ptr() as *mut #row_struct_name<#generics>; + &mut *ptr }; - std::mem::forget(external_buffer); - Ok(Simple { - buffer: Some(buffer_f), - slice_trace, - num_rows, - }) - } - pub fn num_rows(&self) -> usize { - self.num_rows + + #values_struct_name { + buffer: Vec::new(), + slice_values, + } } } - impl<'a, F> std::ops::Index for Simple<'a, F> { - type Output = SimpleRow; - fn index(&self, index: usize) -> &Self::Output { - &self.slice_trace[index] + + impl<'a, #generics> std::ops::Deref for #values_struct_name<'a, #generics> { + type Target = #row_struct_name<#generics>; + + fn deref(&self) -> &Self::Target { + &self.slice_values } } - impl<'a, F> std::ops::IndexMut for Simple<'a, F> { - fn index_mut(&mut self, index: usize) -> &mut Self::Output { - &mut self.slice_trace[index] + + impl<'a, #generics> std::fmt::Debug for #values_struct_name<'a, #generics> + where #generics: std::fmt::Debug { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&self.slice_values, f) } } - impl<'a, F: Send> common::trace::Trace for Simple<'a, F> { - fn num_rows(&self) -> usize { - self.num_rows - } - fn get_buffer_ptr(&mut self) -> *mut u8 { - let buffer = self.buffer.as_mut().expect("Buffer is not available"); - buffer.as_mut_ptr() as *mut u8 + + impl<'a, #generics> std::ops::DerefMut for #values_struct_name<'a, #generics> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.slice_values } } + impl<'a, #generics: Send> common::trace::Values<#generics> for #values_struct_name<'a, #generics> { + fn get_buffer(&mut self) -> Vec<#generics> { + let buffer = std::mem::take(&mut self.buffer); + buffer + } + } }; - let _generated = trace_impl(input).unwrap(); - // assert_eq!(generated.to_string(), expected.into_token_stream().to_string()); + + Ok(quote! { + #row_struct + #values_struct + }) } -#[test] -fn test_trace_macro_with_explicit_row_struct_name() { - let input = quote! { - SimpleRow, Simple { a: F, b: F } - }; +struct ParsedValuesInput { + row_struct_name: Ident, + struct_name: Ident, + generics: Generics, + fields: FieldsNamed, +} - let _expected = quote! { - #[repr(C)] - #[derive(Debug, Clone, Copy, Default)] - pub struct SimpleRow { - pub a: F, - pub b: F, - } +impl Parse for ParsedValuesInput { + fn parse(input: ParseStream) -> Result { + let lookahead = input.lookahead1(); + let row_struct_name; - impl SimpleRow { - pub const ROW_SIZE: usize = 2usize; + // Handle explicit or implicit row struct names + if lookahead.peek(Ident) && input.peek2(Token![,]) { + row_struct_name = Some(input.parse::()?); + input.parse::()?; // Skip comma after explicit row name + } else { + row_struct_name = None; } - pub struct Simple<'a, F> { - pub buffer: Option>, - pub slice_trace: &'a mut [SimpleRow], - num_rows: usize, - } + let struct_name = input.parse::()?; + let row_struct_name = row_struct_name.unwrap_or_else(|| format_ident!("{}Row", struct_name)); - impl<'a, F: Default + Clone + Copy> Simple<'a, F> { - pub fn new(num_rows: usize) -> Self { - assert!(num_rows >= 2); - assert!(num_rows & (num_rows - 1) == 0); - let buffer = vec![F::default(); num_rows * SimpleRow::::ROW_SIZE]; - let slice_trace = unsafe { - std::slice::from_raw_parts_mut( - buffer.as_ptr() as *mut SimpleRow, - num_rows, - ) - }; - Simple { - buffer: Some(buffer), - slice_trace, - num_rows, - } - } + let generics: Generics = input.parse()?; + let fields: FieldsNamed = input.parse()?; - pub fn map_buffer( - external_buffer: &'a mut [F], - num_rows: usize, - offset: usize, - ) -> Result> { - assert!(num_rows >= 2); - assert!(num_rows & (num_rows - 1) == 0); - let start = offset; - let end = start + num_rows * SimpleRow::::ROW_SIZE; - if end > external_buffer.len() { - return Err("Buffer is too small to fit the trace".into()); - } - let slice_trace = unsafe { - std::slice::from_raw_parts_mut( - external_buffer[start..end].as_ptr() as *mut SimpleRow, - num_rows, - ) - }; - Ok(Simple { - buffer: None, - slice_trace, - num_rows, - }) - } + Ok(ParsedValuesInput { row_struct_name, struct_name, generics, fields }) + } +} - pub fn from_row_vec( - external_buffer: Vec>, - ) -> Result> { - let num_rows = external_buffer.len().next_power_of_two(); - assert!(num_rows >= 2); - assert!(num_rows & (num_rows - 1) == 0); - let slice_trace = unsafe { - let ptr = external_buffer.as_ptr() as *mut SimpleRow; - std::slice::from_raw_parts_mut( - ptr, - num_rows, - ) - }; - let buffer_f = unsafe { - Vec::from_raw_parts( - external_buffer.as_ptr() as *mut F, - num_rows * SimpleRow::::ROW_SIZE, num_rows * SimpleRow::::ROW_SIZE, - ) - }; - std::mem::forget(external_buffer); - Ok(Simple { - buffer: Some(buffer_f), - slice_trace, num_rows, - }) - } +fn calculate_field_slots(ty: &syn::Type) -> Result { + match ty { + // Handle `F` + syn::Type::Path(type_path) if is_ident(type_path, "F") => Ok(1), - pub fn num_rows(&self) -> usize { - self.num_rows - } + // Handle `FieldExtension` + syn::Type::Path(type_path) if is_ident(type_path, "FieldExtension") => { + // Assuming FieldExtension size is always 3 slots for this example. + Ok(3) } - impl<'a, F> std::ops::Index for Simple<'a, F> { - type Output = SimpleRow; - - fn index(&self, index: usize) -> &Self::Output { - &self.slice_trace[index] - } + // Handle `[F; N]` and `[FieldExtension; N]` + syn::Type::Array(type_array) => { + let len = type_array.len.to_token_stream().to_string().parse::().map_err(|e| { + syn::Error::new_spanned(&type_array.len, format!("Failed to parse array length: {}", e)) + })?; + let elem_slots = calculate_field_slots(&type_array.elem)?; + Ok(len * elem_slots) } - impl<'a, F> std::ops::IndexMut for Simple<'a, F> { - fn index_mut(&mut self, index: usize) -> &mut Self::Output { - &mut self.slice_trace[index] - } - } + _ => Err(syn::Error::new_spanned(ty, "Unsupported type for slot calculation")), + } +} - impl<'a, F: Send> common::trace::Trace for Simple<'a, F> { - fn num_rows(&self) -> usize { - self.num_rows - } +// Helper to check if a type is a specific identifier +fn is_ident(type_path: &syn::TypePath, name: &str) -> bool { + type_path.path.segments.last().map_or(false, |seg| seg.ident == name) +} - fn get_buffer_ptr(&mut self) -> *mut u8 { - let buffer = self.buffer.as_mut().expect("Buffer is not available"); - buffer.as_mut_ptr() as *mut u8 - } - } +#[test] +fn test_trace_macro_generates_default_row_struct() { + let input = quote! { + Simple { a: F, b: F }, 0,0,0,0 + }; + + let _generated = trace_impl(input).unwrap(); +} + +#[test] +fn test_trace_macro_with_explicit_row_struct_name() { + let input = quote! { + SimpleRow, Simple { a: F, b: F }, 0,0,0,0 }; let _generated = trace_impl(input).unwrap(); - // assert_eq!(generated.to_string(), expected.into_token_stream().to_string()); } #[test] fn test_parsing_01() { let input = quote! { - TraceRow, MyTrace { a: F, b: F } + TraceRow, MyTrace { a: F, b: F }, 0, 0, 34, 38 }; let parsed: ParsedTraceInput = parse2(input).unwrap(); assert_eq!(parsed.row_struct_name, "TraceRow"); assert_eq!(parsed.struct_name, "MyTrace"); + assert_eq!(parsed.airgroup_id.base10_parse::().unwrap(), 0); + assert_eq!(parsed.air_id.base10_parse::().unwrap(), 0); + assert_eq!(parsed.num_rows.base10_parse::().unwrap(), 34); + // assert_eq!(parsed.commit_id.to_string().parse::().unwrap(), 38); } #[test] fn test_parsing_02() { let input = quote! { - SimpleRow, Simple { a: F } + SimpleRow, Simple { a: F }, 0, 0, 127_456, 0 }; let parsed: ParsedTraceInput = parse2(input).unwrap(); assert_eq!(parsed.row_struct_name, "SimpleRow"); assert_eq!(parsed.struct_name, "Simple"); + assert_eq!(parsed.airgroup_id.base10_parse::().unwrap(), 0); + assert_eq!(parsed.air_id.base10_parse::().unwrap(), 0); + assert_eq!(parsed.num_rows.base10_parse::().unwrap(), 127_456); + // assert_eq!(parsed.commit_id.to_string().parse::().unwrap(), 0); } #[test] fn test_parsing_03() { let input = quote! { - Simple { a: F } + Simple { a: F }, 0, 0, 0, 0 }; let parsed: ParsedTraceInput = parse2(input).unwrap(); assert_eq!(parsed.row_struct_name, "SimpleRow"); diff --git a/pil2-components/lib/std/rs/Cargo.toml b/pil2-components/lib/std/rs/Cargo.toml index 68d35bb8..aa58e2cd 100644 --- a/pil2-components/lib/std/rs/Cargo.toml +++ b/pil2-components/lib/std/rs/Cargo.toml @@ -4,14 +4,13 @@ version = "0.1.0" edition = "2021" [dependencies] -proofman.workspace = true proofman-common.workspace = true proofman-hints.workspace = true proofman-util.workspace = true -pilout.workspace = true log.workspace = true num-bigint.workspace = true num-traits.workspace = true p3-goldilocks.workspace = true p3-field.workspace = true rayon.workspace = true +witness.workspace = true diff --git a/pil2-components/lib/std/rs/src/decider.rs b/pil2-components/lib/std/rs/src/decider.rs index 7b5bd441..0dbadaf8 100644 --- a/pil2-components/lib/std/rs/src/decider.rs +++ b/pil2-components/lib/std/rs/src/decider.rs @@ -1,7 +1,7 @@ use std::sync::Arc; -use proofman_common::{ProofCtx, SetupCtx}; +use proofman_common::SetupCtx; pub trait Decider { - fn decide(&self, sctx: Arc, pctx: Arc>); + fn decide(&self, sctx: Arc); } diff --git a/pil2-components/lib/std/rs/src/range_check/specified_ranges.rs b/pil2-components/lib/std/rs/src/range_check/specified_ranges.rs index 426b05e0..7cb06f8a 100644 --- a/pil2-components/lib/std/rs/src/range_check/specified_ranges.rs +++ b/pil2-components/lib/std/rs/src/range_check/specified_ranges.rs @@ -4,10 +4,11 @@ use std::sync::{Arc, Mutex}; use num_traits::ToPrimitive; use p3_field::PrimeField; -use proofman::{get_hint_field_gc, WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{AirInstance, DistributionCtx, ProofCtx, SetupCtx, TraceInfo}; use proofman_hints::{ - get_hint_field, get_hint_field_constant, get_hint_ids_by_name, set_hint_field, HintFieldOptions, HintFieldValue, + get_hint_field_gc, get_hint_field, get_hint_field_constant, get_hint_ids_by_name, set_hint_field, HintFieldOptions, + HintFieldValue, }; use proofman_util::create_buffer_fast; @@ -16,8 +17,6 @@ use crate::Range; const PROVE_CHUNK_SIZE: usize = 1 << 5; pub struct SpecifiedRanges { - wcm: Arc>, - // Parameters hints: Mutex>, airgroup_id: usize, @@ -33,10 +32,7 @@ pub struct SpecifiedRanges { impl SpecifiedRanges { const MY_NAME: &'static str = "SpecRang"; - pub fn new(wcm: Arc>) -> Arc { - let pctx = wcm.get_pctx(); - let sctx = wcm.get_sctx(); - + pub fn new(pctx: Arc>, sctx: Arc) -> Arc { // Scan global hints to get the airgroup_id and air_id let hint_global = get_hint_ids_by_name(sctx.get_global_bin(), "specified_ranges"); let airgroup_id = get_hint_field_gc::(pctx.clone(), sctx.clone(), hint_global[0], "airgroup_id", false); @@ -62,8 +58,7 @@ impl SpecifiedRanges { } }; - let specified_ranges = Arc::new(Self { - wcm: wcm.clone(), + Arc::new(Self { hints: Mutex::new(Vec::new()), airgroup_id, air_id, @@ -71,11 +66,7 @@ impl SpecifiedRanges { ranges: Mutex::new(Vec::new()), inputs: Mutex::new(Vec::new()), mul_columns: Mutex::new(Vec::new()), - }); - - wcm.register_component(specified_ranges.clone(), Some(airgroup_id), Some(&[air_id])); - - specified_ranges + }) } pub fn update_inputs(&self, value: F, range: Range, multiplicity: F) { @@ -91,19 +82,14 @@ impl SpecifiedRanges { } } - pub fn drain_inputs(&self) { + pub fn drain_inputs(&self, pctx: Arc>, sctx: Arc) { let mut inputs = self.inputs.lock().unwrap(); let drained_inputs = inputs.drain(..).collect(); - let pctx = self.wcm.get_pctx(); - let sctx = self.wcm.get_sctx(); - let ectx = self.wcm.get_ectx(); // Perform the last update self.update_multiplicity(drained_inputs); - let mut dctx: std::sync::RwLockWriteGuard<'_, proofman_common::DistributionCtx> = ectx.dctx.write().unwrap(); - - let (is_myne, global_idx) = dctx.add_instance(self.airgroup_id, self.air_id, 1); + let mut dctx: std::sync::RwLockWriteGuard<'_, DistributionCtx> = pctx.dctx.write().unwrap(); let mut multiplicities = self .mul_columns @@ -117,25 +103,33 @@ impl SpecifiedRanges { _ => panic!("Multiplicities must be columns"), }) .collect::>>(); + + let (instance_found, instance_idx) = dctx.find_instance(self.airgroup_id, self.air_id); + + let (is_mine, global_idx) = if instance_found { + (dctx.is_my_instance(instance_idx), instance_idx) + } else { + dctx.add_instance(self.airgroup_id, self.air_id, 1) + }; + let owner = dctx.owner(global_idx); dctx.distribute_multiplicities(&mut multiplicities, owner); - if is_myne { + if is_mine { // Set the multiplicity columns as done let hints = self.hints.lock().unwrap(); - let air_instance_repo = &self.wcm.get_pctx().air_instance_repo; + let air_instance_repo = &pctx.air_instance_repo; let instance: Vec = air_instance_repo.find_air_instances(self.airgroup_id, self.air_id); let air_instance_id = if !instance.is_empty() { - air_instance_repo.find_air_instances(self.airgroup_id, self.air_id)[0] + instance[0] } else { let num_rows = pctx.global_info.airs[self.airgroup_id][self.air_id].num_rows; let buffer_size = multiplicities.len() * num_rows; let buffer: Vec = create_buffer_fast(buffer_size); - let air_instance = AirInstance::new(sctx.clone(), self.airgroup_id, self.air_id, None, buffer); - pctx.air_instance_repo.add_air_instance(air_instance, Some(global_idx)); - pctx.air_instance_repo.air_instances.read().unwrap().len() - 1 + let air_instance = AirInstance::new(TraceInfo::new(self.airgroup_id, self.air_id, buffer)); + pctx.air_instance_repo.add_air_instance(air_instance, Some(global_idx)) }; let mut air_instance_rw = air_instance_repo.air_instances.write().unwrap(); let air_instance = &mut air_instance_rw[air_instance_id]; @@ -148,7 +142,7 @@ impl SpecifiedRanges { .collect::>>(); for (index, hint) in hints[1..].iter().enumerate() { - set_hint_field(&self.wcm.get_sctx(), air_instance, *hint, "reference", &mul_columns_2[index]); + set_hint_field(&sctx, air_instance, *hint, "reference", &mul_columns_2[index]); } log::trace!("{}: ··· Drained inputs for AIR '{}'", Self::MY_NAME, "SpecifiedRanges"); @@ -178,10 +172,18 @@ impl SpecifiedRanges { mul_columns[range_index].add(index, *mul); } } + + pub fn airgroup_id(&self) -> usize { + self.airgroup_id + } + + pub fn air_id(&self) -> usize { + self.air_id + } } impl WitnessComponent for SpecifiedRanges { - fn start_proof(&self, pctx: Arc>, _ectx: Arc, sctx: Arc) { + fn start_proof(&self, pctx: Arc>, sctx: Arc) { // Obtain info from the mul hints let setup = sctx.get_setup(self.airgroup_id, self.air_id); let specified_hints = get_hint_ids_by_name(setup.p_setup.p_expressions_bin, "specified_ranges"); @@ -295,7 +297,7 @@ impl WitnessComponent for SpecifiedRanges { let buffer = create_buffer_fast(buffer_size as usize); // Add a new air instance. Since Specified Ranges is a table, only this air instance is needed - let mut air_instance = AirInstance::new(sctx.clone(), self.airgroup_id, self.air_id, None, buffer); + let mut air_instance = AirInstance::new(TraceInfo::new(self.airgroup_id, self.air_id, buffer)); let mut mul_columns_guard = self.mul_columns.lock().unwrap(); for hint in hints_guard[1..].iter() { mul_columns_guard.push(get_hint_field::( @@ -326,18 +328,11 @@ impl WitnessComponent for SpecifiedRanges { }; *self.num_rows.lock().unwrap() = num_rows.as_canonical_biguint().to_usize().unwrap(); - - //pctx.air_instance_repo.add_air_instance(air_instance); - // note: there is room for simplification here } - fn calculate_witness( - &self, - _stage: u32, - _air_instance: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn calculate_witness(&self, stage: u32, pctx: Arc>, sctx: Arc) { + if stage == 1 { + Self::drain_inputs(self, pctx, sctx); + } } } diff --git a/pil2-components/lib/std/rs/src/range_check/std_range_check.rs b/pil2-components/lib/std/rs/src/range_check/std_range_check.rs index 0b7a5c9b..2ceb048e 100644 --- a/pil2-components/lib/std/rs/src/range_check/std_range_check.rs +++ b/pil2-components/lib/std/rs/src/range_check/std_range_check.rs @@ -7,10 +7,9 @@ use std::{ use num_bigint::BigInt; use p3_field::PrimeField; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{ExecutionCtx, ProofCtx, SetupCtx, StdMode, ModeName}; +use witness::WitnessComponent; +use proofman_common::{ProofCtx, SetupCtx, StdMode, ModeName}; use proofman_hints::{get_hint_field_constant, get_hint_ids_by_name, HintFieldOptions, HintFieldValue}; -use rayon::Scope; use crate::{Decider, Range, SpecifiedRanges, U16Air, U8Air}; @@ -25,71 +24,67 @@ pub enum RangeCheckAir { } #[derive(Clone, Debug, PartialEq, Eq, Hash)] -enum StdRangeCheckType { +pub enum StdRangeCheckType { Valid(RangeCheckAir), U8AirDouble, U16AirDouble, } #[derive(Clone, Debug)] -struct StdRangeItem { +pub struct StdRangeItem { rc_type: StdRangeCheckType, range: Range, } +impl StdRangeItem { + pub fn range_type(&self) -> StdRangeCheckType { + self.rc_type.clone() + } +} + // TODO: Remove Arc pub struct StdRangeCheck { mode: StdMode, ranges: Mutex>>, - u8air: Option>>, - u16air: Option>>, - specified_ranges: Option>>, + pub u8air: Option>>, + pub u16air: Option>>, + pub specified_ranges: Option>>, } impl Decider for StdRangeCheck { - fn decide(&self, sctx: Arc, pctx: Arc>) { + fn decide(&self, sctx: Arc) { // Scan the pilout for airs that have rc-related hints - let air_groups = pctx.pilout.air_groups(); - - air_groups.iter().for_each(|air_group| { - let airs = air_group.airs(); - airs.iter().for_each(|air| { - let airgroup_id = air.airgroup_id; - let air_id = air.air_id; - let setup = sctx.get_setup(airgroup_id, air_id); - - // Obtain info from the range hints - let rc_hints = get_hint_ids_by_name(setup.p_setup.p_expressions_bin, "range_def"); - for hint in rc_hints { - // Register the range - self.register_range(sctx.clone(), airgroup_id, air_id, hint); - } - }); - }); + for (airgroup_id, air_id) in sctx.get_setups_list() { + let setup = sctx.get_setup(airgroup_id, air_id); + + // Obtain info from the range hints + let rc_hints = get_hint_ids_by_name(setup.p_setup.p_expressions_bin, "range_def"); + for hint in rc_hints { + // Register the range + self.register_range(sctx.clone(), airgroup_id, air_id, hint); + } + } } } impl StdRangeCheck { const _MY_NAME: &'static str = "STD Range Check"; - pub fn new(mode: StdMode, wcm: Arc>) -> Arc { - let sctx = wcm.get_sctx(); - + pub fn new(mode: StdMode, pctx: Arc>, sctx: Arc) -> Arc { // Scan global hints to know which airs are associated with the range check let u8air_hint = get_hint_ids_by_name(sctx.get_global_bin(), "u8air"); let u16air_hint = get_hint_ids_by_name(sctx.get_global_bin(), "u16air"); let specified_ranges_hint = get_hint_ids_by_name(sctx.get_global_bin(), "specified_ranges"); - let u8air = if !u8air_hint.is_empty() { Some(U8Air::new(wcm.clone())) } else { None }; - let u16air = if !u16air_hint.is_empty() { Some(U16Air::new(wcm.clone())) } else { None }; - let specified_ranges = - if !specified_ranges_hint.is_empty() { Some(SpecifiedRanges::new(wcm.clone())) } else { None }; - - let std_range_check = Arc::new(Self { mode, ranges: Mutex::new(Vec::new()), u8air, u16air, specified_ranges }); - - wcm.register_component(std_range_check.clone(), None, None); + let u8air = if !u8air_hint.is_empty() { Some(U8Air::new(pctx.clone(), sctx.clone())) } else { None }; + let u16air = if !u16air_hint.is_empty() { Some(U16Air::new(pctx.clone(), sctx.clone())) } else { None }; + let specified_ranges = if !specified_ranges_hint.is_empty() { + Some(SpecifiedRanges::new(pctx.clone(), sctx.clone())) + } else { + None + }; - std_range_check + Arc::new(Self { mode, ranges: Mutex::new(Vec::new()), u8air, u16air, specified_ranges }) } fn register_range(&self, sctx: Arc, airgroup_id: usize, air_id: usize, hint: u64) { @@ -253,32 +248,39 @@ impl StdRangeCheck { } } - pub fn drain_inputs(&self, _pctx: Arc>, _scope: Option<&Scope>) { + pub fn drain_inputs(&self, pctx: Arc>, sctx: Arc) { if let Some(u8air) = self.u8air.as_ref() { - u8air.drain_inputs(); + u8air.drain_inputs(pctx.clone(), sctx.clone()); } if let Some(u16air) = self.u16air.as_ref() { - u16air.drain_inputs(); + u16air.drain_inputs(pctx.clone(), sctx.clone()); } if let Some(specified_ranges) = self.specified_ranges.as_ref() { - specified_ranges.drain_inputs(); + specified_ranges.drain_inputs(pctx.clone(), sctx.clone()); } } -} -impl WitnessComponent for StdRangeCheck { - fn start_proof(&self, pctx: Arc>, _ectx: Arc, sctx: Arc) { - self.decide(sctx, pctx); + pub fn get_ranges(&self) -> Vec<(usize, usize, RangeCheckAir)> { + let mut ranges = Vec::new(); + + if let Some(u8air) = self.u8air.as_ref() { + ranges.push((u8air.airgroup_id(), u8air.air_id(), RangeCheckAir::U8Air)); + } + + if let Some(u16air) = self.u16air.as_ref() { + ranges.push((u16air.airgroup_id(), u16air.air_id(), RangeCheckAir::U16Air)); + } + + if let Some(specified_ranges) = self.specified_ranges.as_ref() { + ranges.push((specified_ranges.airgroup_id(), specified_ranges.air_id(), RangeCheckAir::SpecifiedRanges)); + } + + ranges } +} - fn calculate_witness( - &self, - _stage: u32, - _air_instance: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { - // Nothing to do +impl WitnessComponent for StdRangeCheck { + fn start_proof(&self, _pctx: Arc>, sctx: Arc) { + self.decide(sctx); } } diff --git a/pil2-components/lib/std/rs/src/range_check/u16air.rs b/pil2-components/lib/std/rs/src/range_check/u16air.rs index 1a7c2f6a..72d6101e 100644 --- a/pil2-components/lib/std/rs/src/range_check/u16air.rs +++ b/pil2-components/lib/std/rs/src/range_check/u16air.rs @@ -3,10 +3,12 @@ use std::sync::{atomic::AtomicU64, Arc, Mutex}; use num_traits::ToPrimitive; use p3_field::{Field, PrimeField}; -use proofman::{get_hint_field_gc, WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{TraceInfo, AirInstance, ProofCtx, SetupCtx}; -use proofman_hints::{get_hint_field, get_hint_ids_by_name, set_hint_field, HintFieldOptions, HintFieldValue}; +use proofman_hints::{ + get_hint_field_gc, get_hint_field, get_hint_ids_by_name, set_hint_field, HintFieldOptions, HintFieldValue, +}; use proofman_util::create_buffer_fast; use std::sync::atomic::Ordering; @@ -14,8 +16,6 @@ const PROVE_CHUNK_SIZE: usize = 1 << 5; const NUM_ROWS: usize = 1 << 16; pub struct U16Air { - wcm: Arc>, - // Parameters hint: AtomicU64, airgroup_id: usize, @@ -28,10 +28,7 @@ pub struct U16Air { impl U16Air { const MY_NAME: &'static str = "U16Air "; - pub fn new(wcm: Arc>) -> Arc { - let pctx = wcm.get_pctx(); - let sctx = wcm.get_sctx(); - + pub fn new(pctx: Arc>, sctx: Arc) -> Arc { // Scan global hints to get the airgroup_id and air_id let hint_global = get_hint_ids_by_name(sctx.get_global_bin(), "u16air"); let airgroup_id = get_hint_field_gc::(pctx.clone(), sctx.clone(), hint_global[0], "airgroup_id", false); @@ -57,18 +54,13 @@ impl U16Air { } }; - let u16air = Arc::new(Self { - wcm: wcm.clone(), + Arc::new(Self { hint: AtomicU64::new(0), airgroup_id, air_id, inputs: Mutex::new(Vec::new()), mul_column: Mutex::new(HintFieldValue::Field(F::zero())), - }); - - wcm.register_component(u16air.clone(), Some(airgroup_id), Some(&[air_id])); - - u16air + }) } pub fn update_inputs(&self, value: F, multiplicity: F) { @@ -84,19 +76,15 @@ impl U16Air { } } - pub fn drain_inputs(&self) { + pub fn drain_inputs(&self, pctx: Arc>, sctx: Arc) { let mut inputs = self.inputs.lock().unwrap(); let drained_inputs = inputs.drain(..).collect(); - let pctx = self.wcm.get_pctx(); - let sctx = self.wcm.get_sctx(); - let ectx = self.wcm.get_ectx(); // Perform the last update self.update_multiplicity(drained_inputs); - let mut dctx: std::sync::RwLockWriteGuard<'_, proofman_common::DistributionCtx> = ectx.dctx.write().unwrap(); + let mut dctx: std::sync::RwLockWriteGuard<'_, proofman_common::DistributionCtx> = pctx.dctx.write().unwrap(); - let (is_myne, global_idx) = dctx.add_instance(self.airgroup_id, self.air_id, 1); let mut multiplicity = match &*self.mul_column.lock().unwrap() { HintFieldValue::Column(values) => { values.iter().map(|x| x.as_canonical_biguint().to_u64().unwrap()).collect::>() @@ -104,22 +92,29 @@ impl U16Air { _ => panic!("Multiplicities must be a column"), }; + let (instance_found, instance_idx) = dctx.find_instance(self.airgroup_id, self.air_id); + + let (is_mine, global_idx) = if instance_found { + (dctx.is_my_instance(instance_idx), instance_idx) + } else { + dctx.add_instance(self.airgroup_id, self.air_id, 1) + }; + let owner = dctx.owner(global_idx); dctx.distribute_multiplicity(&mut multiplicity, owner); - if is_myne { - let air_instance_repo = &self.wcm.get_pctx().air_instance_repo; + if is_mine { + let air_instance_repo = &pctx.air_instance_repo; let instance: Vec = air_instance_repo.find_air_instances(self.airgroup_id, self.air_id); let air_instance_id = if !instance.is_empty() { - air_instance_repo.find_air_instances(self.airgroup_id, self.air_id)[0] + instance[0] } else { // create instance let num_rows = pctx.global_info.airs[self.airgroup_id][self.air_id].num_rows; let buffer_size = num_rows; let buffer: Vec = create_buffer_fast(buffer_size); - let air_instance = AirInstance::new(sctx.clone(), self.airgroup_id, self.air_id, None, buffer); - pctx.air_instance_repo.add_air_instance(air_instance, Some(global_idx)); - pctx.air_instance_repo.air_instances.read().unwrap().len() - 1 + let air_instance = AirInstance::new(TraceInfo::new(self.airgroup_id, self.air_id, buffer)); + pctx.air_instance_repo.add_air_instance(air_instance, Some(global_idx)) }; let mut air_instance_rw = air_instance_repo.air_instances.write().unwrap(); @@ -129,13 +124,7 @@ impl U16Air { let mul_column_2 = HintFieldValue::Column(multiplicity.iter().map(|x| F::from_canonical_u64(*x)).collect::>()); - set_hint_field( - &self.wcm.get_sctx(), - air_instance, - self.hint.load(Ordering::Acquire), - "reference", - &mul_column_2, - ); + set_hint_field(&sctx, air_instance, self.hint.load(Ordering::Acquire), "reference", &mul_column_2); log::trace!("{}: ··· Drained inputs for AIR '{}'", Self::MY_NAME, "U16Air"); } @@ -143,19 +132,27 @@ impl U16Air { fn update_multiplicity(&self, drained_inputs: Vec<(F, F)>) { // TODO! Do it in parallel + let mut mul_column = self.mul_column.lock().unwrap(); for (input, mul) in &drained_inputs { let value = input.as_canonical_biguint().to_usize().expect("Cannot convert to usize"); // Note: to avoid non-expected panics, we perform a reduction to the value // In debug mode, this is, in fact, checked before let index = value % NUM_ROWS; - let mut mul_column = self.mul_column.lock().unwrap(); mul_column.add(index, *mul); } } + + pub fn airgroup_id(&self) -> usize { + self.airgroup_id + } + + pub fn air_id(&self) -> usize { + self.air_id + } } impl WitnessComponent for U16Air { - fn start_proof(&self, pctx: Arc>, _ectx: Arc, sctx: Arc) { + fn start_proof(&self, pctx: Arc>, sctx: Arc) { // Obtain info from the mul hints let setup = sctx.get_setup(self.airgroup_id, self.air_id); let u16air_hints = get_hint_ids_by_name(setup.p_setup.p_expressions_bin, "u16air"); @@ -170,28 +167,21 @@ impl WitnessComponent for U16Air { let buffer = create_buffer_fast(buffer_size); // Add a new air instance. Since U16Air is a table, only this air instance is needed - let mut air_instance = AirInstance::new(sctx.clone(), self.airgroup_id, self.air_id, None, buffer); + let mut air_instance = AirInstance::new(TraceInfo::new(self.airgroup_id, self.air_id, buffer)); *self.mul_column.lock().unwrap() = get_hint_field::( - &self.wcm.get_sctx(), + &sctx, &pctx, &mut air_instance, u16air_hints[0] as usize, "reference", HintFieldOptions::dest_with_zeros(), ); - - //pctx.air_instance_repo.add_air_instance(air_instance); - //note: there is room for simplification here } - fn calculate_witness( - &self, - _stage: u32, - _air_instance: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn calculate_witness(&self, stage: u32, pctx: Arc>, sctx: Arc) { + if stage == 1 { + Self::drain_inputs(self, pctx, sctx); + } } } diff --git a/pil2-components/lib/std/rs/src/range_check/u8air.rs b/pil2-components/lib/std/rs/src/range_check/u8air.rs index ebb4fd2d..adf46d5f 100644 --- a/pil2-components/lib/std/rs/src/range_check/u8air.rs +++ b/pil2-components/lib/std/rs/src/range_check/u8air.rs @@ -2,9 +2,11 @@ use std::sync::{atomic::AtomicU64, Arc, Mutex}; use num_traits::ToPrimitive; use p3_field::{Field, PrimeField}; -use proofman::{get_hint_field_gc, WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; -use proofman_hints::{get_hint_field, get_hint_ids_by_name, set_hint_field, HintFieldOptions, HintFieldValue}; +use witness::WitnessComponent; +use proofman_common::{TraceInfo, AirInstance, ProofCtx, SetupCtx}; +use proofman_hints::{ + get_hint_field_gc, get_hint_field, get_hint_ids_by_name, set_hint_field, HintFieldOptions, HintFieldValue, +}; use proofman_util::create_buffer_fast; use std::sync::atomic::Ordering; @@ -12,8 +14,6 @@ const PROVE_CHUNK_SIZE: usize = 1 << 5; const NUM_ROWS: usize = 1 << 8; pub struct U8Air { - wcm: Arc>, - // Parameters hint: AtomicU64, airgroup_id: usize, @@ -27,10 +27,7 @@ pub struct U8Air { impl U8Air { const MY_NAME: &'static str = "U8Air "; - pub fn new(wcm: Arc>) -> Arc { - let pctx = wcm.get_pctx(); - let sctx = wcm.get_sctx(); - + pub fn new(pctx: Arc>, sctx: Arc) -> Arc { // Scan global hints to get the airgroup_id and air_id let hint_global = get_hint_ids_by_name(sctx.get_global_bin(), "u8air"); let airgroup_id = get_hint_field_gc::(pctx.clone(), sctx.clone(), hint_global[0], "airgroup_id", false); @@ -56,18 +53,13 @@ impl U8Air { } }; - let u8air = Arc::new(Self { - wcm: wcm.clone(), + Arc::new(Self { hint: AtomicU64::new(0), airgroup_id, air_id, inputs: Mutex::new(Vec::new()), mul_column: Mutex::new(HintFieldValue::Field(F::zero())), - }); - - wcm.register_component(u8air.clone(), Some(airgroup_id), Some(&[air_id])); - - u8air + }) } pub fn update_inputs(&self, value: F, multiplicity: F) { @@ -83,19 +75,15 @@ impl U8Air { } } - pub fn drain_inputs(&self) { + pub fn drain_inputs(&self, pctx: Arc>, sctx: Arc) { let mut inputs = self.inputs.lock().unwrap(); let drained_inputs = inputs.drain(..).collect(); - let pctx = self.wcm.get_pctx(); - let sctx = self.wcm.get_sctx(); - let ectx = self.wcm.get_ectx(); // Perform the last update self.update_multiplicity(drained_inputs); - let mut dctx: std::sync::RwLockWriteGuard<'_, proofman_common::DistributionCtx> = ectx.dctx.write().unwrap(); + let mut dctx: std::sync::RwLockWriteGuard<'_, proofman_common::DistributionCtx> = pctx.dctx.write().unwrap(); - let (is_myne, global_idx) = dctx.add_instance(self.airgroup_id, self.air_id, 1); let mut multiplicity = match &*self.mul_column.lock().unwrap() { HintFieldValue::Column(values) => { values.iter().map(|x| x.as_canonical_biguint().to_u64().unwrap()).collect::>() @@ -103,21 +91,28 @@ impl U8Air { _ => panic!("Multiplicities must be a column"), }; + let (instance_found, instance_idx) = dctx.find_instance(self.airgroup_id, self.air_id); + + let (is_mine, global_idx) = if instance_found { + (dctx.is_my_instance(instance_idx), instance_idx) + } else { + dctx.add_instance(self.airgroup_id, self.air_id, 1) + }; + let owner = dctx.owner(global_idx); dctx.distribute_multiplicity(&mut multiplicity, owner); - if is_myne { - let air_instance_repo = &self.wcm.get_pctx().air_instance_repo; + if is_mine { + let air_instance_repo = &pctx.air_instance_repo; let instance: Vec = air_instance_repo.find_air_instances(self.airgroup_id, self.air_id); let air_instance_id = if !instance.is_empty() { - air_instance_repo.find_air_instances(self.airgroup_id, self.air_id)[0] + instance[0] } else { let num_rows = pctx.global_info.airs[self.airgroup_id][self.air_id].num_rows; let buffer_size = num_rows; let buffer: Vec = create_buffer_fast(buffer_size); - let air_instance = AirInstance::new(sctx.clone(), self.airgroup_id, self.air_id, None, buffer); - pctx.air_instance_repo.add_air_instance(air_instance, Some(global_idx)); - pctx.air_instance_repo.air_instances.read().unwrap().len() - 1 + let air_instance = AirInstance::new(TraceInfo::new(self.airgroup_id, self.air_id, buffer)); + pctx.air_instance_repo.add_air_instance(air_instance, Some(global_idx)) }; let mut air_instance_rw = air_instance_repo.air_instances.write().unwrap(); @@ -127,32 +122,34 @@ impl U8Air { let mul_column_2 = HintFieldValue::Column(multiplicity.iter().map(|x| F::from_canonical_u64(*x)).collect::>()); - set_hint_field( - &self.wcm.get_sctx(), - air_instance, - self.hint.load(Ordering::Acquire), - "reference", - &mul_column_2, - ); + set_hint_field(&sctx, air_instance, self.hint.load(Ordering::Acquire), "reference", &mul_column_2); log::trace!("{}: ··· Drained inputs for AIR '{}'", Self::MY_NAME, "U8Air"); } } fn update_multiplicity(&self, drained_inputs: Vec<(F, F)>) { + let mut mul_column = self.mul_column.lock().unwrap(); for (input, mul) in &drained_inputs { let value = input.as_canonical_biguint().to_usize().expect("Cannot convert to usize"); // Note: to avoid non-expected panics, we perform a reduction to the value // In debug mode, this is, in fact, checked before let index = value % NUM_ROWS; - let mut mul_column = self.mul_column.lock().unwrap(); mul_column.add(index, *mul); } } + + pub fn airgroup_id(&self) -> usize { + self.airgroup_id + } + + pub fn air_id(&self) -> usize { + self.air_id + } } impl WitnessComponent for U8Air { - fn start_proof(&self, pctx: Arc>, _ectx: Arc, sctx: Arc) { + fn start_proof(&self, pctx: Arc>, sctx: Arc) { // Obtain info from the mul hints let setup = sctx.get_setup(self.airgroup_id, self.air_id); let u8air_hints = get_hint_ids_by_name(setup.p_setup.p_expressions_bin, "u8air"); @@ -167,7 +164,7 @@ impl WitnessComponent for U8Air { let buffer = create_buffer_fast(buffer_size); // Add a new air instance. Since U8Air is a table, only this air instance is needed - let mut air_instance = AirInstance::new(sctx.clone(), self.airgroup_id, self.air_id, None, buffer); + let mut air_instance = AirInstance::new(TraceInfo::new(self.airgroup_id, self.air_id, buffer)); *self.mul_column.lock().unwrap() = get_hint_field::( &sctx, @@ -177,18 +174,11 @@ impl WitnessComponent for U8Air { "reference", HintFieldOptions::dest_with_zeros(), ); - - //pctx.air_instance_repo.add_air_instance(air_instance); - //note: there is room for simplification heres } - fn calculate_witness( - &self, - _stage: u32, - _air_instance: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn calculate_witness(&self, stage: u32, pctx: Arc>, sctx: Arc) { + if stage == 1 { + Self::drain_inputs(self, pctx, sctx); + } } } diff --git a/pil2-components/lib/std/rs/src/std.rs b/pil2-components/lib/std/rs/src/std.rs index e8d8486c..ad5f3557 100644 --- a/pil2-components/lib/std/rs/src/std.rs +++ b/pil2-components/lib/std/rs/src/std.rs @@ -1,46 +1,60 @@ -use std::sync::{ - atomic::{AtomicU32, Ordering}, - Arc, -}; +use std::sync::Arc; use num_bigint::BigInt; use p3_field::PrimeField; -use rayon::Scope; -use proofman::WitnessManager; -use proofman_common::ProofCtx; +use proofman_common::{ProofCtx, SetupCtx}; +use witness::WitnessManager; -use crate::{StdProd, StdRangeCheck, StdSum}; +use crate::{StdProd, StdRangeCheck, RangeCheckAir, StdSum}; pub struct Std { - range_check: Arc>, - range_check_predecessors: AtomicU32, + pub pctx: Arc>, + pub sctx: Arc, + pub range_check: Arc>, + pub std_prod: Arc>, + pub std_sum: Arc>, } impl Std { const MY_NAME: &'static str = "STD "; pub fn new(wcm: Arc>) -> Arc { - let mode = wcm.get_ectx().std_mode.clone(); - - log::info!("{}: ··· The PIL2 STD library has been initialized on mode {}", Self::MY_NAME, mode.name); + let std_mode = wcm.get_pctx().options.debug_info.std_mode.clone(); + log::info!("{}: ··· The PIL2 STD library has been initialized on mode {}", Self::MY_NAME, std_mode.name); // Instantiate the STD components - let _ = StdProd::new(mode.clone(), wcm.clone()); - let _ = StdSum::new(mode.clone(), wcm.clone()); - let range_check = StdRangeCheck::new(mode, wcm); + let std_prod = StdProd::new(std_mode.clone()); + let std_sum = StdSum::new(std_mode.clone()); + let range_check = StdRangeCheck::new(std_mode.clone(), wcm.get_pctx(), wcm.get_sctx()); - Arc::new(Self { range_check, range_check_predecessors: AtomicU32::new(0) }) - } + Self::register_std(wcm.clone(), std_prod.clone(), std_sum.clone(), range_check.clone()); - pub fn register_predecessor(&self) { - self.range_check_predecessors.fetch_add(1, Ordering::SeqCst); + Arc::new(Self { pctx: wcm.get_pctx(), sctx: wcm.get_sctx(), range_check, std_prod, std_sum }) } - pub fn unregister_predecessor(&self, pctx: Arc>, scope: Option<&Scope>) { - if self.range_check_predecessors.fetch_sub(1, Ordering::SeqCst) == 1 { - self.range_check.drain_inputs(pctx, scope); + pub fn register_std( + wcm: Arc>, + std_prod: Arc>, + std_sum: Arc>, + range_check: Arc>, + ) { + wcm.register_component(std_prod.clone()); + wcm.register_component(std_sum.clone()); + + if range_check.u8air.is_some() { + wcm.register_component(range_check.u8air.clone().unwrap()); + } + + if range_check.u16air.is_some() { + wcm.register_component(range_check.u16air.clone().unwrap()); + } + + if range_check.specified_ranges.is_some() { + wcm.register_component(range_check.specified_ranges.clone().unwrap()); } + + wcm.register_component(range_check.clone()); } /// Gets the range for the range check. @@ -52,4 +66,22 @@ impl Std { pub fn range_check(&self, val: F, multiplicity: F, id: usize) { self.range_check.assign_values(val, multiplicity, id); } + + pub fn get_ranges(&self) -> Vec<(usize, usize, RangeCheckAir)> { + self.range_check.get_ranges() + } + + pub fn drain_inputs(&self, rc_type: &RangeCheckAir) { + match rc_type { + RangeCheckAir::U8Air => { + self.range_check.u8air.as_ref().unwrap().drain_inputs(self.pctx.clone(), self.sctx.clone()); + } + RangeCheckAir::U16Air => { + self.range_check.u16air.as_ref().unwrap().drain_inputs(self.pctx.clone(), self.sctx.clone()); + } + RangeCheckAir::SpecifiedRanges => { + self.range_check.specified_ranges.as_ref().unwrap().drain_inputs(self.pctx.clone(), self.sctx.clone()); + } + }; + } } diff --git a/pil2-components/lib/std/rs/src/std_prod.rs b/pil2-components/lib/std/rs/src/std_prod.rs index 7bffe8a1..20174a9a 100644 --- a/pil2-components/lib/std/rs/src/std_prod.rs +++ b/pil2-components/lib/std/rs/src/std_prod.rs @@ -6,8 +6,8 @@ use std::{ use num_traits::ToPrimitive; use p3_field::PrimeField; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ModeName, ProofCtx, SetupCtx, StdMode}; +use witness::WitnessComponent; +use proofman_common::{AirInstance, ModeName, ProofCtx, SetupCtx, StdMode}; use proofman_hints::{ get_hint_field, get_hint_field_a, get_hint_field_constant, get_hint_field_constant_a, get_hint_ids_by_name, update_airgroupvalue, acc_mul_hint_fields, HintFieldOptions, HintFieldOutput, HintFieldValue, HintFieldValuesVec, @@ -24,22 +24,18 @@ pub struct StdProd { } impl Decider for StdProd { - fn decide(&self, sctx: Arc, pctx: Arc>) { + fn decide(&self, sctx: Arc) { // Scan the pilout for airs that have prod-related hints - for airgroup in pctx.pilout.air_groups() { - for air in airgroup.airs() { - let airgroup_id = air.airgroup_id; - let air_id = air.air_id; - - let setup = sctx.get_setup(airgroup_id, air_id); - let p_expressions_bin = setup.p_setup.p_expressions_bin; - - let gprod_hints = get_hint_ids_by_name(p_expressions_bin, "gprod_col"); - let debug_hints_data = get_hint_ids_by_name(p_expressions_bin, "gprod_member_data"); - if !gprod_hints.is_empty() { - // Save the air for latter witness computation - self.prod_airs.lock().unwrap().push((airgroup_id, air_id, gprod_hints, debug_hints_data)); - } + + for (airgroup_id, air_id) in sctx.get_setups_list() { + let setup = sctx.get_setup(airgroup_id, air_id); + let p_expressions_bin = setup.p_setup.p_expressions_bin; + + let gprod_hints = get_hint_ids_by_name(p_expressions_bin, "gprod_col"); + let debug_hints_data = get_hint_ids_by_name(p_expressions_bin, "gprod_member_data"); + if !gprod_hints.is_empty() { + // Save the air for latter witness computation + self.prod_airs.lock().unwrap().push((airgroup_id, air_id, gprod_hints, debug_hints_data)); } } } @@ -48,16 +44,12 @@ impl Decider for StdProd { impl StdProd { const MY_NAME: &'static str = "STD Prod"; - pub fn new(mode: StdMode, wcm: Arc>) -> Arc { - let std_prod = Arc::new(Self { + pub fn new(mode: StdMode) -> Arc { + Arc::new(Self { mode: mode.clone(), prod_airs: Mutex::new(Vec::new()), debug_data: if mode.name == ModeName::Debug { Some(Mutex::new(HashMap::new())) } else { None }, - }); - - wcm.register_component(std_prod.clone(), None, None); - - std_prod + }) } fn debug( @@ -95,10 +87,10 @@ impl StdProd { let opid = get_hint_field::(sctx, pctx, air_instance, *hint as usize, "opid", HintFieldOptions::default()); let opid = if let HintFieldValue::Field(opid) = opid { - if let Some(opids) = &self.mode.opids { - if !opids.contains(&opid.as_canonical_biguint().to_u64().expect("Cannot convert to u64")) { - continue; - } + if !self.mode.opids.is_empty() + && !self.mode.opids.contains(&opid.as_canonical_biguint().to_u64().expect("Cannot convert to u64")) + { + continue; } opid @@ -249,18 +241,11 @@ impl StdProd { } impl WitnessComponent for StdProd { - fn start_proof(&self, pctx: Arc>, _ectx: Arc, sctx: Arc) { - self.decide(sctx, pctx); + fn start_proof(&self, _pctx: Arc>, sctx: Arc) { + self.decide(sctx); } - fn calculate_witness( - &self, - stage: u32, - _air_instance: Option, - pctx: Arc>, - _ectx: Arc, - sctx: Arc, - ) { + fn calculate_witness(&self, stage: u32, pctx: Arc>, sctx: Arc) { if stage == 2 { let prod_airs = self.prod_airs.lock().unwrap(); @@ -271,15 +256,18 @@ impl WitnessComponent for StdProd { let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); let air_instance = &mut air_instances_vec[air_instance_id]; + if !air_instance.prover_initialized { + continue; + } + // Get the air associated with the air_instance let airgroup_id = air_instance.airgroup_id; let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); - let air_name = air.name().unwrap_or("unknown"); + let air_name = &pctx.global_info.airs[airgroup_id][air_id].name; log::debug!("{}: ··· Computing witness for AIR '{}' at stage {}", Self::MY_NAME, air_name, stage); - let num_rows = air.num_rows(); + let num_rows = pctx.global_info.airs[airgroup_id][air_id].num_rows; if self.mode.name == ModeName::Debug { self.debug(&pctx, &sctx, air_instance, num_rows, debug_hints_data.clone()); @@ -287,14 +275,14 @@ impl WitnessComponent for StdProd { // We know that at most one product hint exists let gprod_hint = if gprod_hints.len() > 1 { - panic!("Multiple product hints found for AIR '{}'", air.name().unwrap_or("unknown")); + panic!("Multiple product hints found for AIR '{}'", air_name); } else { gprod_hints[0] as usize }; // This call calculates "numerator" / "denominator" and accumulates it into "reference". Its last value is stored into "result" // Alternatively, this could be done using get_hint_field and set_hint_field methods and calculating the operations in Rust, - let (pol_id, _) = acc_mul_hint_fields::( + acc_mul_hint_fields::( &sctx, &pctx, air_instance, @@ -308,9 +296,7 @@ impl WitnessComponent for StdProd { false, ); - air_instance.set_commit_calculated(pol_id as usize); - - let airgroupvalue_id = update_airgroupvalue::( + update_airgroupvalue::( &sctx, &pctx, air_instance, @@ -322,8 +308,6 @@ impl WitnessComponent for StdProd { HintFieldOptions::inverse(), false, ); - - air_instance.set_airgroupvalue_calculated(airgroupvalue_id as usize); } } } diff --git a/pil2-components/lib/std/rs/src/std_sum.rs b/pil2-components/lib/std/rs/src/std_sum.rs index 58932385..f33ef5a4 100644 --- a/pil2-components/lib/std/rs/src/std_sum.rs +++ b/pil2-components/lib/std/rs/src/std_sum.rs @@ -6,8 +6,8 @@ use std::{ use num_traits::ToPrimitive; use p3_field::PrimeField; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx, StdMode, ModeName}; +use witness::WitnessComponent; +use proofman_common::{AirInstance, ProofCtx, SetupCtx, StdMode, ModeName}; use proofman_hints::{ get_hint_field, get_hint_field_a, get_hint_field_constant, get_hint_field_constant_a, acc_mul_hint_fields, update_airgroupvalue, get_hint_ids_by_name, mul_hint_fields, HintFieldOptions, HintFieldOutput, HintFieldValue, @@ -25,44 +25,33 @@ pub struct StdSum { } impl Decider for StdSum { - fn decide(&self, sctx: Arc, pctx: Arc>) { + fn decide(&self, sctx: Arc) { // Scan the pilout for airs that have sum-related hints - let air_groups = pctx.pilout.air_groups(); let mut sum_airs_guard = self.sum_airs.lock().unwrap(); - air_groups.iter().for_each(|air_group| { - let airs = air_group.airs(); - airs.iter().for_each(|air| { - let airgroup_id = air.airgroup_id; - let air_id = air.air_id; - - let setup = sctx.get_setup(airgroup_id, air_id); - let p_expressions_bin = setup.p_setup.p_expressions_bin; - - let im_hints = get_hint_ids_by_name(p_expressions_bin, "im_col"); - let gsum_hints = get_hint_ids_by_name(p_expressions_bin, "gsum_col"); - let debug_hints_data = get_hint_ids_by_name(p_expressions_bin, "gsum_member_data"); - if !gsum_hints.is_empty() { - // Save the air for latter witness computation - sum_airs_guard.push((airgroup_id, air_id, im_hints, gsum_hints, debug_hints_data)); - } - }); - }); + for (airgroup_id, air_id) in sctx.get_setups_list() { + let setup = sctx.get_setup(airgroup_id, air_id); + let p_expressions_bin = setup.p_setup.p_expressions_bin; + + let im_hints = get_hint_ids_by_name(p_expressions_bin, "im_col"); + let gsum_hints = get_hint_ids_by_name(p_expressions_bin, "gsum_col"); + let debug_hints_data = get_hint_ids_by_name(p_expressions_bin, "gsum_member_data"); + if !gsum_hints.is_empty() { + // Save the air for latter witness computation + sum_airs_guard.push((airgroup_id, air_id, im_hints, gsum_hints, debug_hints_data)); + } + } } } impl StdSum { const MY_NAME: &'static str = "STD Sum "; - pub fn new(mode: StdMode, wcm: Arc>) -> Arc { - let std_sum = Arc::new(Self { + pub fn new(mode: StdMode) -> Arc { + Arc::new(Self { mode: mode.clone(), sum_airs: Mutex::new(Vec::new()), debug_data: if mode.name == ModeName::Debug { Some(Mutex::new(HashMap::new())) } else { None }, - }); - - wcm.register_component(std_sum.clone(), None, None); - - std_sum + }) } fn debug( @@ -100,10 +89,10 @@ impl StdSum { let opid = get_hint_field::(sctx, pctx, air_instance, *hint as usize, "opid", HintFieldOptions::default()); if let HintFieldOutput::Field(opid) = opid.get(0) { - if let Some(opids) = &self.mode.opids { - if !opids.contains(&opid.as_canonical_biguint().to_u64().expect("Cannot convert to u64")) { - continue; - } + if !self.mode.opids.is_empty() + && !self.mode.opids.contains(&opid.as_canonical_biguint().to_u64().expect("Cannot convert to u64")) + { + continue; } } else { panic!("opid must be a field element"); @@ -250,18 +239,11 @@ impl StdSum { } impl WitnessComponent for StdSum { - fn start_proof(&self, pctx: Arc>, _ectx: Arc, sctx: Arc) { - self.decide(sctx, pctx); + fn start_proof(&self, _pctx: Arc>, sctx: Arc) { + self.decide(sctx); } - fn calculate_witness( - &self, - stage: u32, - _air_instance: Option, - pctx: Arc>, - _ectx: Arc, - sctx: Arc, - ) { + fn calculate_witness(&self, stage: u32, pctx: Arc>, sctx: Arc) { if stage == 2 { let sum_airs = self.sum_airs.lock().unwrap(); @@ -273,15 +255,18 @@ impl WitnessComponent for StdSum { let air_instance = &mut air_instaces_vec[air_instance_id]; + if !air_instance.prover_initialized { + continue; + } + // Get the air associated with the air_instance let airgroup_id = air_instance.airgroup_id; let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); - let air_name = air.name().unwrap_or("unknown"); + let air_name = &pctx.global_info.airs[airgroup_id][air_id].name; log::debug!("{}: ··· Computing witness for AIR '{}' at stage {}", Self::MY_NAME, air_name, stage); - let num_rows = air.num_rows(); + let num_rows = pctx.global_info.airs[airgroup_id][air_id].num_rows; if self.mode.name == ModeName::Debug { self.debug(&pctx, &sctx, air_instance, num_rows, debug_hints_data.clone()); @@ -289,7 +274,7 @@ impl WitnessComponent for StdSum { // Populate the im columns for hint in im_hints { - let id = mul_hint_fields::( + mul_hint_fields::( &sctx, &pctx, air_instance, @@ -300,20 +285,18 @@ impl WitnessComponent for StdSum { "denominator", HintFieldOptions::inverse(), ); - - air_instance.set_commit_calculated(id as usize); } // We know that at most one product hint exists let gsum_hint = if gsum_hints.len() > 1 { - panic!("Multiple product hints found for AIR '{}'", air.name().unwrap_or("unknown")); + panic!("Multiple product hints found for AIR '{}'", air_name); } else { gsum_hints[0] as usize }; // This call accumulates "expression" into "reference" expression and stores its last value to "result" // Alternatively, this could be done using get_hint_field and set_hint_field methods and doing the accumulation in Rust, - let (pol_id, _) = acc_mul_hint_fields::( + acc_mul_hint_fields::( &sctx, &pctx, air_instance, @@ -327,9 +310,7 @@ impl WitnessComponent for StdSum { true, ); - air_instance.set_commit_calculated(pol_id as usize); - - let airgroupvalue_id = update_airgroupvalue::( + update_airgroupvalue::( &sctx, &pctx, air_instance, @@ -341,8 +322,6 @@ impl WitnessComponent for StdSum { HintFieldOptions::inverse(), true, ); - - air_instance.set_airgroupvalue_calculated(airgroupvalue_id as usize); } } } diff --git a/pil2-components/test/simple/rs/Cargo.toml b/pil2-components/test/simple/rs/Cargo.toml index 2c452ac5..a202ebeb 100644 --- a/pil2-components/test/simple/rs/Cargo.toml +++ b/pil2-components/test/simple/rs/Cargo.toml @@ -12,6 +12,7 @@ proofman-common.workspace = true proofman-macros.workspace = true proofman-cli.workspace = true pilout.workspace = true +witness.workspace = true pil-std-lib = { path = "../../../lib/std/rs" } env_logger.workspace = true diff --git a/pil2-components/test/simple/rs/src/pil_helpers/mod.rs b/pil2-components/test/simple/rs/src/pil_helpers/mod.rs index 8273843a..8e9f77af 100644 --- a/pil2-components/test/simple/rs/src/pil_helpers/mod.rs +++ b/pil2-components/test/simple/rs/src/pil_helpers/mod.rs @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/pil2-components/test/simple/rs/src/pil_helpers/pilout.rs b/pil2-components/test/simple/rs/src/pil_helpers/pilout.rs deleted file mode 100644 index 9de28165..00000000 --- a/pil2-components/test/simple/rs/src/pil_helpers/pilout.rs +++ /dev/null @@ -1,30 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"Build-hash"; - -//AIRGROUP CONSTANTS - -pub const SIMPLE_AIRGROUP_ID: usize = 0; - -//AIR CONSTANTS - -pub const SIMPLE_LEFT_AIR_IDS: &[usize] = &[0]; - -pub const SIMPLE_RIGHT_AIR_IDS: &[usize] = &[1]; - -pub struct Pilout; - -impl Pilout { - pub fn pilout() -> WitnessPilout { - let mut pilout = WitnessPilout::new("Build", 2, PILOUT_HASH.to_vec()); - - let air_group = pilout.add_air_group(Some("Simple")); - - air_group.add_air(Some("SimpleLeft"), 4); - air_group.add_air(Some("SimpleRight"), 4); - - pilout - } -} diff --git a/pil2-components/test/simple/rs/src/pil_helpers/traces.rs b/pil2-components/test/simple/rs/src/pil_helpers/traces.rs index 8795d8ae..388900e3 100644 --- a/pil2-components/test/simple/rs/src/pil_helpers/traces.rs +++ b/pil2-components/test/simple/rs/src/pil_helpers/traces.rs @@ -2,11 +2,28 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; +pub use proofman_macros::values; -trace!(SimpleLeftRow, SimpleLeftTrace { +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"Build-hash"; + +//AIRGROUP CONSTANTS + +pub const SIMPLE_AIRGROUP_ID: usize = 0; + +//AIR CONSTANTS + +pub const SIMPLE_LEFT_AIR_IDS: &[usize] = &[0]; + +pub const SIMPLE_RIGHT_AIR_IDS: &[usize] = &[1]; + + +trace!(SimpleLeftTrace { a: F, b: F, c: F, d: F, e: F, f: F, g: F, h: F, -}); +}, 0, 0, 4 ); -trace!(SimpleRightRow, SimpleRightTrace { +trace!(SimpleRightTrace { a: F, b: F, c: F, d: F, mul: F, -}); +}, 0, 1, 4 ); diff --git a/pil2-components/test/simple/rs/src/simple_left.rs b/pil2-components/test/simple/rs/src/simple_left.rs index ba705325..1859df80 100644 --- a/pil2-components/test/simple/rs/src/simple_left.rs +++ b/pil2-components/test/simple/rs/src/simple_left.rs @@ -1,101 +1,58 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; -use p3_field::PrimeField; +use p3_field::PrimeField64; use rand::{distributions::Standard, prelude::Distribution, seq::SliceRandom}; -use crate::{SimpleLeftTrace, SIMPLE_AIRGROUP_ID, SIMPLE_LEFT_AIR_IDS}; +use crate::SimpleLeftTrace; -pub struct SimpleLeft { - _phantom: std::marker::PhantomData, -} +pub struct SimpleLeft; -impl SimpleLeft -where - Standard: Distribution, -{ +impl SimpleLeft { const MY_NAME: &'static str = "SimLeft "; - pub fn new(wcm: Arc>) -> Arc { - let simple_left = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(simple_left.clone(), Some(SIMPLE_AIRGROUP_ID), Some(SIMPLE_LEFT_AIR_IDS)); - - simple_left - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[SIMPLE_AIRGROUP_ID][SIMPLE_LEFT_AIR_IDS[0]].num_rows; - let trace = SimpleLeftTrace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), SIMPLE_AIRGROUP_ID, SIMPLE_LEFT_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(SIMPLE_AIRGROUP_ID, SIMPLE_LEFT_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for SimpleLeft +impl WitnessComponent for SimpleLeft where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - - let airgroup_id = air_instance.airgroup_id; - let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); + let mut trace = SimpleLeftTrace::new(); + let num_rows = trace.num_rows(); - log::debug!( - "{}: ··· Computing witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - if stage == 1 { - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(airgroup_id, air_id).num_rows(); + // Assumes + for i in 0..num_rows { + trace[i].a = F::from_canonical_usize(i); + trace[i].b = F::from_canonical_usize(i); - // I cannot, programatically, link the permutation trace with its air_id - let mut trace = SimpleLeftTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + trace[i].e = F::from_canonical_u8(200); + trace[i].f = F::from_canonical_u8(201); - // Assumes - for i in 0..num_rows { - trace[i].a = F::from_canonical_usize(i); - trace[i].b = F::from_canonical_usize(i); - - trace[i].e = F::from_canonical_u8(200); - trace[i].f = F::from_canonical_u8(201); - - trace[i].g = F::from_canonical_usize(i); - trace[i].h = F::from_canonical_usize(num_rows - i - 1); - } + trace[i].g = F::from_canonical_usize(i); + trace[i].h = F::from_canonical_usize(num_rows - i - 1); + } - let mut indices: Vec = (0..num_rows).collect(); - indices.shuffle(&mut rng); + let mut indices: Vec = (0..num_rows).collect(); + indices.shuffle(&mut rng); - // Proves - for i in 0..num_rows { - // We take a random permutation of the indices to show that the permutation check is passing - trace[i].c = trace[indices[i]].a; - trace[i].d = trace[indices[i]].b; - } + // Proves + for i in 0..num_rows { + // We take a random permutation of the indices to show that the permutation check is passing + trace[i].c = trace[indices[i]].a; + trace[i].d = trace[indices[i]].b; } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/simple/rs/src/simple_lib.rs b/pil2-components/test/simple/rs/src/simple_lib.rs index 69ecb24e..c1df9db5 100644 --- a/pil2-components/test/simple/rs/src/simple_lib.rs +++ b/pil2-components/test/simple/rs/src/simple_lib.rs @@ -1,89 +1,25 @@ -use std::{error::Error, path::PathBuf, sync::Arc}; - +use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessLibrary, WitnessManager}; -use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; +use witness::{witness_library, WitnessLibrary, WitnessManager}; -use p3_field::PrimeField; +use p3_field::PrimeField64; use p3_goldilocks::Goldilocks; use rand::{distributions::Standard, prelude::Distribution}; -use crate::{Pilout, SimpleLeft, SimpleRight}; - -pub struct SimpleWitness { - pub wcm: Option>>, - pub simple_left: Option>>, - pub simple_right: Option>>, - pub std_lib: Option>>, -} - -impl Default for SimpleWitness -where - Standard: Distribution, -{ - fn default() -> Self { - Self::new() - } -} - -impl SimpleWitness -where - Standard: Distribution, -{ - pub fn new() -> Self { - Self { wcm: None, simple_left: None, simple_right: None, std_lib: None } - } - - pub fn initialize(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); - - let std_lib = Std::new(wcm.clone()); - let simple_left = SimpleLeft::new(wcm.clone()); - let simple_right = SimpleRight::new(wcm.clone()); +use crate::{SimpleLeft, SimpleRight}; - self.wcm = Some(wcm); - self.std_lib = Some(std_lib); - self.simple_left = Some(simple_left); - self.simple_right = Some(simple_right); - } -} +witness_library!(WitnessLib, Goldilocks); -impl WitnessLibrary for SimpleWitness +impl WitnessLibrary for WitnessLib where Standard: Distribution, { - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); - - self.wcm.as_ref().unwrap().start_proof(pctx, ectx, sctx); - } - - fn end_proof(&mut self) { - self.wcm.as_ref().unwrap().end_proof(); - } - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // Execute those components that need to be executed - self.simple_left.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.simple_right.as_ref().unwrap().execute(pctx, ectx, sctx); - } + fn register_witness(&mut self, wcm: Arc>) { + Std::new(wcm.clone()); + let simple_left = SimpleLeft::new(); + let simple_right = SimpleRight::new(); - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); + wcm.register_component(simple_left.clone()); + wcm.register_component(simple_right.clone()); } - - fn pilout(&self) -> WitnessPilout { - Pilout::pilout() - } -} - -#[no_mangle] -pub extern "Rust" fn init_library( - _: Option, - _: Option, - verbose_mode: VerboseMode, -) -> Result>, Box> { - initialize_logger(verbose_mode); - let simple_witness = SimpleWitness::new(); - Ok(Box::new(simple_witness)) } diff --git a/pil2-components/test/simple/rs/src/simple_right.rs b/pil2-components/test/simple/rs/src/simple_right.rs index 327f050a..e66c2434 100644 --- a/pil2-components/test/simple/rs/src/simple_right.rs +++ b/pil2-components/test/simple/rs/src/simple_right.rs @@ -1,87 +1,45 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; -use p3_field::PrimeField; +use p3_field::PrimeField64; use rand::{distributions::Standard, prelude::Distribution}; -use crate::{SimpleRightTrace, SIMPLE_AIRGROUP_ID, SIMPLE_RIGHT_AIR_IDS}; +use crate::SimpleRightTrace; -pub struct SimpleRight { - _phantom: std::marker::PhantomData, -} +pub struct SimpleRight; -impl SimpleRight -where - Standard: Distribution, -{ +impl SimpleRight { const MY_NAME: &'static str = "SimRight"; - pub fn new(wcm: Arc>) -> Arc { - let simple_right = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(simple_right.clone(), Some(SIMPLE_AIRGROUP_ID), Some(SIMPLE_RIGHT_AIR_IDS)); - - simple_right - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[SIMPLE_AIRGROUP_ID][SIMPLE_RIGHT_AIR_IDS[0]].num_rows; - let trace = SimpleRightTrace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), SIMPLE_AIRGROUP_ID, SIMPLE_RIGHT_AIR_IDS[0], None, trace.buffer.unwrap()); - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(SIMPLE_AIRGROUP_ID, SIMPLE_RIGHT_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for SimpleRight +impl WitnessComponent for SimpleRight where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - - let airgroup_id = air_instance.airgroup_id; - let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); + fn execute(&self, pctx: Arc>) { + let mut trace = SimpleRightTrace::new(); + let num_rows = trace.num_rows(); - log::debug!( - "{}: ··· Computing witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - if stage == 1 { - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(airgroup_id, air_id).num_rows(); + // Proves + for i in 0..num_rows { + trace[i].a = F::from_canonical_u8(200); + trace[i].b = F::from_canonical_u8(201); - // I cannot, programatically, link the permutation trace with its air_id - let mut trace = SimpleRightTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + trace[i].c = F::from_canonical_usize(i); + trace[i].d = F::from_canonical_usize(num_rows - i - 1); - // Proves - for i in 0..num_rows { - trace[i].a = F::from_canonical_u8(200); - trace[i].b = F::from_canonical_u8(201); - - trace[i].c = F::from_canonical_usize(i); - trace[i].d = F::from_canonical_usize(num_rows - i - 1); - - trace[i].mul = F::from_canonical_usize(1); - } + trace[i].mul = F::from_canonical_usize(1); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/connection/rs/Cargo.toml b/pil2-components/test/std/connection/rs/Cargo.toml index 901af7a4..35c23887 100644 --- a/pil2-components/test/std/connection/rs/Cargo.toml +++ b/pil2-components/test/std/connection/rs/Cargo.toml @@ -11,6 +11,7 @@ proofman.workspace = true proofman-common.workspace = true proofman-macros.workspace = true proofman-cli.workspace = true +witness.workspace = true pil-std-lib = { path = "../../../../lib/std/rs" } env_logger.workspace = true diff --git a/pil2-components/test/std/connection/rs/src/connection1.rs b/pil2-components/test/std/connection/rs/src/connection1.rs index 3bfa98a3..9749d33a 100644 --- a/pil2-components/test/std/connection/rs/src/connection1.rs +++ b/pil2-components/test/std/connection/rs/src/connection1.rs @@ -1,87 +1,42 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Connection1Trace, CONNECTION_1_AIR_IDS, CONNECTION_AIRGROUP_ID}; +use crate::Connection1Trace; -pub struct Connection1 { - _phantom: std::marker::PhantomData, -} +pub struct Connection1; -impl Connection1 -where - Standard: Distribution, -{ +impl Connection1 { const MY_NAME: &'static str = "Connct_1"; - pub fn new(wcm: Arc>) -> Arc { - let connection1 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(connection1.clone(), Some(CONNECTION_AIRGROUP_ID), Some(CONNECTION_1_AIR_IDS)); - - connection1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // For simplicity, add a single instance of each air - let num_rows = pctx.global_info.airs[CONNECTION_AIRGROUP_ID][CONNECTION_1_AIR_IDS[0]].num_rows; - let trace = Connection1Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - CONNECTION_AIRGROUP_ID, - CONNECTION_1_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(CONNECTION_AIRGROUP_ID, CONNECTION_1_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Connection1 +impl WitnessComponent for Connection1 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); + let mut trace = Connection1Trace::new_zeroes(); + let num_rows = trace.num_rows(); - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(CONNECTION_AIRGROUP_ID, CONNECTION_1_AIR_IDS[0]).num_rows(); - let mut trace = Connection1Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - for i in 0..num_rows { - trace[i].a = rng.gen(); - trace[i].b = rng.gen(); - trace[i].c = rng.gen(); - } + for i in 0..num_rows { + trace[i].a = rng.gen(); + trace[i].b = rng.gen(); + trace[i].c = rng.gen(); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/connection/rs/src/connection2.rs b/pil2-components/test/std/connection/rs/src/connection2.rs index 6db92acb..0cf0c005 100644 --- a/pil2-components/test/std/connection/rs/src/connection2.rs +++ b/pil2-components/test/std/connection/rs/src/connection2.rs @@ -1,88 +1,44 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Connection2Trace, CONNECTION_2_AIR_IDS, CONNECTION_AIRGROUP_ID}; +use crate::Connection2Trace; -pub struct Connection2 { - _phantom: std::marker::PhantomData, -} +pub struct Connection2; -impl Connection2 -where - Standard: Distribution, -{ +impl Connection2 { const MY_NAME: &'static str = "Connct_2"; - pub fn new(wcm: Arc>) -> Arc { - let connection2 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(connection2.clone(), Some(CONNECTION_AIRGROUP_ID), Some(CONNECTION_2_AIR_IDS)); - - connection2 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[CONNECTION_AIRGROUP_ID][CONNECTION_2_AIR_IDS[0]].num_rows; - let trace = Connection2Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - CONNECTION_AIRGROUP_ID, - CONNECTION_2_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(CONNECTION_AIRGROUP_ID, CONNECTION_2_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Connection2 +impl WitnessComponent for Connection2 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); + let mut trace = Connection2Trace::new_zeroes(); + let num_rows = trace.num_rows(); - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(CONNECTION_AIRGROUP_ID, CONNECTION_2_AIR_IDS[0]).num_rows(); - let mut trace = Connection2Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + for i in 0..num_rows { + trace[i].a = rng.gen(); + trace[i].b = rng.gen(); + trace[i].c = rng.gen(); + } - for i in 0..num_rows { - trace[i].a = rng.gen(); - trace[i].b = rng.gen(); - trace[i].c = rng.gen(); - } + trace[0].a = trace[1].a; - trace[0].a = trace[1].a; - } + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/connection/rs/src/connection_lib.rs b/pil2-components/test/std/connection/rs/src/connection_lib.rs index 0b601a02..cd88f982 100644 --- a/pil2-components/test/std/connection/rs/src/connection_lib.rs +++ b/pil2-components/test/std/connection/rs/src/connection_lib.rs @@ -1,118 +1,28 @@ -use std::{error::Error, path::PathBuf, sync::Arc}; +use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessLibrary, WitnessManager}; -use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; +use witness::{witness_library, WitnessLibrary, WitnessManager}; use p3_field::PrimeField; use p3_goldilocks::Goldilocks; use rand::{distributions::Standard, prelude::Distribution}; -use crate::{Connection1, Connection2, ConnectionNew, Pilout}; +use crate::{Connection1, Connection2, ConnectionNew}; -pub struct ConnectionWitness { - pub wcm: Option>>, - pub connection1: Option>>, - pub connection2: Option>>, - pub connection_new: Option>>, - pub std_lib: Option>>, -} - -impl Default for ConnectionWitness -where - Standard: Distribution, -{ - fn default() -> Self { - Self::new() - } -} +witness_library!(WitnessLib, Goldilocks); -impl ConnectionWitness +impl WitnessLibrary for WitnessLib where Standard: Distribution, { - pub fn new() -> Self { - ConnectionWitness { wcm: None, connection1: None, connection2: None, connection_new: None, std_lib: None } - } + fn register_witness(&mut self, wcm: Arc>) { + Std::new(wcm.clone()); + let connection1 = Connection1::new(); + let connection2 = Connection2::new(); + let connection_new = ConnectionNew::new(); - pub fn initialize(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); - - let std_lib = Std::new(wcm.clone()); - let connection1 = Connection1::new(wcm.clone()); - let connection2 = Connection2::new(wcm.clone()); - let connection_new = ConnectionNew::new(wcm.clone()); - - self.wcm = Some(wcm); - self.connection1 = Some(connection1); - self.connection2 = Some(connection2); - self.connection_new = Some(connection_new); - self.std_lib = Some(std_lib); + wcm.register_component(connection1.clone()); + wcm.register_component(connection2.clone()); + wcm.register_component(connection_new.clone()); } } - -impl WitnessLibrary for ConnectionWitness -where - Standard: Distribution, -{ - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); - - self.wcm.as_ref().unwrap().start_proof(pctx, ectx, sctx); - } - - fn end_proof(&mut self) { - self.wcm.as_ref().unwrap().end_proof(); - } - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // Execute those components that need to be executed - self.connection1.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.connection2.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.connection_new.as_ref().unwrap().execute(pctx, ectx, sctx); - } - - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); - } - - fn pilout(&self) -> WitnessPilout { - Pilout::pilout() - } -} - -#[no_mangle] -pub extern "Rust" fn init_library( - _: Option, - _: Option, - verbose_mode: VerboseMode, -) -> Result>, Box> { - initialize_logger(verbose_mode); - let connection_witness: ConnectionWitness = ConnectionWitness::new(); - Ok(Box::new(connection_witness)) -} - -// #[cfg(test)] -// mod tests { -// use proofman_cli::commands::verify_constraints::{Field, VerifyConstraintsCmd}; - -// #[test] -// fn test_verify_constraints() { -// let root_path = std::env::current_dir().expect("Failed to get current directory").join("../../../../"); -// let root_path = std::fs::canonicalize(root_path).expect("Failed to canonicalize root path"); - -// let verify_constraints = VerifyConstraintsCmd { -// witness_lib: root_path.join("target/debug/libconnection.so"), -// rom: None, -// public_inputs: None, -// proving_key: root_path.join("test/std/connection/build/provingKey"), -// field: Field::Goldilocks, -// verbose: 0, -// }; - -// if let Err(e) = verify_constraints.run() { -// eprintln!("Failed to verify constraints: {:?}", e); -// std::process::exit(1); -// } -// } -// } diff --git a/pil2-components/test/std/connection/rs/src/connection_new.rs b/pil2-components/test/std/connection/rs/src/connection_new.rs index 418e42c4..b4c2fbf1 100644 --- a/pil2-components/test/std/connection/rs/src/connection_new.rs +++ b/pil2-components/test/std/connection/rs/src/connection_new.rs @@ -1,180 +1,135 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{ConnectionNewTrace, CONNECTION_AIRGROUP_ID, CONNECTION_NEW_AIR_IDS}; +use crate::ConnectionNewTrace; -pub struct ConnectionNew { - _phantom: std::marker::PhantomData, -} +pub struct ConnectionNew; -impl ConnectionNew -where - Standard: Distribution, -{ +impl ConnectionNew { const MY_NAME: &'static str = "Connct_N"; - pub fn new(wcm: Arc>) -> Arc { - let connection_new = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(connection_new.clone(), Some(CONNECTION_AIRGROUP_ID), Some(CONNECTION_NEW_AIR_IDS)); - - connection_new - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[CONNECTION_AIRGROUP_ID][CONNECTION_NEW_AIR_IDS[0]].num_rows; - let trace = ConnectionNewTrace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - CONNECTION_AIRGROUP_ID, - CONNECTION_NEW_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(CONNECTION_AIRGROUP_ID, CONNECTION_NEW_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for ConnectionNew +impl WitnessComponent for ConnectionNew where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); + let mut trace = ConnectionNewTrace::new_zeroes(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + let mut frame = [0; 6]; + let mut conn_len = [0; 6]; + for i in 0..num_rows { + // Start connection + trace[i].a[0] = rng.gen(); + trace[i].b[0] = rng.gen(); + trace[i].c[0] = rng.gen(); + + // Start connection + trace[i].a[1] = rng.gen(); + trace[i].b[1] = rng.gen(); + trace[i].c[1] = rng.gen(); + if i == 3 + frame[1] { + trace[i - 1].c[1] = trace[i].c[1]; + frame[1] += num_rows / 2; + } + + // TODO: Finish! + // // Start connection + // trace[i].a[2] = rng.gen(); + // trace[i].b[2] = rng.gen(); + // trace[i].c[2] = rng.gen(); + // if i == 3 + frame[2] { + // trace[i - 1].c[2] = trace[i].c[2]; + + // trace[0 + frame[2]].c[2] = trace[i].b[2]; + // trace[1 + frame[2]].a[2] = trace[i].b[2]; + // conn_len[2] += 2; + // } + + // if i == 3 + frame[2] { + // trace[i - 1].c[2] = trace[i].c[2]; + + // trace[0 + frame[2]].c[2] = trace[i].b[2]; + // trace[1 + frame[2]].a[2] = trace[i].b[2]; + // conn_len[2] += 2; + // } + + // if conn_len[2] == 3 { + // frame[2] += num_rows / 4; + // conn_len[2] = 0; + // } + + // Start connection + trace[i].a[3] = rng.gen(); + trace[i].b[3] = rng.gen(); + trace[i].c[3] = rng.gen(); + if i == 2 + frame[3] { + trace[i - 1].c[3] = trace[i].a[3]; + frame[3] += num_rows / 2; + } + + if i == 3 { + trace[i - 3].c[3] = trace[i].b[3]; + trace[i - 2].a[3] = trace[i - 3].c[3]; + } + + // Start connection + trace[i].a[4] = rng.gen(); + trace[i].b[4] = rng.gen(); + trace[i].c[4] = rng.gen(); + + if i == 2 + frame[4] { + trace[i - 1].d[4] = trace[i - 1].b[4]; + trace[i - 1].a[4] = trace[i].c[4]; + conn_len[4] += 1; + } - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(CONNECTION_AIRGROUP_ID, CONNECTION_NEW_AIR_IDS[0]).num_rows(); - let mut trace = ConnectionNewTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - let mut frame = [0; 6]; - let mut conn_len = [0; 6]; - for i in 0..num_rows { - // Start connection - trace[i].a[0] = rng.gen(); - trace[i].b[0] = rng.gen(); - trace[i].c[0] = rng.gen(); - - // Start connection - trace[i].a[1] = rng.gen(); - trace[i].b[1] = rng.gen(); - trace[i].c[1] = rng.gen(); - if i == 3 + frame[1] { - trace[i - 1].c[1] = trace[i].c[1]; - frame[1] += num_rows / 2; - } - - // TODO: Finish! - // // Start connection - // trace[i].a[2] = rng.gen(); - // trace[i].b[2] = rng.gen(); - // trace[i].c[2] = rng.gen(); - // if i == 3 + frame[2] { - // trace[i - 1].c[2] = trace[i].c[2]; - - // trace[0 + frame[2]].c[2] = trace[i].b[2]; - // trace[1 + frame[2]].a[2] = trace[i].b[2]; - // conn_len[2] += 2; - // } - - // if i == 3 + frame[2] { - // trace[i - 1].c[2] = trace[i].c[2]; - - // trace[0 + frame[2]].c[2] = trace[i].b[2]; - // trace[1 + frame[2]].a[2] = trace[i].b[2]; - // conn_len[2] += 2; - // } - - // if conn_len[2] == 3 { - // frame[2] += num_rows / 4; - // conn_len[2] = 0; - // } - - // Start connection - trace[i].a[3] = rng.gen(); - trace[i].b[3] = rng.gen(); - trace[i].c[3] = rng.gen(); - if i == 2 + frame[3] { - trace[i - 1].c[3] = trace[i].a[3]; - frame[3] += num_rows / 2; - } - - if i == 3 { - trace[i - 3].c[3] = trace[i].b[3]; - trace[i - 2].a[3] = trace[i - 3].c[3]; - } - - // Start connection - trace[i].a[4] = rng.gen(); - trace[i].b[4] = rng.gen(); - trace[i].c[4] = rng.gen(); - - if i == 2 + frame[4] { - trace[i - 1].d[4] = trace[i - 1].b[4]; - trace[i - 1].a[4] = trace[i].c[4]; - conn_len[4] += 1; - } - - if i == 3 + frame[4] { - trace[i - 1].b[4] = trace[i].a[4]; - trace[i].c[4] = trace[i - 1].b[4]; - conn_len[4] += 1; - } - - if conn_len[4] == 2 { - frame[4] += num_rows / 2; - conn_len[4] = 0; - } - - // Start connection - trace[i].a[5] = rng.gen(); - trace[i].b[5] = rng.gen(); - trace[i].c[5] = rng.gen(); - if i == 3 + frame[5] { - trace[i - 1].d[5] = trace[i].d[5]; - trace[i - 3].b[5] = trace[i].d[5]; - conn_len[5] += 2; - } - - if i == 8 { - trace[5].b[5] = trace[i].c[5]; - trace[1].a[5] = trace[i].c[5]; - } - - if conn_len[5] == 2 { - frame[5] += num_rows / 2; - conn_len[5] = 0; - } + if i == 3 + frame[4] { + trace[i - 1].b[4] = trace[i].a[4]; + trace[i].c[4] = trace[i - 1].b[4]; + conn_len[4] += 1; + } + + if conn_len[4] == 2 { + frame[4] += num_rows / 2; + conn_len[4] = 0; + } + + // Start connection + trace[i].a[5] = rng.gen(); + trace[i].b[5] = rng.gen(); + trace[i].c[5] = rng.gen(); + if i == 3 + frame[5] { + trace[i - 1].d[5] = trace[i].d[5]; + trace[i - 3].b[5] = trace[i].d[5]; + conn_len[5] += 2; + } + + if i == 8 { + trace[5].b[5] = trace[i].c[5]; + trace[1].a[5] = trace[i].c[5]; + } + + if conn_len[5] == 2 { + frame[5] += num_rows / 2; + conn_len[5] = 0; } } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/connection/rs/src/pil_helpers/mod.rs b/pil2-components/test/std/connection/rs/src/pil_helpers/mod.rs index 8273843a..8e9f77af 100644 --- a/pil2-components/test/std/connection/rs/src/pil_helpers/mod.rs +++ b/pil2-components/test/std/connection/rs/src/pil_helpers/mod.rs @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/pil2-components/test/std/connection/rs/src/pil_helpers/pilout.rs b/pil2-components/test/std/connection/rs/src/pil_helpers/pilout.rs deleted file mode 100644 index 7e43c745..00000000 --- a/pil2-components/test/std/connection/rs/src/pil_helpers/pilout.rs +++ /dev/null @@ -1,33 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"Build-hash"; - -//AIRGROUP CONSTANTS - -pub const CONNECTION_AIRGROUP_ID: usize = 0; - -//AIR CONSTANTS - -pub const CONNECTION_1_AIR_IDS: &[usize] = &[0]; - -pub const CONNECTION_2_AIR_IDS: &[usize] = &[1]; - -pub const CONNECTION_NEW_AIR_IDS: &[usize] = &[2]; - -pub struct Pilout; - -impl Pilout { - pub fn pilout() -> WitnessPilout { - let mut pilout = WitnessPilout::new("Build", 2, PILOUT_HASH.to_vec()); - - let air_group = pilout.add_air_group(Some("Connection")); - - air_group.add_air(Some("Connection1"), 8); - air_group.add_air(Some("Connection2"), 16); - air_group.add_air(Some("ConnectionNew"), 16); - - pilout - } -} diff --git a/pil2-components/test/std/connection/rs/src/pil_helpers/traces.rs b/pil2-components/test/std/connection/rs/src/pil_helpers/traces.rs index 1f7bf5dc..35fd4886 100644 --- a/pil2-components/test/std/connection/rs/src/pil_helpers/traces.rs +++ b/pil2-components/test/std/connection/rs/src/pil_helpers/traces.rs @@ -2,15 +2,34 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; +pub use proofman_macros::values; -trace!(Connection1Row, Connection1Trace { +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"Build-hash"; + +//AIRGROUP CONSTANTS + +pub const CONNECTION_AIRGROUP_ID: usize = 0; + +//AIR CONSTANTS + +pub const CONNECTION_1_AIR_IDS: &[usize] = &[0]; + +pub const CONNECTION_2_AIR_IDS: &[usize] = &[1]; + +pub const CONNECTION_NEW_AIR_IDS: &[usize] = &[2]; + + +trace!(Connection1Trace { a: F, b: F, c: F, -}); +}, 0, 0, 8 ); -trace!(Connection2Row, Connection2Trace { +trace!(Connection2Trace { a: F, b: F, c: F, -}); +}, 0, 1, 16 ); -trace!(ConnectionNewRow, ConnectionNewTrace { +trace!(ConnectionNewTrace { a: [F; 6], b: [F; 6], c: [F; 6], d: [F; 6], -}); +}, 0, 2, 16 ); diff --git a/pil2-components/test/std/lookup/rs/Cargo.toml b/pil2-components/test/std/lookup/rs/Cargo.toml index e944886c..ee7be23a 100644 --- a/pil2-components/test/std/lookup/rs/Cargo.toml +++ b/pil2-components/test/std/lookup/rs/Cargo.toml @@ -11,6 +11,7 @@ proofman.workspace = true proofman-common.workspace = true proofman-macros.workspace = true proofman-cli.workspace = true +witness.workspace = true pil-std-lib = { path = "../../../../lib/std/rs" } env_logger.workspace = true diff --git a/pil2-components/test/std/lookup/rs/src/lookup0.rs b/pil2-components/test/std/lookup/rs/src/lookup0.rs index dfc622fd..493e74ae 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup0.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup0.rs @@ -1,94 +1,55 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Lookup0Trace, LOOKUP_0_AIR_IDS, LOOKUP_AIRGROUP_ID}; +use crate::Lookup0Trace; -pub struct Lookup0 { - _phantom: std::marker::PhantomData, -} - -impl Lookup0 -where - Standard: Distribution, -{ - const MY_NAME: &'static str = "Lookup0"; - - pub fn new(wcm: Arc>) -> Arc { - let lookup0 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(lookup0.clone(), Some(LOOKUP_AIRGROUP_ID), Some(LOOKUP_0_AIR_IDS)); +pub struct Lookup0; - lookup0 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[LOOKUP_AIRGROUP_ID][LOOKUP_0_AIR_IDS[0]].num_rows; - let trace = Lookup0Trace::new(num_rows); +impl Lookup0 { + const MY_NAME: &'static str = "Lookup_0"; - let air_instance = - AirInstance::new(sctx.clone(), LOOKUP_AIRGROUP_ID, LOOKUP_0_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(LOOKUP_AIRGROUP_ID, LOOKUP_0_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Lookup0 +impl WitnessComponent for Lookup0 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); + let mut trace = Lookup0Trace::new_zeroes(); + let num_rows = trace.num_rows(); - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - if stage == 1 { - let buffer = &mut air_instance.trace; + let num_lookups = trace[0].sel.len(); - let num_rows = pctx.pilout.get_air(LOOKUP_AIRGROUP_ID, LOOKUP_0_AIR_IDS[0]).num_rows(); - let mut trace = Lookup0Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + for j in 0..num_lookups { + for i in 0..num_rows { + // Assumes + trace[i].f[2 * j] = rng.gen(); + trace[i].f[2 * j + 1] = rng.gen(); + let selected = rng.gen_bool(0.5); + trace[i].sel[j] = F::from_bool(selected); - let num_lookups = trace[0].sel.len(); - - for j in 0..num_lookups { - for i in 0..num_rows { - // Assumes - trace[i].f[2 * j] = rng.gen(); - trace[i].f[2 * j + 1] = rng.gen(); - let selected = rng.gen_bool(0.5); - trace[i].sel[j] = F::from_bool(selected); - - // Proves - trace[i].t[2 * j] = trace[i].f[2 * j]; - trace[i].t[2 * j + 1] = trace[i].f[2 * j + 1]; - if selected { - trace[i].mul[j] = F::one(); - } + // Proves + trace[i].t[2 * j] = trace[i].f[2 * j]; + trace[i].t[2 * j + 1] = trace[i].f[2 * j + 1]; + if selected { + trace[i].mul[j] = F::one(); } } } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/lookup/rs/src/lookup1.rs b/pil2-components/test/std/lookup/rs/src/lookup1.rs index da0ea753..19a08962 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup1.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup1.rs @@ -1,92 +1,53 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Lookup1Trace, LOOKUP_1_AIR_IDS, LOOKUP_AIRGROUP_ID}; +use crate::Lookup1Trace; -pub struct Lookup1 { - _phantom: std::marker::PhantomData, -} +pub struct Lookup1; -impl Lookup1 -where - Standard: Distribution, -{ +impl Lookup1 { const MY_NAME: &'static str = "Lookup_1"; - pub fn new(wcm: Arc>) -> Arc { - let lookup1 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(lookup1.clone(), Some(LOOKUP_AIRGROUP_ID), Some(LOOKUP_1_AIR_IDS)); - - lookup1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[LOOKUP_AIRGROUP_ID][LOOKUP_1_AIR_IDS[0]].num_rows; - let trace = Lookup1Trace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), LOOKUP_AIRGROUP_ID, LOOKUP_1_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(LOOKUP_AIRGROUP_ID, LOOKUP_1_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Lookup1 +impl WitnessComponent for Lookup1 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); + let mut trace = Lookup1Trace::new(); + let num_rows = trace.num_rows(); - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - if stage == 1 { - let buffer = &mut air_instance.trace; + let num_lookups = trace[0].sel.len(); - let num_rows = pctx.pilout.get_air(LOOKUP_AIRGROUP_ID, LOOKUP_1_AIR_IDS[0]).num_rows(); - let mut trace = Lookup1Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - let num_lookups = trace[0].sel.len(); - - for i in 0..num_rows { - let val = rng.gen(); - let mut n_sel = 0; - for j in 0..num_lookups { - trace[i].f[j] = val; - let selected = rng.gen_bool(0.5); - trace[i].sel[j] = F::from_bool(selected); - if selected { - n_sel += 1; - } + for i in 0..num_rows { + let val = rng.gen(); + let mut n_sel = 0; + for j in 0..num_lookups { + trace[i].f[j] = val; + let selected = rng.gen_bool(0.5); + trace[i].sel[j] = F::from_bool(selected); + if selected { + n_sel += 1; } - trace[i].t = val; - trace[i].mul = F::from_canonical_usize(n_sel); } + trace[i].t = val; + trace[i].mul = F::from_canonical_usize(n_sel); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/lookup/rs/src/lookup2_12.rs b/pil2-components/test/std/lookup/rs/src/lookup2_12.rs index 156f6423..66c5d5af 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup2_12.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup2_12.rs @@ -1,104 +1,65 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Lookup2_12Trace, LOOKUP_2_12_AIR_IDS, LOOKUP_AIRGROUP_ID}; +use crate::Lookup2_12Trace; -pub struct Lookup2_12 { - _phantom: std::marker::PhantomData, -} +pub struct Lookup2_12; -impl Lookup2_12 -where - Standard: Distribution, -{ +impl Lookup2_12 { const MY_NAME: &'static str = "Lkup2_12"; - pub fn new(wcm: Arc>) -> Arc { - let lookup2_12 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(lookup2_12.clone(), Some(LOOKUP_AIRGROUP_ID), Some(LOOKUP_2_12_AIR_IDS)); - - lookup2_12 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[LOOKUP_AIRGROUP_ID][LOOKUP_2_12_AIR_IDS[0]].num_rows; - let trace = Lookup2_12Trace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), LOOKUP_AIRGROUP_ID, LOOKUP_2_12_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(LOOKUP_AIRGROUP_ID, LOOKUP_2_12_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Lookup2_12 +impl WitnessComponent for Lookup2_12 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(LOOKUP_AIRGROUP_ID, LOOKUP_2_12_AIR_IDS[0]).num_rows(); - let mut trace = Lookup2_12Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - // TODO: Add the ability to send inputs to lookup3 - // and consequently add random selectors - - for i in 0..num_rows { - // Inner lookups - trace[i].a1 = rng.gen(); - trace[i].b1 = rng.gen(); - trace[i].c1 = trace[i].a1; - trace[i].d1 = trace[i].b1; - - trace[i].a3 = rng.gen(); - trace[i].b3 = rng.gen(); - trace[i].c2 = trace[i].a3; - trace[i].d2 = trace[i].b3; - let selected = rng.gen_bool(0.5); - trace[i].sel1 = F::from_bool(selected); - if selected { - trace[i].mul = trace[i].sel1; - } + let mut trace = Lookup2_12Trace::new(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + // TODO: Add the ability to send inputs to lookup3 + // and consequently add random selectors + + for i in 0..num_rows { + // Inner lookups + trace[i].a1 = rng.gen(); + trace[i].b1 = rng.gen(); + trace[i].c1 = trace[i].a1; + trace[i].d1 = trace[i].b1; + + trace[i].a3 = rng.gen(); + trace[i].b3 = rng.gen(); + trace[i].c2 = trace[i].a3; + trace[i].d2 = trace[i].b3; + let selected = rng.gen_bool(0.5); + trace[i].sel1 = F::from_bool(selected); + if selected { + trace[i].mul = trace[i].sel1; + } - // Outer lookups - trace[i].a2 = F::from_canonical_usize(i); - trace[i].b2 = F::from_canonical_usize(i); + // Outer lookups + trace[i].a2 = F::from_canonical_usize(i); + trace[i].b2 = F::from_canonical_usize(i); - trace[i].a4 = F::from_canonical_usize(i); - trace[i].b4 = F::from_canonical_usize(i); - trace[i].sel2 = F::from_bool(true); - } + trace[i].a4 = F::from_canonical_usize(i); + trace[i].b4 = F::from_canonical_usize(i); + trace[i].sel2 = F::from_bool(true); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/lookup/rs/src/lookup2_13.rs b/pil2-components/test/std/lookup/rs/src/lookup2_13.rs index 225de754..7cfb8aab 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup2_13.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup2_13.rs @@ -1,104 +1,65 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Lookup2_13Trace, LOOKUP_2_13_AIR_IDS, LOOKUP_AIRGROUP_ID}; +use crate::Lookup2_13Trace; -pub struct Lookup2_13 { - _phantom: std::marker::PhantomData, -} +pub struct Lookup2_13; -impl Lookup2_13 -where - Standard: Distribution, -{ +impl Lookup2_13 { const MY_NAME: &'static str = "Lkup2_13"; - pub fn new(wcm: Arc>) -> Arc { - let lookup2_13 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(lookup2_13.clone(), Some(LOOKUP_AIRGROUP_ID), Some(LOOKUP_2_13_AIR_IDS)); - - lookup2_13 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[LOOKUP_AIRGROUP_ID][LOOKUP_2_13_AIR_IDS[0]].num_rows; - let trace = Lookup2_13Trace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), LOOKUP_AIRGROUP_ID, LOOKUP_2_13_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(LOOKUP_AIRGROUP_ID, LOOKUP_2_13_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Lookup2_13 +impl WitnessComponent for Lookup2_13 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(LOOKUP_AIRGROUP_ID, LOOKUP_2_13_AIR_IDS[0]).num_rows(); - let mut trace = Lookup2_13Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - // TODO: Add the ability to send inputs to lookup3 - // and consequently add random selectors - - for i in 0..num_rows { - // Inner lookups - trace[i].a1 = rng.gen(); - trace[i].b1 = rng.gen(); - trace[i].c1 = trace[i].a1; - trace[i].d1 = trace[i].b1; - - trace[i].a3 = rng.gen(); - trace[i].b3 = rng.gen(); - trace[i].c2 = trace[i].a3; - trace[i].d2 = trace[i].b3; - let selected = rng.gen_bool(0.5); - trace[i].sel1 = F::from_bool(selected); - if selected { - trace[i].mul = trace[i].sel1; - } + let mut trace = Lookup2_13Trace::new(); + let num_rows = trace.num_rows(); + + // TODO: Add the ability to send inputs to lookup3 + // and consequently add random selectors + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + for i in 0..num_rows { + // Inner lookups + trace[i].a1 = rng.gen(); + trace[i].b1 = rng.gen(); + trace[i].c1 = trace[i].a1; + trace[i].d1 = trace[i].b1; + + trace[i].a3 = rng.gen(); + trace[i].b3 = rng.gen(); + trace[i].c2 = trace[i].a3; + trace[i].d2 = trace[i].b3; + let selected = rng.gen_bool(0.5); + trace[i].sel1 = F::from_bool(selected); + if selected { + trace[i].mul = trace[i].sel1; + } - // Outer lookups - trace[i].a2 = F::from_canonical_usize(i); - trace[i].b2 = F::from_canonical_usize(i); + // Outer lookups + trace[i].a2 = F::from_canonical_usize(i); + trace[i].b2 = F::from_canonical_usize(i); - trace[i].a4 = F::from_canonical_usize(i); - trace[i].b4 = F::from_canonical_usize(i); - trace[i].sel2 = F::from_bool(true); - } + trace[i].a4 = F::from_canonical_usize(i); + trace[i].b4 = F::from_canonical_usize(i); + trace[i].sel2 = F::from_bool(true); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/lookup/rs/src/lookup2_15.rs b/pil2-components/test/std/lookup/rs/src/lookup2_15.rs index 09779c27..20f22cf1 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup2_15.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup2_15.rs @@ -1,104 +1,65 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Lookup2_15Trace, LOOKUP_2_15_AIR_IDS, LOOKUP_AIRGROUP_ID}; +use crate::Lookup2_15Trace; -pub struct Lookup2_15 { - _phantom: std::marker::PhantomData, -} +pub struct Lookup2_15; -impl Lookup2_15 -where - Standard: Distribution, -{ +impl Lookup2_15 { const MY_NAME: &'static str = "Lkup2_15"; - pub fn new(wcm: Arc>) -> Arc { - let lookup2_15 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(lookup2_15.clone(), Some(LOOKUP_AIRGROUP_ID), Some(LOOKUP_2_15_AIR_IDS)); - - lookup2_15 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[LOOKUP_AIRGROUP_ID][LOOKUP_2_15_AIR_IDS[0]].num_rows; - let trace = Lookup2_15Trace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), LOOKUP_AIRGROUP_ID, LOOKUP_2_15_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(LOOKUP_AIRGROUP_ID, LOOKUP_2_15_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Lookup2_15 +impl WitnessComponent for Lookup2_15 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(LOOKUP_AIRGROUP_ID, LOOKUP_2_15_AIR_IDS[0]).num_rows(); - let mut trace = Lookup2_15Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - // TODO: Add the ability to send inputs to lookup3 - // and consequently add random selectors - - for i in 0..num_rows { - // Inner lookups - trace[i].a1 = rng.gen(); - trace[i].b1 = rng.gen(); - trace[i].c1 = trace[i].a1; - trace[i].d1 = trace[i].b1; - - trace[i].a3 = rng.gen(); - trace[i].b3 = rng.gen(); - trace[i].c2 = trace[i].a3; - trace[i].d2 = trace[i].b3; - let selected = rng.gen_bool(0.5); - trace[i].sel1 = F::from_bool(selected); - if selected { - trace[i].mul = trace[i].sel1; - } + let mut trace = Lookup2_15Trace::new(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + // TODO: Add the ability to send inputs to lookup3 + // and consequently add random selectors + + for i in 0..num_rows { + // Inner lookups + trace[i].a1 = rng.gen(); + trace[i].b1 = rng.gen(); + trace[i].c1 = trace[i].a1; + trace[i].d1 = trace[i].b1; + + trace[i].a3 = rng.gen(); + trace[i].b3 = rng.gen(); + trace[i].c2 = trace[i].a3; + trace[i].d2 = trace[i].b3; + let selected = rng.gen_bool(0.5); + trace[i].sel1 = F::from_bool(selected); + if selected { + trace[i].mul = trace[i].sel1; + } - // Outer lookups - trace[i].a2 = F::from_canonical_usize(i % (1 << 14)); - trace[i].b2 = F::from_canonical_usize(i % (1 << 14)); + // Outer lookups + trace[i].a2 = F::from_canonical_usize(i % (1 << 14)); + trace[i].b2 = F::from_canonical_usize(i % (1 << 14)); - trace[i].a4 = F::from_canonical_usize(i % (1 << 14)); - trace[i].b4 = F::from_canonical_usize(i % (1 << 14)); - trace[i].sel2 = F::from_bool(true); - } + trace[i].a4 = F::from_canonical_usize(i % (1 << 14)); + trace[i].b4 = F::from_canonical_usize(i % (1 << 14)); + trace[i].sel2 = F::from_bool(true); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/lookup/rs/src/lookup3.rs b/pil2-components/test/std/lookup/rs/src/lookup3.rs index 70f23056..813abee8 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup3.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup3.rs @@ -1,89 +1,57 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; +use rand::{distributions::Standard, prelude::Distribution}; use p3_field::PrimeField; -use crate::{Lookup3Trace, LOOKUP_3_AIR_IDS, LOOKUP_AIRGROUP_ID}; +use crate::Lookup3Trace; -pub struct Lookup3 { - _phantom: std::marker::PhantomData, -} +pub struct Lookup3; -impl Lookup3 { +impl Lookup3 { const MY_NAME: &'static str = "Lookup_3"; - pub fn new(wcm: Arc>) -> Arc { - let lookup3 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(lookup3.clone(), Some(LOOKUP_AIRGROUP_ID), Some(LOOKUP_3_AIR_IDS)); - - lookup3 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // For simplicity, add a single instance of each air - let num_rows = pctx.global_info.airs[LOOKUP_AIRGROUP_ID][LOOKUP_3_AIR_IDS[0]].num_rows; - let trace = Lookup3Trace::new(num_rows); - - let air_instance = - AirInstance::new(sctx.clone(), LOOKUP_AIRGROUP_ID, LOOKUP_3_AIR_IDS[0], None, trace.buffer.unwrap()); - - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(LOOKUP_AIRGROUP_ID, LOOKUP_3_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Lookup3 { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let air = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(LOOKUP_AIRGROUP_ID, LOOKUP_3_AIR_IDS[0]).num_rows(); - let mut trace = Lookup3Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - for i in 0..num_rows { - trace[i].c1 = F::from_canonical_usize(i); - trace[i].d1 = F::from_canonical_usize(i); - if i < (1 << 12) { - trace[i].mul1 = F::from_canonical_usize(4); - } else if i < (1 << 13) { - trace[i].mul1 = F::from_canonical_usize(3); - } else { - trace[i].mul1 = F::from_canonical_usize(2); - } +impl WitnessComponent for Lookup3 +where + Standard: Distribution, +{ + fn execute(&self, pctx: Arc>) { + // For simplicity, add a single instance of each air + let mut trace = Lookup3Trace::new(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + for i in 0..num_rows { + trace[i].c1 = F::from_canonical_usize(i); + trace[i].d1 = F::from_canonical_usize(i); + if i < (1 << 12) { + trace[i].mul1 = F::from_canonical_usize(4); + } else if i < (1 << 13) { + trace[i].mul1 = F::from_canonical_usize(3); + } else { + trace[i].mul1 = F::from_canonical_usize(2); + } - trace[i].c2 = F::from_canonical_usize(i); - trace[i].d2 = F::from_canonical_usize(i); - if i < (1 << 12) { - trace[i].mul2 = F::from_canonical_usize(4); - } else if i < (1 << 13) { - trace[i].mul2 = F::from_canonical_usize(3); - } else { - trace[i].mul2 = F::from_canonical_usize(2); - } + trace[i].c2 = F::from_canonical_usize(i); + trace[i].d2 = F::from_canonical_usize(i); + if i < (1 << 12) { + trace[i].mul2 = F::from_canonical_usize(4); + } else if i < (1 << 13) { + trace[i].mul2 = F::from_canonical_usize(3); + } else { + trace[i].mul2 = F::from_canonical_usize(2); } } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/lookup/rs/src/lookup_lib.rs b/pil2-components/test/std/lookup/rs/src/lookup_lib.rs index 17ddb6d4..a3dc27e6 100644 --- a/pil2-components/test/std/lookup/rs/src/lookup_lib.rs +++ b/pil2-components/test/std/lookup/rs/src/lookup_lib.rs @@ -1,139 +1,34 @@ -use std::{error::Error, path::PathBuf, sync::Arc}; +use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessLibrary, WitnessManager}; -use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; +use witness::{witness_library, WitnessLibrary, WitnessManager}; use p3_field::PrimeField; use p3_goldilocks::Goldilocks; use rand::{distributions::Standard, prelude::Distribution}; -use crate::{Lookup0, Lookup1, Lookup2_12, Lookup2_13, Lookup2_15, Lookup3, Pilout}; +use crate::{Lookup0, Lookup1, Lookup2_12, Lookup2_13, Lookup2_15, Lookup3}; -pub struct LookupWitness { - pub wcm: Option>>, - pub lookup0: Option>>, - pub lookup1: Option>>, - pub lookup2_12: Option>>, - pub lookup2_13: Option>>, - pub lookup2_15: Option>>, - pub lookup3: Option>>, - pub std_lib: Option>>, -} - -impl Default for LookupWitness -where - Standard: Distribution, -{ - fn default() -> Self { - Self::new() - } -} - -impl LookupWitness -where - Standard: Distribution, -{ - pub fn new() -> Self { - LookupWitness { - wcm: None, - lookup0: None, - lookup1: None, - lookup2_12: None, - lookup2_13: None, - lookup2_15: None, - lookup3: None, - std_lib: None, - } - } - - pub fn initialize(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); - - let std_lib = Std::new(wcm.clone()); - let lookup0 = Lookup0::new(wcm.clone()); - let lookup1 = Lookup1::new(wcm.clone()); - let lookup2_12 = Lookup2_12::new(wcm.clone()); - let lookup2_13 = Lookup2_13::new(wcm.clone()); - let lookup2_15 = Lookup2_15::new(wcm.clone()); - let lookup3 = Lookup3::new(wcm.clone()); - - self.wcm = Some(wcm); - self.lookup0 = Some(lookup0); - self.lookup1 = Some(lookup1); - self.lookup2_12 = Some(lookup2_12); - self.lookup2_13 = Some(lookup2_13); - self.lookup2_15 = Some(lookup2_15); - self.lookup3 = Some(lookup3); - self.std_lib = Some(std_lib); - } -} +witness_library!(WitnessLib, Goldilocks); -impl WitnessLibrary for LookupWitness +impl WitnessLibrary for WitnessLib where Standard: Distribution, { - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); - - self.wcm.as_ref().unwrap().start_proof(pctx, ectx, sctx); - } - - fn end_proof(&mut self) { - self.wcm.as_ref().unwrap().end_proof(); - } - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // Execute those components that need to be executed - self.lookup0.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.lookup1.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.lookup2_12.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.lookup2_13.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.lookup2_15.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.lookup3.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - } - - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); + fn register_witness(&mut self, wcm: Arc>) { + Std::new(wcm.clone()); + let lookup0 = Lookup0::new(); + let lookup1 = Lookup1::new(); + let lookup2_12 = Lookup2_12::new(); + let lookup2_13 = Lookup2_13::new(); + let lookup2_15 = Lookup2_15::new(); + let lookup3 = Lookup3::new(); + + wcm.register_component(lookup0.clone()); + wcm.register_component(lookup1.clone()); + wcm.register_component(lookup2_12.clone()); + wcm.register_component(lookup2_13.clone()); + wcm.register_component(lookup2_15.clone()); + wcm.register_component(lookup3.clone()); } - - fn pilout(&self) -> WitnessPilout { - Pilout::pilout() - } -} - -#[no_mangle] -pub extern "Rust" fn init_library( - _: Option, - _: Option, - verbose_mode: VerboseMode, -) -> Result>, Box> { - initialize_logger(verbose_mode); - let lookup_witness = LookupWitness::new(); - Ok(Box::new(lookup_witness)) } - -// #[cfg(test)] -// mod tests { -// use proofman_cli::commands::verify_constraints::{Field, VerifyConstraintsCmd}; - -// #[test] -// fn test_verify_constraints() { -// let root_path = std::env::current_dir().expect("Failed to get current directory").join("../../../../"); -// let root_path = std::fs::canonicalize(root_path).expect("Failed to canonicalize root path"); - -// let verify_constraints = VerifyConstraintsCmd { -// witness_lib: root_path.join("target/debug/liblookup.so"), -// rom: None, -// public_inputs: None, -// proving_key: root_path.join("test/std/lookup/build/provingKey"), -// field: Field::Goldilocks, -// verbose: 0, -// }; - -// if let Err(e) = verify_constraints.run() { -// eprintln!("Failed to verify constraints: {:?}", e); -// std::process::exit(1); -// } -// } -// } diff --git a/pil2-components/test/std/lookup/rs/src/pil_helpers/mod.rs b/pil2-components/test/std/lookup/rs/src/pil_helpers/mod.rs index 8273843a..8e9f77af 100644 --- a/pil2-components/test/std/lookup/rs/src/pil_helpers/mod.rs +++ b/pil2-components/test/std/lookup/rs/src/pil_helpers/mod.rs @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/pil2-components/test/std/lookup/rs/src/pil_helpers/pilout.rs b/pil2-components/test/std/lookup/rs/src/pil_helpers/pilout.rs deleted file mode 100644 index 73c0d378..00000000 --- a/pil2-components/test/std/lookup/rs/src/pil_helpers/pilout.rs +++ /dev/null @@ -1,42 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"Build-hash"; - -//AIRGROUP CONSTANTS - -pub const LOOKUP_AIRGROUP_ID: usize = 0; - -//AIR CONSTANTS - -pub const LOOKUP_0_AIR_IDS: &[usize] = &[0]; - -pub const LOOKUP_1_AIR_IDS: &[usize] = &[1]; - -pub const LOOKUP_2_12_AIR_IDS: &[usize] = &[2]; - -pub const LOOKUP_2_13_AIR_IDS: &[usize] = &[3]; - -pub const LOOKUP_2_15_AIR_IDS: &[usize] = &[4]; - -pub const LOOKUP_3_AIR_IDS: &[usize] = &[5]; - -pub struct Pilout; - -impl Pilout { - pub fn pilout() -> WitnessPilout { - let mut pilout = WitnessPilout::new("Build", 2, PILOUT_HASH.to_vec()); - - let air_group = pilout.add_air_group(Some("Lookup")); - - air_group.add_air(Some("Lookup0"), 1024); - air_group.add_air(Some("Lookup1"), 1024); - air_group.add_air(Some("Lookup2_12"), 4096); - air_group.add_air(Some("Lookup2_13"), 8192); - air_group.add_air(Some("Lookup2_15"), 32768); - air_group.add_air(Some("Lookup3"), 16384); - - pilout - } -} diff --git a/pil2-components/test/std/lookup/rs/src/pil_helpers/traces.rs b/pil2-components/test/std/lookup/rs/src/pil_helpers/traces.rs index bdc3c49c..a1ec8083 100644 --- a/pil2-components/test/std/lookup/rs/src/pil_helpers/traces.rs +++ b/pil2-components/test/std/lookup/rs/src/pil_helpers/traces.rs @@ -2,27 +2,52 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; +pub use proofman_macros::values; -trace!(Lookup0Row, Lookup0Trace { +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"Build-hash"; + +//AIRGROUP CONSTANTS + +pub const LOOKUP_AIRGROUP_ID: usize = 0; + +//AIR CONSTANTS + +pub const LOOKUP_0_AIR_IDS: &[usize] = &[0]; + +pub const LOOKUP_1_AIR_IDS: &[usize] = &[1]; + +pub const LOOKUP_2_12_AIR_IDS: &[usize] = &[2]; + +pub const LOOKUP_2_13_AIR_IDS: &[usize] = &[3]; + +pub const LOOKUP_2_15_AIR_IDS: &[usize] = &[4]; + +pub const LOOKUP_3_AIR_IDS: &[usize] = &[5]; + + +trace!(Lookup0Trace { f: [F; 4], t: [F; 4], sel: [F; 2], mul: [F; 2], -}); +}, 0, 0, 1024 ); -trace!(Lookup1Row, Lookup1Trace { +trace!(Lookup1Trace { f: [F; 2], t: F, sel: [F; 2], mul: F, -}); +}, 0, 1, 1024 ); -trace!(Lookup2_12Row, Lookup2_12Trace { +trace!(Lookup2_12Trace { a1: F, b1: F, a2: F, b2: F, a3: F, b3: F, a4: F, b4: F, c1: F, d1: F, c2: F, d2: F, sel1: F, sel2: F, mul: F, -}); +}, 0, 2, 4096 ); -trace!(Lookup2_13Row, Lookup2_13Trace { +trace!(Lookup2_13Trace { a1: F, b1: F, a2: F, b2: F, a3: F, b3: F, a4: F, b4: F, c1: F, d1: F, c2: F, d2: F, sel1: F, sel2: F, mul: F, -}); +}, 0, 3, 8192 ); -trace!(Lookup2_15Row, Lookup2_15Trace { +trace!(Lookup2_15Trace { a1: F, b1: F, a2: F, b2: F, a3: F, b3: F, a4: F, b4: F, c1: F, d1: F, c2: F, d2: F, sel1: F, sel2: F, mul: F, -}); +}, 0, 4, 32768 ); -trace!(Lookup3Row, Lookup3Trace { +trace!(Lookup3Trace { c1: F, d1: F, c2: F, d2: F, mul1: F, mul2: F, -}); +}, 0, 5, 16384 ); diff --git a/pil2-components/test/std/permutation/rs/Cargo.toml b/pil2-components/test/std/permutation/rs/Cargo.toml index 0737b660..14065161 100644 --- a/pil2-components/test/std/permutation/rs/Cargo.toml +++ b/pil2-components/test/std/permutation/rs/Cargo.toml @@ -11,6 +11,7 @@ proofman.workspace = true proofman-common.workspace = true proofman-macros.workspace = true proofman-cli.workspace = true +witness.workspace = true pil-std-lib = { path = "../../../../lib/std/rs" } env_logger.workspace = true diff --git a/pil2-components/test/std/permutation/rs/src/permutation1_6.rs b/pil2-components/test/std/permutation/rs/src/permutation1_6.rs index 374c9a6a..cc1f76f7 100644 --- a/pil2-components/test/std/permutation/rs/src/permutation1_6.rs +++ b/pil2-components/test/std/permutation/rs/src/permutation1_6.rs @@ -1,135 +1,109 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, seq::SliceRandom, Rng}; -use crate::{Permutation1_6Trace, PERMUTATION_1_6_AIR_IDS, PERMUTATION_AIRGROUP_ID}; +use crate::Permutation1_6Trace; -pub struct Permutation1_6 { - _phantom: std::marker::PhantomData, -} +pub struct Permutation1_6; + +impl Permutation1_6 { + const MY_NAME: &'static str = "Perm1_6 "; -impl Permutation1_6 + pub fn new() -> Arc { + Arc::new(Self) + } +} +impl WitnessComponent for Permutation1_6 where Standard: Distribution, { - const MY_NAME: &'static str = "Perm1_6 "; + fn execute(&self, pctx: Arc>) { + let mut rng = rand::thread_rng(); - pub fn new(wcm: Arc>) -> Arc { - let permutation1_6 = Arc::new(Self { _phantom: std::marker::PhantomData }); + let mut trace = Permutation1_6Trace::new(); + let num_rows = trace.num_rows(); - wcm.register_component(permutation1_6.clone(), Some(PERMUTATION_AIRGROUP_ID), Some(PERMUTATION_1_6_AIR_IDS)); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - permutation1_6 - } + // TODO: Add the ability to send inputs to permutation2 + // and consequently add random selectors - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[PERMUTATION_AIRGROUP_ID][PERMUTATION_1_6_AIR_IDS[0]].num_rows; - let trace = Permutation1_6Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - PERMUTATION_AIRGROUP_ID, - PERMUTATION_1_6_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(PERMUTATION_AIRGROUP_ID, PERMUTATION_1_6_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + // Assumes + for i in 0..num_rows { + trace[i].a1 = rng.gen(); + trace[i].b1 = rng.gen(); - let trace2 = Permutation1_6Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - PERMUTATION_AIRGROUP_ID, - PERMUTATION_1_6_AIR_IDS[0], - None, - trace2.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(PERMUTATION_AIRGROUP_ID, PERMUTATION_1_6_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } - } -} + trace[i].a2 = F::from_canonical_u8(200); + trace[i].b2 = F::from_canonical_u8(201); -impl WitnessComponent for Permutation1_6 -where - Standard: Distribution, -{ - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { - let mut rng = rand::thread_rng(); + trace[i].a3 = rng.gen(); + trace[i].b3 = rng.gen(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; + trace[i].a4 = F::from_canonical_u8(100); + trace[i].b4 = F::from_canonical_u8(101); - let airgroup_id = air_instance.airgroup_id; - let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); + trace[i].sel1 = F::from_bool(rng.gen_bool(0.5)); + trace[i].sel3 = F::one(); + } - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); + let mut indices: Vec = (0..num_rows).collect(); + indices.shuffle(&mut rng); - if stage == 1 { - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(air_instance.airgroup_id, air_instance.air_id).num_rows(); + // Proves + for i in 0..num_rows { + // We take a random permutation of the indices to show that the permutation check is passing + trace[i].c1 = trace[indices[i]].a1; + trace[i].d1 = trace[indices[i]].b1; - // I cannot, programatically, link the permutation trace with its air_id - let mut trace = Permutation1_6Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + trace[i].c2 = trace[indices[i]].a3; + trace[i].d2 = trace[indices[i]].b3; - // TODO: Add the ability to send inputs to permutation2 - // and consequently add random selectors + trace[i].sel2 = trace[indices[i]].sel1; + } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); - // Assumes - for i in 0..num_rows { - trace[i].a1 = rng.gen(); - trace[i].b1 = rng.gen(); + let mut trace2 = Permutation1_6Trace::new(); - trace[i].a2 = F::from_canonical_u8(200); - trace[i].b2 = F::from_canonical_u8(201); + // Assumes + for i in 0..num_rows { + trace2[i].a1 = rng.gen(); + trace2[i].b1 = rng.gen(); - trace[i].a3 = rng.gen(); - trace[i].b3 = rng.gen(); + trace2[i].a2 = F::from_canonical_u8(200); + trace2[i].b2 = F::from_canonical_u8(201); - trace[i].a4 = F::from_canonical_u8(100); - trace[i].b4 = F::from_canonical_u8(101); + trace2[i].a3 = rng.gen(); + trace2[i].b3 = rng.gen(); - trace[i].sel1 = F::from_bool(rng.gen_bool(0.5)); - trace[i].sel3 = F::one(); - } + trace2[i].a4 = F::from_canonical_u8(100); + trace2[i].b4 = F::from_canonical_u8(101); - let mut indices: Vec = (0..num_rows).collect(); - indices.shuffle(&mut rng); + trace2[i].sel1 = F::from_bool(rng.gen_bool(0.5)); + trace2[i].sel3 = F::one(); + } - // Proves - for i in 0..num_rows { - // We take a random permutation of the indices to show that the permutation check is passing - trace[i].c1 = trace[indices[i]].a1; - trace[i].d1 = trace[indices[i]].b1; + let mut indices: Vec = (0..num_rows).collect(); + indices.shuffle(&mut rng); - trace[i].c2 = trace[indices[i]].a3; - trace[i].d2 = trace[indices[i]].b3; + // Proves + for i in 0..num_rows { + // We take a random permutation of the indices to show that the permutation check is passing + trace2[i].c1 = trace2[indices[i]].a1; + trace2[i].d1 = trace2[indices[i]].b1; - trace[i].sel2 = trace[indices[i]].sel1; - } + trace2[i].c2 = trace2[indices[i]].a3; + trace2[i].d2 = trace2[indices[i]].b3; + + trace2[i].sel2 = trace2[indices[i]].sel1; } + + let air_instance2 = AirInstance::new_from_trace(FromTrace::new(&mut trace2)); + add_air_instance::(air_instance2, pctx.clone()); } } diff --git a/pil2-components/test/std/permutation/rs/src/permutation1_7.rs b/pil2-components/test/std/permutation/rs/src/permutation1_7.rs index 7c503c1a..943e12fd 100644 --- a/pil2-components/test/std/permutation/rs/src/permutation1_7.rs +++ b/pil2-components/test/std/permutation/rs/src/permutation1_7.rs @@ -1,121 +1,72 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Permutation1_7Trace, PERMUTATION_1_7_AIR_IDS, PERMUTATION_AIRGROUP_ID}; +use crate::Permutation1_7Trace; -pub struct Permutation1_7 { - _phantom: std::marker::PhantomData, -} +pub struct Permutation1_7; -impl Permutation1_7 -where - Standard: Distribution, -{ +impl Permutation1_7 { const MY_NAME: &'static str = "Perm1_7 "; - pub fn new(wcm: Arc>) -> Arc { - let permutation1_7 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(permutation1_7.clone(), Some(PERMUTATION_AIRGROUP_ID), Some(PERMUTATION_1_7_AIR_IDS)); - - permutation1_7 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[PERMUTATION_AIRGROUP_ID][PERMUTATION_1_7_AIR_IDS[0]].num_rows; - let trace = Permutation1_7Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - PERMUTATION_AIRGROUP_ID, - PERMUTATION_1_7_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(PERMUTATION_AIRGROUP_ID, PERMUTATION_1_7_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Permutation1_7 +impl WitnessComponent for Permutation1_7 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); + let mut trace = Permutation1_7Trace::new(); + let num_rows = trace.num_rows(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - - let airgroup_id = air_instance.airgroup_id; - let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(airgroup_id, air_id).num_rows(); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - // I cannot, programatically, link the permutation trace with its air_id - let mut trace = Permutation1_7Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + // TODO: Add the ability to send inputs to permutation2 + // and consequently add random selectors - // TODO: Add the ability to send inputs to permutation2 - // and consequently add random selectors + // Assumes + for i in 0..num_rows { + trace[i].a1 = rng.gen(); + trace[i].b1 = rng.gen(); - // Assumes - for i in 0..num_rows { - trace[i].a1 = rng.gen(); - trace[i].b1 = rng.gen(); + trace[i].a2 = F::from_canonical_u8(200); + trace[i].b2 = F::from_canonical_u8(201); - trace[i].a2 = F::from_canonical_u8(200); - trace[i].b2 = F::from_canonical_u8(201); + trace[i].a3 = rng.gen(); + trace[i].b3 = rng.gen(); - trace[i].a3 = rng.gen(); - trace[i].b3 = rng.gen(); + trace[i].a4 = F::from_canonical_u8(100); + trace[i].b4 = F::from_canonical_u8(101); - trace[i].a4 = F::from_canonical_u8(100); - trace[i].b4 = F::from_canonical_u8(101); - - trace[i].sel1 = F::one(); - trace[i].sel3 = F::one(); // F::from_canonical_u8(rng.gen_range(0..=1)); - } + trace[i].sel1 = F::one(); + trace[i].sel3 = F::one(); // F::from_canonical_u8(rng.gen_range(0..=1)); + } - // TODO: Add the permutation of indexes + // TODO: Add the permutation of indexes - // Proves - for i in 0..num_rows { - let index = num_rows - i - 1; - // let mut index = rng.gen_range(0..num_rows); - trace[i].c1 = trace[index].a1; - trace[i].d1 = trace[index].b1; + // Proves + for i in 0..num_rows { + let index = num_rows - i - 1; + // let mut index = rng.gen_range(0..num_rows); + trace[i].c1 = trace[index].a1; + trace[i].d1 = trace[index].b1; - // index = rng.gen_range(0..num_rows); - trace[i].c2 = trace[index].a3; - trace[i].d2 = trace[index].b3; + // index = rng.gen_range(0..num_rows); + trace[i].c2 = trace[index].a3; + trace[i].d2 = trace[index].b3; - trace[i].sel2 = trace[i].sel1; - } + trace[i].sel2 = trace[i].sel1; } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/permutation/rs/src/permutation1_8.rs b/pil2-components/test/std/permutation/rs/src/permutation1_8.rs index 409f0f84..b948bf9a 100644 --- a/pil2-components/test/std/permutation/rs/src/permutation1_8.rs +++ b/pil2-components/test/std/permutation/rs/src/permutation1_8.rs @@ -1,121 +1,72 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{Permutation1_8Trace, PERMUTATION_1_8_AIR_IDS, PERMUTATION_AIRGROUP_ID}; +use crate::Permutation1_8Trace; -pub struct Permutation1_8 { - _phantom: std::marker::PhantomData, -} +pub struct Permutation1_8; -impl Permutation1_8 -where - Standard: Distribution, -{ +impl Permutation1_8 { const MY_NAME: &'static str = "Perm1_8 "; - pub fn new(wcm: Arc>) -> Arc { - let permutation1_8 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(permutation1_8.clone(), Some(PERMUTATION_AIRGROUP_ID), Some(PERMUTATION_1_8_AIR_IDS)); - - permutation1_8 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[PERMUTATION_AIRGROUP_ID][PERMUTATION_1_8_AIR_IDS[0]].num_rows; - let trace = Permutation1_8Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - PERMUTATION_AIRGROUP_ID, - PERMUTATION_1_8_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(PERMUTATION_AIRGROUP_ID, PERMUTATION_1_8_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Permutation1_8 +impl WitnessComponent for Permutation1_8 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); + let mut trace = Permutation1_8Trace::new(); + let num_rows = trace.num_rows(); - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - - let airgroup_id = air_instance.airgroup_id; - let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); - - if stage == 1 { - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(airgroup_id, air_id).num_rows(); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - // I cannot, programatically, link the permutation trace with its air_id - let mut trace = Permutation1_8Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + // TODO: Add the ability to send inputs to permutation2 + // and consequently add random selectors - // TODO: Add the ability to send inputs to permutation2 - // and consequently add random selectors + // Assumes + for i in 0..num_rows { + trace[i].a1 = rng.gen(); + trace[i].b1 = rng.gen(); - // Assumes - for i in 0..num_rows { - trace[i].a1 = rng.gen(); - trace[i].b1 = rng.gen(); + trace[i].a2 = F::from_canonical_u8(200); + trace[i].b2 = F::from_canonical_u8(201); - trace[i].a2 = F::from_canonical_u8(200); - trace[i].b2 = F::from_canonical_u8(201); + trace[i].a3 = rng.gen(); + trace[i].b3 = rng.gen(); - trace[i].a3 = rng.gen(); - trace[i].b3 = rng.gen(); + trace[i].a4 = F::from_canonical_u8(100); + trace[i].b4 = F::from_canonical_u8(101); - trace[i].a4 = F::from_canonical_u8(100); - trace[i].b4 = F::from_canonical_u8(101); - - trace[i].sel1 = F::one(); - trace[i].sel3 = F::one(); // F::from_canonical_u8(rng.gen_range(0..=1)); - } + trace[i].sel1 = F::one(); + trace[i].sel3 = F::one(); // F::from_canonical_u8(rng.gen_range(0..=1)); + } - // TODO: Add the permutation of indexes + // TODO: Add the permutation of indexes - // Proves - for i in 0..num_rows { - let index = num_rows - i - 1; - // let mut index = rng.gen_range(0..num_rows); - trace[i].c1 = trace[index].a1; - trace[i].d1 = trace[index].b1; + // Proves + for i in 0..num_rows { + let index = num_rows - i - 1; + // let mut index = rng.gen_range(0..num_rows); + trace[i].c1 = trace[index].a1; + trace[i].d1 = trace[index].b1; - // index = rng.gen_range(0..num_rows); - trace[i].c2 = trace[index].a3; - trace[i].d2 = trace[index].b3; + // index = rng.gen_range(0..num_rows); + trace[i].c2 = trace[index].a3; + trace[i].d2 = trace[index].b3; - trace[i].sel2 = trace[i].sel1; - } + trace[i].sel2 = trace[i].sel1; } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/permutation/rs/src/permutation2.rs b/pil2-components/test/std/permutation/rs/src/permutation2.rs index f627101f..66182dd6 100644 --- a/pil2-components/test/std/permutation/rs/src/permutation2.rs +++ b/pil2-components/test/std/permutation/rs/src/permutation2.rs @@ -1,88 +1,44 @@ use std::sync::Arc; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use p3_field::PrimeField; -use crate::{Permutation2_6Trace, PERMUTATION_2_6_AIR_IDS, PERMUTATION_AIRGROUP_ID}; +use crate::Permutation2_6Trace; -pub struct Permutation2 { - _phantom: std::marker::PhantomData, -} +pub struct Permutation2; -impl Permutation2 { +impl Permutation2 { const MY_NAME: &'static str = "Perm2 "; - pub fn new(wcm: Arc>) -> Arc { - let permutation2 = Arc::new(Self { _phantom: std::marker::PhantomData }); - - wcm.register_component(permutation2.clone(), Some(PERMUTATION_AIRGROUP_ID), Some(PERMUTATION_2_6_AIR_IDS)); - - permutation2 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[PERMUTATION_AIRGROUP_ID][PERMUTATION_2_6_AIR_IDS[0]].num_rows; - let trace = Permutation2_6Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - PERMUTATION_AIRGROUP_ID, - PERMUTATION_2_6_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(PERMUTATION_AIRGROUP_ID, PERMUTATION_2_6_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new() -> Arc { + Arc::new(Self) } } -impl WitnessComponent for Permutation2 { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - - let airgroup_id = air_instance.airgroup_id; - let air_id = air_instance.air_id; - let air = pctx.pilout.get_air(airgroup_id, air_id); - - log::debug!( - "{}: ··· Witness computation for AIR '{}' at stage {}", - Self::MY_NAME, - air.name().unwrap_or("unknown"), - stage - ); +impl WitnessComponent for Permutation2 { + fn execute(&self, pctx: Arc>) { + let mut trace = Permutation2_6Trace::new(); + let num_rows = trace.num_rows(); - if stage == 1 { - let buffer = &mut air_instance.trace; + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let num_rows = pctx.pilout.get_air(airgroup_id, air_id).num_rows(); - let mut trace = Permutation2_6Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + // Note: Here it is assumed that num_rows of permutation2 is equal to + // the sum of num_rows of each variant of permutation1. + // Ohterwise, the permutation check cannot be satisfied. + // Proves + for i in 0..num_rows { + trace[i].c1 = F::from_canonical_u8(200); + trace[i].d1 = F::from_canonical_u8(201); - // Note: Here it is assumed that num_rows of permutation2 is equal to - // the sum of num_rows of each variant of permutation1. - // Ohterwise, the permutation check cannot be satisfied. - // Proves - for i in 0..num_rows { - trace[i].c1 = F::from_canonical_u8(200); - trace[i].d1 = F::from_canonical_u8(201); + trace[i].c2 = F::from_canonical_u8(100); + trace[i].d2 = F::from_canonical_u8(101); - trace[i].c2 = F::from_canonical_u8(100); - trace[i].d2 = F::from_canonical_u8(101); - - trace[i].sel = F::from_bool(true); - } + trace[i].sel = F::from_bool(true); } + + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/permutation/rs/src/permutation_lib.rs b/pil2-components/test/std/permutation/rs/src/permutation_lib.rs index 48e97994..813af882 100644 --- a/pil2-components/test/std/permutation/rs/src/permutation_lib.rs +++ b/pil2-components/test/std/permutation/rs/src/permutation_lib.rs @@ -1,129 +1,25 @@ -use std::{error::Error, path::PathBuf, sync::Arc}; +use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessLibrary, WitnessManager}; -use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; +use witness::{witness_library, WitnessLibrary, WitnessManager}; use p3_field::PrimeField; use p3_goldilocks::Goldilocks; use rand::{distributions::Standard, prelude::Distribution}; -use crate::{Permutation1_6, Permutation1_7, Permutation1_8, Permutation2, Pilout}; +use crate::{Permutation1_6, Permutation1_7, Permutation1_8, Permutation2}; -pub struct PermutationWitness { - pub wcm: Option>>, - pub permutation1_6: Option>>, - pub permutation1_7: Option>>, - pub permutation1_8: Option>>, - pub permutation2: Option>>, - pub std_lib: Option>>, -} - -impl Default for PermutationWitness -where - Standard: Distribution, -{ - fn default() -> Self { - Self::new() - } -} - -impl PermutationWitness -where - Standard: Distribution, -{ - pub fn new() -> Self { - PermutationWitness { - wcm: None, - permutation1_6: None, - permutation1_7: None, - permutation1_8: None, - permutation2: None, - std_lib: None, - } - } - - pub fn initialize(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); - - let std_lib = Std::new(wcm.clone()); - let permutation1_6 = Permutation1_6::new(wcm.clone()); - let permutation1_7 = Permutation1_7::new(wcm.clone()); - let permutation1_8 = Permutation1_8::new(wcm.clone()); - let permutation2 = Permutation2::new(wcm.clone()); - - self.wcm = Some(wcm); - self.permutation1_6 = Some(permutation1_6); - self.permutation1_7 = Some(permutation1_7); - self.permutation1_8 = Some(permutation1_8); - self.permutation2 = Some(permutation2); - self.std_lib = Some(std_lib); - } -} +witness_library!(WitnessLib, Goldilocks); -impl WitnessLibrary for PermutationWitness +impl WitnessLibrary for WitnessLib where Standard: Distribution, { - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); - - self.wcm.as_ref().unwrap().start_proof(pctx, ectx, sctx); - } - - fn end_proof(&mut self) { - self.wcm.as_ref().unwrap().end_proof(); - } - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // Execute those components that need to be executed - self.permutation1_6.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.permutation1_7.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.permutation1_8.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.permutation2.as_ref().unwrap().execute(pctx, ectx, sctx); - } - - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); + fn register_witness(&mut self, wcm: Arc>) { + Std::new(wcm.clone()); + wcm.register_component(Permutation1_6::new()); + wcm.register_component(Permutation1_7::new()); + wcm.register_component(Permutation1_8::new()); + wcm.register_component(Permutation2::new()); } - - fn pilout(&self) -> WitnessPilout { - Pilout::pilout() - } -} - -#[no_mangle] -pub extern "Rust" fn init_library( - _: Option, - _: Option, - verbose_mode: VerboseMode, -) -> Result>, Box> { - initialize_logger(verbose_mode); - let permutation_witness = PermutationWitness::new(); - Ok(Box::new(permutation_witness)) } - -// #[cfg(test)] -// mod tests { -// use proofman_cli::commands::verify_constraints::{Field, VerifyConstraintsCmd}; - -// #[test] -// fn test_verify_constraints() { -// let root_path = std::env::current_dir().expect("Failed to get current directory").join("../../../../"); -// let root_path = std::fs::canonicalize(root_path).expect("Failed to canonicalize root path"); - -// let verify_constraints = VerifyConstraintsCmd { -// witness_lib: root_path.join("target/debug/libpermutation.so"), -// rom: None, -// public_inputs: None, -// proving_key: root_path.join("test/std/permutation/build/provingKey"), -// field: Field::Goldilocks, -// verbose: 0, -// }; - -// if let Err(e) = verify_constraints.run() { -// eprintln!("Failed to verify constraints: {:?}", e); -// std::process::exit(1); -// } -// } -// } diff --git a/pil2-components/test/std/permutation/rs/src/pil_helpers/mod.rs b/pil2-components/test/std/permutation/rs/src/pil_helpers/mod.rs index 8273843a..8e9f77af 100644 --- a/pil2-components/test/std/permutation/rs/src/pil_helpers/mod.rs +++ b/pil2-components/test/std/permutation/rs/src/pil_helpers/mod.rs @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/pil2-components/test/std/permutation/rs/src/pil_helpers/pilout.rs b/pil2-components/test/std/permutation/rs/src/pil_helpers/pilout.rs deleted file mode 100644 index 12ce2855..00000000 --- a/pil2-components/test/std/permutation/rs/src/pil_helpers/pilout.rs +++ /dev/null @@ -1,36 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"Build-hash"; - -//AIRGROUP CONSTANTS - -pub const PERMUTATION_AIRGROUP_ID: usize = 0; - -//AIR CONSTANTS - -pub const PERMUTATION_1_6_AIR_IDS: &[usize] = &[0]; - -pub const PERMUTATION_1_7_AIR_IDS: &[usize] = &[1]; - -pub const PERMUTATION_1_8_AIR_IDS: &[usize] = &[2]; - -pub const PERMUTATION_2_6_AIR_IDS: &[usize] = &[3]; - -pub struct Pilout; - -impl Pilout { - pub fn pilout() -> WitnessPilout { - let mut pilout = WitnessPilout::new("Build", 2, PILOUT_HASH.to_vec()); - - let air_group = pilout.add_air_group(Some("Permutation")); - - air_group.add_air(Some("Permutation1_6"), 64); - air_group.add_air(Some("Permutation1_7"), 128); - air_group.add_air(Some("Permutation1_8"), 256); - air_group.add_air(Some("Permutation2_6"), 512); - - pilout - } -} diff --git a/pil2-components/test/std/permutation/rs/src/pil_helpers/traces.rs b/pil2-components/test/std/permutation/rs/src/pil_helpers/traces.rs index 12c0e74b..3be8f3e2 100644 --- a/pil2-components/test/std/permutation/rs/src/pil_helpers/traces.rs +++ b/pil2-components/test/std/permutation/rs/src/pil_helpers/traces.rs @@ -2,19 +2,40 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; +pub use proofman_macros::values; -trace!(Permutation1_6Row, Permutation1_6Trace { +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"Build-hash"; + +//AIRGROUP CONSTANTS + +pub const PERMUTATION_AIRGROUP_ID: usize = 0; + +//AIR CONSTANTS + +pub const PERMUTATION_1_6_AIR_IDS: &[usize] = &[0]; + +pub const PERMUTATION_1_7_AIR_IDS: &[usize] = &[1]; + +pub const PERMUTATION_1_8_AIR_IDS: &[usize] = &[2]; + +pub const PERMUTATION_2_6_AIR_IDS: &[usize] = &[3]; + + +trace!(Permutation1_6Trace { a1: F, b1: F, a2: F, b2: F, a3: F, b3: F, a4: F, b4: F, c1: F, d1: F, c2: F, d2: F, sel1: F, sel2: F, sel3: F, -}); +}, 0, 0, 64 ); -trace!(Permutation1_7Row, Permutation1_7Trace { +trace!(Permutation1_7Trace { a1: F, b1: F, a2: F, b2: F, a3: F, b3: F, a4: F, b4: F, c1: F, d1: F, c2: F, d2: F, sel1: F, sel2: F, sel3: F, -}); +}, 0, 1, 128 ); -trace!(Permutation1_8Row, Permutation1_8Trace { +trace!(Permutation1_8Trace { a1: F, b1: F, a2: F, b2: F, a3: F, b3: F, a4: F, b4: F, c1: F, d1: F, c2: F, d2: F, sel1: F, sel2: F, sel3: F, -}); +}, 0, 2, 256 ); -trace!(Permutation2_6Row, Permutation2_6Trace { +trace!(Permutation2_6Trace { c1: F, d1: F, c2: F, d2: F, sel: F, -}); +}, 0, 3, 512 ); diff --git a/pil2-components/test/std/range_check/rs/Cargo.toml b/pil2-components/test/std/range_check/rs/Cargo.toml index 795f9bae..7e64129e 100644 --- a/pil2-components/test/std/range_check/rs/Cargo.toml +++ b/pil2-components/test/std/range_check/rs/Cargo.toml @@ -11,6 +11,7 @@ proofman.workspace = true proofman-common.workspace = true proofman-macros.workspace = true proofman-cli.workspace = true +witness.workspace = true pil-std-lib = { path = "../../../../lib/std/rs" } env_logger.workspace = true diff --git a/pil2-components/test/std/range_check/rs/src/multi_range_check1.rs b/pil2-components/test/std/range_check/rs/src/multi_range_check1.rs index 7cb59b88..50139d46 100644 --- a/pil2-components/test/std/range_check/rs/src/multi_range_check1.rs +++ b/pil2-components/test/std/range_check/rs/src/multi_range_check1.rs @@ -1,14 +1,15 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{MultiRangeCheck1Trace, MULTI_RANGE_CHECK_1_AIRGROUP_ID, MULTI_RANGE_CHECK_1_AIR_IDS}; +use crate::MultiRangeCheck1Trace; pub struct MultiRangeCheck1 { std_lib: Arc>, @@ -20,37 +21,8 @@ where { const MY_NAME: &'static str = "MtRngCh1"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let multi_range_check1 = Arc::new(Self { std_lib }); - - wcm.register_component( - multi_range_check1.clone(), - Some(MULTI_RANGE_CHECK_1_AIRGROUP_ID), - Some(MULTI_RANGE_CHECK_1_AIR_IDS), - ); - - // Register dependency relations - multi_range_check1.std_lib.register_predecessor(); - - multi_range_check1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[MULTI_RANGE_CHECK_1_AIRGROUP_ID][MULTI_RANGE_CHECK_1_AIR_IDS[0]].num_rows; - let trace = MultiRangeCheck1Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - MULTI_RANGE_CHECK_1_AIRGROUP_ID, - MULTI_RANGE_CHECK_1_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(MULTI_RANGE_CHECK_1_AIRGROUP_ID, MULTI_RANGE_CHECK_1_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -58,88 +30,74 @@ impl WitnessComponent for MultiRangeCheck1 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "MultiRangeCheck1", stage); - - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - - let buffer = &mut air_instance.trace; - let num_rows = - pctx.pilout.get_air(MULTI_RANGE_CHECK_1_AIRGROUP_ID, MULTI_RANGE_CHECK_1_AIR_IDS[0]).num_rows(); - let mut trace = MultiRangeCheck1Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); - let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); - let range3 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 6) - 1), Some(false)); - let range4 = self.std_lib.get_range(BigInt::from(1 << 5), BigInt::from((1 << 8) - 1), Some(false)); - let range5 = self.std_lib.get_range(BigInt::from(1 << 8), BigInt::from((1 << 9) - 1), Some(false)); - - for i in 0..num_rows { - let selected1 = rng.gen_bool(0.5); - let range_selector1 = rng.gen_bool(0.5); - trace[i].sel[0] = F::from_bool(selected1); - trace[i].range_sel[0] = F::from_bool(range_selector1); - - let selected2 = rng.gen_bool(0.5); - let range_selector2 = rng.gen_bool(0.5); - trace[i].sel[1] = F::from_bool(selected2); - trace[i].range_sel[1] = F::from_bool(range_selector2); - - let selected3 = rng.gen_bool(0.5); - let range_selector3 = rng.gen_bool(0.5); - trace[i].sel[2] = F::from_bool(selected3); - trace[i].range_sel[2] = F::from_bool(range_selector3); - - if selected1 { - if range_selector1 { - trace[i].a[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); - - self.std_lib.range_check(trace[i].a[0], F::one(), range1); - } else { - trace[i].a[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - - self.std_lib.range_check(trace[i].a[0], F::one(), range2); - } + let mut trace = MultiRangeCheck1Trace::new_zeroes(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); + let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); + let range3 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 6) - 1), Some(false)); + let range4 = self.std_lib.get_range(BigInt::from(1 << 5), BigInt::from((1 << 8) - 1), Some(false)); + let range5 = self.std_lib.get_range(BigInt::from(1 << 8), BigInt::from((1 << 9) - 1), Some(false)); + + for i in 0..num_rows { + let selected1 = rng.gen_bool(0.5); + let range_selector1 = rng.gen_bool(0.5); + trace[i].sel[0] = F::from_bool(selected1); + trace[i].range_sel[0] = F::from_bool(range_selector1); + + let selected2 = rng.gen_bool(0.5); + let range_selector2 = rng.gen_bool(0.5); + trace[i].sel[1] = F::from_bool(selected2); + trace[i].range_sel[1] = F::from_bool(range_selector2); + + let selected3 = rng.gen_bool(0.5); + let range_selector3 = rng.gen_bool(0.5); + trace[i].sel[2] = F::from_bool(selected3); + trace[i].range_sel[2] = F::from_bool(range_selector3); + + if selected1 { + if range_selector1 { + trace[i].a[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); + + self.std_lib.range_check(trace[i].a[0], F::one(), range1); + } else { + trace[i].a[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + + self.std_lib.range_check(trace[i].a[0], F::one(), range2); } + } - if selected2 { - if range_selector2 { - trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); + if selected2 { + if range_selector2 { + trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); - self.std_lib.range_check(trace[i].a[1], F::one(), range1); - } else { - trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 6) - 1)); + self.std_lib.range_check(trace[i].a[1], F::one(), range1); + } else { + trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 6) - 1)); - self.std_lib.range_check(trace[i].a[1], F::one(), range3); - } + self.std_lib.range_check(trace[i].a[1], F::one(), range3); } + } - if selected3 { - if range_selector3 { - trace[i].a[2] = F::from_canonical_u16(rng.gen_range((1 << 5)..=(1 << 8) - 1)); + if selected3 { + if range_selector3 { + trace[i].a[2] = F::from_canonical_u16(rng.gen_range((1 << 5)..=(1 << 8) - 1)); - self.std_lib.range_check(trace[i].a[2], F::one(), range4); - } else { - trace[i].a[2] = F::from_canonical_u16(rng.gen_range((1 << 8)..=(1 << 9) - 1)); + self.std_lib.range_check(trace[i].a[2], F::one(), range4); + } else { + trace[i].a[2] = F::from_canonical_u16(rng.gen_range((1 << 8)..=(1 << 9) - 1)); - self.std_lib.range_check(trace[i].a[2], F::one(), range5); - } + self.std_lib.range_check(trace[i].a[2], F::one(), range5); } } } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/multi_range_check2.rs b/pil2-components/test/std/range_check/rs/src/multi_range_check2.rs index 6ab0e9ec..7c3cddf5 100644 --- a/pil2-components/test/std/range_check/rs/src/multi_range_check2.rs +++ b/pil2-components/test/std/range_check/rs/src/multi_range_check2.rs @@ -1,14 +1,15 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{MultiRangeCheck2Trace, MULTI_RANGE_CHECK_2_AIRGROUP_ID, MULTI_RANGE_CHECK_2_AIR_IDS}; +use crate::MultiRangeCheck2Trace; pub struct MultiRangeCheck2 { std_lib: Arc>, @@ -20,37 +21,8 @@ where { const MY_NAME: &'static str = "MtRngCh2"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let multi_range_check2 = Arc::new(Self { std_lib }); - - wcm.register_component( - multi_range_check2.clone(), - Some(MULTI_RANGE_CHECK_2_AIRGROUP_ID), - Some(MULTI_RANGE_CHECK_2_AIR_IDS), - ); - - // Register dependency relations - multi_range_check2.std_lib.register_predecessor(); - - multi_range_check2 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[MULTI_RANGE_CHECK_2_AIRGROUP_ID][MULTI_RANGE_CHECK_2_AIR_IDS[0]].num_rows; - let trace = MultiRangeCheck2Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - MULTI_RANGE_CHECK_2_AIRGROUP_ID, - MULTI_RANGE_CHECK_2_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(MULTI_RANGE_CHECK_2_AIRGROUP_ID, MULTI_RANGE_CHECK_2_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -58,70 +30,56 @@ impl WitnessComponent for MultiRangeCheck2 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "MultiRangeCheck2", stage); + let mut trace = MultiRangeCheck2Trace::new_zeroes(); + let num_rows = trace.num_rows(); - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let num_rows = - pctx.pilout.get_air(MULTI_RANGE_CHECK_2_AIRGROUP_ID, MULTI_RANGE_CHECK_2_AIR_IDS[0]).num_rows(); - let mut trace = MultiRangeCheck2Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + let range1 = self.std_lib.get_range(BigInt::from(1 << 5), BigInt::from((1 << 8) - 1), Some(false)); + let range2 = self.std_lib.get_range(BigInt::from(1 << 8), BigInt::from((1 << 9) - 1), Some(false)); + let range3 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); + let range4 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); - let range1 = self.std_lib.get_range(BigInt::from(1 << 5), BigInt::from((1 << 8) - 1), Some(false)); - let range2 = self.std_lib.get_range(BigInt::from(1 << 8), BigInt::from((1 << 9) - 1), Some(false)); - let range3 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); - let range4 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); + for i in 0..num_rows { + let selected1 = rng.gen_bool(0.5); + let range_selector1 = rng.gen_bool(0.5); + trace[i].sel[0] = F::from_bool(selected1); + trace[i].range_sel[0] = F::from_bool(range_selector1); - for i in 0..num_rows { - let selected1 = rng.gen_bool(0.5); - let range_selector1 = rng.gen_bool(0.5); - trace[i].sel[0] = F::from_bool(selected1); - trace[i].range_sel[0] = F::from_bool(range_selector1); + let selected2 = rng.gen_bool(0.5); + let range_selector2 = rng.gen_bool(0.5); + trace[i].sel[1] = F::from_bool(selected2); + trace[i].range_sel[1] = F::from_bool(range_selector2); - let selected2 = rng.gen_bool(0.5); - let range_selector2 = rng.gen_bool(0.5); - trace[i].sel[1] = F::from_bool(selected2); - trace[i].range_sel[1] = F::from_bool(range_selector2); + if selected1 { + if range_selector1 { + trace[i].a[0] = F::from_canonical_u16(rng.gen_range((1 << 5)..=(1 << 8) - 1)); - if selected1 { - if range_selector1 { - trace[i].a[0] = F::from_canonical_u16(rng.gen_range((1 << 5)..=(1 << 8) - 1)); + self.std_lib.range_check(trace[i].a[0], F::one(), range1); + } else { + trace[i].a[0] = F::from_canonical_u16(rng.gen_range((1 << 8)..=(1 << 9) - 1)); - self.std_lib.range_check(trace[i].a[0], F::one(), range1); - } else { - trace[i].a[0] = F::from_canonical_u16(rng.gen_range((1 << 8)..=(1 << 9) - 1)); - - self.std_lib.range_check(trace[i].a[0], F::one(), range2); - } + self.std_lib.range_check(trace[i].a[0], F::one(), range2); } + } - if selected2 { - if range_selector2 { - trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); + if selected2 { + if range_selector2 { + trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); - self.std_lib.range_check(trace[i].a[1], F::one(), range3); - } else { - trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 4) - 1)); + self.std_lib.range_check(trace[i].a[1], F::one(), range3); + } else { + trace[i].a[1] = F::from_canonical_u16(rng.gen_range(0..=(1 << 4) - 1)); - self.std_lib.range_check(trace[i].a[1], F::one(), range4); - } + self.std_lib.range_check(trace[i].a[1], F::one(), range4); } } } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/pil_helpers/mod.rs b/pil2-components/test/std/range_check/rs/src/pil_helpers/mod.rs index 8273843a..8e9f77af 100644 --- a/pil2-components/test/std/range_check/rs/src/pil_helpers/mod.rs +++ b/pil2-components/test/std/range_check/rs/src/pil_helpers/mod.rs @@ -1,7 +1,6 @@ // WARNING: This file has been autogenerated from the PILOUT file. // Manual modifications are not recommended and may be overwritten. -mod pilout; +#[rustfmt::skip] mod traces; -pub use pilout::*; pub use traces::*; diff --git a/pil2-components/test/std/range_check/rs/src/pil_helpers/pilout.rs b/pil2-components/test/std/range_check/rs/src/pil_helpers/pilout.rs deleted file mode 100644 index 2e0d3565..00000000 --- a/pil2-components/test/std/range_check/rs/src/pil_helpers/pilout.rs +++ /dev/null @@ -1,110 +0,0 @@ -// WARNING: This file has been autogenerated from the PILOUT file. -// Manual modifications are not recommended and may be overwritten. -use proofman_common::WitnessPilout; - -pub const PILOUT_HASH: &[u8] = b"Build-hash"; - -//AIRGROUP CONSTANTS - -pub const RANGE_CHECK_3_AIRGROUP_ID: usize = 0; - -pub const RANGE_CHECK_2_AIRGROUP_ID: usize = 1; - -pub const RANGE_CHECK_1_AIRGROUP_ID: usize = 2; - -pub const RANGE_CHECK_4_AIRGROUP_ID: usize = 3; - -pub const MULTI_RANGE_CHECK_1_AIRGROUP_ID: usize = 4; - -pub const MULTI_RANGE_CHECK_2_AIRGROUP_ID: usize = 5; - -pub const RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID: usize = 6; - -pub const RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID: usize = 7; - -pub const RANGE_CHECK_MIX_AIRGROUP_ID: usize = 8; - -pub const U_8_AIR_AIRGROUP_ID: usize = 9; - -pub const SPECIFIED_RANGES_AIRGROUP_ID: usize = 10; - -//AIR CONSTANTS - -pub const RANGE_CHECK_3_AIR_IDS: &[usize] = &[0]; - -pub const RANGE_CHECK_2_AIR_IDS: &[usize] = &[0]; - -pub const RANGE_CHECK_1_AIR_IDS: &[usize] = &[0]; - -pub const RANGE_CHECK_4_AIR_IDS: &[usize] = &[0]; - -pub const U_16_AIR_AIR_IDS: &[usize] = &[1]; - -pub const MULTI_RANGE_CHECK_1_AIR_IDS: &[usize] = &[0]; - -pub const MULTI_RANGE_CHECK_2_AIR_IDS: &[usize] = &[0]; - -pub const RANGE_CHECK_DYNAMIC_1_AIR_IDS: &[usize] = &[0]; - -pub const RANGE_CHECK_DYNAMIC_2_AIR_IDS: &[usize] = &[0]; - -pub const RANGE_CHECK_MIX_AIR_IDS: &[usize] = &[0]; - -pub const U_8_AIR_AIR_IDS: &[usize] = &[0]; - -pub const SPECIFIED_RANGES_AIR_IDS: &[usize] = &[0]; - -pub struct Pilout; - -impl Pilout { - pub fn pilout() -> WitnessPilout { - let mut pilout = WitnessPilout::new("Build", 2, PILOUT_HASH.to_vec()); - - let air_group = pilout.add_air_group(Some("RangeCheck3")); - - air_group.add_air(Some("RangeCheck3"), 32); - - let air_group = pilout.add_air_group(Some("RangeCheck2")); - - air_group.add_air(Some("RangeCheck2"), 16); - - let air_group = pilout.add_air_group(Some("RangeCheck1")); - - air_group.add_air(Some("RangeCheck1"), 8); - - let air_group = pilout.add_air_group(Some("RangeCheck4")); - - air_group.add_air(Some("RangeCheck4"), 64); - air_group.add_air(Some("U16Air"), 65536); - - let air_group = pilout.add_air_group(Some("MultiRangeCheck1")); - - air_group.add_air(Some("MultiRangeCheck1"), 8); - - let air_group = pilout.add_air_group(Some("MultiRangeCheck2")); - - air_group.add_air(Some("MultiRangeCheck2"), 16); - - let air_group = pilout.add_air_group(Some("RangeCheckDynamic1")); - - air_group.add_air(Some("RangeCheckDynamic1"), 256); - - let air_group = pilout.add_air_group(Some("RangeCheckDynamic2")); - - air_group.add_air(Some("RangeCheckDynamic2"), 64); - - let air_group = pilout.add_air_group(Some("RangeCheckMix")); - - air_group.add_air(Some("RangeCheckMix"), 64); - - let air_group = pilout.add_air_group(Some("U8Air")); - - air_group.add_air(Some("U8Air"), 256); - - let air_group = pilout.add_air_group(Some("SpecifiedRanges")); - - air_group.add_air(Some("SpecifiedRanges"), 131072); - - pilout - } -} diff --git a/pil2-components/test/std/range_check/rs/src/pil_helpers/traces.rs b/pil2-components/test/std/range_check/rs/src/pil_helpers/traces.rs index 10ad6e66..d9f65fdb 100644 --- a/pil2-components/test/std/range_check/rs/src/pil_helpers/traces.rs +++ b/pil2-components/test/std/range_check/rs/src/pil_helpers/traces.rs @@ -2,51 +2,108 @@ // Manual modifications are not recommended and may be overwritten. use proofman_common as common; pub use proofman_macros::trace; +pub use proofman_macros::values; -trace!(RangeCheck3Row, RangeCheck3Trace { +#[allow(dead_code)] +type FieldExtension = [F; 3]; + +pub const PILOUT_HASH: &[u8] = b"Build-hash"; + +//AIRGROUP CONSTANTS + +pub const RANGE_CHECK_3_AIRGROUP_ID: usize = 0; + +pub const RANGE_CHECK_2_AIRGROUP_ID: usize = 1; + +pub const RANGE_CHECK_1_AIRGROUP_ID: usize = 2; + +pub const RANGE_CHECK_4_AIRGROUP_ID: usize = 3; + +pub const MULTI_RANGE_CHECK_1_AIRGROUP_ID: usize = 4; + +pub const MULTI_RANGE_CHECK_2_AIRGROUP_ID: usize = 5; + +pub const RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID: usize = 6; + +pub const RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID: usize = 7; + +pub const RANGE_CHECK_MIX_AIRGROUP_ID: usize = 8; + +pub const U_8_AIR_AIRGROUP_ID: usize = 9; + +pub const SPECIFIED_RANGES_AIRGROUP_ID: usize = 10; + +//AIR CONSTANTS + +pub const RANGE_CHECK_3_AIR_IDS: &[usize] = &[0]; + +pub const RANGE_CHECK_2_AIR_IDS: &[usize] = &[0]; + +pub const RANGE_CHECK_1_AIR_IDS: &[usize] = &[0]; + +pub const RANGE_CHECK_4_AIR_IDS: &[usize] = &[0]; + +pub const U_16_AIR_AIR_IDS: &[usize] = &[1]; + +pub const MULTI_RANGE_CHECK_1_AIR_IDS: &[usize] = &[0]; + +pub const MULTI_RANGE_CHECK_2_AIR_IDS: &[usize] = &[0]; + +pub const RANGE_CHECK_DYNAMIC_1_AIR_IDS: &[usize] = &[0]; + +pub const RANGE_CHECK_DYNAMIC_2_AIR_IDS: &[usize] = &[0]; + +pub const RANGE_CHECK_MIX_AIR_IDS: &[usize] = &[0]; + +pub const U_8_AIR_AIR_IDS: &[usize] = &[0]; + +pub const SPECIFIED_RANGES_AIR_IDS: &[usize] = &[0]; + + +trace!(RangeCheck3Trace { c1: F, c2: F, -}); +}, 0, 0, 32 ); -trace!(RangeCheck2Row, RangeCheck2Trace { +trace!(RangeCheck2Trace { b1: F, b2: F, b3: F, -}); +}, 1, 0, 16 ); -trace!(RangeCheck1Row, RangeCheck1Trace { +trace!(RangeCheck1Trace { a1: F, a2: F, a3: F, a4: F, a5: F, sel1: F, sel2: F, sel3: F, -}); +}, 2, 0, 8 ); -trace!(RangeCheck4Row, RangeCheck4Trace { +trace!(RangeCheck4Trace { a1: F, a2: F, a3: F, a4: F, a5: F, a6: F, a7: F, a8: F, sel1: F, sel2: F, -}); +}, 3, 0, 64 ); -trace!(U16AirRow, U16AirTrace { +trace!(U16AirTrace { mul: F, -}); +}, 3, 1, 65536 ); -trace!(MultiRangeCheck1Row, MultiRangeCheck1Trace { +trace!(MultiRangeCheck1Trace { a: [F; 3], sel: [F; 3], range_sel: [F; 3], -}); +}, 4, 0, 8 ); -trace!(MultiRangeCheck2Row, MultiRangeCheck2Trace { +trace!(MultiRangeCheck2Trace { a: [F; 2], sel: [F; 2], range_sel: [F; 2], -}); +}, 5, 0, 16 ); -trace!(RangeCheckDynamic1Row, RangeCheckDynamic1Trace { +trace!(RangeCheckDynamic1Trace { colu: F, sel_7: F, sel_8: F, sel_16: F, sel_17: F, -}); +}, 6, 0, 256 ); -trace!(RangeCheckDynamic2Row, RangeCheckDynamic2Trace { +trace!(RangeCheckDynamic2Trace { colu: F, sel_1: F, sel_2: F, sel_3: F, sel_4: F, sel_5: F, -}); +}, 7, 0, 64 ); -trace!(RangeCheckMixRow, RangeCheckMixTrace { +trace!(RangeCheckMixTrace { a: [F; 4], b: [F; 2], c: [F; 1], range_sel: [F; 5], -}); +}, 8, 0, 64 ); -trace!(U8AirRow, U8AirTrace { +trace!(U8AirTrace { mul: F, -}); +}, 9, 0, 256 ); -trace!(SpecifiedRangesRow, SpecifiedRangesTrace { +trace!(SpecifiedRangesTrace { mul: [F; 20], -}); +}, 10, 0, 131072 ); diff --git a/pil2-components/test/std/range_check/rs/src/range_check1.rs b/pil2-components/test/std/range_check/rs/src/range_check1.rs index a6192526..ad9fee08 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check1.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check1.rs @@ -1,14 +1,15 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheck1Trace, RANGE_CHECK_1_AIRGROUP_ID, RANGE_CHECK_1_AIR_IDS}; +use crate::RangeCheck1Trace; pub struct RangeCheck1 { std_lib: Arc>, @@ -20,33 +21,8 @@ where { const MY_NAME: &'static str = "RngChck1"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check1 = Arc::new(Self { std_lib }); - - wcm.register_component(range_check1.clone(), Some(RANGE_CHECK_1_AIRGROUP_ID), Some(RANGE_CHECK_1_AIR_IDS)); - - // Register dependency relations - range_check1.std_lib.register_predecessor(); - - range_check1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[RANGE_CHECK_1_AIRGROUP_ID][RANGE_CHECK_1_AIR_IDS[0]].num_rows; - let trace = RangeCheck1Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_1_AIRGROUP_ID, - RANGE_CHECK_1_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(RANGE_CHECK_1_AIRGROUP_ID, RANGE_CHECK_1_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -54,65 +30,53 @@ impl WitnessComponent for RangeCheck1 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheck1", stage); - - if stage == 1 { - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(RANGE_CHECK_1_AIRGROUP_ID, RANGE_CHECK_1_AIR_IDS[0]).num_rows(); + let mut trace = RangeCheck1Trace::new_zeroes(); + let num_rows = trace.num_rows(); - let mut trace = RangeCheck1Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); - let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); - let range3 = self.std_lib.get_range(BigInt::from(60), BigInt::from((1 << 16) - 1), Some(false)); - let range4 = self.std_lib.get_range(BigInt::from(8228), BigInt::from(17400), Some(false)); + let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); + let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); + let range3 = self.std_lib.get_range(BigInt::from(60), BigInt::from((1 << 16) - 1), Some(false)); + let range4 = self.std_lib.get_range(BigInt::from(8228), BigInt::from(17400), Some(false)); - for i in 0..num_rows { - let selected1 = rng.gen_bool(0.5); - trace[i].sel1 = F::from_bool(selected1); + for i in 0..num_rows { + let selected1 = rng.gen_bool(0.5); + trace[i].sel1 = F::from_bool(selected1); - let selected2 = rng.gen_bool(0.5); - trace[i].sel2 = F::from_bool(selected2); + let selected2 = rng.gen_bool(0.5); + trace[i].sel2 = F::from_bool(selected2); - let selected3 = rng.gen_bool(0.5); - trace[i].sel3 = F::from_bool(selected3); + let selected3 = rng.gen_bool(0.5); + trace[i].sel3 = F::from_bool(selected3); - if selected1 { - trace[i].a1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - trace[i].a3 = F::from_canonical_u32(rng.gen_range(60..=(1 << 16) - 1)); + if selected1 { + trace[i].a1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + trace[i].a3 = F::from_canonical_u32(rng.gen_range(60..=(1 << 16) - 1)); - self.std_lib.range_check(trace[i].a1, F::one(), range1); - self.std_lib.range_check(trace[i].a3, F::one(), range3); - } + self.std_lib.range_check(trace[i].a1, F::one(), range1); + self.std_lib.range_check(trace[i].a3, F::one(), range3); + } - if selected2 { - trace[i].a2 = F::from_canonical_u8(rng.gen_range(0..=(1 << 4) - 1)); - trace[i].a4 = F::from_canonical_u16(rng.gen_range(8228..=17400)); + if selected2 { + trace[i].a2 = F::from_canonical_u8(rng.gen_range(0..=(1 << 4) - 1)); + trace[i].a4 = F::from_canonical_u16(rng.gen_range(8228..=17400)); - self.std_lib.range_check(trace[i].a2, F::one(), range2); - self.std_lib.range_check(trace[i].a4, F::one(), range4); - } + self.std_lib.range_check(trace[i].a2, F::one(), range2); + self.std_lib.range_check(trace[i].a4, F::one(), range4); + } - if selected3 { - trace[i].a5 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + if selected3 { + trace[i].a5 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - self.std_lib.range_check(trace[i].a5, F::one(), range1); - } + self.std_lib.range_check(trace[i].a5, F::one(), range1); } } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/range_check2.rs b/pil2-components/test/std/range_check/rs/src/range_check2.rs index abecaee0..b4f673b5 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check2.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check2.rs @@ -1,14 +1,15 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheck2Trace, RANGE_CHECK_2_AIRGROUP_ID, RANGE_CHECK_2_AIR_IDS}; +use crate::RangeCheck2Trace; pub struct RangeCheck2 { std_lib: Arc>, @@ -20,33 +21,8 @@ where { const MY_NAME: &'static str = "RngChck2"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check1 = Arc::new(Self { std_lib }); - - wcm.register_component(range_check1.clone(), Some(RANGE_CHECK_2_AIRGROUP_ID), Some(RANGE_CHECK_2_AIR_IDS)); - - // Register dependency relations - range_check1.std_lib.register_predecessor(); - - range_check1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[RANGE_CHECK_2_AIRGROUP_ID][RANGE_CHECK_2_AIR_IDS[0]].num_rows; - let trace = RangeCheck2Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_2_AIRGROUP_ID, - RANGE_CHECK_2_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(RANGE_CHECK_2_AIRGROUP_ID, RANGE_CHECK_2_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -54,41 +30,28 @@ impl WitnessComponent for RangeCheck2 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); + let mut trace = RangeCheck2Trace::new_zeroes(); + let num_rows = trace.num_rows(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheck2", stage); - - if stage == 1 { - let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; - let num_rows = pctx.pilout.get_air(RANGE_CHECK_2_AIRGROUP_ID, RANGE_CHECK_2_AIR_IDS[0]).num_rows(); - - let mut trace = RangeCheck2Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); - let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 9) - 1), Some(false)); - let range3 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 10) - 1), Some(false)); + let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); + let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 9) - 1), Some(false)); + let range3 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 10) - 1), Some(false)); - for i in 0..num_rows { - trace[i].b1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - trace[i].b2 = F::from_canonical_u16(rng.gen_range(0..=(1 << 9) - 1)); - trace[i].b3 = F::from_canonical_u16(rng.gen_range(0..=(1 << 10) - 1)); + for i in 0..num_rows { + trace[i].b1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + trace[i].b2 = F::from_canonical_u16(rng.gen_range(0..=(1 << 9) - 1)); + trace[i].b3 = F::from_canonical_u16(rng.gen_range(0..=(1 << 10) - 1)); - self.std_lib.range_check(trace[i].b1, F::one(), range1); - self.std_lib.range_check(trace[i].b2, F::one(), range2); - self.std_lib.range_check(trace[i].b3, F::one(), range3); - } + self.std_lib.range_check(trace[i].b1, F::one(), range1); + self.std_lib.range_check(trace[i].b2, F::one(), range2); + self.std_lib.range_check(trace[i].b3, F::one(), range3); } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/range_check3.rs b/pil2-components/test/std/range_check/rs/src/range_check3.rs index 7e046a92..5d92f071 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check3.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check3.rs @@ -1,14 +1,15 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheck3Trace, RANGE_CHECK_3_AIRGROUP_ID, RANGE_CHECK_3_AIR_IDS}; +use crate::RangeCheck3Trace; pub struct RangeCheck3 { std_lib: Arc>, @@ -20,33 +21,8 @@ where { const MY_NAME: &'static str = "RngChck3"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check1 = Arc::new(Self { std_lib }); - - wcm.register_component(range_check1.clone(), Some(RANGE_CHECK_3_AIRGROUP_ID), Some(RANGE_CHECK_3_AIR_IDS)); - - // Register dependency relations - range_check1.std_lib.register_predecessor(); - - range_check1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[RANGE_CHECK_3_AIRGROUP_ID][RANGE_CHECK_3_AIR_IDS[0]].num_rows; - let trace = RangeCheck3Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_3_AIRGROUP_ID, - RANGE_CHECK_3_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(RANGE_CHECK_3_AIRGROUP_ID, RANGE_CHECK_3_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -54,39 +30,26 @@ impl WitnessComponent for RangeCheck3 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheck3", stage); - - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; + let mut trace = RangeCheck3Trace::new_zeroes(); + let num_rows = trace.num_rows(); - let num_rows = pctx.pilout.get_air(RANGE_CHECK_3_AIRGROUP_ID, RANGE_CHECK_3_AIR_IDS[0]).num_rows(); - let mut trace = RangeCheck3Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); - let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); + let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); + let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); - for i in 0..num_rows { - trace[i].c1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 4) - 1)); - trace[i].c2 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + for i in 0..num_rows { + trace[i].c1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 4) - 1)); + trace[i].c2 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - self.std_lib.range_check(trace[i].c1, F::one(), range1); - self.std_lib.range_check(trace[i].c2, F::one(), range2); - } + self.std_lib.range_check(trace[i].c1, F::one(), range1); + self.std_lib.range_check(trace[i].c2, F::one(), range2); } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/range_check4.rs b/pil2-components/test/std/range_check/rs/src/range_check4.rs index 90fe9e1c..0430c315 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check4.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check4.rs @@ -1,15 +1,16 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use num_traits::ToPrimitive; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheck4Trace, RANGE_CHECK_4_AIRGROUP_ID, RANGE_CHECK_4_AIR_IDS}; +use crate::RangeCheck4Trace; pub struct RangeCheck4 { std_lib: Arc>, @@ -21,33 +22,8 @@ where { const MY_NAME: &'static str = "RngChck4"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check4 = Arc::new(Self { std_lib }); - - wcm.register_component(range_check4.clone(), Some(RANGE_CHECK_4_AIRGROUP_ID), Some(RANGE_CHECK_4_AIR_IDS)); - - // Register dependency relations - range_check4.std_lib.register_predecessor(); - - range_check4 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[RANGE_CHECK_4_AIRGROUP_ID][RANGE_CHECK_4_AIR_IDS[0]].num_rows; - let trace = RangeCheck4Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_4_AIRGROUP_ID, - RANGE_CHECK_4_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(RANGE_CHECK_4_AIRGROUP_ID, RANGE_CHECK_4_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -55,86 +31,73 @@ impl WitnessComponent for RangeCheck4 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheck4", stage); - - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; - - let num_rows = pctx.pilout.get_air(RANGE_CHECK_4_AIRGROUP_ID, RANGE_CHECK_4_AIR_IDS[0]).num_rows(); - let mut trace = RangeCheck4Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 16) - 1), Some(true)); - let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(true)); - let range3 = self.std_lib.get_range(BigInt::from(50), BigInt::from((1 << 7) - 1), Some(true)); - let range4 = self.std_lib.get_range(BigInt::from(127), BigInt::from(1 << 8), Some(true)); - let range5 = self.std_lib.get_range(BigInt::from(1), BigInt::from((1 << 16) + 1), Some(true)); - let range6 = self.std_lib.get_range(BigInt::from(127), BigInt::from(1 << 16), Some(true)); - let range7 = self.std_lib.get_range(BigInt::from(-1), BigInt::from(1 << 3), Some(true)); - let range8 = self.std_lib.get_range(BigInt::from(-(1 << 7) + 1), BigInt::from(-50), Some(true)); - let range9 = self.std_lib.get_range(BigInt::from(-(1 << 8) + 1), BigInt::from(-127), Some(true)); - - for i in 0..num_rows { - let selected1 = rng.gen_bool(0.5); - trace[i].sel1 = F::from_bool(selected1); - - // selected1 and selected2 have to be disjoint for the range check to pass - let selected2 = if selected1 { false } else { rng.gen_bool(0.5) }; - trace[i].sel2 = F::from_bool(selected2); - - if selected1 { - trace[i].a1 = F::from_canonical_u32(rng.gen_range(0..=(1 << 16) - 1)); - trace[i].a5 = F::from_canonical_u32(rng.gen_range(127..=(1 << 16))); - let mut a6_val: i128 = rng.gen_range(-1..=2i128.pow(3)); - if a6_val < 0 { - a6_val += F::order().to_i128().unwrap(); - } - trace[i].a6 = F::from_canonical_u64(a6_val as u64); - - self.std_lib.range_check(trace[i].a1, F::one(), range1); - self.std_lib.range_check(trace[i].a5, F::one(), range6); - self.std_lib.range_check(trace[i].a6, F::one(), range7); - } - if selected2 { - trace[i].a1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - trace[i].a2 = F::from_canonical_u8(rng.gen_range(50..=(1 << 7) - 1)); - trace[i].a3 = F::from_canonical_u16(rng.gen_range(127..=(1 << 8))); - trace[i].a4 = F::from_canonical_u32(rng.gen_range(1..=(1 << 16) + 1)); - - self.std_lib.range_check(trace[i].a1, F::one(), range2); - self.std_lib.range_check(trace[i].a2, F::one(), range3); - self.std_lib.range_check(trace[i].a3, F::one(), range4); - self.std_lib.range_check(trace[i].a4, F::one(), range5); + let mut trace = RangeCheck4Trace::new_zeroes(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 16) - 1), Some(true)); + let range2 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(true)); + let range3 = self.std_lib.get_range(BigInt::from(50), BigInt::from((1 << 7) - 1), Some(true)); + let range4 = self.std_lib.get_range(BigInt::from(127), BigInt::from(1 << 8), Some(true)); + let range5 = self.std_lib.get_range(BigInt::from(1), BigInt::from((1 << 16) + 1), Some(true)); + let range6 = self.std_lib.get_range(BigInt::from(127), BigInt::from(1 << 16), Some(true)); + let range7 = self.std_lib.get_range(BigInt::from(-1), BigInt::from(1 << 3), Some(true)); + let range8 = self.std_lib.get_range(BigInt::from(-(1 << 7) + 1), BigInt::from(-50), Some(true)); + let range9 = self.std_lib.get_range(BigInt::from(-(1 << 8) + 1), BigInt::from(-127), Some(true)); + + for i in 0..num_rows { + let selected1 = rng.gen_bool(0.5); + trace[i].sel1 = F::from_bool(selected1); + + // selected1 and selected2 have to be disjoint for the range check to pass + let selected2 = if selected1 { false } else { rng.gen_bool(0.5) }; + trace[i].sel2 = F::from_bool(selected2); + + if selected1 { + trace[i].a1 = F::from_canonical_u32(rng.gen_range(0..=(1 << 16) - 1)); + trace[i].a5 = F::from_canonical_u32(rng.gen_range(127..=(1 << 16))); + let mut a6_val: i128 = rng.gen_range(-1..=2i128.pow(3)); + if a6_val < 0 { + a6_val += F::order().to_i128().unwrap(); } + trace[i].a6 = F::from_canonical_u64(a6_val as u64); - let mut a7_val: i128 = rng.gen_range(-(2i128.pow(7)) + 1..=-50); - if a7_val < 0 { - a7_val += F::order().to_i128().unwrap(); - } - trace[i].a7 = F::from_canonical_u64(a7_val as u64); - self.std_lib.range_check(trace[i].a7, F::one(), range8); + self.std_lib.range_check(trace[i].a1, F::one(), range1); + self.std_lib.range_check(trace[i].a5, F::one(), range6); + self.std_lib.range_check(trace[i].a6, F::one(), range7); + } + if selected2 { + trace[i].a1 = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + trace[i].a2 = F::from_canonical_u8(rng.gen_range(50..=(1 << 7) - 1)); + trace[i].a3 = F::from_canonical_u16(rng.gen_range(127..=(1 << 8))); + trace[i].a4 = F::from_canonical_u32(rng.gen_range(1..=(1 << 16) + 1)); + + self.std_lib.range_check(trace[i].a1, F::one(), range2); + self.std_lib.range_check(trace[i].a2, F::one(), range3); + self.std_lib.range_check(trace[i].a3, F::one(), range4); + self.std_lib.range_check(trace[i].a4, F::one(), range5); + } - let mut a8_val: i128 = rng.gen_range(-(2i128.pow(8)) + 1..=-127); - if a8_val < 0 { - a8_val += F::order().to_i128().unwrap(); - } - trace[i].a8 = F::from_canonical_u64(a8_val as u64); - self.std_lib.range_check(trace[i].a8, F::one(), range9); + let mut a7_val: i128 = rng.gen_range(-(2i128.pow(7)) + 1..=-50); + if a7_val < 0 { + a7_val += F::order().to_i128().unwrap(); + } + trace[i].a7 = F::from_canonical_u64(a7_val as u64); + self.std_lib.range_check(trace[i].a7, F::one(), range8); + + let mut a8_val: i128 = rng.gen_range(-(2i128.pow(8)) + 1..=-127); + if a8_val < 0 { + a8_val += F::order().to_i128().unwrap(); } + trace[i].a8 = F::from_canonical_u64(a8_val as u64); + self.std_lib.range_check(trace[i].a8, F::one(), range9); } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/range_check_dynamic1.rs b/pil2-components/test/std/range_check/rs/src/range_check_dynamic1.rs index 020360dd..82c6eea2 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check_dynamic1.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check_dynamic1.rs @@ -1,14 +1,15 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheckDynamic1Trace, RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID, RANGE_CHECK_DYNAMIC_1_AIR_IDS}; +use crate::RangeCheckDynamic1Trace; pub struct RangeCheckDynamic1 { std_lib: Arc>, @@ -20,41 +21,8 @@ where { const MY_NAME: &'static str = "RngChDy1"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check_dynamic1 = Arc::new(Self { std_lib }); - - wcm.register_component( - range_check_dynamic1.clone(), - Some(RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID), - Some(RANGE_CHECK_DYNAMIC_1_AIR_IDS), - ); - - // Register dependency relations - range_check_dynamic1.std_lib.register_predecessor(); - - range_check_dynamic1 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = - pctx.global_info.airs[RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID][RANGE_CHECK_DYNAMIC_1_AIR_IDS[0]].num_rows; - let trace = RangeCheckDynamic1Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID, - RANGE_CHECK_DYNAMIC_1_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance( - RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID, - RANGE_CHECK_DYNAMIC_1_AIR_IDS[0], - 1, - ); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -62,66 +30,52 @@ impl WitnessComponent for RangeCheckDynamic1 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheckDynamic1", stage); - - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; - - let num_rows = - pctx.pilout.get_air(RANGE_CHECK_DYNAMIC_1_AIRGROUP_ID, RANGE_CHECK_DYNAMIC_1_AIR_IDS[0]).num_rows(); - let mut trace = RangeCheckDynamic1Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); - - let range7 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); - let range8 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); - let range16 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 16) - 1), Some(false)); - let range17 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 17) - 1), Some(false)); - - for i in 0..num_rows { - let range = rng.gen_range(0..=3); - - match range { - 0 => { - trace[i].sel_7 = F::one(); - trace[i].colu = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); - - self.std_lib.range_check(trace[i].colu, F::one(), range7); - } - 1 => { - trace[i].sel_8 = F::one(); - trace[i].colu = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - - self.std_lib.range_check(trace[i].colu, F::one(), range8); - } - 2 => { - trace[i].sel_16 = F::one(); - trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 16) - 1)); - - self.std_lib.range_check(trace[i].colu, F::one(), range16); - } - 3 => { - trace[i].sel_17 = F::one(); - trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 17) - 1)); - - self.std_lib.range_check(trace[i].colu, F::one(), range17); - } - _ => panic!("Invalid range"), + let mut trace = RangeCheckDynamic1Trace::new_zeroes(); + let num_rows = trace.num_rows(); + + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); + + let range7 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); + let range8 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); + let range16 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 16) - 1), Some(false)); + let range17 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 17) - 1), Some(false)); + + for i in 0..num_rows { + let range = rng.gen_range(0..=3); + + match range { + 0 => { + trace[i].sel_7 = F::one(); + trace[i].colu = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); + + self.std_lib.range_check(trace[i].colu, F::one(), range7); + } + 1 => { + trace[i].sel_8 = F::one(); + trace[i].colu = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + + self.std_lib.range_check(trace[i].colu, F::one(), range8); + } + 2 => { + trace[i].sel_16 = F::one(); + trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 16) - 1)); + + self.std_lib.range_check(trace[i].colu, F::one(), range16); + } + 3 => { + trace[i].sel_17 = F::one(); + trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 17) - 1)); + + self.std_lib.range_check(trace[i].colu, F::one(), range17); } + _ => panic!("Invalid range"), } } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/range_check_dynamic2.rs b/pil2-components/test/std/range_check/rs/src/range_check_dynamic2.rs index f13575aa..2eed62e0 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check_dynamic2.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check_dynamic2.rs @@ -1,15 +1,16 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use num_traits::ToPrimitive; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheckDynamic2Trace, RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID, RANGE_CHECK_DYNAMIC_2_AIR_IDS}; +use crate::RangeCheckDynamic2Trace; pub struct RangeCheckDynamic2 { std_lib: Arc>, @@ -21,41 +22,8 @@ where { const MY_NAME: &'static str = "RngChDy2"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check_dynamic2 = Arc::new(Self { std_lib }); - - wcm.register_component( - range_check_dynamic2.clone(), - Some(RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID), - Some(RANGE_CHECK_DYNAMIC_2_AIR_IDS), - ); - - // Register dependency relations - range_check_dynamic2.std_lib.register_predecessor(); - - range_check_dynamic2 - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = - pctx.global_info.airs[RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID][RANGE_CHECK_DYNAMIC_2_AIR_IDS[0]].num_rows; - let trace = RangeCheckDynamic2Trace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID, - RANGE_CHECK_DYNAMIC_2_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance( - RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID, - RANGE_CHECK_DYNAMIC_2_AIR_IDS[0], - 1, - ); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -63,78 +31,64 @@ impl WitnessComponent for RangeCheckDynamic2 where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheckDynamic2", stage); + let mut trace = RangeCheckDynamic2Trace::new_zeroes(); + let num_rows = trace.num_rows(); - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let num_rows = - pctx.pilout.get_air(RANGE_CHECK_DYNAMIC_2_AIRGROUP_ID, RANGE_CHECK_DYNAMIC_2_AIR_IDS[0]).num_rows(); - let mut trace = RangeCheckDynamic2Trace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + let range1 = self.std_lib.get_range(BigInt::from(5225), BigInt::from(29023), Some(false)); + let range2 = self.std_lib.get_range(BigInt::from(-8719), BigInt::from(-7269), Some(false)); + let range3 = self.std_lib.get_range(BigInt::from(-10), BigInt::from(10), Some(false)); + let range4 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); + let range5 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); - let range1 = self.std_lib.get_range(BigInt::from(5225), BigInt::from(29023), Some(false)); - let range2 = self.std_lib.get_range(BigInt::from(-8719), BigInt::from(-7269), Some(false)); - let range3 = self.std_lib.get_range(BigInt::from(-10), BigInt::from(10), Some(false)); - let range4 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), Some(false)); - let range5 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); + for i in 0..num_rows { + let range = rng.gen_range(0..=4); - for i in 0..num_rows { - let range = rng.gen_range(0..=4); + match range { + 0 => { + trace[i].sel_1 = F::one(); + trace[i].colu = F::from_canonical_u16(rng.gen_range(5225..=29023)); - match range { - 0 => { - trace[i].sel_1 = F::one(); - trace[i].colu = F::from_canonical_u16(rng.gen_range(5225..=29023)); + self.std_lib.range_check(trace[i].colu, F::one(), range1); + } + 1 => { + trace[i].sel_2 = F::one(); + let colu_val = rng.gen_range(-8719..=-7269) + F::order().to_i128().unwrap(); + trace[i].colu = F::from_canonical_u64(colu_val as u64); - self.std_lib.range_check(trace[i].colu, F::one(), range1); + self.std_lib.range_check(trace[i].colu, F::one(), range2); + } + 2 => { + trace[i].sel_3 = F::one(); + let mut colu_val: i128 = rng.gen_range(-10..=10); + if colu_val < 0 { + colu_val += F::order().to_i128().unwrap(); } - 1 => { - trace[i].sel_2 = F::one(); - let colu_val = rng.gen_range(-8719..=-7269) + F::order().to_i128().unwrap(); - trace[i].colu = F::from_canonical_u64(colu_val as u64); + trace[i].colu = F::from_canonical_u64(colu_val as u64); - self.std_lib.range_check(trace[i].colu, F::one(), range2); - } - 2 => { - trace[i].sel_3 = F::one(); - let mut colu_val: i128 = rng.gen_range(-10..=10); - if colu_val < 0 { - colu_val += F::order().to_i128().unwrap(); - } - trace[i].colu = F::from_canonical_u64(colu_val as u64); - - self.std_lib.range_check(trace[i].colu, F::one(), range3); - } - 3 => { - trace[i].sel_4 = F::one(); - trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 8) - 1)); + self.std_lib.range_check(trace[i].colu, F::one(), range3); + } + 3 => { + trace[i].sel_4 = F::one(); + trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 8) - 1)); - self.std_lib.range_check(trace[i].colu, F::one(), range4); - } - 4 => { - trace[i].sel_5 = F::one(); - trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 7) - 1)); + self.std_lib.range_check(trace[i].colu, F::one(), range4); + } + 4 => { + trace[i].sel_5 = F::one(); + trace[i].colu = F::from_canonical_u32(rng.gen_range(0..=(1 << 7) - 1)); - self.std_lib.range_check(trace[i].colu, F::one(), range5); - } - _ => panic!("Invalid range"), + self.std_lib.range_check(trace[i].colu, F::one(), range5); } + _ => panic!("Invalid range"), } } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-components/test/std/range_check/rs/src/range_check_lib.rs b/pil2-components/test/std/range_check/rs/src/range_check_lib.rs index 0f04caa6..8d5e0550 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check_lib.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check_lib.rs @@ -1,157 +1,43 @@ -use std::{cell::OnceCell, error::Error, path::PathBuf, sync::Arc}; +use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessLibrary, WitnessManager}; -use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; +use witness::{witness_library, WitnessLibrary, WitnessManager}; use p3_field::PrimeField; use p3_goldilocks::Goldilocks; use rand::{distributions::Standard, prelude::Distribution}; use crate::{ - RangeCheckMix, RangeCheckDynamic1, RangeCheckDynamic2, MultiRangeCheck1, MultiRangeCheck2, Pilout, RangeCheck1, + RangeCheckMix, RangeCheckDynamic1, RangeCheckDynamic2, MultiRangeCheck1, MultiRangeCheck2, RangeCheck1, RangeCheck2, RangeCheck3, RangeCheck4, }; -pub struct RangeCheckWitness { - pub wcm: OnceCell>>, - pub range_check1: OnceCell>>, - pub range_check2: OnceCell>>, - pub range_check3: OnceCell>>, - pub range_check4: OnceCell>>, - pub multi_range_check1: OnceCell>>, - pub multi_range_check2: OnceCell>>, - pub range_check_dynamic1: OnceCell>>, - pub range_check_dynamic2: OnceCell>>, - pub range_check_mix: OnceCell>>, - pub std_lib: OnceCell>>, -} - -impl Default for RangeCheckWitness -where - Standard: Distribution, -{ - fn default() -> Self { - Self::new() - } -} +witness_library!(WitnessLib, Goldilocks); -impl RangeCheckWitness +impl WitnessLibrary for WitnessLib where Standard: Distribution, { - pub fn new() -> Self { - RangeCheckWitness { - wcm: OnceCell::new(), - range_check1: OnceCell::new(), - range_check2: OnceCell::new(), - range_check3: OnceCell::new(), - range_check4: OnceCell::new(), - multi_range_check1: OnceCell::new(), - multi_range_check2: OnceCell::new(), - range_check_dynamic1: OnceCell::new(), - range_check_dynamic2: OnceCell::new(), - range_check_mix: OnceCell::new(), - std_lib: OnceCell::new(), - } - } - - fn initialize(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); - + fn register_witness(&mut self, wcm: Arc>) { let std_lib = Std::new(wcm.clone()); - let range_check1 = RangeCheck1::new(wcm.clone(), std_lib.clone()); - let range_check2 = RangeCheck2::new(wcm.clone(), std_lib.clone()); - let range_check3 = RangeCheck3::new(wcm.clone(), std_lib.clone()); - let range_check4 = RangeCheck4::new(wcm.clone(), std_lib.clone()); - let multi_range_check1 = MultiRangeCheck1::new(wcm.clone(), std_lib.clone()); - let multi_range_check2 = MultiRangeCheck2::new(wcm.clone(), std_lib.clone()); - let range_check_dynamic1 = RangeCheckDynamic1::new(wcm.clone(), std_lib.clone()); - let range_check_dynamic2 = RangeCheckDynamic2::new(wcm.clone(), std_lib.clone()); - let range_check_mix = RangeCheckMix::new(wcm.clone(), std_lib.clone()); - - let _ = self.wcm.set(wcm); - let _ = self.range_check1.set(range_check1); - let _ = self.range_check2.set(range_check2); - let _ = self.range_check3.set(range_check3); - let _ = self.range_check4.set(range_check4); - let _ = self.multi_range_check1.set(multi_range_check1); - let _ = self.multi_range_check2.set(multi_range_check2); - let _ = self.range_check_dynamic1.set(range_check_dynamic1); - let _ = self.range_check_dynamic2.set(range_check_dynamic2); - let _ = self.range_check_mix.set(range_check_mix); - let _ = self.std_lib.set(std_lib); - } -} - -impl WitnessLibrary for RangeCheckWitness -where - Standard: Distribution, -{ - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); - - self.wcm.get().unwrap().start_proof(pctx, ectx, sctx); - } - - fn end_proof(&mut self) { - self.wcm.get().unwrap().end_proof(); - } - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - // Execute those components that need to be executed - self.range_check1.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.range_check2.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.range_check3.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.range_check4.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.multi_range_check1.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.multi_range_check2.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.range_check_dynamic1.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.range_check_dynamic2.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - self.range_check_mix.get().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); - } - - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - self.wcm.get().unwrap().calculate_witness(stage, pctx, ectx, sctx); + let range_check1 = RangeCheck1::new(std_lib.clone()); + let range_check2 = RangeCheck2::new(std_lib.clone()); + let range_check3 = RangeCheck3::new(std_lib.clone()); + let range_check4 = RangeCheck4::new(std_lib.clone()); + let multi_range_check1 = MultiRangeCheck1::new(std_lib.clone()); + let multi_range_check2 = MultiRangeCheck2::new(std_lib.clone()); + let range_check_dynamic1 = RangeCheckDynamic1::new(std_lib.clone()); + let range_check_dynamic2 = RangeCheckDynamic2::new(std_lib.clone()); + let range_check_mix = RangeCheckMix::new(std_lib.clone()); + + wcm.register_component(range_check1.clone()); + wcm.register_component(range_check2.clone()); + wcm.register_component(range_check3.clone()); + wcm.register_component(range_check4.clone()); + wcm.register_component(multi_range_check1.clone()); + wcm.register_component(multi_range_check2.clone()); + wcm.register_component(range_check_dynamic1.clone()); + wcm.register_component(range_check_dynamic2.clone()); + wcm.register_component(range_check_mix.clone()); } - - fn pilout(&self) -> WitnessPilout { - Pilout::pilout() - } -} - -#[no_mangle] -pub extern "Rust" fn init_library( - _: Option, - _: Option, - verbose_mode: VerboseMode, -) -> Result>, Box> { - initialize_logger(verbose_mode); - let range_check_witness = RangeCheckWitness::new(); - Ok(Box::new(range_check_witness)) } - -// #[cfg(test)] -// mod tests { -// use proofman_cli::commands::verify_constraints::{Field, VerifyConstraintsCmd}; - -// #[test] -// fn test_verify_constraints() { -// let root_path = std::env::current_dir().expect("Failed to get current directory").join("../../../../"); -// let root_path = std::fs::canonicalize(root_path).expect("Failed to canonicalize root path"); - -// let verify_constraints = VerifyConstraintsCmd { -// witness_lib: root_path.join("target/debug/librange_check.so"), -// rom: None, -// public_inputs: None, -// proving_key: root_path.join("test/std/range_check/build/provingKey"), -// field: Field::Goldilocks, -// verbose: 0, -// }; - -// if let Err(e) = verify_constraints.run() { -// eprintln!("Failed to verify constraints: {:?}", e); -// std::process::exit(1); -// } -// } -// } diff --git a/pil2-components/test/std/range_check/rs/src/range_check_mix.rs b/pil2-components/test/std/range_check/rs/src/range_check_mix.rs index 197568bc..25003a85 100644 --- a/pil2-components/test/std/range_check/rs/src/range_check_mix.rs +++ b/pil2-components/test/std/range_check/rs/src/range_check_mix.rs @@ -1,15 +1,16 @@ use std::sync::Arc; use pil_std_lib::Std; -use proofman::{WitnessComponent, WitnessManager}; -use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; +use witness::WitnessComponent; + +use proofman_common::{add_air_instance, FromTrace, AirInstance, ProofCtx}; use num_bigint::BigInt; use num_traits::ToPrimitive; use p3_field::PrimeField; use rand::{distributions::Standard, prelude::Distribution, Rng}; -use crate::{RangeCheckMixTrace, RANGE_CHECK_MIX_AIRGROUP_ID, RANGE_CHECK_MIX_AIR_IDS}; +use crate::RangeCheckMixTrace; pub struct RangeCheckMix { std_lib: Arc>, @@ -21,37 +22,8 @@ where { const MY_NAME: &'static str = "RngChMix"; - pub fn new(wcm: Arc>, std_lib: Arc>) -> Arc { - let range_check_mix = Arc::new(Self { std_lib }); - - wcm.register_component( - range_check_mix.clone(), - Some(RANGE_CHECK_MIX_AIRGROUP_ID), - Some(RANGE_CHECK_MIX_AIR_IDS), - ); - - // Register dependency relations - range_check_mix.std_lib.register_predecessor(); - - range_check_mix - } - - pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - let num_rows = pctx.global_info.airs[RANGE_CHECK_MIX_AIRGROUP_ID][RANGE_CHECK_MIX_AIR_IDS[0]].num_rows; - let trace = RangeCheckMixTrace::new(num_rows); - - let air_instance = AirInstance::new( - sctx.clone(), - RANGE_CHECK_MIX_AIRGROUP_ID, - RANGE_CHECK_MIX_AIR_IDS[0], - None, - trace.buffer.unwrap(), - ); - let (is_myne, gid) = - ectx.dctx.write().unwrap().add_instance(RANGE_CHECK_MIX_AIRGROUP_ID, RANGE_CHECK_MIX_AIR_IDS[0], 1); - if is_myne { - pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); - } + pub fn new(std_lib: Arc>) -> Arc { + Arc::new(Self { std_lib }) } } @@ -59,118 +31,105 @@ impl WitnessComponent for RangeCheckMix where Standard: Distribution, { - fn calculate_witness( - &self, - stage: u32, - air_instance_id: Option, - pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { + fn execute(&self, pctx: Arc>) { let mut rng = rand::thread_rng(); - log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "RangeCheckMix", stage); - - if stage == 1 { - let proof_ctx = pctx.clone(); - let air_instances_vec = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap(); - let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; - let buffer = &mut air_instance.trace; + let mut trace = RangeCheckMixTrace::new_zeroes(); + let num_rows = trace.num_rows(); - let num_rows = pctx.pilout.get_air(RANGE_CHECK_MIX_AIRGROUP_ID, RANGE_CHECK_MIX_AIR_IDS[0]).num_rows(); - let mut trace = RangeCheckMixTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + log::debug!("{} ··· Starting witness computation stage {}", Self::MY_NAME, 1); - let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), None); - let range2 = self.std_lib.get_range(BigInt::from(50), BigInt::from((1 << 7) - 1), None); - let range3 = self.std_lib.get_range(BigInt::from(-1), BigInt::from(1 << 3), None); - let range4 = self.std_lib.get_range(BigInt::from(-(1 << 7) + 1), BigInt::from(-50), None); + let range1 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 8) - 1), None); + let range2 = self.std_lib.get_range(BigInt::from(50), BigInt::from((1 << 7) - 1), None); + let range3 = self.std_lib.get_range(BigInt::from(-1), BigInt::from(1 << 3), None); + let range4 = self.std_lib.get_range(BigInt::from(-(1 << 7) + 1), BigInt::from(-50), None); - let range5 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); - let range6 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); - let range7 = self.std_lib.get_range(BigInt::from(1 << 5), BigInt::from((1 << 8) - 1), Some(false)); - let range8 = self.std_lib.get_range(BigInt::from(1 << 8), BigInt::from((1 << 9) - 1), Some(false)); + let range5 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 7) - 1), Some(false)); + let range6 = self.std_lib.get_range(BigInt::from(0), BigInt::from((1 << 4) - 1), Some(false)); + let range7 = self.std_lib.get_range(BigInt::from(1 << 5), BigInt::from((1 << 8) - 1), Some(false)); + let range8 = self.std_lib.get_range(BigInt::from(1 << 8), BigInt::from((1 << 9) - 1), Some(false)); - let range9 = self.std_lib.get_range(BigInt::from(5225), BigInt::from(29023), Some(false)); - // let range10 = self.std_lib.get_range(BigInt::from(-8719), BigInt::from(-7269), Some(false)); - let range11 = self.std_lib.get_range(BigInt::from(-10), BigInt::from(10), Some(false)); + let range9 = self.std_lib.get_range(BigInt::from(5225), BigInt::from(29023), Some(false)); + // let range10 = self.std_lib.get_range(BigInt::from(-8719), BigInt::from(-7269), Some(false)); + let range11 = self.std_lib.get_range(BigInt::from(-10), BigInt::from(10), Some(false)); - for i in 0..num_rows { - // First interface - trace[i].a[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); - self.std_lib.range_check(trace[i].a[0], F::one(), range1); + for i in 0..num_rows { + // First interface + trace[i].a[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 8) - 1)); + self.std_lib.range_check(trace[i].a[0], F::one(), range1); - trace[i].a[1] = F::from_canonical_u8(rng.gen_range(50..=(1 << 7) - 1)); - self.std_lib.range_check(trace[i].a[1], F::one(), range2); + trace[i].a[1] = F::from_canonical_u8(rng.gen_range(50..=(1 << 7) - 1)); + self.std_lib.range_check(trace[i].a[1], F::one(), range2); - let mut a2_val: i128 = rng.gen_range(-1..=2i128.pow(3)); - if a2_val < 0 { - a2_val += F::order().to_i128().unwrap(); - } - trace[i].a[2] = F::from_canonical_u64(a2_val as u64); - self.std_lib.range_check(trace[i].a[2], F::one(), range3); + let mut a2_val: i128 = rng.gen_range(-1..=2i128.pow(3)); + if a2_val < 0 { + a2_val += F::order().to_i128().unwrap(); + } + trace[i].a[2] = F::from_canonical_u64(a2_val as u64); + self.std_lib.range_check(trace[i].a[2], F::one(), range3); - let a3_val = rng.gen_range(-(2i128.pow(7)) + 1..=-50) + F::order().to_i128().unwrap(); - trace[i].a[3] = F::from_canonical_u64(a3_val as u64); - self.std_lib.range_check(trace[i].a[3], F::one(), range4); + let a3_val = rng.gen_range(-(2i128.pow(7)) + 1..=-50) + F::order().to_i128().unwrap(); + trace[i].a[3] = F::from_canonical_u64(a3_val as u64); + self.std_lib.range_check(trace[i].a[3], F::one(), range4); - // Second interface - let range_selector1 = rng.gen_bool(0.5); - trace[i].range_sel[0] = F::from_bool(range_selector1); + // Second interface + let range_selector1 = rng.gen_bool(0.5); + trace[i].range_sel[0] = F::from_bool(range_selector1); - let range_selector2 = rng.gen_bool(0.5); - trace[i].range_sel[1] = F::from_bool(range_selector2); + let range_selector2 = rng.gen_bool(0.5); + trace[i].range_sel[1] = F::from_bool(range_selector2); - if range_selector1 { - trace[i].b[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); + if range_selector1 { + trace[i].b[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 7) - 1)); - self.std_lib.range_check(trace[i].b[0], F::one(), range5); - } else { - trace[i].b[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 4) - 1)); + self.std_lib.range_check(trace[i].b[0], F::one(), range5); + } else { + trace[i].b[0] = F::from_canonical_u16(rng.gen_range(0..=(1 << 4) - 1)); - self.std_lib.range_check(trace[i].b[0], F::one(), range6); - } + self.std_lib.range_check(trace[i].b[0], F::one(), range6); + } - if range_selector2 { - trace[i].b[1] = F::from_canonical_u16(rng.gen_range((1 << 5)..=(1 << 8) - 1)); + if range_selector2 { + trace[i].b[1] = F::from_canonical_u16(rng.gen_range((1 << 5)..=(1 << 8) - 1)); - self.std_lib.range_check(trace[i].b[1], F::one(), range7); - } else { - trace[i].b[1] = F::from_canonical_u16(rng.gen_range((1 << 8)..=(1 << 9) - 1)); + self.std_lib.range_check(trace[i].b[1], F::one(), range7); + } else { + trace[i].b[1] = F::from_canonical_u16(rng.gen_range((1 << 8)..=(1 << 9) - 1)); - self.std_lib.range_check(trace[i].b[1], F::one(), range8); - } + self.std_lib.range_check(trace[i].b[1], F::one(), range8); + } - // Third interface - let range = rng.gen_range(0..=2); + // Third interface + let range = rng.gen_range(0..=2); - match range { - 0 => { - trace[i].range_sel[2] = F::one(); - trace[i].c[0] = F::from_canonical_u32(rng.gen_range(5225..=29023)); + match range { + 0 => { + trace[i].range_sel[2] = F::one(); + trace[i].c[0] = F::from_canonical_u32(rng.gen_range(5225..=29023)); - self.std_lib.range_check(trace[i].c[0], F::one(), range9); - } - 1 => { - trace[i].range_sel[3] = F::one(); - let mut colu_val: i128 = rng.gen_range(-10..=10); - if colu_val < 0 { - colu_val += F::order().to_i128().unwrap(); - } - trace[i].c[0] = F::from_canonical_u64(colu_val as u64); - - self.std_lib.range_check(trace[i].c[0], F::one(), range11); + self.std_lib.range_check(trace[i].c[0], F::one(), range9); + } + 1 => { + trace[i].range_sel[3] = F::one(); + let mut colu_val: i128 = rng.gen_range(-10..=10); + if colu_val < 0 { + colu_val += F::order().to_i128().unwrap(); } - 2 => { - trace[i].range_sel[4] = F::one(); - trace[i].c[0] = F::from_canonical_u32(rng.gen_range(0..=(1 << 7) - 1)); + trace[i].c[0] = F::from_canonical_u64(colu_val as u64); - self.std_lib.range_check(trace[i].c[0], F::one(), range5); - } - _ => panic!("Invalid range"), + self.std_lib.range_check(trace[i].c[0], F::one(), range11); + } + 2 => { + trace[i].range_sel[4] = F::one(); + trace[i].c[0] = F::from_canonical_u32(rng.gen_range(0..=(1 << 7) - 1)); + + self.std_lib.range_check(trace[i].c[0], F::one(), range5); } + _ => panic!("Invalid range"), } } - self.std_lib.unregister_predecessor(pctx, None); + let air_instance = AirInstance::new_from_trace(FromTrace::new(&mut trace)); + add_air_instance::(air_instance, pctx.clone()); } } diff --git a/pil2-stark/lib/include/starks_lib.h b/pil2-stark/lib/include/starks_lib.h index 0c24fbf4..0712cb4f 100644 --- a/pil2-stark/lib/include/starks_lib.h +++ b/pil2-stark/lib/include/starks_lib.h @@ -6,7 +6,7 @@ // ======================================================================================== void save_challenges(void *pChallenges, char* globalInfoFile, char *fileDir); void save_publics(unsigned long numPublicInputs, void *pPublicInputs, char *fileDir); - void save_proof_values(unsigned long numProofValues, void *pProofValues, char *fileDir); + void save_proof_values(void *pProofValues, char* globalInfoFile, char *fileDir); // FRIProof // ======================================================================================== @@ -20,15 +20,13 @@ // SetupCtx // ======================================================================================== - void *get_hint_ids_by_name(void *p_expression_bin, char* hintName); + uint64_t n_hints_by_name(void *p_expression_bin, char* hintName); + void get_hint_ids_by_name(void *p_expression_bin, uint64_t* hintIds, char* hintName); // Stark Info // ======================================================================================== void *stark_info_new(char* filename); uint64_t get_map_total_n(void *pStarkInfo); - uint64_t get_map_total_n_custom_commits(void *pStarkInfo, uint64_t commit_id); - uint64_t get_map_offsets(void *pStarkInfo, char *stage, bool flag); - void *get_custom_commit_map_ids(void *pStarkInfo, uint64_t commit_id, uint64_t stage); void stark_info_free(void *pStarkInfo); // Prover Helpers @@ -51,12 +49,15 @@ // Hints // ======================================================================================== - void *get_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char* hintFieldName, void* hintOptions); + void get_hint_field(void *pSetupCtx, void* stepsParams, void* hintFieldValues, uint64_t hintId, char* hintFieldName, void* hintOptions); + uint64_t get_hint_field_values(void *pSetupCtx, uint64_t hintId, char* hintFieldName); + void get_hint_field_sizes(void *pSetupCtx, void* hintFieldValues, uint64_t hintId, char* hintFieldName, void* hintOptions); uint64_t mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2); - void *acc_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName, bool add); - void *acc_mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); - void *update_airgroupvalue(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); + void acc_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName, bool add); + void acc_mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); + uint64_t update_airgroupvalue(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); uint64_t set_hint_field(void *pSetupCtx, void* stepsParams, void *values, uint64_t hintId, char* hintFieldName); + uint64_t get_hint_id(void *pSetupCtx, uint64_t hintId, char * hintFieldName); // Starks // ======================================================================================== @@ -73,8 +74,8 @@ void calculate_quotient_polynomial(void *pStarks, void* stepsParams); void calculate_impols_expressions(void *pStarks, uint64_t step, void* stepsParams); - void extend_and_merkelize_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *pProof, void *pBuffHelper, char *treeFile); - void load_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *pProof, char *treeFile); + void extend_and_merkelize_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *bufferExt, void *pProof, void *pBuffHelper, char *treeFile); + void load_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *bufferExt, void *pProof, char *treeFile); void commit_stage(void *pStarks, uint32_t elementType, uint64_t step, void *trace, void *buffer, void *pProof, void *pBuffHelper); @@ -109,22 +110,29 @@ // Constraints // ================================================================================= - void *verify_constraints(void *pSetupCtx, void* stepsParams); + uint64_t get_n_constraints(void *pSetupCtx); + void get_constraints_lines_sizes(void* pSetupCtx, uint64_t *constraintsLinesSizes); + void get_constraints_lines(void* pSetupCtx, uint8_t **constraintsLines); + void verify_constraints(void *pSetupCtx, void* stepsParams, void* constraintsInfo); // Global constraints // ================================================================================= - bool verify_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues); - void *get_hint_field_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression); - uint64_t set_hint_field_global_constraints(void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName); + uint64_t get_n_global_constraints(void* p_globalinfo_bin); + void get_global_constraints_lines_sizes(void* p_globalinfo_bin, uint64_t *constraintsLinesSizes); + void get_global_constraints_lines(void* p_globalinfo_bin, uint8_t **constraintsLines); + void verify_global_constraints(char* globalInfoFile, void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues, void* globalConstraintsInfo); + uint64_t get_hint_field_global_constraints_values(void* p_globalinfo_bin, uint64_t hintId, char* hintFieldName); + void get_hint_field_global_constraints_sizes(char* globalInfoFile, void* p_globalinfo_bin, void* hintFieldValues, uint64_t hintId, char *hintFieldName, bool print_expression); + void get_hint_field_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void* hintFieldValues, void *publics, void *challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression); + uint64_t set_hint_field_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName); // Debug functions // ================================================================================= void print_row(void *pSetupCtx, void *buffer, uint64_t stage, uint64_t row); - void print_expression(void *pSetupCtx, void* pol, uint64_t dim, uint64_t first_value, uint64_t last_value); // Recursive proof // ================================================================================= - void *gen_recursive_proof(void *pSetupCtx, char* globalInfoFile, uint64_t airgroupId, void* pAddress, void *pConstPols, void *pConstTree, void* pPublicInputs, char *proof_file, bool vadcop); + void *gen_recursive_proof(void *pSetupCtx, char* globalInfoFile, uint64_t airgroupId, void* witness, void *pConstPols, void *pConstTree, void* pPublicInputs, char *proof_file, bool vadcop); void *get_zkin_ptr(char *zkin_file); void *add_recursive2_verkey(void *pZkin, char* recursive2VerKeyFilename); void *join_zkin_recursive2(char* globalInfoFile, uint64_t airgroupId, void* pPublics, void* pChallenges, void *zkin1, void *zkin2, void *starkInfoRecursive2); @@ -134,11 +142,11 @@ void *get_zkin_proof(char* zkin); void zkin_proof_free(void *pZkinProof); void serialized_proof_free(char *zkinCStr); - void get_committed_pols(void *pWitness, char* execFile, void *pAddress, void* pPublics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t offsetCm1, uint64_t nCols); + void get_committed_pols(void *circomWitness, char* execFile, void *witness, void* pPublics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t nCols); // Final proof // ================================================================================= - void gen_final_snark_proof(void *pWitnessFinal, char* zkeyFile, char* outputDir); + void gen_final_snark_proof(void *circomWitnessFinal, char* zkeyFile, char* outputDir); // Util calls // ================================================================================= diff --git a/pil2-stark/src/api/starks_api.cpp b/pil2-stark/src/api/starks_api.cpp index 06fa254d..342e7452 100644 --- a/pil2-stark/src/api/starks_api.cpp +++ b/pil2-stark/src/api/starks_api.cpp @@ -44,15 +44,26 @@ void save_publics(unsigned long numPublicInputs, void *pPublicInputs, char *file json2file(publicStarkJson, string(fileDir) + "/publics.json"); } -void save_proof_values(unsigned long numProofValues, void *pProofValues, char *fileDir) { +void save_proof_values(void *pProofValues, char* globalInfoFile, char *fileDir) { Goldilocks::Element* proofValues = (Goldilocks::Element *)pProofValues; + json globalInfo; + file2json(globalInfoFile, globalInfo); + json proofValuesJson; - for(uint64_t i = 0; i < numProofValues; i++) { + uint64_t p = 0; + for(uint64_t i = 0; i < globalInfo["proofValuesMap"].size(); i++) { proofValuesJson[i] = json::array(); - for(uint64_t j = 0; j < FIELD_EXTENSION; ++j) { - proofValuesJson[i][j] = Goldilocks::toString(proofValues[i*FIELD_EXTENSION + j]); + if(globalInfo["proofValuesMap"][i]["stage"] == 1) { + proofValuesJson[i][0] = Goldilocks::toString(proofValues[p++]); + proofValuesJson[i][1] = "0"; + proofValuesJson[i][2] = "0"; + } else { + proofValuesJson[i][0] = Goldilocks::toString(proofValues[p++]); + proofValuesJson[i][1] = Goldilocks::toString(proofValues[p++]); + proofValuesJson[i][2] = Goldilocks::toString(proofValues[p++]); } + } json2file(proofValuesJson, string(fileDir) + "/proof_values.json"); @@ -108,11 +119,18 @@ void *fri_proof_get_zkinproof(void *pFriProof, void* pPublics, void* pChallenges zkin["publics"][i] = Goldilocks::toString(publics[i]); } + uint64_t p = 0; for (uint64_t i = 0; i < starkInfo.proofValuesMap.size(); i++) { - zkin["proofvalues"][i][0] = Goldilocks::toString(proofValues[i*FIELD_EXTENSION]); - zkin["proofvalues"][i][1] = Goldilocks::toString(proofValues[i*FIELD_EXTENSION + 1]); - zkin["proofvalues"][i][2] = Goldilocks::toString(proofValues[i*FIELD_EXTENSION + 2]); + if(starkInfo.proofValuesMap[i].stage == 1) { + zkin["proofvalues"][i][0] = Goldilocks::toString(proofValues[p++]); + zkin["proofvalues"][i][1] = "0"; + zkin["proofvalues"][i][2] = "0"; + } else { + zkin["proofvalues"][i][0] = Goldilocks::toString(proofValues[p++]); + zkin["proofvalues"][i][1] = Goldilocks::toString(proofValues[p++]); + zkin["proofvalues"][i][2] = Goldilocks::toString(proofValues[p++]); + } } json challengesJson = challenges2zkin(globalInfo, challenges); @@ -147,12 +165,15 @@ void fri_proof_free(void *pFriProof) // SetupCtx // ======================================================================================== -void* get_hint_ids_by_name(void *p_expression_bin, char* hintName) -{ +uint64_t n_hints_by_name(void *p_expression_bin, char* hintName) { ExpressionsBin *expressionsBin = (ExpressionsBin*)p_expression_bin; + return expressionsBin->getNumberHintIdsByName(string(hintName)); +} - VecU64Result hintIds = expressionsBin->getHintIdsByName(string(hintName)); - return new VecU64Result(hintIds); +void get_hint_ids_by_name(void *p_expression_bin, uint64_t* hintIds, char* hintName) +{ + ExpressionsBin *expressionsBin = (ExpressionsBin*)p_expression_bin; + expressionsBin->getHintIdsByName(hintIds, string(hintName)); } // StarkInfo @@ -169,31 +190,6 @@ uint64_t get_map_total_n(void *pStarkInfo) return ((StarkInfo *)pStarkInfo)->mapTotalN; } -uint64_t get_map_total_n_custom_commits(void *pStarkInfo, uint64_t commit_id) { - auto starkInfo = *(StarkInfo *)pStarkInfo; - return starkInfo.mapTotalNcustomCommits[starkInfo.customCommits[commit_id].name]; -} - -void *get_custom_commit_map_ids(void *pStarkInfo, uint64_t commit_id, uint64_t stage) { - auto starkInfo = *(StarkInfo *)pStarkInfo; - VecU64Result customCommitIds; - customCommitIds.nElements = starkInfo.customCommits[commit_id].stageWidths[stage]; - customCommitIds.ids = new uint64_t[customCommitIds.nElements]; - uint64_t c = 0; - for(uint64_t i = 0; i < starkInfo.customCommitsMap[commit_id].size(); ++i) { - if(starkInfo.customCommitsMap[commit_id][i].stage == stage) { - customCommitIds.ids[c++] = i; - } - } - return new VecU64Result(customCommitIds); -} - -uint64_t get_map_offsets(void *pStarkInfo, char *stage, bool flag) -{ - auto starkInfo = (StarkInfo *)pStarkInfo; - return starkInfo->mapOffsets[std::make_pair(stage, flag)]; -} - void stark_info_free(void *pStarkInfo) { auto starkInfo = (StarkInfo *)pStarkInfo; @@ -268,10 +264,18 @@ void expressions_bin_free(void *pExpressionsBin) // Hints // ======================================================================================== -void *get_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char* hintFieldName, void* hintOptions) +void get_hint_field(void *pSetupCtx, void* stepsParams, void* hintFieldValues, uint64_t hintId, char* hintFieldName, void* hintOptions) +{ + getHintField(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, (HintFieldInfo *) hintFieldValues, hintId, string(hintFieldName), *(HintFieldOptions *) hintOptions); +} + +uint64_t get_hint_field_values(void *pSetupCtx, uint64_t hintId, char* hintFieldName) { + return getHintFieldValues(*(SetupCtx *)pSetupCtx, hintId, string(hintFieldName)); +} + +void get_hint_field_sizes(void *pSetupCtx, void* hintFieldValues, uint64_t hintId, char* hintFieldName, void* hintOptions) { - HintFieldValues hintFieldValues = getHintField(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldName), *(HintFieldOptions *) hintOptions); - return new HintFieldValues(hintFieldValues); + getHintFieldSizes(*(SetupCtx *)pSetupCtx, (HintFieldInfo *) hintFieldValues, hintId, string(hintFieldName), *(HintFieldOptions *) hintOptions); } uint64_t mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2) @@ -279,18 +283,21 @@ uint64_t mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, ch return multiplyHintFields(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameDest), string(hintFieldName1), string(hintFieldName2), *(HintFieldOptions *)hintOptions1, *(HintFieldOptions *)hintOptions2); } -void *acc_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName, bool add) { - return new VecU64Result(accHintField(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameDest), string(hintFieldNameAirgroupVal), string(hintFieldName), add)); +void acc_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName, bool add) { + accHintField(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameDest), string(hintFieldNameAirgroupVal), string(hintFieldName), add); } -void *acc_mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add) { - return new VecU64Result(accMulHintFields(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameDest), string(hintFieldNameAirgroupVal), string(hintFieldName1), string(hintFieldName2),*(HintFieldOptions *)hintOptions1, *(HintFieldOptions *)hintOptions2, add)); +void acc_mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add) { + accMulHintFields(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameDest), string(hintFieldNameAirgroupVal), string(hintFieldName1), string(hintFieldName2),*(HintFieldOptions *)hintOptions1, *(HintFieldOptions *)hintOptions2, add); } -void *update_airgroupvalue(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add) { - return new VecU64Result(updateAirgroupValue(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameAirgroupVal), string(hintFieldName1), string(hintFieldName2),*(HintFieldOptions *)hintOptions1, *(HintFieldOptions *)hintOptions2, add)); +uint64_t update_airgroupvalue(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add) { + return updateAirgroupValue(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, hintId, string(hintFieldNameAirgroupVal), string(hintFieldName1), string(hintFieldName2),*(HintFieldOptions *)hintOptions1, *(HintFieldOptions *)hintOptions2, add); } +uint64_t get_hint_id(void *pSetupCtx, uint64_t hintId, char * hintFieldName) { + return getHintId(*(SetupCtx *)pSetupCtx, hintId, string(hintFieldName)); +} uint64_t set_hint_field(void *pSetupCtx, void* params, void *values, uint64_t hintId, char * hintFieldName) { @@ -345,16 +352,16 @@ void calculate_impols_expressions(void *pStarks, uint64_t step, void* stepsParam starks->calculateImPolsExpressions(step, *(StepsParams *)stepsParams); } -void extend_and_merkelize_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *pProof, void *pBuffHelper, char *bufferFile) +void extend_and_merkelize_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void* bufferExt, void *pProof, void *pBuffHelper, char *bufferFile) { Starks *starks = (Starks *)pStarks; - starks->extendAndMerkelizeCustomCommit(commitId, step, (Goldilocks::Element *)buffer, *(FRIProof *)pProof, (Goldilocks::Element *)pBuffHelper, string(bufferFile)); + starks->extendAndMerkelizeCustomCommit(commitId, step, (Goldilocks::Element *)buffer, (Goldilocks::Element *)bufferExt, *(FRIProof *)pProof, (Goldilocks::Element *)pBuffHelper, string(bufferFile)); } -void load_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *pProof, char *bufferFile) +void load_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *bufferExt, void *pProof, char *bufferFile) { Starks *starks = (Starks *)pStarks; - starks->loadCustomCommit(commitId, step, (Goldilocks::Element *)buffer, *(FRIProof *)pProof, string(bufferFile)); + starks->loadCustomCommit(commitId, step, (Goldilocks::Element *)buffer, (Goldilocks::Element *)bufferExt, *(FRIProof *)pProof, string(bufferFile)); } void commit_stage(void *pStarks, uint32_t elementType, uint64_t step, void *trace, void *buffer, void *pProof, void *pBuffHelper) { @@ -507,35 +514,88 @@ void get_permutations(void *pTranscript, uint64_t *res, uint64_t n, uint64_t nBi // Constraints // ================================================================================= -void *verify_constraints(void *pSetupCtx, void* stepsParams) +uint64_t get_n_constraints(void *pSetupCtx) +{ + auto setupCtx = *(SetupCtx *)pSetupCtx; + return setupCtx.expressionsBin.constraintsInfoDebug.size(); +} + +void get_constraints_lines_sizes(void* pSetupCtx, uint64_t *constraintsLinesSizes) +{ + auto setupCtx = *(SetupCtx *)pSetupCtx; + for(uint64_t i = 0; i < setupCtx.expressionsBin.constraintsInfoDebug.size(); ++i) { + constraintsLinesSizes[i] = setupCtx.expressionsBin.constraintsInfoDebug[i].line.size(); + } +} + +void get_constraints_lines(void* pSetupCtx, uint8_t **constraintsLines) { - ConstraintsResults *constraintsInfo = verifyConstraints(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams); - return constraintsInfo; + auto setupCtx = *(SetupCtx *)pSetupCtx; + for(uint64_t i = 0; i < setupCtx.expressionsBin.constraintsInfoDebug.size(); ++i) { + std::memcpy(constraintsLines[i], setupCtx.expressionsBin.constraintsInfoDebug[i].line.data(), setupCtx.expressionsBin.constraintsInfoDebug[i].line.size()); + } +} + +void verify_constraints(void *pSetupCtx, void* stepsParams, void* constraintsInfo) +{ + verifyConstraints(*(SetupCtx *)pSetupCtx, *(StepsParams *)stepsParams, (ConstraintInfo *)constraintsInfo); } // Global Constraints // ================================================================================= -bool verify_global_constraints(void* p_globalinfo_bin, void *publics, void *challenges, void *proofValues, void **airgroupValues) { - return verifyGlobalConstraints(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)challenges, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues); +uint64_t get_n_global_constraints(void* p_globalinfo_bin) +{ + return getNumberGlobalConstraints(*(ExpressionsBin*)p_globalinfo_bin); } -void *get_hint_field_global_constraints(void* p_globalinfo_bin, void *publics, void *challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression) +void get_global_constraints_lines_sizes(void* p_globalinfo_bin, uint64_t *constraintsLinesSizes) { - HintFieldValues hintFieldValues = getHintFieldGlobalConstraint(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)challenges, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues, hintId, string(hintFieldName), print_expression); - return new HintFieldValues(hintFieldValues); + return getGlobalConstraintsLinesSizes(*(ExpressionsBin*)p_globalinfo_bin, constraintsLinesSizes); } -uint64_t set_hint_field_global_constraints(void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName) +void get_global_constraints_lines(void* p_globalinfo_bin, uint8_t **constraintsLines) { - return setHintFieldGlobalConstraint(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)proofValues, (Goldilocks::Element *)values, hintId, string(hintFieldName)); + return getGlobalConstraintsLines(*(ExpressionsBin*)p_globalinfo_bin, constraintsLines); } -// Debug functions -// ================================================================================= +void verify_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void *publics, void *challenges, void *proofValues, void **airgroupValues, void *globalConstraintsInfo) { + json globalInfo; + file2json(globalInfoFile, globalInfo); -void print_expression(void *pSetupCtx, void* pol, uint64_t dim, uint64_t first_value, uint64_t last_value) { - printExpression((Goldilocks::Element *)pol, dim, first_value, last_value); + verifyGlobalConstraints(globalInfo, *(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)challenges, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues, (GlobalConstraintInfo *)globalConstraintsInfo); } + +uint64_t get_hint_field_global_constraints_values(void* p_globalinfo_bin, uint64_t hintId, char* hintFieldName) { + return getHintFieldGlobalConstraintValues(*(ExpressionsBin*)p_globalinfo_bin, hintId, string(hintFieldName)); +} + +void get_hint_field_global_constraints_sizes(char* globalInfoFile, void* p_globalinfo_bin, void* hintFieldValues, uint64_t hintId, char *hintFieldName, bool print_expression) +{ + json globalInfo; + file2json(globalInfoFile, globalInfo); + + getHintFieldGlobalConstraintSizes(globalInfo, *(ExpressionsBin*)p_globalinfo_bin, (HintFieldInfo *)hintFieldValues, hintId, string(hintFieldName), print_expression); +} + + +void get_hint_field_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void* hintFieldValues, void *publics, void *challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression) +{ + json globalInfo; + file2json(globalInfoFile, globalInfo); + + getHintFieldGlobalConstraint(globalInfo, *(ExpressionsBin*)p_globalinfo_bin, (HintFieldInfo *)hintFieldValues, (Goldilocks::Element *)publics, (Goldilocks::Element *)challenges, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues, hintId, string(hintFieldName), print_expression); +} + +uint64_t set_hint_field_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName) +{ + json globalInfo; + file2json(globalInfoFile, globalInfo); + + return setHintFieldGlobalConstraint(globalInfo, *(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)proofValues, (Goldilocks::Element *)values, hintId, string(hintFieldName)); +} + +// Debug functions +// ================================================================================= void print_row(void *pSetupCtx, void *buffer, uint64_t stage, uint64_t row) { printRow(*(SetupCtx *)pSetupCtx, (Goldilocks::Element *)buffer, stage, row); @@ -543,21 +603,21 @@ void print_row(void *pSetupCtx, void *buffer, uint64_t stage, uint64_t row) { // Recursive proof // ================================================================================= -void *gen_recursive_proof(void *pSetupCtx, char* globalInfoFile, uint64_t airgroupId, void* pAddress, void *pConstPols, void *pConstTree, void* pPublicInputs, char* proof_file, bool vadcop) { +void *gen_recursive_proof(void *pSetupCtx, char* globalInfoFile, uint64_t airgroupId, void* witness, void *pConstPols, void *pConstTree, void* pPublicInputs, char* proof_file, bool vadcop) { json globalInfo; file2json(globalInfoFile, globalInfo); auto setup = *(SetupCtx *)pSetupCtx; if(setup.starkInfo.starkStruct.verificationHashType == "GL") { - return genRecursiveProof(*(SetupCtx *)pSetupCtx, globalInfo, airgroupId, (Goldilocks::Element *)pAddress, (Goldilocks::Element *)pConstPols, (Goldilocks::Element *)pConstTree, (Goldilocks::Element *)pPublicInputs, string(proof_file), vadcop); + return genRecursiveProof(*(SetupCtx *)pSetupCtx, globalInfo, airgroupId, (Goldilocks::Element *)witness, (Goldilocks::Element *)pConstPols, (Goldilocks::Element *)pConstTree, (Goldilocks::Element *)pPublicInputs, string(proof_file), vadcop); } else { - return genRecursiveProof(*(SetupCtx *)pSetupCtx, globalInfo, airgroupId, (Goldilocks::Element *)pAddress, (Goldilocks::Element *)pConstPols, (Goldilocks::Element *)pConstTree, (Goldilocks::Element *)pPublicInputs, string(proof_file), false); + return genRecursiveProof(*(SetupCtx *)pSetupCtx, globalInfo, airgroupId, (Goldilocks::Element *)witness, (Goldilocks::Element *)pConstPols, (Goldilocks::Element *)pConstTree, (Goldilocks::Element *)pPublicInputs, string(proof_file), false); } } void *get_zkin_ptr(char *zkin_file) { json zkin; - + file2json(zkin_file, zkin); return (void *) new nlohmann::json(zkin); } @@ -637,12 +697,12 @@ void serialized_proof_free(char *zkinCStr) { delete[] zkinCStr; } -void get_committed_pols(void *pWitness, char* execFile, void *pAddress, void* pPublics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t offsetCm1, uint64_t nCommitedPols) { - getCommitedPols((Goldilocks::Element *)pWitness, string(execFile), (Goldilocks::Element *)pAddress, (Goldilocks::Element *)pPublics, sizeWitness, N, nPublics, offsetCm1, nCommitedPols); +void get_committed_pols(void *circomWitness, char* execFile, void *witness, void* pPublics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t nCommitedPols) { + getCommitedPols((Goldilocks::Element *)circomWitness, string(execFile), (Goldilocks::Element *)witness, (Goldilocks::Element *)pPublics, sizeWitness, N, nPublics, nCommitedPols); } -void gen_final_snark_proof(void *pWitnessFinal, char* zkeyFile, char* outputDir) { - genFinalSnarkProof(pWitnessFinal, string(zkeyFile), string(outputDir)); +void gen_final_snark_proof(void *circomWitnessFinal, char* zkeyFile, char* outputDir) { + genFinalSnarkProof(circomWitnessFinal, string(zkeyFile), string(outputDir)); } void setLogLevel(uint64_t level) { diff --git a/pil2-stark/src/api/starks_api.hpp b/pil2-stark/src/api/starks_api.hpp index 0c24fbf4..0712cb4f 100644 --- a/pil2-stark/src/api/starks_api.hpp +++ b/pil2-stark/src/api/starks_api.hpp @@ -6,7 +6,7 @@ // ======================================================================================== void save_challenges(void *pChallenges, char* globalInfoFile, char *fileDir); void save_publics(unsigned long numPublicInputs, void *pPublicInputs, char *fileDir); - void save_proof_values(unsigned long numProofValues, void *pProofValues, char *fileDir); + void save_proof_values(void *pProofValues, char* globalInfoFile, char *fileDir); // FRIProof // ======================================================================================== @@ -20,15 +20,13 @@ // SetupCtx // ======================================================================================== - void *get_hint_ids_by_name(void *p_expression_bin, char* hintName); + uint64_t n_hints_by_name(void *p_expression_bin, char* hintName); + void get_hint_ids_by_name(void *p_expression_bin, uint64_t* hintIds, char* hintName); // Stark Info // ======================================================================================== void *stark_info_new(char* filename); uint64_t get_map_total_n(void *pStarkInfo); - uint64_t get_map_total_n_custom_commits(void *pStarkInfo, uint64_t commit_id); - uint64_t get_map_offsets(void *pStarkInfo, char *stage, bool flag); - void *get_custom_commit_map_ids(void *pStarkInfo, uint64_t commit_id, uint64_t stage); void stark_info_free(void *pStarkInfo); // Prover Helpers @@ -51,12 +49,15 @@ // Hints // ======================================================================================== - void *get_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char* hintFieldName, void* hintOptions); + void get_hint_field(void *pSetupCtx, void* stepsParams, void* hintFieldValues, uint64_t hintId, char* hintFieldName, void* hintOptions); + uint64_t get_hint_field_values(void *pSetupCtx, uint64_t hintId, char* hintFieldName); + void get_hint_field_sizes(void *pSetupCtx, void* hintFieldValues, uint64_t hintId, char* hintFieldName, void* hintOptions); uint64_t mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2); - void *acc_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName, bool add); - void *acc_mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); - void *update_airgroupvalue(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); + void acc_hint_field(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName, bool add); + void acc_mul_hint_fields(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameDest, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); + uint64_t update_airgroupvalue(void *pSetupCtx, void* stepsParams, uint64_t hintId, char *hintFieldNameAirgroupVal, char *hintFieldName1, char *hintFieldName2, void* hintOptions1, void *hintOptions2, bool add); uint64_t set_hint_field(void *pSetupCtx, void* stepsParams, void *values, uint64_t hintId, char* hintFieldName); + uint64_t get_hint_id(void *pSetupCtx, uint64_t hintId, char * hintFieldName); // Starks // ======================================================================================== @@ -73,8 +74,8 @@ void calculate_quotient_polynomial(void *pStarks, void* stepsParams); void calculate_impols_expressions(void *pStarks, uint64_t step, void* stepsParams); - void extend_and_merkelize_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *pProof, void *pBuffHelper, char *treeFile); - void load_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *pProof, char *treeFile); + void extend_and_merkelize_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *bufferExt, void *pProof, void *pBuffHelper, char *treeFile); + void load_custom_commit(void *pStarks, uint64_t commitId, uint64_t step, void *buffer, void *bufferExt, void *pProof, char *treeFile); void commit_stage(void *pStarks, uint32_t elementType, uint64_t step, void *trace, void *buffer, void *pProof, void *pBuffHelper); @@ -109,22 +110,29 @@ // Constraints // ================================================================================= - void *verify_constraints(void *pSetupCtx, void* stepsParams); + uint64_t get_n_constraints(void *pSetupCtx); + void get_constraints_lines_sizes(void* pSetupCtx, uint64_t *constraintsLinesSizes); + void get_constraints_lines(void* pSetupCtx, uint8_t **constraintsLines); + void verify_constraints(void *pSetupCtx, void* stepsParams, void* constraintsInfo); // Global constraints // ================================================================================= - bool verify_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues); - void *get_hint_field_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression); - uint64_t set_hint_field_global_constraints(void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName); + uint64_t get_n_global_constraints(void* p_globalinfo_bin); + void get_global_constraints_lines_sizes(void* p_globalinfo_bin, uint64_t *constraintsLinesSizes); + void get_global_constraints_lines(void* p_globalinfo_bin, uint8_t **constraintsLines); + void verify_global_constraints(char* globalInfoFile, void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues, void* globalConstraintsInfo); + uint64_t get_hint_field_global_constraints_values(void* p_globalinfo_bin, uint64_t hintId, char* hintFieldName); + void get_hint_field_global_constraints_sizes(char* globalInfoFile, void* p_globalinfo_bin, void* hintFieldValues, uint64_t hintId, char *hintFieldName, bool print_expression); + void get_hint_field_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void* hintFieldValues, void *publics, void *challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression); + uint64_t set_hint_field_global_constraints(char* globalInfoFile, void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName); // Debug functions // ================================================================================= void print_row(void *pSetupCtx, void *buffer, uint64_t stage, uint64_t row); - void print_expression(void *pSetupCtx, void* pol, uint64_t dim, uint64_t first_value, uint64_t last_value); // Recursive proof // ================================================================================= - void *gen_recursive_proof(void *pSetupCtx, char* globalInfoFile, uint64_t airgroupId, void* pAddress, void *pConstPols, void *pConstTree, void* pPublicInputs, char *proof_file, bool vadcop); + void *gen_recursive_proof(void *pSetupCtx, char* globalInfoFile, uint64_t airgroupId, void* witness, void *pConstPols, void *pConstTree, void* pPublicInputs, char *proof_file, bool vadcop); void *get_zkin_ptr(char *zkin_file); void *add_recursive2_verkey(void *pZkin, char* recursive2VerKeyFilename); void *join_zkin_recursive2(char* globalInfoFile, uint64_t airgroupId, void* pPublics, void* pChallenges, void *zkin1, void *zkin2, void *starkInfoRecursive2); @@ -134,11 +142,11 @@ void *get_zkin_proof(char* zkin); void zkin_proof_free(void *pZkinProof); void serialized_proof_free(char *zkinCStr); - void get_committed_pols(void *pWitness, char* execFile, void *pAddress, void* pPublics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t offsetCm1, uint64_t nCols); + void get_committed_pols(void *circomWitness, char* execFile, void *witness, void* pPublics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t nCols); // Final proof // ================================================================================= - void gen_final_snark_proof(void *pWitnessFinal, char* zkeyFile, char* outputDir); + void gen_final_snark_proof(void *circomWitnessFinal, char* zkeyFile, char* outputDir); // Util calls // ================================================================================= diff --git a/pil2-stark/src/starkpil/exec_file.hpp b/pil2-stark/src/starkpil/exec_file.hpp index bddc1714..f9aeda08 100644 --- a/pil2-stark/src/starkpil/exec_file.hpp +++ b/pil2-stark/src/starkpil/exec_file.hpp @@ -13,8 +13,8 @@ #include "commit_pols_starks.hpp" -void getCommitedPols(Goldilocks::Element *pWitness, std::string execFile, Goldilocks::Element *pAddress, Goldilocks::Element* publics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t offsetCm1, uint64_t nCommitedPols) { - CommitPolsStarks commitPols((uint8_t *)pAddress + offsetCm1 * sizeof(Goldilocks::Element), N, nCommitedPols); +void getCommitedPols(Goldilocks::Element *circomWitness, std::string execFile, Goldilocks::Element *witness, Goldilocks::Element* publics, uint64_t sizeWitness, uint64_t N, uint64_t nPublics, uint64_t nCommitedPols) { + CommitPolsStarks commitPols((uint8_t *)witness, N, nCommitedPols); uint64_t nAdds; uint64_t nSMap; @@ -31,7 +31,7 @@ void getCommitedPols(Goldilocks::Element *pWitness, std::string execFile, Goldil uint64_t *p_sMap = &p_data[2 + nAdds * 4]; for(uint64_t i = 0; i < nPublics; ++i) { - publics[i] = pWitness[1 + i]; + publics[i] = circomWitness[1 + i]; } // #pragma omp parallel for @@ -39,16 +39,16 @@ void getCommitedPols(Goldilocks::Element *pWitness, std::string execFile, Goldil uint64_t idx_1 = p_adds[i * 4]; uint64_t idx_2 = p_adds[i * 4 + 1]; - Goldilocks::Element c = pWitness[idx_1] * Goldilocks::fromU64(p_adds[i * 4 + 2]); - Goldilocks::Element d = pWitness[idx_2] * Goldilocks::fromU64(p_adds[i * 4 + 3]); - pWitness[sizeWitness + i] = c + d; + Goldilocks::Element c = circomWitness[idx_1] * Goldilocks::fromU64(p_adds[i * 4 + 2]); + Goldilocks::Element d = circomWitness[idx_2] * Goldilocks::fromU64(p_adds[i * 4 + 3]); + circomWitness[sizeWitness + i] = c + d; } // #pragma omp parallel for for (uint i = 0; i < N; i++) { for (uint j = 0; j < nCommitedPols; j++) { if (i < nSMap && p_sMap[nCommitedPols * i + j] != 0) { - commitPols.Compressor.a[j][i] = pWitness[p_sMap[nCommitedPols * i + j]]; + commitPols.Compressor.a[j][i] = circomWitness[p_sMap[nCommitedPols * i + j]]; } else { commitPols.Compressor.a[j][i] = Goldilocks::zero(); } diff --git a/pil2-stark/src/starkpil/expressions_avx.hpp b/pil2-stark/src/starkpil/expressions_avx.hpp index 5f1bee6f..60795b3d 100644 --- a/pil2-stark/src/starkpil/expressions_avx.hpp +++ b/pil2-stark/src/starkpil/expressions_avx.hpp @@ -127,7 +127,7 @@ class ExpressionsAvx : public ExpressionsCtx { if(stage == 1 && !domainExtended) { bufferT[nrowsPack*o + j] = params.trace[l * nColsStages[stage] + stagePos + d]; } else { - bufferT[nrowsPack*o + j] = params.pols[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; + bufferT[nrowsPack*o + j] = params.aux_trace[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; } } Goldilocks::load_avx(bufferT_[nColsStagesAcc[ns*o + stage] + (stagePos + d)], &bufferT[nrowsPack*o]); @@ -136,6 +136,7 @@ class ExpressionsAvx : public ExpressionsCtx { } for(uint64_t i = 0; i < setupCtx.starkInfo.customCommits.size(); ++i) { + Goldilocks::Element *customCommits = domainExtended ? params.pCustomCommitsExtended[i] : params.pCustomCommits[i]; for(uint64_t j = 0; j < setupCtx.starkInfo.customCommits[i].stageWidths[0]; ++j) { if(!customCommitsUsed[i][j]) continue; PolMap polInfo = setupCtx.starkInfo.customCommitsMap[i][j]; @@ -145,7 +146,7 @@ class ExpressionsAvx : public ExpressionsCtx { for(uint64_t o = 0; o < nOpenings; ++o) { for(uint64_t j = 0; j < nrowsPack; ++j) { uint64_t l = (row + j + nextStrides[o]) % domainSize; - bufferT[nrowsPack*o + j] = params.customCommits[i][offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; + bufferT[nrowsPack*o + j] = customCommits[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; } Goldilocks::load_avx(bufferT_[nColsStagesAcc[ns*o + stage] + (stagePos + d)], &bufferT[nrowsPack*o]); } @@ -299,29 +300,56 @@ class ExpressionsAvx : public ExpressionsCtx { } __m256i publics[setupCtx.starkInfo.nPublics]; - for(uint64_t i = 0; i < setupCtx.starkInfo.nPublics; ++i) { + for(uint64_t i = 0; i < setupCtx.starkInfo.nPublics; ++i) { publics[i] = _mm256_set1_epi64x(params.publicInputs[i].fe); } + uint64_t p = 0; Goldilocks3::Element_avx proofValues[setupCtx.starkInfo.proofValuesMap.size()]; for(uint64_t i = 0; i < setupCtx.starkInfo.proofValuesMap.size(); ++i) { - proofValues[i][0] = _mm256_set1_epi64x(params.proofValues[i * FIELD_EXTENSION].fe); - proofValues[i][1] = _mm256_set1_epi64x(params.proofValues[i * FIELD_EXTENSION + 1].fe); - proofValues[i][2] = _mm256_set1_epi64x(params.proofValues[i * FIELD_EXTENSION + 2].fe); + if(setupCtx.starkInfo.proofValuesMap[i].stage == 1) { + proofValues[i][0] = _mm256_set1_epi64x(params.proofValues[p].fe); + proofValues[i][1] = _mm256_set1_epi64x(0); + proofValues[i][2] = _mm256_set1_epi64x(0); + p += 1; + } else { + proofValues[i][0] = _mm256_set1_epi64x(params.proofValues[p].fe); + proofValues[i][1] = _mm256_set1_epi64x(params.proofValues[p + 1].fe); + proofValues[i][2] = _mm256_set1_epi64x(params.proofValues[p + 2].fe); + p += 3; + } } Goldilocks3::Element_avx airgroupValues[setupCtx.starkInfo.airgroupValuesMap.size()]; + p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.airgroupValuesMap.size(); ++i) { - airgroupValues[i][0] = _mm256_set1_epi64x(params.airgroupValues[i * FIELD_EXTENSION].fe); - airgroupValues[i][1] = _mm256_set1_epi64x(params.airgroupValues[i * FIELD_EXTENSION + 1].fe); - airgroupValues[i][2] = _mm256_set1_epi64x(params.airgroupValues[i * FIELD_EXTENSION + 2].fe); + if(setupCtx.starkInfo.airgroupValuesMap[i].stage == 1) { + airgroupValues[i][0] = _mm256_set1_epi64x(params.airgroupValues[p].fe); + airgroupValues[i][1] = _mm256_set1_epi64x(0); + airgroupValues[i][2] = _mm256_set1_epi64x(0); + p += 1; + } else { + airgroupValues[i][0] = _mm256_set1_epi64x(params.airgroupValues[p].fe); + airgroupValues[i][1] = _mm256_set1_epi64x(params.airgroupValues[p+ 1].fe); + airgroupValues[i][2] = _mm256_set1_epi64x(params.airgroupValues[p+ 2].fe); + p += 3; + } } Goldilocks3::Element_avx airValues[setupCtx.starkInfo.airValuesMap.size()]; + p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.airValuesMap.size(); ++i) { - airValues[i][0] = _mm256_set1_epi64x(params.airValues[i * FIELD_EXTENSION].fe); - airValues[i][1] = _mm256_set1_epi64x(params.airValues[i * FIELD_EXTENSION + 1].fe); - airValues[i][2] = _mm256_set1_epi64x(params.airValues[i * FIELD_EXTENSION + 2].fe); + if(setupCtx.starkInfo.airValuesMap[i].stage == 1) { + airValues[i][0] = _mm256_set1_epi64x(params.airValues[p].fe); + airValues[i][1] = _mm256_set1_epi64x(0); + airValues[i][2] = _mm256_set1_epi64x(0); + p += 1; + } else { + airValues[i][0] = _mm256_set1_epi64x(params.airValues[p].fe); + airValues[i][1] = _mm256_set1_epi64x(params.airValues[p + 1].fe); + airValues[i][2] = _mm256_set1_epi64x(params.airValues[p + 2].fe); + p += 3; + } } Goldilocks3::Element_avx evals[setupCtx.starkInfo.evMap.size()]; @@ -399,456 +427,534 @@ class ExpressionsAvx : public ExpressionsCtx { break; } case 6: { + // OPERATION WITH DEST: tmp1 - SRC0: commit1 - SRC1: proofvalue1 + Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], proofValues[args[i_args + 4]][0]); + i_args += 5; + break; + } + case 7: { // COPY tmp1 to tmp1 Goldilocks::copy_avx(tmp1[args[i_args]], tmp1[args[i_args + 1]]); i_args += 2; break; } - case 7: { + case 8: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: tmp1 Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 8: { + case 9: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: public Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 9: { + case 10: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: number Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 10: { + case 11: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: airvalue1 Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 11: { + case 12: { + // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: proofvalue1 + Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 13: { // COPY public to tmp1 Goldilocks::copy_avx(tmp1[args[i_args]], publics[args[i_args + 1]]); i_args += 2; break; } - case 12: { + case 14: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: public Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 13: { + case 15: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: number Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 14: { + case 16: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: airvalue1 Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 15: { + case 17: { + // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: proofvalue1 + Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 18: { // COPY number to tmp1 Goldilocks::copy_avx(tmp1[args[i_args]], numbers_[args[i_args + 1]]); i_args += 2; break; } - case 16: { + case 19: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: number Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], numbers_[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 17: { + case 20: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: airvalue1 Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], numbers_[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 18: { + case 21: { + // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: proofvalue1 + Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], numbers_[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 22: { // COPY airvalue1 to tmp1 Goldilocks::copy_avx(tmp1[args[i_args]], airValues[args[i_args + 1]][0]); i_args += 2; break; } - case 19: { + case 23: { // OPERATION WITH DEST: tmp1 - SRC0: airvalue1 - SRC1: airvalue1 Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], airValues[args[i_args + 2]][0], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 20: { + case 24: { + // OPERATION WITH DEST: tmp1 - SRC0: airvalue1 - SRC1: proofvalue1 + Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], airValues[args[i_args + 2]][0], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 25: { + // COPY proofvalue1 to tmp1 + Goldilocks::copy_avx(tmp1[args[i_args]], proofValues[args[i_args + 1]][0]); + i_args += 2; + break; + } + case 26: { + // OPERATION WITH DEST: tmp1 - SRC0: proofvalue1 - SRC1: proofvalue1 + Goldilocks::op_avx(args[i_args], tmp1[args[i_args + 1]], proofValues[args[i_args + 2]][0], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 27: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], bufferT_[nColsStagesAcc[args[i_args + 4]] + args[i_args + 5]]); i_args += 6; break; } - case 21: { + case 28: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: tmp1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], tmp1[args[i_args + 4]]); i_args += 5; break; } - case 22: { + case 29: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], publics[args[i_args + 4]]); i_args += 5; break; } - case 23: { + case 30: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], numbers_[args[i_args + 4]]); i_args += 5; break; } - case 24: { + case 31: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airvalue1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], airValues[args[i_args + 4]][0]); i_args += 5; break; } - case 25: { + case 32: { + // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], proofValues[args[i_args + 4]][0]); + i_args += 5; + break; + } + case 33: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 26: { + case 34: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 27: { + case 35: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 28: { + case 36: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 29: { + case 37: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airvalue1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 30: { + case 38: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 39: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 31: { + case 40: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: tmp1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 32: { + case 41: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 33: { + case 42: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 34: { + case 43: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airvalue1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 35: { + case 44: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue1 + Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 45: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 36: { + case 46: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: tmp1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 37: { + case 47: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 38: { + case 48: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 39: { + case 49: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airvalue1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 40: { + case 50: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue1 + Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 51: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 41: { + case 52: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: tmp1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 42: { + case 53: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 43: { + case 54: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 44: { + case 55: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: airvalue1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 45: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: commit1 + case 56: { + // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 57: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 46: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: tmp1 + case 58: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: tmp1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 47: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: public + case 59: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 48: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: number + case 60: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 49: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: airvalue1 + case 61: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: airvalue1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 50: { + case 62: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 63: { // COPY commit3 to tmp3 Goldilocks3::copy_avx(tmp3[args[i_args]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 1]] + args[i_args + 2]]); i_args += 3; break; } - case 51: { + case 64: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: commit3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 4]] + args[i_args + 5]]); i_args += 6; break; } - case 52: { + case 65: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: tmp3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], tmp3[args[i_args + 4]]); i_args += 5; break; } - case 53: { + case 66: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: challenge Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], challenges[args[i_args + 4]]); i_args += 5; break; } - case 54: { + case 67: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airgroupvalue Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], airgroupValues[args[i_args + 4]]); i_args += 5; break; } - case 55: { + case 68: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], airValues[args[i_args + 4]]); i_args += 5; break; } - case 56: { - // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue + case 69: { + // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], proofValues[args[i_args + 4]]); i_args += 5; break; } - case 57: { + case 70: { // COPY tmp3 to tmp3 Goldilocks3::copy_avx(tmp3[args[i_args]], tmp3[args[i_args + 1]]); i_args += 2; break; } - case 58: { + case 71: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], tmp3[args[i_args + 3]]); i_args += 4; break; } - case 59: { + case 72: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: challenge Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], challenges[args[i_args + 3]]); i_args += 4; break; } - case 60: { + case 73: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airgroupvalue Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], airgroupValues[args[i_args + 3]]); i_args += 4; break; } - case 61: { + case 74: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 62: { - // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue + case 75: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 63: { + case 76: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: challenge Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], challenges[args[i_args + 3]]); i_args += 4; break; } - case 64: { + case 77: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airgroupvalue Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], airgroupValues[args[i_args + 3]]); i_args += 4; break; } - case 65: { + case 78: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 66: { - // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue + case 79: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 67: { + case 80: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airgroupvalue Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], airgroupValues[args[i_args + 3]]); i_args += 4; break; } - case 68: { + case 81: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 69: { - // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue + case 82: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 70: { + case 83: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: airvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 71: { - // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue + case 84: { + // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 72: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: proofvalue + case 85: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue3 Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 73: { + case 86: { // COPY eval to tmp3 Goldilocks3::copy_avx(tmp3[args[i_args]], evals[args[i_args + 1]]); i_args += 2; break; } - case 74: { + case 87: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: eval Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; break; } - case 75: { + case 88: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: eval Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; break; } - case 76: { + case 89: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: commit1 Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 77: { + case 90: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: eval Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], evals[args[i_args + 4]]); i_args += 5; break; } - case 78: { + case 91: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: eval Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; break; } - case 79: { + case 92: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: public Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 80: { + case 93: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: number Goldilocks3::op_31_avx(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 81: { + case 94: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: eval Goldilocks3::op_avx(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; diff --git a/pil2-stark/src/starkpil/expressions_avx512.hpp b/pil2-stark/src/starkpil/expressions_avx512.hpp index f240714a..58478c11 100644 --- a/pil2-stark/src/starkpil/expressions_avx512.hpp +++ b/pil2-stark/src/starkpil/expressions_avx512.hpp @@ -127,7 +127,7 @@ class ExpressionsAvx512 : public ExpressionsCtx { if(stage == 1 && !domainExtended) { bufferT[nrowsPack*o + j] = params.trace[l * nColsStages[stage] + stagePos + d]; } else { - bufferT[nrowsPack*o + j] = params.pols[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; + bufferT[nrowsPack*o + j] = params.aux_trace[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; } } Goldilocks::load_avx512(bufferT_[nColsStagesAcc[ns*o + stage] + (stagePos + d)], &bufferT[nrowsPack*o]); @@ -136,6 +136,7 @@ class ExpressionsAvx512 : public ExpressionsCtx { } for(uint64_t i = 0; i < setupCtx.starkInfo.customCommits.size(); ++i) { + Goldilocks::Element *customCommits = domainExtended ? params.pCustomCommitsExtended[i] : params.pCustomCommits[i]; for(uint64_t j = 0; j < setupCtx.starkInfo.customCommits[i].stageWidths[0]; ++j) { if(!customCommitsUsed[i][j]) continue; PolMap polInfo = setupCtx.starkInfo.customCommitsMap[i][j]; @@ -145,7 +146,7 @@ class ExpressionsAvx512 : public ExpressionsCtx { for(uint64_t o = 0; o < nOpenings; ++o) { for(uint64_t j = 0; j < nrowsPack; ++j) { uint64_t l = (row + j + nextStrides[o]) % domainSize; - bufferT[nrowsPack*o + j] = params.customCommits[i][offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; + bufferT[nrowsPack*o + j] = customCommits[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; } Goldilocks::load_avx(bufferT_[nColsStagesAcc[ns*o + stage] + (stagePos + d)], &bufferT[nrowsPack*o]); } @@ -303,25 +304,52 @@ class ExpressionsAvx512 : public ExpressionsCtx { publics[i] = _mm512_set1_epi64(params.publicInputs[i].fe); } + uint64_t p = 0; Goldilocks3::Element_avx512 proofValues[setupCtx.starkInfo.proofValuesMap.size()]; for(uint64_t i = 0; i < setupCtx.starkInfo.proofValuesMap.size(); ++i) { - proofValues[i][0] = _mm512_set1_epi64(params.proofValues[i * FIELD_EXTENSION].fe); - proofValues[i][1] = _mm512_set1_epi64(params.proofValues[i * FIELD_EXTENSION + 1].fe); - proofValues[i][2] = _mm512_set1_epi64(params.proofValues[i * FIELD_EXTENSION + 2].fe); + if(setupCtx.starkInfo.proofValuesMap[i].stage == 1) { + proofValues[i][0] = _mm512_set1_epi64(params.proofValues[p].fe); + proofValues[i][1] = _mm512_set1_epi64(0); + proofValues[i][2] = _mm512_set1_epi64(0); + p += 1; + } else { + proofValues[i][0] = _mm512_set1_epi64(params.proofValues[p].fe); + proofValues[i][1] = _mm512_set1_epi64(params.proofValues[p + 1].fe); + proofValues[i][2] = _mm512_set1_epi64(params.proofValues[p + 2].fe); + p += 3; + } } Goldilocks3::Element_avx512 airgroupValues[setupCtx.starkInfo.airgroupValuesMap.size()]; + p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.airgroupValuesMap.size(); ++i) { - airgroupValues[i][0] = _mm512_set1_epi64(params.airgroupValues[i * FIELD_EXTENSION].fe); - airgroupValues[i][1] = _mm512_set1_epi64(params.airgroupValues[i * FIELD_EXTENSION + 1].fe); - airgroupValues[i][2] = _mm512_set1_epi64(params.airgroupValues[i * FIELD_EXTENSION + 2].fe); + if(setupCtx.starkInfo.airgroupValuesMap[i].stage == 1) { + airgroupValues[i][0] = _mm512_set1_epi64(params.airgroupValues[p].fe); + airgroupValues[i][1] = _mm512_set1_epi64(0); + airgroupValues[i][2] = _mm512_set1_epi64(0); + p += 1; + } else { + airgroupValues[i][0] = _mm512_set1_epi64(params.airgroupValues[p].fe); + airgroupValues[i][1] = _mm512_set1_epi64(params.airgroupValues[p+ 1].fe); + airgroupValues[i][2] = _mm512_set1_epi64(params.airgroupValues[p+ 2].fe); + p += 3; + } } Goldilocks3::Element_avx512 airValues[setupCtx.starkInfo.airValuesMap.size()]; + p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.airValuesMap.size(); ++i) { - airValues[i][0] = _mm512_set1_epi64(params.airValues[i * FIELD_EXTENSION].fe); - airValues[i][1] = _mm512_set1_epi64(params.airValues[i * FIELD_EXTENSION + 1].fe); - airValues[i][2] = _mm512_set1_epi64(params.airValues[i * FIELD_EXTENSION + 2].fe); + if(setupCtx.starkInfo.airValuesMap[i].stage == 1) { + airValues[i][0] = _mm512_set1_epi64(params.airValues[p].fe); + airValues[i][1] = _mm512_set1_epi64(0); + airValues[i][2] = _mm512_set1_epi64(0); + p += 1; + } else { + airValues[i][0] = _mm512_set1_epi64(params.airValues[p].fe); + airValues[i][1] = _mm512_set1_epi64(params.airValues[p + 1].fe); + airValues[i][2] = _mm512_set1_epi64(params.airValues[p + 2].fe); + p += 3; + } } Goldilocks3::Element_avx512 evals[setupCtx.starkInfo.evMap.size()]; @@ -399,456 +427,534 @@ class ExpressionsAvx512 : public ExpressionsCtx { break; } case 6: { + // OPERATION WITH DEST: tmp1 - SRC0: commit1 - SRC1: proofvalue1 + Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], proofValues[args[i_args + 4]][0]); + i_args += 5; + break; + } + case 7: { // COPY tmp1 to tmp1 Goldilocks::copy_avx512(tmp1[args[i_args]], tmp1[args[i_args + 1]]); i_args += 2; break; } - case 7: { + case 8: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: tmp1 Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 8: { + case 9: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: public Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 9: { + case 10: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: number Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 10: { + case 11: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: airvalue1 Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 11: { + case 12: { + // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: proofvalue1 + Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], tmp1[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 13: { // COPY public to tmp1 Goldilocks::copy_avx512(tmp1[args[i_args]], publics[args[i_args + 1]]); i_args += 2; break; } - case 12: { + case 14: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: public Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 13: { + case 15: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: number Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 14: { + case 16: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: airvalue1 Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 15: { + case 17: { + // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: proofvalue1 + Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], publics[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 18: { // COPY number to tmp1 Goldilocks::copy_avx512(tmp1[args[i_args]], numbers_[args[i_args + 1]]); i_args += 2; break; } - case 16: { + case 19: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: number Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], numbers_[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 17: { + case 20: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: airvalue1 Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], numbers_[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 18: { + case 21: { + // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: proofvalue1 + Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], numbers_[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 22: { // COPY airvalue1 to tmp1 Goldilocks::copy_avx512(tmp1[args[i_args]], airValues[args[i_args + 1]][0]); i_args += 2; break; } - case 19: { + case 23: { // OPERATION WITH DEST: tmp1 - SRC0: airvalue1 - SRC1: airvalue1 Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], airValues[args[i_args + 2]][0], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 20: { + case 24: { + // OPERATION WITH DEST: tmp1 - SRC0: airvalue1 - SRC1: proofvalue1 + Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], airValues[args[i_args + 2]][0], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 25: { + // COPY proofvalue1 to tmp1 + Goldilocks::copy_avx512(tmp1[args[i_args]], proofValues[args[i_args + 1]][0]); + i_args += 2; + break; + } + case 26: { + // OPERATION WITH DEST: tmp1 - SRC0: proofvalue1 - SRC1: proofvalue1 + Goldilocks::op_avx512(args[i_args], tmp1[args[i_args + 1]], proofValues[args[i_args + 2]][0], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 27: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], bufferT_[nColsStagesAcc[args[i_args + 4]] + args[i_args + 5]]); i_args += 6; break; } - case 21: { + case 28: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: tmp1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], tmp1[args[i_args + 4]]); i_args += 5; break; } - case 22: { + case 29: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], publics[args[i_args + 4]]); i_args += 5; break; } - case 23: { + case 30: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], numbers_[args[i_args + 4]]); i_args += 5; break; } - case 24: { + case 31: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airvalue1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], airValues[args[i_args + 4]][0]); i_args += 5; break; } - case 25: { + case 32: { + // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], proofValues[args[i_args + 4]][0]); + i_args += 5; + break; + } + case 33: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 26: { + case 34: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 27: { + case 35: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 28: { + case 36: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 29: { + case 37: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airvalue1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 30: { + case 38: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 39: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 31: { + case 40: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: tmp1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 32: { + case 41: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 33: { + case 42: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 34: { + case 43: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airvalue1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 35: { + case 44: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue1 + Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 45: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 36: { + case 46: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: tmp1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 37: { + case 47: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 38: { + case 48: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 39: { + case 49: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airvalue1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 40: { + case 50: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue1 + Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 51: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 41: { + case 52: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: tmp1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 42: { + case 53: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 43: { + case 54: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 44: { + case 55: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: airvalue1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 45: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: commit1 + case 56: { + // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 57: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 46: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: tmp1 + case 58: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: tmp1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], tmp1[args[i_args + 3]]); i_args += 4; break; } - case 47: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: public + case 59: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 48: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: number + case 60: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 49: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: airvalue1 + case 61: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: airvalue1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], airValues[args[i_args + 3]][0]); i_args += 4; break; } - case 50: { + case 62: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], proofValues[args[i_args + 3]][0]); + i_args += 4; + break; + } + case 63: { // COPY commit3 to tmp3 Goldilocks3::copy_avx512(tmp3[args[i_args]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 1]] + args[i_args + 2]]); i_args += 3; break; } - case 51: { + case 64: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: commit3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 4]] + args[i_args + 5]]); i_args += 6; break; } - case 52: { + case 65: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: tmp3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], tmp3[args[i_args + 4]]); i_args += 5; break; } - case 53: { + case 66: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: challenge Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], challenges[args[i_args + 4]]); i_args += 5; break; } - case 54: { + case 67: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airgroupvalue Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], airgroupValues[args[i_args + 4]]); i_args += 5; break; } - case 55: { + case 68: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], airValues[args[i_args + 4]]); i_args += 5; break; } - case 56: { - // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue + case 69: { + // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], proofValues[args[i_args + 4]]); i_args += 5; break; } - case 57: { + case 70: { // COPY tmp3 to tmp3 Goldilocks3::copy_avx512(tmp3[args[i_args]], tmp3[args[i_args + 1]]); i_args += 2; break; } - case 58: { + case 71: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], tmp3[args[i_args + 3]]); i_args += 4; break; } - case 59: { + case 72: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: challenge Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], challenges[args[i_args + 3]]); i_args += 4; break; } - case 60: { + case 73: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airgroupvalue Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], airgroupValues[args[i_args + 3]]); i_args += 4; break; } - case 61: { + case 74: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 62: { - // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue + case 75: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 63: { + case 76: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: challenge Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], challenges[args[i_args + 3]]); i_args += 4; break; } - case 64: { + case 77: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airgroupvalue Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], airgroupValues[args[i_args + 3]]); i_args += 4; break; } - case 65: { + case 78: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 66: { - // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue + case 79: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 67: { + case 80: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airgroupvalue Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], airgroupValues[args[i_args + 3]]); i_args += 4; break; } - case 68: { + case 81: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 69: { - // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue + case 82: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 70: { + case 83: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: airvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], airValues[args[i_args + 3]]); i_args += 4; break; } - case 71: { - // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue + case 84: { + // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], airValues[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 72: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: proofvalue + case 85: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue3 Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], proofValues[args[i_args + 2]], proofValues[args[i_args + 3]]); i_args += 4; break; } - case 73: { + case 86: { // COPY eval to tmp3 Goldilocks3::copy_avx512(tmp3[args[i_args]], evals[args[i_args + 1]]); i_args += 2; break; } - case 74: { + case 87: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: eval Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], challenges[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; break; } - case 75: { + case 88: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: eval Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], tmp3[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; break; } - case 76: { + case 89: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: commit1 Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], bufferT_[nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]]); i_args += 5; break; } - case 77: { + case 90: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: eval Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], (Goldilocks3::Element_avx512 &)bufferT_[nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]], evals[args[i_args + 4]]); i_args += 5; break; } - case 78: { + case 91: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: eval Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; break; } - case 79: { + case 92: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: public Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], publics[args[i_args + 3]]); i_args += 4; break; } - case 80: { + case 93: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: number Goldilocks3::op_31_avx512(args[i_args], tmp3[args[i_args + 1]], evals[args[i_args + 2]], numbers_[args[i_args + 3]]); i_args += 4; break; } - case 81: { + case 94: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: eval Goldilocks3::op_avx512(args[i_args], tmp3[args[i_args + 1]], airgroupValues[args[i_args + 2]], evals[args[i_args + 3]]); i_args += 4; diff --git a/pil2-stark/src/starkpil/expressions_bin.cpp b/pil2-stark/src/starkpil/expressions_bin.cpp index 308fa0e8..e6187647 100644 --- a/pil2-stark/src/starkpil/expressions_bin.cpp +++ b/pil2-stark/src/starkpil/expressions_bin.cpp @@ -407,24 +407,24 @@ void ExpressionsBin::loadGlobalBin(BinFileUtils::BinFile *globalBin) { } - -VecU64Result ExpressionsBin::getHintIdsByName(std::string name) { - VecU64Result hintIds; - - hintIds.nElements = 0; +void ExpressionsBin::getHintIdsByName(uint64_t* hintIds, std::string name) { + uint64_t c = 0; for (uint64_t i = 0; i < hints.size(); ++i) { if (hints[i].name == name) { - hintIds.nElements++; + hintIds[c++] = i; } } +} - uint64_t c = 0; - hintIds.ids = new uint64_t[hintIds.nElements]; + +uint64_t ExpressionsBin::getNumberHintIdsByName(std::string name) { + + uint64_t nHints = 0; for (uint64_t i = 0; i < hints.size(); ++i) { if (hints[i].name == name) { - hintIds.ids[c++] = i; + nHints++; } } - return hintIds; + return nHints; } \ No newline at end of file diff --git a/pil2-stark/src/starkpil/expressions_bin.hpp b/pil2-stark/src/starkpil/expressions_bin.hpp index b1cc6f7e..90ad6fce 100644 --- a/pil2-stark/src/starkpil/expressions_bin.hpp +++ b/pil2-stark/src/starkpil/expressions_bin.hpp @@ -47,11 +47,6 @@ struct Hint std::vector fields; }; -struct VecU64Result { - uint64_t nElements; - uint64_t* ids; -}; - struct ParserParams { uint32_t stage = 0; @@ -143,7 +138,9 @@ class ExpressionsBin void loadGlobalBin(BinFileUtils::BinFile *globalBin); - VecU64Result getHintIdsByName(std::string name); + uint64_t getNumberHintIdsByName(std::string name); + + void getHintIdsByName(uint64_t* hintIds, std::string name); }; diff --git a/pil2-stark/src/starkpil/expressions_pack.hpp b/pil2-stark/src/starkpil/expressions_pack.hpp index 6f0500b7..84371e14 100644 --- a/pil2-stark/src/starkpil/expressions_pack.hpp +++ b/pil2-stark/src/starkpil/expressions_pack.hpp @@ -122,7 +122,7 @@ class ExpressionsPack : public ExpressionsCtx { if(stage == 1 && !domainExtended) { bufferT_[(nColsStagesAcc[ns*o + stage] + (stagePos + d))*nrowsPack + j] = params.trace[l * nColsStages[stage] + stagePos + d]; } else { - bufferT_[(nColsStagesAcc[ns*o + stage] + (stagePos + d))*nrowsPack + j] = params.pols[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; + bufferT_[(nColsStagesAcc[ns*o + stage] + (stagePos + d))*nrowsPack + j] = params.aux_trace[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; } } } @@ -130,6 +130,7 @@ class ExpressionsPack : public ExpressionsCtx { } for(uint64_t i = 0; i < setupCtx.starkInfo.customCommits.size(); ++i) { + Goldilocks::Element *customCommits = domainExtended ? params.pCustomCommitsExtended[i] : params.pCustomCommits[i]; for(uint64_t j = 0; j < setupCtx.starkInfo.customCommits[i].stageWidths[0]; ++j) { if(!customCommitsUsed[i][j]) continue; PolMap polInfo = setupCtx.starkInfo.customCommitsMap[i][j]; @@ -139,7 +140,7 @@ class ExpressionsPack : public ExpressionsCtx { for(uint64_t o = 0; o < nOpenings; ++o) { for(uint64_t j = 0; j < nrowsPack; ++j) { uint64_t l = (row + j + nextStrides[o]) % domainSize; - bufferT_[(nColsStagesAcc[ns*o + stage] + (stagePos + d))*nrowsPack + j] = params.customCommits[i][offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; + bufferT_[(nColsStagesAcc[ns*o + stage] + (stagePos + d))*nrowsPack + j] = customCommits[offsetsStages[stage] + l * nColsStages[stage] + stagePos + d]; } } } @@ -319,33 +320,60 @@ class ExpressionsPack : public ExpressionsCtx { Goldilocks::Element airgroupValues[setupCtx.starkInfo.airgroupValuesMap.size()*FIELD_EXTENSION*nrowsPack]; if(!compilation_time) { + uint64_t p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.airgroupValuesMap.size(); ++i) { for(uint64_t j = 0; j < nrowsPack; ++j) { - airgroupValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.airgroupValues[i * FIELD_EXTENSION]; - airgroupValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = params.airgroupValues[i * FIELD_EXTENSION + 1]; - airgroupValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = params.airgroupValues[i * FIELD_EXTENSION + 2]; + if(setupCtx.starkInfo.airgroupValuesMap[i].stage == 1) { + airgroupValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.airgroupValues[p]; + airgroupValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = Goldilocks::zero(); + airgroupValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = Goldilocks::zero(); + p += 1; + } else { + airgroupValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.airgroupValues[p]; + airgroupValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = params.airgroupValues[p + 1]; + airgroupValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = params.airgroupValues[p + 2]; + p += 3; + } } } } Goldilocks::Element proofValues[setupCtx.starkInfo.proofValuesMap.size()*FIELD_EXTENSION*nrowsPack]; if(!compilation_time) { + uint64_t p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.proofValuesMap.size(); ++i) { for(uint64_t j = 0; j < nrowsPack; ++j) { - proofValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.proofValues[i * FIELD_EXTENSION]; - proofValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = params.proofValues[i * FIELD_EXTENSION + 1]; - proofValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = params.proofValues[i * FIELD_EXTENSION + 2]; + if(setupCtx.starkInfo.proofValuesMap[i].stage == 1) { + proofValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.proofValues[p]; + proofValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = Goldilocks::zero(); + proofValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = Goldilocks::zero(); + p += 1; + } else { + proofValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.proofValues[p]; + proofValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = params.proofValues[p + 1]; + proofValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = params.proofValues[p + 2]; + p += 3; + } } } } Goldilocks::Element airValues[setupCtx.starkInfo.airValuesMap.size()*FIELD_EXTENSION*nrowsPack]; if(!compilation_time) { + uint64_t p = 0; for(uint64_t i = 0; i < setupCtx.starkInfo.airValuesMap.size(); ++i) { for(uint64_t j = 0; j < nrowsPack; ++j) { - airValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.airValues[i * FIELD_EXTENSION]; - airValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = params.airValues[i * FIELD_EXTENSION + 1]; - airValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = params.airValues[i * FIELD_EXTENSION + 2]; + if(setupCtx.starkInfo.airValuesMap[i].stage == 1) { + airValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.airValues[p]; + airValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = Goldilocks::zero(); + airValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = Goldilocks::zero(); + p += 1; + } else { + airValues[(i*FIELD_EXTENSION)*nrowsPack + j] = params.airValues[p]; + airValues[(i*FIELD_EXTENSION + 1)*nrowsPack + j] = params.airValues[p + 1]; + airValues[(i*FIELD_EXTENSION + 2)*nrowsPack + j] = params.airValues[p + 2]; + p += 3; + } } } } @@ -420,456 +448,534 @@ class ExpressionsPack : public ExpressionsCtx { break; } case 6: { + // OPERATION WITH DEST: tmp1 - SRC0: commit1 - SRC1: proofvalue1 + Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &proofValues[args[i_args + 4] * nrowsPack * FIELD_EXTENSION]); + i_args += 5; + break; + } + case 7: { // COPY tmp1 to tmp1 Goldilocks::copy_pack(nrowsPack, &tmp1[args[i_args] * nrowsPack], &tmp1[args[i_args + 1] * nrowsPack]); i_args += 2; break; } - case 7: { + case 8: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: tmp1 Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &tmp1[args[i_args + 2] * nrowsPack], &tmp1[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 8: { + case 9: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: public Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &tmp1[args[i_args + 2] * nrowsPack], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 9: { + case 10: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: number Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &tmp1[args[i_args + 2] * nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 10: { + case 11: { // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: airvalue1 Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &tmp1[args[i_args + 2] * nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 11: { + case 12: { + // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: proofvalue1 + Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &tmp1[args[i_args + 2] * nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 13: { // COPY public to tmp1 Goldilocks::copy_pack(nrowsPack, &tmp1[args[i_args] * nrowsPack], &publics[args[i_args + 1] * nrowsPack]); i_args += 2; break; } - case 12: { + case 14: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: public Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &publics[args[i_args + 2] * nrowsPack], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 13: { + case 15: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: number Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &publics[args[i_args + 2] * nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 14: { + case 16: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: airvalue1 Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &publics[args[i_args + 2] * nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 15: { + case 17: { + // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: proofvalue1 + Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &publics[args[i_args + 2] * nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 18: { // COPY number to tmp1 Goldilocks::copy_pack(nrowsPack, &tmp1[args[i_args] * nrowsPack], &numbers_[args[i_args + 1]*nrowsPack]); i_args += 2; break; } - case 16: { + case 19: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: number Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &numbers_[args[i_args + 2]*nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 17: { + case 20: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: airvalue1 Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &numbers_[args[i_args + 2]*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 18: { + case 21: { + // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: proofvalue1 + Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &numbers_[args[i_args + 2]*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 22: { // COPY airvalue1 to tmp1 Goldilocks::copy_pack(nrowsPack, &tmp1[args[i_args] * nrowsPack], &airValues[args[i_args + 1]*FIELD_EXTENSION*nrowsPack]); i_args += 2; break; } - case 19: { + case 23: { // OPERATION WITH DEST: tmp1 - SRC0: airvalue1 - SRC1: airvalue1 Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 20: { + case 24: { + // OPERATION WITH DEST: tmp1 - SRC0: airvalue1 - SRC1: proofvalue1 + Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 25: { + // COPY proofvalue1 to tmp1 + Goldilocks::copy_pack(nrowsPack, &tmp1[args[i_args] * nrowsPack], &proofValues[args[i_args + 1] * nrowsPack * FIELD_EXTENSION]); + i_args += 2; + break; + } + case 26: { + // OPERATION WITH DEST: tmp1 - SRC0: proofvalue1 - SRC1: proofvalue1 + Goldilocks::op_pack(nrowsPack, args[i_args], &tmp1[args[i_args + 1] * nrowsPack], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 27: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 4]] + args[i_args + 5]) * nrowsPack]); i_args += 6; break; } - case 21: { + case 28: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: tmp1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &tmp1[args[i_args + 4] * nrowsPack]); i_args += 5; break; } - case 22: { + case 29: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &publics[args[i_args + 4] * nrowsPack]); i_args += 5; break; } - case 23: { + case 30: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &numbers_[args[i_args + 4]*nrowsPack]); i_args += 5; break; } - case 24: { + case 31: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airvalue1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &airValues[args[i_args + 4]*FIELD_EXTENSION*nrowsPack]); i_args += 5; break; } - case 25: { + case 32: { + // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue1 + Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &proofValues[args[i_args + 4] * nrowsPack * FIELD_EXTENSION]); + i_args += 5; + break; + } + case 33: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]) * nrowsPack]); i_args += 5; break; } - case 26: { + case 34: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &tmp1[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 27: { + case 35: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 28: { + case 36: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 29: { + case 37: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airvalue1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 30: { + case 38: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue1 + Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 39: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]) * nrowsPack]); i_args += 5; break; } - case 31: { + case 40: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: tmp1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &tmp1[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 32: { + case 41: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 33: { + case 42: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 34: { + case 43: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airvalue1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 35: { + case 44: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue1 + Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 45: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]) * nrowsPack]); i_args += 5; break; } - case 36: { + case 46: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: tmp1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &tmp1[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 37: { + case 47: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 38: { + case 48: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 39: { + case 49: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airvalue1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 40: { + case 50: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue1 + Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 51: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]) * nrowsPack]); i_args += 5; break; } - case 41: { + case 52: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: tmp1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &tmp1[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 42: { + case 53: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 43: { + case 54: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 44: { + case 55: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: airvalue1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 45: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: commit1 + case 56: { + // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 57: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]) * nrowsPack]); i_args += 5; break; } - case 46: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: tmp1 + case 58: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: tmp1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &tmp1[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 47: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: public + case 59: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 48: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: number + case 60: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 49: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: airvalue1 + case 61: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: airvalue1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 50: { + case 62: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 63: { // COPY commit3 to tmp3 Goldilocks3::copy_pack(nrowsPack, &tmp3[args[i_args] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 1]] + args[i_args + 2]) * nrowsPack]); i_args += 3; break; } - case 51: { + case 64: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: commit3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 4]] + args[i_args + 5]) * nrowsPack]); i_args += 6; break; } - case 52: { + case 65: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: tmp3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &tmp3[args[i_args + 4] * nrowsPack * FIELD_EXTENSION]); i_args += 5; break; } - case 53: { + case 66: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: challenge Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &challenges[args[i_args + 4]*FIELD_EXTENSION*nrowsPack]); i_args += 5; break; } - case 54: { + case 67: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airgroupvalue Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &airgroupValues[args[i_args + 4]*FIELD_EXTENSION*nrowsPack]); i_args += 5; break; } - case 55: { + case 68: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: airvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &airValues[args[i_args + 4]*FIELD_EXTENSION*nrowsPack]); i_args += 5; break; } - case 56: { - // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue + case 69: { + // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: proofvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &proofValues[args[i_args + 4] * nrowsPack * FIELD_EXTENSION]); i_args += 5; break; } - case 57: { + case 70: { // COPY tmp3 to tmp3 Goldilocks3::copy_pack(nrowsPack, &tmp3[args[i_args] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION]); i_args += 2; break; } - case 58: { + case 71: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); i_args += 4; break; } - case 59: { + case 72: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: challenge Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 60: { + case 73: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airgroupvalue Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 61: { + case 74: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 62: { - // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue + case 75: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); i_args += 4; break; } - case 63: { + case 76: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: challenge Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &challenges[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 64: { + case 77: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airgroupvalue Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airgroupValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 65: { + case 78: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: airvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 66: { - // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue + case 79: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); i_args += 4; break; } - case 67: { + case 80: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airgroupvalue Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airgroupValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 68: { + case 81: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 69: { - // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue + case 82: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); i_args += 4; break; } - case 70: { + case 83: { // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: airvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &airValues[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 71: { - // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue + case 84: { + // OPERATION WITH DEST: tmp3 - SRC0: airvalue3 - SRC1: proofvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); i_args += 4; break; } - case 72: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: proofvalue + case 85: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue3 Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &proofValues[args[i_args + 3] * nrowsPack * FIELD_EXTENSION]); i_args += 4; break; } - case 73: { + case 86: { // COPY eval to tmp3 Goldilocks3::copy_pack(nrowsPack, &tmp3[args[i_args] * nrowsPack * FIELD_EXTENSION], &evals[args[i_args + 1]*FIELD_EXTENSION*nrowsPack]); i_args += 2; break; } - case 74: { + case 87: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: eval Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &evals[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 75: { + case 88: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: eval Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &tmp3[args[i_args + 2] * nrowsPack * FIELD_EXTENSION], &evals[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 76: { + case 89: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: commit1 Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &evals[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &bufferT_[(nColsStagesAcc[args[i_args + 3]] + args[i_args + 4]) * nrowsPack]); i_args += 5; break; } - case 77: { + case 90: { // OPERATION WITH DEST: tmp3 - SRC0: commit3 - SRC1: eval Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &bufferT_[(nColsStagesAcc[args[i_args + 2]] + args[i_args + 3]) * nrowsPack], &evals[args[i_args + 4]*FIELD_EXTENSION*nrowsPack]); i_args += 5; break; } - case 78: { + case 91: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: eval Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &evals[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &evals[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; break; } - case 79: { + case 92: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: public Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &evals[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &publics[args[i_args + 3] * nrowsPack]); i_args += 4; break; } - case 80: { + case 93: { // OPERATION WITH DEST: tmp3 - SRC0: eval - SRC1: number Goldilocks3::op_31_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &evals[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &numbers_[args[i_args + 3]*nrowsPack]); i_args += 4; break; } - case 81: { + case 94: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: eval Goldilocks3::op_pack(nrowsPack, args[i_args], &tmp3[args[i_args + 1] * nrowsPack * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]*FIELD_EXTENSION*nrowsPack], &evals[args[i_args + 3]*FIELD_EXTENSION*nrowsPack]); i_args += 4; diff --git a/pil2-stark/src/starkpil/final_snark_proof.hpp b/pil2-stark/src/starkpil/final_snark_proof.hpp index afdbe3ce..f8e136ce 100644 --- a/pil2-stark/src/starkpil/final_snark_proof.hpp +++ b/pil2-stark/src/starkpil/final_snark_proof.hpp @@ -4,10 +4,10 @@ #include "utils.hpp" #include "alt_bn128.hpp" -void genFinalSnarkProof(void *pWitnessFinal, std::string zkeyFile, std::string outputDir) { +void genFinalSnarkProof(void *circomWitnessFinal, std::string zkeyFile, std::string outputDir) { TimerStart(PROVER_FINAL_SNARK_PROOF); - AltBn128::FrElement *witnessFinal = (AltBn128::FrElement *)pWitnessFinal; + AltBn128::FrElement *witnessFinal = (AltBn128::FrElement *)circomWitnessFinal; // Save public file json publicJson; AltBn128::FrElement aux; diff --git a/pil2-stark/src/starkpil/fri/fri.cpp b/pil2-stark/src/starkpil/fri/fri.cpp index dd41da77..a7c0543c 100644 --- a/pil2-stark/src/starkpil/fri/fri.cpp +++ b/pil2-stark/src/starkpil/fri/fri.cpp @@ -86,7 +86,7 @@ void FRI::merkelize(uint64_t step, FRIProof &proof, Go treeFRI->merkelize(); treeFRI->getRoot(&proof.proof.fri.treesFRI[step].root[0]); - delete aux; + delete[] aux; } template diff --git a/pil2-stark/src/starkpil/gen_recursive_proof.hpp b/pil2-stark/src/starkpil/gen_recursive_proof.hpp index 65706b61..53f00e76 100644 --- a/pil2-stark/src/starkpil/gen_recursive_proof.hpp +++ b/pil2-stark/src/starkpil/gen_recursive_proof.hpp @@ -1,7 +1,7 @@ #include "starks.hpp" template -void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupId, Goldilocks::Element *pAddress, Goldilocks::Element *pConstPols, Goldilocks::Element *pConstTree, Goldilocks::Element *publicInputs, std::string proofFile, bool vadcop) { +void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupId, Goldilocks::Element *witness, Goldilocks::Element *pConstPols, Goldilocks::Element *pConstTree, Goldilocks::Element *publicInputs, std::string proofFile, bool vadcop) { TimerStart(STARK_PROOF); FRIProof proof(setupCtx.starkInfo); @@ -10,7 +10,9 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI Starks starks(setupCtx, pConstTree); - Goldilocks::Element *pols = new Goldilocks::Element[setupCtx.starkInfo.mapTotalN]; + setupCtx.starkInfo.addMemoryRecursive(); + + Goldilocks::Element *trace = new Goldilocks::Element[setupCtx.starkInfo.mapTotalN]; #ifdef __AVX512__ ExpressionsAvx512 expressionsCtx(setupCtx); @@ -29,8 +31,8 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI vector airgroupValuesCalculated(setupCtx.starkInfo.airgroupValuesMap.size(), false); StepsParams params = { - trace: pAddress, - pols : pols, + witness, + trace, publicInputs : publicInputs, proofValues: nullptr, challenges : challenges, @@ -68,7 +70,7 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI } } TimerStart(STARK_COMMIT_STAGE_1); - starks.commitStage(1, params.trace, params.pols, proof); + starks.commitStage(1, params.trace, params.aux_trace, proof); TimerStopAndLog(STARK_COMMIT_STAGE_1); starks.addTranscript(transcript, &proof.proof.roots[0][0], nFieldElements); @@ -102,7 +104,7 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI } Polinomial gprodTransposedPol; - setupCtx.starkInfo.getPolynomial(gprodTransposedPol, params.pols, "cm", setupCtx.starkInfo.cmPolsMap[gprodFieldId], false); + setupCtx.starkInfo.getPolynomial(gprodTransposedPol, params.aux_trace, "cm", setupCtx.starkInfo.cmPolsMap[gprodFieldId], false); #pragma omp parallel for for(uint64_t j = 0; j < N; ++j) { std::memcpy(gprodTransposedPol[j], &gprod[j*FIELD_EXTENSION], FIELD_EXTENSION * sizeof(Goldilocks::Element)); @@ -116,7 +118,7 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI TimerStopAndLog(CALCULATE_IM_POLS); TimerStart(STARK_COMMIT_STAGE_2); - starks.commitStage(2, nullptr, params.pols, proof); + starks.commitStage(2, nullptr, params.aux_trace, proof); TimerStopAndLog(STARK_COMMIT_STAGE_2); starks.addTranscript(transcript, &proof.proof.roots[1][0], nFieldElements); @@ -131,10 +133,10 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI } } - expressionsCtx.calculateExpression(params, ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("q", true)]], setupCtx.starkInfo.cExpId); + expressionsCtx.calculateExpression(params, ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("q", true)]], setupCtx.starkInfo.cExpId); TimerStart(STARK_COMMIT_QUOTIENT_POLYNOMIAL); - starks.commitStage(setupCtx.starkInfo.nStages + 1, nullptr, params.pols, proof); + starks.commitStage(setupCtx.starkInfo.nStages + 1, nullptr, params.aux_trace, proof); TimerStopAndLog(STARK_COMMIT_QUOTIENT_POLYNOMIAL); starks.addTranscript(transcript, &proof.proof.roots[setupCtx.starkInfo.nStages][0], nFieldElements); TimerStopAndLog(STARK_STEP_Q); @@ -151,7 +153,7 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI } Goldilocks::Element *xiChallenge = &challenges[xiChallengeIndex * FIELD_EXTENSION]; - Goldilocks::Element* LEv = ¶ms.pols[setupCtx.starkInfo.mapOffsets[make_pair("LEv", true)]]; + Goldilocks::Element* LEv = ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[make_pair("LEv", true)]]; starks.computeLEv(xiChallenge, LEv); starks.computeEvals(params ,LEv, proof); @@ -180,13 +182,13 @@ void *genRecursiveProof(SetupCtx& setupCtx, json& globalInfo, uint64_t airgroupI TimerStart(STARK_STEP_FRI); TimerStart(COMPUTE_FRI_POLYNOMIAL); - params.xDivXSub = ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("xDivXSubXi", true)]]; + params.xDivXSub = ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("xDivXSubXi", true)]]; starks.calculateXDivXSub(xiChallenge, params.xDivXSub); starks.calculateFRIPolynomial(params); TimerStopAndLog(COMPUTE_FRI_POLYNOMIAL); Goldilocks::Element challenge[FIELD_EXTENSION]; - Goldilocks::Element *friPol = ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("f", true)]]; + Goldilocks::Element *friPol = ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("f", true)]]; TimerStart(STARK_FRI_FOLDING); uint64_t nBitsExt = setupCtx.starkInfo.starkStruct.steps[0].nBits; diff --git a/pil2-stark/src/starkpil/global_constraints.hpp b/pil2-stark/src/starkpil/global_constraints.hpp index 65812dd8..aa3ca4d7 100644 --- a/pil2-stark/src/starkpil/global_constraints.hpp +++ b/pil2-stark/src/starkpil/global_constraints.hpp @@ -1,12 +1,19 @@ #ifndef GLOBAL_CONSTRAINTS_HPP #define GLOBAL_CONSTRAINTS_HPP -#include "timer.hpp" #include "goldilocks_base_field.hpp" #include using json = nlohmann::json; -void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* publics, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) { +struct GlobalConstraintInfo { + uint64_t id; + uint64_t dim; + bool valid; + bool skip; + uint64_t value[3]; +}; + +void calculateGlobalExpression(json& globalInfo, Goldilocks::Element* dest, Goldilocks::Element* publics, Goldilocks::Element* challenges, Goldilocks::Element* proofValues_, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) { uint8_t* ops = &parserArgs.ops[parserParams.opsOffset]; uint16_t* args = &parserArgs.args[parserParams.argsOffset]; @@ -16,6 +23,22 @@ void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* p Goldilocks::Element tmp1[parserParams.nTemp1]; Goldilocks::Element tmp3[parserParams.nTemp3*FIELD_EXTENSION]; + Goldilocks::Element proofValues[globalInfo["proofValuesMap"].size()*FIELD_EXTENSION]; + uint64_t p = 0; + for(uint64_t i = 0; i < globalInfo["proofValuesMap"].size(); ++i) { + if(globalInfo["proofValuesMap"][i]["stage"] == 1) { + proofValues[i*FIELD_EXTENSION] = proofValues_[p]; + proofValues[i*FIELD_EXTENSION + 1] = Goldilocks::zero(); + proofValues[i*FIELD_EXTENSION + 2] = Goldilocks::zero(); + p += 1; + } else { + proofValues[i*FIELD_EXTENSION] = proofValues_[p]; + proofValues[i*FIELD_EXTENSION + 1] = proofValues_[p + 1]; + proofValues[i*FIELD_EXTENSION + 2] = proofValues_[p + 2]; + p += 3; + } + } + Goldilocks::Element numbers_[parserArgs.nNumbers]; for(uint64_t i = 0; i < parserArgs.nNumbers; ++i) { numbers_[i] = Goldilocks::fromU64(parserArgs.numbers[i]); @@ -42,159 +65,203 @@ void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* p break; } case 3: { + // OPERATION WITH DEST: tmp1 - SRC0: tmp1 - SRC1: proofvalue1 + Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &tmp1[args[i_args + 2]], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 4: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: public Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &publics[args[i_args + 2]], &publics[args[i_args + 3]]); i_args += 4; break; } - case 4: { + case 5: { // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: number Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &publics[args[i_args + 2]], &numbers_[args[i_args + 3]]); i_args += 4; break; } - case 5: { + case 6: { + // OPERATION WITH DEST: tmp1 - SRC0: public - SRC1: proofvalue1 + Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &publics[args[i_args + 2]], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 7: { // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: number Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &numbers_[args[i_args + 2]], &numbers_[args[i_args + 3]]); i_args += 4; break; } - case 6: { + case 8: { + // OPERATION WITH DEST: tmp1 - SRC0: number - SRC1: proofvalue1 + Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &numbers_[args[i_args + 2]], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 9: { + // OPERATION WITH DEST: tmp1 - SRC0: proofvalue1 - SRC1: proofvalue1 + Goldilocks::op_pack(1, args[i_args], &tmp1[args[i_args + 1]], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 10: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp1 Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &tmp1[args[i_args + 3]]); i_args += 4; break; } - case 7: { + case 11: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: public Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &publics[args[i_args + 3]]); i_args += 4; break; } - case 8: { + case 12: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: number Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &numbers_[args[i_args + 3]]); i_args += 4; break; } - case 9: { + case 13: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue1 + Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 14: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: tmp1 Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &tmp1[args[i_args + 4]]); i_args += 5; break; } - case 10: { + case 15: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: public Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &publics[args[i_args + 4]]); i_args += 5; break; } - case 11: { + case 16: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: number Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &numbers_[args[i_args + 4]]); i_args += 5; break; } - case 12: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: tmp1 + case 17: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue1 + Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &proofValues[args[i_args + 4] * FIELD_EXTENSION]); + i_args += 5; + break; + } + case 18: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: tmp1 Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &tmp1[args[i_args + 3]]); i_args += 4; break; } - case 13: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: public + case 19: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: public Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &publics[args[i_args + 3]]); i_args += 4; break; } - case 14: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: number + case 20: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: number Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &numbers_[args[i_args + 3]]); i_args += 4; break; } - case 15: { + case 21: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue1 + Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 22: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: tmp1 Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &tmp1[args[i_args + 3]]); i_args += 4; break; } - case 16: { + case 23: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: public Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &publics[args[i_args + 3]]); i_args += 4; break; } - case 17: { + case 24: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: number Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &numbers_[args[i_args + 3]]); i_args += 4; break; } - case 18: { + case 25: { + // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: proofvalue1 + Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); + i_args += 4; + break; + } + case 26: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp3 Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &tmp3[args[i_args + 3] * FIELD_EXTENSION]); i_args += 4; break; } - case 19: { + case 27: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airgroupvalue Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &airgroupValues[args[i_args + 3]][args[i_args + 4] * FIELD_EXTENSION]); i_args += 5; break; } - case 20: { - // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue + case 28: { + // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue3 Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); i_args += 4; break; } - case 21: { + case 29: { // OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: challenge Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION]); i_args += 4; break; } - case 22: { + case 30: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airgroupvalue Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &airgroupValues[args[i_args + 4]][args[i_args + 5] * FIELD_EXTENSION]); i_args += 6; break; } - case 23: { - // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue + case 31: { + // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue3 Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &proofValues[args[i_args + 4] * FIELD_EXTENSION]); i_args += 5; break; } - case 24: { + case 32: { // OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: challenge Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &challenges[args[i_args + 4]*FIELD_EXTENSION]); i_args += 5; break; } - case 25: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: proofvalue + case 33: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: proofvalue3 Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]); i_args += 4; break; } - case 26: { - // OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: challenge + case 34: { + // OPERATION WITH DEST: tmp3 - SRC0: proofvalue3 - SRC1: challenge Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION]); i_args += 4; break; } - case 27: { + case 35: { // OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: challenge Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION]); i_args += 4; break; } - default: { - std::cout << " Wrong operation!" << std::endl; - exit(1); - } } } @@ -209,59 +276,174 @@ void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* p } -bool verifyGlobalConstraint(Goldilocks::Element* publics, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) { +void verifyGlobalConstraint(json& globalInfo, uint64_t constraintId, Goldilocks::Element* publics, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams, GlobalConstraintInfo& globalConstraintInfo) { + + globalConstraintInfo.id = constraintId; + globalConstraintInfo.valid = true; Goldilocks::Element dest[parserParams.destDim]; - calculateGlobalExpression(dest, publics, challenges, proofValues, airgroupValues, parserArgs, parserParams); + calculateGlobalExpression(globalInfo, dest, publics, challenges, proofValues, airgroupValues, parserArgs, parserParams); - bool isValidConstraint = true; if(parserParams.destDim == 1) { - if(!Goldilocks::isZero(dest[0])) { - cout << "Global constraint check failed with value: " << Goldilocks::toString(dest[0]) << endl; - isValidConstraint = false; - } + globalConstraintInfo.dim = parserParams.destDim; + globalConstraintInfo.value[0] = Goldilocks::toU64(dest[0]); + globalConstraintInfo.value[1] = 0; + globalConstraintInfo.value[2] = 0; + if(globalConstraintInfo.value[0] != 0) globalConstraintInfo.valid = false; } else { - for(uint64_t i = 0; i < FIELD_EXTENSION; ++i) { - if(!Goldilocks::isZero(dest[i])) { - cout << "Global constraint check failed with value: [" << Goldilocks::toString(dest[0]) << ", " << Goldilocks::toString(dest[1]) << ", " << Goldilocks::toString(dest[2]) << "]" << endl; - isValidConstraint = false; - break; - } + globalConstraintInfo.dim = parserParams.destDim; + globalConstraintInfo.value[0] = Goldilocks::toU64(dest[0]); + globalConstraintInfo.value[1] = Goldilocks::toU64(dest[1]); + globalConstraintInfo.value[2] = Goldilocks::toU64(dest[2]); + if(globalConstraintInfo.value[0] != 0 || globalConstraintInfo.value[1] != 0 || globalConstraintInfo.value[2] != 0) globalConstraintInfo.valid = false; + } +} + +uint64_t getNumberGlobalConstraints(ExpressionsBin &globalConstraintsBin) { + std::vector globalConstraints = globalConstraintsBin.constraintsInfoDebug; + return globalConstraints.size(); +} + +void getGlobalConstraintsLinesSizes(ExpressionsBin &globalConstraintsBin, uint64_t* constraintsLinesSizes) { + std::vector globalConstraints = globalConstraintsBin.constraintsInfoDebug; + for(uint64_t i = 0; i globalConstraints = globalConstraintsBin.constraintsInfoDebug; + for(uint64_t i = 0; i < globalConstraintsBin.constraintsInfoDebug.size(); ++i) { + std::memcpy(constraintsLines[i], globalConstraints[i].line.data(), globalConstraints[i].line.size()); + } +} + + +void verifyGlobalConstraints(json& globalInfo, ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, GlobalConstraintInfo *globalConstraintsInfo) +{ + std::vector globalConstraints = globalConstraintsBin.constraintsInfoDebug; + + for(uint64_t i = 0; i < globalConstraints.size(); ++i) { + if(!globalConstraintsInfo[i].skip) { + verifyGlobalConstraint(globalInfo, i, publicInputs, challenges, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsConstraints, globalConstraints[i], globalConstraintsInfo[i]); } } +} - if(isValidConstraint) { - TimerLog(VALID_CONSTRAINT); - return true; +std::string getExpressionDebug(json& globalInfo, ExpressionsBin &globalConstraintsBin, uint64_t hintId, std::string hintFieldName, HintFieldValue hintFieldVal) { + std::string debug = "Hint name " + hintFieldName + " for hint id " + to_string(hintId) + " is "; + if (hintFieldVal.operand == opType::tmp) { + if(globalConstraintsBin.expressionsInfo[hintFieldVal.id].line != "") { + debug += "the expression with id: " + to_string(hintFieldVal.id) + " " + globalConstraintsBin.expressionsInfo[hintFieldVal.id].line; + } + } else if (hintFieldVal.operand == opType::public_) { + debug += "public input " + to_string(globalInfo["publicsMap"][hintFieldVal.id]["name"]); + } else if (hintFieldVal.operand == opType::number) { + debug += "number " + to_string(hintFieldVal.value); + } else if (hintFieldVal.operand == opType::airgroupvalue) { + debug += "airgroupvalue "; + } else if (hintFieldVal.operand == opType::proofvalue) { + debug += "proof value " + to_string(globalInfo["proofValuesMap"][hintFieldVal.id]["name"]); + } else if (hintFieldVal.operand == opType::string_) { + debug += "string " + hintFieldVal.stringValue; } else { - TimerLog(INVALID_CONSTRAINT); - return false; + zklog.error("Unknown HintFieldType"); + exitProcess(); + exit(-1); } + + return debug; } - -bool verifyGlobalConstraints(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues) -{ +uint64_t getHintFieldGlobalConstraintValues(ExpressionsBin &globalConstraintsBin, uint64_t hintId, std::string hintFieldName) { + if(globalConstraintsBin.hints.size() == 0) { + zklog.error("No hints were found."); + exitProcess(); + exit(-1); + } + + Hint hint = globalConstraintsBin.hints[hintId]; + + auto hintField = std::find_if(hint.fields.begin(), hint.fields.end(), [hintFieldName](const HintField& hintField) { + return hintField.name == hintFieldName; + }); - std::vector globalConstraintsInfo = globalConstraintsBin.constraintsInfoDebug; - - bool validGlobalConstraints = true; - for(uint64_t i = 0; i < globalConstraintsInfo.size(); ++i) { - TimerLog(CHECKING_CONSTRAINT); - cout << "--------------------------------------------------------" << endl; - cout << globalConstraintsInfo[i].line << endl; - cout << "--------------------------------------------------------" << endl; - if(!verifyGlobalConstraint(publicInputs, challenges, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsConstraints, globalConstraintsInfo[i])) { - validGlobalConstraints = false; - }; + if(hintField == hint.fields.end()) { + zklog.error("Hint field " + hintFieldName + " not found in hint " + hint.name + "."); + exitProcess(); + exit(-1); } - return validGlobalConstraints; + return hintField->values.size(); } +void getHintFieldGlobalConstraintSizes(json& globalInfo, ExpressionsBin &globalConstraintsBin, HintFieldInfo *hintFieldValues, uint64_t hintId, std::string hintFieldName, bool print_expression) { + if(globalConstraintsBin.hints.size() == 0) { + zklog.error("No hints were found."); + exitProcess(); + exit(-1); + } + + Hint hint = globalConstraintsBin.hints[hintId]; + + auto hintField = std::find_if(hint.fields.begin(), hint.fields.end(), [hintFieldName](const HintField& hintField) { + return hintField.name == hintFieldName; + }); + + if(hintField == hint.fields.end()) { + zklog.error("Hint field " + hintFieldName + " not found in hint " + hint.name + "."); + exitProcess(); + exit(-1); + } + + for(uint64_t i = 0; i < hintField->values.size(); ++i) { + HintFieldValue hintFieldVal = hintField->values[i]; + + if(print_expression) { + std::string expression_line = getExpressionDebug(globalInfo, globalConstraintsBin, hintId, hintFieldName, hintFieldVal); + hintFieldValues[i].expression_line_size = expression_line.size(); + } + + if (hintFieldVal.operand == opType::tmp) { + uint64_t dim = globalConstraintsBin.expressionsInfo[hintFieldVal.id].destDim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; + hintFieldValues[i].offset = dim; + hintFieldValues[i].size = dim; + } else if (hintFieldVal.operand == opType::public_) { + hintFieldValues[i].size = 1; + hintFieldValues[i].fieldType = HintFieldType::Field; + hintFieldValues[i].offset = 1; + } else if (hintFieldVal.operand == opType::number) { + hintFieldValues[i].size = 1; + hintFieldValues[i].fieldType = HintFieldType::Field; + hintFieldValues[i].offset = 1; + } else if (hintFieldVal.operand == opType::airgroupvalue) { + hintFieldValues[i].size = FIELD_EXTENSION; + hintFieldValues[i].fieldType = HintFieldType::FieldExtended; + hintFieldValues[i].offset = FIELD_EXTENSION; + } else if (hintFieldVal.operand == opType::proofvalue) { + uint64_t dim = globalInfo["proofValuesMap"][hintFieldVal.id]["stage"] == 1 ? 1 : FIELD_EXTENSION; + hintFieldValues[i].size = dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; + hintFieldValues[i].offset = FIELD_EXTENSION; + } else if (hintFieldVal.operand == opType::string_) { + hintFieldValues[i].fieldType = HintFieldType::String; + hintFieldValues[i].size = hintFieldVal.stringValue.size(); + hintFieldValues[i].offset = 0; + } else { + zklog.error("Unknown HintFieldType"); + exitProcess(); + exit(-1); + } + + hintFieldValues[i].matrix_size = hintFieldVal.pos.size(); + } + + return; +} -HintFieldValues getHintFieldGlobalConstraint(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, uint64_t hintId, std::string hintFieldName, bool print_expression) { +void getHintFieldGlobalConstraint(json& globalInfo, ExpressionsBin &globalConstraintsBin, HintFieldInfo *hintFieldValues, Goldilocks::Element* publicInputs, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, uint64_t hintId, std::string hintFieldName, bool print_expression) { if(globalConstraintsBin.hints.size() == 0) { @@ -282,85 +464,44 @@ HintFieldValues getHintFieldGlobalConstraint(ExpressionsBin &globalConstraintsBi exit(-1); } - HintFieldValues hintFieldValues; - hintFieldValues.nValues = hintField->values.size(); - hintFieldValues.values = new HintFieldInfo[hintField->values.size()]; - for(uint64_t i = 0; i < hintField->values.size(); ++i) { HintFieldValue hintFieldVal = hintField->values[i]; - HintFieldInfo hintFieldInfo; + HintFieldInfo hintFieldInfo = hintFieldValues[i]; if(print_expression) { - cout << "--------------------------------------------------------" << endl; - cout << "Hint name " << hintFieldName << " for hint id " << hintId << " is "; + std::string expression_line = getExpressionDebug(globalInfo, globalConstraintsBin, hintId, hintFieldName, hintFieldVal); + hintFieldValues[i].expression_line_size = expression_line.size(); } + if (hintFieldVal.operand == opType::tmp) { - uint64_t dim = globalConstraintsBin.expressionsInfo[hintFieldVal.id].destDim; - hintFieldInfo.size = dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; - hintFieldInfo.offset = dim; - if(print_expression && globalConstraintsBin.expressionsInfo[hintFieldVal.id].line != "") { - cout << "the expression with id: " << hintFieldVal.id << " " << globalConstraintsBin.expressionsInfo[hintFieldVal.id].line << endl; - } - - calculateGlobalExpression(hintFieldInfo.values, publicInputs, challenges, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsExpressions, globalConstraintsBin.expressionsInfo[hintFieldVal.id]); + calculateGlobalExpression(globalInfo, hintFieldInfo.values, publicInputs, challenges, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsExpressions, globalConstraintsBin.expressionsInfo[hintFieldVal.id]); } else if (hintFieldVal.operand == opType::public_) { - hintFieldInfo.size = 1; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; hintFieldInfo.values[0] = publicInputs[hintFieldVal.id]; - hintFieldInfo.fieldType = HintFieldType::Field; - hintFieldInfo.offset = 1; } else if (hintFieldVal.operand == opType::number) { - hintFieldInfo.size = 1; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; hintFieldInfo.values[0] = Goldilocks::fromU64(hintFieldVal.value); - hintFieldInfo.fieldType = HintFieldType::Field; - hintFieldInfo.offset = 1; - if(print_expression) cout << "number " << hintFieldVal.value << endl; } else if (hintFieldVal.operand == opType::airgroupvalue) { - hintFieldInfo.size = FIELD_EXTENSION; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = HintFieldType::FieldExtended; - hintFieldInfo.offset = FIELD_EXTENSION; std::memcpy(hintFieldInfo.values, &airgroupValues[hintFieldVal.dim][FIELD_EXTENSION*hintFieldVal.id], FIELD_EXTENSION * sizeof(Goldilocks::Element)); } else if (hintFieldVal.operand == opType::proofvalue) { - hintFieldInfo.size = FIELD_EXTENSION; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = HintFieldType::FieldExtended; - hintFieldInfo.offset = FIELD_EXTENSION; - std::memcpy(hintFieldInfo.values, &proofValues[FIELD_EXTENSION*hintFieldVal.id], FIELD_EXTENSION * sizeof(Goldilocks::Element)); + std::memcpy(hintFieldInfo.values, &proofValues[FIELD_EXTENSION*hintFieldVal.id], hintFieldInfo.size * sizeof(Goldilocks::Element)); } else if (hintFieldVal.operand == opType::string_) { - hintFieldInfo.values = nullptr; - hintFieldInfo.fieldType = HintFieldType::String; - hintFieldInfo.size = hintFieldVal.stringValue.size(); - hintFieldInfo.stringValue = new uint8_t[hintFieldVal.stringValue.size()]; std::memcpy(hintFieldInfo.stringValue, hintFieldVal.stringValue.data(), hintFieldVal.stringValue.size()); - hintFieldInfo.offset = 0; - if(print_expression) cout << "string " << hintFieldVal.stringValue << endl; } else { zklog.error("Unknown HintFieldType"); exitProcess(); exit(-1); } - if(print_expression) cout << "--------------------------------------------------------" << endl; - - hintFieldInfo.matrix_size = hintFieldVal.pos.size(); - hintFieldInfo.pos = new uint64_t[hintFieldInfo.matrix_size]; for(uint64_t i = 0; i < hintFieldInfo.matrix_size; ++i) { hintFieldInfo.pos[i] = hintFieldVal.pos[i]; } - hintFieldValues.values[i] = hintFieldInfo; } - return hintFieldValues; + return; } -uint64_t setHintFieldGlobalConstraint(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* proofValues, Goldilocks::Element* values, uint64_t hintId, std::string hintFieldName) { +uint64_t setHintFieldGlobalConstraint(json& globalInfo, ExpressionsBin &globalConstraintsBin, Goldilocks::Element* proofValues, Goldilocks::Element* values, uint64_t hintId, std::string hintFieldName) { Hint hint = globalConstraintsBin.hints[hintId]; auto hintField = std::find_if(hint.fields.begin(), hint.fields.end(), [hintFieldName](const HintField& hintField) { diff --git a/pil2-stark/src/starkpil/hints.hpp b/pil2-stark/src/starkpil/hints.hpp index a93bbe7a..b7e3c8c0 100644 --- a/pil2-stark/src/starkpil/hints.hpp +++ b/pil2-stark/src/starkpil/hints.hpp @@ -9,18 +9,16 @@ typedef enum { } HintFieldType; struct HintFieldInfo { - uint64_t size; // Destination size (in Goldilocks elements) + uint64_t size; + uint64_t string_size; uint8_t offset; HintFieldType fieldType; Goldilocks::Element* values; uint8_t* stringValue; uint64_t matrix_size; uint64_t* pos; -}; - -struct HintFieldValues { - uint64_t nValues; - HintFieldInfo* values; + uint8_t* expression_line; + uint64_t expression_line_size; }; struct HintFieldArgs { @@ -67,18 +65,6 @@ void setPolynomial(SetupCtx& setupCtx, Goldilocks::Element *buffer, Goldilocks:: } } -void printExpression(Goldilocks::Element* pol, uint64_t dim, uint64_t firstPrintValue = 0, uint64_t lastPrintValue = 0) { - cout << "-------------------------------------------------" << endl; - for(uint64_t i = firstPrintValue; i < lastPrintValue; ++i) { - if(dim == 3) { - cout << "Value at " << i << " is: " << " [" << Goldilocks::toString(pol[i*FIELD_EXTENSION]) << ", " << Goldilocks::toString(pol[i*FIELD_EXTENSION + 1]) << ", " << Goldilocks::toString(pol[i*FIELD_EXTENSION + 2]) << " ]" << endl; - } else { - cout << "Value at " << i << " is: " << Goldilocks::toString(pol[i]) << endl; - } - } - cout << "-------------------------------------------------" << endl; -} - void printRow(SetupCtx& setupCtx, Goldilocks::Element* buffer, uint64_t stage, uint64_t row) { Goldilocks::Element *pol = &buffer[setupCtx.starkInfo.mapOffsets[std::make_pair("cm" + to_string(stage), false)] + setupCtx.starkInfo.mapSectionsN["cm" + to_string(stage)] * row]; cout << "Values at row " << row << " = {" << endl; @@ -112,78 +98,88 @@ void printRow(SetupCtx& setupCtx, Goldilocks::Element* buffer, uint64_t stage, u cout << "}" << endl; } -void printExpressionDebug(SetupCtx& setupCtx, uint64_t hintId, std::string hintFieldName, HintFieldValue hintFieldVal) { - cout << "--------------------------------------------------------" << endl; - cout << "Hint name " << hintFieldName << " for hint id " << hintId << " is "; +std::string getExpressionDebug(SetupCtx& setupCtx, uint64_t hintId, std::string hintFieldName, HintFieldValue hintFieldVal) { + std::string debug = "Hint name " + hintFieldName + " for hint id " + to_string(hintId) + " is "; if(hintFieldVal.operand == opType::cm) { - cout << "witness col " << setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].name; + debug += "witness col " + setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].name; if(setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].lengths.size() > 0) { - cout << "["; + debug += "["; for(uint64_t i = 0; i < setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].lengths.size(); ++i) { - cout << setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].lengths[i]; - if(i != setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].lengths.size() - 1) cout << ", "; + debug += to_string(setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].lengths[i]); + if(i != setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].lengths.size() - 1) debug += ", "; } - cout << "]"; + debug += "]"; } - cout << endl; } else if(hintFieldVal.operand == opType::custom) { - cout << "custom col " << setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].name; + debug += "custom col " + setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].name; if(setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size() > 0) { - cout << "["; + debug += "["; for(uint64_t i = 0; i < setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size(); ++i) { - cout << setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths[i]; - if(i != setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size() - 1) cout << ", "; + debug += to_string(setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths[i]); + if(i != setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size() - 1) debug += ", "; } - cout << "]"; + debug += "]"; } - cout << endl; } else if(hintFieldVal.operand == opType::const_) { - cout << "fixed col" << setupCtx.starkInfo.constPolsMap[hintFieldVal.id].name; + debug += "fixed col" + setupCtx.starkInfo.constPolsMap[hintFieldVal.id].name; if(setupCtx.starkInfo.constPolsMap[hintFieldVal.id].lengths.size() > 0) { - cout << "["; + debug += "["; for(uint64_t i = 0; i < setupCtx.starkInfo.constPolsMap[hintFieldVal.id].lengths.size(); ++i) { - cout << setupCtx.starkInfo.constPolsMap[hintFieldVal.id].lengths[i]; - if(i != setupCtx.starkInfo.constPolsMap[hintFieldVal.id].lengths.size() - 1) cout << ", "; + debug += to_string(setupCtx.starkInfo.constPolsMap[hintFieldVal.id].lengths[i]); + if(i != setupCtx.starkInfo.constPolsMap[hintFieldVal.id].lengths.size() - 1) debug += ", "; } - cout << "]"; + debug += "]"; } - cout << endl; } else if (hintFieldVal.operand == opType::tmp) { - cout << "the expression with id: "; + debug += "the expression with id: "; if(setupCtx.expressionsBin.expressionsInfo[hintFieldVal.id].line != "") { - cout << " " << setupCtx.expressionsBin.expressionsInfo[hintFieldVal.id].line << endl; + debug += " " + setupCtx.expressionsBin.expressionsInfo[hintFieldVal.id].line; } - cout << endl; } else if (hintFieldVal.operand == opType::public_) { - cout << "public input " << setupCtx.starkInfo.publicsMap[hintFieldVal.id].name << endl; + debug += "public input " + setupCtx.starkInfo.publicsMap[hintFieldVal.id].name; } else if (hintFieldVal.operand == opType::proofvalue) { - cout << "proof value " << setupCtx.starkInfo.proofValuesMap[hintFieldVal.id].name << endl; + debug += "proof value " + setupCtx.starkInfo.proofValuesMap[hintFieldVal.id].name; } else if (hintFieldVal.operand == opType::number) { - cout << "number " << hintFieldVal.value << endl; + debug += "number " + to_string(hintFieldVal.value); } else if (hintFieldVal.operand == opType::airgroupvalue) { - cout << "airgroupValue " << setupCtx.starkInfo.airgroupValuesMap[hintFieldVal.id].name << endl; + debug += "airgroupValue " + setupCtx.starkInfo.airgroupValuesMap[hintFieldVal.id].name; } else if (hintFieldVal.operand == opType::airvalue) { - cout << "airValue " << setupCtx.starkInfo.airValuesMap[hintFieldVal.id].name << endl; + debug += "airValue " + setupCtx.starkInfo.airValuesMap[hintFieldVal.id].name; } else if (hintFieldVal.operand == opType::challenge) { - cout << "challenge " << setupCtx.starkInfo.challengesMap[hintFieldVal.id].name << endl; + debug += "challenge " + setupCtx.starkInfo.challengesMap[hintFieldVal.id].name; } else if (hintFieldVal.operand == opType::string_) { - cout << "string " << hintFieldVal.stringValue << endl; + debug += "string " + hintFieldVal.stringValue; } else { zklog.error("Unknown HintFieldType"); exitProcess(); exit(-1); } - cout << "--------------------------------------------------------" << endl; + return debug; +} + +uint64_t getHintFieldValues(SetupCtx& setupCtx, uint64_t hintId, std::string hintFieldName) { + Hint hint = setupCtx.expressionsBin.hints[hintId]; + + auto hintField = std::find_if(hint.fields.begin(), hint.fields.end(), [hintFieldName](const HintField& hintField) { + return hintField.name == hintFieldName; + }); + + if(hintField == hint.fields.end()) { + zklog.error("Hint field " + hintFieldName + " not found in hint " + hint.name + "."); + exitProcess(); + exit(-1); + } + return hintField->values.size(); } -HintFieldValues getHintField( +void getHintFieldSizes( SetupCtx& setupCtx, - StepsParams ¶ms, + HintFieldInfo *hintFieldValues, uint64_t hintId, - std::string hintFieldName, + std::string hintFieldName, HintFieldOptions& hintOptions ) { @@ -207,9 +203,108 @@ HintFieldValues getHintField( exit(-1); } - HintFieldValues hintFieldValues; - hintFieldValues.nValues = hintField->values.size(); - hintFieldValues.values = new HintFieldInfo[hintField->values.size()]; + for(uint64_t i = 0; i < hintField->values.size(); ++i) { + HintFieldValue hintFieldVal = hintField->values[i]; + + if(hintOptions.print_expression) { + std::string expression_line = getExpressionDebug(setupCtx, hintId, hintFieldName, hintFieldVal); + hintFieldValues[i].expression_line_size = expression_line.size(); + } + if(hintFieldVal.operand == opType::cm) { + uint64_t dim = setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].dim; + hintFieldValues[i].size = deg*dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; + hintFieldValues[i].offset = dim; + } else if(hintFieldVal.operand == opType::custom) { + uint64_t dim = setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].dim; + hintFieldValues[i].size = deg*dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; + hintFieldValues[i].offset = dim; + } else if(hintFieldVal.operand == opType::const_) { + uint64_t dim = setupCtx.starkInfo.constPolsMap[hintFieldVal.id].dim; + hintFieldValues[i].size = deg*dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; + hintFieldValues[i].offset = dim; + } else if (hintFieldVal.operand == opType::tmp) { + if(hintOptions.compilation_time) { + hintFieldValues[i].size = 1; + hintFieldValues[i].fieldType = HintFieldType::Field; + hintFieldValues[i].offset = 1; + } else { + uint64_t dim = setupCtx.expressionsBin.expressionsInfo[hintFieldVal.id].destDim; + hintFieldValues[i].size = deg*dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; + hintFieldValues[i].offset = dim; + } + } else if (hintFieldVal.operand == opType::public_) { + hintFieldValues[i].size = 1; + hintFieldValues[i].fieldType = HintFieldType::Field; + hintFieldValues[i].offset = 1; + } else if (hintFieldVal.operand == opType::number) { + hintFieldValues[i].size = 1; + hintFieldValues[i].fieldType = HintFieldType::Field; + hintFieldValues[i].offset = 1; + } else if (hintFieldVal.operand == opType::airgroupvalue) { + uint64_t dim = setupCtx.starkInfo.airgroupValuesMap[hintFieldVal.id].stage == 1 ? 1 : FIELD_EXTENSION; + hintFieldValues[i].size = dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; + hintFieldValues[i].offset = dim; + } else if (hintFieldVal.operand == opType::airvalue) { + uint64_t dim = setupCtx.starkInfo.airValuesMap[hintFieldVal.id].stage == 1 ? 1 : FIELD_EXTENSION; + hintFieldValues[i].size = dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; + hintFieldValues[i].offset = dim; + } else if (hintFieldVal.operand == opType::proofvalue) { + uint64_t dim = setupCtx.starkInfo.proofValuesMap[hintFieldVal.id].stage == 1 ? 1 : FIELD_EXTENSION; + hintFieldValues[i].size = dim; + hintFieldValues[i].fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; + hintFieldValues[i].offset = dim; + } else if (hintFieldVal.operand == opType::challenge) { + hintFieldValues[i].size = FIELD_EXTENSION; + hintFieldValues[i].fieldType = HintFieldType::FieldExtended; + hintFieldValues[i].offset = FIELD_EXTENSION; + } else if (hintFieldVal.operand == opType::string_) { + hintFieldValues[i].string_size = hintFieldVal.stringValue.size(); + hintFieldValues[i].fieldType = HintFieldType::String; + hintFieldValues[i].offset = 0; + } else { + zklog.error("Unknown HintFieldType"); + exitProcess(); + exit(-1); + } + + hintFieldValues[i].matrix_size = hintFieldVal.pos.size(); + } + + return; +} + +void getHintField( + SetupCtx& setupCtx, + StepsParams ¶ms, + HintFieldInfo *hintFieldValues, + uint64_t hintId, + std::string hintFieldName, + HintFieldOptions& hintOptions +) { + + if(setupCtx.expressionsBin.hints.size() == 0) { + zklog.error("No hints were found."); + exitProcess(); + exit(-1); + } + + Hint hint = setupCtx.expressionsBin.hints[hintId]; + + auto hintField = std::find_if(hint.fields.begin(), hint.fields.end(), [hintFieldName](const HintField& hintField) { + return hintField.name == hintFieldName; + }); + + if(hintField == hint.fields.end()) { + zklog.error("Hint field " + hintFieldName + " not found in hint " + hint.name + "."); + exitProcess(); + exit(-1); + } for(uint64_t i = 0; i < hintField->values.size(); ++i) { HintFieldValue hintFieldVal = hintField->values[i]; @@ -219,20 +314,17 @@ HintFieldValues getHintField( exit(-1); } - HintFieldInfo hintFieldInfo; + HintFieldInfo hintFieldInfo = hintFieldValues[i]; if(hintOptions.print_expression) { - printExpressionDebug(setupCtx, hintId, hintFieldName, hintFieldVal); + std::string expression_line = getExpressionDebug(setupCtx, hintId, hintFieldName, hintFieldVal); + std::memcpy(hintFieldInfo.expression_line, expression_line.data(), expression_line.size()); + hintFieldInfo.expression_line_size = expression_line.size(); } if(hintFieldVal.operand == opType::cm) { - uint64_t dim = setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].dim; - hintFieldInfo.size = deg*dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; - hintFieldInfo.offset = dim; if(!hintOptions.dest) { - Goldilocks::Element *pols = setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].stage == 1 ? params.trace : params.pols; - getPolynomial(setupCtx, pols, hintFieldInfo.values, setupCtx.starkInfo.cmPolsMap[hintFieldVal.id], hintFieldVal.rowOffsetIndex, "cm"); + Goldilocks::Element *pAddress = setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].stage == 1 ? params.trace : params.aux_trace; + getPolynomial(setupCtx, pAddress, hintFieldInfo.values, setupCtx.starkInfo.cmPolsMap[hintFieldVal.id], hintFieldVal.rowOffsetIndex, "cm"); if(hintOptions.inverse) { zklog.error("Inverse not supported still for polynomials"); exitProcess(); @@ -241,22 +333,12 @@ HintFieldValues getHintField( memset((uint8_t *)hintFieldInfo.values, 0, hintFieldInfo.size * sizeof(Goldilocks::Element)); } } else if(hintFieldVal.operand == opType::custom) { - uint64_t dim = setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].dim; - hintFieldInfo.size = deg*dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; - hintFieldInfo.offset = dim; - getPolynomial(setupCtx, params.customCommits[hintFieldVal.commitId], hintFieldInfo.values, setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id], hintFieldVal.rowOffsetIndex, "custom"); + getPolynomial(setupCtx, params.pCustomCommits[hintFieldVal.commitId], hintFieldInfo.values, setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id], hintFieldVal.rowOffsetIndex, "custom"); if(hintOptions.inverse) { zklog.error("Inverse not supported still for polynomials"); exitProcess(); } } else if(hintFieldVal.operand == opType::const_) { - uint64_t dim = setupCtx.starkInfo.constPolsMap[hintFieldVal.id].dim; - hintFieldInfo.size = deg*dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; - hintFieldInfo.offset = dim; getPolynomial(setupCtx, params.pConstPolsAddress, hintFieldInfo.values, setupCtx.starkInfo.constPolsMap[hintFieldVal.id], hintFieldVal.rowOffsetIndex, "const"); if(hintOptions.inverse) { zklog.error("Inverse not supported still for polynomials"); @@ -264,18 +346,9 @@ HintFieldValues getHintField( } } else if (hintFieldVal.operand == opType::tmp) { if(hintOptions.compilation_time) { - hintFieldInfo.size = 1; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = HintFieldType::Field; - hintFieldInfo.offset = 1; ExpressionsPack expressionsCtx(setupCtx, 1); expressionsCtx.calculateExpression(params, hintFieldInfo.values, hintFieldVal.id, hintOptions.inverse, true); } else { - uint64_t dim = setupCtx.expressionsBin.expressionsInfo[hintFieldVal.id].destDim; - hintFieldInfo.size = deg*dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended; - hintFieldInfo.offset = dim; #ifdef __AVX512__ ExpressionsAvx512 expressionsCtx(setupCtx); #elif defined(__AVX2__) @@ -286,23 +359,10 @@ HintFieldValues getHintField( expressionsCtx.calculateExpression(params, hintFieldInfo.values, hintFieldVal.id, hintOptions.inverse, false); } } else if (hintFieldVal.operand == opType::public_) { - hintFieldInfo.size = 1; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; hintFieldInfo.values[0] = hintOptions.inverse ? Goldilocks::inv(params.publicInputs[hintFieldVal.id]) : params.publicInputs[hintFieldVal.id]; - hintFieldInfo.fieldType = HintFieldType::Field; - hintFieldInfo.offset = 1; } else if (hintFieldVal.operand == opType::number) { - hintFieldInfo.size = 1; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; hintFieldInfo.values[0] = hintOptions.inverse ? Goldilocks::inv(Goldilocks::fromU64(hintFieldVal.value)) : Goldilocks::fromU64(hintFieldVal.value); - hintFieldInfo.fieldType = HintFieldType::Field; - hintFieldInfo.offset = 1; } else if (hintFieldVal.operand == opType::airgroupvalue) { - uint64_t dim = setupCtx.starkInfo.airgroupValuesMap[hintFieldVal.id].stage == 1 ? 1 : FIELD_EXTENSION; - hintFieldInfo.size = dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; - hintFieldInfo.offset = FIELD_EXTENSION; if(!hintOptions.dest) { if(hintOptions.inverse) { Goldilocks3::inv((Goldilocks3::Element *)hintFieldInfo.values, (Goldilocks3::Element *)¶ms.airgroupValues[FIELD_EXTENSION*hintFieldVal.id]); @@ -311,11 +371,6 @@ HintFieldValues getHintField( } } } else if (hintFieldVal.operand == opType::airvalue) { - uint64_t dim = setupCtx.starkInfo.airValuesMap[hintFieldVal.id].stage == 1 ? 1 : FIELD_EXTENSION; - hintFieldInfo.size = dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; - hintFieldInfo.offset = FIELD_EXTENSION; if(!hintOptions.dest) { if(hintOptions.inverse) { Goldilocks3::inv((Goldilocks3::Element *)hintFieldInfo.values, (Goldilocks3::Element *)¶ms.airValues[FIELD_EXTENSION*hintFieldVal.id]); @@ -324,11 +379,6 @@ HintFieldValues getHintField( } } } else if (hintFieldVal.operand == opType::proofvalue) { - uint64_t dim = 1; - hintFieldInfo.size = dim; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Field : HintFieldType::FieldExtended; - hintFieldInfo.offset = FIELD_EXTENSION; if(!hintOptions.dest) { if(hintOptions.inverse) { Goldilocks3::inv((Goldilocks3::Element *)hintFieldInfo.values, (Goldilocks3::Element *)¶ms.proofValues[FIELD_EXTENSION*hintFieldVal.id]); @@ -337,37 +387,25 @@ HintFieldValues getHintField( } } } else if (hintFieldVal.operand == opType::challenge) { - hintFieldInfo.size = FIELD_EXTENSION; - hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size]; - hintFieldInfo.fieldType = HintFieldType::FieldExtended; - hintFieldInfo.offset = FIELD_EXTENSION; if(hintOptions.inverse) { Goldilocks3::inv((Goldilocks3::Element *)hintFieldInfo.values, (Goldilocks3::Element *)¶ms.challenges[FIELD_EXTENSION*hintFieldVal.id]); } else { std::memcpy(hintFieldInfo.values, ¶ms.challenges[FIELD_EXTENSION*hintFieldVal.id], hintFieldInfo.size * sizeof(Goldilocks::Element)); } } else if (hintFieldVal.operand == opType::string_) { - hintFieldInfo.values = nullptr; - hintFieldInfo.fieldType = HintFieldType::String; - hintFieldInfo.size = hintFieldVal.stringValue.size(); - hintFieldInfo.stringValue = new uint8_t[hintFieldVal.stringValue.size()]; - std::memcpy(hintFieldInfo.stringValue, hintFieldVal.stringValue.data(), hintFieldVal.stringValue.size()); - hintFieldInfo.offset = 0; + std::memcpy(hintFieldInfo.stringValue, hintFieldVal.stringValue.data(), hintFieldVal.stringValue.size()); } else { zklog.error("Unknown HintFieldType"); exitProcess(); exit(-1); } - hintFieldInfo.matrix_size = hintFieldVal.pos.size(); - hintFieldInfo.pos = new uint64_t[hintFieldInfo.matrix_size]; for(uint64_t i = 0; i < hintFieldInfo.matrix_size; ++i) { hintFieldInfo.pos[i] = hintFieldVal.pos[i]; } - hintFieldValues.values[i] = hintFieldInfo; } - return hintFieldValues; + return; } uint64_t setHintField(SetupCtx& setupCtx, StepsParams& params, Goldilocks::Element* values, uint64_t hintId, std::string hintFieldName) { @@ -391,8 +429,8 @@ uint64_t setHintField(SetupCtx& setupCtx, StepsParams& params, Goldilocks::Eleme auto hintFieldVal = hintField->values[0]; if(hintFieldVal.operand == opType::cm) { - Goldilocks::Element *pols = setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].stage > 1 ? params.pols : params.trace; - setPolynomial(setupCtx, pols, values, hintFieldVal.id); + Goldilocks::Element *pAddress = setupCtx.starkInfo.cmPolsMap[hintFieldVal.id].stage > 1 ? params.aux_trace : params.trace; + setPolynomial(setupCtx, pAddress, values, hintFieldVal.id); } else if(hintFieldVal.operand == opType::airgroupvalue) { if(setupCtx.starkInfo.airgroupValuesMap[hintFieldVal.id].stage > 1) { std::memcpy(¶ms.airgroupValues[FIELD_EXTENSION*hintFieldVal.id], values, FIELD_EXTENSION * sizeof(Goldilocks::Element)); @@ -429,7 +467,7 @@ void addHintField(SetupCtx& setupCtx, StepsParams& params, uint64_t hintId, Dest } if(hintFieldOptions.print_expression) { - printExpressionDebug(setupCtx, hintId, hintFieldName, hintFieldVal); + std::string expression_line = getExpressionDebug(setupCtx, hintId, hintFieldName, hintFieldVal); } if(hintFieldVal.operand == opType::cm) { destStruct.addCmPol(setupCtx.starkInfo.cmPolsMap[hintFieldVal.id], hintFieldVal.rowOffsetIndex, hintFieldOptions.inverse); @@ -474,7 +512,7 @@ uint64_t multiplyHintFields(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hi HintFieldValue hintFieldDestVal = hintFieldDest->values[0]; uint64_t offset = setupCtx.starkInfo.mapSectionsN["cm" + to_string(setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].stage)]; - Goldilocks::Element *buff = ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("cm" + to_string(setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].stage), false)] + setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].stagePos]; + Goldilocks::Element *buff = ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("cm" + to_string(setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].stage), false)] + setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].stagePos]; Dest destStruct(buff, offset); @@ -488,22 +526,28 @@ uint64_t multiplyHintFields(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hi return hintFieldDestVal.id; } -VecU64Result accHintField(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hintId, std::string hintFieldNameDest, std::string hintFieldNameAirgroupVal, std::string hintFieldName, bool add) { +void accHintField(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hintId, std::string hintFieldNameDest, std::string hintFieldNameAirgroupVal, std::string hintFieldName, bool add) { + uint64_t N = 1 << setupCtx.starkInfo.starkStruct.nBits; + Hint hint = setupCtx.expressionsBin.hints[hintId]; auto hintFieldDest = std::find_if(hint.fields.begin(), hint.fields.end(), [hintFieldNameDest](const HintField& hintField) { return hintField.name == hintFieldNameDest; }); + + HintFieldOptions hintOptions; HintFieldValue hintFieldDestVal = hintFieldDest->values[0]; + + uint64_t dim = setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].dim; + Goldilocks::Element *vals = new Goldilocks::Element[dim * N]; - HintFieldOptions hintOptions; - HintFieldValues hintValues = getHintField(setupCtx, params, hintId, hintFieldName, hintOptions); + Dest destStruct(vals, 0); + addHintField(setupCtx, params, hintId, destStruct, hintFieldName, hintOptions); - Goldilocks::Element *vals = &hintValues.values[0].values[0]; + std::vector dests = {destStruct}; - uint64_t dim = setupCtx.starkInfo.cmPolsMap[hintFieldDestVal.id].dim; + opHintFields(setupCtx, params, dests); - uint64_t N = 1 << setupCtx.starkInfo.starkStruct.nBits; for(uint64_t i = 1; i < N; ++i) { if(add) { if(dim == 1) { @@ -520,18 +564,22 @@ VecU64Result accHintField(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hint } } - VecU64Result hintIds; - hintIds.nElements = 2; - hintIds.ids = new uint64_t[hintIds.nElements]; - hintIds.ids[0] = setHintField(setupCtx, params, vals, hintId, hintFieldNameDest); - hintIds.ids[1] = setHintField(setupCtx, params, &vals[(N - 1)*FIELD_EXTENSION], hintId, hintFieldNameAirgroupVal); + setHintField(setupCtx, params, vals, hintId, hintFieldNameDest); + setHintField(setupCtx, params, &vals[(N - 1)*FIELD_EXTENSION], hintId, hintFieldNameAirgroupVal); - delete[] hintValues.values; + delete[] vals; +} + +uint64_t getHintId(SetupCtx& setupCtx, uint64_t hintId, std::string name) { + Hint hint = setupCtx.expressionsBin.hints[hintId]; - return hintIds; + auto hintField = std::find_if(hint.fields.begin(), hint.fields.end(), [name](const HintField& hintField) { + return hintField.name == name; + }); + return hintField->values[0].id; } -VecU64Result accMulHintFields(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hintId, std::string hintFieldNameDest, std::string hintFieldNameAirgroupVal, std::string hintFieldName1, std::string hintFieldName2, HintFieldOptions &hintOptions1, HintFieldOptions &hintOptions2, bool add) { +void accMulHintFields(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hintId, std::string hintFieldNameDest, std::string hintFieldNameAirgroupVal, std::string hintFieldName1, std::string hintFieldName2, HintFieldOptions &hintOptions1, HintFieldOptions &hintOptions2, bool add) { uint64_t N = 1 << setupCtx.starkInfo.starkStruct.nBits; Hint hint = setupCtx.expressionsBin.hints[hintId]; @@ -568,18 +616,13 @@ VecU64Result accMulHintFields(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t } } - VecU64Result hintIds; - hintIds.nElements = 2; - hintIds.ids = new uint64_t[hintIds.nElements]; - hintIds.ids[0] = setHintField(setupCtx, params, vals, hintId, hintFieldNameDest); - hintIds.ids[1] = setHintField(setupCtx, params, &vals[(N - 1)*FIELD_EXTENSION], hintId, hintFieldNameAirgroupVal); - + setHintField(setupCtx, params, vals, hintId, hintFieldNameDest); + setHintField(setupCtx, params, &vals[(N - 1)*FIELD_EXTENSION], hintId, hintFieldNameAirgroupVal); + delete[] vals; - - return hintIds; } -VecU64Result updateAirgroupValue(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hintId, std::string hintFieldNameAirgroupVal, std::string hintFieldName1, std::string hintFieldName2, HintFieldOptions &hintOptions1, HintFieldOptions &hintOptions2, bool add) { +uint64_t updateAirgroupValue(SetupCtx& setupCtx, StepsParams ¶ms, uint64_t hintId, std::string hintFieldNameAirgroupVal, std::string hintFieldName1, std::string hintFieldName2, HintFieldOptions &hintOptions1, HintFieldOptions &hintOptions2, bool add) { Hint hint = setupCtx.expressionsBin.hints[hintId]; @@ -588,7 +631,7 @@ VecU64Result updateAirgroupValue(SetupCtx& setupCtx, StepsParams ¶ms, uint64 }); HintFieldValue hintFieldAirgroupVal = hintFieldAirgroup->values[0]; - Goldilocks::Element *vals = new Goldilocks::Element[3]; + Goldilocks::Element vals[3]; Dest destStruct(vals, 0); addHintField(setupCtx, params, hintId, destStruct, hintFieldName1, hintOptions1); @@ -614,12 +657,5 @@ VecU64Result updateAirgroupValue(SetupCtx& setupCtx, StepsParams ¶ms, uint64 } } - VecU64Result hintIds; - hintIds.nElements = 1; - hintIds.ids = new uint64_t[1]; - hintIds.ids[0] = hintFieldAirgroupVal.id; - - delete[] vals; - - return hintIds; + return hintFieldAirgroupVal.id; } diff --git a/pil2-stark/src/starkpil/proof2zkinStark.cpp b/pil2-stark/src/starkpil/proof2zkinStark.cpp index 54c641df..388ad979 100644 --- a/pil2-stark/src/starkpil/proof2zkinStark.cpp +++ b/pil2-stark/src/starkpil/proof2zkinStark.cpp @@ -387,13 +387,20 @@ json joinzkinfinal(json& globalInfo, Goldilocks::Element* publics, Goldilocks::E } } - if(globalInfo["numProofValues"] > 0) { + if(globalInfo["proofValuesMap"].size() > 0) { + uint64_t p = 0; zkinFinal["proofValues"] = json::array(); - for (uint64_t i = 0; i < globalInfo["numProofValues"]; i++) + for (uint64_t i = 0; i < globalInfo["proofValuesMap"].size(); i++) { zkinFinal["proofValues"][i] = json::array(); - for(uint64_t j = 0; j < FIELD_EXTENSION; ++j) { - zkinFinal["proofValues"][i][j] = Goldilocks::toString(proofValues[i*FIELD_EXTENSION + j]); + if(globalInfo["proofValuesMap"][i]["stage"] == 1) { + zkinFinal["proofValues"][i][0] = Goldilocks::toString(proofValues[p++]); + zkinFinal["proofValues"][i][1] = "0"; + zkinFinal["proofValues"][i][2] = "0"; + } else { + zkinFinal["proofValues"][i][0] = Goldilocks::toString(proofValues[p++]); + zkinFinal["proofValues"][i][1] = Goldilocks::toString(proofValues[p++]); + zkinFinal["proofValues"][i][2] = Goldilocks::toString(proofValues[p++]); } } } diff --git a/pil2-stark/src/starkpil/proof_stark.hpp b/pil2-stark/src/starkpil/proof_stark.hpp index 73dd1b46..5ac3da07 100644 --- a/pil2-stark/src/starkpil/proof_stark.hpp +++ b/pil2-stark/src/starkpil/proof_stark.hpp @@ -198,6 +198,7 @@ template class Proofs { public: + StarkInfo &starkInfo; uint64_t nStages; uint64_t nCustomCommits; uint64_t nFieldElements; @@ -209,19 +210,20 @@ class Proofs std::vector> airgroupValues; std::vector> airValues; std::vector customCommits; - Proofs(StarkInfo &starkInfo) : - fri(starkInfo), - evals(starkInfo.evMap.size(), std::vector(FIELD_EXTENSION, Goldilocks::zero())), - airgroupValues(starkInfo.airgroupValuesMap.size(), std::vector(FIELD_EXTENSION, Goldilocks::zero())), - airValues(starkInfo.airValuesMap.size(), std::vector(FIELD_EXTENSION, Goldilocks::zero())), - customCommits(starkInfo.customCommits.size()) + Proofs(StarkInfo &starkInfo_) : + starkInfo(starkInfo_), + fri(starkInfo_), + evals(starkInfo_.evMap.size(), std::vector(FIELD_EXTENSION, Goldilocks::zero())), + airgroupValues(starkInfo_.airgroupValuesMap.size(), std::vector(FIELD_EXTENSION, Goldilocks::zero())), + airValues(starkInfo_.airValuesMap.size(), std::vector(FIELD_EXTENSION, Goldilocks::zero())), + customCommits(starkInfo_.customCommits.size()) { - nStages = starkInfo.nStages + 1; - nCustomCommits = starkInfo.customCommits.size(); + nStages = starkInfo_.nStages + 1; + nCustomCommits = starkInfo_.customCommits.size(); roots = new ElementType*[nStages + nCustomCommits]; - nFieldElements = starkInfo.starkStruct.verificationHashType == "GL" ? HASH_SIZE : 1; - airId = starkInfo.airId; - airgroupId = starkInfo.airgroupId; + nFieldElements = starkInfo_.starkStruct.verificationHashType == "GL" ? HASH_SIZE : 1; + airId = starkInfo_.airId; + airgroupId = starkInfo_.airgroupId; for(uint64_t i = 0; i < nStages + nCustomCommits; i++) { roots[i] = new ElementType[nFieldElements]; @@ -247,16 +249,32 @@ class Proofs } void setAirgroupValues(Goldilocks::Element *_airgroupValues) { - for (uint64_t i = 0; i < airgroupValues.size(); i++) + uint64_t p = 0; + for (uint64_t i = 0; i < starkInfo.airgroupValuesMap.size(); i++) { - std::memcpy(&airgroupValues[i][0], &_airgroupValues[i * FIELD_EXTENSION], FIELD_EXTENSION * sizeof(Goldilocks::Element)); + if(starkInfo.airgroupValuesMap[i].stage == 1) { + airgroupValues[i][0] = _airgroupValues[p++]; + airgroupValues[i][1] = Goldilocks::zero(); + airgroupValues[i][2] = Goldilocks::zero(); + } else { + std::memcpy(&airgroupValues[i][0], &_airgroupValues[p], FIELD_EXTENSION * sizeof(Goldilocks::Element)); + p += 3; + } } } void setAirValues(Goldilocks::Element *_airValues) { - for (uint64_t i = 0; i < airValues.size(); i++) + uint64_t p = 0; + for (uint64_t i = 0; i < starkInfo.airValuesMap.size(); i++) { - std::memcpy(&airValues[i][0], &_airValues[i * FIELD_EXTENSION], FIELD_EXTENSION * sizeof(Goldilocks::Element)); + if(starkInfo.airValuesMap[i].stage == 1) { + airValues[i][0] = _airValues[p++]; + airValues[i][1] = Goldilocks::zero(); + airValues[i][2] = Goldilocks::zero(); + } else { + std::memcpy(&airValues[i][0], &_airValues[p], FIELD_EXTENSION * sizeof(Goldilocks::Element)); + p += 3; + } } } diff --git a/pil2-stark/src/starkpil/stark_info.cpp b/pil2-stark/src/starkpil/stark_info.cpp index 2712015d..c2e9d756 100644 --- a/pil2-stark/src/starkpil/stark_info.cpp +++ b/pil2-stark/src/starkpil/stark_info.cpp @@ -126,6 +126,7 @@ void StarkInfo::load(json j) { PolMap map; map.name = j["proofValuesMap"][i]["name"]; + map.stage = j["proofValuesMap"][i]["stage"]; proofValuesMap.push_back(map); } @@ -223,8 +224,7 @@ void StarkInfo::load(json j) mapSectionsN[it.key()] = it.value(); } - setMapOffsets(); - + setMapOffsets(); } void StarkInfo::setMapOffsets() { @@ -239,13 +239,12 @@ void StarkInfo::setMapOffsets() { // Set offsets for custom commits for(uint64_t i = 0; i < customCommits.size(); ++i) { mapOffsets[std::make_pair(customCommits[i].name + "0", false)] = 0; - mapOffsets[std::make_pair(customCommits[i].name + "0", true)] = N * mapSectionsN[customCommits[i].name + "0"]; - mapTotalNcustomCommits[customCommits[i].name] = (N + NExtended) * mapSectionsN[customCommits[i].name + "0"]; + mapOffsets[std::make_pair(customCommits[i].name + "0", true)] = 0; } mapTotalN = 0; - for(uint64_t stage = 2; stage <= nStages; stage++) { + for(uint64_t stage = nStages; stage >= 2; stage--) { mapOffsets[std::make_pair("cm" + to_string(stage), false)] = mapTotalN; mapTotalN += N * mapSectionsN["cm" + to_string(stage)]; } @@ -260,26 +259,22 @@ void StarkInfo::setMapOffsets() { mapTotalN += NExtended * mapSectionsN["cm" + to_string(stage)]; } - mapOffsets[std::make_pair("q", true)] = mapTotalN; - mapTotalN += NExtended * qDim; - - // Stage FRIPolynomial - uint64_t offsetPolsFRI = mapOffsets[std::make_pair("q", true)]; - mapOffsets[std::make_pair("xDivXSubXi", true)] = offsetPolsFRI; - offsetPolsFRI += openingPoints.size() * NExtended * FIELD_EXTENSION; - - mapOffsets[std::make_pair("f", true)] = offsetPolsFRI; - offsetPolsFRI += NExtended * FIELD_EXTENSION; - - if(offsetPolsFRI > mapTotalN) mapTotalN = offsetPolsFRI; - - uint64_t offsetPolsEvals = mapOffsets[std::make_pair("q", true)]; - mapOffsets[std::make_pair("LEv", true)] = offsetPolsEvals; - offsetPolsEvals += N * openingPoints.size() * FIELD_EXTENSION; - - mapOffsets[std::make_pair("evals", true)] = offsetPolsEvals; - offsetPolsEvals += evMap.size() * omp_get_max_threads() * FIELD_EXTENSION; - if(offsetPolsEvals > mapTotalN) mapTotalN = offsetPolsEvals; + mapOffsets[std::make_pair("f", true)] = mapTotalN; + mapTotalN += NExtended * FIELD_EXTENSION; + + mapOffsets[std::make_pair("q", true)] = mapOffsets[std::make_pair("f", true)]; + + mapOffsets[std::make_pair("evals", true)] = mapTotalN; + mapTotalN += evMap.size() * omp_get_max_threads() * FIELD_EXTENSION; +} + +void StarkInfo::addMemoryRecursive() { + uint64_t NExtended = (1 << starkStruct.nBitsExt); + uint64_t offsetFRI = mapOffsets[std::make_pair("f", true)]; + mapOffsets[std::make_pair("xDivXSubXi", true)] = offsetFRI; + mapOffsets[std::make_pair("LEv", true)] = offsetFRI; + offsetFRI += openingPoints.size() * NExtended * FIELD_EXTENSION; + if(offsetFRI > mapTotalN) mapTotalN = offsetFRI; } void StarkInfo::getPolynomial(Polinomial &pol, Goldilocks::Element *pAddress, string type, PolMap& polInfo, bool domainExtended) { diff --git a/pil2-stark/src/starkpil/stark_info.hpp b/pil2-stark/src/starkpil/stark_info.hpp index a00267e5..52f8c9d7 100644 --- a/pil2-stark/src/starkpil/stark_info.hpp +++ b/pil2-stark/src/starkpil/stark_info.hpp @@ -155,8 +155,6 @@ class StarkInfo std::map, uint64_t> mapOffsets; uint64_t mapTotalN; - - std::map mapTotalNcustomCommits; /* Constructor */ StarkInfo(string file); @@ -166,6 +164,8 @@ class StarkInfo void setMapOffsets(); + void addMemoryRecursive(); + /* Returns a polynomial specified by its ID */ void getPolynomial(Polinomial &pol, Goldilocks::Element *pAddress, string type, PolMap& polInfo, bool domainExtended); }; diff --git a/pil2-stark/src/starkpil/starks.cpp b/pil2-stark/src/starkpil/starks.cpp index 9ab164e0..cef93362 100644 --- a/pil2-stark/src/starkpil/starks.cpp +++ b/pil2-stark/src/starkpil/starks.cpp @@ -4,15 +4,15 @@ #include "exit_process.hpp" template -void Starks::extendAndMerkelizeCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, FRIProof &proof, Goldilocks::Element *pBuffHelper, string bufferFile) +void Starks::extendAndMerkelizeCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, Goldilocks::Element *bufferExt, FRIProof &proof, Goldilocks::Element *pBuffHelper, string bufferFile) { uint64_t N = 1 << setupCtx.starkInfo.starkStruct.nBits; uint64_t NExtended = 1 << setupCtx.starkInfo.starkStruct.nBitsExt; std::string section = setupCtx.starkInfo.customCommits[commitId].name + to_string(step); uint64_t nCols = setupCtx.starkInfo.mapSectionsN[section]; - Goldilocks::Element *pBuff = &buffer[setupCtx.starkInfo.mapOffsets[make_pair(section, false)]]; - Goldilocks::Element *pBuffExtended = &buffer[setupCtx.starkInfo.mapOffsets[make_pair(section, true)]]; + Goldilocks::Element *pBuff = buffer; + Goldilocks::Element *pBuffExtended = bufferExt; NTT_Goldilocks ntt(N); if(pBuffHelper != nullptr) { @@ -36,15 +36,15 @@ void Starks::extendAndMerkelizeCustomCommit(uint64_t commitId, uint } template -void Starks::loadCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, FRIProof &proof, string bufferFile) +void Starks::loadCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, Goldilocks::Element *bufferExt, FRIProof &proof, string bufferFile) { uint64_t N = 1 << setupCtx.starkInfo.starkStruct.nBits; uint64_t NExtended = 1 << setupCtx.starkInfo.starkStruct.nBitsExt; std::string section = setupCtx.starkInfo.customCommits[commitId].name + to_string(step); uint64_t nCols = setupCtx.starkInfo.mapSectionsN[section]; - Goldilocks::Element *pBuff = &buffer[setupCtx.starkInfo.mapOffsets[make_pair(section, false)]]; - Goldilocks::Element *pBuffExtended = &buffer[setupCtx.starkInfo.mapOffsets[make_pair(section, true)]]; + Goldilocks::Element *pBuff = buffer; + Goldilocks::Element *pBuffExtended = bufferExt; uint64_t pos = setupCtx.starkInfo.nStages + 2 + commitId; @@ -59,7 +59,7 @@ void Starks::loadCustomCommit(uint64_t commitId, uint64_t step, Gol } template -void Starks::extendAndMerkelize(uint64_t step, Goldilocks::Element *trace, Goldilocks::Element *buffer, FRIProof &proof, Goldilocks::Element *pBuffHelper) +void Starks::extendAndMerkelize(uint64_t step, Goldilocks::Element *trace, Goldilocks::Element *aux_trace, FRIProof &proof, Goldilocks::Element *pBuffHelper) { uint64_t N = 1 << setupCtx.starkInfo.starkStruct.nBits; uint64_t NExtended = 1 << setupCtx.starkInfo.starkStruct.nBitsExt; @@ -67,8 +67,8 @@ void Starks::extendAndMerkelize(uint64_t step, Goldilocks::Element std::string section = "cm" + to_string(step); uint64_t nCols = setupCtx.starkInfo.mapSectionsN["cm" + to_string(step)]; - Goldilocks::Element *pBuff = step == 1 ? trace : &buffer[setupCtx.starkInfo.mapOffsets[make_pair(section, false)]]; - Goldilocks::Element *pBuffExtended = &buffer[setupCtx.starkInfo.mapOffsets[make_pair(section, true)]]; + Goldilocks::Element *pBuff = step == 1 ? trace : &aux_trace[setupCtx.starkInfo.mapOffsets[make_pair(section, false)]]; + Goldilocks::Element *pBuffExtended = &aux_trace[setupCtx.starkInfo.mapOffsets[make_pair(section, true)]]; NTT_Goldilocks ntt(N); if(pBuffHelper != nullptr) { @@ -83,16 +83,16 @@ void Starks::extendAndMerkelize(uint64_t step, Goldilocks::Element } template -void Starks::commitStage(uint64_t step, Goldilocks::Element *trace, Goldilocks::Element *buffer, FRIProof &proof, Goldilocks::Element* pBuffHelper) +void Starks::commitStage(uint64_t step, Goldilocks::Element *trace, Goldilocks::Element *aux_trace, FRIProof &proof, Goldilocks::Element* pBuffHelper) { if (step <= setupCtx.starkInfo.nStages) { - extendAndMerkelize(step, trace, buffer, proof, pBuffHelper); + extendAndMerkelize(step, trace, aux_trace, proof, pBuffHelper); } else { - computeQ(step, buffer, proof, pBuffHelper); + computeQ(step, aux_trace, proof, pBuffHelper); } } @@ -244,7 +244,7 @@ void Starks::evmap(StepsParams& params, Goldilocks::Element *LEv) int num_threads = omp_get_max_threads(); int size_thread = size_eval * FIELD_EXTENSION; - Goldilocks::Element *evals_acc = ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("evals", true)]]; + Goldilocks::Element *evals_acc = ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("evals", true)]]; memset(&evals_acc[0], 0, num_threads * size_thread * sizeof(Goldilocks::Element)); Polinomial *ordPols = new Polinomial[size_eval]; @@ -253,9 +253,9 @@ void Starks::evmap(StepsParams& params, Goldilocks::Element *LEv) { EvMap ev = setupCtx.starkInfo.evMap[i]; string type = ev.type == EvMap::eType::cm ? "cm" : ev.type == EvMap::eType::custom ? "custom" : "fixed"; - Goldilocks::Element *pols = type == "cm" ? params.pols : type == "custom" ? params.customCommits[ev.commitId] : ¶ms.pConstPolsExtendedTreeAddress[2]; + Goldilocks::Element *pAddress = type == "cm" ? params.aux_trace : type == "custom" ? params.pCustomCommitsExtended[ev.commitId] : ¶ms.pConstPolsExtendedTreeAddress[2]; PolMap polInfo = type == "cm" ? setupCtx.starkInfo.cmPolsMap[ev.id] : type == "custom" ? setupCtx.starkInfo.customCommitsMap[ev.commitId][ev.id] : setupCtx.starkInfo.constPolsMap[ev.id]; - setupCtx.starkInfo.getPolynomial(ordPols[i], pols, type, polInfo, true); + setupCtx.starkInfo.getPolynomial(ordPols[i], pAddress, type, polInfo, true); } #pragma omp parallel @@ -341,8 +341,8 @@ void Starks::calculateImPolsExpressions(uint64_t step, StepsParams std::vector dests; for(uint64_t i = 0; i < setupCtx.starkInfo.cmPolsMap.size(); i++) { if(setupCtx.starkInfo.cmPolsMap[i].imPol && setupCtx.starkInfo.cmPolsMap[i].stage == step) { - Goldilocks::Element* pols = setupCtx.starkInfo.cmPolsMap[i].stage == 1 ? params.trace : params.pols; - Dest destStruct(&pols[setupCtx.starkInfo.mapOffsets[std::make_pair("cm" + to_string(step), false)] + setupCtx.starkInfo.cmPolsMap[i].stagePos], setupCtx.starkInfo.mapSectionsN["cm" + to_string(step)]); + Goldilocks::Element* pAddress = setupCtx.starkInfo.cmPolsMap[i].stage == 1 ? params.trace : params.aux_trace; + Dest destStruct(&pAddress[setupCtx.starkInfo.mapOffsets[std::make_pair("cm" + to_string(step), false)] + setupCtx.starkInfo.cmPolsMap[i].stagePos], setupCtx.starkInfo.mapSectionsN["cm" + to_string(step)]); destStruct.addParams(setupCtx.expressionsBin.expressionsInfo[setupCtx.starkInfo.cmPolsMap[i].expId], false); dests.push_back(destStruct); @@ -371,7 +371,7 @@ void Starks::calculateQuotientPolynomial(StepsParams ¶ms) { #else ExpressionsPack expressionsCtx(setupCtx); #endif - expressionsCtx.calculateExpression(params, ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("q", true)]], setupCtx.starkInfo.cExpId); + expressionsCtx.calculateExpression(params, ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("q", true)]], setupCtx.starkInfo.cExpId); } template @@ -383,5 +383,5 @@ void Starks::calculateFRIPolynomial(StepsParams ¶ms) { #else ExpressionsPack expressionsCtx(setupCtx); #endif - expressionsCtx.calculateExpression(params, ¶ms.pols[setupCtx.starkInfo.mapOffsets[std::make_pair("f", true)]], setupCtx.starkInfo.friExpId); + expressionsCtx.calculateExpression(params, ¶ms.aux_trace[setupCtx.starkInfo.mapOffsets[std::make_pair("f", true)]], setupCtx.starkInfo.friExpId); } \ No newline at end of file diff --git a/pil2-stark/src/starkpil/starks.hpp b/pil2-stark/src/starkpil/starks.hpp index 4bd8ea0a..0f44e3aa 100644 --- a/pil2-stark/src/starkpil/starks.hpp +++ b/pil2-stark/src/starkpil/starks.hpp @@ -73,8 +73,8 @@ class Starks delete[] treesFRI; }; - void extendAndMerkelizeCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, FRIProof &proof, Goldilocks::Element *pBuffHelper, string bufferFile); - void loadCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, FRIProof &proof, string bufferFile); + void extendAndMerkelizeCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, Goldilocks::Element *bufferExt, FRIProof &proof, Goldilocks::Element *pBuffHelper, string bufferFile); + void loadCustomCommit(uint64_t commitId, uint64_t step, Goldilocks::Element *buffer, Goldilocks::Element *bufferExt, FRIProof &proof, string bufferFile); void extendAndMerkelize(uint64_t step, Goldilocks::Element *trace, Goldilocks::Element *buffer, FRIProof &proof, Goldilocks::Element* pBuffHelper = nullptr); void commitStage(uint64_t step, Goldilocks::Element *trace, Goldilocks::Element *buffer, FRIProof &proof, Goldilocks::Element* pBuffHelper = nullptr); diff --git a/pil2-stark/src/starkpil/steps.hpp b/pil2-stark/src/starkpil/steps.hpp index 01337609..bcda4310 100644 --- a/pil2-stark/src/starkpil/steps.hpp +++ b/pil2-stark/src/starkpil/steps.hpp @@ -6,7 +6,7 @@ struct StepsParams { Goldilocks::Element *trace; - Goldilocks::Element *pols; + Goldilocks::Element *aux_trace; Goldilocks::Element *publicInputs; Goldilocks::Element *proofValues; Goldilocks::Element *challenges; @@ -16,7 +16,8 @@ struct StepsParams Goldilocks::Element *xDivXSub; Goldilocks::Element *pConstPolsAddress; Goldilocks::Element *pConstPolsExtendedTreeAddress; - Goldilocks::Element *customCommits[10]; + Goldilocks::Element *pCustomCommits[10]; + Goldilocks::Element *pCustomCommitsExtended[10]; }; #endif \ No newline at end of file diff --git a/pil2-stark/src/starkpil/verify_constraints.hpp b/pil2-stark/src/starkpil/verify_constraints.hpp index 78a6e84e..69fae1fd 100644 --- a/pil2-stark/src/starkpil/verify_constraints.hpp +++ b/pil2-stark/src/starkpil/verify_constraints.hpp @@ -10,16 +10,11 @@ struct ConstraintInfo { uint64_t id; uint64_t stage; bool imPol; - const char* line; uint64_t nrows; + bool skip; ConstraintRowInfo rows[10]; }; -struct ConstraintsResults { - uint64_t nConstraints; - ConstraintInfo* constraintInfo; -}; - std::tuple checkConstraint(Goldilocks::Element* dest, ParserParams& parserParams, uint64_t row) { bool isValid = true; ConstraintRowInfo rowInfo; @@ -51,12 +46,10 @@ std::tuple checkConstraint(Goldilocks::Element* dest, P } -ConstraintInfo verifyConstraint(SetupCtx& setupCtx, Goldilocks::Element* dest, uint64_t constraintId) { - ConstraintInfo constraintInfo; +void verifyConstraint(SetupCtx& setupCtx, Goldilocks::Element* dest, uint64_t constraintId, ConstraintInfo& constraintInfo) { constraintInfo.id = constraintId; constraintInfo.stage = setupCtx.expressionsBin.constraintsInfoDebug[constraintId].stage; constraintInfo.imPol = setupCtx.expressionsBin.constraintsInfoDebug[constraintId].imPol; - constraintInfo.line = setupCtx.expressionsBin.constraintsInfoDebug[constraintId].line.c_str(); constraintInfo.nrows = 0; uint64_t N = (1 << setupCtx.starkInfo.starkStruct.nBits); @@ -83,16 +76,10 @@ ConstraintInfo verifyConstraint(SetupCtx& setupCtx, Goldilocks::Element* dest, u constraintInfo.rows[i] = constraintInvalidRows[i]; } } - - return constraintInfo; } -ConstraintsResults *verifyConstraints(SetupCtx& setupCtx, StepsParams ¶ms) { +void verifyConstraints(SetupCtx& setupCtx, StepsParams ¶ms, ConstraintInfo *constraintsInfo) { - ConstraintsResults *constraintsInfo = new ConstraintsResults(); - constraintsInfo->nConstraints = setupCtx.expressionsBin.constraintsInfoDebug.size(); - constraintsInfo->constraintInfo = new ConstraintInfo[constraintsInfo->nConstraints]; - uint64_t N = (1 << setupCtx.starkInfo.starkStruct.nBits); uint64_t nPols = 0; @@ -103,11 +90,16 @@ ConstraintsResults *verifyConstraints(SetupCtx& setupCtx, StepsParams ¶ms) { // TODO: REUSE MEMORY Goldilocks::Element* pBuffer = new Goldilocks::Element[setupCtx.expressionsBin.constraintsInfoDebug.size() * N * FIELD_EXTENSION]; + std::vector destToConstraintIndex; + std::vector dests; for (uint64_t i = 0; i < setupCtx.expressionsBin.constraintsInfoDebug.size(); i++) { - Dest constraintDest(&pBuffer[i*FIELD_EXTENSION*N]); - constraintDest.addParams(setupCtx.expressionsBin.constraintsInfoDebug[i]); - dests.push_back(constraintDest); + if(!constraintsInfo[i].skip) { + Dest constraintDest(&pBuffer[i*FIELD_EXTENSION*N]); + constraintDest.addParams(setupCtx.expressionsBin.constraintsInfoDebug[i]); + dests.push_back(constraintDest); + destToConstraintIndex.push_back(i); + } } #ifdef __AVX512__ @@ -121,11 +113,10 @@ ConstraintsResults *verifyConstraints(SetupCtx& setupCtx, StepsParams ¶ms) { expressionsCtx.calculateExpressions(params, setupCtx.expressionsBin.expressionsBinArgsConstraints, dests, uint64_t(1 << setupCtx.starkInfo.starkStruct.nBits), false); #pragma omp parallel for - for (uint64_t i = 0; i < setupCtx.expressionsBin.constraintsInfoDebug.size(); i++) { - auto constraintInfo = verifyConstraint(setupCtx, dests[i].dest, i); - constraintsInfo->constraintInfo[i] = constraintInfo; + for (uint64_t i = 0; i < dests.size(); i++) { + uint64_t constraintIndex = destToConstraintIndex[i]; + verifyConstraint(setupCtx, dests[i].dest, constraintIndex, constraintsInfo[constraintIndex]); } - delete pBuffer; - return constraintsInfo; + delete[] pBuffer; } diff --git a/pilout/Cargo.toml b/pilout/Cargo.toml index 1d0cc1c7..eb7da2b0 100644 --- a/pilout/Cargo.toml +++ b/pilout/Cargo.toml @@ -3,13 +3,10 @@ name = "pilout" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] bytes.workspace = true prost.workspace = true log.workspace = true -proofman-util.workspace = true [build-dependencies] prost-build.workspace = true diff --git a/pilout/src/pilout.proto b/pilout/src/pilout.proto index 042b86a2..7b25bf8c 100644 --- a/pilout/src/pilout.proto +++ b/pilout/src/pilout.proto @@ -6,7 +6,7 @@ message PilOut { bytes baseField = 2; // base field characteristic repeated AirGroup airGroups = 3; repeated uint32 numChallenges = 4; // number of challenges per stage - uint32 numProofValues = 5; + repeated uint32 numProofValues = 5; uint32 numPublicValues = 6; repeated PublicTable publicTables = 7; repeated GlobalExpression expressions = 8; @@ -82,7 +82,8 @@ message GlobalOperand { } message ProofValue { - uint32 idx = 1; + uint32 stage = 1; + uint32 idx = 2; } message AirGroupValue { @@ -192,7 +193,8 @@ message Operand { } message ProofValue { - uint32 idx = 1; + uint32 stage = 1; + uint32 idx = 2; } message AirGroupValue { diff --git a/pilout/src/pilout_proxy.rs b/pilout/src/pilout_proxy.rs index 2d4a3ddc..7e619bea 100644 --- a/pilout/src/pilout_proxy.rs +++ b/pilout/src/pilout_proxy.rs @@ -120,7 +120,7 @@ impl PilOutProxy { trace!( "{}: #Proof values: {}, #Public values: {}, #Global expressions: {}, #Global constraints: {}", Self::MY_NAME, - self.pilout.num_proof_values, + self.pilout.num_proof_values.len(), self.pilout.num_public_values, self.pilout.expressions.len(), self.pilout.constraints.len() diff --git a/proofman/Cargo.toml b/proofman/Cargo.toml index 77335cdc..9306f320 100644 --- a/proofman/Cargo.toml +++ b/proofman/Cargo.toml @@ -7,10 +7,10 @@ edition = "2021" proofman-common = { path = "../common" } proofman-hints = { path = "../hints" } proofman-util = { path = "../util" } -pilout = { path = "../pilout" } stark = { path = "../provers/stark" } proofman-starks-lib-c = { path = "../provers/starks-lib-c" } transcript = { path = "../transcript" } +witness = { path = "../witness" } log.workspace = true env_logger.workspace = true diff --git a/proofman/src/constraints.rs b/proofman/src/constraints.rs deleted file mode 100644 index e40332de..00000000 --- a/proofman/src/constraints.rs +++ /dev/null @@ -1,149 +0,0 @@ -use p3_field::Field; -use std::{cmp, ffi::CStr}; - -use std::sync::Arc; - -use crate::{verify_global_constraints_proof, WitnessLibrary}; - -use proofman_common::{ExecutionCtx, ProofCtx, Prover, SetupCtx}; - -use colored::*; - -pub fn verify_constraints_proof( - pctx: Arc>, - ectx: Arc, - sctx: Arc, - provers: &mut [Box>], - witness_lib: &mut Box>, -) -> Result<(), Box> { - const MY_NAME: &str = "CstrVrfy"; - - log::info!("{}: --> Checking constraints", MY_NAME); - - witness_lib.debug(pctx.clone(), ectx.clone(), sctx.clone()); - - let mut constraints = Vec::new(); - for prover in provers.iter() { - let constraints_prover_info = prover.verify_constraints(sctx.clone(), pctx.clone()); - constraints.push(constraints_prover_info); - } - - let mut valid_constraints = true; - for (air_instance_index, air_instance) in pctx.air_instance_repo.air_instances.read().unwrap().iter().enumerate() { - let air_name = &pctx.global_info.airs[air_instance.airgroup_id][air_instance.air_id].name; - let mut valid_constraints_prover = true; - log::info!( - "{}: ► Instance #{} of {} (Airgroup {} - Air {})", - MY_NAME, - air_instance.air_instance_id.unwrap(), - air_name, - air_instance.airgroup_id, - air_instance.air_id, - ); - for constraint in &constraints[air_instance_index] { - let line_str = unsafe { CStr::from_ptr(constraint.line) }; - let valid = if constraint.n_rows > 0 { - format!("has {} invalid rows", constraint.n_rows).bright_red() - } else { - "is valid".bright_green() - }; - if constraint.im_pol { - log::trace!( - "{}: ··· Intermediate polynomial (stage {}) {} -> {:?}", - MY_NAME, - constraint.stage, - valid, - line_str.to_str().unwrap() - ); - } else if constraint.n_rows == 0 { - log::debug!( - "{}: · Constraint #{} (stage {}) {} -> {:?}", - MY_NAME, - constraint.id, - constraint.stage, - valid, - line_str.to_str().unwrap() - ); - } else { - log::info!( - "{}: · Constraint #{} (stage {}) {} -> {:?}", - MY_NAME, - constraint.id, - constraint.stage, - valid, - line_str.to_str().unwrap() - ); - } - if constraint.n_rows > 0 { - valid_constraints_prover = false; - } - let n_rows = cmp::min(constraint.n_rows, 10); - for i in 0..n_rows { - let row = constraint.rows[i as usize]; - if row.dim == 1 { - log::info!( - "{}: ··· \u{2717} Failed at row {} with value: {}", - MY_NAME, - row.row, - row.value[0] - ); - } else { - log::info!( - "{}: ··· \u{2717} Failed at row {} with value: [{}, {}, {}]", - MY_NAME, - row.row, - row.value[0], - row.value[1], - row.value[2] - ); - } - } - } - - if !valid_constraints_prover { - log::info!( - "{}: ··· {}", - MY_NAME, - format!( - "\u{2717} Not all constraints for Instance #{} of {} were verified", - air_instance.air_instance_id.unwrap(), - air_name - ) - .bright_red() - .bold() - ); - } else { - log::info!( - "{}: {}", - MY_NAME, - format!( - "\u{2713} All constraints for Instance #{} of {} were verified", - air_instance.air_instance_id.unwrap(), - air_name - ) - .bright_green() - .bold() - ); - } - - if !valid_constraints_prover { - valid_constraints = false; - } - } - - log::info!("{}: <-- Checking constraints", MY_NAME); - - let global_constraints_verified = verify_global_constraints_proof(pctx.clone(), sctx.clone()); - - if valid_constraints && global_constraints_verified { - log::info!("{}: ··· {}", MY_NAME, "\u{2713} All constraints were verified".bright_green().bold()); - Ok(()) - } else { - log::info!("{}: ··· {}", MY_NAME, "\u{2717} Not all constraints were verified.".bright_red().bold()); - Err(Box::new(std::io::Error::new( - // <-- Return a boxed error - std::io::ErrorKind::Other, - format!("{}: Not all constraints were verified.", MY_NAME), - ))) - } -} diff --git a/proofman/src/global_constraints.rs b/proofman/src/global_constraints.rs deleted file mode 100644 index 97f7d918..00000000 --- a/proofman/src/global_constraints.rs +++ /dev/null @@ -1,277 +0,0 @@ -use p3_field::Field; -use proofman_hints::{HintCol, HintFieldInfoValues, HintFieldOutput, HintFieldValue, HintFieldValues, HintFieldValuesVec}; -use proofman_starks_lib_c::{ - get_hint_field_global_constraints_c, set_hint_field_global_constraints_c, verify_global_constraints_c, -}; - -use std::{collections::HashMap, sync::Arc}; - -use proofman_common::{ExtensionField, ProofCtx, SetupCtx}; - -use colored::*; - -use std::os::raw::c_void; - -pub fn aggregate_airgroupvals(pctx: Arc>) -> Vec> { - const FIELD_EXTENSION: usize = 3; - - let mut airgroupvalues: Vec> = Vec::new(); - for agg_types in pctx.global_info.agg_types.iter() { - let mut values = vec![F::zero(); agg_types.len() * FIELD_EXTENSION]; - for (idx, agg_type) in agg_types.iter().enumerate() { - if agg_type.agg_type == 1 { - values[idx * FIELD_EXTENSION] = F::one(); - } - } - airgroupvalues.push(values); - } - - for air_instance in pctx.air_instance_repo.air_instances.write().unwrap().iter() { - for (idx, agg_type) in pctx.global_info.agg_types[air_instance.airgroup_id].iter().enumerate() { - let mut acc = ExtensionField { - value: [ - airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION], - airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 1], - airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 2], - ], - }; - if !air_instance.airgroup_values.is_empty() { - let instance_airgroup_val = ExtensionField { - value: [ - air_instance.airgroup_values[idx * FIELD_EXTENSION], - air_instance.airgroup_values[idx * FIELD_EXTENSION + 1], - air_instance.airgroup_values[idx * FIELD_EXTENSION + 2], - ], - }; - if agg_type.agg_type == 0 { - acc += instance_airgroup_val; - } else { - acc *= instance_airgroup_val; - } - airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION] = acc.value[0]; - airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 1] = acc.value[1]; - airgroupvalues[air_instance.airgroup_id][idx * FIELD_EXTENSION + 2] = acc.value[2]; - } - } - } - - airgroupvalues -} - -pub fn verify_global_constraints_proof(pctx: Arc>, sctx: Arc) -> bool { - const MY_NAME: &str = "GlCstVfy"; - - log::info!("{}: --> Checking global constraints", MY_NAME); - - let public_inputs_guard = pctx.public_inputs.inputs.read().unwrap(); - let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void; - - let challenges_guard = pctx.challenges.challenges.read().unwrap(); - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - - let proof_values_guard = pctx.proof_values.values.read().unwrap(); - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; - - let mut airgroupvalues = aggregate_airgroupvals(pctx.clone()); - - let mut airgroup_values_ptrs: Vec<*mut F> = airgroupvalues - .iter_mut() // Iterate mutably over the inner Vecs - .map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec - .collect(); - - let global_constraints_verified = verify_global_constraints_c( - sctx.get_global_bin(), - public_inputs, - challenges, - proof_values, - airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void, - ); - - log::info!("{}: <-- Checking global constraints", MY_NAME); - - if global_constraints_verified { - log::info!( - "{}: ··· {}", - MY_NAME, - "\u{2713} All global constraints were successfully verified".bright_green().bold() - ); - } else { - log::info!("{}: ··· {}", MY_NAME, "\u{2717} Not all global constraints were verified".bright_red().bold()); - } - - global_constraints_verified -} - -pub fn get_hint_field_gc( - pctx: Arc>, - sctx: Arc, - hint_id: u64, - hint_field_name: &str, - print_expression: bool, -) -> HintFieldValue { - let public_inputs_ptr = pctx.public_inputs.inputs.read().unwrap().as_ptr() as *mut c_void; - - let mut airgroupvalues = aggregate_airgroupvals(pctx.clone()); - let mut airgroup_values_ptrs: Vec<*mut F> = airgroupvalues - .iter_mut() // Iterate mutably over the inner Vecs - .map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec - .collect(); - - let challenges_guard = pctx.challenges.challenges.read().unwrap(); - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - - let proof_values_guard = pctx.proof_values.values.read().unwrap(); - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; - - let raw_ptr = get_hint_field_global_constraints_c( - sctx.get_global_bin(), - public_inputs_ptr, - challenges, - proof_values, - airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void, - hint_id, - hint_field_name, - print_expression, - ); - - unsafe { - let hint_field_values = &*(raw_ptr as *mut HintFieldInfoValues); - let value = &*(hint_field_values.hint_field_values.add(0)); - if value.matrix_size != 0 { - panic!("get_hint_field can only be called with single expressions, but {} is an array", hint_field_name); - } - HintCol::from_hint_field(value) - } -} - -pub fn get_hint_field_gc_a( - pctx: Arc>, - sctx: Arc, - hint_id: u64, - hint_field_name: &str, - print_expression: bool, -) -> HintFieldValuesVec { - let public_inputs_ptr = pctx.public_inputs.inputs.read().unwrap().as_ptr() as *mut c_void; - - let mut airgroupvalues = aggregate_airgroupvals(pctx.clone()); - let mut airgroup_values_ptrs: Vec<*mut F> = airgroupvalues - .iter_mut() // Iterate mutably over the inner Vecs - .map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec - .collect(); - - let challenges_guard = pctx.challenges.challenges.read().unwrap(); - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - - let proof_values_guard = pctx.proof_values.values.read().unwrap(); - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; - - let raw_ptr = get_hint_field_global_constraints_c( - sctx.get_global_bin(), - public_inputs_ptr, - challenges, - proof_values, - airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void, - hint_id, - hint_field_name, - print_expression, - ); - - unsafe { - let mut hint_field_values = Vec::new(); - let hint_field = &*(raw_ptr as *mut HintFieldInfoValues); - for v in 0..hint_field.n_values { - let h = &*(hint_field.hint_field_values.add(v as usize)); - if v == 0 && h.matrix_size != 1 { - panic!("get_hint_field_m can only be called with an array of expressions!"); - } - let hint_value = HintCol::from_hint_field(h); - hint_field_values.push(hint_value); - } - - HintFieldValuesVec { values: hint_field_values } - } -} - -pub fn get_hint_field_gc_m( - pctx: Arc>, - sctx: Arc, - hint_id: u64, - hint_field_name: &str, - print_expression: bool, -) -> HintFieldValues { - let public_inputs_ptr = pctx.public_inputs.inputs.read().unwrap().as_ptr() as *mut c_void; - - let mut airgroupvalues = aggregate_airgroupvals(pctx.clone()); - let mut airgroup_values_ptrs: Vec<*mut F> = airgroupvalues - .iter_mut() // Iterate mutably over the inner Vecs - .map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec - .collect(); - - let challenges_guard = pctx.challenges.challenges.read().unwrap(); - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - - let proof_values_guard = pctx.proof_values.values.read().unwrap(); - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; - - let raw_ptr = get_hint_field_global_constraints_c( - sctx.get_global_bin(), - public_inputs_ptr, - challenges, - proof_values, - airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void, - hint_id, - hint_field_name, - print_expression, - ); - - unsafe { - let hint_field = &*(raw_ptr as *mut HintFieldInfoValues); - let mut hint_field_values = HashMap::with_capacity(hint_field.n_values as usize); - - for v in 0..hint_field.n_values { - let h = &*(hint_field.hint_field_values.add(v as usize)); - if v == 0 && h.matrix_size > 2 { - panic!("get_hint_field_m can only be called with a matrix of expressions!",); - } - let hint_value = HintCol::from_hint_field(h); - let mut pos = Vec::new(); - for p in 0..h.matrix_size { - pos.push(h.pos.wrapping_add(p as usize) as u64); - } - hint_field_values.insert(pos, hint_value); - } - - HintFieldValues { values: hint_field_values } - } -} - -pub fn set_hint_field( - pctx: Arc>, - sctx: Arc, - hint_id: u64, - hint_field_name: &str, - value: HintFieldOutput, -) { - let proof_values_guard = pctx.proof_values.values.read().unwrap(); - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; - - let mut value_array: Vec = Vec::new(); - - match value { - HintFieldOutput::Field(val) => { - value_array.push(val); - } - HintFieldOutput::FieldExtended(val) => { - value_array.push(val.value[0]); - value_array.push(val.value[1]); - value_array.push(val.value[2]); - } - }; - - let values_ptr = value_array.as_ptr() as *mut c_void; - - let id = - set_hint_field_global_constraints_c(sctx.get_global_bin(), proof_values, values_ptr, hint_id, hint_field_name); - - pctx.set_proof_value_calculated(id as usize); -} diff --git a/proofman/src/lib.rs b/proofman/src/lib.rs index 68e25327..8e6a82fb 100644 --- a/proofman/src/lib.rs +++ b/proofman/src/lib.rs @@ -1,17 +1,7 @@ mod proofman; -mod witness_component; -mod witness_executor; -mod witness_library; -mod witness_manager; -mod constraints; +mod verify_constraints; mod recursion; -mod global_constraints; pub use proofman::*; -pub use constraints::*; -pub use global_constraints::*; +pub use verify_constraints::*; pub use recursion::*; -pub use witness_component::*; -pub use witness_executor::*; -pub use witness_library::*; -pub use witness_manager::*; diff --git a/proofman/src/proofman.rs b/proofman/src/proofman.rs index ac53665b..f8870ff5 100644 --- a/proofman/src/proofman.rs +++ b/proofman/src/proofman.rs @@ -1,9 +1,8 @@ use libloading::{Library, Symbol}; use log::{info, trace}; -use p3_field::Field; -use stark::{StarkBufferAllocator, StarkProver}; -use proofman_starks_lib_c::{save_challenges_c, save_proof_values_c, save_publics_c}; -use core::panic; +use p3_field::PrimeField; +use stark::StarkProver; +use proofman_starks_lib_c::{save_challenges_c, save_proof_values_c, save_publics_c, get_map_totaln_c}; use std::fs; use std::error::Error; @@ -13,14 +12,16 @@ use std::{collections::HashMap, path::PathBuf, sync::Arc}; use transcript::FFITranscript; -use crate::{WitnessLibInitFn, WitnessLibrary}; -use crate::verify_constraints_proof; +use witness::{WitnessLibInitFn, WitnessManager}; + use crate::{ - generate_vadcop_recursive1_proof, generate_vadcop_final_proof, generate_vadcop_recursive2_proof, - generate_recursivef_proof, generate_fflonk_snark_proof, + verify_constraints_proof, generate_vadcop_recursive1_proof, generate_vadcop_final_proof, + generate_vadcop_recursive2_proof, generate_recursivef_proof, generate_fflonk_snark_proof, }; -use proofman_common::{ExecutionCtx, ProofCtx, ProofOptions, ProofType, Prover, SetupCtx, SetupsVadcop}; +use proofman_common::{ + format_bytes, skip_prover_instance, ProofCtx, ProofOptions, ProofType, Prover, SetupCtx, SetupsVadcop, +}; use std::os::raw::c_void; @@ -32,14 +33,13 @@ pub struct ProofMan { _phantom: std::marker::PhantomData, } -impl ProofMan { +impl ProofMan { const MY_NAME: &'static str = "ProofMan"; pub fn generate_proof( witness_lib_path: PathBuf, rom_path: Option, public_inputs_path: Option, - cached_buffers_paths: Option>, proving_key_path: PathBuf, output_dir_path: PathBuf, options: ProofOptions, @@ -52,53 +52,39 @@ impl ProofMan { &witness_lib_path, &rom_path, &public_inputs_path, - &cached_buffers_paths, &proving_key_path, &output_dir_path, options.verify_constraints, )?; - let buffer_allocator: Arc = Arc::new(StarkBufferAllocator::new(proving_key_path.clone())); - let ectx = ExecutionCtx::builder() - .with_rom_path(rom_path.clone()) - .with_cached_buffers_path(cached_buffers_paths.clone()) - .with_buffer_allocator(buffer_allocator) - .with_verbose_mode(options.verbose_mode) - .with_std_mode(options.std_mode) - .build(); - let ectx = Arc::new(ectx); - // Load the witness computation dynamic library - let library = unsafe { Library::new(&witness_lib_path)? }; - let witness_lib: Symbol> = unsafe { library.get(b"init_library")? }; + let pctx = Arc::new(ProofCtx::create_ctx(proving_key_path.clone(), options)); - let mut witness_lib = witness_lib(rom_path, public_inputs_path, ectx.verbose_mode)?; - - let pctx = Arc::new(ProofCtx::create_ctx(witness_lib.pilout(), proving_key_path.clone())); - - let setups = Arc::new(SetupsVadcop::new(&pctx.global_info, options.aggregation, options.final_snark)); + let setups = Arc::new(SetupsVadcop::new(&pctx.global_info, pctx.options.aggregation, pctx.options.final_snark)); let sctx: Arc = setups.sctx.clone(); - Self::initialize_witness(&mut witness_lib, pctx.clone(), ectx.clone(), sctx.clone()); - witness_lib.calculate_witness(1, pctx.clone(), ectx.clone(), sctx.clone()); + let wcm = Arc::new(WitnessManager::new(pctx.clone(), sctx.clone(), rom_path, public_inputs_path)); - let mut dctx = ectx.dctx.write().unwrap(); + Self::initialize_witness(witness_lib_path, wcm.clone())?; + wcm.calculate_witness(1); + + let mut dctx = pctx.dctx.write().unwrap(); dctx.close(pctx.global_info.air_groups.len()); let mpi_rank = dctx.rank; + let n_processes = dctx.n_processes; drop(dctx); + if mpi_rank == 0 { - Self::print_summary(pctx.clone()); + Self::print_global_summary(pctx.clone(), setups.sctx.clone()); } - Self::initialize_fixed_pols( - setups.clone(), - pctx.clone(), - ectx.clone(), - options.aggregation, - options.final_snark, - ); + if n_processes > 1 { + Self::print_summary(pctx.clone(), setups.sctx.clone()); + } + + Self::initialize_fixed_pols(setups.clone(), pctx.clone()); let mut provers: Vec>> = Vec::new(); - Self::initialize_provers(sctx.clone(), &mut provers, pctx.clone(), ectx.clone()); + Self::initialize_provers(sctx.clone(), &mut provers, pctx.clone()); if provers.is_empty() { return Err("No instances found".into()); @@ -106,7 +92,6 @@ impl ProofMan { let mut transcript: FFITranscript = provers[0].new_transcript(); - Self::check_stage(0, &mut provers, pctx.clone()); for prover in provers.iter_mut() { prover.commit_stage(0, pctx.clone()); } @@ -118,81 +103,43 @@ impl ProofMan { if stage != 1 { timer_start_debug!(CALCULATING_WITNESS); info!("{}: Calculating witness stage {}", Self::MY_NAME, stage); - witness_lib.calculate_witness(stage, pctx.clone(), ectx.clone(), sctx.clone()); + wcm.calculate_witness(stage); timer_stop_and_log_debug!(CALCULATING_WITNESS); } Self::calculate_stage(stage, &mut provers, sctx.clone(), pctx.clone()); - Self::check_stage(stage, &mut provers, pctx.clone()); - if !options.verify_constraints { + if !pctx.options.verify_constraints { Self::commit_stage(stage, &mut provers, pctx.clone()); } - let publics_set = pctx.public_inputs.inputs_set.read().unwrap(); - for i in 0..pctx.global_info.n_publics { - let public = pctx.global_info.publics_map.as_ref().expect("REASON").get(i).unwrap(); - if !publics_set[i] { - if !public.lengths.is_empty() { - panic!( - "Not all publics are set: Public {}[{}] is not calculated", - public.name, public.lengths[0] - ); - } else { - panic!("Not all publics are set: Public {} is not calculated", public.name); - } - } - } - - if !options.verify_constraints || stage < num_commit_stages { - Self::calculate_challenges( - stage, - &mut provers, - pctx.clone(), - ectx.clone(), - &mut transcript, - options.verify_constraints, - ); + if !pctx.options.verify_constraints || stage < num_commit_stages { + Self::calculate_challenges(stage, &mut provers, pctx.clone(), &mut transcript); } } - witness_lib.end_proof(); - - for i in 0..pctx.global_info.n_proof_values { - if !pctx.proof_values.values_set.read().unwrap().contains_key(&i) { - panic!( - "Proof cannot be generated: Proof value {} is not set", - pctx.global_info.proof_values_map.as_ref().expect("REASON").get(i).unwrap().name - ); - } - } + wcm.end_proof(); - if options.verify_constraints { - return verify_constraints_proof(pctx.clone(), ectx.clone(), sctx.clone(), &mut provers, &mut witness_lib); + if pctx.options.verify_constraints { + wcm.debug(); + return verify_constraints_proof(pctx.clone(), sctx.clone(), &mut provers); } // Compute Quotient polynomial Self::get_challenges(num_commit_stages + 1, &mut provers, pctx.clone(), &transcript); Self::calculate_stage(num_commit_stages + 1, &mut provers, sctx.clone(), pctx.clone()); Self::commit_stage(num_commit_stages + 1, &mut provers, pctx.clone()); - Self::calculate_challenges( - num_commit_stages + 1, - &mut provers, - pctx.clone(), - ectx.clone(), - &mut transcript, - false, - ); + Self::calculate_challenges(num_commit_stages + 1, &mut provers, pctx.clone(), &mut transcript); // Compute openings - Self::opening_stages(&mut provers, pctx.clone(), sctx.clone(), ectx.clone(), &mut transcript); + Self::opening_stages(&mut provers, pctx.clone(), sctx.clone(), &mut transcript); //Generate proves_out let proves_out = Self::finalize_proof(&mut provers, pctx.clone(), output_dir_path.to_string_lossy().as_ref()); timer_stop_and_log_info!(GENERATING_PROOF); - if !options.aggregation { + if !pctx.options.aggregation { return Ok(()); } @@ -205,12 +152,11 @@ impl ProofMan { timer_stop_and_log_info!(GENERATING_COMPRESSOR_AND_RECURSIVE1_PROOFS); log::info!("{}: Compressor and recursive1 proofs generated successfully", Self::MY_NAME); - ectx.dctx.read().unwrap().barrier(); + pctx.dctx.read().unwrap().barrier(); timer_start_info!(GENERATING_RECURSIVE2_PROOFS); let sctx_recursive2 = setups.sctx_recursive2.clone(); let recursive2_proof = generate_vadcop_recursive2_proof( &pctx, - &ectx, sctx_recursive2.as_ref().unwrap().clone(), &recursive1_proofs, output_dir_path.clone(), @@ -219,7 +165,7 @@ impl ProofMan { timer_stop_and_log_info!(GENERATING_RECURSIVE2_PROOFS); log::info!("{}: Recursive2 proofs generated successfully", Self::MY_NAME); - ectx.dctx.read().unwrap().barrier(); + pctx.dctx.read().unwrap().barrier(); if mpi_rank == 0 { timer_start_info!(GENERATING_VADCOP_FINAL_PROOF); let final_proof = generate_vadcop_final_proof( @@ -233,7 +179,7 @@ impl ProofMan { timer_stop_and_log_info!(GENERATING_AGGREGATION_PROOFS); - if options.final_snark { + if pctx.options.final_snark { timer_start_info!(GENERATING_RECURSIVE_F_PROOF); let recursivef_proof = generate_recursivef_proof( &pctx, @@ -250,20 +196,27 @@ impl ProofMan { } timer_stop_and_log_info!(GENERATING_VADCOP_PROOF); log::info!("{}: Proofs generated successfully", Self::MY_NAME); - ectx.dctx.read().unwrap().barrier(); + pctx.dctx.read().unwrap().barrier(); Ok(()) } fn initialize_witness( - witness_lib: &mut Box>, - pctx: Arc>, - ectx: Arc, - sctx: Arc, - ) { + witness_lib_path: PathBuf, + wcm: Arc>, + ) -> Result<(), Box> { timer_start_debug!(INITIALIZE_WITNESS); - witness_lib.start_proof(pctx.clone(), ectx.clone(), sctx.clone()); - witness_lib.execute(pctx.clone(), ectx, sctx); + // Load the witness computation dynamic library + let library = unsafe { Library::new(&witness_lib_path)? }; + + let witness_lib: Symbol> = unsafe { library.get(b"init_library")? }; + let mut witness_lib = witness_lib(wcm.get_pctx().options.verbose_mode)?; + witness_lib.register_witness(wcm.clone()); + + wcm.start_proof(); + wcm.execute(); + + let pctx = wcm.get_pctx(); // After the execution print the planned instances trace!("{}: --> Air instances: ", Self::MY_NAME); @@ -295,25 +248,31 @@ impl ProofMan { } } timer_stop_and_log_debug!(INITIALIZE_WITNESS); + Ok(()) } - fn initialize_provers( - sctx: Arc, - provers: &mut Vec>>, - pctx: Arc>, - _ectx: Arc, - ) { + fn initialize_provers(sctx: Arc, provers: &mut Vec>>, pctx: Arc>) { timer_start_debug!(INITIALIZE_PROVERS); info!("{}: Initializing provers", Self::MY_NAME); for air_instance in pctx.air_instance_repo.air_instances.read().unwrap().iter() { let air_name = &pctx.global_info.airs[air_instance.airgroup_id][air_instance.air_id].name; log::debug!("{}: Initializing prover for air instance {}", Self::MY_NAME, air_name); + let (skip, constraints_skip) = skip_prover_instance( + pctx.options.clone(), + air_instance.airgroup_id, + air_instance.air_id, + air_instance.air_instance_id.unwrap(), + ); + if skip { + continue; + }; let prover = Box::new(StarkProver::new( sctx.clone(), air_instance.airgroup_id, air_instance.air_id, air_instance.air_instance_id.unwrap(), air_instance.idx.unwrap(), + constraints_skip, )); provers.push(prover); @@ -324,8 +283,9 @@ impl ProofMan { } let mut buff_helper_size = 0_usize; + for prover in provers.iter_mut() { - let buff_helper_prover_size = prover.get_buff_helper_size(); + let buff_helper_prover_size = prover.get_buff_helper_size(pctx.clone()); if buff_helper_prover_size > buff_helper_size { buff_helper_size = buff_helper_prover_size; } @@ -333,17 +293,11 @@ impl ProofMan { let buff_helper = create_buffer_fast(buff_helper_size); - *pctx.buff_helper.buff_helper.write().unwrap() = buff_helper; + *pctx.buff_helper.values.write().unwrap() = buff_helper; timer_stop_and_log_debug!(INITIALIZE_PROVERS); } - fn initialize_fixed_pols( - setups: Arc, - pctx: Arc>, - _ectx: Arc, - aggregation: bool, - final_snark: bool, - ) { + fn initialize_fixed_pols(setups: Arc, pctx: Arc>) { info!("{}: Initializing setup fixed pols", Self::MY_NAME); timer_start_debug!(INITIALIZE_SETUP); timer_start_debug!(INITIALIZE_CONST_POLS); @@ -362,7 +316,7 @@ impl ProofMan { timer_stop_and_log_debug!(INITIALIZE_CONST_POLS); - if aggregation { + if pctx.options.aggregation { info!("{}: Initializing setup fixed pols aggregation", Self::MY_NAME); let sctx_compressor = setups.sctx_compressor.as_ref().unwrap().clone(); @@ -411,19 +365,22 @@ impl ProofMan { } timer_stop_and_log_debug!(INITIALIZE_CONST_POLS_RECURSIVE2); - timer_start_debug!(INITIALIZE_CONST_POLS_VADCOP_FINAL); - info!("{}: ··· Initializing setup fixed pols vadcop final", Self::MY_NAME); - setup_vadcop_final.load_const_pols(&pctx.global_info, &ProofType::VadcopFinal); - setup_vadcop_final.load_const_pols_tree(&pctx.global_info, &ProofType::VadcopFinal, false); - timer_stop_and_log_debug!(INITIALIZE_CONST_POLS_VADCOP_FINAL); - - if final_snark { - let setup_recursivef = setups.setup_recursivef.as_ref().unwrap().clone(); - timer_start_debug!(INITIALIZE_CONST_POLS_RECURSIVE_FINAL); - info!("{}: ··· Initializing setup fixed pols recursive final", Self::MY_NAME); - setup_recursivef.load_const_pols(&pctx.global_info, &ProofType::RecursiveF); - setup_recursivef.load_const_pols_tree(&pctx.global_info, &ProofType::RecursiveF, false); - timer_stop_and_log_debug!(INITIALIZE_CONST_POLS_RECURSIVE_FINAL); + let dctx = pctx.dctx.read().unwrap(); + if dctx.rank == 0 { + timer_start_debug!(INITIALIZE_CONST_POLS_VADCOP_FINAL); + info!("{}: ··· Initializing setup fixed pols vadcop final", Self::MY_NAME); + setup_vadcop_final.load_const_pols(&pctx.global_info, &ProofType::VadcopFinal); + setup_vadcop_final.load_const_pols_tree(&pctx.global_info, &ProofType::VadcopFinal, false); + timer_stop_and_log_debug!(INITIALIZE_CONST_POLS_VADCOP_FINAL); + + if pctx.options.final_snark { + let setup_recursivef = setups.setup_recursivef.as_ref().unwrap().clone(); + timer_start_debug!(INITIALIZE_CONST_POLS_RECURSIVE_FINAL); + info!("{}: ··· Initializing setup fixed pols recursive final", Self::MY_NAME); + setup_recursivef.load_const_pols(&pctx.global_info, &ProofType::RecursiveF); + setup_recursivef.load_const_pols_tree(&pctx.global_info, &ProofType::RecursiveF, false); + timer_stop_and_log_debug!(INITIALIZE_CONST_POLS_RECURSIVE_FINAL); + } } } timer_stop_and_log_debug!(INITIALIZE_SETUP); @@ -452,13 +409,6 @@ impl ProofMan { } } - pub fn check_stage(stage: u32, provers: &mut [Box>], proof_ctx: Arc>) { - log::debug!("{}: Checking stage can be calculated", Self::MY_NAME); - for prover in provers.iter_mut() { - prover.check_stage(stage, proof_ctx.clone()); - } - } - pub fn commit_stage(stage: u32, provers: &mut [Box>], proof_ctx: Arc>) { if stage as usize == proof_ctx.global_info.n_challenges.len() + 1 { info!("{}: Committing stage Q", Self::MY_NAME); @@ -512,77 +462,55 @@ impl ProofMan { stage: u32, provers: &mut [Box>], pctx: Arc>, - _ectx: Arc, transcript: &mut FFITranscript, - verify_constraints: bool, ) { if stage == 1 { - let public_inputs_guard = pctx.public_inputs.inputs.read().unwrap(); - let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void; - - transcript.add_elements(public_inputs, pctx.global_info.n_publics); - } - - // TODO: ACTIVATE DCTX BACK - // let dctx = ectx.dctx.read().unwrap(); - - // // calculate my roots - // let mut roots: Vec = vec![0; 4 * provers.len()]; - // for (i, prover) in provers.iter_mut().enumerate() { - // // Important we need the roots in u64 in order to distribute them - // let values = prover.get_transcript_values_u64(stage as u64, pctx.clone()); - // if values.is_empty() { - // panic!("No transcript values found for prover {}", i); - // } - // roots[i * 4..(i + 1) * 4].copy_from_slice(&values) - // } - // // get all roots - // let all_roots = dctx.distribute_roots(roots); - - // // add challenges to transcript in order - // for group_idxs in dctx.my_groups.iter() { - // if verify_constraints { - // let dummy_elements = [F::zero(), F::one(), F::two(), F::neg_one()]; - // transcript.add_elements(dummy_elements.as_ptr() as *mut c_void, 4); - // } else { - // let mut values = Vec::new(); - // for idx in group_idxs.iter() { - // let value = vec![ - // F::from_wrapped_u64(all_roots[*idx]), - // F::from_wrapped_u64(all_roots[*idx + 1]), - // F::from_wrapped_u64(all_roots[*idx + 2]), - // F::from_wrapped_u64(all_roots[*idx + 3]), - // ]; - // values.push(value); - // } - // if !values.is_empty() { - // let value = Self::hash_b_tree(&*provers[0], values); - // transcript.add_elements(value.as_ptr() as *mut c_void, value.len()); - // } - // } - // } - // drop(dctx); - let airgroups = pctx.global_info.air_groups.clone(); - for (airgroup_id, _airgroup) in airgroups.iter().enumerate() { - if verify_constraints { + transcript.add_elements(pctx.get_publics_ptr(), pctx.global_info.n_publics); + } + + let proof_values_stage = pctx.get_proof_values_by_stage(stage); + if !proof_values_stage.is_empty() { + transcript.add_elements(proof_values_stage.as_ptr() as *mut u8, proof_values_stage.len()); + } + + let dctx = pctx.dctx.read().unwrap(); + + // calculate my roots + let mut roots: Vec = vec![0; 4 * provers.len()]; + for (i, prover) in provers.iter_mut().enumerate() { + // Important we need the roots in u64 in order to distribute them + let values = prover.get_transcript_values_u64(stage as u64, pctx.clone()); + if values.is_empty() { + panic!("No transcript values found for prover {}", i); + } + roots[i * 4..(i + 1) * 4].copy_from_slice(&values) + } + // get all roots + let all_roots = dctx.distribute_roots(roots); + + // add challenges to transcript in order + for group_idxs in dctx.my_groups.iter() { + if pctx.options.verify_constraints { let dummy_elements = [F::zero(), F::one(), F::two(), F::neg_one()]; - transcript.add_elements(dummy_elements.as_ptr() as *mut c_void, 4); + transcript.add_elements(dummy_elements.as_ptr() as *mut u8, 4); } else { - let airgroup_instances = pctx.air_instance_repo.find_airgroup_instances(airgroup_id); - - if !airgroup_instances.is_empty() { - let mut values = Vec::new(); - for prover_idx in airgroup_instances.iter() { - let value = provers[*prover_idx].get_transcript_values(stage as u64, pctx.clone()); - values.push(value); - } - if !values.is_empty() { - let value = Self::hash_b_tree(&*provers[airgroup_instances[0]], values); - transcript.add_elements(value.as_ptr() as *mut c_void, value.len()); - } + let mut values = Vec::new(); + for idx in group_idxs.iter() { + let value = vec![ + F::from_wrapped_u64(all_roots[*idx]), + F::from_wrapped_u64(all_roots[*idx + 1]), + F::from_wrapped_u64(all_roots[*idx + 2]), + F::from_wrapped_u64(all_roots[*idx + 3]), + ]; + values.push(value); + } + if !values.is_empty() { + let value = Self::hash_b_tree(&*provers[0], values); + transcript.add_elements(value.as_ptr() as *mut u8, value.len()); } } } + drop(dctx); } fn get_challenges( @@ -598,52 +526,34 @@ impl ProofMan { provers: &mut [Box>], pctx: Arc>, sctx: Arc, - ectx: Arc, + transcript: &mut FFITranscript, ) { let num_commit_stages = pctx.global_info.n_challenges.len() as u32; + let dctx = pctx.dctx.read().unwrap(); + // Calculate evals timer_start_debug!(CALCULATING_EVALS); Self::get_challenges(pctx.global_info.n_challenges.len() as u32 + 2, provers, pctx.clone(), transcript); - // TODO: ACTIVATE DCTX BACK - // for group_idx in dctx.my_air_groups.iter() { - // provers[group_idx[0]].calculate_lev(pctx.clone()); - // for idx in group_idx.iter() { - // provers[*idx].opening_stage(1, sctx.clone(), pctx.clone()); - // } - // } - for airgroup_id in 0..pctx.global_info.air_groups.len() { - for air_id in 0..pctx.global_info.airs[airgroup_id].len() { - let instances = pctx.air_instance_repo.find_air_instances(airgroup_id, air_id); - for instance in instances { - provers[instance].calculate_lev(pctx.clone()); - provers[instance].opening_stage(1, sctx.clone(), pctx.clone()); - } + for group_idx in dctx.my_air_groups.iter() { + provers[group_idx[0]].calculate_lev(pctx.clone()); + for idx in group_idx.iter() { + provers[*idx].opening_stage(1, sctx.clone(), pctx.clone()); } } timer_stop_and_log_debug!(CALCULATING_EVALS); - Self::calculate_challenges(num_commit_stages + 2, provers, pctx.clone(), ectx.clone(), transcript, false); + Self::calculate_challenges(num_commit_stages + 2, provers, pctx.clone(), transcript); // Calculate fri polynomial Self::get_challenges(pctx.global_info.n_challenges.len() as u32 + 3, provers, pctx.clone(), transcript); info!("{}: Calculating FRI Polynomials", Self::MY_NAME); timer_start_debug!(CALCULATING_FRI_POLINOMIAL); - // TODO: ACTIVATE DCTX BACK - // for group_idx in dctx.my_air_groups.iter() { - // provers[group_idx[0]].calculate_xdivxsub(pctx.clone()); - // for idx in group_idx.iter() { - // provers[*idx].opening_stage(2, sctx.clone(), pctx.clone()); - // } - // } - for airgroup_id in 0..pctx.global_info.air_groups.len() { - for air_id in 0..pctx.global_info.airs[airgroup_id].len() { - let instances = pctx.air_instance_repo.find_air_instances(airgroup_id, air_id); - for instance in instances { - provers[instance].calculate_xdivxsub(pctx.clone()); - provers[instance].opening_stage(2, sctx.clone(), pctx.clone()); - } + for group_idx in dctx.my_air_groups.iter() { + provers[group_idx[0]].calculate_xdivxsub(pctx.clone()); + for idx in group_idx.iter() { + provers[*idx].opening_stage(2, sctx.clone(), pctx.clone()); } } timer_stop_and_log_debug!(CALCULATING_FRI_POLINOMIAL); @@ -684,9 +594,7 @@ impl ProofMan { pctx.global_info.n_challenges.len() as u32 + 4 + opening_id, provers, pctx.clone(), - ectx.clone(), transcript, - false, ); } timer_stop_and_log_debug!(CALCULATING_FRI_STEP); @@ -705,41 +613,159 @@ impl ProofMan { proves.push(prover.get_zkin_proof(proof_ctx.clone(), output_dir)); prover.free(); } - let public_inputs_guard = proof_ctx.public_inputs.inputs.read().unwrap(); - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - let proof_values_guard = proof_ctx.proof_values.values.read().unwrap(); let n_publics = proof_ctx.global_info.n_publics as u64; - let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void; - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - - let n_proof_values = proof_ctx.global_info.n_proof_values as u64; - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; let global_info_path = proof_ctx.global_info.get_proving_key_path().join("pilout.globalInfo.json"); let global_info_file: &str = global_info_path.to_str().unwrap(); - save_publics_c(n_publics, public_inputs, output_dir); + save_publics_c(n_publics, proof_ctx.get_publics_ptr(), output_dir); - save_proof_values_c(n_proof_values, proof_values, output_dir); + save_proof_values_c(proof_ctx.get_proof_values_ptr(), global_info_file, output_dir); - save_challenges_c(challenges, global_info_file, output_dir); + save_challenges_c(proof_ctx.get_challenges_ptr(), global_info_file, output_dir); timer_stop_and_log_debug!(FINALIZING_PROOF); proves } - fn print_summary(pctx: Arc>) { + fn print_global_summary(pctx: Arc>, sctx: Arc) { + let dctx = pctx.dctx.read().unwrap(); + + let mut air_info = HashMap::new(); + + let mut air_instances = HashMap::new(); + + for (airgroup_id, air_id) in &dctx.instances { + let air_name = pctx.global_info.airs[*airgroup_id][*air_id].clone().name; + let air_group_name = pctx.global_info.air_groups[*airgroup_id].clone(); + let air_instance_map = air_instances.entry(air_group_name).or_insert_with(HashMap::new); + if !air_instance_map.contains_key(&air_name.clone()) { + let setup = sctx.get_setup(*airgroup_id, *air_id); + let n_bits = setup.stark_info.stark_struct.n_bits; + let memory_instance = get_map_totaln_c(setup.p_setup.p_stark_info) as f64 * 8.0; + let memory_fixed = (setup.stark_info.n_constants * (1 << (setup.stark_info.stark_struct.n_bits)) + + setup.stark_info.n_constants * (1 << (setup.stark_info.stark_struct.n_bits_ext))) + as f64 + * 8.0; + let memory_helpers = setup.stark_info.get_buff_helper_size() as f64 * 8.0; + let total_cols: u64 = setup + .stark_info + .map_sections_n + .iter() + .filter(|(key, _)| *key != "const") + .map(|(_, value)| *value) + .sum(); + air_info.insert(air_name.clone(), (n_bits, total_cols, memory_fixed, memory_helpers, memory_instance)); + } + let air_instance_map_key = air_instance_map.entry(air_name).or_insert(0); + *air_instance_map_key += 1; + } + + let mut air_groups: Vec<_> = air_instances.keys().collect(); + air_groups.sort(); + + info!( + "{}", + format!("{}: --- TOTAL PROOF INSTANCES SUMMARY ------------------------", Self::MY_NAME) + .bright_white() + .bold() + ); + info!("{}: ► {} Air instances found:", Self::MY_NAME, dctx.instances.len()); + for air_group in air_groups.clone() { + let air_group_instances = air_instances.get(air_group).unwrap(); + let mut air_names: Vec<_> = air_group_instances.keys().collect(); + air_names.sort(); + + info!("{}: Air Group [{}]", Self::MY_NAME, air_group); + for air_name in air_names { + let count = air_group_instances.get(air_name).unwrap(); + let (n_bits, total_cols, _, _, _) = air_info.get(air_name).unwrap(); + info!( + "{}: {}", + Self::MY_NAME, + format!("· {} x Air [{}] ({} x 2^{})", count, air_name, total_cols, n_bits).bright_white().bold() + ); + } + } + info!("{}: ----------------------------------------------------------", Self::MY_NAME); + info!( + "{}", + format!("{}: --- TOTAL PROVER MEMORY USAGE ----------------------------", Self::MY_NAME) + .bright_white() + .bold() + ); + let mut total_memory = 0f64; + let mut memory_helper_size = 0f64; + for air_group in air_groups { + let air_group_instances = air_instances.get(air_group).unwrap(); + let mut air_names: Vec<_> = air_group_instances.keys().collect(); + air_names.sort(); + + for air_name in air_names { + let count = air_group_instances.get(air_name).unwrap(); + let (_, _, memory_fixed, memory_helper_instance_size, memory_instance) = + air_info.get(air_name).unwrap(); + let total_memory_instance = memory_fixed + memory_instance * *count as f64; + total_memory += total_memory_instance; + if *memory_helper_instance_size > memory_helper_size { + memory_helper_size = *memory_helper_instance_size; + } + info!( + "{}: {}", + Self::MY_NAME, + format!( + "· {}: {} fixed cols | {} per each of {} instance | Total {}", + air_name, + format_bytes(*memory_fixed), + format_bytes(*memory_instance), + count, + format_bytes(total_memory_instance) + ) + ); + } + } + total_memory += memory_helper_size; + info!("{}: {}", Self::MY_NAME, format!("Extra helper memory: {}", format_bytes(memory_helper_size))); + info!( + "{}: {}", + Self::MY_NAME, + format!("Total prover memory required: {}", format_bytes(total_memory)).bright_white().bold() + ); + info!("{}: ----------------------------------------------------------", Self::MY_NAME); + } + + fn print_summary(pctx: Arc>, sctx: Arc) { let air_instances_repo = pctx.air_instance_repo.air_instances.read().unwrap(); let air_instances_repo = &*air_instances_repo; + let mut air_info = HashMap::new(); + let mut air_instances = HashMap::new(); for air_instance in air_instances_repo.iter() { let air_name = pctx.global_info.airs[air_instance.airgroup_id][air_instance.air_id].clone().name; let air_group_name = pctx.global_info.air_groups[air_instance.airgroup_id].clone(); - let air_instance = air_instances.entry(air_group_name).or_insert_with(HashMap::new); - let air_instance = air_instance.entry(air_name).or_insert(0); - *air_instance += 1; + let air_instance_map = air_instances.entry(air_group_name).or_insert_with(HashMap::new); + if !air_instance_map.contains_key(&air_name.clone()) { + let setup = sctx.get_setup(air_instance.airgroup_id, air_instance.air_id); + let n_bits = setup.stark_info.stark_struct.n_bits; + let memory_instance = get_map_totaln_c(setup.p_setup.p_stark_info) as f64 * 8.0; + let memory_fixed = (setup.stark_info.n_constants * (1 << (setup.stark_info.stark_struct.n_bits)) + + setup.stark_info.n_constants * (1 << (setup.stark_info.stark_struct.n_bits_ext))) + as f64 + * 8.0; + let memory_helpers = setup.stark_info.get_buff_helper_size() as f64 * 8.0; + let total_cols: u64 = setup + .stark_info + .map_sections_n + .iter() + .filter(|(key, _)| *key != "const") + .map(|(_, value)| *value) + .sum(); + air_info.insert(air_name.clone(), (n_bits, total_cols, memory_fixed, memory_helpers, memory_instance)); + } + let air_instance_map_key = air_instance_map.entry(air_name).or_insert(0); + *air_instance_map_key += 1; } let mut air_groups: Vec<_> = air_instances.keys().collect(); @@ -747,7 +773,7 @@ impl ProofMan { info!("{}: --- PROOF INSTANCES SUMMARY ------------------------", Self::MY_NAME); info!("{}: ► {} Air instances found:", Self::MY_NAME, air_instances_repo.len()); - for air_group in air_groups { + for air_group in air_groups.clone() { let air_group_instances = air_instances.get(air_group).unwrap(); let mut air_names: Vec<_> = air_group_instances.keys().collect(); air_names.sort(); @@ -755,17 +781,61 @@ impl ProofMan { info!("{}: Air Group [{}]", Self::MY_NAME, air_group); for air_name in air_names { let count = air_group_instances.get(air_name).unwrap(); - info!("{}: {}", Self::MY_NAME, format!("· {} x Air [{}]", count, air_name).bright_white().bold()); + let (n_bits, total_cols, _, _, _) = air_info.get(air_name).unwrap(); + info!( + "{}: {}", + Self::MY_NAME, + format!("· {} x Air [{}] ({} x 2^{})", count, air_name, total_cols, n_bits).bright_white().bold() + ); } } - info!("{}: --- PROOF INSTANCES SUMMARY ------------------------", Self::MY_NAME); + info!("{}: ------------------------------------------------", Self::MY_NAME); + info!("{}: --- PROVER MEMORY USAGE ------------------------", Self::MY_NAME); + info!("{}: ► {} Air instances found:", Self::MY_NAME, air_instances_repo.len()); + let mut total_memory = 0f64; + let mut memory_helper_size = 0f64; + for air_group in air_groups { + let air_group_instances = air_instances.get(air_group).unwrap(); + let mut air_names: Vec<_> = air_group_instances.keys().collect(); + air_names.sort(); + + for air_name in air_names { + let count = air_group_instances.get(air_name).unwrap(); + let (_, _, memory_fixed, memory_helper_instance_size, memory_instance) = + air_info.get(air_name).unwrap(); + let total_memory_instance = memory_fixed + memory_instance * *count as f64; + total_memory += total_memory_instance; + if *memory_helper_instance_size > memory_helper_size { + memory_helper_size = *memory_helper_instance_size; + } + info!( + "{}: {}", + Self::MY_NAME, + format!( + "· {}: {} fixed cols | {} per each of {} instance | Total {}", + air_name, + format_bytes(*memory_fixed), + format_bytes(*memory_instance), + count, + format_bytes(total_memory_instance) + ) + ); + } + } + total_memory += memory_helper_size; + info!("{}: {}", Self::MY_NAME, format!("Extra helper memory: {}", format_bytes(memory_helper_size))); + info!( + "{}: {}", + Self::MY_NAME, + format!("Total prover memory required: {}", format_bytes(total_memory)).bright_white().bold() + ); + info!("{}: ------------------------------------------------", Self::MY_NAME); } fn check_paths( witness_lib_path: &PathBuf, rom_path: &Option, public_inputs_path: &Option, - cached_buffers_paths: &Option>, proving_key_path: &PathBuf, output_dir_path: &PathBuf, verify_constraints: bool, @@ -789,15 +859,6 @@ impl ProofMan { } } - // Check each path in cached_buffers_paths exists - if let Some(cached_buffers) = cached_buffers_paths { - for (key, path) in cached_buffers { - if !path.exists() { - return Err(format!("Cached buffer not found for key '{}' at path: {:?}", key, path).into()); - } - } - } - // Check proving_key_path exists if !proving_key_path.exists() { return Err(format!("Proving key folder not found at path: {:?}", proving_key_path).into()); diff --git a/proofman/src/recursion.rs b/proofman/src/recursion.rs index c3977b7a..d8df2f51 100644 --- a/proofman/src/recursion.rs +++ b/proofman/src/recursion.rs @@ -7,7 +7,7 @@ use proofman_starks_lib_c::*; use std::path::{Path, PathBuf}; use std::io::Read; -use proofman_common::{ExecutionCtx, ProofCtx, ProofType, Setup, SetupCtx, SetupsVadcop}; +use proofman_common::{ProofCtx, ProofType, Setup, SetupCtx, SetupsVadcop}; use std::os::raw::{c_void, c_char}; @@ -55,9 +55,6 @@ pub fn generate_vadcop_recursive1_proof( let (buffer, publics) = generate_witness::(&setup_path, setup, proofs[prover_idx], 18)?; - let p_publics = publics.as_ptr() as *mut c_void; - let p_address = buffer.as_ptr() as *mut c_void; - log::info!( "{}: {}", MY_NAME, @@ -76,15 +73,12 @@ pub fn generate_vadcop_recursive1_proof( false => String::from(""), }; - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - zkin = gen_recursive_proof_c( p_setup, - p_address, - const_pols_ptr, - const_tree_ptr, - p_publics, + buffer.as_ptr() as *mut u8, + setup.get_const_ptr(), + setup.get_const_tree_ptr(), + publics.as_ptr() as *mut u8, &proof_file, global_info_file, air_instance.airgroup_id as u64, @@ -118,9 +112,6 @@ pub fn generate_vadcop_recursive1_proof( let (buffer, publics) = generate_witness::(&setup_path, setup, zkin, 18)?; - let p_publics = publics.as_ptr() as *mut c_void; - let p_address = buffer.as_ptr() as *mut c_void; - log::info!( "{}: {}", MY_NAME, @@ -139,15 +130,12 @@ pub fn generate_vadcop_recursive1_proof( false => String::from(""), }; - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let p_prove = gen_recursive_proof_c( p_setup, - p_address, - const_pols_ptr, - const_tree_ptr, - p_publics, + buffer.as_ptr() as *mut u8, + setup.get_const_ptr(), + setup.get_const_tree_ptr(), + publics.as_ptr() as *mut u8, &proof_file, global_info_file, air_instance.airgroup_id as u64, @@ -166,7 +154,6 @@ pub fn generate_vadcop_recursive1_proof( pub fn generate_vadcop_recursive2_proof( pctx: &ProofCtx, - _ectx: &ExecutionCtx, sctx: Arc, proofs: &[*mut c_void], output_dir_path: PathBuf, @@ -177,54 +164,43 @@ pub fn generate_vadcop_recursive2_proof( let global_info_path = pctx.global_info.get_proving_key_path().join("pilout.globalInfo.json"); let global_info_file: &str = global_info_path.to_str().unwrap(); - // let mut dctx = ectx.dctx.write().unwrap(); + let mut dctx = pctx.dctx.write().unwrap(); let n_airgroups = pctx.global_info.air_groups.len(); let mut alives = Vec::with_capacity(n_airgroups); let mut airgroup_proofs: Vec>> = Vec::with_capacity(n_airgroups); - // TODO: ACTIVATE DCTX BACK - // let mut null_zkin: Option<*mut c_void> = None; - // let mut zkin_final = std::ptr::null_mut(); + let mut null_zkin: Option<*mut c_void> = None; + let mut zkin_final = std::ptr::null_mut(); // Pre-process data before starting recursion loop for airgroup in 0..n_airgroups { - let setup_path = pctx.global_info.get_air_setup_path(airgroup, 0, &ProofType::Recursive2); - // let instances = &dctx.airgroup_instances[airgroup]; - let instances = pctx.air_instance_repo.find_airgroup_instances(airgroup); + let instances = &dctx.airgroup_instances[airgroup]; airgroup_proofs.push(Vec::with_capacity(instances.len().max(1))); - if instances.is_empty() { - let zkin_file = setup_path.display().to_string() + ".null_zkin.json"; - airgroup_proofs[airgroup].push(Some(get_zkin_ptr_c(&zkin_file))); - } else { + if !instances.is_empty() { for instance in instances.iter() { - airgroup_proofs[airgroup].push(Some(proofs[*instance])); + let local_instance = dctx.glob2loc[*instance]; + let proof = local_instance.map(|idx| proofs[idx]); + airgroup_proofs[airgroup].push(proof); + } + } else { + // If there are no instances, we need to add a null proof (only rank 0) + if dctx.rank == 0 { + if null_zkin.is_none() { + let setup_path = pctx.global_info.get_air_setup_path(airgroup, 0, &ProofType::Recursive2); + let zkin_file = setup_path.display().to_string() + ".null_zkin.json"; + null_zkin = Some(get_zkin_ptr_c(&zkin_file)); + } + airgroup_proofs[airgroup].push(Some(null_zkin.unwrap())); + } else { + airgroup_proofs[airgroup].push(None); } } - // if !instances.is_empty() { - // for instance in instances.iter() { - // let local_instance = dctx.glob2loc[*instance]; - // let proof = local_instance.map(|idx| proofs[idx]); - // airgroup_proofs[airgroup].push(proof); - // } - // } else { - // If there are no instances, we need to add a null proof (only rank 0) - // if dctx.rank == 0 { - // if null_zkin.is_none() { - // let setup_path = pctx.global_info.get_air_setup_path(airgroup, 0, &ProofType::Recursive2); - // let zkin_file = setup_path.display().to_string() + ".null_zkin.json"; - // null_zkin = Some(get_zkin_ptr_c(&zkin_file)); - // } - // airgroup_proofs[airgroup].push(Some(null_zkin.unwrap())); - // } else { - // airgroup_proofs[airgroup].push(None); - // } - // } alives.push(airgroup_proofs[airgroup].len()); } // agregation loop loop { - // dctx.barrier(); - // dctx.distribute_recursive2_proofs(&alives, &mut airgroup_proofs); + dctx.barrier(); + dctx.distribute_recursive2_proofs(&alives, &mut airgroup_proofs); let mut pending_agregations = false; for airgroup in 0..n_airgroups { //create a vector of sice indices length @@ -243,16 +219,10 @@ pub fn generate_vadcop_recursive2_proof( let p_setup: *mut c_void = (&setup.p_setup).into(); let p_stark_info: *mut c_void = setup.p_setup.p_stark_info; - let public_inputs_guard = pctx.public_inputs.inputs.read().unwrap(); - let challenges_guard = pctx.challenges.challenges.read().unwrap(); - - let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void; - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - let zkin_recursive2 = join_zkin_recursive2_c( airgroup as u64, - public_inputs, - challenges, + pctx.get_publics_ptr(), + pctx.get_challenges_ptr(), global_info_file, airgroup_proofs[airgroup][j].unwrap(), airgroup_proofs[airgroup][j + 1].unwrap(), @@ -271,8 +241,6 @@ pub fn generate_vadcop_recursive2_proof( let setup_path = pctx.global_info.get_air_setup_path(airgroup, 0, &ProofType::Recursive2); let (buffer, publics) = generate_witness::(&setup_path, setup, zkin_recursive2_updated, 18)?; - let p_publics = publics.as_ptr() as *mut c_void; - let p_address = buffer.as_ptr() as *mut c_void; timer_start_trace!(GENERATE_RECURSIVE2_PROOF); let proof_file = match save_proof { @@ -295,15 +263,13 @@ pub fn generate_vadcop_recursive2_proof( MY_NAME, format!("··· Generating recursive2 proof for instances of {}", air_instance_name) ); - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; let zkin = gen_recursive_proof_c( p_setup, - p_address, - const_pols_ptr, - const_tree_ptr, - p_publics, + buffer.as_ptr() as *mut u8, + setup.get_const_ptr(), + setup.get_const_tree_ptr(), + publics.as_ptr() as *mut u8, &proof_file, global_info_file, airgroup as u64, @@ -333,37 +299,31 @@ pub fn generate_vadcop_recursive2_proof( break; } } - // if dctx.rank == 0 { - let mut proofs_recursive2: Vec<*mut c_void> = Vec::with_capacity(n_airgroups); - for proofs in airgroup_proofs { - proofs_recursive2.push(proofs[0].unwrap()); - } - let public_inputs_guard = pctx.public_inputs.inputs.read().unwrap(); - let challenges_guard = pctx.challenges.challenges.read().unwrap(); - let proof_values_guard = pctx.proof_values.values.read().unwrap(); - let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void; - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; + if dctx.rank == 0 { + let mut proofs_recursive2: Vec<*mut c_void> = Vec::with_capacity(n_airgroups); + for proofs in airgroup_proofs { + proofs_recursive2.push(proofs[0].unwrap()); + } - let mut stark_infos_recursive2 = Vec::new(); - for (idx, _) in pctx.global_info.air_groups.iter().enumerate() { - stark_infos_recursive2.push(sctx.get_setup(idx, 0).p_setup.p_stark_info); - } + let mut stark_infos_recursive2 = Vec::new(); + for (idx, _) in pctx.global_info.air_groups.iter().enumerate() { + stark_infos_recursive2.push(sctx.get_setup(idx, 0).p_setup.p_stark_info); + } - let proofs_recursive2_ptr = proofs_recursive2.as_mut_ptr(); + let proofs_recursive2_ptr = proofs_recursive2.as_mut_ptr(); - let stark_infos_recursive2_ptr = stark_infos_recursive2.as_mut_ptr(); + let stark_infos_recursive2_ptr = stark_infos_recursive2.as_mut_ptr(); - let zkin_final = join_zkin_final_c( - public_inputs, - proof_values, - challenges, - global_info_file, - proofs_recursive2_ptr, - stark_infos_recursive2_ptr, - ); - // } + zkin_final = join_zkin_final_c( + pctx.get_publics_ptr(), + pctx.get_proof_values_ptr(), + pctx.get_challenges_ptr(), + global_info_file, + proofs_recursive2_ptr, + stark_infos_recursive2_ptr, + ); + } Ok(zkin_final) } @@ -384,20 +344,16 @@ pub fn generate_vadcop_final_proof( let setup_path = pctx.global_info.get_setup_path("vadcop_final"); let (buffer, publics) = generate_witness::(&setup_path, &setup, proof, 18)?; - let p_address = buffer.as_ptr() as *mut c_void; - let p_publics = publics.as_ptr() as *mut c_void; log::info!("{}: ··· Generating vadcop final proof", MY_NAME); timer_start_trace!(GENERATE_VADCOP_FINAL_PROOF); // prove - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; let p_prove = gen_recursive_proof_c( p_setup, - p_address, - const_pols_ptr, - const_tree_ptr, - p_publics, + buffer.as_ptr() as *mut u8, + setup.get_const_ptr(), + setup.get_const_tree_ptr(), + publics.as_ptr() as *mut u8, output_dir_path.join("proofs/vadcop_final_proof.json").to_string_lossy().as_ref(), global_info_file, 0, @@ -426,20 +382,16 @@ pub fn generate_recursivef_proof( let setup_path = pctx.global_info.get_setup_path("recursivef"); let (buffer, publics) = generate_witness::(&setup_path, &setup, proof, 12)?; - let p_address = buffer.as_ptr() as *mut c_void; - let p_publics = publics.as_ptr() as *mut c_void; log::info!("{}: ··· Generating recursiveF proof", MY_NAME); timer_start_trace!(GENERATE_RECURSIVEF_PROOF); // prove - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; let p_prove = gen_recursive_proof_c( p_setup, - p_address, - const_pols_ptr, - const_tree_ptr, - p_publics, + buffer.as_ptr() as *mut u8, + setup.get_const_ptr(), + setup.get_const_tree_ptr(), + publics.as_ptr() as *mut u8, output_dir_path.join("proofs/recursivef.json").to_string_lossy().as_ref(), global_info_file, 0, @@ -480,12 +432,12 @@ pub fn generate_fflonk_snark_proof( let size_witness = get_size_witness(); let witness: Vec = create_buffer_fast((size_witness * 32) as usize); - let witness_ptr = witness.as_ptr() as *mut c_void; + let witness_ptr = witness.as_ptr() as *mut u8; let get_witness: Symbol = library.get(b"getWitness\0")?; let nmutex = 128; - get_witness(proof, dat_filename_ptr, witness_ptr, nmutex); + get_witness(proof, dat_filename_ptr, witness_ptr as *mut c_void, nmutex); timer_stop_and_log_trace!(CALCULATE_FINAL_WITNESS); @@ -553,21 +505,20 @@ fn generate_witness( let n = 1 << (setup.stark_info.stark_struct.n_bits); let buffer = create_buffer_fast(n_cols * n); - let p_address = buffer.as_ptr() as *mut c_void; + let p_address = buffer.as_ptr() as *mut u8; let n_publics = setup.stark_info.n_publics as usize; let publics = create_buffer_fast(n_publics); - let p_publics = publics.as_ptr() as *mut c_void; + let p_publics = publics.as_ptr() as *mut u8; get_committed_pols_c( - witness_ptr, + witness.as_ptr() as *mut u8, exec_filename_ptr, p_address, p_publics, size_witness, n as u64, n_publics as u64, - 0, n_cols as u64, ); diff --git a/proofman/src/verify_constraints.rs b/proofman/src/verify_constraints.rs new file mode 100644 index 00000000..af21811a --- /dev/null +++ b/proofman/src/verify_constraints.rs @@ -0,0 +1,255 @@ +use p3_field::Field; +use proofman_hints::aggregate_airgroupvals; +use proofman_starks_lib_c::{get_n_global_constraints_c, verify_global_constraints_c}; +use std::cmp; + +use std::sync::Arc; + +use proofman_common::{ + get_constraints_lines_str, get_global_constraints_lines_str, GlobalConstraintInfo, ProofCtx, Prover, SetupCtx, +}; +use std::os::raw::c_void; + +use colored::*; + +pub fn verify_global_constraints_proof( + pctx: Arc>, + sctx: Arc, + mut airgroupvalues: Vec>, +) -> Vec { + const MY_NAME: &str = "GlCstVfy"; + + log::info!("{}: --> Checking global constraints", MY_NAME); + + let mut airgroup_values_ptrs: Vec<*mut F> = airgroupvalues + .iter_mut() // Iterate mutably over the inner Vecs + .map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec + .collect(); + + let n_global_constraints = get_n_global_constraints_c(sctx.get_global_bin()); + let mut global_constraints_info = vec![GlobalConstraintInfo::default(); n_global_constraints as usize]; + + if !pctx.options.debug_info.debug_global_instances.is_empty() { + global_constraints_info.iter_mut().for_each(|constraint| constraint.skip = true); + for constraint_id in &pctx.options.debug_info.debug_global_instances { + global_constraints_info[*constraint_id].skip = false; + } + } + verify_global_constraints_c( + sctx.get_global_info_file().as_str(), + sctx.get_global_bin(), + pctx.get_publics_ptr(), + pctx.get_challenges_ptr(), + pctx.get_proof_values_ptr(), + airgroup_values_ptrs.as_mut_ptr() as *mut *mut u8, + global_constraints_info.as_mut_ptr() as *mut c_void, + ); + + global_constraints_info +} + +pub fn verify_constraints_proof( + pctx: Arc>, + sctx: Arc, + provers: &mut [Box>], +) -> Result<(), Box> { + const MY_NAME: &str = "CstrVrfy"; + + log::info!("{}: --> Checking constraints", MY_NAME); + + let mut constraints = Vec::new(); + for prover in provers.iter() { + let constraints_prover_info = prover.verify_constraints(sctx.clone(), pctx.clone()); + constraints.push(constraints_prover_info); + } + + let mut valid_constraints = true; + + for (idx, prover) in provers.iter().enumerate() { + let prover_info = prover.get_prover_info(); + let (airgroup_id, air_id, air_instance_id) = + (prover_info.airgroup_id, prover_info.air_id, prover_info.instance_id); + + let air_name = &pctx.global_info.airs[airgroup_id][air_id].name; + + let constraints_lines = get_constraints_lines_str(sctx.clone(), airgroup_id, air_id); + + let mut valid_constraints_prover = true; + log::info!("{}: ► Instance #{} of {} [{}:{}]", MY_NAME, air_instance_id, air_name, airgroup_id, air_id,); + for constraint in &constraints[idx] { + if constraint.skip { + log::debug!( + "{}: · Skipping Constraint #{} (stage {}) -> {}", + MY_NAME, + constraint.id, + constraint.stage, + constraints_lines[constraint.id as usize] + ); + continue; + } + let valid = if constraint.n_rows > 0 { + format!("has {} invalid rows", constraint.n_rows).bright_red() + } else { + "is valid".bright_green() + }; + if constraint.im_pol { + log::trace!( + "{}: ··· Intermediate polynomial (stage {}) {} -> {}", + MY_NAME, + constraint.stage, + valid, + constraints_lines[constraint.id as usize] + ); + } else if constraint.n_rows == 0 { + log::debug!( + "{}: · Constraint #{} (stage {}) {} -> {}", + MY_NAME, + constraint.id, + constraint.stage, + valid, + constraints_lines[constraint.id as usize] + ); + } else { + log::info!( + "{}: · Constraint #{} (stage {}) {} -> {}", + MY_NAME, + constraint.id, + constraint.stage, + valid, + constraints_lines[constraint.id as usize] + ); + } + if constraint.n_rows > 0 { + valid_constraints_prover = false; + } + let n_rows = cmp::min(constraint.n_rows, 10); + for i in 0..n_rows { + let row = constraint.rows[i as usize]; + if row.dim == 1 { + log::info!( + "{}: ··· \u{2717} Failed at row {} with value: {}", + MY_NAME, + row.row, + row.value[0] + ); + } else { + log::info!( + "{}: ··· \u{2717} Failed at row {} with value: [{}, {}, {}]", + MY_NAME, + row.row, + row.value[0], + row.value[1], + row.value[2] + ); + } + } + } + + if !valid_constraints_prover { + log::info!( + "{}: ··· {}", + MY_NAME, + format!("\u{2717} Not all constraints for Instance #{} of {} were verified", air_instance_id, air_name) + .bright_red() + .bold() + ); + } else { + log::info!( + "{}: {}", + MY_NAME, + format!("\u{2713} All constraints for Instance #{} of {} were verified", air_instance_id, air_name) + .bright_green() + .bold() + ); + } + + if !valid_constraints_prover { + valid_constraints = false; + } + } + + let dctx = pctx.dctx.read().unwrap(); + + let airgroupvalues = aggregate_airgroupvals(pctx.clone()); + + if dctx.rank == 0 && pctx.options.debug_info.debug_instances.is_empty() { + // TODO: Distribute airgroupvalues + + let global_constraints = verify_global_constraints_proof(pctx.clone(), sctx.clone(), airgroupvalues); + let mut valid_global_constraints = true; + + let global_constraints_lines = get_global_constraints_lines_str(sctx.clone()); + + for idx in 0..global_constraints.len() { + let constraint = global_constraints[idx]; + let line_str = &global_constraints_lines[idx]; + + if constraint.skip { + log::debug!("{}: · Skipping Global Constraint #{} -> {}", MY_NAME, idx, line_str,); + continue; + } + + let valid = if !constraint.valid { "is invalid".bright_red() } else { "is valid".bright_green() }; + if constraint.valid { + log::debug!("{}: · Global Constraint #{} {} -> {}", MY_NAME, constraint.id, valid, line_str); + } else { + log::info!("{}: · Global Constraint #{} {} -> {}", MY_NAME, constraint.id, valid, line_str); + } + if !constraint.valid { + valid_global_constraints = false; + if constraint.dim == 1 { + log::info!("{}: ··· \u{2717} Failed with value: {}", MY_NAME, constraint.value[0]); + } else { + log::info!( + "{}: ··· \u{2717} Failed with value: [{}, {}, {}]", + MY_NAME, + constraint.value[0], + constraint.value[1], + constraint.value[2] + ); + } + } + } + + if valid_global_constraints { + log::info!( + "{}: ··· {}", + MY_NAME, + "\u{2713} All global constraints were successfully verified".bright_green().bold() + ); + } else { + log::info!("{}: ··· {}", MY_NAME, "\u{2717} Not all global constraints were verified".bright_red().bold()); + } + + if valid_constraints && valid_global_constraints { + log::info!("{}: ··· {}", MY_NAME, "\u{2713} All constraints were verified".bright_green().bold()); + Ok(()) + } else { + log::info!("{}: ··· {}", MY_NAME, "\u{2717} Not all constraints were verified.".bright_red().bold()); + Err(Box::new(std::io::Error::new( + // <-- Return a boxed error + std::io::ErrorKind::Other, + format!("{}: Not all constraints were verified.", MY_NAME), + ))) + } + } else { + if !pctx.options.debug_info.debug_instances.is_empty() { + log::info!( + "{}: ··· {}", + MY_NAME, + "\u{2713} Skipping global constraints verification".bright_yellow().bold() + ); + } + if valid_constraints { + log::info!("{}: ··· {}", MY_NAME, "\u{2713} All constraints were verified".bright_green().bold()); + Ok(()) + } else { + log::info!("{}: ··· {}", MY_NAME, "\u{2717} Not all constraints were verified.".bright_red().bold()); + Err(Box::new(std::io::Error::new( + // <-- Return a boxed error + std::io::ErrorKind::Other, + format!("{}: Not all constraints were verified.", MY_NAME), + ))) + } + } +} diff --git a/proofman/src/witness_component.rs b/proofman/src/witness_component.rs deleted file mode 100644 index 97694446..00000000 --- a/proofman/src/witness_component.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::sync::Arc; - -use proofman_common::{ExecutionCtx, ProofCtx, SetupCtx}; - -pub trait WitnessComponent: Send + Sync { - fn start_proof(&self, _pctx: Arc>, _ectx: Arc, _sctx: Arc) {} - - fn calculate_witness( - &self, - _stage: u32, - _air_instance: Option, - _pctx: Arc>, - _ectx: Arc, - _sctx: Arc, - ) { - } - - fn end_proof(&self) {} -} diff --git a/proofman/src/witness_executor.rs b/proofman/src/witness_executor.rs deleted file mode 100644 index 7ac119dc..00000000 --- a/proofman/src/witness_executor.rs +++ /dev/null @@ -1,5 +0,0 @@ -use proofman_common::{ExecutionCtx, ProofCtx}; - -pub trait WitnessExecutor { - fn execute(&self, pctx: &mut ProofCtx, ectx: &mut ExecutionCtx); -} diff --git a/proofman/src/witness_library.rs b/proofman/src/witness_library.rs deleted file mode 100644 index d56f7477..00000000 --- a/proofman/src/witness_library.rs +++ /dev/null @@ -1,21 +0,0 @@ -use std::{error::Error, path::PathBuf, sync::Arc}; - -use proofman_common::{ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; - -/// This is the type of the function that is used to load a witness library. -pub type WitnessLibInitFn = - fn(Option, Option, VerboseMode) -> Result>, Box>; - -pub trait WitnessLibrary { - fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc); - - fn end_proof(&mut self); - - fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc); - - fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc); - - fn debug(&mut self, _pctx: Arc>, _ectx: Arc, _sctx: Arc) {} - - fn pilout(&self) -> WitnessPilout; -} diff --git a/proofman/src/witness_manager.rs b/proofman/src/witness_manager.rs deleted file mode 100644 index f28ccf81..00000000 --- a/proofman/src/witness_manager.rs +++ /dev/null @@ -1,126 +0,0 @@ -use std::{ - collections::HashMap, - sync::{Arc, RwLock}, -}; - -use proofman_common::{ExecutionCtx, ProofCtx, SetupCtx}; -use proofman_util::{timer_start_debug, timer_stop_and_log_debug}; -use crate::WitnessComponent; - -type AirGroupId = usize; -type AirId = usize; - -pub struct WitnessManager { - components: RwLock>>>, - airs: RwLock>, // First usize is the air_id, second usize is the index of the component in the components vector - - pctx: Arc>, - ectx: Arc, - sctx: Arc, -} - -impl WitnessManager { - const MY_NAME: &'static str = "WCMnager"; - - pub fn new(pctx: Arc>, ectx: Arc, sctx: Arc) -> Self { - WitnessManager { components: RwLock::new(Vec::new()), airs: RwLock::new(HashMap::new()), pctx, ectx, sctx } - } - - pub fn register_component( - &self, - component: Arc>, - airgroup_id: Option, - air_ids: Option<&[AirId]>, - ) { - self.components.write().unwrap().push(component); - - let idx = self.components.write().unwrap().len() - 1; - - if let Some(air_ids) = air_ids { - self.register_airs(airgroup_id.unwrap(), air_ids, idx); - } - } - - pub fn register_airs(&self, airgroup_id: AirGroupId, air_ids: &[AirId], component_idx: usize) { - for air_id in air_ids.iter() { - self.register_air(airgroup_id, *air_id, component_idx); - } - } - - pub fn register_air(&self, airgroup_id: AirGroupId, air_id: AirId, component_idx: usize) { - if self.airs.read().unwrap().contains_key(&(airgroup_id, air_id)) { - panic!("{}: AirGroup ID + Air ID ({},{}) already registered", Self::MY_NAME, airgroup_id, air_id); - } - - self.airs.write().unwrap().insert((airgroup_id, air_id), component_idx); - } - - pub fn start_proof(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { - for component in self.components.read().unwrap().iter() { - component.start_proof(pctx.clone(), ectx.clone(), sctx.clone()); - } - } - - pub fn end_proof(&self) { - for component in self.components.read().unwrap().iter() { - component.end_proof(); - } - } - - pub fn calculate_witness(&self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { - log::info!( - "{}: Calculating witness for stage {} / {}", - Self::MY_NAME, - stage, - pctx.global_info.n_challenges.len() - ); - - timer_start_debug!(CALCULATING_WITNESS); - - let air_instances = pctx.air_instance_repo.air_instances.read().unwrap(); - - let mut components = HashMap::new(); - let airs = self.airs.read().unwrap(); - for (air_instance_id, air_instance) in air_instances.iter().enumerate() { - let component = airs.get(&(air_instance.airgroup_id, air_instance.air_id)).unwrap(); - - components - .entry((air_instance.airgroup_id, air_instance.air_id)) - .or_insert_with(Vec::new) - .push((component, air_instance_id)); - } - drop(air_instances); - - // Call all used components - let mut used_components = Vec::new(); - let self_components = self.components.read().unwrap(); - for component_group in components.values() { - for (component_idx, id) in component_group.iter() { - let component = &self_components[**component_idx]; - component.calculate_witness(stage, Some(*id), pctx.clone(), ectx.clone(), sctx.clone()); - used_components.push(**component_idx); - } - } - - // Call one time all unused components - for component_idx in 0..self.components.read().unwrap().len() { - if !used_components.contains(&component_idx) { - self_components[component_idx].calculate_witness(stage, None, pctx.clone(), ectx.clone(), sctx.clone()); - } - } - - timer_stop_and_log_debug!(CALCULATING_WITNESS); - } - - pub fn get_pctx(&self) -> Arc> { - self.pctx.clone() - } - - pub fn get_ectx(&self) -> Arc { - self.ectx.clone() - } - - pub fn get_sctx(&self) -> Arc { - self.sctx.clone() - } -} diff --git a/provers/stark/Cargo.toml b/provers/stark/Cargo.toml index a1a4138f..94345121 100644 --- a/provers/stark/Cargo.toml +++ b/provers/stark/Cargo.toml @@ -13,7 +13,6 @@ proofman-util = { path = "../../util" } proofman-starks-lib-c = { path = "../starks-lib-c" } transcript = { path = "../../transcript" } proofman-common = { path = "../../common" } -proofman-hints = { path = "../../hints" } log.workspace = true p3-goldilocks.workspace = true diff --git a/provers/stark/src/stark_prover.rs b/provers/stark/src/stark_prover.rs index 970bf174..158a891c 100644 --- a/provers/stark/src/stark_prover.rs +++ b/provers/stark/src/stark_prover.rs @@ -1,15 +1,12 @@ use core::panic; -use std::error::Error; use std::fs::File; use std::io::Read; -use std::path::PathBuf; use std::any::type_name; use std::sync::Arc; use proofman_common::{ - BufferAllocator, ConstraintInfo, ConstraintsResults, ProofCtx, ProofType, Prover, ProverInfo, ProverStatus, - StepsParams, SetupCtx, StarkInfo, + ConstraintInfo, ProofCtx, ProofType, Prover, ProverInfo, ProverStatus, StepsParams, SetupCtx, StarkInfo, }; use log::{debug, trace}; use transcript::FFITranscript; @@ -23,15 +20,8 @@ use p3_field::PrimeField64; use std::os::raw::c_void; use std::marker::PhantomData; -#[repr(C)] -pub struct VecU64Result { - pub n_elements: u64, - pub ids: *mut u64, -} - #[allow(dead_code)] pub struct StarkProver { - initialized: bool, prover_idx: usize, air_id: usize, airgroup_id: usize, @@ -43,6 +33,7 @@ pub struct StarkProver { merkle_tree_arity: u64, merkle_tree_custom: bool, p_proof: *mut c_void, + constraints_to_check: Vec, _marker: PhantomData, // Add PhantomData to track the type F } @@ -52,12 +43,17 @@ impl StarkProver { const HASH_SIZE: usize = 4; const FIELD_EXTENSION: usize = 3; - pub fn new(sctx: Arc, airgroup_id: usize, air_id: usize, instance_id: usize, prover_idx: usize) -> Self { + pub fn new( + sctx: Arc, + airgroup_id: usize, + air_id: usize, + instance_id: usize, + prover_idx: usize, + constraints_to_check: Vec, + ) -> Self { let setup = sctx.get_setup(airgroup_id, air_id); - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - - let p_stark = starks_new_c((&setup.p_setup).into(), const_tree_ptr); + let p_stark = starks_new_c((&setup.p_setup).into(), setup.get_const_tree_ptr()); let stark_info = setup.stark_info.clone(); @@ -73,7 +69,6 @@ impl StarkProver { let p_proof = fri_proof_new_c((&setup.p_setup).into()); Self { - initialized: true, prover_idx, air_id, airgroup_id, @@ -85,6 +80,7 @@ impl StarkProver { n_field_elements, merkle_tree_arity, merkle_tree_custom, + constraints_to_check, _marker: PhantomData, } } @@ -93,21 +89,37 @@ impl StarkProver { impl Prover for StarkProver { fn build(&mut self, proof_ctx: Arc>) { let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; + air_instance.init_aux_trace(get_map_totaln_c(self.p_stark_info) as usize); + air_instance.init_evals(self.stark_info.ev_map.len() * Self::FIELD_EXTENSION); + + let n_custom_commits = self.stark_info.custom_commits.len(); - let buffer = Vec::with_capacity(get_map_totaln_c(self.p_stark_info) as usize); - air_instance.set_buffer(buffer); + for commit_id in 0..n_custom_commits { + let n_cols = *self + .stark_info + .map_sections_n + .get(&(self.stark_info.custom_commits[commit_id].name.clone() + "0")) + .unwrap() as usize; + + if air_instance.custom_commits[commit_id].is_empty() { + air_instance.init_custom_commit(commit_id, (1 << self.stark_info.stark_struct.n_bits_ext) * n_cols); + } + air_instance + .init_custom_commit_extended(commit_id, (1 << self.stark_info.stark_struct.n_bits_ext) * n_cols); + } + + let n_airgroup_values = self.stark_info.airgroupvalues_map.as_ref().unwrap().len(); + let n_air_values = self.stark_info.airvalues_map.as_ref().unwrap().len(); - //initialize the common challenges if have not been initialized by another prover - let challenges = - vec![F::zero(); self.stark_info.challenges_map.as_ref().unwrap().len() * Self::FIELD_EXTENSION]; - *proof_ctx.challenges.challenges.write().unwrap() = challenges; + if n_air_values > 0 && air_instance.airvalues.is_empty() { + air_instance.init_airvalues(n_air_values * Self::FIELD_EXTENSION); + } - let number_stage1_commits = *self.stark_info.map_sections_n.get("cm1").unwrap() as usize; - for i in 0..number_stage1_commits { - air_instance.set_commit_calculated(i); + if n_airgroup_values > 0 && air_instance.airgroup_values.is_empty() { + air_instance.init_airgroup_values(n_airgroup_values * Self::FIELD_EXTENSION); } - self.initialized = true; + air_instance.set_prover_initialized(); } fn free(&mut self) { @@ -132,64 +144,59 @@ impl Prover for StarkProver { let setup = setup_ctx.get_setup(self.airgroup_id, self.air_id); - let public_inputs_guard = proof_ctx.public_inputs.inputs.read().unwrap(); - let proof_values_guard = proof_ctx.proof_values.values.read().unwrap(); - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: (*public_inputs_guard).as_ptr() as *mut c_void, - proof_values: (*proof_values_guard).as_ptr() as *mut c_void, - challenges: (*challenges_guard).as_ptr() as *mut c_void, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + p_const_pols: setup.get_const_ptr(), + p_const_tree: std::ptr::null_mut(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: [std::ptr::null_mut(); 10], }; - let raw_ptr = verify_constraints_c((&setup.p_setup).into(), (&steps_params).into()); + let p_setup = (&setup.p_setup).into(); + + let n_constraints = get_n_constraints_c(p_setup); - unsafe { - let constraints_result = Box::from_raw(raw_ptr as *mut ConstraintsResults); - std::slice::from_raw_parts(constraints_result.constraints_info, constraints_result.n_constraints as usize) + let mut constraints_info = vec![ConstraintInfo::default(); n_constraints as usize]; + + if !self.constraints_to_check.is_empty() { + constraints_info.iter_mut().for_each(|constraint| constraint.skip = true); + for constraint_id in &self.constraints_to_check { + constraints_info[*constraint_id].skip = false; + } } - .to_vec() + + verify_constraints_c(p_setup, (&steps_params).into(), constraints_info.as_mut_ptr() as *mut c_void); + + constraints_info } fn calculate_stage(&mut self, stage_id: u32, setup_ctx: Arc, proof_ctx: Arc>) { let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let n_commits = self.stark_info.cm_pols_map.as_ref().expect("REASON").len(); - let setup = setup_ctx.get_setup(self.airgroup_id, self.air_id); - let public_inputs_guard = proof_ctx.public_inputs.inputs.read().unwrap(); - let proof_values_guard = proof_ctx.proof_values.values.read().unwrap(); - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { - trace: air_instance.get_trace_ptr() as *mut c_void, - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: (*public_inputs_guard).as_ptr() as *mut c_void, - proof_values: (*proof_values_guard).as_ptr() as *mut c_void, - challenges: (*challenges_guard).as_ptr() as *mut c_void, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, + trace: air_instance.get_trace_ptr(), + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; if stage_id as usize <= proof_ctx.global_info.n_challenges.len() { @@ -208,22 +215,8 @@ impl Prover for StarkProver { self.instance_id, air_name ); - for i in 0..n_commits { - let cm_pol = self.stark_info.cm_pols_map.as_ref().expect("REASON").get(i).unwrap(); - if (cm_pol.stage < stage_id as u64 || cm_pol.stage == stage_id as u64 && !cm_pol.im_pol) - && !air_instance.commits_calculated.contains_key(&i) - { - panic!("Intermediate polynomials for stage {} cannot be calculated: Witness column {} is not calculated", stage_id, cm_pol.name); - } - } calculate_impols_expressions_c(self.p_stark, stage_id as u64, (&steps_params).into()); - for i in 0..n_commits { - let cm_pol = self.stark_info.cm_pols_map.as_ref().expect("REASON").get(i).unwrap(); - if cm_pol.stage == stage_id as u64 && cm_pol.im_pol { - air_instance.set_commit_calculated(i); - } - } } if stage_id as usize == proof_ctx.global_info.n_challenges.len() { @@ -240,70 +233,19 @@ impl Prover for StarkProver { air_name ); calculate_quotient_polynomial_c(self.p_stark, (&steps_params).into()); - for i in 0..n_commits { - let cm_pol = self.stark_info.cm_pols_map.as_ref().expect("REASON").get(i).unwrap(); - if cm_pol.stage == (proof_ctx.global_info.n_challenges.len() + 1) as u64 { - air_instance.set_commit_calculated(i); - } - } } } - fn check_stage(&self, stage_id: u32, proof_ctx: Arc>) { + fn commit_stage(&mut self, stage_id: u32, proof_ctx: Arc>) -> ProverStatus { let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let n_commits = self.stark_info.cm_pols_map.as_ref().expect("REASON").len(); - for i in 0..n_commits { - let cm_pol = self.stark_info.cm_pols_map.as_ref().expect("REASON").get(i).unwrap(); - if cm_pol.stage == stage_id as u64 && !air_instance.commits_calculated.contains_key(&i) { - panic!("Stage {} cannot be committed: Witness column {} is not calculated", stage_id, cm_pol.name); - } - } - - let n_airgroupvalues = self.stark_info.airgroupvalues_map.as_ref().expect("REASON").len(); - for i in 0..n_airgroupvalues { - let airgroup_value = self.stark_info.airgroupvalues_map.as_ref().expect("REASON").get(i).unwrap(); - if airgroup_value.stage == stage_id as u64 && !air_instance.airgroupvalue_calculated.contains_key(&i) { - panic!( - "Stage {} cannot be committed: Airgroupvalue {} is not calculated", - stage_id, airgroup_value.name - ); - } + if stage_id == self.num_stages() { + air_instance.clear_trace(); } - let n_airvalues = self.stark_info.airvalues_map.as_ref().expect("REASON").len(); - for i in 0..n_airvalues { - let air_value = self.stark_info.airvalues_map.as_ref().expect("REASON").get(i).unwrap(); - - if air_value.stage == stage_id as u64 && !air_instance.airvalue_calculated.contains_key(&i) { - panic!("Stage {} cannot be committed: Airvalue {} is not calculated", stage_id, air_value.name); - } - } - - let n_custom_commits = self.stark_info.custom_commits_map.len(); - for i in 0..n_custom_commits { - let n_custom_commits = self.stark_info.custom_commits_map[i].as_ref().expect("REASON").len(); - for j in 0..n_custom_commits { - let custom_pol = self.stark_info.custom_commits_map[i].as_ref().expect("REASON").get(j).unwrap(); - if stage_id as u64 == custom_pol.stage && !air_instance.custom_commits_calculated[i].contains_key(&j) { - panic!( - "Stage {} cannot be committed: Custom commit of {} that is {} is not calculated", - stage_id, self.stark_info.custom_commits[i].name, custom_pol.name - ); - } - } - } - } - - fn commit_stage(&mut self, stage_id: u32, proof_ctx: Arc>) -> ProverStatus { - let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let p_stark = self.p_stark; let p_proof = self.p_proof; - let buff_helper_guard = proof_ctx.buff_helper.buff_helper.read().unwrap(); - let buff_helper = (*buff_helper_guard).as_ptr() as *mut c_void; - let air_name = &proof_ctx.global_info.airs[self.airgroup_id][self.air_id].name; if stage_id >= 1 { debug!( @@ -316,15 +258,23 @@ impl Prover for StarkProver { timer_start_trace!(STARK_COMMIT_STAGE_, stage_id); - let trace = match stage_id == 1 { - true => air_instance.get_trace_ptr() as *mut c_void, + let witness = match stage_id == 1 { + true => air_instance.get_trace_ptr(), false => std::ptr::null_mut(), }; - let buffer = air_instance.get_buffer_ptr() as *mut c_void; let element_type = if type_name::() == type_name::() { 1 } else { 0 }; - commit_stage_c(p_stark, element_type, stage_id as u64, trace, buffer, p_proof, buff_helper); + commit_stage_c( + p_stark, + element_type, + stage_id as u64, + witness, + air_instance.get_aux_trace_ptr(), + p_proof, + proof_ctx.get_buff_helper_ptr(), + ); + timer_stop_and_log_trace!(STARK_COMMIT_STAGE_, stage_id); } else { let n_custom_commits = self.stark_info.custom_commits.len(); @@ -336,33 +286,23 @@ impl Prover for StarkProver { .any(|custom_commit| custom_commit.stage == stage_id as u64); if custom_commits_stage { - if air_instance.custom_commits[commit_id].cached_file.to_str() == Some("") { - extend_and_merkelize_custom_commit_c( - p_stark, - commit_id as u64, - stage_id as u64, - air_instance.custom_commits[commit_id].buffer.as_ptr() as *mut c_void, - p_proof, - buff_helper, - "", - ); - } else { - load_custom_commit_c( - p_stark, - commit_id as u64, - stage_id as u64, - air_instance.custom_commits[commit_id].buffer.as_ptr() as *mut c_void, - p_proof, - air_instance.custom_commits[commit_id].cached_file.to_str().unwrap(), - ); - } + extend_and_merkelize_custom_commit_c( + p_stark, + commit_id as u64, + stage_id as u64, + air_instance.custom_commits[commit_id].as_ptr() as *mut u8, + air_instance.custom_commits_extended[commit_id].as_ptr() as *mut u8, + p_proof, + proof_ctx.get_buff_helper_ptr(), + "", + ); } - let mut value = vec![Goldilocks::zero(); self.n_field_elements]; + let mut value = vec![F::zero(); self.n_field_elements]; treesGL_get_root_c( p_stark, (self.stark_info.n_stages + 2 + commit_id as u32) as u64, - value.as_mut_ptr() as *mut c_void, + value.as_mut_ptr() as *mut u8, ); if !self.stark_info.custom_commits[commit_id].public_values.is_empty() { assert!( @@ -371,8 +311,8 @@ impl Prover for StarkProver { ); for (idx, val) in value.iter().enumerate() { proof_ctx.set_public_value( - val.as_canonical_u64(), - self.stark_info.custom_commits[commit_id].public_values[idx].idx, + *val, + self.stark_info.custom_commits[commit_id].public_values[idx].idx as usize, ); } } @@ -422,10 +362,7 @@ impl Prover for StarkProver { } fn calculate_xdivxsub(&mut self, proof_ctx: Arc>) { - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - - let buff_helper_guard = proof_ctx.buff_helper.buff_helper.read().unwrap(); - let xdivxsub = (*buff_helper_guard).as_ptr() as *mut c_void; + let challenges_guard = proof_ctx.challenges.values.read().unwrap(); let challenges_map = self.stark_info.challenges_map.as_ref().unwrap(); @@ -438,14 +375,11 @@ impl Prover for StarkProver { } let xi_challenge = &(*challenges_guard)[xi_challenge_index * Self::FIELD_EXTENSION] as *const F as *mut c_void; - calculate_xdivxsub_c(self.p_stark, xi_challenge, xdivxsub); + calculate_xdivxsub_c(self.p_stark, xi_challenge, proof_ctx.get_buff_helper_ptr()); } fn calculate_lev(&mut self, proof_ctx: Arc>) { - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - - let buff_helper_guard = proof_ctx.buff_helper.buff_helper.read().unwrap(); - let lev = (*buff_helper_guard).as_ptr() as *mut c_void; + let challenges_guard = proof_ctx.challenges.values.read().unwrap(); let challenges_map = self.stark_info.challenges_map.as_ref().unwrap(); @@ -458,37 +392,20 @@ impl Prover for StarkProver { } let xi_challenge = &(*challenges_guard)[xi_challenge_index * Self::FIELD_EXTENSION] as *const F as *mut c_void; - compute_lev_c(self.p_stark, xi_challenge, lev); + compute_lev_c(self.p_stark, xi_challenge, proof_ctx.get_buff_helper_ptr()); } - fn get_buff_helper_size(&self) -> usize { - let mut max_cols = 0; - for stage in 1..=Self::num_stages(self) + 1 { - let n_cols = *self.stark_info.map_sections_n.get(&format!("cm{}", stage)).unwrap() as usize; - if n_cols > max_cols { - max_cols = n_cols; - } - } - - let n_extended = (1 << self.stark_info.stark_struct.n_bits_ext) as usize; - let buff_size_stages = max_cols * n_extended; - - let buff_size_xdivxsub = self.stark_info.opening_points.len() * 3 * n_extended; + fn get_buff_helper_size(&self, _proof_ctx: Arc>) -> usize { + // if proof_ctx.options.verify_constraints { - match buff_size_stages > buff_size_xdivxsub { - true => buff_size_stages, - false => buff_size_xdivxsub, - } + // } else { + self.stark_info.get_buff_helper_size() + // } } fn calculate_hash(&self, values: Vec) -> Vec { let hash = vec![F::zero(); self.n_field_elements]; - calculate_hash_c( - self.p_stark, - hash.as_ptr() as *mut c_void, - values.as_ptr() as *mut c_void, - values.len() as u64, - ); + calculate_hash_c(self.p_stark, hash.as_ptr() as *mut u8, values.as_ptr() as *mut u8, values.len() as u64); hash } @@ -505,28 +422,12 @@ impl Prover for StarkProver { let mut value = vec![Goldilocks::zero(); self.n_field_elements]; if stage <= (Self::num_stages(self) + 1) as u64 { - let (n_airvals_stage, indexes): (usize, Vec) = self + let n_airvals_stage: usize = self .stark_info .airvalues_map .as_ref() - .map(|map| { - let mut indexes = Vec::new(); - let count = map - .iter() - .enumerate() - .filter(|(index, entry)| { - if entry.stage == stage { - indexes.push(*index); - true - } else { - false - } - }) - .count(); - - (count, indexes) - }) - .unwrap_or((0, Vec::new())); + .map(|map| map.iter().filter(|entry| entry.stage == stage).count()) + .unwrap_or(0); if stage == 1 || n_airvals_stage > 0 { let size = if stage == 1 { @@ -552,8 +453,7 @@ impl Prover for StarkProver { } let mut root = vec![F::zero(); self.n_field_elements]; - treesGL_get_root_c(p_stark, stage - 1, root.as_mut_ptr() as *mut c_void); - + treesGL_get_root_c(p_stark, stage - 1, root.as_mut_ptr() as *mut u8); trace!( "{}: ··· MerkleTree root for stage {} of instance {} of {} is: {:?}", Self::MY_NAME, @@ -567,36 +467,45 @@ impl Prover for StarkProver { values_hash[index] = root_value; } let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - for index in 0..n_airvals_stage { - if stage == 1 { - values_hash[2 * self.n_field_elements + index] = - air_instance.airvalues[indexes[index] * Self::FIELD_EXTENSION]; + let airvalues_map = self.stark_info.airvalues_map.as_ref().unwrap(); + let mut p = 0; + let mut count = 0; + for air_value in airvalues_map { + if air_value.stage > stage { + break; + } + if air_value.stage == 1 { + if stage == 1 { + values_hash[2 * self.n_field_elements + count] = air_instance.airvalues[p]; + count += 1; + } + p += 1; } else { - values_hash[self.n_field_elements + index * Self::FIELD_EXTENSION] = - air_instance.airvalues[indexes[index] * Self::FIELD_EXTENSION]; - values_hash[self.n_field_elements + index * Self::FIELD_EXTENSION + 1] = - air_instance.airvalues[indexes[index] * Self::FIELD_EXTENSION]; - values_hash[self.n_field_elements + index * Self::FIELD_EXTENSION + 2] = - air_instance.airvalues[indexes[index] * Self::FIELD_EXTENSION]; + if air_value.stage == stage { + values_hash[self.n_field_elements + count] = air_instance.airvalues[p]; + values_hash[self.n_field_elements + count + 1] = air_instance.airvalues[p + 1]; + values_hash[self.n_field_elements + count + 2] = air_instance.airvalues[p + 2]; + count += 3; + } + p += 3; } } calculate_hash_c( p_stark, - value.as_mut_ptr() as *mut c_void, - values_hash.as_mut_ptr() as *mut c_void, + value.as_mut_ptr() as *mut u8, + values_hash.as_mut_ptr() as *mut u8, size as u64, ); } else { - treesGL_get_root_c(p_stark, stage - 1, value.as_mut_ptr() as *mut c_void); + treesGL_get_root_c(p_stark, stage - 1, value.as_mut_ptr() as *mut u8); } } else if stage == (Self::num_stages(self) + 2) as u64 { let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let evals = air_instance.evals.as_ptr() as *mut c_void; calculate_hash_c( p_stark, - value.as_mut_ptr() as *mut c_void, - evals, + value.as_mut_ptr() as *mut u8, + air_instance.get_evals_ptr(), (self.stark_info.ev_map.len() * Self::FIELD_EXTENSION) as u64, ); } else if stage > (Self::num_stages(self) + 3) as u64 { @@ -612,14 +521,12 @@ impl Prover for StarkProver { let n_steps = steps.len() - 1; if step_index < n_steps { let p_proof = self.p_proof; - fri_proof_get_tree_root_c(p_proof, value.as_mut_ptr() as *mut c_void, step_index as u64); + fri_proof_get_tree_root_c(p_proof, value.as_mut_ptr() as *mut u8, step_index as u64); } else { let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let buffer = air_instance.get_buffer_ptr() as *mut c_void; - let n_hash = (1 << (steps[n_steps].n_bits)) * Self::FIELD_EXTENSION as u64; - let fri_pol = get_fri_pol_c(self.p_stark_info, buffer); - calculate_hash_c(p_stark, value.as_mut_ptr() as *mut c_void, fri_pol, n_hash); + let fri_pol = get_fri_pol_c(self.p_stark_info, air_instance.get_aux_trace_ptr()); + calculate_hash_c(p_stark, value.as_mut_ptr() as *mut u8, fri_pol as *mut u8, n_hash); } } } @@ -640,7 +547,7 @@ impl Prover for StarkProver { let challenges_map = self.stark_info.challenges_map.as_ref().unwrap(); - let challenges = &*proof_ctx.challenges.challenges.read().unwrap(); + let challenges = &*proof_ctx.challenges.values.read().unwrap(); for i in 0..challenges_map.len() { if challenges_map[i].stage == stage_id as u64 { let challenge = &challenges[i * Self::FIELD_EXTENSION]; @@ -656,7 +563,7 @@ impl Prover for StarkProver { } } else { //Fri folding + . queries: add one challenge for each step - let mut challenges_guard = proof_ctx.challenges.challenges.write().unwrap(); + let mut challenges_guard = proof_ctx.challenges.values.write().unwrap(); challenges_guard.extend(std::iter::repeat(F::zero()).take(3)); transcript.get_challenge(&(*challenges_guard)[challenges_guard.len() - 3] as *const F as *mut c_void); @@ -675,15 +582,6 @@ impl Prover for StarkProver { } fn get_zkin_proof(&self, proof_ctx: Arc>, output_dir: &str) -> *mut c_void { - let public_inputs_guard = proof_ctx.public_inputs.inputs.read().unwrap(); - let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void; - - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - let challenges = (*challenges_guard).as_ptr() as *mut c_void; - - let proof_values_guard = proof_ctx.proof_values.values.read().unwrap(); - let proof_values = (*proof_values_guard).as_ptr() as *mut c_void; - let global_info_path = proof_ctx.global_info.get_proving_key_path().join("pilout.globalInfo.json"); let global_info_file: &str = global_info_path.to_str().unwrap(); @@ -692,9 +590,9 @@ impl Prover for StarkProver { fri_proof_get_zkinproof_c( self.p_proof, - public_inputs, - challenges, - proof_values, + proof_ctx.get_publics_ptr(), + proof_ctx.get_challenges_ptr(), + proof_ctx.get_proof_values_ptr(), self.p_stark_info, &proof_name, global_info_file, @@ -747,27 +645,23 @@ impl StarkProver { let p_stark = self.p_stark; let p_proof = self.p_proof; - let buff_helper_guard = proof_ctx.buff_helper.buff_helper.read().unwrap(); - let buff_helper = (*buff_helper_guard).as_ptr() as *mut c_void; - - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let steps_params = StepsParams { trace: std::ptr::null_mut(), - pols: air_instance.get_buffer_ptr() as *mut c_void, + aux_trace: air_instance.get_aux_trace_ptr(), public_inputs: std::ptr::null_mut(), proof_values: std::ptr::null_mut(), challenges: std::ptr::null_mut(), airgroup_values: std::ptr::null_mut(), airvalues: std::ptr::null_mut(), - evals: air_instance.evals.as_mut_ptr() as *mut c_void, + evals: air_instance.get_evals_ptr(), xdivxsub: std::ptr::null_mut(), p_const_pols: std::ptr::null_mut(), - p_const_tree: const_tree_ptr, + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; - compute_evals_c(p_stark, (&steps_params).into(), buff_helper, p_proof); + compute_evals_c(p_stark, (&steps_params).into(), proof_ctx.get_buff_helper_ptr(), p_proof); } fn compute_fri_pol(&mut self, _opening_id: u32, setup_ctx: Arc, proof_ctx: Arc>) { @@ -777,29 +671,22 @@ impl StarkProver { let setup = setup_ctx.get_setup(self.airgroup_id, self.air_id); - let public_inputs_guard = proof_ctx.public_inputs.inputs.read().unwrap(); - let proof_values_guard = proof_ctx.proof_values.values.read().unwrap(); - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); - let buff_helper_guard = proof_ctx.buff_helper.buff_helper.read().unwrap(); - - let const_pols_ptr = (*setup.const_pols.values.read().unwrap()).as_ptr() as *mut c_void; - let const_tree_ptr = (*setup.const_tree.values.read().unwrap()).as_ptr() as *mut c_void; - let p_stark = self.p_stark; let steps_params = StepsParams { trace: std::ptr::null_mut(), - pols: air_instance.get_buffer_ptr() as *mut c_void, - public_inputs: (*public_inputs_guard).as_ptr() as *mut c_void, - proof_values: (*proof_values_guard).as_ptr() as *mut c_void, - challenges: (*challenges_guard).as_ptr() as *mut c_void, - airgroup_values: air_instance.airgroup_values.as_ptr() as *mut c_void, - airvalues: air_instance.airvalues.as_ptr() as *mut c_void, - evals: air_instance.evals.as_ptr() as *mut c_void, - xdivxsub: (*buff_helper_guard).as_ptr() as *mut c_void, - p_const_pols: const_pols_ptr, - p_const_tree: const_tree_ptr, + aux_trace: air_instance.get_aux_trace_ptr(), + public_inputs: proof_ctx.get_publics_ptr(), + proof_values: proof_ctx.get_proof_values_ptr(), + challenges: proof_ctx.get_challenges_ptr(), + airgroup_values: air_instance.get_airgroup_values_ptr(), + airvalues: air_instance.get_airvalues_ptr(), + evals: air_instance.get_evals_ptr(), + xdivxsub: proof_ctx.get_buff_helper_ptr(), + p_const_pols: setup.get_const_ptr(), + p_const_tree: setup.get_const_tree_ptr(), custom_commits: air_instance.get_custom_commits_ptr(), + custom_commits_extended: air_instance.get_custom_commits_extended_ptr(), }; calculate_fri_polynomial_c(p_stark, (&steps_params).into()); @@ -824,11 +711,10 @@ impl StarkProver { } let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let buffer = air_instance.get_buffer_ptr() as *mut c_void; - let fri_pol = get_fri_pol_c(self.p_stark_info, buffer); + let fri_pol = get_fri_pol_c(self.p_stark_info, air_instance.get_aux_trace_ptr()); - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); + let challenges_guard = proof_ctx.challenges.values.read().unwrap(); let challenge: Vec = challenges_guard.iter().skip(challenges_guard.len() - 3).cloned().collect(); let current_bits = steps[step_index as usize].n_bits; @@ -836,8 +722,8 @@ impl StarkProver { compute_fri_folding_c( step_index as u64, - fri_pol, - challenge.as_ptr() as *mut c_void, + fri_pol as *mut u8, + challenge.as_ptr() as *mut u8, self.stark_info.stark_struct.n_bits_ext, prev_bits, current_bits, @@ -845,7 +731,14 @@ impl StarkProver { if step_index != n_steps { let next_bits = steps[(step_index + 1) as usize].n_bits; - compute_fri_merkelize_c(self.p_stark, p_proof, step_index as u64, fri_pol, current_bits, next_bits); + compute_fri_merkelize_c( + self.p_stark, + p_proof, + step_index as u64, + fri_pol as *mut u8, + current_bits, + next_bits, + ); } } @@ -860,7 +753,7 @@ impl StarkProver { let mut fri_queries = vec![u64::default(); n_queries as usize]; - let challenges_guard = proof_ctx.challenges.challenges.read().unwrap(); + let challenges_guard = proof_ctx.challenges.values.read().unwrap(); let challenge: Vec = challenges_guard.iter().skip(challenges_guard.len() - 3).cloned().collect(); @@ -868,7 +761,7 @@ impl StarkProver { let transcript_permutation = FFITranscript::new(p_stark, element_type, self.merkle_tree_arity, self.merkle_tree_custom); - transcript_permutation.add_elements(challenge.as_ptr() as *mut c_void, Self::FIELD_EXTENSION); + transcript_permutation.add_elements(challenge.as_ptr() as *mut u8, Self::FIELD_EXTENSION); transcript_permutation.get_permutations( fri_queries.as_mut_ptr(), n_queries, @@ -884,9 +777,7 @@ impl StarkProver { ); let air_instance = &mut proof_ctx.air_instance_repo.air_instances.write().unwrap()[self.prover_idx]; - let buffer = air_instance.get_buffer_ptr() as *mut c_void; - - let fri_pol = get_fri_pol_c(self.p_stark_info, buffer); + let fri_pol = get_fri_pol_c(self.p_stark_info, air_instance.get_aux_trace_ptr()); let n_trees = self.num_stages() + 2 + self.stark_info.custom_commits.len() as u32; compute_queries_c(p_stark, p_proof, fri_queries.as_mut_ptr(), n_queries, n_trees as u64); @@ -903,39 +794,8 @@ impl StarkProver { set_fri_final_pol_c( p_proof, - fri_pol, + fri_pol as *mut u8, self.stark_info.stark_struct.steps[self.stark_info.stark_struct.steps.len() - 1].n_bits, ); } } - -pub struct StarkBufferAllocator { - pub proving_key_path: PathBuf, -} - -impl StarkBufferAllocator { - pub fn new(proving_key_path: PathBuf) -> Self { - Self { proving_key_path } - } -} - -impl BufferAllocator for StarkBufferAllocator { - fn get_buffer_info_custom_commit( - &self, - sctx: &SetupCtx, - airgroup_id: usize, - air_id: usize, - name: &str, - ) -> Result<(u64, Vec, u64), Box> { - let ps = sctx.get_setup(airgroup_id, air_id); - - let commit_id = match ps.stark_info.custom_commits.iter().position(|custom_commit| custom_commit.name == name) { - Some(commit_id) => commit_id as u64, - None => { - eprintln!("Custom commit '{}' not found in custom commits.", name); - std::process::exit(1); - } - }; - Ok((get_map_totaln_custom_commits_c(ps.p_setup.p_stark_info, commit_id), vec![0], commit_id)) - } -} diff --git a/provers/starks-lib-c/bindings_starks.rs b/provers/starks-lib-c/bindings_starks.rs index d4b385f8..0e09ccae 100644 --- a/provers/starks-lib-c/bindings_starks.rs +++ b/provers/starks-lib-c/bindings_starks.rs @@ -15,10 +15,10 @@ extern "C" { ); } extern "C" { - #[link_name = "\u{1}_Z17save_proof_valuesmPvPc"] + #[link_name = "\u{1}_Z17save_proof_valuesPvPcS0_"] pub fn save_proof_values( - numProofValues: ::std::os::raw::c_ulong, pProofValues: *mut ::std::os::raw::c_void, + globalInfoFile: *mut ::std::os::raw::c_char, fileDir: *mut ::std::os::raw::c_char, ); } @@ -43,10 +43,7 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z23fri_proof_set_airvaluesPvS_"] - pub fn fri_proof_set_airvalues( - pFriProof: *mut ::std::os::raw::c_void, - airValues: *mut ::std::os::raw::c_void, - ); + pub fn fri_proof_set_airvalues(pFriProof: *mut ::std::os::raw::c_void, airValues: *mut ::std::os::raw::c_void); } extern "C" { #[link_name = "\u{1}_Z23fri_proof_get_zkinproofPvS_S_S_S_PcS0_S0_"] @@ -70,11 +67,17 @@ extern "C" { pub fn fri_proof_free(pFriProof: *mut ::std::os::raw::c_void); } extern "C" { - #[link_name = "\u{1}_Z20get_hint_ids_by_namePvPc"] + #[link_name = "\u{1}_Z15n_hints_by_namePvPc"] + pub fn n_hints_by_name(p_expression_bin: *mut ::std::os::raw::c_void, hintName: *mut ::std::os::raw::c_char) + -> u64; +} +extern "C" { + #[link_name = "\u{1}_Z20get_hint_ids_by_namePvPmPc"] pub fn get_hint_ids_by_name( p_expression_bin: *mut ::std::os::raw::c_void, + hintIds: *mut u64, hintName: *mut ::std::os::raw::c_char, - ) -> *mut ::std::os::raw::c_void; + ); } extern "C" { #[link_name = "\u{1}_Z14stark_info_newPc"] @@ -84,38 +87,13 @@ extern "C" { #[link_name = "\u{1}_Z15get_map_total_nPv"] pub fn get_map_total_n(pStarkInfo: *mut ::std::os::raw::c_void) -> u64; } -extern "C" { - #[link_name = "\u{1}_Z30get_map_total_n_custom_commitsPvm"] - pub fn get_map_total_n_custom_commits( - pStarkInfo: *mut ::std::os::raw::c_void, - commit_id: u64, - ) -> u64; -} -extern "C" { - #[link_name = "\u{1}_Z15get_map_offsetsPvPcb"] - pub fn get_map_offsets( - pStarkInfo: *mut ::std::os::raw::c_void, - stage: *mut ::std::os::raw::c_char, - flag: bool, - ) -> u64; -} -extern "C" { - #[link_name = "\u{1}_Z25get_custom_commit_map_idsPvmm"] - pub fn get_custom_commit_map_ids( - pStarkInfo: *mut ::std::os::raw::c_void, - commit_id: u64, - stage: u64, - ) -> *mut ::std::os::raw::c_void; -} extern "C" { #[link_name = "\u{1}_Z15stark_info_freePv"] pub fn stark_info_free(pStarkInfo: *mut ::std::os::raw::c_void); } extern "C" { #[link_name = "\u{1}_Z18prover_helpers_newPv"] - pub fn prover_helpers_new( - pStarkInfo: *mut ::std::os::raw::c_void, - ) -> *mut ::std::os::raw::c_void; + pub fn prover_helpers_new(pStarkInfo: *mut ::std::os::raw::c_void) -> *mut ::std::os::raw::c_void; } extern "C" { #[link_name = "\u{1}_Z19prover_helpers_freePv"] @@ -156,24 +134,40 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z19expressions_bin_newPcb"] - pub fn expressions_bin_new( - filename: *mut ::std::os::raw::c_char, - global: bool, - ) -> *mut ::std::os::raw::c_void; + pub fn expressions_bin_new(filename: *mut ::std::os::raw::c_char, global: bool) -> *mut ::std::os::raw::c_void; } extern "C" { #[link_name = "\u{1}_Z20expressions_bin_freePv"] pub fn expressions_bin_free(pExpressionsBin: *mut ::std::os::raw::c_void); } extern "C" { - #[link_name = "\u{1}_Z14get_hint_fieldPvS_mPcS_"] + #[link_name = "\u{1}_Z14get_hint_fieldPvS_S_mPcS_"] pub fn get_hint_field( pSetupCtx: *mut ::std::os::raw::c_void, stepsParams: *mut ::std::os::raw::c_void, + hintFieldValues: *mut ::std::os::raw::c_void, hintId: u64, hintFieldName: *mut ::std::os::raw::c_char, hintOptions: *mut ::std::os::raw::c_void, - ) -> *mut ::std::os::raw::c_void; + ); +} +extern "C" { + #[link_name = "\u{1}_Z21get_hint_field_valuesPvmPc"] + pub fn get_hint_field_values( + pSetupCtx: *mut ::std::os::raw::c_void, + hintId: u64, + hintFieldName: *mut ::std::os::raw::c_char, + ) -> u64; +} +extern "C" { + #[link_name = "\u{1}_Z20get_hint_field_sizesPvS_mPcS_"] + pub fn get_hint_field_sizes( + pSetupCtx: *mut ::std::os::raw::c_void, + hintFieldValues: *mut ::std::os::raw::c_void, + hintId: u64, + hintFieldName: *mut ::std::os::raw::c_char, + hintOptions: *mut ::std::os::raw::c_void, + ); } extern "C" { #[link_name = "\u{1}_Z15mul_hint_fieldsPvS_mPcS0_S0_S_S_"] @@ -198,7 +192,7 @@ extern "C" { hintFieldNameAirgroupVal: *mut ::std::os::raw::c_char, hintFieldName: *mut ::std::os::raw::c_char, add: bool, - ) -> *mut ::std::os::raw::c_void; + ); } extern "C" { #[link_name = "\u{1}_Z19acc_mul_hint_fieldsPvS_mPcS0_S0_S0_S_S_b"] @@ -213,7 +207,7 @@ extern "C" { hintOptions1: *mut ::std::os::raw::c_void, hintOptions2: *mut ::std::os::raw::c_void, add: bool, - ) -> *mut ::std::os::raw::c_void; + ); } extern "C" { #[link_name = "\u{1}_Z20update_airgroupvaluePvS_mPcS0_S0_S_S_b"] @@ -227,7 +221,7 @@ extern "C" { hintOptions1: *mut ::std::os::raw::c_void, hintOptions2: *mut ::std::os::raw::c_void, add: bool, - ) -> *mut ::std::os::raw::c_void; + ) -> u64; } extern "C" { #[link_name = "\u{1}_Z14set_hint_fieldPvS_S_mPc"] @@ -239,6 +233,14 @@ extern "C" { hintFieldName: *mut ::std::os::raw::c_char, ) -> u64; } +extern "C" { + #[link_name = "\u{1}_Z11get_hint_idPvmPc"] + pub fn get_hint_id( + pSetupCtx: *mut ::std::os::raw::c_void, + hintId: u64, + hintFieldName: *mut ::std::os::raw::c_char, + ) -> u64; +} extern "C" { #[link_name = "\u{1}_Z10starks_newPvS_"] pub fn starks_new( @@ -252,19 +254,11 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z16treesGL_get_rootPvmS_"] - pub fn treesGL_get_root( - pStarks: *mut ::std::os::raw::c_void, - index: u64, - root: *mut ::std::os::raw::c_void, - ); + pub fn treesGL_get_root(pStarks: *mut ::std::os::raw::c_void, index: u64, root: *mut ::std::os::raw::c_void); } extern "C" { #[link_name = "\u{1}_Z16treesGL_set_rootPvmS_"] - pub fn treesGL_set_root( - pStarks: *mut ::std::os::raw::c_void, - index: u64, - pProof: *mut ::std::os::raw::c_void, - ); + pub fn treesGL_set_root(pStarks: *mut ::std::os::raw::c_void, index: u64, pProof: *mut ::std::os::raw::c_void); } extern "C" { #[link_name = "\u{1}_Z18calculate_xdivxsubPvS_S_"] @@ -283,10 +277,7 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z24calculate_fri_polynomialPvS_"] - pub fn calculate_fri_polynomial( - pStarks: *mut ::std::os::raw::c_void, - stepsParams: *mut ::std::os::raw::c_void, - ); + pub fn calculate_fri_polynomial(pStarks: *mut ::std::os::raw::c_void, stepsParams: *mut ::std::os::raw::c_void); } extern "C" { #[link_name = "\u{1}_Z29calculate_quotient_polynomialPvS_"] @@ -304,24 +295,26 @@ extern "C" { ); } extern "C" { - #[link_name = "\u{1}_Z34extend_and_merkelize_custom_commitPvmmS_S_S_Pc"] + #[link_name = "\u{1}_Z34extend_and_merkelize_custom_commitPvmmS_S_S_S_Pc"] pub fn extend_and_merkelize_custom_commit( pStarks: *mut ::std::os::raw::c_void, commitId: u64, step: u64, buffer: *mut ::std::os::raw::c_void, + bufferExt: *mut ::std::os::raw::c_void, pProof: *mut ::std::os::raw::c_void, pBuffHelper: *mut ::std::os::raw::c_void, treeFile: *mut ::std::os::raw::c_char, ); } extern "C" { - #[link_name = "\u{1}_Z18load_custom_commitPvmmS_S_Pc"] + #[link_name = "\u{1}_Z18load_custom_commitPvmmS_S_S_Pc"] pub fn load_custom_commit( pStarks: *mut ::std::os::raw::c_void, commitId: u64, step: u64, buffer: *mut ::std::os::raw::c_void, + bufferExt: *mut ::std::os::raw::c_void, pProof: *mut ::std::os::raw::c_void, treeFile: *mut ::std::os::raw::c_char, ); @@ -366,12 +359,7 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z15merkle_tree_newmmmb"] - pub fn merkle_tree_new( - height: u64, - width: u64, - arity: u64, - custom: bool, - ) -> *mut ::std::os::raw::c_void; + pub fn merkle_tree_new(height: u64, width: u64, arity: u64, custom: bool) -> *mut ::std::os::raw::c_void; } extern "C" { #[link_name = "\u{1}_Z16merkle_tree_freePv"] @@ -422,27 +410,15 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z17set_fri_final_polPvS_m"] - pub fn set_fri_final_pol( - pProof: *mut ::std::os::raw::c_void, - buffer: *mut ::std::os::raw::c_void, - nBits: u64, - ); + pub fn set_fri_final_pol(pProof: *mut ::std::os::raw::c_void, buffer: *mut ::std::os::raw::c_void, nBits: u64); } extern "C" { #[link_name = "\u{1}_Z14transcript_newjmb"] - pub fn transcript_new( - elementType: u32, - arity: u64, - custom: bool, - ) -> *mut ::std::os::raw::c_void; + pub fn transcript_new(elementType: u32, arity: u64, custom: bool) -> *mut ::std::os::raw::c_void; } extern "C" { #[link_name = "\u{1}_Z14transcript_addPvS_m"] - pub fn transcript_add( - pTranscript: *mut ::std::os::raw::c_void, - pInput: *mut ::std::os::raw::c_void, - size: u64, - ); + pub fn transcript_add(pTranscript: *mut ::std::os::raw::c_void, pInput: *mut ::std::os::raw::c_void, size: u64); } extern "C" { #[link_name = "\u{1}_Z25transcript_add_polinomialPvS_"] @@ -465,34 +441,81 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z16get_permutationsPvPmmm"] - pub fn get_permutations( - pTranscript: *mut ::std::os::raw::c_void, - res: *mut u64, - n: u64, - nBits: u64, - ); + pub fn get_permutations(pTranscript: *mut ::std::os::raw::c_void, res: *mut u64, n: u64, nBits: u64); +} +extern "C" { + #[link_name = "\u{1}_Z17get_n_constraintsPv"] + pub fn get_n_constraints(pSetupCtx: *mut ::std::os::raw::c_void) -> u64; } extern "C" { - #[link_name = "\u{1}_Z18verify_constraintsPvS_"] + #[link_name = "\u{1}_Z27get_constraints_lines_sizesPvPm"] + pub fn get_constraints_lines_sizes(pSetupCtx: *mut ::std::os::raw::c_void, constraintsLinesSizes: *mut u64); +} +extern "C" { + #[link_name = "\u{1}_Z21get_constraints_linesPvPPh"] + pub fn get_constraints_lines(pSetupCtx: *mut ::std::os::raw::c_void, constraintsLines: *mut *mut u8); +} +extern "C" { + #[link_name = "\u{1}_Z18verify_constraintsPvS_S_"] pub fn verify_constraints( pSetupCtx: *mut ::std::os::raw::c_void, stepsParams: *mut ::std::os::raw::c_void, - ) -> *mut ::std::os::raw::c_void; + constraintsInfo: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[link_name = "\u{1}_Z24get_n_global_constraintsPv"] + pub fn get_n_global_constraints(p_globalinfo_bin: *mut ::std::os::raw::c_void) -> u64; +} + +extern "C" { + #[link_name = "\u{1}_Z34get_global_constraints_lines_sizesPvPm"] + pub fn get_global_constraints_lines_sizes( + p_globalinfo_bin: *mut ::std::os::raw::c_void, + constraintsLinesSizes: *mut u64, + ); +} +extern "C" { + #[link_name = "\u{1}_Z28get_global_constraints_linesPvPPh"] + pub fn get_global_constraints_lines(p_globalinfo_bin: *mut ::std::os::raw::c_void, constraintsLines: *mut *mut u8); } extern "C" { - #[link_name = "\u{1}_Z25verify_global_constraintsPvS_S_S_PS_"] + #[link_name = "\u{1}_Z25verify_global_constraintsPcPvS0_S0_S0_PS0_S0_"] pub fn verify_global_constraints( + globalInfoFile: *mut ::std::os::raw::c_char, globalBin: *mut ::std::os::raw::c_void, publics: *mut ::std::os::raw::c_void, challenges: *mut ::std::os::raw::c_void, proofValues: *mut ::std::os::raw::c_void, airgroupValues: *mut *mut ::std::os::raw::c_void, - ) -> bool; + globalConstraintsInfo: *mut ::std::os::raw::c_void, + ); } extern "C" { - #[link_name = "\u{1}_Z33get_hint_field_global_constraintsPvS_S_S_PS_mPcb"] + #[link_name = "\u{1}_Z40get_hint_field_global_constraints_valuesPvmPc"] + pub fn get_hint_field_global_constraints_values( + p_globalinfo_bin: *mut ::std::os::raw::c_void, + hintId: u64, + hintFieldName: *mut ::std::os::raw::c_char, + ) -> u64; +} +extern "C" { + #[link_name = "\u{1}_Z39get_hint_field_global_constraints_sizesPcPvS0_mS_b"] + pub fn get_hint_field_global_constraints_sizes( + globalInfoFile: *mut ::std::os::raw::c_char, + p_globalinfo_bin: *mut ::std::os::raw::c_void, + hintFieldValues: *mut ::std::os::raw::c_void, + hintId: u64, + hintFieldName: *mut ::std::os::raw::c_char, + print_expression: bool, + ); +} +extern "C" { + #[link_name = "\u{1}_Z33get_hint_field_global_constraintsPcPvS0_S0_S0_S0_PS0_mS_b"] pub fn get_hint_field_global_constraints( - globalBin: *mut ::std::os::raw::c_void, + globalInfoFile: *mut ::std::os::raw::c_char, + p_globalinfo_bin: *mut ::std::os::raw::c_void, + hintFieldValues: *mut ::std::os::raw::c_void, publics: *mut ::std::os::raw::c_void, challenges: *mut ::std::os::raw::c_void, proofValues: *mut ::std::os::raw::c_void, @@ -500,11 +523,12 @@ extern "C" { hintId: u64, hintFieldName: *mut ::std::os::raw::c_char, print_expression: bool, - ) -> *mut ::std::os::raw::c_void; + ); } extern "C" { - #[link_name = "\u{1}_Z33set_hint_field_global_constraintsPvS_S_mPc"] + #[link_name = "\u{1}_Z33set_hint_field_global_constraintsPcPvS0_S0_mS_"] pub fn set_hint_field_global_constraints( + globalInfoFile: *mut ::std::os::raw::c_char, p_globalinfo_bin: *mut ::std::os::raw::c_void, proofValues: *mut ::std::os::raw::c_void, values: *mut ::std::os::raw::c_void, @@ -514,22 +538,7 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z9print_rowPvS_mm"] - pub fn print_row( - pSetupCtx: *mut ::std::os::raw::c_void, - buffer: *mut ::std::os::raw::c_void, - stage: u64, - row: u64, - ); -} -extern "C" { - #[link_name = "\u{1}_Z16print_expressionPvS_mmm"] - pub fn print_expression( - pSetupCtx: *mut ::std::os::raw::c_void, - pol: *mut ::std::os::raw::c_void, - dim: u64, - first_value: u64, - last_value: u64, - ); + pub fn print_row(pSetupCtx: *mut ::std::os::raw::c_void, buffer: *mut ::std::os::raw::c_void, stage: u64, row: u64); } extern "C" { #[link_name = "\u{1}_Z19gen_recursive_proofPvPcmS_S_S_S_S0_b"] @@ -537,7 +546,7 @@ extern "C" { pSetupCtx: *mut ::std::os::raw::c_void, globalInfoFile: *mut ::std::os::raw::c_char, airgroupId: u64, - pAddress: *mut ::std::os::raw::c_void, + witness: *mut ::std::os::raw::c_void, pConstPols: *mut ::std::os::raw::c_void, pConstTree: *mut ::std::os::raw::c_void, pPublicInputs: *mut ::std::os::raw::c_void, @@ -581,16 +590,11 @@ extern "C" { } extern "C" { #[link_name = "\u{1}_Z20get_serialized_proofPvPm"] - pub fn get_serialized_proof( - zkin: *mut ::std::os::raw::c_void, - size: *mut u64, - ) -> *mut ::std::os::raw::c_char; + pub fn get_serialized_proof(zkin: *mut ::std::os::raw::c_void, size: *mut u64) -> *mut ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}_Z22deserialize_zkin_proofPc"] - pub fn deserialize_zkin_proof( - serialized_proof: *mut ::std::os::raw::c_char, - ) -> *mut ::std::os::raw::c_void; + pub fn deserialize_zkin_proof(serialized_proof: *mut ::std::os::raw::c_char) -> *mut ::std::os::raw::c_void; } extern "C" { #[link_name = "\u{1}_Z14get_zkin_proofPc"] @@ -605,23 +609,22 @@ extern "C" { pub fn serialized_proof_free(zkinCStr: *mut ::std::os::raw::c_char); } extern "C" { - #[link_name = "\u{1}_Z18get_committed_polsPvPcS_S_mmmmm"] + #[link_name = "\u{1}_Z18get_committed_polsPvPcS_S_mmmm"] pub fn get_committed_pols( - pWitness: *mut ::std::os::raw::c_void, + circomWitness: *mut ::std::os::raw::c_void, execFile: *mut ::std::os::raw::c_char, - pAddress: *mut ::std::os::raw::c_void, + witness: *mut ::std::os::raw::c_void, pPublics: *mut ::std::os::raw::c_void, sizeWitness: u64, N: u64, nPublics: u64, - offsetCm1: u64, nCols: u64, ); } extern "C" { #[link_name = "\u{1}_Z21gen_final_snark_proofPvPcS0_"] pub fn gen_final_snark_proof( - pWitnessFinal: *mut ::std::os::raw::c_void, + circomWitnessFinal: *mut ::std::os::raw::c_void, zkeyFile: *mut ::std::os::raw::c_char, outputDir: *mut ::std::os::raw::c_char, ); @@ -629,4 +632,4 @@ extern "C" { extern "C" { #[link_name = "\u{1}_Z11setLogLevelm"] pub fn setLogLevel(level: u64); -} +} \ No newline at end of file diff --git a/provers/starks-lib-c/src/ffi_starks.rs b/provers/starks-lib-c/src/ffi_starks.rs index 162e9ef4..1239e84a 100644 --- a/provers/starks-lib-c/src/ffi_starks.rs +++ b/provers/starks-lib-c/src/ffi_starks.rs @@ -4,12 +4,6 @@ use ::std::os::raw::c_void; -#[repr(C)] -pub struct VecU64Result { - pub n_values: u64, - pub values: *mut u64, -} - #[cfg(feature = "no_lib_link")] use log::trace; @@ -20,7 +14,7 @@ include!("../bindings_starks.rs"); use std::ffi::CString; #[cfg(not(feature = "no_lib_link"))] -pub fn save_challenges_c(p_challenges: *mut std::os::raw::c_void, global_info_file: &str, output_dir: &str) { +pub fn save_challenges_c(p_challenges: *mut u8, global_info_file: &str, output_dir: &str) { unsafe { let file_dir = CString::new(output_dir).unwrap(); let file_ptr = file_dir.as_ptr() as *mut std::os::raw::c_char; @@ -33,18 +27,30 @@ pub fn save_challenges_c(p_challenges: *mut std::os::raw::c_void, global_info_fi } #[cfg(not(feature = "no_lib_link"))] -pub fn save_publics_c(n_publics: u64, public_inputs: *mut std::os::raw::c_void, output_dir: &str) { +pub fn save_publics_c(n_publics: u64, public_inputs: *mut u8, output_dir: &str) { let file_dir: CString = CString::new(output_dir).unwrap(); unsafe { - save_publics(n_publics, public_inputs, file_dir.as_ptr() as *mut std::os::raw::c_char); + save_publics( + n_publics, + public_inputs as *mut std::os::raw::c_void, + file_dir.as_ptr() as *mut std::os::raw::c_char, + ); } } #[cfg(not(feature = "no_lib_link"))] -pub fn save_proof_values_c(n_proof_values: u64, proof_values: *mut std::os::raw::c_void, output_dir: &str) { +pub fn save_proof_values_c(proof_values: *mut u8, global_info_file: &str, output_dir: &str) { let file_dir: CString = CString::new(output_dir).unwrap(); + + let global_info_file_name = CString::new(global_info_file).unwrap(); + let global_info_file_ptr = global_info_file_name.as_ptr() as *mut std::os::raw::c_char; + unsafe { - save_proof_values(n_proof_values, proof_values, file_dir.as_ptr() as *mut std::os::raw::c_char); + save_proof_values( + proof_values as *mut std::os::raw::c_void, + global_info_file_ptr, + file_dir.as_ptr() as *mut std::os::raw::c_char, + ); } } @@ -54,29 +60,29 @@ pub fn fri_proof_new_c(p_setup_ctx: *mut c_void) -> *mut c_void { } #[cfg(not(feature = "no_lib_link"))] -pub fn fri_proof_get_tree_root_c(p_fri_proof: *mut c_void, root: *mut c_void, tree_index: u64) { +pub fn fri_proof_get_tree_root_c(p_fri_proof: *mut c_void, root: *mut u8, tree_index: u64) { unsafe { - fri_proof_get_tree_root(p_fri_proof, root, tree_index); + fri_proof_get_tree_root(p_fri_proof, root as *mut std::os::raw::c_void, tree_index); } } #[cfg(not(feature = "no_lib_link"))] -pub fn fri_proof_set_airgroup_values_c(p_fri_proof: *mut c_void, p_airgroup_values: *mut c_void) { - unsafe { fri_proof_set_airgroupvalues(p_fri_proof, p_airgroup_values) } +pub fn fri_proof_set_airgroup_values_c(p_fri_proof: *mut c_void, p_airgroup_values: *mut u8) { + unsafe { fri_proof_set_airgroupvalues(p_fri_proof, p_airgroup_values as *mut std::os::raw::c_void) } } #[cfg(not(feature = "no_lib_link"))] -pub fn fri_proof_set_air_values_c(p_fri_proof: *mut c_void, p_air_values: *mut c_void) { - unsafe { fri_proof_set_airvalues(p_fri_proof, p_air_values) } +pub fn fri_proof_set_air_values_c(p_fri_proof: *mut c_void, p_air_values: *mut u8) { + unsafe { fri_proof_set_airvalues(p_fri_proof, p_air_values as *mut std::os::raw::c_void) } } #[cfg(not(feature = "no_lib_link"))] #[allow(clippy::too_many_arguments)] pub fn fri_proof_get_zkinproof_c( p_fri_proof: *mut c_void, - p_publics: *mut c_void, - p_challenges: *mut c_void, - p_proof_values: *mut c_void, + p_publics: *mut u8, + p_challenges: *mut u8, + p_proof_values: *mut u8, p_stark_info: *mut c_void, proof_name: &str, global_info_file: &str, @@ -94,9 +100,9 @@ pub fn fri_proof_get_zkinproof_c( unsafe { fri_proof_get_zkinproof( p_fri_proof, - p_publics, - p_challenges, - p_proof_values, + p_publics as *mut std::os::raw::c_void, + p_challenges as *mut std::os::raw::c_void, + p_proof_values as *mut std::os::raw::c_void, p_stark_info, proof_name_ptr, global_info_file_ptr, @@ -133,32 +139,6 @@ pub fn get_map_totaln_c(p_stark_info: *mut c_void) -> u64 { unsafe { get_map_total_n(p_stark_info) } } -#[cfg(not(feature = "no_lib_link"))] -pub fn get_map_totaln_custom_commits_c(p_stark_info: *mut c_void, commit_id: u64) -> u64 { - unsafe { get_map_total_n_custom_commits(p_stark_info, commit_id) } -} - -#[cfg(not(feature = "no_lib_link"))] -pub fn get_custom_commit_map_ids_c(p_stark_info: *mut c_void, commit_id: u64, stage: u64) -> Vec { - unsafe { - let raw_ptr = get_custom_commit_map_ids(p_stark_info, commit_id, stage); - - let ids_result = Box::from_raw(raw_ptr as *mut VecU64Result); - - let slice = std::slice::from_raw_parts(ids_result.values, ids_result.n_values as usize); - - // Copy the contents of the slice into a Vec - - slice.to_vec() - } -} - -#[cfg(not(feature = "no_lib_link"))] -pub fn get_map_offsets_c(pStarkInfo: *mut c_void, stage: &str, flag: bool) -> u64 { - let stage = CString::new(stage).unwrap(); - unsafe { get_map_offsets(pStarkInfo, stage.as_ptr() as *mut std::os::raw::c_char, flag) } -} - #[cfg(not(feature = "no_lib_link"))] pub fn stark_info_free_c(p_stark_info: *mut c_void) { unsafe { @@ -179,11 +159,15 @@ pub fn prover_helpers_free_c(p_prover_helpers: *mut c_void) { } #[cfg(not(feature = "no_lib_link"))] -pub fn load_const_pols_c(pConstPolsAddress: *mut c_void, const_filename: &str, const_size: u64) { +pub fn load_const_pols_c(pConstPolsAddress: *mut u8, const_filename: &str, const_size: u64) { unsafe { let const_filename: CString = CString::new(const_filename).unwrap(); - load_const_pols(pConstPolsAddress, const_filename.as_ptr() as *mut std::os::raw::c_char, const_size); + load_const_pols( + pConstPolsAddress as *mut std::os::raw::c_void, + const_filename.as_ptr() as *mut std::os::raw::c_char, + const_size, + ); } } @@ -198,19 +182,23 @@ pub fn get_const_tree_size_c(pStarkInfo: *mut c_void) -> u64 { } #[cfg(not(feature = "no_lib_link"))] -pub fn load_const_tree_c(pConstPolsTreeAddress: *mut c_void, tree_filename: &str, const_tree_size: u64) { +pub fn load_const_tree_c(pConstPolsTreeAddress: *mut u8, tree_filename: &str, const_tree_size: u64) { unsafe { let tree_filename: CString = CString::new(tree_filename).unwrap(); - load_const_tree(pConstPolsTreeAddress, tree_filename.as_ptr() as *mut std::os::raw::c_char, const_tree_size); + load_const_tree( + pConstPolsTreeAddress as *mut std::os::raw::c_void, + tree_filename.as_ptr() as *mut std::os::raw::c_char, + const_tree_size, + ); } } #[cfg(not(feature = "no_lib_link"))] pub fn calculate_const_tree_c( pStarkInfo: *mut c_void, - pConstPols: *mut c_void, - pConstPolsTreeAddress: *mut c_void, + pConstPols: *mut u8, + pConstPolsTreeAddress: *mut u8, tree_filename: &str, ) { unsafe { @@ -218,8 +206,8 @@ pub fn calculate_const_tree_c( calculate_const_tree( pStarkInfo, - pConstPols, - pConstPolsTreeAddress, + pConstPols as *mut std::os::raw::c_void, + pConstPolsTreeAddress as *mut std::os::raw::c_void, tree_filename.as_ptr() as *mut std::os::raw::c_char, ); } @@ -242,42 +230,78 @@ pub fn expressions_bin_free_c(p_expressions_bin: *mut c_void) { } #[cfg(not(feature = "no_lib_link"))] -pub fn get_hint_ids_by_name_c(p_expressions_bin: *mut c_void, hint_name: &str) -> *mut c_void { +pub fn n_hint_ids_by_name_c(p_expressions_bin: *mut c_void, hint_name: &str) -> u64 { let name = CString::new(hint_name).unwrap(); - unsafe { get_hint_ids_by_name(p_expressions_bin, name.as_ptr() as *mut std::os::raw::c_char) } + unsafe { n_hints_by_name(p_expressions_bin, name.as_ptr() as *mut std::os::raw::c_char) } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_hint_ids_by_name_c(p_expressions_bin: *mut c_void, hint_ids: *mut u64, hint_name: &str) { + let name = CString::new(hint_name).unwrap(); + unsafe { + get_hint_ids_by_name(p_expressions_bin, hint_ids, name.as_ptr() as *mut std::os::raw::c_char); + } } #[cfg(not(feature = "no_lib_link"))] pub fn get_hint_field_c( p_setup_ctx: *mut c_void, - p_steps_params: *mut c_void, + p_steps_params: *mut u8, + hint_field_values: *mut c_void, hint_id: u64, hint_field_name: &str, - hint_options: *mut c_void, -) -> *mut c_void { + hint_options: *mut u8, +) { let field_name = CString::new(hint_field_name).unwrap(); unsafe { get_hint_field( p_setup_ctx, - p_steps_params, + p_steps_params as *mut std::os::raw::c_void, + hint_field_values, hint_id, field_name.as_ptr() as *mut std::os::raw::c_char, - hint_options, + hint_options as *mut std::os::raw::c_void, ) } } +#[cfg(not(feature = "no_lib_link"))] +pub fn get_hint_field_values_c(p_setup_ctx: *mut c_void, hint_id: u64, hint_field_name: &str) -> u64 { + let field_name = CString::new(hint_field_name).unwrap(); + unsafe { get_hint_field_values(p_setup_ctx, hint_id, field_name.as_ptr() as *mut std::os::raw::c_char) } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_hint_field_sizes_c( + p_setup_ctx: *mut c_void, + hint_field_values: *mut c_void, + hint_id: u64, + hint_field_name: &str, + hint_options: *mut u8, +) { + let field_name = CString::new(hint_field_name).unwrap(); + unsafe { + get_hint_field_sizes( + p_setup_ctx, + hint_field_values, + hint_id, + field_name.as_ptr() as *mut std::os::raw::c_char, + hint_options as *mut std::os::raw::c_void, + ); + } +} + #[cfg(not(feature = "no_lib_link"))] #[allow(clippy::too_many_arguments)] pub fn mul_hint_fields_c( p_setup_ctx: *mut c_void, - p_steps_params: *mut c_void, + p_steps_params: *mut u8, hint_id: u64, hint_field_dest: &str, hint_field_name1: &str, hint_field_name2: &str, - hint_options1: *mut c_void, - hint_options2: *mut c_void, + hint_options1: *mut u8, + hint_options2: *mut u8, ) -> u64 { let field_dest = CString::new(hint_field_dest).unwrap(); let field_name1 = CString::new(hint_field_name1).unwrap(); @@ -286,13 +310,13 @@ pub fn mul_hint_fields_c( unsafe { mul_hint_fields( p_setup_ctx, - p_steps_params, + p_steps_params as *mut std::os::raw::c_void, hint_id, field_dest.as_ptr() as *mut std::os::raw::c_char, field_name1.as_ptr() as *mut std::os::raw::c_char, field_name2.as_ptr() as *mut std::os::raw::c_char, - hint_options1, - hint_options2, + hint_options1 as *mut std::os::raw::c_void, + hint_options2 as *mut std::os::raw::c_void, ) } } @@ -300,13 +324,13 @@ pub fn mul_hint_fields_c( #[cfg(not(feature = "no_lib_link"))] pub fn acc_hint_field_c( p_setup_ctx: *mut c_void, - p_steps_params: *mut c_void, + p_steps_params: *mut u8, hint_id: u64, hint_field_dest: &str, hint_field_airgroupvalue: &str, hint_field_name: &str, add: bool, -) -> *mut c_void { +) { let field_dest = CString::new(hint_field_dest).unwrap(); let field_airgroupvalue = CString::new(hint_field_airgroupvalue).unwrap(); let field_name = CString::new(hint_field_name).unwrap(); @@ -314,13 +338,13 @@ pub fn acc_hint_field_c( unsafe { acc_hint_field( p_setup_ctx, - p_steps_params, + p_steps_params as *mut std::os::raw::c_void, hint_id, field_dest.as_ptr() as *mut std::os::raw::c_char, field_airgroupvalue.as_ptr() as *mut std::os::raw::c_char, field_name.as_ptr() as *mut std::os::raw::c_char, add, - ) + ); } } @@ -328,16 +352,16 @@ pub fn acc_hint_field_c( #[allow(clippy::too_many_arguments)] pub fn acc_mul_hint_fields_c( p_setup_ctx: *mut c_void, - p_steps_params: *mut c_void, + p_steps_params: *mut u8, hint_id: u64, hint_field_dest: &str, hint_field_airgroupvalue: &str, hint_field_name1: &str, hint_field_name2: &str, - hint_options1: *mut c_void, - hint_options2: *mut c_void, + hint_options1: *mut u8, + hint_options2: *mut u8, add: bool, -) -> *mut c_void { +) { let field_dest = CString::new(hint_field_dest).unwrap(); let field_airgroupvalue = CString::new(hint_field_airgroupvalue).unwrap(); let field_name1 = CString::new(hint_field_name1).unwrap(); @@ -346,16 +370,16 @@ pub fn acc_mul_hint_fields_c( unsafe { acc_mul_hint_fields( p_setup_ctx, - p_steps_params, + p_steps_params as *mut std::os::raw::c_void, hint_id, field_dest.as_ptr() as *mut std::os::raw::c_char, field_airgroupvalue.as_ptr() as *mut std::os::raw::c_char, field_name1.as_ptr() as *mut std::os::raw::c_char, field_name2.as_ptr() as *mut std::os::raw::c_char, - hint_options1, - hint_options2, + hint_options1 as *mut std::os::raw::c_void, + hint_options2 as *mut std::os::raw::c_void, add, - ) + ); } } @@ -363,15 +387,15 @@ pub fn acc_mul_hint_fields_c( #[allow(clippy::too_many_arguments)] pub fn update_airgroupvalue_c( p_setup_ctx: *mut c_void, - p_steps_params: *mut c_void, + p_steps_params: *mut u8, hint_id: u64, hint_field_airgroupvalue: &str, hint_field_name1: &str, hint_field_name2: &str, - hint_options1: *mut c_void, - hint_options2: *mut c_void, + hint_options1: *mut u8, + hint_options2: *mut u8, add: bool, -) -> *mut c_void { +) -> u64 { let field_airgroupvalue = CString::new(hint_field_airgroupvalue).unwrap(); let field_name1 = CString::new(hint_field_name1).unwrap(); let field_name2: CString = CString::new(hint_field_name2).unwrap(); @@ -379,13 +403,13 @@ pub fn update_airgroupvalue_c( unsafe { update_airgroupvalue( p_setup_ctx, - p_steps_params, + p_steps_params as *mut std::os::raw::c_void, hint_id, field_airgroupvalue.as_ptr() as *mut std::os::raw::c_char, field_name1.as_ptr() as *mut std::os::raw::c_char, field_name2.as_ptr() as *mut std::os::raw::c_char, - hint_options1, - hint_options2, + hint_options1 as *mut std::os::raw::c_void, + hint_options2 as *mut std::os::raw::c_void, add, ) } @@ -394,20 +418,34 @@ pub fn update_airgroupvalue_c( #[cfg(not(feature = "no_lib_link"))] pub fn set_hint_field_c( p_setup_ctx: *mut c_void, - p_params: *mut c_void, - values: *mut c_void, + p_params: *mut u8, + values: *mut u8, hint_id: u64, hint_field_name: &str, ) -> u64 { unsafe { let field_name = CString::new(hint_field_name).unwrap(); - set_hint_field(p_setup_ctx, p_params, values, hint_id, field_name.as_ptr() as *mut std::os::raw::c_char) + set_hint_field( + p_setup_ctx, + p_params as *mut std::os::raw::c_void, + values as *mut std::os::raw::c_void, + hint_id, + field_name.as_ptr() as *mut std::os::raw::c_char, + ) + } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_hint_field_id_c(p_setup_ctx: *mut c_void, hint_id: u64, hint_field_name: &str) -> u64 { + unsafe { + let field_name = CString::new(hint_field_name).unwrap(); + get_hint_id(p_setup_ctx, hint_id, field_name.as_ptr() as *mut std::os::raw::c_char) } } #[cfg(not(feature = "no_lib_link"))] -pub fn starks_new_c(p_setup_ctx: *mut c_void, p_const_tree: *mut c_void) -> *mut c_void { - unsafe { starks_new(p_setup_ctx, p_const_tree) } +pub fn starks_new_c(p_setup_ctx: *mut c_void, p_const_tree: *mut u8) -> *mut c_void { + unsafe { starks_new(p_setup_ctx, p_const_tree as *mut std::os::raw::c_void) } } #[cfg(not(feature = "no_lib_link"))] @@ -430,9 +468,9 @@ pub fn merkle_tree_free_c(merkle_tree: *mut c_void) { } #[cfg(not(feature = "no_lib_link"))] -pub fn treesGL_get_root_c(pStark: *mut c_void, index: u64, root: *mut c_void) { +pub fn treesGL_get_root_c(pStark: *mut c_void, index: u64, root: *mut u8) { unsafe { - treesGL_get_root(pStark, index, root); + treesGL_get_root(pStark, index, root as *mut std::os::raw::c_void); } } @@ -444,46 +482,48 @@ pub fn treesGL_set_root_c(pStark: *mut c_void, index: u64, pProof: *mut c_void) } #[cfg(not(feature = "no_lib_link"))] -pub fn calculate_xdivxsub_c(p_stark: *mut c_void, xi_challenge: *mut c_void, xdivxsub: *mut c_void) { +pub fn calculate_xdivxsub_c(p_stark: *mut c_void, xi_challenge: *mut c_void, xdivxsub: *mut u8) { unsafe { - calculate_xdivxsub(p_stark, xi_challenge, xdivxsub); + calculate_xdivxsub(p_stark, xi_challenge, xdivxsub as *mut std::os::raw::c_void); } } #[cfg(not(feature = "no_lib_link"))] -pub fn get_fri_pol_c(p_stark_info: *mut c_void, buffer: *mut c_void) -> *mut c_void { - unsafe { get_fri_pol(p_stark_info, buffer) } +pub fn get_fri_pol_c(p_stark_info: *mut c_void, buffer: *mut u8) -> *mut c_void { + unsafe { get_fri_pol(p_stark_info, buffer as *mut std::os::raw::c_void) } } #[cfg(not(feature = "no_lib_link"))] -pub fn calculate_fri_polynomial_c(p_starks: *mut c_void, p_steps_params: *mut c_void) { +pub fn calculate_fri_polynomial_c(p_starks: *mut c_void, p_steps_params: *mut u8) { unsafe { - calculate_fri_polynomial(p_starks, p_steps_params); + calculate_fri_polynomial(p_starks, p_steps_params as *mut std::os::raw::c_void); } } #[cfg(not(feature = "no_lib_link"))] -pub fn calculate_quotient_polynomial_c(p_starks: *mut c_void, p_steps_params: *mut c_void) { +pub fn calculate_quotient_polynomial_c(p_starks: *mut c_void, p_steps_params: *mut u8) { unsafe { - calculate_quotient_polynomial(p_starks, p_steps_params); + calculate_quotient_polynomial(p_starks, p_steps_params as *mut std::os::raw::c_void); } } #[cfg(not(feature = "no_lib_link"))] -pub fn calculate_impols_expressions_c(p_starks: *mut c_void, step: u64, p_steps_params: *mut c_void) { +pub fn calculate_impols_expressions_c(p_starks: *mut c_void, step: u64, p_steps_params: *mut u8) { unsafe { - calculate_impols_expressions(p_starks, step, p_steps_params); + calculate_impols_expressions(p_starks, step, p_steps_params as *mut std::os::raw::c_void); } } #[cfg(not(feature = "no_lib_link"))] +#[allow(clippy::too_many_arguments)] pub fn extend_and_merkelize_custom_commit_c( p_starks: *mut c_void, commit_id: u64, step: u64, - buffer: *mut c_void, + buffer: *mut u8, + buffer_ext: *mut u8, p_proof: *mut c_void, - p_buff_helper: *mut c_void, + p_buff_helper: *mut u8, buffer_file: &str, ) { let buffer_file_name = CString::new(buffer_file).unwrap(); @@ -492,9 +532,10 @@ pub fn extend_and_merkelize_custom_commit_c( p_starks, commit_id, step, - buffer, + buffer as *mut std::os::raw::c_void, + buffer_ext as *mut std::os::raw::c_void, p_proof, - p_buff_helper, + p_buff_helper as *mut std::os::raw::c_void, buffer_file_name.as_ptr() as *mut std::os::raw::c_char, ); } @@ -505,7 +546,8 @@ pub fn load_custom_commit_c( p_starks: *mut c_void, commit_id: u64, step: u64, - buffer: *mut c_void, + buffer: *mut u8, + buffer_ext: *mut u8, p_proof: *mut c_void, buffer_file: &str, ) { @@ -515,7 +557,8 @@ pub fn load_custom_commit_c( p_starks, commit_id, step, - buffer, + buffer as *mut std::os::raw::c_void, + buffer_ext as *mut std::os::raw::c_void, p_proof, buffer_file_name.as_ptr() as *mut std::os::raw::c_char, ); @@ -527,41 +570,56 @@ pub fn commit_stage_c( p_starks: *mut c_void, element_type: u32, step: u64, - trace: *mut c_void, - buffer: *mut c_void, + witness: *mut u8, + buffer: *mut u8, p_proof: *mut c_void, - p_buff_helper: *mut c_void, + p_buff_helper: *mut u8, ) { unsafe { - commit_stage(p_starks, element_type, step, trace, buffer, p_proof, p_buff_helper); + commit_stage( + p_starks, + element_type, + step, + witness as *mut std::os::raw::c_void, + buffer as *mut std::os::raw::c_void, + p_proof, + p_buff_helper as *mut std::os::raw::c_void, + ); } } #[cfg(not(feature = "no_lib_link"))] -pub fn compute_lev_c(p_stark: *mut c_void, xi_challenge: *mut c_void, lev: *mut c_void) { +pub fn compute_lev_c(p_stark: *mut c_void, xi_challenge: *mut c_void, lev: *mut u8) { unsafe { - compute_lev(p_stark, xi_challenge, lev); + compute_lev(p_stark, xi_challenge, lev as *mut std::os::raw::c_void); } } #[cfg(not(feature = "no_lib_link"))] -pub fn compute_evals_c(p_stark: *mut c_void, params: *mut c_void, lev: *mut c_void, pProof: *mut c_void) { +pub fn compute_evals_c(p_stark: *mut c_void, params: *mut u8, lev: *mut u8, pProof: *mut c_void) { unsafe { - compute_evals(p_stark, params, lev, pProof); + compute_evals(p_stark, params as *mut std::os::raw::c_void, lev as *mut std::os::raw::c_void, pProof); } } #[cfg(not(feature = "no_lib_link"))] pub fn compute_fri_folding_c( step: u64, - buffer: *mut c_void, - challenge: *mut c_void, + buffer: *mut u8, + challenge: *mut u8, n_bits_ext: u64, prev_bits: u64, current_bits: u64, ) { unsafe { - compute_fri_folding(step, buffer, challenge, n_bits_ext, prev_bits, current_bits); + compute_fri_folding( + step, + buffer as *mut std::os::raw::c_void, + challenge as *mut std::os::raw::c_void, + n_bits_ext, + prev_bits, + current_bits, + ); } } @@ -570,12 +628,12 @@ pub fn compute_fri_merkelize_c( p_starks: *mut c_void, p_proof: *mut c_void, step: u64, - buffer: *mut c_void, + buffer: *mut u8, current_bits: u64, next_bits: u64, ) { unsafe { - compute_fri_merkelize(p_starks, p_proof, step, buffer, current_bits, next_bits); + compute_fri_merkelize(p_starks, p_proof, step, buffer as *mut std::os::raw::c_void, current_bits, next_bits); } } @@ -607,16 +665,16 @@ pub fn compute_fri_queries_c( } #[cfg(not(feature = "no_lib_link"))] -pub fn set_fri_final_pol_c(p_proof: *mut c_void, buffer: *mut c_void, n_bits: u64) { +pub fn set_fri_final_pol_c(p_proof: *mut c_void, buffer: *mut u8, n_bits: u64) { unsafe { - set_fri_final_pol(p_proof, buffer, n_bits); + set_fri_final_pol(p_proof, buffer as *mut std::os::raw::c_void, n_bits); } } #[cfg(not(feature = "no_lib_link"))] -pub fn calculate_hash_c(pStarks: *mut c_void, pHhash: *mut c_void, pBuffer: *mut c_void, nElements: u64) { +pub fn calculate_hash_c(pStarks: *mut c_void, pHhash: *mut u8, pBuffer: *mut u8, nElements: u64) { unsafe { - calculate_hash(pStarks, pHhash, pBuffer, nElements); + calculate_hash(pStarks, pHhash as *mut std::os::raw::c_void, pBuffer as *mut std::os::raw::c_void, nElements); } } @@ -626,9 +684,9 @@ pub fn transcript_new_c(element_type: u32, arity: u64, custom: bool) -> *mut c_v } #[cfg(not(feature = "no_lib_link"))] -pub fn transcript_add_c(p_transcript: *mut c_void, p_input: *mut c_void, size: u64) { +pub fn transcript_add_c(p_transcript: *mut c_void, p_input: *mut u8, size: u64) { unsafe { - transcript_add(p_transcript, p_input, size); + transcript_add(p_transcript, p_input as *mut std::os::raw::c_void, size); } } @@ -661,62 +719,121 @@ pub fn get_permutations_c(p_transcript: *mut c_void, res: *mut u64, n: u64, n_bi } #[cfg(not(feature = "no_lib_link"))] -pub fn verify_constraints_c(p_setup: *mut c_void, p_steps_params: *mut c_void) -> *mut c_void { - unsafe { verify_constraints(p_setup, p_steps_params) } +pub fn get_n_constraints_c(p_setup: *mut c_void) -> u64 { + unsafe { get_n_constraints(p_setup) } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_constraints_lines_sizes_c(p_setup: *mut c_void, constraints_sizes: *mut u64) { + unsafe { + get_constraints_lines_sizes(p_setup, constraints_sizes); + } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_constraints_lines_c(p_setup: *mut c_void, constraints_lines: *mut *mut u8) { + unsafe { + get_constraints_lines(p_setup, constraints_lines); + } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn verify_constraints_c(p_setup: *mut c_void, p_steps_params: *mut u8, constraints_info: *mut c_void) { + unsafe { + verify_constraints(p_setup, p_steps_params as *mut std::os::raw::c_void, constraints_info); + } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_n_global_constraints_c(p_global_constraints_bin: *mut c_void) -> u64 { + unsafe { get_n_global_constraints(p_global_constraints_bin) } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_global_constraints_lines_sizes_c(p_global_constraints_bin: *mut c_void, global_constraints_sizes: *mut u64) { + unsafe { + get_global_constraints_lines_sizes(p_global_constraints_bin, global_constraints_sizes); + } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn get_global_constraints_lines_c(p_global_constraints_bin: *mut c_void, global_constraints_lines: *mut *mut u8) { + unsafe { + get_global_constraints_lines(p_global_constraints_bin, global_constraints_lines); + } } #[cfg(not(feature = "no_lib_link"))] pub fn verify_global_constraints_c( + global_info_file: &str, p_global_constraints_bin: *mut c_void, - publics: *mut c_void, - challenges: *mut c_void, - proof_values: *mut c_void, - airgroupvalues: *mut *mut c_void, -) -> bool { - unsafe { verify_global_constraints(p_global_constraints_bin, publics, challenges, proof_values, airgroupvalues) } + publics: *mut u8, + challenges: *mut u8, + proof_values: *mut u8, + airgroupvalues: *mut *mut u8, + global_constraints_info: *mut c_void, +) { + let global_info_file_name = CString::new(global_info_file).unwrap(); + let global_info_file_ptr = global_info_file_name.as_ptr() as *mut std::os::raw::c_char; + + unsafe { + verify_global_constraints( + global_info_file_ptr, + p_global_constraints_bin, + publics as *mut std::os::raw::c_void, + challenges as *mut std::os::raw::c_void, + proof_values as *mut std::os::raw::c_void, + airgroupvalues as *mut *mut std::os::raw::c_void, + global_constraints_info, + ); + } } #[cfg(not(feature = "no_lib_link"))] #[allow(clippy::too_many_arguments)] pub fn get_hint_field_global_constraints_c( + global_info_file: &str, p_global_constraints_bin: *mut c_void, - publics: *mut c_void, - challenges: *mut c_void, - proof_values: *mut c_void, - airgroupvalues: *mut *mut c_void, + hint_field_values: *mut c_void, + publics: *mut u8, + challenges: *mut u8, + proof_values: *mut u8, + airgroupvalues: *mut *mut u8, hint_id: u64, hint_field_name: &str, print_expression: bool, -) -> *mut c_void { +) { let field_name = CString::new(hint_field_name).unwrap(); + + let global_info_file_name = CString::new(global_info_file).unwrap(); + let global_info_file_ptr = global_info_file_name.as_ptr() as *mut std::os::raw::c_char; + unsafe { get_hint_field_global_constraints( + global_info_file_ptr, p_global_constraints_bin, - publics, - challenges, - proof_values, - airgroupvalues, + hint_field_values, + publics as *mut std::os::raw::c_void, + challenges as *mut std::os::raw::c_void, + proof_values as *mut std::os::raw::c_void, + airgroupvalues as *mut *mut std::os::raw::c_void, hint_id, field_name.as_ptr() as *mut std::os::raw::c_char, print_expression, - ) + ); } } #[cfg(not(feature = "no_lib_link"))] -pub fn set_hint_field_global_constraints_c( +pub fn get_hint_field_global_constraints_values_c( p_global_constraints_bin: *mut c_void, - proof_values: *mut c_void, - values: *mut c_void, hint_id: u64, hint_field_name: &str, ) -> u64 { let field_name = CString::new(hint_field_name).unwrap(); unsafe { - set_hint_field_global_constraints( + get_hint_field_global_constraints_values( p_global_constraints_bin, - proof_values, - values, hint_id, field_name.as_ptr() as *mut std::os::raw::c_char, ) @@ -724,22 +841,61 @@ pub fn set_hint_field_global_constraints_c( } #[cfg(not(feature = "no_lib_link"))] -pub fn print_expression_c( - p_setup_ctx: *mut c_void, - pol: *mut c_void, - dim: u64, - first_print_value: u64, - last_print_value: u64, +pub fn get_hint_field_global_constraints_sizes_c( + global_info_file: &str, + p_global_constraints_bin: *mut c_void, + hint_field_values: *mut c_void, + hint_id: u64, + hint_field_name: &str, + print_expression: bool, ) { + let field_name = CString::new(hint_field_name).unwrap(); + + let global_info_file_name = CString::new(global_info_file).unwrap(); + let global_info_file_ptr = global_info_file_name.as_ptr() as *mut std::os::raw::c_char; + + unsafe { + get_hint_field_global_constraints_sizes( + global_info_file_ptr, + p_global_constraints_bin, + hint_field_values, + hint_id, + field_name.as_ptr() as *mut std::os::raw::c_char, + print_expression, + ); + } +} + +#[cfg(not(feature = "no_lib_link"))] +pub fn set_hint_field_global_constraints_c( + global_info_file: &str, + p_global_constraints_bin: *mut c_void, + proof_values: *mut u8, + values: *mut u8, + hint_id: u64, + hint_field_name: &str, +) -> u64 { + let field_name = CString::new(hint_field_name).unwrap(); + + let global_info_file_name = CString::new(global_info_file).unwrap(); + let global_info_file_ptr = global_info_file_name.as_ptr() as *mut std::os::raw::c_char; + unsafe { - print_expression(p_setup_ctx, pol, dim, first_print_value, last_print_value); + set_hint_field_global_constraints( + global_info_file_ptr, + p_global_constraints_bin, + proof_values as *mut std::os::raw::c_void, + values as *mut std::os::raw::c_void, + hint_id, + field_name.as_ptr() as *mut std::os::raw::c_char, + ) } } #[cfg(not(feature = "no_lib_link"))] -pub fn print_row_c(p_setup_ctx: *mut c_void, buffer: *mut c_void, stage: u64, row: u64) { +pub fn print_row_c(p_setup_ctx: *mut c_void, buffer: *mut u8, stage: u64, row: u64) { unsafe { - print_row(p_setup_ctx, buffer, stage, row); + print_row(p_setup_ctx, buffer as *mut std::os::raw::c_void, stage, row); } } @@ -747,10 +903,10 @@ pub fn print_row_c(p_setup_ctx: *mut c_void, buffer: *mut c_void, stage: u64, ro #[allow(clippy::too_many_arguments)] pub fn gen_recursive_proof_c( p_setup_ctx: *mut c_void, - p_address: *mut c_void, - p_const_pols: *mut c_void, - p_const_tree: *mut c_void, - p_public_inputs: *mut c_void, + p_address: *mut u8, + p_const_pols: *mut u8, + p_const_tree: *mut u8, + p_public_inputs: *mut u8, proof_file: &str, global_info_file: &str, airgroup_id: u64, @@ -767,10 +923,10 @@ pub fn gen_recursive_proof_c( p_setup_ctx, global_info_file_ptr, airgroup_id, - p_address, - p_const_pols, - p_const_tree, - p_public_inputs, + p_address as *mut std::os::raw::c_void, + p_const_pols as *mut std::os::raw::c_void, + p_const_tree as *mut std::os::raw::c_void, + p_public_inputs as *mut std::os::raw::c_void, proof_file_ptr, vadcop, ) @@ -795,9 +951,9 @@ pub fn add_recursive2_verkey_c(p_zkin: *mut c_void, recursive2_verkey: &str) -> #[cfg(not(feature = "no_lib_link"))] pub fn join_zkin_final_c( - p_publics: *mut c_void, - p_proof_values: *mut c_void, - p_challenges: *mut c_void, + p_publics: *mut u8, + p_proof_values: *mut u8, + p_challenges: *mut u8, global_info_file: &str, zkin_recursive2: *mut *mut c_void, stark_info_recursive2: *mut *mut c_void, @@ -807,9 +963,9 @@ pub fn join_zkin_final_c( unsafe { join_zkin_final( - p_publics, - p_proof_values, - p_challenges, + p_publics as *mut std::os::raw::c_void, + p_proof_values as *mut std::os::raw::c_void, + p_challenges as *mut std::os::raw::c_void, global_info_file_ptr, zkin_recursive2, stark_info_recursive2, @@ -820,8 +976,8 @@ pub fn join_zkin_final_c( #[cfg(not(feature = "no_lib_link"))] pub fn join_zkin_recursive2_c( airgroup_id: u64, - p_publics: *mut c_void, - p_challenges: *mut c_void, + p_publics: *mut u8, + p_challenges: *mut u8, global_info_file: &str, zkin1: *mut c_void, zkin2: *mut c_void, @@ -834,8 +990,8 @@ pub fn join_zkin_recursive2_c( join_zkin_recursive2( global_info_file_ptr, airgroup_id, - p_publics, - p_challenges, + p_publics as *mut std::os::raw::c_void, + p_challenges as *mut std::os::raw::c_void, zkin1, zkin2, stark_info_recursive2, @@ -881,30 +1037,38 @@ pub fn serialized_proof_free_c(zkin_cstr: *mut std::os::raw::c_char) { #[cfg(not(feature = "no_lib_link"))] #[allow(clippy::too_many_arguments)] pub fn get_committed_pols_c( - pWitness: *mut ::std::os::raw::c_void, - execFile: *mut ::std::os::raw::c_char, - pAddress: *mut ::std::os::raw::c_void, - pPublics: *mut ::std::os::raw::c_void, + circomWitness: *mut u8, + execFile: *const i8, + witness: *mut u8, + pPublics: *mut u8, sizeWitness: u64, N: u64, nPublics: u64, - offsetCm1: u64, nCols: u64, ) { unsafe { - get_committed_pols(pWitness, execFile, pAddress, pPublics, sizeWitness, N, nPublics, offsetCm1, nCols); + get_committed_pols( + circomWitness as *mut std::os::raw::c_void, + execFile as *mut std::os::raw::c_char, + witness as *mut std::os::raw::c_void, + pPublics as *mut std::os::raw::c_void, + sizeWitness, + N, + nPublics, + nCols, + ); } } #[cfg(not(feature = "no_lib_link"))] -pub fn gen_final_snark_proof_c(pWitnessFinal: *mut ::std::os::raw::c_void, zkeyFile: &str, outputDir: &str) { +pub fn gen_final_snark_proof_c(circomWitnessFinal: *mut u8, zkeyFile: &str, outputDir: &str) { let zkey_file_name = CString::new(zkeyFile).unwrap(); let zkey_file_ptr = zkey_file_name.as_ptr() as *mut std::os::raw::c_char; let output_dir_name = CString::new(outputDir).unwrap(); let output_dir_ptr = output_dir_name.as_ptr() as *mut std::os::raw::c_char; unsafe { - gen_final_snark_proof(pWitnessFinal, zkey_file_ptr, output_dir_ptr); + gen_final_snark_proof(circomWitnessFinal as *mut std::os::raw::c_void, zkey_file_ptr, output_dir_ptr); } } @@ -919,17 +1083,17 @@ pub fn set_log_level_c(level: u64) { // MOCK METHODS FOR TESTING // ------------------------ #[cfg(feature = "no_lib_link")] -pub fn save_challenges_c(_p_challenges: *mut std::os::raw::c_void, _global_info_file: &str, _output_dir: &str) { +pub fn save_challenges_c(_p_challenges: *mut u8, _global_info_file: &str, _output_dir: &str) { trace!("{}: ··· {}", "ffi ", "save_challenges: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn save_publics_c(_n_publics: u64, _public_inputs: *mut c_void, _output_dir: &str) { +pub fn save_publics_c(_n_publics: u64, _public_inputs: *mut u8, _output_dir: &str) { trace!("{}: ··· {}", "ffi ", "save_publics: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn save_proof_values_c(_n_proof_values: u64, _proof_values: *mut c_void, _output_dir: &str) { +pub fn save_proof_values_c(_proof_values: *mut u8, _global_info_file: &str, _output_dir: &str) { trace!("{}: ··· {}", "ffi ", "save_proof_values: This is a mock call because there is no linked library"); } @@ -940,13 +1104,13 @@ pub fn fri_proof_new_c(_p_setup_ctx: *mut c_void) -> *mut c_void { } #[cfg(feature = "no_lib_link")] -pub fn fri_proof_get_tree_root_c(_p_fri_proof: *mut c_void, _root: *mut c_void, _tree_index: u64) -> *mut c_void { +pub fn fri_proof_get_tree_root_c(_p_fri_proof: *mut c_void, _root: *mut u8, _tree_index: u64) -> *mut c_void { trace!("{}: ··· {}", "ffi ", "fri_proof_get_tree_root: This is a mock call because there is no linked library"); std::ptr::null_mut() } #[cfg(feature = "no_lib_link")] -pub fn fri_proof_set_airgroup_values_c(_p_fri_proof: *mut c_void, _p_params: *mut c_void) { +pub fn fri_proof_set_airgroup_values_c(_p_fri_proof: *mut c_void, _p_params: *mut u8) { trace!( "{}: ··· {}", "ffi ", @@ -955,7 +1119,7 @@ pub fn fri_proof_set_airgroup_values_c(_p_fri_proof: *mut c_void, _p_params: *mu } #[cfg(feature = "no_lib_link")] -pub fn fri_proof_set_air_values_c(_p_fri_proof: *mut c_void, _p_params: *mut c_void) { +pub fn fri_proof_set_air_values_c(_p_fri_proof: *mut c_void, _p_params: *mut u8) { trace!( "{}: ··· {}", "ffi ", @@ -967,9 +1131,9 @@ pub fn fri_proof_set_air_values_c(_p_fri_proof: *mut c_void, _p_params: *mut c_v #[allow(clippy::too_many_arguments)] pub fn fri_proof_get_zkinproof_c( _p_fri_proof: *mut c_void, - _p_publics: *mut c_void, - _p_challenges: *mut c_void, - _p_proof_values: *mut c_void, + _p_publics: *mut u8, + _p_challenges: *mut u8, + _p_proof_values: *mut u8, _p_stark_info: *mut c_void, _proof_name: &str, _global_info_file: &str, @@ -1005,38 +1169,12 @@ pub fn get_map_totaln_c(_p_stark_info: *mut c_void) -> u64 { 100000000 } -#[cfg(feature = "no_lib_link")] -pub fn get_map_totaln_custom_commits_c(_p_stark_info: *mut c_void, _commit_id: u64) -> u64 { - trace!( - "{}: ··· {}", - "ffi ", - "get_map_totaln_custom_commits: This is a mock call because there is no linked library" - ); - 100000000 -} - #[cfg(feature = "no_lib_link")] pub fn get_custom_commit_id_c(_p_stark_info: *mut c_void, _name: &str) -> u64 { trace!("{}: ··· {}", "ffi ", "get_custom_commit_id: This is a mock call because there is no linked library"); 100000000 } -#[cfg(feature = "no_lib_link")] -pub fn get_custom_commit_map_ids_c(_p_stark_info: *mut c_void, _commit_id: u64, _stage: u64) -> Vec { - trace!( - "{}: ··· {}", - "ffi ", - "get_custom_commit_map_ids: This is a mock call because there is no linked library" - ); - Vec::new() -} - -#[cfg(feature = "no_lib_link")] -pub fn get_map_offsets_c(_p_stark_info: *mut c_void, _stage: &str, _flag: bool) -> u64 { - trace!("{}: ··· {}", "ffi ", "get_map_offsets: This is a mock call because there is no linked library"); - 0 -} - #[cfg(feature = "no_lib_link")] pub fn stark_info_free_c(_p_stark_info: *mut c_void) { trace!("{}: ··· {}", "ffi ", "starkinfo_free: This is a mock call because there is no linked library"); @@ -1052,7 +1190,7 @@ pub fn prover_helpers_new_c(_p_stark_info: *mut c_void) -> *mut c_void { pub fn prover_helpers_free_c(_p_prover_helpers: *mut c_void) {} #[cfg(feature = "no_lib_link")] -pub fn load_const_pols_c(_pConstPolsAddress: *mut c_void, _const_filename: &str, _const_size: u64) { +pub fn load_const_pols_c(_pConstPolsAddress: *mut u8, _const_filename: &str, _const_size: u64) { trace!("{}: ··· {}", "ffi ", "load_const_pols: This is a mock call because there is no linked library"); } @@ -1069,15 +1207,15 @@ pub fn get_const_size_c(_pStarkInfo: *mut c_void) -> u64 { } #[cfg(feature = "no_lib_link")] -pub fn load_const_tree_c(_pConstPolsTreeAddress: *mut c_void, _tree_filename: &str, _const_tree_size: u64) { +pub fn load_const_tree_c(_pConstPolsTreeAddress: *mut u8, _tree_filename: &str, _const_tree_size: u64) { trace!("{}: ··· {}", "ffi ", "load_const_tree: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] pub fn calculate_const_tree_c( _pStarkInfo: *mut c_void, - _pConstPols: *mut c_void, - _pConstPolsTreeAddress: *mut c_void, + _pConstPols: *mut u8, + _pConstPolsTreeAddress: *mut u8, _tree_filename: &str, ) { trace!("{}: ··· {}", "ffi ", "calculate_const_tree: This is a mock call because there is no linked library"); @@ -1092,34 +1230,56 @@ pub fn expressions_bin_new_c(_filename: &str, _global: bool) -> *mut c_void { pub fn expressions_bin_free_c(_p_expressions_bin: *mut c_void) {} #[cfg(feature = "no_lib_link")] -pub fn get_hint_ids_by_name_c(_p_expressions_bin: *mut c_void, _hint_name: &str) -> *mut c_void { +pub fn n_hint_ids_by_name_c(_p_expressions_bin: *mut c_void, _hint_name: &str) -> u64 { + trace!("{}: ··· {}", "ffi ", "n_hint_ids_by_name: This is a mock call because there is no linked library"); + 0 +} + +#[cfg(feature = "no_lib_link")] +pub fn get_hint_ids_by_name_c(_p_expressions_bin: *mut c_void, _hint_ids: *mut u64, _hint_name: &str) { trace!("{}: ··· {}", "ffi ", "get_hint_ids_by_name: This is a mock call because there is no linked library"); - std::ptr::null_mut() } #[cfg(feature = "no_lib_link")] pub fn get_hint_field_c( _p_setup_ctx: *mut c_void, - _p_steps_params: *mut c_void, + _p_steps_params: *mut u8, + _hint_field_values: *mut c_void, _hint_id: u64, _hint_field_name: &str, - _hint_options: *mut c_void, -) -> *mut c_void { + _hint_options: *mut u8, +) { trace!("{}: ··· {}", "ffi ", "get_hint_field: This is a mock call because there is no linked library"); - std::ptr::null_mut() +} + +#[cfg(feature = "no_lib_link")] +pub fn get_hint_field_sizes_c( + _p_setup_ctx: *mut c_void, + _hint_field_values: *mut c_void, + _hint_id: u64, + _hint_field_name: &str, + _hint_options: *mut u8, +) { + trace!("{}: ··· {}", "ffi ", "get_hint_field_sizes: This is a mock call because there is no linked library"); +} + +#[cfg(feature = "no_lib_link")] +pub fn get_hint_field_values_c(_p_setup_ctx: *mut c_void, _hint_id: u64, _hint_field_name: &str) -> u64 { + trace!("{}: ··· {}", "ffi ", "get_hint_field: This is a mock call because there is no linked library"); + 0 } #[cfg(feature = "no_lib_link")] #[allow(clippy::too_many_arguments)] pub fn mul_hint_fields_c( _p_setup_ctx: *mut c_void, - _p_steps_params: *mut c_void, + _p_steps_params: *mut u8, _hint_id: u64, _hint_field_dest: &str, _hint_field_name1: &str, _hint_field_name2: &str, - _hint_options1: *mut c_void, - _hint_options2: *mut c_void, + _hint_options1: *mut u8, + _hint_options2: *mut u8, ) -> u64 { trace!("{}: ··· {}", "ffi ", "mul_hint_fields: This is a mock call because there is no linked library"); 0 @@ -1128,57 +1288,55 @@ pub fn mul_hint_fields_c( #[cfg(feature = "no_lib_link")] pub fn acc_hint_field_c( _p_setup_ctx: *mut c_void, - _p_steps_params: *mut c_void, + _p_steps_params: *mut u8, _hint_id: u64, _hint_field_dest: &str, _hint_field_airgroupvalue: &str, _hint_field_name: &str, _add: bool, -) -> *mut c_void { +) { trace!("{}: ··· {}", "ffi ", "acc_hint_fields: This is a mock call because there is no linked library"); - std::ptr::null_mut() } #[cfg(feature = "no_lib_link")] #[allow(clippy::too_many_arguments)] pub fn acc_mul_hint_fields_c( _p_setup_ctx: *mut c_void, - _p_steps_params: *mut c_void, + _p_steps_params: *mut u8, _hint_id: u64, _hint_field_dest: &str, _hint_field_airgroupvalue: &str, _hint_field_name1: &str, _hint_field_name2: &str, - _hint_options1: *mut c_void, - _hint_options2: *mut c_void, + _hint_options1: *mut u8, + _hint_options2: *mut u8, _add: bool, -) -> *mut c_void { +) { trace!("{}: ··· {}", "ffi ", "acc_mul_hint_fields: This is a mock call because there is no linked library"); - std::ptr::null_mut() } #[cfg(feature = "no_lib_link")] #[allow(clippy::too_many_arguments)] pub fn update_airgroupvalue_c( _p_setup_ctx: *mut c_void, - _p_steps_params: *mut c_void, + _p_steps_params: *mut u8, _hint_id: u64, _hint_field_airgroupvalue: &str, _hint_field_name1: &str, _hint_field_name2: &str, - _hint_options1: *mut c_void, - _hint_options2: *mut c_void, + _hint_options1: *mut u8, + _hint_options2: *mut u8, _add: bool, -) -> *mut c_void { +) -> u64 { trace!("{}: ··· {}", "ffi ", "update_airgroupvalue: This is a mock call because there is no linked library"); - std::ptr::null_mut() + 10000 } #[cfg(feature = "no_lib_link")] pub fn set_hint_field_c( _p_setup_ctx: *mut c_void, - _p_params: *mut c_void, - _values: *mut c_void, + _p_params: *mut u8, + _values: *mut u8, _hint_id: u64, _hint_field_name: &str, ) -> u64 { @@ -1187,7 +1345,13 @@ pub fn set_hint_field_c( } #[cfg(feature = "no_lib_link")] -pub fn starks_new_c(_p_setup: *mut c_void, _p_const_tree: *mut c_void) -> *mut c_void { +pub fn get_hint_field_id_c(_p_setup_ctx: *mut c_void, _hint_id: u64, _hint_field_name: &str) -> u64 { + trace!("{}: ··· {}", "ffi ", "get_hint_field_id: This is a mock call because there is no linked library"); + 0 +} + +#[cfg(feature = "no_lib_link")] +pub fn starks_new_c(_p_setup: *mut c_void, _p_const_tree: *mut u8) -> *mut c_void { trace!("{}: ··· {}", "ffi ", "starks_new: This is a mock call because there is no linked library"); std::ptr::null_mut() } @@ -1209,7 +1373,7 @@ pub fn merkle_tree_free_c(_merkle_tree: *mut c_void) { } #[cfg(feature = "no_lib_link")] -pub fn treesGL_get_root_c(_pStark: *mut c_void, _index: u64, _root: *mut c_void) { +pub fn treesGL_get_root_c(_pStark: *mut c_void, _index: u64, _root: *mut u8) { trace!("{}: ··· {}", "ffi ", "treesGL_get_root: This is a mock call because there is no linked library"); } @@ -1219,17 +1383,17 @@ pub fn treesGL_set_root_c(_pStark: *mut c_void, _index: u64, _pProof: *mut c_voi } #[cfg(feature = "no_lib_link")] -pub fn calculate_fri_polynomial_c(_p_starks: *mut c_void, _p_steps_params: *mut c_void) { +pub fn calculate_fri_polynomial_c(_p_starks: *mut c_void, _p_steps_params: *mut u8) { trace!("mckzkevm: ··· {}", "calculate_fri_polynomial: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn calculate_quotient_polynomial_c(_p_starks: *mut c_void, _p_steps_params: *mut c_void) { +pub fn calculate_quotient_polynomial_c(_p_starks: *mut c_void, _p_steps_params: *mut u8) { trace!("mckzkevm: ··· {}", "calculate_quotient_polynomial: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn calculate_impols_expressions_c(_p_starks: *mut c_void, _step: u64, _p_steps_params: *mut c_void) { +pub fn calculate_impols_expressions_c(_p_starks: *mut c_void, _step: u64, _p_steps_params: *mut u8) { trace!( "{}: ··· {}", "mckzkevm", @@ -1238,13 +1402,15 @@ pub fn calculate_impols_expressions_c(_p_starks: *mut c_void, _step: u64, _p_ste } #[cfg(feature = "no_lib_link")] +#[allow(clippy::too_many_arguments)] pub fn extend_and_merkelize_custom_commit_c( _p_starks: *mut c_void, _commit_id: u64, _step: u64, - _buffer: *mut c_void, + _buffer: *mut u8, + _buffer_ext: *mut u8, _p_proof: *mut c_void, - _p_buff_helper: *mut c_void, + _p_buff_helper: *mut u8, _tree_file: &str, ) { trace!( @@ -1259,7 +1425,8 @@ pub fn load_custom_commit_c( _p_starks: *mut c_void, _commit_id: u64, _step: u64, - _buffer: *mut c_void, + _buffer: *mut u8, + _buffer_ext: *mut u8, _p_proof: *mut c_void, _tree_file: &str, ) { @@ -1271,31 +1438,31 @@ pub fn commit_stage_c( _p_starks: *mut c_void, _element_type: u32, _step: u64, - _trace: *mut c_void, - _buffer: *mut c_void, + _witness: *mut u8, + _buffer: *mut u8, _p_proof: *mut c_void, - _p_buff_helper: *mut c_void, + _p_buff_helper: *mut u8, ) { trace!("{}: ··· {}", "ffi ", "commit_stage: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn compute_lev_c(_p_stark: *mut c_void, _xi_challenge: *mut c_void, _lev: *mut c_void) { +pub fn compute_lev_c(_p_stark: *mut c_void, _xi_challenge: *mut c_void, _lev: *mut u8) { trace!("{}: ··· {}", "ffi ", "compute_lev: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn compute_evals_c(_p_stark: *mut c_void, _params: *mut c_void, _lev: *mut c_void, _pProof: *mut c_void) { +pub fn compute_evals_c(_p_stark: *mut c_void, _params: *mut u8, _lev: *mut u8, _pProof: *mut c_void) { trace!("{}: ··· {}", "ffi ", "compute_evals: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn calculate_xdivxsub_c(_p_stark: *mut c_void, _xi_challenge: *mut c_void, _buffer: *mut c_void) { +pub fn calculate_xdivxsub_c(_p_stark: *mut c_void, _xi_challenge: *mut c_void, _buffer: *mut u8) { trace!("{}: ··· {}", "ffi ", "calculate_xdivxsub: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn get_fri_pol_c(_p_stark_info: *mut c_void, _buffer: *mut c_void) -> *mut c_void { +pub fn get_fri_pol_c(_p_stark_info: *mut c_void, _buffer: *mut u8) -> *mut c_void { trace!("ffi : ··· {}", "get_fri_pol: This is a mock call because there is no linked library"); std::ptr::null_mut() } @@ -1303,8 +1470,8 @@ pub fn get_fri_pol_c(_p_stark_info: *mut c_void, _buffer: *mut c_void) -> *mut c #[cfg(feature = "no_lib_link")] pub fn compute_fri_folding_c( _step: u64, - _buffer: *mut c_void, - _challenge: *mut c_void, + _buffer: *mut u8, + _challenge: *mut u8, _n_bits_ext: u64, _prev_bits: u64, _current_bits: u64, @@ -1317,7 +1484,7 @@ pub fn compute_fri_merkelize_c( _p_starks: *mut c_void, _p_proof: *mut c_void, _step: u64, - _buffer: *mut c_void, + _buffer: *mut u8, _current_bits: u64, _next_bits: u64, ) { @@ -1348,12 +1515,12 @@ pub fn compute_fri_queries_c( } #[cfg(feature = "no_lib_link")] -pub fn set_fri_final_pol_c(_p_proof: *mut c_void, _buffer: *mut c_void, _n_bits: u64) { +pub fn set_fri_final_pol_c(_p_proof: *mut c_void, _buffer: *mut u8, _n_bits: u64) { trace!("{}: ··· {}", "ffi ", "set_fri_final_pol: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn calculate_hash_c(_pStarks: *mut c_void, _pHhash: *mut c_void, _pBuffer: *mut c_void, _nElements: u64) { +pub fn calculate_hash_c(_pStarks: *mut c_void, _pHhash: *mut u8, _pBuffer: *mut u8, _nElements: u64) { trace!("{}: ··· {}", "ffi ", "calculate_hash: This is a mock call because there is no linked library"); } @@ -1364,7 +1531,7 @@ pub fn transcript_new_c(_element_type: u32, _arity: u64, _custom: bool) -> *mut } #[cfg(feature = "no_lib_link")] -pub fn transcript_add_c(_p_transcript: *mut c_void, _p_input: *mut c_void, _size: u64) { +pub fn transcript_add_c(_p_transcript: *mut c_void, _p_input: *mut u8, _size: u64) { trace!("{}: ··· {}", "ffi ", "transcript_add: This is a mock call because there is no linked library"); } @@ -1393,76 +1560,148 @@ pub fn get_permutations_c(_p_transcript: *mut c_void, _res: *mut u64, _n: u64, _ } #[cfg(feature = "no_lib_link")] -pub fn verify_constraints_c(_p_setup: *mut c_void, _p_steps_params: *mut c_void) -> *mut c_void { +pub fn get_n_constraints_c(_p_setup: *mut c_void) -> u64 { + trace!("{}: ··· {}", "ffi ", "get_n_constraints: This is a mock call because there is no linked library"); + 0 +} + +#[cfg(feature = "no_lib_link")] +pub fn get_constraints_lines_sizes_c(_p_setup: *mut c_void, _constraints_sizes: *mut u64) { + trace!( + "{}: ··· {}", + "ffi ", + "get_constraints_lines_sizes: This is a mock call because there is no linked library" + ); +} + +#[cfg(feature = "no_lib_link")] +pub fn get_constraints_lines_c(_p_setup: *mut c_void, _constraints_lines: *mut *mut u8) { + trace!("{}: ··· {}", "ffi ", "get_constraints_lines: This is a mock call because there is no linked library"); +} + +#[cfg(feature = "no_lib_link")] +pub fn verify_constraints_c(_p_setup: *mut c_void, _p_steps_params: *mut u8, _constraints_info: *mut c_void) { trace!("{}: ··· {}", "ffi ", "verify_constraints: This is a mock call because there is no linked library"); - std::ptr::null_mut() +} + +#[cfg(feature = "no_lib_link")] +pub fn get_n_global_constraints_c(_p_global_constraints_bin: *mut c_void) -> u64 { + trace!( + "{}: ··· {}", + "ffi ", + "get_n_global_constraints: This is a mock call because there is no linked library" + ); + 0 +} + +#[cfg(feature = "no_lib_link")] +pub fn get_global_constraints_lines_sizes_c( + _p_global_constraints_bin: *mut c_void, + _global_constraints_sizes: *mut u64, +) { + trace!( + "{}: ··· {}", + "ffi ", + "get_global_constraints_lines_sizes: This is a mock call because there is no linked library" + ); +} + +#[cfg(feature = "no_lib_link")] +pub fn get_global_constraints_lines_c(_p_global_constraints_bin: *mut c_void, _global_constraints_lines: *mut *mut u8) { + trace!( + "{}: ··· {}", + "ffi ", + "get_global_constraints_lines: This is a mock call because there is no linked library" + ); } #[cfg(feature = "no_lib_link")] pub fn verify_global_constraints_c( + _global_info_file: &str, _p_global_constraints_bin: *mut c_void, - _publics: *mut c_void, - _challenges: *mut c_void, - _proof_values: *mut c_void, - _airgroupvalues: *mut *mut c_void, -) -> bool { + _publics: *mut u8, + _challenges: *mut u8, + _proof_values: *mut u8, + _airgroupvalues: *mut *mut u8, + _global_constraints_info: *mut c_void, +) { trace!( "{}: ··· {}", "ffi ", "verify_global_constraints: This is a mock call because there is no linked library" ); - true } #[cfg(feature = "no_lib_link")] #[allow(clippy::too_many_arguments)] pub fn get_hint_field_global_constraints_c( + _global_info_file: &str, _p_global_constraints_bin: *mut c_void, - _publics: *mut c_void, - _challenges: *mut c_void, - _proof_values: *mut c_void, - _airgroupvalues: *mut *mut c_void, + _hint_field_values: *mut c_void, + _publics: *mut u8, + _challenges: *mut u8, + _proof_values: *mut u8, + _airgroupvalues: *mut *mut u8, _hint_id: u64, _hint_field_name: &str, _print_expression: bool, -) -> *mut c_void { +) { trace!( "{}: ··· {}", "ffi ", "get_hint_field_global_constraints: This is a mock call because there is no linked library" ); - std::ptr::null_mut() } #[cfg(feature = "no_lib_link")] -pub fn set_hint_field_global_constraints_c( +pub fn get_hint_field_global_constraints_values_c( _p_global_constraints_bin: *mut c_void, - _proof_values: *mut c_void, - _values: *mut c_void, _hint_id: u64, _hint_field_name: &str, ) -> u64 { trace!( "{}: ··· {}", "ffi ", - "set_hint_field_global_constraints: This is a mock call because there is no linked library" + "get_hint_field_global_constraints_values: This is a mock call because there is no linked library" ); - 100000 + 0 } #[cfg(feature = "no_lib_link")] -pub fn print_expression_c( - _p_setup_ctx: *mut c_void, - _pol: *mut c_void, - _dim: u64, - _first_print_value: u64, - _last_print_value: u64, +pub fn get_hint_field_global_constraints_sizes_c( + _global_info_file: &str, + _p_global_constraints_bin: *mut c_void, + _hint_field_values: *mut c_void, + _hint_id: u64, + _hint_field_name: &str, + _print_expression: bool, ) { - trace!("{}: ··· {}", "ffi ", "print_expression: This is a mock call because there is no linked library"); + trace!( + "{}: ··· {}", + "ffi ", + "get_hint_field_global_constraints_sizes: This is a mock call because there is no linked library" + ); +} + +#[cfg(feature = "no_lib_link")] +pub fn set_hint_field_global_constraints_c( + _global_info_file: &str, + _p_global_constraints_bin: *mut c_void, + _proof_values: *mut u8, + _values: *mut u8, + _hint_id: u64, + _hint_field_name: &str, +) -> u64 { + trace!( + "{}: ··· {}", + "ffi ", + "set_hint_field_global_constraints: This is a mock call because there is no linked library" + ); + 100000 } #[cfg(feature = "no_lib_link")] -pub fn print_row_c(_p_setup_ctx: *mut c_void, _buffer: *mut c_void, _stage: u64, _row: u64) { +pub fn print_row_c(_p_setup_ctx: *mut c_void, _buffer: *mut u8, _stage: u64, _row: u64) { trace!("{}: ··· {}", "ffi ", "print_row: This is a mock call because there is no linked library"); } @@ -1470,10 +1709,10 @@ pub fn print_row_c(_p_setup_ctx: *mut c_void, _buffer: *mut c_void, _stage: u64, #[allow(clippy::too_many_arguments)] pub fn gen_recursive_proof_c( _p_setup_ctx: *mut c_void, - _p_address: *mut c_void, - _p_const_pols: *mut c_void, - _p_const_tree: *mut c_void, - _p_public_inputs: *mut c_void, + _p_address: *mut u8, + _p_const_pols: *mut u8, + _p_const_tree: *mut u8, + _p_public_inputs: *mut u8, _proof_file: &str, _global_info_file: &str, _airgroup_id: u64, @@ -1498,8 +1737,8 @@ pub fn add_recursive2_verkey_c(_p_zkin: *mut c_void, _recursive2_verkey: &str) - #[cfg(feature = "no_lib_link")] pub fn join_zkin_recursive2_c( _airgroup_id: u64, - _p_publics: *mut c_void, - _p_challenges: *mut c_void, + _p_publics: *mut u8, + _p_challenges: *mut u8, _global_info_file: &str, _zkin1: *mut c_void, _zkin2: *mut c_void, @@ -1511,9 +1750,9 @@ pub fn join_zkin_recursive2_c( #[cfg(feature = "no_lib_link")] pub fn join_zkin_final_c( - _p_publics: *mut c_void, - _p_proof_values: *mut c_void, - _p_challenges: *mut c_void, + _p_publics: *mut u8, + _p_proof_values: *mut u8, + _p_challenges: *mut u8, _global_info_file: &str, _zkin_recursive2: *mut *mut c_void, _stark_info_recursive2: *mut *mut c_void, @@ -1523,9 +1762,9 @@ pub fn join_zkin_final_c( } #[cfg(feature = "no_lib_link")] -pub fn get_serialized_proof_c(_zkin: *mut c_void, _size: *mut u64) -> *mut std::os::raw::c_char { +pub fn get_serialized_proof_c(_zkin: *mut c_void) -> (*mut std::os::raw::c_char, u64) { trace!("{}: ··· {}", "ffi ", "get_serialized_proof: This is a mock call because there is no linked library"); - std::ptr::null_mut() + (std::ptr::null_mut(), 0) } #[cfg(feature = "no_lib_link")] @@ -1553,21 +1792,20 @@ pub fn serialized_proof_free_c(_zkin_cstr: *mut std::os::raw::c_char) { #[cfg(feature = "no_lib_link")] #[allow(clippy::too_many_arguments)] pub fn get_committed_pols_c( - _pWitness: *mut ::std::os::raw::c_void, - _execFile: *mut ::std::os::raw::c_char, - _pAddress: *mut ::std::os::raw::c_void, - _pPublics: *mut ::std::os::raw::c_void, + _circomWitness: *mut u8, + _execFile: *const i8, + _witness: *mut u8, + _pPublics: *mut u8, _sizeWitness: u64, _N: u64, _nPublics: u64, - _offsetCm1: u64, _nCols: u64, ) { trace!("{}: ··· {}", "ffi ", "get_committed_pols: This is a mock call because there is no linked library"); } #[cfg(feature = "no_lib_link")] -pub fn gen_final_snark_proof_c(_pWitnessFinal: *mut ::std::os::raw::c_void, _zkeyFile: &str, _outputDir: &str) { +pub fn gen_final_snark_proof_c(_circomWitnessFinal: *mut u8, _zkeyFile: &str, _outputDir: &str) { trace!("{}: ··· {}", "ffi ", "gen_final_snark_proof: This is a mock call because there is no linked library"); } diff --git a/transcript/src/ffi_transcript.rs b/transcript/src/ffi_transcript.rs index d7c1b0c5..7b6d202d 100644 --- a/transcript/src/ffi_transcript.rs +++ b/transcript/src/ffi_transcript.rs @@ -20,7 +20,7 @@ impl FFITranscript { Self { element_type, p_stark, p_transcript } } - pub fn add_elements(&self, input: *mut c_void, size: usize) { + pub fn add_elements(&self, input: *mut u8, size: usize) { transcript_add_c(self.p_transcript, input, size as u64); } diff --git a/util/Cargo.toml b/util/Cargo.toml index 178956d5..f22b206a 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -4,5 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -sysinfo = "0.31.2" +sysinfo = "0.33" colored = "2" \ No newline at end of file diff --git a/witness/Cargo.toml b/witness/Cargo.toml new file mode 100644 index 00000000..c9795116 --- /dev/null +++ b/witness/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "witness" +version = "0.1.0" +edition = "2021" + +[dependencies] +proofman-common = { path = "../common" } +proofman-util = { path = "../util" } + +log.workspace = true +env_logger.workspace = true +p3-goldilocks.workspace = true +p3-field.workspace = true + +colored = "2" +mpi = { version = "0.8.0", optional = true } +serde_json = "1.0.68" + + +[features] +default = [] +no_lib_link = [] +distributed = [] diff --git a/witness/src/lib.rs b/witness/src/lib.rs new file mode 100644 index 00000000..b19d4b12 --- /dev/null +++ b/witness/src/lib.rs @@ -0,0 +1,7 @@ +mod witness_component; +mod witness_library; +mod witness_manager; + +pub use witness_component::*; +pub use witness_library::*; +pub use witness_manager::*; diff --git a/witness/src/witness_component.rs b/witness/src/witness_component.rs new file mode 100644 index 00000000..d587fb03 --- /dev/null +++ b/witness/src/witness_component.rs @@ -0,0 +1,15 @@ +use std::sync::Arc; + +use proofman_common::{ProofCtx, SetupCtx}; + +pub trait WitnessComponent: Send + Sync { + fn start_proof(&self, _pctx: Arc>, _sctx: Arc) {} + + fn execute(&self, _pctx: Arc>) {} + + fn debug(&self, _pctx: Arc>) {} + + fn calculate_witness(&self, _stage: u32, _pctx: Arc>, _sctx: Arc) {} + + fn end_proof(&self) {} +} diff --git a/witness/src/witness_library.rs b/witness/src/witness_library.rs new file mode 100644 index 00000000..a09fb226 --- /dev/null +++ b/witness/src/witness_library.rs @@ -0,0 +1,36 @@ +use std::{error::Error, sync::Arc}; + +use crate::WitnessManager; +use proofman_common::VerboseMode; + +/// This is the type of the function that is used to load a witness library. +pub type WitnessLibInitFn = fn(VerboseMode) -> Result>, Box>; + +pub trait WitnessLibrary { + fn register_witness(&mut self, wcm: Arc>); +} + +#[macro_export] +macro_rules! witness_library { + ($lib_name:ident, $field_type:ty) => { + // Define the struct + pub struct $lib_name; + + // Implement Default for the struct + impl Default for $lib_name { + fn default() -> Self { + $lib_name + } + } + + // Define the init_library function + #[no_mangle] + pub extern "Rust" fn init_library( + verbose_mode: proofman_common::VerboseMode, + ) -> Result>, Box> { + proofman_common::initialize_logger(verbose_mode); + + Ok(Box::new($lib_name::default())) + } + }; +} diff --git a/witness/src/witness_manager.rs b/witness/src/witness_manager.rs new file mode 100644 index 00000000..51bf8a39 --- /dev/null +++ b/witness/src/witness_manager.rs @@ -0,0 +1,91 @@ +use std::sync::{Arc, RwLock}; +use std::path::PathBuf; + +use proofman_common::{ProofCtx, SetupCtx}; +use proofman_util::{timer_start_info, timer_stop_and_log_info}; +use crate::WitnessComponent; + +pub struct WitnessManager { + components: RwLock>>>, + pctx: Arc>, + sctx: Arc, + rom_path: Option, + public_inputs_path: Option, +} + +impl WitnessManager { + const MY_NAME: &'static str = "WCMnager"; + + pub fn new( + pctx: Arc>, + sctx: Arc, + rom_path: Option, + public_inputs_path: Option, + ) -> Self { + WitnessManager { components: RwLock::new(Vec::new()), pctx, sctx, rom_path, public_inputs_path } + } + + pub fn register_component(&self, component: Arc>) { + self.components.write().unwrap().push(component); + } + + pub fn start_proof(&self) { + for component in self.components.read().unwrap().iter() { + component.start_proof(self.pctx.clone(), self.sctx.clone()); + } + } + + pub fn execute(&self) { + timer_start_info!(EXECUTE); + for component in self.components.read().unwrap().iter() { + component.execute(self.pctx.clone()); + } + timer_stop_and_log_info!(EXECUTE); + } + + pub fn debug(&self) { + for component in self.components.read().unwrap().iter() { + component.debug(self.pctx.clone()); + } + } + + pub fn end_proof(&self) { + for component in self.components.read().unwrap().iter() { + component.end_proof(); + } + } + + pub fn calculate_witness(&self, stage: u32) { + log::info!( + "{}: Calculating witness for stage {} / {}", + Self::MY_NAME, + stage, + self.pctx.global_info.n_challenges.len() + ); + + timer_start_info!(CALCULATING_WITNESS); + + // Call one time all unused components + for component in self.components.read().unwrap().iter() { + component.calculate_witness(stage, self.pctx.clone(), self.sctx.clone()); + } + + timer_stop_and_log_info!(CALCULATING_WITNESS); + } + + pub fn get_pctx(&self) -> Arc> { + self.pctx.clone() + } + + pub fn get_sctx(&self) -> Arc { + self.sctx.clone() + } + + pub fn get_rom_path(&self) -> Option { + self.rom_path.clone() + } + + pub fn get_public_inputs_path(&self) -> Option { + self.public_inputs_path.clone() + } +}