diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 5d0762b2..00000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: Benchmark - -on: - workflow_dispatch: - inputs: - run_benchmark: - description: "Please confirm running the benchmarks by typing 'yes' in the input box." - required: true - default: "no" - -jobs: - wakeup: - runs-on: ubuntu-latest - if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.run_benchmark == 'yes' }} - permissions: - id-token: write - contents: read - steps: - - uses: actions/checkout@v3 - - - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v4 - with: - role-to-assume: arn:aws:iam::490752553772:role/summa-solvency-ec2-slc - role-duration-seconds: 900 - aws-region: us-west-2 - - - name: Wakeup runner - run: .github/scripts/wakeup.sh - - benchmark: - runs-on: [summa-solvency-runner] - needs: [wakeup] - if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.run_benchmark == 'yes' }} - steps: - - uses: actions/checkout@v3 - - - name: Run Benchmark Tests - run: | - cd prover - cargo bench - - - name: Upload Benchmark Results - uses: actions/upload-artifact@v2 - with: - name: benchmark-results - path: prover/target/criterion diff --git a/.github/workflows/contracts.yml b/.github/workflows/contracts.yml deleted file mode 100644 index f05c06f8..00000000 --- a/.github/workflows/contracts.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Contracts - -on: - push: - branches: ["*"] - pull_request: - branches: ["*"] - -jobs: - tests: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Install packages - uses: actions/setup-node@v3 - - run: | - cd contracts - npm ci - - name: Run Tests - run: | - cd contracts - npx hardhat test diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1ba15d57..c72b2a4c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -14,53 +14,31 @@ env: CARGO_TERM_COLOR: always jobs: - wakeup: + test-zk-prover: runs-on: ubuntu-latest - permissions: - id-token: write - contents: read - - steps: - - uses: actions/checkout@v3 - - - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v4 - with: - role-to-assume: arn:aws:iam::490752553772:role/summa-solvency-ec2-slc - role-duration-seconds: 900 - aws-region: us-west-2 - - - name: Wakeup runner - run: .github/scripts/wakeup.sh - - build: - runs-on: [summa-solvency-runner] - needs: [wakeup] - steps: - uses: actions/checkout@v3 - - - name: Set Environment - run: echo "PATH=/home/ubuntu/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" >> "$GITHUB_ENV" - - - name: Install solc - run: (hash svm 2>/dev/null || cargo install --version 0.2.23 svm-rs) && svm install 0.8.20 && solc --version - - - name: Test Prover + - name: Test Zk Prover run: | cd prover - cargo test --release -- --nocapture - - - name: Install Foundry - uses: foundry-rs/foundry-toolchain@v1 - - # TODO restore workflow - # - name: Test backend - # run: | - # cd backend - # cargo test --release -- --nocapture - - # - name: Test example - # run: | - # cd backend - # cargo run --release --example summa_solvency_flow + cargo test --release --features dev-graph -- --nocapture + + # TODO: restore workflow after fix backend + # test-backend: + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - name: Test backend + # run: | + # cd backend + # cargo test --release -- --nocapture + + # test-backend-examples: + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - name: Test backend example + # run: | + # cd backend + # cargo run --release --example summa_solvency_flow + \ No newline at end of file diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 95318bca..24e50410 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -2427,7 +2427,7 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "plonkish_backend" version = "0.1.0" -source = "git+https://github.com/summa-dev/plonkish?branch=nonzero-constraints#e37ba53dcc8f8bd6e7add4e479d0e3295ee80661" +source = "git+https://github.com/summa-dev/plonkish?branch=nonzero-constraints#c9dc12571d4a9aa06a419598ff2422b42770ae91" dependencies = [ "bincode", "bitvec 1.0.1", diff --git a/backend/examples/summa_solvency_flow.rs b/backend/examples/summa_solvency_flow.rs index 953a040d..b4e63c8b 100644 --- a/backend/examples/summa_solvency_flow.rs +++ b/backend/examples/summa_solvency_flow.rs @@ -68,7 +68,7 @@ async fn main() -> Result<(), Box> { // // Initialize the `Round` instance to submit the liability commitment. let entry_csv = "../csv/entry_16.csv"; - let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; + let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; let mut cryptos = vec![Cryptocurrency::init_empty(); N_CURRENCIES]; parse_csv_to_entries::<&str, N_CURRENCIES>(entry_csv, &mut entries, &mut cryptos).unwrap(); diff --git a/backend/src/tests.rs b/backend/src/tests.rs index 7e61fe64..30d12364 100644 --- a/backend/src/tests.rs +++ b/backend/src/tests.rs @@ -187,7 +187,7 @@ mod test { .await?; let entry_csv = "../csv/entry_16.csv"; - let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; + let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; let mut cryptos = vec![Cryptocurrency::init_empty(); N_CURRENCIES]; parse_csv_to_entries::<&str, N_CURRENCIES>(entry_csv, &mut entries, &mut cryptos).unwrap(); @@ -299,7 +299,7 @@ mod test { // Initialize Round. let entry_csv = "../csv/entry_16.csv"; - let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; + let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; let mut cryptos = vec![Cryptocurrency::init_empty(); N_CURRENCIES]; parse_csv_to_entries::<&str, N_CURRENCIES>(entry_csv, &mut entries, &mut cryptos).unwrap(); diff --git a/prover/Cargo.lock b/prover/Cargo.lock index 610ff69a..7dd74ea1 100644 --- a/prover/Cargo.lock +++ b/prover/Cargo.lock @@ -34,9 +34,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.82" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "ark-std" @@ -73,9 +73,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "bincode" @@ -154,9 +154,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "bytemuck" -version = "1.15.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15" +checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5" [[package]] name = "byteorder" @@ -172,9 +172,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.0.94" +version = "1.0.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17f6e324229dc011159fcc089755d1e2e216a90d43a7dea6853ca740b84f35e7" +checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" [[package]] name = "cfg-if" @@ -213,12 +213,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" -[[package]] -name = "const-cstr" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed3d0b5ff30645a68f35ece8cea4556ca14ef8a1651455f789a099a0513532a6" - [[package]] name = "constant_time_eq" version = "0.3.0" @@ -243,9 +237,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "core-graphics" -version = "0.22.3" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" dependencies = [ "bitflags 1.3.2", "core-foundation", @@ -267,9 +261,9 @@ dependencies = [ [[package]] name = "core-text" -version = "19.2.0" +version = "20.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99d74ada66e07c1cefa18f8abfba765b486f250de2e4a999e5727fc0dd4b4a25" +checksum = "c9d2790b5c08465d49f8dc05c8bcae9fea467855947db39b0f8145c091aaced5" dependencies = [ "core-foundation", "core-graphics", @@ -288,9 +282,9 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.4.0" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ "cfg-if", ] @@ -352,9 +346,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.19" +version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "crypto-common" @@ -366,6 +360,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "cstr" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68523903c8ae5aacfa32a0d9ae60cadeb764e1da14ee0d26b1f3089f13a54636" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "csv" version = "1.3.0" @@ -510,9 +514,9 @@ dependencies = [ [[package]] name = "either" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" [[package]] name = "fdeflate" @@ -536,9 +540,9 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.28" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" dependencies = [ "crc32fast", "miniz_oxide", @@ -546,9 +550,9 @@ dependencies = [ [[package]] name = "float-ord" -version = "0.2.0" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e" +checksum = "8ce81f49ae8a0482e4c55ea62ebbd7e5a686af544c00b9d090bba3ff9be97b3d" [[package]] name = "fnv" @@ -558,11 +562,11 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "font-kit" -version = "0.11.0" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21fe28504d371085fae9ac7a3450f0b289ab71e07c8e57baa3fb68b9e57d6ce5" +checksum = "2845a73bbd781e691ab7c2a028c579727cd254942e8ced57ff73e0eafd60de87" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.5.0", "byteorder", "core-foundation", "core-graphics", @@ -570,7 +574,7 @@ dependencies = [ "dirs-next", "dwrote", "float-ord", - "freetype", + "freetype-sys", "lazy_static", "libc", "log", @@ -583,34 +587,36 @@ dependencies = [ [[package]] name = "foreign-types" -version = "0.3.2" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" dependencies = [ + "foreign-types-macros", "foreign-types-shared", ] [[package]] -name = "foreign-types-shared" -version = "0.1.1" +name = "foreign-types-macros" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] [[package]] -name = "freetype" -version = "0.7.1" +name = "foreign-types-shared" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efc8599a3078adf8edeb86c71e9f8fa7d88af5ca31e806a867756081f90f5d83" -dependencies = [ - "freetype-sys", - "libc", -] +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" [[package]] name = "freetype-sys" -version = "0.19.0" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66ee28c39a43d89fbed8b4798fb4ba56722cfd2b5af81f9326c27614ba88ecd5" +checksum = "0e7edc5b9669349acfda99533e9e0bcf26a51862ab43b08ee7745c55d28eb134" dependencies = [ "cc", "libc", @@ -636,9 +642,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", @@ -839,9 +845,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.153" +version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libloading" @@ -877,9 +883,9 @@ checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "miniz_oxide" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" dependencies = [ "adler", "simd-adler32", @@ -887,11 +893,10 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" dependencies = [ - "autocfg", "num-integer", "num-traits", ] @@ -907,9 +912,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] @@ -951,9 +956,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pathfinder_geometry" @@ -989,7 +994,7 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] name = "plonkish_backend" version = "0.1.0" -source = "git+https://github.com/summa-dev/plonkish?branch=nonzero-constraints#e37ba53dcc8f8bd6e7add4e479d0e3295ee80661" +source = "git+https://github.com/summa-dev/plonkish?branch=nonzero-constraints#c9dc12571d4a9aa06a419598ff2422b42770ae91" dependencies = [ "bincode", "bitvec", @@ -1009,9 +1014,9 @@ dependencies = [ [[package]] name = "plotters" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" +checksum = "a15b6eccb8484002195a3e44fe65a4ce8e93a625797a063735536fd59cb01cf3" dependencies = [ "chrono", "font-kit", @@ -1029,15 +1034,15 @@ dependencies = [ [[package]] name = "plotters-backend" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" +checksum = "414cec62c6634ae900ea1c56128dfe87cf63e7caece0852ec76aba307cebadb7" [[package]] name = "plotters-bitmap" -version = "0.3.3" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cebbe1f70205299abc69e8b295035bb52a6a70ee35474ad10011f0a4efb8543" +checksum = "f7e7f6fb8302456d7c264a94dada86f76d76e1a03e2294ee86ca7da92983b0a6" dependencies = [ "gif", "image", @@ -1046,9 +1051,9 @@ dependencies = [ [[package]] name = "plotters-svg" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" +checksum = "81b30686a7d9c3e010b84284bdd26a29f2138574f52f5eb6f794fc0ad924e705" dependencies = [ "plotters-backend", ] @@ -1083,9 +1088,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.80" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56dea16b0a29e94408b9aa5e2940a4eedbd128a1ba20e8f7ae60fd3d465af0e" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" dependencies = [ "unicode-ident", ] @@ -1206,9 +1211,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "same-file" @@ -1221,15 +1226,15 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] @@ -1246,20 +1251,20 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn 2.0.59", + "syn 2.0.66", ] [[package]] name = "serde_json" -version = "1.0.116" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ "itoa", "ryu", @@ -1345,9 +1350,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.59" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a6531ffc7b071655e4ce2e04bd464c4830bb585a61cabb96cf808f05172615a" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", @@ -1382,22 +1387,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.58" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.58" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.59", + "syn 2.0.66", ] [[package]] @@ -1429,7 +1434,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.59", + "syn 2.0.66", ] [[package]] @@ -1443,9 +1448,9 @@ dependencies = [ [[package]] name = "ttf-parser" -version = "0.17.1" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "375812fa44dab6df41c195cd2f7fecb488f6c09fbaafb62807488cefab642bff" +checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4" [[package]] name = "typenum" @@ -1461,9 +1466,9 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unicode-width" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" [[package]] name = "version_check" @@ -1508,7 +1513,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.59", + "syn 2.0.66", "wasm-bindgen-shared", ] @@ -1530,7 +1535,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.59", + "syn 2.0.66", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1575,11 +1580,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.6" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "winapi", + "windows-sys", ] [[package]] @@ -1597,6 +1602,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.5" @@ -1681,11 +1695,11 @@ dependencies = [ [[package]] name = "yeslogic-fontconfig-sys" -version = "3.2.0" +version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2bbd69036d397ebbff671b1b8e4d918610c181c5a16073b96f984a38d08c386" +checksum = "ffb6b23999a8b1a997bf47c7bb4d19ad4029c3327bb3386ebe0a5ff584b33c7a" dependencies = [ - "const-cstr", + "cstr", "dlib", "once_cell", "pkg-config", diff --git a/prover/Cargo.toml b/prover/Cargo.toml index a897e375..16740928 100644 --- a/prover/Cargo.toml +++ b/prover/Cargo.toml @@ -30,4 +30,4 @@ criterion= "0.3" [[bench]] name = "proof_of_liabilities" -harness = false \ No newline at end of file +harness = false diff --git a/prover/prints/range-check-layout.png b/prover/prints/range-check-layout.png index 579656ef..a95aff4d 100644 Binary files a/prover/prints/range-check-layout.png and b/prover/prints/range-check-layout.png differ diff --git a/prover/prints/summa-hyperplonk-layout.png b/prover/prints/summa-hyperplonk-layout.png index 497b11f3..4b1b6d10 100644 Binary files a/prover/prints/summa-hyperplonk-layout.png and b/prover/prints/summa-hyperplonk-layout.png differ diff --git a/prover/rust-toolchain b/prover/rust-toolchain new file mode 100644 index 00000000..4524b7cb --- /dev/null +++ b/prover/rust-toolchain @@ -0,0 +1 @@ +nightly-2023-07-11 \ No newline at end of file diff --git a/prover/src/circuits/config/circuit_config.rs b/prover/src/circuits/config/circuit_config.rs index 51382773..8fbcbf94 100644 --- a/prover/src/circuits/config/circuit_config.rs +++ b/prover/src/circuits/config/circuit_config.rs @@ -1,6 +1,6 @@ use halo2_proofs::{ circuit::{Layouter, Value}, - plonk::{Advice, Column, ConstraintSystem, Error, Instance}, + plonk::{Advice, Column, ConstraintSystem, Error, Instance, Selector}, }; use crate::{entry::Entry, utils::big_uint_to_fp}; @@ -18,12 +18,16 @@ pub trait CircuitConfig: Clone fn configure( meta: &mut ConstraintSystem, username: Column, + concatenated_balance: Column, + selector: Selector, balances: [Column; N_CURRENCIES], instance: Column, ) -> Self; fn get_username(&self) -> Column; + fn get_concatenated_balance(&self) -> Column; + fn get_balances(&self) -> [Column; N_CURRENCIES]; fn get_instance(&self) -> Column; @@ -32,8 +36,8 @@ pub trait CircuitConfig: Clone fn synthesize( &self, mut layouter: impl Layouter, - entries: &[Entry], - grand_total: &[Fp], + entries: &[Entry], + concatenated_grand_total: &Fp, ) -> Result<(), Error> { // Initiate the range check chips let range_check_chips = self.initialize_range_check_chips(); @@ -49,7 +53,19 @@ pub trait CircuitConfig: Clone || Value::known(big_uint_to_fp::(entry.username_as_big_uint())), )?; - let mut last_decompositions = vec![]; + region.assign_advice( + || "concatenated balance", + self.get_concatenated_balance(), + 0, + || { + Value::known(big_uint_to_fp::( + &entry.concatenated_balance().unwrap(), + )) + }, + )?; + + // Decompose the balances + let mut assigned_balances = Vec::new(); for (j, balance) in entry.balances().iter().enumerate() { let assigned_balance = region.assign_advice( @@ -59,10 +75,16 @@ pub trait CircuitConfig: Clone || Value::known(big_uint_to_fp(balance)), )?; + assigned_balances.push(assigned_balance); + } + + let mut last_decompositions = vec![]; + + for (j, assigned_balance) in assigned_balances.iter().enumerate() { let mut zs = Vec::with_capacity(4); if !range_check_chips.is_empty() { - range_check_chips[j].assign(&mut region, &mut zs, &assigned_balance)?; + range_check_chips[j].assign(&mut region, &mut zs, assigned_balance)?; last_decompositions.push(zs[3].clone()); } @@ -76,28 +98,20 @@ pub trait CircuitConfig: Clone } let assigned_total = layouter.assign_region( - || "assign total".to_string(), + || "assign concatenated total".to_string(), |mut region| { - let mut assigned_total = vec![]; - - for (j, total) in grand_total.iter().enumerate() { - let balance_total = region.assign_advice( - || format!("total {}", j), - self.get_balances()[j], - 0, - || Value::known(total.neg()), - )?; - - assigned_total.push(balance_total); - } - - Ok(assigned_total) + let balance_total = region.assign_advice( + || format!("concateneated total({} currencies)", N_CURRENCIES), + self.get_concatenated_balance(), + 0, + || Value::known(concatenated_grand_total.neg()), + )?; + + Ok(balance_total) }, )?; - for (j, total) in assigned_total.iter().enumerate() { - layouter.constrain_instance(total.cell(), self.get_instance(), 1 + j)?; - } + layouter.constrain_instance(assigned_total.cell(), self.get_instance(), 1)?; self.load_lookup_table(layouter)?; diff --git a/prover/src/circuits/config/no_range_check_config.rs b/prover/src/circuits/config/no_range_check_config.rs index 5d6d4ada..15a70b38 100644 --- a/prover/src/circuits/config/no_range_check_config.rs +++ b/prover/src/circuits/config/no_range_check_config.rs @@ -1,6 +1,6 @@ use halo2_proofs::{ circuit::Layouter, - plonk::{Advice, Column, ConstraintSystem, Error, Instance}, + plonk::{Advice, Column, ConstraintSystem, Error, Instance, Selector}, }; use crate::chips::range::range_check::RangeCheckU64Chip; @@ -19,10 +19,13 @@ use super::circuit_config::CircuitConfig; /// # Fields /// /// * `username`: Advice column used to store the usernames of the users +/// * `concatenated_balance`: Advice column used to store the concatenated balances of the users /// * `balances`: Advice columns used to store the balances of the users +/// * `instance`: Instance column used to constrain the last balance decomposition #[derive(Clone)] pub struct NoRangeCheckConfig { username: Column, + concatenated_balance: Column, balances: [Column; N_CURRENCIES], instance: Column, } @@ -33,11 +36,14 @@ impl CircuitConfig, username: Column, + concatenated_balance: Column, + _selector: Selector, balances: [Column; N_CURRENCIES], instance: Column, ) -> NoRangeCheckConfig { Self { username, + concatenated_balance, balances, instance, } @@ -47,6 +53,10 @@ impl CircuitConfig Column { + self.concatenated_balance + } + fn get_balances(&self) -> [Column; N_CURRENCIES] { self.balances } diff --git a/prover/src/circuits/config/range_check_config.rs b/prover/src/circuits/config/range_check_config.rs index 7a7c0e8a..08ad6f94 100644 --- a/prover/src/circuits/config/range_check_config.rs +++ b/prover/src/circuits/config/range_check_config.rs @@ -1,6 +1,6 @@ use halo2_proofs::{ circuit::{Layouter, Value}, - plonk::{Advice, Column, ConstraintSystem, Error, Fixed, Instance}, + plonk::{Advice, Column, ConstraintSystem, Error, Fixed, Instance, Selector}, }; use crate::chips::range::range_check::{RangeCheckChipConfig, RangeCheckU64Chip}; @@ -18,6 +18,7 @@ use super::circuit_config::CircuitConfig; /// # Fields /// /// * `username`: Advice column used to store the usernames of the users +/// * `concatenated_balance`: Advice column used to store the concatenated balances of the users /// * `balances`: Advice columns used to store the balances of the users /// * `range_check_configs`: Range check chip configurations /// * `range_u16`: Fixed column used to store the lookup table @@ -25,6 +26,7 @@ use super::circuit_config::CircuitConfig; #[derive(Clone)] pub struct RangeCheckConfig { username: Column, + concatenated_balance: Column, balances: [Column; N_CURRENCIES], range_check_configs: [RangeCheckChipConfig; N_CURRENCIES], range_u16: Column, @@ -37,6 +39,8 @@ impl CircuitConfig, username: Column, + concatenated_balance: Column, + selector: Selector, balances: [Column; N_CURRENCIES], instance: Column, ) -> Self { @@ -46,8 +50,6 @@ impl CircuitConfig CircuitConfig CircuitConfig Column { + self.concatenated_balance + } + fn get_balances(&self) -> [Column; N_CURRENCIES] { self.balances } diff --git a/prover/src/circuits/summa_circuit.rs b/prover/src/circuits/summa_circuit.rs index e8762d45..199809bd 100644 --- a/prover/src/circuits/summa_circuit.rs +++ b/prover/src/circuits/summa_circuit.rs @@ -1,19 +1,19 @@ -use std::marker::PhantomData; - use halo2_proofs::{ + arithmetic::Field, circuit::{Layouter, SimpleFloorPlanner}, - plonk::{Circuit, ConstraintSystem, Error}, + halo2curves::{bn256::Fr as Fp, ff::PrimeField}, + plonk::{Circuit, ConstraintSystem, Error, Expression}, poly::Rotation, }; - -use crate::{entry::Entry, utils::big_uint_to_fp}; - -use halo2_proofs::arithmetic::Field; -use halo2_proofs::halo2curves::bn256::Fr as Fp; use plonkish_backend::frontend::halo2::CircuitExt; use rand::RngCore; +use std::marker::PhantomData; use super::config::circuit_config::CircuitConfig; +use crate::{ + entry::Entry, + utils::{big_uint_to_fp, calculate_shift_bits}, +}; #[derive(Clone, Default)] pub struct SummaHyperplonk< @@ -21,8 +21,8 @@ pub struct SummaHyperplonk< const N_CURRENCIES: usize, CONFIG: CircuitConfig, > { - pub entries: Vec>, - pub grand_total: Vec, + pub entries: Vec>, + pub concatenated_grand_total: Fp, _marker: PhantomData, } @@ -32,17 +32,17 @@ impl< CONFIG: CircuitConfig, > SummaHyperplonk { - pub fn init(user_entries: Vec>) -> Self { - let mut grand_total = vec![Fp::ZERO; N_CURRENCIES]; + pub fn init(user_entries: Vec>) -> Self { + let mut concatenated_grand_total = Fp::ZERO; + for entry in user_entries.iter() { - for (i, balance) in entry.balances().iter().enumerate() { - grand_total[i] += big_uint_to_fp::(balance); - } + concatenated_grand_total += + big_uint_to_fp::(&entry.concatenated_balance().unwrap()); } Self { entries: user_entries, - grand_total, + concatenated_grand_total, _marker: PhantomData, } } @@ -50,17 +50,14 @@ impl< /// Initialize the circuit with an invalid grand total /// (for testing purposes only). #[cfg(test)] - pub fn init_invalid_grand_total(user_entries: Vec>) -> Self { + pub fn init_invalid_grand_total(user_entries: Vec>) -> Self { use plonkish_backend::util::test::seeded_std_rng; - let mut grand_total = vec![Fp::ZERO; N_CURRENCIES]; - for i in 0..N_CURRENCIES { - grand_total[i] = Fp::random(seeded_std_rng()); - } + let concatenated_grand_total = Fp::random(seeded_std_rng()); Self { entries: user_entries, - grand_total, + concatenated_grand_total, _marker: PhantomData, } } @@ -84,28 +81,83 @@ impl< let username = meta.advice_column(); + let concatenated_balance = meta.advice_column(); + meta.enable_equality(concatenated_balance); + + meta.create_gate("Concatenated balance sumcheck gate", |meta| { + let current_balance = meta.query_advice(concatenated_balance, Rotation::cur()); + vec![current_balance.clone()] + }); + + let q_enable = meta.complex_selector(); + let balances = [(); N_CURRENCIES].map(|_| meta.advice_column()); for column in &balances { meta.enable_equality(*column); } - meta.create_gate("Balance sumcheck gate", |meta| { - let mut nonzero_constraint = vec![]; - for balance in balances { - let current_balance = meta.query_advice(balance, Rotation::cur()); - nonzero_constraint.push(current_balance.clone()); + meta.create_gate("Concatenated balance validation check gate", |meta| { + let s = meta.query_selector(q_enable); + + let concatenated_balance = meta.query_advice(concatenated_balance, Rotation::cur()); + + // Right-most balance column is for the least significant balance in concatenated balance. + let mut balances_expr = meta.query_advice(balances[N_CURRENCIES - 1], Rotation::cur()); + + let shift_bits = calculate_shift_bits::().unwrap(); + + // The shift bits would not be exceed 93 bits + let base_shift = Fp::from_u128(1u128 << shift_bits); + + let mut current_shift = Expression::Constant(base_shift); + + // The number of currencies is limited 1 or 3 because the range check chip logic. + // In other words, more than 3 currencies would exceed the maximum bit count of 254, which is number of bits in Bn254. + match N_CURRENCIES { + 1 => { + // No need to add any shift for the only balance + println!("For a better performance for single currency, check out V3c. More details at: https://github.com/summa-dev/summa-solvency/tree/v3c"); + }, + 3 => { + for i in (0..N_CURRENCIES - 1).rev() { + let balance = meta.query_advice(balances[i], Rotation::cur()); + let shifted_balance = balance * current_shift.clone(); + balances_expr = balances_expr + shifted_balance; + + if i != 0 { + current_shift = current_shift * Expression::Constant(base_shift); + } + } + } + _ => panic!( + "Unsupported number of currencies, Only 1 and 3 currencies are supported" + ), } - nonzero_constraint + + // Ensure that the whole expression equals to the concatenated_balance + vec![s * (concatenated_balance - balances_expr)] }); let instance = meta.instance_column(); meta.enable_equality(instance); - CONFIG::configure(meta, username, balances, instance) + CONFIG::configure( + meta, + username, + concatenated_balance, + q_enable, + balances, + instance, + ) } fn synthesize(&self, config: Self::Config, layouter: impl Layouter) -> Result<(), Error> { - CONFIG::synthesize(&config, layouter, &self.entries, &self.grand_total) + CONFIG::synthesize( + &config, + layouter, + &self.entries, + &self.concatenated_grand_total, + ) } } @@ -121,9 +173,6 @@ impl< fn instances(&self) -> Vec> { // The 1st element is zero because the last decomposition of each range check chip should be zero - vec![vec![Fp::ZERO] - .into_iter() - .chain(self.grand_total.iter().map(|x| x.neg())) - .collect::>()] + vec![vec![Fp::ZERO, self.concatenated_grand_total.neg()]] } } diff --git a/prover/src/circuits/tests.rs b/prover/src/circuits/tests.rs index 88bbb4af..c9fbd96d 100644 --- a/prover/src/circuits/tests.rs +++ b/prover/src/circuits/tests.rs @@ -29,9 +29,8 @@ use crate::{ }, }; const K: u32 = 17; -const N_CURRENCIES: usize = 2; +const N_CURRENCIES: usize = 3; // One row is reserved for the grand total. -// TODO find out what occupies one extra row const N_USERS: usize = (1 << K) - 2; pub fn seeded_std_rng() -> impl RngCore + CryptoRng { @@ -48,21 +47,12 @@ fn test_summa_hyperplonk_e2e() { entries.to_vec(), ); - let neg_grand_total = halo2_circuit - .grand_total - .iter() - .fold(Fp::ZERO, |acc, f| acc + f) - .neg(); + let neg_grand_total = halo2_circuit.concatenated_grand_total.neg(); // We're putting the negated grand total at the end of each balance column, // so the sumcheck over such balance column would yield zero (given the special gate, // see the circuit). - assert!( - neg_grand_total - == halo2_circuit.instances()[0] - .iter() - .fold(Fp::ZERO, |acc, instance| { acc + instance }) - ); + assert!(neg_grand_total == halo2_circuit.instances()[0][1]); let num_vars = K; @@ -95,8 +85,6 @@ fn test_summa_hyperplonk_e2e() { (witness_polys, proof_transcript) }; - let num_points = N_CURRENCIES + 1; - let proof = proof_transcript.into_proof(); let mut transcript; @@ -177,7 +165,7 @@ fn test_summa_hyperplonk_e2e() { ); assert_eq!( fp_to_big_uint(&witness_polys[1].evaluate_as_univariate(&random_user_index)), - entries[random_user_index].balances()[0] + entries[random_user_index].concatenated_balance().unwrap() ); // Convert challenge into a multivariate form @@ -188,6 +176,9 @@ fn test_summa_hyperplonk_e2e() { let mut transcript = Keccak256Transcript::from_proof((), proof.as_slice()); + // Username and Concatenated balance + let num_points = 2; + let user_entry_commitments = MultilinearKzg::::read_commitments( &verifier_parameters.pcs, num_points, @@ -239,24 +230,18 @@ fn test_summa_hyperplonk_e2e() { multivariate_challenge.push(kzg_transcript.read_field_element().unwrap()); } - //The user knows their evaluation at the challenge point - let evals: Vec> = (0..N_CURRENCIES + 1) - .map(|i| { - if i == 0 { - Evaluation::new( - i, - 0, - big_uint_to_fp::(entries[random_user_index].username_as_big_uint()), - ) - } else { - Evaluation::new( - i, - 0, - big_uint_to_fp::(&entries[random_user_index].balances()[i - 1]), - ) - } - }) - .collect(); + let evals = vec![ + Evaluation::new( + 0, + 0, + big_uint_to_fp::(entries[random_user_index].username_as_big_uint()), + ), + Evaluation::new( + 1, + 0, + big_uint_to_fp::(&entries[random_user_index].concatenated_balance().unwrap()), + ), + ]; MultilinearKzg::::batch_verify( &verifier_parameters.pcs, @@ -340,7 +325,10 @@ fn print_univariate_grand_sum_circuit() { let entries = generate_dummy_entries::().unwrap(); - let circuit = SummaHyperplonk::::init(entries.to_vec()); + let circuit = + SummaHyperplonk::>::init( + entries.to_vec(), + ); let root = BitMapBackend::new("prints/summa-hyperplonk-layout.png", (2048, 32768)).into_drawing_area(); @@ -350,6 +338,6 @@ fn print_univariate_grand_sum_circuit() { .unwrap(); halo2_proofs::dev::CircuitLayout::default() - .render::, _, true>(K, &circuit, &root) + .render::>, _, true>(K, &circuit, &root) .unwrap(); } diff --git a/prover/src/entry.rs b/prover/src/entry.rs index daaef76c..e891f566 100644 --- a/prover/src/entry.rs +++ b/prover/src/entry.rs @@ -1,17 +1,17 @@ use num_bigint::BigUint; -use crate::utils::big_intify_username; +use crate::utils::{big_intify_username, calculate_shift_bits}; /// An entry in the Merkle Sum Tree from the database of the CEX. /// It contains the username and the balances of the user. #[derive(Clone, Debug)] -pub struct Entry { +pub struct Entry { username_as_big_uint: BigUint, balances: [BigUint; N_CURRENCIES], username: String, } -impl Entry { +impl Entry { pub fn new(username: String, balances: [BigUint; N_CURRENCIES]) -> Result { Ok(Entry { username_as_big_uint: big_intify_username(&username), @@ -34,6 +34,19 @@ impl Entry { &self.balances } + pub fn concatenated_balance(&self) -> Result { + let shift_bits = calculate_shift_bits::().unwrap(); + + let mut concatenated_balance = BigUint::from(0u32); + + // Reverse the array to correctly order the balances + for (i, balance) in self.balances.iter().rev().enumerate() { + concatenated_balance += balance << (shift_bits * i); + } + + Ok(concatenated_balance) + } + pub fn username_as_big_uint(&self) -> &BigUint { &self.username_as_big_uint } diff --git a/prover/src/utils/dummy_entries.rs b/prover/src/utils/dummy_entries.rs index f566b9c0..fb4dd60f 100644 --- a/prover/src/utils/dummy_entries.rs +++ b/prover/src/utils/dummy_entries.rs @@ -7,13 +7,13 @@ use crate::entry::Entry; // This is for testing purposes with a large dataset instead of using a CSV file pub fn generate_dummy_entries( -) -> Result>, Box> { +) -> Result>, Box> { // Ensure N_CURRENCIES is greater than 0. if N_CURRENCIES == 0 { return Err("N_CURRENCIES must be greater than 0".into()); } - let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; + let mut entries: Vec> = vec![Entry::init_empty(); N_USERS]; entries.par_iter_mut().for_each(|entry| { let mut rng = rand::thread_rng(); @@ -21,7 +21,7 @@ pub fn generate_dummy_entries( let username: String = (0..10).map(|_| rng.sample(Alphanumeric) as char).collect(); let balances: [BigUint; N_CURRENCIES] = - std::array::from_fn(|_| BigUint::from(rng.gen_range(1000..90000) as u32)); + std::array::from_fn(|_| BigUint::from(rng.gen_range(10000..90000) as u32)); *entry = Entry::new(username, balances).expect("Failed to create entry"); }); diff --git a/prover/src/utils/operation_helpers.rs b/prover/src/utils/operation_helpers.rs index 7e2f8f9e..0d16d6b3 100644 --- a/prover/src/utils/operation_helpers.rs +++ b/prover/src/utils/operation_helpers.rs @@ -59,3 +59,87 @@ pub fn uni_to_multivar_binary_index(x: &usize, num_vars: result } + +pub fn calculate_shift_bits( +) -> Result { + // Define the maximum number of bits that can be used, based on the modulus in bn254. + const MAX_ALLOWANCE_BITS: usize = 253; + + // Calculate the maximum number of bits that can be allocated to the user base, + // taking into account the number of currencies and the bits needed for the balances range check and buffer. + let maximum_allowance_user_base_bits = (MAX_ALLOWANCE_BITS / N_CURRENCIES) - 64 - 1; + + // Determine the number of bits needed to represent the user base. + // For example, if `N_USERS` is 1025, the user base bit count would be 11. + let user_base_bits = N_USERS.next_power_of_two().ilog2() as usize; + + if user_base_bits > maximum_allowance_user_base_bits { + return Err(format!( + "The bit count for the user base exceeds the maximum limit of {}", + maximum_allowance_user_base_bits + )); + } + + // Define shift bits: 1 for buffer, bits for user base that not exceed 19, and 64 bits for the balances range check + let shift_bits: usize = (1 + user_base_bits + 64).try_into().unwrap(); + + Ok(shift_bits) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_calculate_shift_bits() { + { + // Practical Nnumber of users cases + const N_USERS: usize = 1 << 28; + const N_CURRENCIES: usize = 1; + + let result = calculate_shift_bits::(); + assert_eq!(result.unwrap(), 93); + assert_eq!(93 * N_CURRENCIES < 253, true); + } + { + const N_USERS: usize = 1 << 28; + const N_CURRENCIES: usize = 2; + + let result = calculate_shift_bits::(); + assert_eq!(result.unwrap(), 93); + assert_eq!(93 * N_CURRENCIES < 253, true); + } + { + // Maximum number of user when N_CURRENCIES = 3 + const N_USERS: usize = 1 << 19; + const N_CURRENCIES: usize = 3; + + let result = calculate_shift_bits::(); + assert_eq!(result.unwrap(), 84); + assert_eq!(84 * N_CURRENCIES < 253, true); + } + { + // Error case in N_CURRENCIES = 2 with infeasible N_USERS + const N_USERS: usize = 1 << 63; + const N_CURRENCIES: usize = 2; + + let result = calculate_shift_bits::(); + assert!(result.is_err()); + assert_eq!( + result.unwrap_err(), + "The bit count for the user base exceeds the maximum limit of 61" + ); + } + { + const N_USERS: usize = 1 << 63; + const N_CURRENCIES: usize = 3; + + let result = calculate_shift_bits::(); + assert!(result.is_err()); + assert_eq!( + result.unwrap_err(), + "The bit count for the user base exceeds the maximum limit of 19" + ); + } + } +}