diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index f907c296..7e401882 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -28,7 +28,7 @@ - [translate-witness](./translate-witness.md) - [generate-proof](./generate-proof.md) - [verify](./verify.md) - - [Network Config File](./network-config.md) + - [Configuration](./config.md) - [Examples](./examples.md) - [Poseidon](./poseidon.md) - [KYC](./kyc.md) diff --git a/book/src/config.md b/book/src/config.md new file mode 100644 index 00000000..de548936 --- /dev/null +++ b/book/src/config.md @@ -0,0 +1,91 @@ +# Configuration + +`co-circom` uses a configuration for general settings and network configuration. +The configuration can be done via a config file, environment variables, and cli arguments. +Values are loaded in hierarchical order `file < environment variables < cli args`. + +A path to the configuration file can be passed to all commands using `--config `. +Different commands have different required values that must be passed by file, env, or cli. +The network section is only required for the commands `generate-witness`, `translate-witness` and `generate-proof`. + +## TOML File + +The configuration file is a TOML file with the following (non-exhaustive) structure: + +```toml +protocol = "REP3" +curve = "BN254" + +[compiler] +allow_leaky_loops = false + +[vm] +allow_leaky_logs = false + +[network] +my_id = 0 +bind_addr = "0.0.0.0:10000" +key_path = "data/key0.der" +[[network.parties]] +id = 0 +# normally we would use DNS name here such as localhost, but localhost under windows is resolved to ::1, which causes problems since we bind to ipv4 above +dns_name = "127.0.0.1:10000" +cert_path = "data/cert0.der" +[[network.parties]] +id = 1 +dns_name = "127.0.0.1:10001" +cert_path = "data/cert1.der" +[[network.parties]] +id = 2 +dns_name = "127.0.0.1:10002" +cert_path = "data/cert2.der" +``` + +See the example configuration in the `collaborative-circom/examples/configs` folder, with pre-generated certificates and keys in the `collaborative-circom/examples/data` folder. + +## Env Variables + +Environment variables use the prefix `COCIRCOM_`. +The different types can be set as follows: + +* Boolean: `true`, `false` (e.g. `COCIRCOM_VAR=true`) +* Strings/Enums: delimited by `"` (e.g. `COCIRCOM_VAR=\"foo\"`) or else (e.g. `COCIRCOM_VAR=foo`) +* Arrays: delimited by `[]` (e.g. `COCIRCOM_VAR=[1, 2, 3]`) +* Structs: as dictionary with `{key=value}` (e.g. `COCIRCOM_VAR={foo=1, bar=true}`) + +E.g. the protocol can be set with `COCIRCOM_PROTOCOL=BN254`. +Structs such as the CompilerConfig can be set with `COCIRCOM_COMPILER={allow_leaky_loops=true}`. + +## Cli Arguments + +See [co-circom CLI](./co-circom.md) + +## Compiler Configuration + +This section is used to configure the co-circom MPC compiler. + +### Keys + +* `allow_leaky_loops`: used to allow leaking of secret values in loops, default: `false` (*currently not implemented*). + +## VM Configuration + +This section is used to configure the co-circom VM. + +### Keys + +* `allow_leaky_logs`: used to allow leaking of secret values logs, default: `false`. + +## Network Configuration + +`co-circom` requires a network configuration for establishing connections to other MPC parties for the `generate-witness` and `generate-proof` commands. + +### Keys + +* `my_id` is the party id of the party executing the `co-circom` binary using the configuration file. +* `bind_addr` is the local socket address this party is binding to and listening for incoming connections from other parties. +* `key_path` is a path to a DER encoded PKCS8 private key file corresponding to the public key used in the certificate for our party. +* `parties` is an array of tables containing the public information of each MPC party. + * `id`: the party id of the MPC party + * `dns_name`: the hostname/port combination where the party is publicly reachable. The hostname must be the a valid CN or SNI in the used certificate. + * `cert_path`: a path to the DER encoded certificate (chain) file that is used to authenticate the connection with the party and is used to establish the secure communication channel. diff --git a/book/src/examples.md b/book/src/examples.md index a4c7291e..d1497d88 100644 --- a/book/src/examples.md +++ b/book/src/examples.md @@ -4,5 +4,5 @@ This section shows how to use the co-circom CLI to generate proofs for different circuits. Example bash scripts are available in the `examples` directory of the [co-circom repository](https://github.com/TaceoLabs/collaborative-circom/tree/main/collaborative-circom/examples). -You will also find [network configs](./network-config.md), TLS keys, and sample +You will also find [configs](./config.md), TLS keys, and sample inputs for these circuits. diff --git a/book/src/generate-proof.md b/book/src/generate-proof.md index 6dff26a0..c1f373e8 100644 --- a/book/src/generate-proof.md +++ b/book/src/generate-proof.md @@ -16,10 +16,10 @@ The above command takes a witness share `test_vectors/poseidon/witness.wtns.0.sh $ co-circom generate-proof --help Evaluates the prover algorithm for the specified circuit and witness share in MPC -Usage: co-circom generate-proof [OPTIONS] --config --witness --zkey +Usage: co-circom generate-proof [OPTIONS] Arguments: - [possible values: groth16, plonk] + The proof system to be used [possible values: groth16, plonk] Options: --config The path to the config file diff --git a/book/src/generate-witness.md b/book/src/generate-witness.md index f05d59a2..f2b29ac0 100644 --- a/book/src/generate-witness.md +++ b/book/src/generate-witness.md @@ -16,15 +16,15 @@ The above command takes a shared input file `input.json.0.shared` for the circui $ co-circom generate-witness --help Evaluates the extended witness generation for the specified circuit and input share in MPC -Usage: co-circom generate-witness [OPTIONS] --input --circuit --protocol --curve --config --out +Usage: co-circom generate-witness [OPTIONS] Options: + --config The path to the config file --input The path to the input share file --circuit The path to the circuit file --link-library The path to Circom library files --protocol The MPC protocol to be used [possible values: REP3, SHAMIR] --curve The pairing friendly curve to be used [possible values: BN254, BLS12-381] - --config The path to MPC network configuration file --out The output file where the final witness share is written to - -h, --help Print help + -h, --help Print help (see more with '--help') ``` diff --git a/book/src/merge-input-shares.md b/book/src/merge-input-shares.md index 65c34ddd..2c55c1d8 100644 --- a/book/src/merge-input-shares.md +++ b/book/src/merge-input-shares.md @@ -18,12 +18,13 @@ The above command takes the two input shares `input0.json.0.shared` and `input1. co-circom merge-input-shares --help Merge multiple shared inputs received from multiple parties into a single one -Usage: co-circom merge-input-shares [OPTIONS] --protocol --curve --out +Usage: co-circom merge-input-shares [OPTIONS] Options: + --config The path to the config file --inputs The path to the input JSON file --protocol The MPC protocol to be used [possible values: REP3, SHAMIR] --curve The pairing friendly curve to be used [possible values: BN254, BLS12-381] --out The output file where the merged input share is written to - -h, --help Print help + -h, --help Print help (see more with '--help') ``` diff --git a/book/src/network-config.md b/book/src/network-config.md deleted file mode 100644 index bbe4069c..00000000 --- a/book/src/network-config.md +++ /dev/null @@ -1,35 +0,0 @@ -# Network Configuration - -`co-circom` requires a network configuration file for establishing connections to other MPC parties for the `generate-witness` and `generate-proof` commands. - -The network configuration file is a TOML file with the following structure: - -```toml -my_id = 0 -bind_addr = "0.0.0.0:10000" -key_path = "data/key0.der" -[[parties]] -id = 0 -dns_name = "localhost:10000" -cert_path = "data/cert0.der" -[[parties]] -id = 1 -dns_name = "localhost:10001" -cert_path = "data/cert1.der" -[[parties]] -id = 2 -dns_name = "localhost:10002" -cert_path = "data/cert2.der" -``` - -See the example configuration in the `collaborative-circom/examples/configs` folder, with pre-generated certificates and keys in the `collaborative-circom/examples/data` folder. - -## Keys - -* `my_id` is the party id of the party executing the `co-circom` binary using the configuration file. -* `bind_addr` is the local socket address this party is binding to and listening for incoming connections from other parties. -* `key_path` is a path to a DER encoded PKCS8 private key file corresponding to the public key used in the certificate for our party. -* `parties` is an array of tables containing the public information of each MPC party. - * `id`: the party id of the MPC party - * `dns_name`: the hostname/port combination where the party is publicly reachable. The hostname must be the a valid CN or SNI in the used certificate. - * `cert_path`: a path to the DER encoded certificate (chain) file that is used to authenticate the connection with the party and is used to establish the secure communication channel. diff --git a/book/src/quick_start.md b/book/src/quick_start.md index db115aa4..5dc0d2f8 100644 --- a/book/src/quick_start.md +++ b/book/src/quick_start.md @@ -72,21 +72,22 @@ This command secret shares the private inputs (everything that is not explicitly Now we have to compute the extended witness. In a real-world setting you would have to send the input files from the previous step to the parties. -To achieve that we need another config file for every party, namely the network config (you can read an in-depth explanation about the config at [here](./network-config.md)). You can copy-paste the config from here and call it `party0.toml` for party0 and so on: +To achieve that we need a network config for every party (you can read an in-depth explanation about the config at [here](./config.md)). You can copy-paste the config from here and call it `party0.toml` for party0 and so on: ```toml +[network] my_id = 0 bind_addr = "0.0.0.0:10000" key_path = "data/key0.der" -[[parties]] +[[network.parties]] id = 0 dns_name = "localhost:10000" cert_path = "data/cert0.der" -[[parties]] +[[network.parties]] id = 1 dns_name = "localhost:10001" cert_path = "data/cert1.der" -[[parties]] +[[network.parties]] id = 2 dns_name = "localhost:10002" cert_path = "data/cert2.der" diff --git a/book/src/split-input.md b/book/src/split-input.md index ba83c5eb..4584f897 100644 --- a/book/src/split-input.md +++ b/book/src/split-input.md @@ -18,14 +18,15 @@ These shares can be handed to the 3 different MPC parties for the witness genera $ co-circom split-input --help Splits a JSON input file into secret shares for use in MPC -Usage: co-circom split-input [OPTIONS] --input --circuit --protocol --curve --out-dir +Usage: co-circom split-input [OPTIONS] Options: + --config The path to the config file --input The path to the input JSON file --circuit The path to the circuit file --link-library The path to Circom library files --protocol The MPC protocol to be used [possible values: REP3, SHAMIR] --curve The pairing friendly curve to be used [possible values: BN254, BLS12-381] --out-dir The path to the (existing) output directory - -h, --help Print help + -h, --help Print help (see more with '--help') ``` diff --git a/book/src/split-witness.md b/book/src/split-witness.md index 6de3cbaf..22b100bb 100644 --- a/book/src/split-witness.md +++ b/book/src/split-witness.md @@ -18,9 +18,10 @@ These shares can be handed to the 3 different MPC parties for the proof generati $ co-circom split-witness --help Splits an existing witness file generated by Circom into secret shares for use in MPC -Usage: co-circom split-witness [OPTIONS] --witness --r1cs --protocol --curve --out-dir +Usage: co-circom split-witness [OPTIONS] Options: + --config The path to the config file --witness The path to the input witness file generated by Circom --r1cs The path to the r1cs file, generated by Circom compiler --protocol The MPC protocol to be used [possible values: REP3, SHAMIR] @@ -28,5 +29,5 @@ Options: --out-dir The path to the (existing) output directory -t, --threshold The threshold of tolerated colluding parties [default: 1] -n, --num-parties The number of parties [default: 3] - -h, --help Print help + -h, --help Print help (see more with '--help') ``` diff --git a/book/src/translate-witness.md b/book/src/translate-witness.md index 2dfa968c..369e854b 100644 --- a/book/src/translate-witness.md +++ b/book/src/translate-witness.md @@ -8,7 +8,7 @@ The aim of the `translate-witness` command is to take a witness file `witness.wt co-circom translate-witness --witness test_vectors/poseidon/witness.wtns --src-protocol REP3 --target-protocol SHAMIR --curve BN254 --config configs/party1.toml --out test_vectors/poseidon/shamir_witness.wtns ``` -The above command takes the witness file `test_vectors/poseidon/witness.wtns` which was generated with the source MPC protocol `REP3` and translates it to the witness file `test_vectors/poseidon/shamir_witness.wtns` which is suitable for the target MPC protocol `SHAMIR`. The translation process requires network interaction, thus a [networking config](./network-config.md) is required as well. +The above command takes the witness file `test_vectors/poseidon/witness.wtns` which was generated with the source MPC protocol `REP3` and translates it to the witness file `test_vectors/poseidon/shamir_witness.wtns` which is suitable for the target MPC protocol `SHAMIR`. The translation process requires network interaction, thus a [networking config](./config.md) is required as well. ## Reference @@ -16,9 +16,11 @@ The above command takes the witness file `test_vectors/poseidon/witness.wtns` wh $ co-circom translate-witness --help Translates the witness generated with one MPC protocol to a witness for a different one -Usage: co-circom translate-witness --witness --src-protocol --target-protocol --curve --config --out +Usage: co-circom translate-witness [OPTIONS] Options: + --config + The path to the config file --witness The path to the witness share file --src-protocol @@ -27,10 +29,8 @@ Options: The MPC protocol to be used for the proof generation [possible values: REP3, SHAMIR] --curve The pairing friendly curve to be used [possible values: BN254, BLS12-381] - --config - The path to MPC network configuration file --out The output file where the final witness share is written to -h, --help - Print help + Print help (see more with '--help') ``` diff --git a/book/src/verify.md b/book/src/verify.md index d3ff995d..76817008 100644 --- a/book/src/verify.md +++ b/book/src/verify.md @@ -15,10 +15,10 @@ The above command verifies the proof in `proof.json` using the verification key ```txt Verification of a Circom proof -Usage: co-circom verify [OPTIONS] --config --proof --vk --public-input +Usage: co-circom verify [OPTIONS] Arguments: - [possible values: groth16, plonk] + The proof system to be used [possible values: groth16, plonk] Options: --config The path to the config file diff --git a/collaborative-circom/examples/configs/config.toml b/collaborative-circom/examples/configs/config.toml deleted file mode 100644 index dc12d1c4..00000000 --- a/collaborative-circom/examples/configs/config.toml +++ /dev/null @@ -1,5 +0,0 @@ -[compiler] -allow_leaky_loops = false - -[vm] -allow_leaky_logs = false diff --git a/collaborative-circom/examples/groth16/cleanup.sh b/collaborative-circom/examples/groth16/cleanup.sh new file mode 100755 index 00000000..2a7bb935 --- /dev/null +++ b/collaborative-circom/examples/groth16/cleanup.sh @@ -0,0 +1,4 @@ +# rm all proof files +rm proof.0.json proof.1.json proof.2.json +# delete all shared files +find . -name "*.shared" -type f -delete diff --git a/collaborative-circom/examples/groth16/run_full_kyc.sh b/collaborative-circom/examples/groth16/run_full_kyc.sh index b35d9cce..53923b95 100755 --- a/collaborative-circom/examples/groth16/run_full_kyc.sh +++ b/collaborative-circom/examples/groth16/run_full_kyc.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/kyc +cargo run --release --bin co-circom -- split-input --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/kyc # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.0.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/kyc/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.1.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/kyc/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/witness.wtns.1.shared --zkey test_vectors/kyc/bn254/kyc.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/witness.wtns.2.shared --zkey test_vectors/kyc/bn254/kyc.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/kyc/bn254/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/kyc/bn254/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_full_kyc_shamir_bls.sh b/collaborative-circom/examples/groth16/run_full_kyc_shamir_bls.sh index 5f55af48..c92c6e49 100755 --- a/collaborative-circom/examples/groth16/run_full_kyc_shamir_bls.sh +++ b/collaborative-circom/examples/groth16/run_full_kyc_shamir_bls.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc +cargo run --release --bin co-circom -- split-input --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.0.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BLS12-381 --config ../configs/party1.toml --out test_vectors/kyc/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.1.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BLS12-381 --config ../configs/party2.toml --out test_vectors/kyc/witness.wtns.1.shared & @@ -13,4 +13,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/shamir_witness.wtns.1.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol SHAMIR --curve BLS12-381 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/shamir_witness.wtns.2.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol SHAMIR --curve BLS12-381 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 diff --git a/collaborative-circom/examples/groth16/run_full_multiplier2.sh b/collaborative-circom/examples/groth16/run_full_multiplier2.sh index ae1eb254..09179d7d 100755 --- a/collaborative-circom/examples/groth16/run_full_multiplier2.sh +++ b/collaborative-circom/examples/groth16/run_full_multiplier2.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/multiplier2/circuit.circom --input test_vectors/multiplier2/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/multiplier2 +cargo run --release --bin co-circom -- split-input --circuit test_vectors/multiplier2/circuit.circom --input test_vectors/multiplier2/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/multiplier2 # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/multiplier2/input.json.0.shared --circuit test_vectors/multiplier2/circuit.circom --link-library test_vectors/multiplier2/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/multiplier2/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/multiplier2/input.json.1.shared --circuit test_vectors/multiplier2/circuit.circom --link-library test_vectors/multiplier2/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/multiplier2/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/multiplier2/witness.wtns.1.shared --zkey test_vectors/multiplier2/multiplier2.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/multiplier2/witness.wtns.2.shared --zkey test_vectors/multiplier2/multiplier2.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/multiplier2/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/multiplier2/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_full_poseidon.sh b/collaborative-circom/examples/groth16/run_full_poseidon.sh index aa1aad22..a960df1e 100755 --- a/collaborative-circom/examples/groth16/run_full_poseidon.sh +++ b/collaborative-circom/examples/groth16/run_full_poseidon.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-input --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.0.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/poseidon/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.1.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/poseidon/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_full_poseidon_shamir.sh b/collaborative-circom/examples/groth16/run_full_poseidon_shamir.sh index 70984a29..63e404fe 100755 --- a/collaborative-circom/examples/groth16/run_full_poseidon_shamir.sh +++ b/collaborative-circom/examples/groth16/run_full_poseidon_shamir.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-input --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.0.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/poseidon/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.1.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/poseidon/witness.wtns.1.shared & @@ -13,4 +13,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/shamir_witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/shamir_witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_full_sum_arrays.sh b/collaborative-circom/examples/groth16/run_full_sum_arrays.sh index 522b5bc9..9580ac4b 100755 --- a/collaborative-circom/examples/groth16/run_full_sum_arrays.sh +++ b/collaborative-circom/examples/groth16/run_full_sum_arrays.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/sum_arrays/circuit.circom --input test_vectors/sum_arrays/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/sum_arrays +cargo run --release --bin co-circom -- split-input --circuit test_vectors/sum_arrays/circuit.circom --input test_vectors/sum_arrays/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/sum_arrays # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/sum_arrays/input.json.0.shared --circuit test_vectors/sum_arrays/circuit.circom --link-library test_vectors/sum_arrays/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/sum_arrays/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/sum_arrays/input.json.1.shared --circuit test_vectors/sum_arrays/circuit.circom --link-library test_vectors/sum_arrays/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/sum_arrays/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/sum_arrays/witness.wtns.1.shared --zkey test_vectors/sum_arrays/sum_arrays.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/sum_arrays/witness.wtns.2.shared --zkey test_vectors/sum_arrays/sum_arrays.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/sum_arrays/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/sum_arrays/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_full_with_merge.sh b/collaborative-circom/examples/groth16/run_full_with_merge.sh index 5104896a..d8e7195f 100755 --- a/collaborative-circom/examples/groth16/run_full_with_merge.sh +++ b/collaborative-circom/examples/groth16/run_full_with_merge.sh @@ -1,12 +1,12 @@ EXAMPLE_NAME=multiplier2 # split inputs into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input0.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input1.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME +cargo run --release --bin co-circom -- split-input --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input0.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME +cargo run --release --bin co-circom -- split-input --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input1.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME # merge inputs into single input file -cargo run --release --bin co-circom -- merge-input-shares --config ../configs/config.toml --inputs test_vectors/$EXAMPLE_NAME/input0.json.0.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.0.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.0.shared -cargo run --release --bin co-circom -- merge-input-shares --config ../configs/config.toml --inputs test_vectors/$EXAMPLE_NAME/input0.json.1.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.1.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.1.shared -cargo run --release --bin co-circom -- merge-input-shares --config ../configs/config.toml --inputs test_vectors/$EXAMPLE_NAME/input0.json.2.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.2.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.2.shared +cargo run --release --bin co-circom -- merge-input-shares --inputs test_vectors/$EXAMPLE_NAME/input0.json.0.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.0.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.0.shared +cargo run --release --bin co-circom -- merge-input-shares --inputs test_vectors/$EXAMPLE_NAME/input0.json.1.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.1.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.1.shared +cargo run --release --bin co-circom -- merge-input-shares --inputs test_vectors/$EXAMPLE_NAME/input0.json.2.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.2.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.2.shared # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/$EXAMPLE_NAME/input.json.0.shared --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/$EXAMPLE_NAME/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/$EXAMPLE_NAME/input.json.1.shared --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/$EXAMPLE_NAME/witness.wtns.1.shared & @@ -16,4 +16,4 @@ cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vec cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/$EXAMPLE_NAME/witness.wtns.1.shared --zkey test_vectors/$EXAMPLE_NAME/$EXAMPLE_NAME.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/$EXAMPLE_NAME/witness.wtns.2.shared --zkey test_vectors/$EXAMPLE_NAME/$EXAMPLE_NAME.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/$EXAMPLE_NAME/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/$EXAMPLE_NAME/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_proof_only_kyc_bls.sh b/collaborative-circom/examples/groth16/run_proof_only_kyc_bls.sh index 9b688fe2..0a8df7af 100755 --- a/collaborative-circom/examples/groth16/run_proof_only_kyc_bls.sh +++ b/collaborative-circom/examples/groth16/run_proof_only_kyc_bls.sh @@ -1,8 +1,8 @@ # split input into shares -cargo run --release --bin co-circom -- split-witness --config ../configs/config.toml --witness test_vectors/kyc/bls12/witness.wtns --r1cs test_vectors/kyc/bls12/kyc.r1cs --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc +cargo run --release --bin co-circom -- split-witness --witness test_vectors/kyc/bls12/witness.wtns --r1cs test_vectors/kyc/bls12/kyc.r1cs --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc # run proving in MPC cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/witness.wtns.0.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol REP3 --curve BLS12-381 --config ../configs/party1.toml --out proof.0.json --public-input public_input.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/witness.wtns.1.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol REP3 --curve BLS12-381 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/kyc/witness.wtns.2.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol REP3 --curve BLS12-381 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 diff --git a/collaborative-circom/examples/groth16/run_proof_only_poseidon.sh b/collaborative-circom/examples/groth16/run_proof_only_poseidon.sh index 195af4df..781ce42b 100755 --- a/collaborative-circom/examples/groth16/run_proof_only_poseidon.sh +++ b/collaborative-circom/examples/groth16/run_proof_only_poseidon.sh @@ -1,8 +1,8 @@ # split input into shares -cargo run --release --bin co-circom -- split-witness --config ../configs/config.toml --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-witness --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon # run proving in MPC cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.0.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party1.toml --out proof.0.json --public-input public_input.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/groth16/run_proof_only_poseidon_shamir.sh b/collaborative-circom/examples/groth16/run_proof_only_poseidon_shamir.sh index 355db9a1..cad46b31 100755 --- a/collaborative-circom/examples/groth16/run_proof_only_poseidon_shamir.sh +++ b/collaborative-circom/examples/groth16/run_proof_only_poseidon_shamir.sh @@ -1,8 +1,8 @@ # split input into shares -cargo run --release --bin co-circom -- split-witness --config ../configs/config.toml --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol SHAMIR --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-witness --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol SHAMIR --curve BN254 --out-dir test_vectors/poseidon # run proving in MPC cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.0.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party1.toml --out proof.0.json --public-input public_input.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof groth16 --witness test_vectors/poseidon/witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify groth16 --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify groth16 --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/cleanup.sh b/collaborative-circom/examples/plonk/cleanup.sh new file mode 100755 index 00000000..2a7bb935 --- /dev/null +++ b/collaborative-circom/examples/plonk/cleanup.sh @@ -0,0 +1,4 @@ +# rm all proof files +rm proof.0.json proof.1.json proof.2.json +# delete all shared files +find . -name "*.shared" -type f -delete diff --git a/collaborative-circom/examples/plonk/run_full_kyc.sh b/collaborative-circom/examples/plonk/run_full_kyc.sh index cb209067..dfdb0eb7 100755 --- a/collaborative-circom/examples/plonk/run_full_kyc.sh +++ b/collaborative-circom/examples/plonk/run_full_kyc.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/kyc +cargo run --release --bin co-circom -- split-input --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/kyc # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.0.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/kyc/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.1.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/kyc/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/witness.wtns.1.shared --zkey test_vectors/kyc/bn254/kyc.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/witness.wtns.2.shared --zkey test_vectors/kyc/bn254/kyc.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/kyc/bn254/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/kyc/bn254/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_full_kyc_shamir_bls.sh b/collaborative-circom/examples/plonk/run_full_kyc_shamir_bls.sh index 8c4f34bb..682557c5 100755 --- a/collaborative-circom/examples/plonk/run_full_kyc_shamir_bls.sh +++ b/collaborative-circom/examples/plonk/run_full_kyc_shamir_bls.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc +cargo run --release --bin co-circom -- split-input --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --input test_vectors/kyc/input.json --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.0.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BLS12-381 --config ../configs/party1.toml --out test_vectors/kyc/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/kyc/input.json.1.shared --circuit test_vectors/kyc/circuit.circom --link-library test_vectors/kyc/lib --protocol REP3 --curve BLS12-381 --config ../configs/party2.toml --out test_vectors/kyc/witness.wtns.1.shared & @@ -13,4 +13,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/shamir_witness.wtns.1.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol SHAMIR --curve BLS12-381 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/shamir_witness.wtns.2.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol SHAMIR --curve BLS12-381 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 diff --git a/collaborative-circom/examples/plonk/run_full_multiplier2.sh b/collaborative-circom/examples/plonk/run_full_multiplier2.sh index 00d54a09..c3847cf0 100755 --- a/collaborative-circom/examples/plonk/run_full_multiplier2.sh +++ b/collaborative-circom/examples/plonk/run_full_multiplier2.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/multiplier2/circuit.circom --input test_vectors/multiplier2/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/multiplier2 +cargo run --release --bin co-circom -- split-input --circuit test_vectors/multiplier2/circuit.circom --input test_vectors/multiplier2/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/multiplier2 # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/multiplier2/input.json.0.shared --circuit test_vectors/multiplier2/circuit.circom --link-library test_vectors/multiplier2/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/multiplier2/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/multiplier2/input.json.1.shared --circuit test_vectors/multiplier2/circuit.circom --link-library test_vectors/multiplier2/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/multiplier2/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/multiplier2/witness.wtns.1.shared --zkey test_vectors/multiplier2/multiplier2.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/multiplier2/witness.wtns.2.shared --zkey test_vectors/multiplier2/multiplier2.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/multiplier2/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/multiplier2/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_full_poseidon.sh b/collaborative-circom/examples/plonk/run_full_poseidon.sh index bc5aa657..02fb4119 100755 --- a/collaborative-circom/examples/plonk/run_full_poseidon.sh +++ b/collaborative-circom/examples/plonk/run_full_poseidon.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-input --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.0.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/poseidon/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.1.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/poseidon/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_full_poseidon_shamir.sh b/collaborative-circom/examples/plonk/run_full_poseidon_shamir.sh index 5d7b12c2..d7c4c6b7 100755 --- a/collaborative-circom/examples/plonk/run_full_poseidon_shamir.sh +++ b/collaborative-circom/examples/plonk/run_full_poseidon_shamir.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-input --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --input test_vectors/poseidon/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.0.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/poseidon/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/poseidon/input.json.1.shared --circuit test_vectors/poseidon/circuit.circom --link-library test_vectors/poseidon/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/poseidon/witness.wtns.1.shared & @@ -13,4 +13,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/shamir_witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/shamir_witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_full_sum_arrays.sh b/collaborative-circom/examples/plonk/run_full_sum_arrays.sh index 72bcee2a..b9014d12 100755 --- a/collaborative-circom/examples/plonk/run_full_sum_arrays.sh +++ b/collaborative-circom/examples/plonk/run_full_sum_arrays.sh @@ -1,5 +1,5 @@ # split input into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/sum_arrays/circuit.circom --input test_vectors/sum_arrays/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/sum_arrays +cargo run --release --bin co-circom -- split-input --circuit test_vectors/sum_arrays/circuit.circom --input test_vectors/sum_arrays/input.json --protocol REP3 --curve BN254 --out-dir test_vectors/sum_arrays # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/sum_arrays/input.json.0.shared --circuit test_vectors/sum_arrays/circuit.circom --link-library test_vectors/sum_arrays/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/sum_arrays/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/sum_arrays/input.json.1.shared --circuit test_vectors/sum_arrays/circuit.circom --link-library test_vectors/sum_arrays/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/sum_arrays/witness.wtns.1.shared & @@ -9,4 +9,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/sum_arrays/witness.wtns.1.shared --zkey test_vectors/sum_arrays/sum_arrays.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/sum_arrays/witness.wtns.2.shared --zkey test_vectors/sum_arrays/sum_arrays.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/sum_arrays/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/sum_arrays/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_full_with_merge.sh b/collaborative-circom/examples/plonk/run_full_with_merge.sh index 34eb9e53..5773475e 100755 --- a/collaborative-circom/examples/plonk/run_full_with_merge.sh +++ b/collaborative-circom/examples/plonk/run_full_with_merge.sh @@ -1,12 +1,12 @@ EXAMPLE_NAME=multiplier2 # split inputs into shares -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input0.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME -cargo run --release --bin co-circom -- split-input --config ../configs/config.toml --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input1.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME +cargo run --release --bin co-circom -- split-input --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input0.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME +cargo run --release --bin co-circom -- split-input --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --input test_vectors/$EXAMPLE_NAME/input1.json --protocol REP3 --curve BN254 --out-dir test_vectors/$EXAMPLE_NAME # merge inputs into single input file -cargo run --release --bin co-circom -- merge-input-shares --config ../configs/config.toml --inputs test_vectors/$EXAMPLE_NAME/input0.json.0.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.0.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.0.shared -cargo run --release --bin co-circom -- merge-input-shares --config ../configs/config.toml --inputs test_vectors/$EXAMPLE_NAME/input0.json.1.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.1.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.1.shared -cargo run --release --bin co-circom -- merge-input-shares --config ../configs/config.toml --inputs test_vectors/$EXAMPLE_NAME/input0.json.2.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.2.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.2.shared +cargo run --release --bin co-circom -- merge-input-shares --inputs test_vectors/$EXAMPLE_NAME/input0.json.0.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.0.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.0.shared +cargo run --release --bin co-circom -- merge-input-shares --inputs test_vectors/$EXAMPLE_NAME/input0.json.1.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.1.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.1.shared +cargo run --release --bin co-circom -- merge-input-shares --inputs test_vectors/$EXAMPLE_NAME/input0.json.2.shared --inputs test_vectors/$EXAMPLE_NAME/input1.json.2.shared --protocol REP3 --curve BN254 --out test_vectors/$EXAMPLE_NAME/input.json.2.shared # run witness extension in MPC cargo run --release --bin co-circom -- generate-witness --input test_vectors/$EXAMPLE_NAME/input.json.0.shared --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --protocol REP3 --curve BN254 --config ../configs/party1.toml --out test_vectors/$EXAMPLE_NAME/witness.wtns.0.shared & cargo run --release --bin co-circom -- generate-witness --input test_vectors/$EXAMPLE_NAME/input.json.1.shared --circuit test_vectors/$EXAMPLE_NAME/circuit.circom --link-library test_vectors/$EXAMPLE_NAME/lib --protocol REP3 --curve BN254 --config ../configs/party2.toml --out test_vectors/$EXAMPLE_NAME/witness.wtns.1.shared & @@ -16,4 +16,4 @@ cargo run --release --bin co-circom -- generate-proof plonk --witness test_vecto cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/$EXAMPLE_NAME/witness.wtns.1.shared --zkey test_vectors/$EXAMPLE_NAME/$EXAMPLE_NAME.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/$EXAMPLE_NAME/witness.wtns.2.shared --zkey test_vectors/$EXAMPLE_NAME/$EXAMPLE_NAME.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/$EXAMPLE_NAME/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/$EXAMPLE_NAME/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_proof_only_kyc_bls.sh b/collaborative-circom/examples/plonk/run_proof_only_kyc_bls.sh index 3a9cf3f5..c870d285 100755 --- a/collaborative-circom/examples/plonk/run_proof_only_kyc_bls.sh +++ b/collaborative-circom/examples/plonk/run_proof_only_kyc_bls.sh @@ -1,8 +1,8 @@ # split input into shares -cargo run --release --bin co-circom -- split-witness --config ../configs/config.toml --witness test_vectors/kyc/bls12/witness.wtns --r1cs test_vectors/kyc/bls12/kyc.r1cs --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc +cargo run --release --bin co-circom -- split-witness --witness test_vectors/kyc/bls12/witness.wtns --r1cs test_vectors/kyc/bls12/kyc.r1cs --protocol REP3 --curve BLS12-381 --out-dir test_vectors/kyc # run proving in MPC cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/witness.wtns.0.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol REP3 --curve BLS12-381 --config ../configs/party1.toml --out proof.0.json --public-input public_input.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/witness.wtns.1.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol REP3 --curve BLS12-381 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/kyc/witness.wtns.2.shared --zkey test_vectors/kyc/bls12/kyc.zkey --protocol REP3 --curve BLS12-381 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/kyc/bls12/verification_key.json --public-input public_input.json --curve BLS12-381 diff --git a/collaborative-circom/examples/plonk/run_proof_only_poseidon.sh b/collaborative-circom/examples/plonk/run_proof_only_poseidon.sh index 3f2f7257..414417c6 100755 --- a/collaborative-circom/examples/plonk/run_proof_only_poseidon.sh +++ b/collaborative-circom/examples/plonk/run_proof_only_poseidon.sh @@ -1,8 +1,8 @@ # split input into shares -cargo run --release --bin co-circom -- split-witness --config ../configs/config.toml --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-witness --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol REP3 --curve BN254 --out-dir test_vectors/poseidon # run proving in MPC cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.0.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party1.toml --out proof.0.json --public-input public_input.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol REP3 --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/examples/plonk/run_proof_only_poseidon_shamir.sh b/collaborative-circom/examples/plonk/run_proof_only_poseidon_shamir.sh index e9f22cc6..2f476df0 100755 --- a/collaborative-circom/examples/plonk/run_proof_only_poseidon_shamir.sh +++ b/collaborative-circom/examples/plonk/run_proof_only_poseidon_shamir.sh @@ -1,8 +1,8 @@ # split input into shares -cargo run --release --bin co-circom -- split-witness --config ../configs/config.toml --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol SHAMIR --curve BN254 --out-dir test_vectors/poseidon +cargo run --release --bin co-circom -- split-witness --witness test_vectors/poseidon/witness.wtns --r1cs test_vectors/poseidon/poseidon.r1cs --protocol SHAMIR --curve BN254 --out-dir test_vectors/poseidon # run proving in MPC cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.0.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party1.toml --out proof.0.json --public-input public_input.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.1.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party2.toml --out proof.1.json & cargo run --release --bin co-circom -- generate-proof plonk --witness test_vectors/poseidon/witness.wtns.2.shared --zkey test_vectors/poseidon/poseidon.zkey --protocol SHAMIR --curve BN254 --config ../configs/party3.toml --out proof.2.json # verify proof -cargo run --release --bin co-circom -- verify plonk --config ../configs/config.toml --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 +cargo run --release --bin co-circom -- verify plonk --proof proof.0.json --vk test_vectors/poseidon/verification_key.json --public-input public_input.json --curve BN254 diff --git a/collaborative-circom/src/bin/bench-co-circom.rs b/collaborative-circom/src/bin/bench-co-circom.rs index 925315dd..8ae406d9 100644 --- a/collaborative-circom/src/bin/bench-co-circom.rs +++ b/collaborative-circom/src/bin/bench-co-circom.rs @@ -64,6 +64,9 @@ struct Cli { /// MPC protocol for co-circom #[arg(long, default_value = "REP3")] protocol: String, + /// MPC curve for co-circom + #[arg(long, default_value = "BN254")] + curve: String, /// The path to the co_circom binary #[arg(long, default_value = "co_circom")] co_circom_bin: String, @@ -195,6 +198,7 @@ struct Config { keep_wtns: bool, keep_inp_shr: bool, protocol: String, + curve: String, vkey: PathBuf, pub_inp_sjs: PathBuf, proof_sjs: PathBuf, @@ -435,6 +439,7 @@ impl From for Config { keep_wtns: cli.keep_wtns, keep_inp_shr: cli.keep_inp_shr, protocol: cli.protocol, + curve: cli.curve, vkey: cli.vkey, pub_inp_sjs: cli.pub_inp_sjs, proof_sjs: cli.proof_sjs, @@ -708,6 +713,8 @@ fn bench_co_circom_prover_one_party( .arg(wtns_shr) .arg("--zkey") .arg(conf.zkey.as_path()) + .arg("--curve") + .arg(&conf.curve) .arg("--protocol") .arg(&conf.protocol) .arg("--config") @@ -801,6 +808,8 @@ fn bench_co_circom_gen_wtns_one_party( .arg(conf.circom.as_ref().expect("gen witness is true").as_path()) .arg("--protocol") .arg(&conf.protocol) + .arg("--curve") + .arg(&conf.curve) .arg("--config") .arg(config_toml) .arg("--out") @@ -887,6 +896,8 @@ fn bench_co_circom(conf: &Config) -> color_eyre::Result { .arg(conf.circom.as_ref().expect("gen witness is true").as_path()) .arg("--protocol") .arg(&conf.protocol) + .arg("--curve") + .arg(&conf.curve) .arg("--out-dir") .arg(".") .args(link_library_to_args(conf, "--link-library")?) @@ -918,6 +929,8 @@ fn bench_co_circom(conf: &Config) -> color_eyre::Result { .arg(conf.r1cs.as_ref().expect("gen witness is false").as_path()) .arg("--protocol") .arg(&conf.protocol) + .arg("--curve") + .arg(&conf.curve) .arg("--out-dir") .arg( conf.witness_path @@ -944,6 +957,8 @@ fn bench_co_circom(conf: &Config) -> color_eyre::Result { let now = Instant::now(); let out_co_circom_verifier = Command::new(&conf.co_circom_bin) .arg("verify") + .arg("--curve") + .arg(&conf.curve) .arg("--proof") .arg(conf.proof_coc_1.as_path()) .arg("--vk") diff --git a/collaborative-circom/src/bin/co-circom.rs b/collaborative-circom/src/bin/co-circom.rs index ad47df21..ac87ab2f 100644 --- a/collaborative-circom/src/bin/co-circom.rs +++ b/collaborative-circom/src/bin/co-circom.rs @@ -3,20 +3,23 @@ use ark_bn254::Bn254; use ark_ec::pairing::Pairing; use ark_ff::PrimeField; use circom_mpc_compiler::CompilerBuilder; -use circom_types::groth16::{ - Groth16Proof, JsonVerificationKey as Groth16JsonVerificationKey, ZKey as Groth16ZKey, -}; -use circom_types::plonk::JsonVerificationKey as PlonkJsonVerificationKey; -use circom_types::plonk::PlonkProof; -use circom_types::plonk::ZKey as PlonkZKey; -use circom_types::traits::CircomArkworksPairingBridge; -use circom_types::traits::CircomArkworksPrimeFieldBridge; -use circom_types::Witness; use circom_types::R1CS; +use circom_types::{ + groth16::{ + Groth16Proof, JsonVerificationKey as Groth16JsonVerificationKey, ZKey as Groth16ZKey, + }, + plonk::{JsonVerificationKey as PlonkJsonVerificationKey, PlonkProof, ZKey as PlonkZKey}, + traits::{CircomArkworksPairingBridge, CircomArkworksPrimeFieldBridge}, + Witness, +}; use clap::{Parser, Subcommand}; -use collaborative_circom::{file_utils, Config, MPCCurve, MPCProtocol, ProofSystem}; -use collaborative_groth16::groth16::Groth16; -use collaborative_groth16::groth16::{CollaborativeGroth16, SharedInput, SharedWitness}; +use collaborative_circom::{ + file_utils, GenerateProofCli, GenerateProofConfig, GenerateWitnessCli, GenerateWitnessConfig, + MPCCurve, MPCProtocol, MergeInputSharesCli, MergeInputSharesConfig, ProofSystem, SplitInputCli, + SplitInputConfig, SplitWitnessCli, SplitWitnessConfig, TranslateWitnessCli, + TranslateWitnessConfig, VerifyCli, VerifyConfig, +}; +use collaborative_groth16::groth16::{CollaborativeGroth16, Groth16, SharedInput, SharedWitness}; use collaborative_plonk::{plonk::Plonk, CollaborativePlonk}; use color_eyre::eyre::{eyre, Context, ContextCompat}; use mpc_core::{ @@ -60,758 +63,597 @@ struct Cli { #[derive(Subcommand)] enum Commands { /// Splits an existing witness file generated by Circom into secret shares for use in MPC - SplitWitness { - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the input witness file generated by Circom - #[arg(long)] - witness: PathBuf, - /// The path to the r1cs file, generated by Circom compiler - #[arg(long)] - r1cs: PathBuf, - /// The MPC protocol to be used - #[arg(long, value_enum)] - protocol: Option, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The path to the (existing) output directory - #[arg(long)] - out_dir: PathBuf, - /// The threshold of tolerated colluding parties - #[arg(short, long, default_value_t = 1)] - threshold: usize, - /// The number of parties - #[arg(short, long, default_value_t = 3)] - num_parties: usize, - }, + SplitWitness(SplitWitnessCli), /// Splits a JSON input file into secret shares for use in MPC - SplitInput { - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the input JSON file - #[arg(long)] - input: PathBuf, - /// The path to the circuit file - #[arg(long)] - circuit: String, - /// The path to Circom library files - #[arg(long)] - link_library: Vec, - /// The MPC protocol to be used - #[arg(long, value_enum)] - protocol: Option, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The path to the (existing) output directory - #[arg(long)] - out_dir: PathBuf, - }, + SplitInput(SplitInputCli), /// Merge multiple shared inputs received from multiple parties into a single one - MergeInputShares { - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the input JSON file - #[arg(long)] - inputs: Vec, - /// The MPC protocol to be used - #[arg(long, value_enum)] - protocol: Option, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The output file where the merged input share is written to - #[arg(long)] - out: PathBuf, - }, + MergeInputShares(MergeInputSharesCli), /// Evaluates the extended witness generation for the specified circuit and input share in MPC - GenerateWitness { - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the input share file - #[arg(long)] - input: PathBuf, - /// The path to the circuit file - #[arg(long)] - circuit: String, - /// The path to Circom library files - #[arg(long)] - link_library: Vec, - /// The MPC protocol to be used - #[arg(long, value_enum)] - protocol: Option, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The output file where the final witness share is written to - #[arg(long)] - out: PathBuf, - }, + GenerateWitness(GenerateWitnessCli), /// Translates the witness generated with one MPC protocol to a witness for a different one - TranslateWitness { - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the witness share file - #[arg(long)] - witness: PathBuf, - /// The MPC protocol that was used for the witness generation - #[arg(long, value_enum)] - src_protocol: MPCProtocol, - /// The MPC protocol to be used for the proof generation - #[arg(long, value_enum)] - target_protocol: MPCProtocol, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The output file where the final witness share is written to - #[arg(long)] - out: PathBuf, - }, + TranslateWitness(TranslateWitnessCli), /// Evaluates the prover algorithm for the specified circuit and witness share in MPC - GenerateProof { - // The proof system to be used - #[arg(value_enum)] - proofsystem: ProofSystem, - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the witness share file - #[arg(long)] - witness: PathBuf, - /// The path to the proving key (.zkey) file, generated by snarkjs setup phase - #[arg(long)] - zkey: PathBuf, - /// The MPC protocol to be used - #[arg(long, value_enum)] - protocol: Option, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The output file where the final proof is written to. If not passed, this party will not write the proof to a file. - #[arg(long)] - out: Option, - /// The output JSON file where the public inputs are written to. If not passed, this party will not write the public inputs to a file. - #[arg(long)] - public_input: Option, - /// The threshold of tolerated colluding parties - #[arg(short, long, default_value_t = 1)] - threshold: usize, - }, + GenerateProof(GenerateProofCli), /// Verification of a Circom proof. - Verify { - // The proof system to be used - #[arg(value_enum)] - proofsystem: ProofSystem, - /// The path to the config file - #[arg(long)] - config: PathBuf, - /// The path to the proof file - #[arg(long)] - proof: PathBuf, - /// The pairing friendly curve to be used - #[arg(long, value_enum)] - curve: Option, - /// The path to the verification key file - #[arg(long)] - vk: PathBuf, - /// The path to the public input JSON file - #[arg(long)] - public_input: PathBuf, - }, -} - -impl Commands { - fn get_config_path(&self) -> &PathBuf { - match self { - Self::SplitWitness { config, .. } => config, - Self::SplitInput { config, .. } => config, - Self::MergeInputShares { config, .. } => config, - Self::GenerateWitness { config, .. } => config, - Self::TranslateWitness { config, .. } => config, - Self::GenerateProof { config, .. } => config, - Self::Verify { config, .. } => config, - } - } - - fn get_curve(&self) -> Option { - match self { - Self::SplitWitness { curve, .. } => *curve, - Self::SplitInput { curve, .. } => *curve, - Self::MergeInputShares { curve, .. } => *curve, - Self::GenerateWitness { curve, .. } => *curve, - Self::TranslateWitness { curve, .. } => *curve, - Self::GenerateProof { curve, .. } => *curve, - Self::Verify { curve, .. } => *curve, - } - } + Verify(VerifyCli), } fn main() -> color_eyre::Result { install_tracing(); let args = Cli::parse(); - let config_path = args.command.get_config_path(); - - file_utils::check_file_exists(config_path)?; - // parse and merge config from env and config file - let config = Config::new(config_path.to_str().context("invalid path")?) - .context("expected valid config")?; - - // verify that curve is set in args or config - let curve = args - .command - .get_curve() - .or(config.curve) - .context("expected curve either in args or config")?; - - match curve { - MPCCurve::BN254 => main_function::(args, config), - MPCCurve::BLS12_381 => main_function::(args, config), + match args.command { + Commands::SplitWitness(cli) => { + let config = SplitWitnessConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_split_witness::(config), + MPCCurve::BLS12_381 => run_split_witness::(config), + } + } + Commands::SplitInput(cli) => { + let config = SplitInputConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_split_input::(config), + MPCCurve::BLS12_381 => run_split_input::(config), + } + } + Commands::MergeInputShares(cli) => { + let config = MergeInputSharesConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_merge_input_shares::(config), + MPCCurve::BLS12_381 => run_merge_input_shares::(config), + } + } + Commands::GenerateWitness(cli) => { + let config = GenerateWitnessConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_generate_witness::(config), + MPCCurve::BLS12_381 => run_generate_witness::(config), + } + } + Commands::TranslateWitness(cli) => { + let config = TranslateWitnessConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_translate_witness::(config), + MPCCurve::BLS12_381 => run_translate_witness::(config), + } + } + Commands::GenerateProof(cli) => { + let config = GenerateProofConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_generate_proof::(config), + MPCCurve::BLS12_381 => run_generate_proof::(config), + } + } + Commands::Verify(cli) => { + let config = VerifyConfig::parse(cli).context("while parsing config")?; + match config.curve { + MPCCurve::BN254 => run_verify::(config), + MPCCurve::BLS12_381 => run_verify::(config), + } + } } } -fn main_function( - args: Cli, - cfg: Config, +fn run_split_witness( + config: SplitWitnessConfig, ) -> color_eyre::Result where P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, P::BaseField: CircomArkworksPrimeFieldBridge, { - match args.command { - Commands::SplitWitness { - config: _, - witness: witness_path, - r1cs, - protocol, - curve: _, - out_dir, - threshold: t, - num_parties: n, - } => { - file_utils::check_file_exists(&witness_path)?; - file_utils::check_file_exists(&r1cs)?; - file_utils::check_dir_exists(&out_dir)?; - - // verify that protocol is set in args or config - let protocol = protocol - .or(cfg.protocol) - .context("protocol required for this command")?; - - // read the Circom witness file - let witness_file = - BufReader::new(File::open(&witness_path).context("while opening witness file")?); - let witness = Witness::::from_reader(witness_file) - .context("while parsing witness file")?; - - // read the Circom r1cs file - let r1cs_file = BufReader::new(File::open(&r1cs).context("while opening r1cs file")?); - let r1cs = R1CS::

::from_reader(r1cs_file).context("while parsing r1cs file")?; - - let mut rng = rand::thread_rng(); - - match protocol { - MPCProtocol::REP3 => { - if t != 1 { - return Err(eyre!("REP3 only allows the threshold to be 1")); - } - if n != 3 { - return Err(eyre!("REP3 only allows the number of parties to be 3")); - } - // create witness shares - let shares = - SharedWitness::, P>::share_rep3( - witness, - r1cs.num_inputs, - &mut rng, - ); - - // write out the shares to the output directory - let base_name = witness_path - .file_name() - .context("we have a file name")? - .to_str() - .context("witness file name is not valid UTF-8")?; - for (i, share) in shares.iter().enumerate() { - let path = out_dir.join(format!("{}.{}.shared", base_name, i)); - let out_file = BufWriter::new( - File::create(&path).context("while creating output file")?, - ); - bincode::serialize_into(out_file, share) - .context("while serializing witness share")?; - tracing::info!("Wrote witness share {} to file {}", i, path.display()); - } - } - MPCProtocol::SHAMIR => { - // create witness shares - let shares = - SharedWitness::, P>::share_shamir( - witness, - r1cs.num_inputs,t,n, - &mut rng, - ); - - // write out the shares to the output directory - let base_name = witness_path - .file_name() - .context("we have a file name")? - .to_str() - .context("witness file name is not valid UTF-8")?; - for (i, share) in shares.iter().enumerate() { - let path = out_dir.join(format!("{}.{}.shared", base_name, i)); - let out_file = BufWriter::new( - File::create(&path).context("while creating output file")?, - ); - bincode::serialize_into(out_file, share) - .context("while serializing witness share")?; - tracing::info!("Wrote witness share {} to file {}", i, path.display()); - } - } + let witness_path = config.witness; + let r1cs = config.r1cs; + let protocol = config.protocol; + let out_dir = config.out_dir; + let t = config.threshold; + let n = config.num_parties; + + file_utils::check_file_exists(&witness_path)?; + file_utils::check_file_exists(&r1cs)?; + file_utils::check_dir_exists(&out_dir)?; + + // read the Circom witness file + let witness_file = + BufReader::new(File::open(&witness_path).context("while opening witness file")?); + let witness = Witness::::from_reader(witness_file) + .context("while parsing witness file")?; + + // read the Circom r1cs file + let r1cs_file = BufReader::new(File::open(&r1cs).context("while opening r1cs file")?); + let r1cs = R1CS::

::from_reader(r1cs_file).context("while parsing r1cs file")?; + + let mut rng = rand::thread_rng(); + + match protocol { + MPCProtocol::REP3 => { + if t != 1 { + return Err(eyre!("REP3 only allows the threshold to be 1")); } - tracing::info!("Split witness into shares successfully") - } - Commands::SplitInput { - config: _, - input, - circuit, - link_library, - protocol, - curve: _, - out_dir, - } => { - // verify that protocol is set in args or config - let protocol = protocol - .or(cfg.protocol) - .context("protocol required for this command")?; - - if protocol != MPCProtocol::REP3 { - return Err(eyre!( - "Only REP3 protocol is supported for splitting inputs" - )); + if n != 3 { + return Err(eyre!("REP3 only allows the number of parties to be 3")); } - file_utils::check_file_exists(&input)?; - let circuit_path = PathBuf::from(&circuit); - file_utils::check_file_exists(&circuit_path)?; - file_utils::check_dir_exists(&out_dir)?; - - let compiler_config = cfg.compiler.context("expected a compiler config")?; + // create witness shares + let shares = SharedWitness::, P>::share_rep3( + witness, + r1cs.num_inputs, + &mut rng, + ); - //get the public inputs if any from parser - let mut builder = CompilerBuilder::

::new(compiler_config, circuit); - for lib in link_library { - builder = builder.link_library(lib); - } - let public_inputs = builder.build().get_public_inputs()?; - - // read the input file - let input_file = - BufReader::new(File::open(&input).context("while opening input file")?); - - let input_json: serde_json::Map = - serde_json::from_reader(input_file).context("while parsing input file")?; - - // create input shares - let mut shares = [ - SharedInput::, P>::default(), - SharedInput::, P>::default(), - SharedInput::, P>::default(), - ]; - - let mut rng = rand::thread_rng(); - for (name, val) in input_json { - let parsed_vals = if val.is_array() { - parse_array(&val)? - } else { - vec![parse_field(&val)?] - }; - if public_inputs.contains(&name) { - shares[0] - .public_inputs - .insert(name.clone(), parsed_vals.clone()); - shares[1] - .public_inputs - .insert(name.clone(), parsed_vals.clone()); - shares[2].public_inputs.insert(name.clone(), parsed_vals); - } else { - let [share0, share1, share2] = - rep3::utils::share_field_elements(&parsed_vals, &mut rng); - shares[0].shared_inputs.insert(name.clone(), share0); - shares[1].shared_inputs.insert(name.clone(), share1); - shares[2].shared_inputs.insert(name.clone(), share2); - } + // write out the shares to the output directory + let base_name = witness_path + .file_name() + .context("we have a file name")? + .to_str() + .context("witness file name is not valid UTF-8")?; + for (i, share) in shares.iter().enumerate() { + let path = out_dir.join(format!("{}.{}.shared", base_name, i)); + let out_file = + BufWriter::new(File::create(&path).context("while creating output file")?); + bincode::serialize_into(out_file, share) + .context("while serializing witness share")?; + tracing::info!("Wrote witness share {} to file {}", i, path.display()); } + } + MPCProtocol::SHAMIR => { + // create witness shares + let shares = + SharedWitness::, P>::share_shamir( + witness, + r1cs.num_inputs, + t, + n, + &mut rng, + ); // write out the shares to the output directory - let base_name = input + let base_name = witness_path .file_name() .context("we have a file name")? .to_str() - .context("input file name is not valid UTF-8")?; + .context("witness file name is not valid UTF-8")?; for (i, share) in shares.iter().enumerate() { let path = out_dir.join(format!("{}.{}.shared", base_name, i)); let out_file = BufWriter::new(File::create(&path).context("while creating output file")?); bincode::serialize_into(out_file, share) .context("while serializing witness share")?; - tracing::info!("Wrote input share {} to file {}", i, path.display()); + tracing::info!("Wrote witness share {} to file {}", i, path.display()); } - tracing::info!("Split input into shares successfully") } - Commands::MergeInputShares { - config: _, - inputs, - protocol, - curve: _, - out, - } => { - if inputs.len() < 2 { - return Err(eyre!("Need at least two input shares to merge")); - } - for input in &inputs { - file_utils::check_file_exists(input)?; - } + } + tracing::info!("Split witness into shares successfully"); + Ok(ExitCode::SUCCESS) +} - // verify that protocol is set in args or config - let protocol = protocol - .or(cfg.protocol) - .context("protocol required for this command")?; +fn run_split_input( + config: SplitInputConfig, +) -> color_eyre::Result +where + P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, + P::BaseField: CircomArkworksPrimeFieldBridge, +{ + let input = config.input; + let circuit = config.circuit; + let link_library = config.link_library; + let protocol = config.protocol; + let out_dir = config.out_dir; + + if protocol != MPCProtocol::REP3 { + return Err(eyre!( + "Only REP3 protocol is supported for splitting inputs" + )); + } + file_utils::check_file_exists(&input)?; + let circuit_path = PathBuf::from(&circuit); + file_utils::check_file_exists(&circuit_path)?; + file_utils::check_dir_exists(&out_dir)?; + + //get the public inputs if any from parser + let mut builder = CompilerBuilder::

::new(config.compiler, circuit); + for lib in link_library { + builder = builder.link_library(lib); + } + let public_inputs = builder.build().get_public_inputs()?; - match protocol { - MPCProtocol::REP3 => { - merge_input_shares::>(inputs, out)?; - } - MPCProtocol::SHAMIR => { - merge_input_shares::>( - inputs, out, - )?; - } - } + // read the input file + let input_file = BufReader::new(File::open(&input).context("while opening input file")?); + + let input_json: serde_json::Map = + serde_json::from_reader(input_file).context("while parsing input file")?; + + // create input shares + let mut shares = [ + SharedInput::, P>::default(), + SharedInput::, P>::default(), + SharedInput::, P>::default(), + ]; + + let mut rng = rand::thread_rng(); + for (name, val) in input_json { + let parsed_vals = if val.is_array() { + parse_array(&val)? + } else { + vec![parse_field(&val)?] + }; + if public_inputs.contains(&name) { + shares[0] + .public_inputs + .insert(name.clone(), parsed_vals.clone()); + shares[1] + .public_inputs + .insert(name.clone(), parsed_vals.clone()); + shares[2].public_inputs.insert(name.clone(), parsed_vals); + } else { + let [share0, share1, share2] = + rep3::utils::share_field_elements(&parsed_vals, &mut rng); + shares[0].shared_inputs.insert(name.clone(), share0); + shares[1].shared_inputs.insert(name.clone(), share1); + shares[2].shared_inputs.insert(name.clone(), share2); } - Commands::GenerateWitness { - config: _, - input, - circuit, - link_library, - protocol, - curve: _, - out, - } => { - // verify that protocol is set in args or config - let protocol = protocol - .or(cfg.protocol) - .context("protocol required for this command")?; - - if protocol != MPCProtocol::REP3 { - return Err(eyre!( - "Only REP3 protocol is supported for merging input shares" - )); - } - file_utils::check_file_exists(&input)?; - let circuit_path = PathBuf::from(&circuit); - file_utils::check_file_exists(&circuit_path)?; + } - // parse input shares - let input_share_file = - BufReader::new(File::open(&input).context("while opening input share file")?); - let input_share = collaborative_circom::parse_shared_input(input_share_file)?; - - let result_witness_share = collaborative_circom::generate_witness_rep3::

( - circuit, - link_library, - input_share, - cfg, - )?; - // write result to output file - let out_file = BufWriter::new(std::fs::File::create(&out)?); - bincode::serialize_into(out_file, &result_witness_share)?; - tracing::info!("Witness successfully written to {}", out.display()); + // write out the shares to the output directory + let base_name = input + .file_name() + .context("we have a file name")? + .to_str() + .context("input file name is not valid UTF-8")?; + for (i, share) in shares.iter().enumerate() { + let path = out_dir.join(format!("{}.{}.shared", base_name, i)); + let out_file = BufWriter::new(File::create(&path).context("while creating output file")?); + bincode::serialize_into(out_file, share).context("while serializing witness share")?; + tracing::info!("Wrote input share {} to file {}", i, path.display()); + } + tracing::info!("Split input into shares successfully"); + Ok(ExitCode::SUCCESS) +} + +fn run_merge_input_shares( + config: MergeInputSharesConfig, +) -> color_eyre::Result +where + P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, + P::BaseField: CircomArkworksPrimeFieldBridge, +{ + let inputs = config.inputs; + let protocol = config.protocol; + let out = config.out; + + if inputs.len() < 2 { + return Err(eyre!("Need at least two input shares to merge")); + } + for input in &inputs { + file_utils::check_file_exists(input)?; + } + + match protocol { + MPCProtocol::REP3 => { + merge_input_shares::>(inputs, out)?; } - Commands::TranslateWitness { - config: _, - witness, - src_protocol, - target_protocol, - curve: _, - out, - } => { - if src_protocol != MPCProtocol::REP3 || target_protocol != MPCProtocol::SHAMIR { - return Err(eyre!("Only REP3 to SHAMIR translation is supported")); - } - file_utils::check_file_exists(&witness)?; - - let network_config = cfg.network.context("expected network config")?; - - // parse witness shares - let witness_file = - BufReader::new(File::open(witness).context("trying to open witness share file")?); - let witness_share: SharedWitness, P> = - collaborative_circom::parse_witness_share(witness_file)?; - - // connect to network - let net = Rep3MpcNet::new(network_config)?; - - // init MPC protocol - let protocol = Rep3Protocol::new(net)?; - let mut protocol = protocol.get_shamir_protocol()?; - - // Translate witness to shamir shares - let shamir_witness_share: SharedWitness< - ShamirProtocol, - P, - > = SharedWitness { - public_inputs: witness_share.public_inputs, - witness: protocol.translate_primefield_repshare_vec(witness_share.witness)?, - }; - // write result to output file - let out_file = BufWriter::new(std::fs::File::create(&out)?); - bincode::serialize_into(out_file, &shamir_witness_share)?; - tracing::info!("Witness successfully written to {}", out.display()); + MPCProtocol::SHAMIR => { + merge_input_shares::>(inputs, out)? } - Commands::GenerateProof { - proofsystem, - config: _, - witness, - zkey, - protocol, - curve: _, - out, - public_input: public_input_filename, - threshold: t, - } => { - file_utils::check_file_exists(&witness)?; - file_utils::check_file_exists(&zkey)?; - - let network_config = cfg.network.context("expected a network config")?; - - // verify that protocol is set in args or config - let protocol = protocol - .or(cfg.protocol) - .context("protocol required for this command")?; - - // parse witness shares - let witness_file = - BufReader::new(File::open(witness).context("trying to open witness share file")?); - - // parse Circom zkey file - let zkey_file = File::open(zkey)?; - - let public_input = match proofsystem { - ProofSystem::Groth16 => { - let zkey = Groth16ZKey::

::from_reader(zkey_file).unwrap(); - - let (proof, public_input) = match protocol { - MPCProtocol::REP3 => { - if t != 1 { - return Err(eyre!("REP3 only allows the threshold to be 1")); - } - - let witness_share = - collaborative_circom::parse_witness_share(witness_file)?; - let public_input = witness_share.public_inputs.clone(); - // connect to network - let net = Rep3MpcNet::new(network_config)?; - - // init MPC protocol - let protocol = Rep3Protocol::new(net)?; - - let mut prover = CollaborativeGroth16::new(protocol); - - // execute prover in MPC - let proof = prover.prove(&zkey, witness_share)?; - (proof, public_input) - } - MPCProtocol::SHAMIR => { - let witness_share = - collaborative_circom::parse_witness_share(witness_file)?; - let public_input = witness_share.public_inputs.clone(); - - // connect to network - let net = ShamirMpcNet::new(network_config)?; - - // init MPC protocol - let protocol = ShamirProtocol::new(t, net)?; - - let mut prover = CollaborativeGroth16::new(protocol); - - // execute prover in MPC - let proof = prover.prove(&zkey, witness_share)?; - (proof, public_input) - } - }; - - // write result to output file - if let Some(out) = out { - let out_file = BufWriter::new( - std::fs::File::create(&out).context("while creating output file")?, - ); - - serde_json::to_writer(out_file, &proof) - .context("while serializing proof to JSON file")?; - tracing::info!("Wrote proof to file {}", out.display()); + } + + Ok(ExitCode::SUCCESS) +} + +fn run_generate_witness( + config: GenerateWitnessConfig, +) -> color_eyre::Result +where + P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, + P::BaseField: CircomArkworksPrimeFieldBridge, +{ + let input = config.input.clone(); + let circuit = config.circuit.clone(); + let link_library = config.link_library.clone(); + let protocol = config.protocol; + let out = config.out.clone(); + + if protocol != MPCProtocol::REP3 { + return Err(eyre!( + "Only REP3 protocol is supported for merging input shares" + )); + } + file_utils::check_file_exists(&input)?; + let circuit_path = PathBuf::from(&circuit); + file_utils::check_file_exists(&circuit_path)?; + + // parse input shares + let input_share_file = + BufReader::new(File::open(&input).context("while opening input share file")?); + let input_share = collaborative_circom::parse_shared_input(input_share_file)?; + + let result_witness_share = collaborative_circom::generate_witness_rep3::

( + circuit, + link_library, + input_share, + config, + )?; + // write result to output file + let out_file = BufWriter::new(std::fs::File::create(&out)?); + bincode::serialize_into(out_file, &result_witness_share)?; + tracing::info!("Witness successfully written to {}", out.display()); + Ok(ExitCode::SUCCESS) +} + +fn run_translate_witness( + config: TranslateWitnessConfig, +) -> color_eyre::Result +where + P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, + P::BaseField: CircomArkworksPrimeFieldBridge, +{ + let witness = config.witness; + let src_protocol = config.src_protocol; + let target_protocol = config.target_protocol; + let out = config.out; + + if src_protocol != MPCProtocol::REP3 || target_protocol != MPCProtocol::SHAMIR { + return Err(eyre!("Only REP3 to SHAMIR translation is supported")); + } + file_utils::check_file_exists(&witness)?; + + // parse witness shares + let witness_file = + BufReader::new(File::open(witness).context("trying to open witness share file")?); + let witness_share: SharedWitness, P> = + collaborative_circom::parse_witness_share(witness_file)?; + + // connect to network + let net = Rep3MpcNet::new(config.network)?; + + // init MPC protocol + let protocol = Rep3Protocol::new(net)?; + let mut protocol = protocol.get_shamir_protocol()?; + + // Translate witness to shamir shares + let shamir_witness_share: SharedWitness, P> = + SharedWitness { + public_inputs: witness_share.public_inputs, + witness: protocol.translate_primefield_repshare_vec(witness_share.witness)?, + }; + // write result to output file + let out_file = BufWriter::new(std::fs::File::create(&out)?); + bincode::serialize_into(out_file, &shamir_witness_share)?; + tracing::info!("Witness successfully written to {}", out.display()); + Ok(ExitCode::SUCCESS) +} + +fn run_generate_proof( + config: GenerateProofConfig, +) -> color_eyre::Result +where + P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, + P::BaseField: CircomArkworksPrimeFieldBridge, +{ + let proof_system = config.proof_system; + let witness = config.witness; + let zkey = config.zkey; + let protocol = config.protocol; + let out = config.out; + let public_input_filename = config.public_input; + let t = config.threshold; + + file_utils::check_file_exists(&witness)?; + file_utils::check_file_exists(&zkey)?; + + // parse witness shares + let witness_file = + BufReader::new(File::open(witness).context("trying to open witness share file")?); + + // parse Circom zkey file + let zkey_file = File::open(zkey)?; + + let public_input = match proof_system { + ProofSystem::Groth16 => { + let zkey = Groth16ZKey::

::from_reader(zkey_file).context("reading zkey")?; + + let (proof, public_input) = match protocol { + MPCProtocol::REP3 => { + if t != 1 { + return Err(eyre!("REP3 only allows the threshold to be 1")); } - public_input + + let witness_share = collaborative_circom::parse_witness_share(witness_file)?; + let public_input = witness_share.public_inputs.clone(); + // connect to network + let net = Rep3MpcNet::new(config.network)?; + + // init MPC protocol + let protocol = Rep3Protocol::new(net)?; + + let mut prover = CollaborativeGroth16::new(protocol); + + // execute prover in MPC + let proof = prover.prove(&zkey, witness_share)?; + (proof, public_input) } - ProofSystem::Plonk => { - let pk = PlonkZKey::

::from_reader(zkey_file).unwrap(); - - let (proof, public_input) = match protocol { - MPCProtocol::REP3 => { - if t != 1 { - return Err(eyre!("REP3 only allows the threshold to be 1")); - } - - let witness_share = - collaborative_circom::parse_witness_share(witness_file)?; - let public_input = witness_share.public_inputs.clone(); - // connect to network - let net = Rep3MpcNet::new(network_config)?; - - // init MPC protocol - let protocol = Rep3Protocol::new(net)?; - - let prover = CollaborativePlonk::new(protocol); - - // execute prover in MPC - let proof = prover.prove(pk, witness_share)?; - (proof, public_input) - } - MPCProtocol::SHAMIR => { - let witness_share = - collaborative_circom::parse_witness_share(witness_file)?; - let public_input = witness_share.public_inputs.clone(); - - // connect to network - let net = ShamirMpcNet::new(network_config)?; - - // init MPC protocol - let protocol = ShamirProtocol::new(t, net)?; - - let prover = CollaborativePlonk::new(protocol); - - // execute prover in MPC - let proof = prover.prove(pk, witness_share)?; - (proof, public_input) - } - }; - - // write result to output file - if let Some(out) = out { - let out_file = BufWriter::new( - std::fs::File::create(&out).context("while creating output file")?, - ); - - serde_json::to_writer(out_file, &proof) - .context("while serializing proof to JSON file")?; - tracing::info!("Wrote proof to file {}", out.display()); - } - public_input + MPCProtocol::SHAMIR => { + let witness_share = collaborative_circom::parse_witness_share(witness_file)?; + let public_input = witness_share.public_inputs.clone(); + + // connect to network + let net = ShamirMpcNet::new(config.network)?; + + // init MPC protocol + let protocol = ShamirProtocol::new(t, net)?; + + let mut prover = CollaborativeGroth16::new(protocol); + + // execute prover in MPC + let proof = prover.prove(&zkey, witness_share)?; + (proof, public_input) } }; - // write public input to output file - if let Some(public_input_filename) = public_input_filename { - let public_input_as_strings = public_input - .iter() - .skip(1) // we skip the constant 1 at position 0 - .map(|f| { - if f.is_zero() { - "0".to_string() - } else { - f.to_string() - } - }) - .collect::>(); - let public_input_file = BufWriter::new( - std::fs::File::create(&public_input_filename) - .context("while creating public input file")?, - ); - serde_json::to_writer(public_input_file, &public_input_as_strings) - .context("while writing out public inputs to JSON file")?; - tracing::info!( - "Wrote public inputs to file {}", - public_input_filename.display() + // write result to output file + if let Some(out) = out { + let out_file = BufWriter::new( + std::fs::File::create(&out).context("while creating output file")?, ); + + serde_json::to_writer(out_file, &proof) + .context("while serializing proof to JSON file")?; + tracing::info!("Wrote proof to file {}", out.display()); } - tracing::info!("Proof generation finished successfully") + public_input } - Commands::Verify { - proofsystem, - config: _, - proof, - curve: _, - vk, - public_input, - } => { - file_utils::check_file_exists(&proof)?; - file_utils::check_file_exists(&vk)?; - file_utils::check_file_exists(&public_input)?; - - // parse Circom proof file - let proof_file = - BufReader::new(File::open(&proof).context("while opening proof file")?); - - // parse Circom verification key file - let vk_file = - BufReader::new(File::open(&vk).context("while opening verification key file")?); - - // parse public inputs - let public_inputs_file = BufReader::new( - File::open(&public_input).context("while opening public inputs file")?, - ); - let public_inputs_as_strings: Vec = serde_json::from_reader(public_inputs_file) - .context("while parsing public inputs, expect them to be array of stringified field elements")?; - // skip 1 atm - let public_inputs = public_inputs_as_strings - .into_iter() - .map(|s| { - s.parse::() - .map_err(|_| eyre!("could not parse as field element: {}", s)) - }) - .collect::, _>>() - .context("while converting public input strings to field elements")?; - - // verify proof - let res = match proofsystem { - ProofSystem::Groth16 => { - let proof: Groth16Proof

= serde_json::from_reader(proof_file) - .context("while deserializing proof from file")?; - - let vk: Groth16JsonVerificationKey

= serde_json::from_reader(vk_file) - .context("while deserializing verification key from file")?; - - Groth16::

::verify(&vk, &proof, &public_inputs) - .context("while verifying proof")? + ProofSystem::Plonk => { + let pk = PlonkZKey::

::from_reader(zkey_file).unwrap(); + + let (proof, public_input) = match protocol { + MPCProtocol::REP3 => { + if t != 1 { + return Err(eyre!("REP3 only allows the threshold to be 1")); + } + + let witness_share = collaborative_circom::parse_witness_share(witness_file)?; + let public_input = witness_share.public_inputs.clone(); + // connect to network + let net = Rep3MpcNet::new(config.network)?; + + // init MPC protocol + let protocol = Rep3Protocol::new(net)?; + + let prover = CollaborativePlonk::new(protocol); + + // execute prover in MPC + let proof = prover.prove(pk, witness_share)?; + (proof, public_input) } - ProofSystem::Plonk => { - let proof: PlonkProof

= serde_json::from_reader(proof_file) - .context("while deserializing proof from file")?; + MPCProtocol::SHAMIR => { + let witness_share = collaborative_circom::parse_witness_share(witness_file)?; + let public_input = witness_share.public_inputs.clone(); + + // connect to network + let net = ShamirMpcNet::new(config.network)?; + + // init MPC protocol + let protocol = ShamirProtocol::new(t, net)?; - let vk: PlonkJsonVerificationKey

= serde_json::from_reader(vk_file) - .context("while deserializing verification key from file")?; + let prover = CollaborativePlonk::new(protocol); - Plonk::

::verify(&vk, &proof, &public_inputs) - .context("while verifying proof")? + // execute prover in MPC + let proof = prover.prove(pk, witness_share)?; + (proof, public_input) } }; - if res { - tracing::info!("Proof verified successfully"); - } else { - tracing::error!("Proof verification failed"); - return Ok(ExitCode::FAILURE); + // write result to output file + if let Some(out) = out { + let out_file = BufWriter::new( + std::fs::File::create(&out).context("while creating output file")?, + ); + + serde_json::to_writer(out_file, &proof) + .context("while serializing proof to JSON file")?; + tracing::info!("Wrote proof to file {}", out.display()); } + public_input } - } + }; + // write public input to output file + if let Some(public_input_filename) = public_input_filename { + let public_input_as_strings = public_input + .iter() + .skip(1) // we skip the constant 1 at position 0 + .map(|f| { + if f.is_zero() { + "0".to_string() + } else { + f.to_string() + } + }) + .collect::>(); + let public_input_file = BufWriter::new( + std::fs::File::create(&public_input_filename) + .context("while creating public input file")?, + ); + serde_json::to_writer(public_input_file, &public_input_as_strings) + .context("while writing out public inputs to JSON file")?; + tracing::info!( + "Wrote public inputs to file {}", + public_input_filename.display() + ); + } + tracing::info!("Proof generation finished successfully"); Ok(ExitCode::SUCCESS) } +fn run_verify( + config: VerifyConfig, +) -> color_eyre::Result +where + P::ScalarField: FFTPostProcessing + CircomArkworksPrimeFieldBridge, + P::BaseField: CircomArkworksPrimeFieldBridge, +{ + let proofsystem = config.proof_system; + let proof = config.proof; + let vk = config.vk; + let public_input = config.public_input; + + file_utils::check_file_exists(&proof)?; + file_utils::check_file_exists(&vk)?; + file_utils::check_file_exists(&public_input)?; + + // parse Circom proof file + let proof_file = BufReader::new(File::open(&proof).context("while opening proof file")?); + + // parse Circom verification key file + let vk_file = BufReader::new(File::open(&vk).context("while opening verification key file")?); + + // parse public inputs + let public_inputs_file = + BufReader::new(File::open(&public_input).context("while opening public inputs file")?); + let public_inputs_as_strings: Vec = serde_json::from_reader(public_inputs_file) + .context( + "while parsing public inputs, expect them to be array of stringified field elements", + )?; + // skip 1 atm + let public_inputs = public_inputs_as_strings + .into_iter() + .map(|s| { + s.parse::() + .map_err(|_| eyre!("could not parse as field element: {}", s)) + }) + .collect::, _>>() + .context("while converting public input strings to field elements")?; + + // verify proof + let res = match proofsystem { + ProofSystem::Groth16 => { + let proof: Groth16Proof

= serde_json::from_reader(proof_file) + .context("while deserializing proof from file")?; + + let vk: Groth16JsonVerificationKey

= serde_json::from_reader(vk_file) + .context("while deserializing verification key from file")?; + + Groth16::

::verify(&vk, &proof, &public_inputs).context("while verifying proof")? + } + ProofSystem::Plonk => { + let proof: PlonkProof

= serde_json::from_reader(proof_file) + .context("while deserializing proof from file")?; + + let vk: PlonkJsonVerificationKey

= serde_json::from_reader(vk_file) + .context("while deserializing verification key from file")?; + + Plonk::

::verify(&vk, &proof, &public_inputs).context("while verifying proof")? + } + }; + + if res { + tracing::info!("Proof verified successfully"); + Ok(ExitCode::SUCCESS) + } else { + tracing::error!("Proof verification failed"); + Ok(ExitCode::FAILURE) + } +} + fn parse_field(val: &serde_json::Value) -> color_eyre::Result where F: std::str::FromStr + PrimeField, diff --git a/collaborative-circom/src/lib.rs b/collaborative-circom/src/lib.rs index f635db23..db0ec429 100644 --- a/collaborative-circom/src/lib.rs +++ b/collaborative-circom/src/lib.rs @@ -9,11 +9,11 @@ use circom_types::{ groth16::{Groth16Proof, ZKey}, traits::{CircomArkworksPairingBridge, CircomArkworksPrimeFieldBridge}, }; -use clap::ValueEnum; +use clap::{Args, ValueEnum}; use collaborative_groth16::groth16::{CollaborativeGroth16, SharedInput, SharedWitness}; -use color_eyre::eyre::{Context, ContextCompat}; +use color_eyre::eyre::Context; use figment::{ - providers::{Env, Format, Toml}, + providers::{Env, Format, Serialized, Toml}, Figment, }; use mpc_core::{ @@ -21,13 +21,13 @@ use mpc_core::{ traits::{FFTPostProcessing, PrimeFieldMpcProtocol}, }; use mpc_net::config::NetworkConfig; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; /// A module for file utility functions. pub mod file_utils; /// An enum representing the ZK proof system to use. -#[derive(Clone, ValueEnum)] +#[derive(Debug, Clone, ValueEnum, Serialize, Deserialize)] #[clap(rename_all = "lower")] pub enum ProofSystem { /// The Groth16 proof system. @@ -46,7 +46,7 @@ impl std::fmt::Display for ProofSystem { } /// An enum representing the MPC protocol to use. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum MPCCurve { /// The BN254 curve (called BN128 in circom). BN254, @@ -77,7 +77,7 @@ impl std::fmt::Display for MPCCurve { } /// An enum representing the MPC protocol to use. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, ValueEnum)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, ValueEnum)] #[clap(rename_all = "UPPER")] pub enum MPCProtocol { /// A protocol based on the Replicated Secret Sharing Scheme for 3 parties. @@ -97,31 +97,395 @@ impl std::fmt::Display for MPCProtocol { } } -/// Collaborative-circom configuration -#[derive(Debug, Deserialize, Default)] -pub struct Config { - /// Mpc curve to be sued +/// Cli arguments for `split_witness` +#[derive(Debug, Default, Serialize, Args)] +pub struct SplitWitnessCli { + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the input witness file generated by Circom + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub witness: Option, + /// The path to the r1cs file, generated by Circom compiler + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub r1cs: Option, + /// The MPC protocol to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub protocol: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub curve: Option, + /// The path to the (existing) output directory + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub out_dir: Option, + /// The threshold of tolerated colluding parties + #[arg(short, long, default_value_t = 1)] + pub threshold: usize, + /// The number of parties + #[arg(short, long, default_value_t = 3)] + pub num_parties: usize, +} + +/// Config for `split_witness` +#[derive(Debug, Deserialize)] +pub struct SplitWitnessConfig { + /// The path to the input witness file generated by Circom + pub witness: PathBuf, + /// The path to the r1cs file, generated by Circom compiler + pub r1cs: PathBuf, + /// The MPC protocol to be used + pub protocol: MPCProtocol, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The path to the (existing) output directory + pub out_dir: PathBuf, + /// The threshold of tolerated colluding parties + pub threshold: usize, + /// The number of parties + pub num_parties: usize, +} + +/// Cli arguments for `split_input` +#[derive(Debug, Default, Clone, Serialize, Args)] +pub struct SplitInputCli { + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the input JSON file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub input: Option, + /// The path to the circuit file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub circuit: Option, + /// The path to Circom library files + #[arg(long)] + pub link_library: Vec, + /// The MPC protocol to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub protocol: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub curve: Option, + /// The path to the (existing) output directory + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub out_dir: Option, +} + +/// Config for `split_input` +#[derive(Debug, Clone, Deserialize)] +pub struct SplitInputConfig { + /// The path to the input JSON file + pub input: PathBuf, + /// The path to the circuit file + pub circuit: String, + /// The path to Circom library files + pub link_library: Vec, + /// The MPC protocol to be used + pub protocol: MPCProtocol, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The path to the (existing) output directory + pub out_dir: PathBuf, + /// MPC compiler config + #[serde(default)] + pub compiler: CompilerConfig, +} + +/// Cli arguments for `merge_input_shares` +#[derive(Debug, Default, Serialize, Args)] +pub struct MergeInputSharesCli { + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the input JSON file + #[arg(long)] + pub inputs: Vec, + /// The MPC protocol to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub protocol: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] pub curve: Option, - /// Mpc protocl to bu sued + /// The output file where the merged input share is written to + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub out: Option, +} + +/// Config for `merge_input_shares` +#[derive(Debug, Deserialize)] +pub struct MergeInputSharesConfig { + /// The path to the input JSON file + pub inputs: Vec, + /// The MPC protocol to be used + pub protocol: MPCProtocol, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The output file where the merged input share is written to + pub out: PathBuf, +} + +/// Cli arguments for `generate_witness` +#[derive(Debug, Default, Serialize, Args)] +pub struct GenerateWitnessCli { + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the input share file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub input: Option, + /// The path to the circuit file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub circuit: Option, + /// The path to Circom library files + #[arg(long)] + pub link_library: Vec, + /// The MPC protocol to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] pub protocol: Option, - /// Mpc-vm config - pub vm: Option, - /// Mpc-compiler config - pub compiler: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub curve: Option, + /// The output file where the final witness share is written to + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub out: Option, +} + +/// Config for `generate_witness` +#[derive(Debug, Deserialize)] +pub struct GenerateWitnessConfig { + /// The path to the input share file + pub input: PathBuf, + /// The path to the circuit file + pub circuit: String, + /// The path to Circom library files + pub link_library: Vec, + /// The MPC protocol to be used + pub protocol: MPCProtocol, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The output file where the final witness share is written to + pub out: PathBuf, + /// MPC compiler config + #[serde(default)] + pub compiler: CompilerConfig, + /// MPC VM config + #[serde(default)] + pub vm: VMConfig, /// Network config - pub network: Option, + pub network: NetworkConfig, } -impl Config { - /// Create a new config with given path - pub fn new(path: &str) -> color_eyre::Result { - Ok(Figment::new() - .merge(Toml::file(path)) - .merge(Env::prefixed("COCIRCOM_")) - .extract()?) - } +/// Cli arguments for `transalte_witness` +#[derive(Debug, Serialize, Args)] +pub struct TranslateWitnessCli { + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the witness share file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub witness: Option, + /// The MPC protocol that was used for the witness generation + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub src_protocol: Option, + /// The MPC protocol to be used for the proof generation + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub target_protocol: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub curve: Option, + /// The output file where the final witness share is written to + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub out: Option, } +/// Config for `transalte_witness` +#[derive(Debug, Deserialize)] +pub struct TranslateWitnessConfig { + /// The path to the witness share file + pub witness: PathBuf, + /// The MPC protocol that was used for the witness generation + pub src_protocol: MPCProtocol, + /// The MPC protocol to be used for the proof generation + pub target_protocol: MPCProtocol, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The output file where the final witness share is written to + pub out: PathBuf, + /// Network config + pub network: NetworkConfig, +} + +/// Cli arguments for `generate_proof` +#[derive(Debug, Serialize, Args)] +pub struct GenerateProofCli { + /// The proof system to be used + #[arg(value_enum)] + pub proof_system: ProofSystem, + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the witness share file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub witness: Option, + /// The path to the proving key (.zkey) file, generated by snarkjs setup phase + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub zkey: Option, + /// The MPC protocol to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub protocol: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub curve: Option, + /// The output file where the final proof is written to. If not passed, this party will not write the proof to a file. + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub out: Option, + /// The output JSON file where the public inputs are written to. If not passed, this party will not write the public inputs to a file. + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub public_input: Option, + /// The threshold of tolerated colluding parties + #[arg(short, long, default_value_t = 1)] + pub threshold: usize, +} + +/// Config for `generate_proof` +#[derive(Debug, Deserialize)] +pub struct GenerateProofConfig { + /// The proof system to be used + pub proof_system: ProofSystem, + /// The path to the witness share file + pub witness: PathBuf, + /// The path to the proving key (.zkey) file, generated by snarkjs setup phase + pub zkey: PathBuf, + /// The MPC protocol to be used + pub protocol: MPCProtocol, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The output file where the final proof is written to. If not passed, this party will not write the proof to a file. + pub out: Option, + /// The output JSON file where the public inputs are written to. If not passed, this party will not write the public inputs to a file. + pub public_input: Option, + /// The threshold of tolerated colluding parties + pub threshold: usize, + /// Network config + pub network: NetworkConfig, +} + +/// Cli arguments for `verify` +#[derive(Debug, Serialize, Args)] +pub struct VerifyCli { + /// The proof system to be used + #[arg(value_enum)] + pub proof_system: ProofSystem, + /// The path to the config file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub config: Option, + /// The path to the proof file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub proof: Option, + /// The pairing friendly curve to be used + #[arg(long, value_enum)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub curve: Option, + /// The path to the verification key file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub vk: Option, + /// The path to the public input JSON file + #[arg(long)] + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub public_input: Option, +} + +/// Config for `verify` +#[derive(Debug, Deserialize)] +pub struct VerifyConfig { + /// The proof system to be used + pub proof_system: ProofSystem, + /// The path to the proof file + pub proof: PathBuf, + /// The pairing friendly curve to be used + pub curve: MPCCurve, + /// The path to the verification key file + pub vk: PathBuf, + /// The path to the public input JSON file + pub public_input: PathBuf, +} + +/// Prefix for config env variables +pub const CONFIG_ENV_PREFIX: &str = "COCIRCOM_"; + +/// Error type for config parsing and merging +#[derive(thiserror::Error, Debug)] +#[error(transparent)] +pub struct ConfigError(#[from] figment::error::Error); + +macro_rules! impl_config { + ($cli: ty, $config: ty) => { + impl $config { + /// Parse config from file, env, cli + pub fn parse(cli: $cli) -> Result { + if let Some(path) = &cli.config { + Ok(Figment::new() + .merge(Toml::file(path)) + .merge(Env::prefixed(CONFIG_ENV_PREFIX)) + .merge(Serialized::defaults(cli)) + .extract()?) + } else { + Ok(Figment::new() + .merge(Env::prefixed(CONFIG_ENV_PREFIX)) + .merge(Serialized::defaults(cli)) + .extract()?) + } + } + } + }; +} + +impl_config!(SplitInputCli, SplitInputConfig); +impl_config!(SplitWitnessCli, SplitWitnessConfig); +impl_config!(MergeInputSharesCli, MergeInputSharesConfig); +impl_config!(GenerateWitnessCli, GenerateWitnessConfig); +impl_config!(TranslateWitnessCli, TranslateWitnessConfig); +impl_config!(GenerateProofCli, GenerateProofConfig); +impl_config!(VerifyCli, VerifyConfig); + /// Try to parse a [SharedWitness] from a [Read]er. pub fn parse_witness_share>( reader: R, @@ -146,17 +510,13 @@ pub fn generate_witness_rep3( circuit: String, link_library: Vec, input_share: SharedInput, P>, - config: Config, + config: GenerateWitnessConfig, ) -> color_eyre::Result, P>> { let circuit_path = PathBuf::from(&circuit); file_utils::check_file_exists(&circuit_path)?; - let network_config = config.network.context("expected a network config")?; - let compiler_config = config.compiler.context("expected a network config")?; - let vm_config = config.vm.context("expected a vm config")?; - // parse circuit file & put through our compiler - let mut builder = CompilerBuilder::

::new(compiler_config, circuit); + let mut builder = CompilerBuilder::

::new(config.compiler, circuit); for lib in link_library { builder = builder.link_library(lib); } @@ -166,11 +526,11 @@ pub fn generate_witness_rep3( .context("while parsing circuit file")?; // connect to network - let net = Rep3MpcNet::new(network_config).context("while connecting to network")?; + let net = Rep3MpcNet::new(config.network).context("while connecting to network")?; // init MPC protocol let rep3_vm = parsed_circom_circuit - .to_rep3_vm_with_network(net, vm_config) + .to_rep3_vm_with_network(net, config.vm) .context("while constructing MPC VM")?; // execute witness generation in MPC