Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/build scripts for circuit #211

Merged
merged 12 commits into from
Oct 7, 2024
30 changes: 16 additions & 14 deletions circuits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ yarn test

This will run tests with sample data generated on the fly.


## OpenPassport Prove circuit

OpenPassport Prove is the main circuit of the project.
It is used for these 3 different `circuit modes`:

- prove offChain
- prove onChain
- register

Learn more on these 3 use cases on [OpenPassport documentation.](https://docs.openpassport.app/docs/use-openpassport/quickstart)

The circuit achieves the following actions:

- verify the signature of the passport and the integrity of the datagroups
- disclose attributes
- verify that user's name is not part of the OFAC list
Expand All @@ -93,23 +95,21 @@ If this "everything circuit" is executing all those actions each time, we want a

In order to achieve that we will input a bitmap `selector_mode[2]` that will ensure that the circuit can only disclose the attributes related to the `circuit mode` selected.

| Circuit Mode | selector_mode[0] | selector_mode[1] |
| --- | --- | --- |
| prove offChain | 1 | 1 |
| prove onChain | 1 | 0 |
| register | 0 | 0 |
| Circuit Mode | selector_mode[0] | selector_mode[1] |
| -------------- | ---------------- | ---------------- |
| prove offChain | 1 | 1 |
| prove onChain | 1 | 0 |
| register | 0 | 0 |

Using the value [0,1] for `selector_mode` will fail proof generation.


Here are the attributes disclosed according to the `circuit_mode`:

| Circuit Mode | Attributes Disclosed |
| --- | --- |
| prove offChain | packedReveal-dg1, older than, OFAC, countryIsNotInList, pubKey |
| prove onChain | packedReveal-dg1, older than, OFAC, countryIsNotInList, blinded DSC commitment |
| register | blinded DSC commitment, commitment |

| Circuit Mode | Attributes Disclosed |
| -------------- | ------------------------------------------------------------------------------ |
| prove offChain | packedReveal-dg1, older than, OFAC, countryIsNotInList, pubKey |
| prove onChain | packedReveal-dg1, older than, OFAC, countryIsNotInList, blinded DSC commitment |
| register | blinded DSC commitment, commitment |

## Certificate Chain verification

Expand All @@ -119,7 +119,9 @@ Both DSC and CSCA lists are published on online registry of the ICAO, however ma
In order to maximize passport readability we need to verify the full certificate chain.

### On chain

To avoid huge proving time and (too) heavy zkeys, the signature of the passport data is verified on the mobile (the passport data never leaves the device) and the certificate chain verification is done on a remote modal server. A `blindedDscCommitment` is generated on both sides to link proofs.

### Off chain
In off chain setup users will send their DSC to the verifier along with their passport proof. The pubKey will be revealed as an output of the proof.

In off chain setup users will send their DSC to the verifier along with their passport proof. The pubKey will be revealed as an output of the proof.
53 changes: 53 additions & 0 deletions circuits/scripts/build_disclose_circuit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

source "scripts/download_ptau.sh"

build_circuit() {
local CIRCUIT_NAME=$1
local CIRCUIT_TYPE=$2
local START_TIME=$(date +%s)

echo "compiling circuit: $CIRCUIT_NAME"
mkdir -p build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/
circom circuits/${CIRCUIT_TYPE}/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits/ --r1cs --O1 --wasm -c --output build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/

echo "building zkey"
yarn snarkjs groth16 setup build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_20.ptau build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey

echo "building vkey"
yarn snarkjs zkey contribute build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey -e="random text"
yarn snarkjs zkey export verificationkey build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}_vkey.json

yarn snarkjs zkey export solidityverifier build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/${CIRCUIT_TYPE}/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/${CIRCUIT_TYPE}/Verifier_${CIRCUIT_NAME}.sol
mkdir -p ../contracts/contracts/verifiers/local/${CIRCUIT_TYPE}/
cp build/${CIRCUIT_TYPE}/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/verifiers/local/${CIRCUIT_TYPE}/Verifier_${CIRCUIT_NAME}.sol
echo "copied Verifier_${CIRCUIT_NAME}.sol to contracts"

echo "Build of $CIRCUIT_NAME completed in $(($(date +%s) - START_TIME)) seconds"
echo "Size of ${CIRCUIT_NAME}.r1cs: $(wc -c <build/${CIRCUIT_NAME}.r1cs) bytes"
echo "Size of ${CIRCUIT_NAME}.wasm: $(wc -c <build/${CIRCUIT_NAME}_js/${CIRCUIT_NAME}.wasm) bytes"
echo "Size of ${CIRCUIT_NAME}_final.zkey: $(wc -c <build/${CIRCUIT_NAME}_final.zkey) bytes"
}

# Define circuits and their types
# name:folder:build_flag
# set build_flag to false if you want to skip the build
CIRCUITS=(
"vc_and_disclose:disclose:20:true"
"disclose:disclose:20:false"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only want to build vc_and_disclose circuit here, others don't have "component main" statement

"proveCountryIsNotInList:disclose:20:false"
"verify_commitment:disclose:22:false"
)

TOTAL_START_TIME=$(date +%s)
for circuit in "${CIRCUITS[@]}"; do
IFS=':' read -r CIRCUIT_NAME CIRCUIT_TYPE POWEROFTAU BUILD_FLAG <<< "$circuit"
if [ "$BUILD_FLAG" = "true" ]; then
echo "Debug: Building circuit $CIRCUIT_NAME of type $CIRCUIT_TYPE"
build_circuit "$CIRCUIT_NAME" "$CIRCUIT_TYPE"
else
echo "Skipping build for $CIRCUIT_NAME"
fi
done
echo "Total completed in $(($(date +%s) - TOTAL_START_TIME)) seconds"
22 changes: 12 additions & 10 deletions circuits/scripts/build_dsc_2048_circuits.sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we actually want to delete all instances of dsc circuits with 2048 bits key length (and this script), we are no longer using these anymore

Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ build_circuit() {
local START_TIME=$(date +%s)

echo -e "\033[34mcompiling circuit: $CIRCUIT_NAME\033[0m"
circom circuits/tests/dsc/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build
mkdir -p ./build/dsc/${CIRCUIT_NAME}/
circom circuits/dsc/instances/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build/dsc/${CIRCUIT_NAME}/

echo -e "\033[34mbuilding zkey\033[0m"
yarn snarkjs groth16 setup build/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_20.ptau build/${CIRCUIT_NAME}.zkey
yarn snarkjs groth16 setup build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_22.ptau build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey

if command -v openssl &> /dev/null
then
Expand All @@ -32,12 +33,13 @@ build_circuit() {
fi

echo -e "\033[34mbuilding vkey\033[0m"
echo $RAND_STR | yarn snarkjs zkey contribute build/${CIRCUIT_NAME}.zkey build/${CIRCUIT_NAME}_final.zkey
yarn snarkjs zkey export verificationkey build/${CIRCUIT_NAME}_final.zkey build/${CIRCUIT_NAME}_vkey.json
yarn snarkjs zkey contribute build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey -e="random text"
yarn snarkjs zkey export verificationkey build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_vkey.json

yarn snarkjs zkey export solidityverifier build/${CIRCUIT_NAME}_final.zkey build/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/Verifier_${CIRCUIT_NAME}.sol
cp build/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/Verifier_${CIRCUIT_NAME}.sol
yarn snarkjs zkey export solidityverifier build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/dsc/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/dsc/Verifier_${CIRCUIT_NAME}.sol
mkdir -p ../contracts/contracts/local/dsc/
cp build/dsc/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/local/dsc/Verifier_${CIRCUIT_NAME}.sol
echo -e "\033[34mcopied Verifier_${CIRCUIT_NAME}.sol to contracts\033[0m"

echo -e "\033[32mBuild of $CIRCUIT_NAME completed in $(($(date +%s) - START_TIME)) seconds\033[0m"
Expand All @@ -51,9 +53,9 @@ build_circuit() {
# Define circuits and their deployment flags
# name:deploy_flag
CIRCUITS=(
"dsc_sha256_rsapss_2048:true"
"dsc_sha256_rsa_2048:true"
"dsc_sha1_rsa_2048:true"
"dsc_rsapss_65537_sha256_2048:false"
"dsc_rsa_65537_sha256_2048:true"
"dsc_rsa_65537_sha1_2048:false"
)

for circuit in "${CIRCUITS[@]}"; do
Expand Down
15 changes: 8 additions & 7 deletions circuits/scripts/build_dsc_4096_circuits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ build_circuit() {
local START_TIME=$(date +%s)

echo -e "\033[34mcompiling circuit: $CIRCUIT_NAME\033[0m"
circom circuits/dsc/instances/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build
mkdir -p ./build/dsc/${CIRCUIT_NAME}/
circom circuits/dsc/instances/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build/dsc/${CIRCUIT_NAME}/

echo -e "\033[34mbuilding zkey\033[0m"
NODE_OPTIONS="--max-old-space-size=8192" yarn snarkjs groth16 setup build/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_22.ptau build/${CIRCUIT_NAME}.zkey
NODE_OPTIONS="--max-old-space-size=8192" yarn snarkjs groth16 setup build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_22.ptau build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey

if command -v openssl &> /dev/null
then
Expand All @@ -32,12 +33,12 @@ build_circuit() {
fi

echo -e "\033[34mbuilding vkey\033[0m"
echo $RAND_STR | yarn snarkjs zkey contribute build/${CIRCUIT_NAME}.zkey build/${CIRCUIT_NAME}_final.zkey
yarn snarkjs zkey export verificationkey build/${CIRCUIT_NAME}_final.zkey build/${CIRCUIT_NAME}_vkey.json
echo $RAND_STR | yarn snarkjs zkey contribute build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey
yarn snarkjs zkey export verificationkey build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_vkey.json

yarn snarkjs zkey export solidityverifier build/${CIRCUIT_NAME}_final.zkey build/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/Verifier_${CIRCUIT_NAME}.sol
cp build/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/Verifier_${CIRCUIT_NAME}.sol
yarn snarkjs zkey export solidityverifier build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/dsc/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/dsc/Verifier_${CIRCUIT_NAME}.sol
cp build/dsc/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/dsc/Verifier_${CIRCUIT_NAME}.sol
echo -e "\033[34mcopied Verifier_${CIRCUIT_NAME}.sol to contracts\033[0m"

echo -e "\033[32mBuild of $CIRCUIT_NAME completed in $(($(date +%s) - START_TIME)) seconds\033[0m"
Expand Down
26 changes: 14 additions & 12 deletions circuits/scripts/build_prove_circuits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@ build_circuit() {
local START_TIME=$(date +%s)

echo "compiling circuit: $CIRCUIT_NAME"
circom circuits/${CIRCUIT_TYPE}/instances/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build
mkdir -p build/prove/${CIRCUIT_NAME}/
circom circuits/${CIRCUIT_TYPE}/instances/${CIRCUIT_NAME}.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build/prove/${CIRCUIT_NAME}/

echo "building zkey"
yarn snarkjs groth16 setup build/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_20.ptau build/${CIRCUIT_NAME}.zkey
yarn snarkjs groth16 setup build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}.r1cs build/powersOfTau28_hez_final_20.ptau build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey

echo "building vkey"
yarn snarkjs zkey contribute build/${CIRCUIT_NAME}.zkey build/${CIRCUIT_NAME}_final.zkey -e="random text"
yarn snarkjs zkey export verificationkey build/${CIRCUIT_NAME}_final.zkey build/${CIRCUIT_NAME}_vkey.json
yarn snarkjs zkey contribute build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}.zkey build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey -e="random text"
yarn snarkjs zkey export verificationkey build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}_vkey.json

yarn snarkjs zkey export solidityverifier build/${CIRCUIT_NAME}_final.zkey build/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/Verifier_${CIRCUIT_NAME}.sol
cp build/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/Verifier_${CIRCUIT_NAME}.sol
yarn snarkjs zkey export solidityverifier build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/prove/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/prove/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol
mkdir -p ../contracts/contracts/verifiers/local/${CIRCUIT_TYPE}/
cp build/prove/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/verifiers/local/${CIRCUIT_TYPE}/Verifier_${CIRCUIT_NAME}.sol
echo "copied Verifier_${CIRCUIT_NAME}.sol to contracts"

echo "Build of $CIRCUIT_NAME completed in $(($(date +%s) - START_TIME)) seconds"
echo "Size of ${CIRCUIT_NAME}.r1cs: $(wc -c <build/${CIRCUIT_NAME}.r1cs) bytes"
echo "Size of ${CIRCUIT_NAME}.wasm: $(wc -c <build/${CIRCUIT_NAME}_js/${CIRCUIT_NAME}.wasm) bytes"
echo "Size of ${CIRCUIT_NAME}_final.zkey: $(wc -c <build/${CIRCUIT_NAME}_final.zkey) bytes"
echo "Size of ${CIRCUIT_NAME}.r1cs: $(wc -c <build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}.r1cs) bytes"
echo "Size of ${CIRCUIT_NAME}.wasm: $(wc -c <build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}_js/${CIRCUIT_NAME}.wasm) bytes"
echo "Size of ${CIRCUIT_NAME}_final.zkey: $(wc -c <build/prove/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey) bytes"
}

# Define circuits and their types
# name:folder:build_flag
# set build_flag to false if you want to skip the build
CIRCUITS=(
"prove_rsa_65537_sha256:prove:20:true"
"prove_rsa_65537_sha1:prove:20:true"
"prove_rsapss_65537_sha256:prove:20:true"
"prove_rsa_65537_sha1:prove:20:false"
"prove_rsapss_65537_sha256:prove:20:false"
"prove_ecdsa_secp256r1_sha256:prove:22:false"
"prove_ecdsa_secp256r1_sha1:prove:22:false"
)
Expand Down
5 changes: 4 additions & 1 deletion contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ cache
artifacts
ignition/deployed_addresses.json
ignition/parameters.json
ignition/deployments
ignition/deployments

#Local verifier
contracts/verifiers/local/*
Loading