Skip to content

Commit

Permalink
Merge pull request #218 from zk-passport/feat/integration-test-for-on…
Browse files Browse the repository at this point in the history
…e-time-verification

Feat/integration test for one time verification
  • Loading branch information
remicolin authored Oct 10, 2024
2 parents df08107 + ac6e8fb commit 8d1cc18
Show file tree
Hide file tree
Showing 20 changed files with 17,112 additions and 16,588 deletions.
14 changes: 7 additions & 7 deletions circuits/scripts/build_dsc_4096_circuits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ build_circuit() {
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/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
yarn snarkjs zkey export solidityverifier build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey build/dsc/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol
sed -i '' "s/Groth16Verifier/Verifier_${CIRCUIT_NAME}/g" build/dsc/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol
cp build/dsc/${CIRCUIT_NAME}/Verifier_${CIRCUIT_NAME}.sol ../contracts/contracts/verifiers/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"

echo "file sizes:"
echo -e "\033[34mSize of ${CIRCUIT_NAME}.r1cs: $(wc -c <build/${CIRCUIT_NAME}.r1cs) bytes\033[0m"
echo -e "\033[34mSize of ${CIRCUIT_NAME}.wasm: $(wc -c <build/${CIRCUIT_NAME}_js/${CIRCUIT_NAME}.wasm) bytes\033[0m"
echo -e "\033[34mSize of ${CIRCUIT_NAME}_final.zkey: $(wc -c <build/${CIRCUIT_NAME}_final.zkey) bytes\033[0m"
echo -e "\033[34mSize of ${CIRCUIT_NAME}.r1cs: $(wc -c <build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}.r1cs) bytes\033[0m"
echo -e "\033[34mSize of ${CIRCUIT_NAME}.wasm: $(wc -c <build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_js/${CIRCUIT_NAME}.wasm) bytes\033[0m"
echo -e "\033[34mSize of ${CIRCUIT_NAME}_final.zkey: $(wc -c <build/dsc/${CIRCUIT_NAME}/${CIRCUIT_NAME}_final.zkey) bytes\033[0m"
}

# Define circuits and their deployment flags
# name:deploy_flag
CIRCUITS=(
"dsc_rsapss_65537_sha256_4096:false"
"dsc_rsapss_65537_sha256_4096:true"
"dsc_rsa_65537_sha256_4096:true"
"dsc_rsa_65537_sha1_4096:true"
)
Expand Down
4 changes: 2 additions & 2 deletions circuits/scripts/build_prove_circuits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ build_circuit() {
# 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:false"
"prove_rsapss_65537_sha256:prove:20:false"
"prove_rsa_65537_sha1:prove:20:true"
"prove_rsapss_65537_sha256:prove:20:true"
"prove_ecdsa_secp256r1_sha256:prove:22:false"
"prove_ecdsa_secp256r1_sha1:prove:22:false"
)
Expand Down
4 changes: 4 additions & 0 deletions circuits/scripts/download_circuits_from_aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ download_and_compile_circuit() {
--O1 --wasm \
--output build/fromAWS

echo "Exporting vkey: $circuit_name"
snarkjs zkey export verificationkey build/fromAWS/${circuit_name}.zkey build/fromAWS/${circuit_name}_vkey.json

if [ $? -eq 0 ]; then
echo "Successfully compiled $circuit_name"
# Keep only the wasm file and remove other generated files
Expand All @@ -52,6 +55,7 @@ download_and_compile_circuit() {
else
echo "Failed to compile $circuit_name"
fi

else
echo "Failed to download $circuit_name"
fi
Expand Down
9 changes: 7 additions & 2 deletions circuits/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -881,9 +881,9 @@ circom_tester@^0.0.19:
tmp-promise "^3.0.3"
util "^0.12.4"

"circom_tester@github:Atomic-Buy/circom_tester#main":
"circom_tester@github:remicolin/circom_tester#main":
version "0.0.20"
resolved "https://codeload.github.com/Atomic-Buy/circom_tester/tar.gz/cf7d7a60bddcffc2388f0acaf44f38bb00d6f2f6"
resolved "https://codeload.github.com/remicolin/circom_tester/tar.gz/81e963cea5fb91ca31126058c8fdc9aafc9d695d"
dependencies:
chai "^4.3.6"
ffjavascript "^0.2.60"
Expand Down Expand Up @@ -944,6 +944,11 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

crypto@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==

[email protected]:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
Expand Down
3 changes: 3 additions & 0 deletions common/src/constants/contractConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ export const PROVE_RSA_SCOPE_INDEX = 50;

export const DSC_BLINDED_DSC_COMMITMENT_INDEX = 0;

// Enum in VerifiersManager.sol
export const VERIFICATION_TYPE_ENUM_PROVE = 0;
export const VERIFICATION_TYPE_ENUM_DSC = 1;
38 changes: 36 additions & 2 deletions common/src/utils/smtTree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { poseidon9, poseidon3, poseidon2, poseidon6, poseidon13 } from "poseidon-lite"
import { stringToAsciiBigIntArray } from "./utils";
import { ChildNodes,SMT } from "@ashpect/smt"
import { hash, stringToAsciiBigIntArray } from "./utils";
import { ChildNodes,SMT } from "@ashpect/smt";
import fs from 'fs';
import path from 'path';

// SMT trees for 3 levels :
// 1. Passport tree : level 3 (Absolute Match)
Expand Down Expand Up @@ -47,6 +49,38 @@ export function buildSMT(field :any[], treetype:string): [number, number, SMT]{
return [count, performance.now() - startTime, tree]
}

export function exportSMTToJsonFile(count: number, time: number, smt: SMT, outputPath?: string) {
const serializedSMT = smt.export();
const data = {
count: count,
time: time,
smt: serializedSMT
};
const jsonString = JSON.stringify(data, null, 2);
const defaultPath = path.join(process.cwd(), 'smt.json');
const finalPath = outputPath ? path.resolve(process.cwd(), outputPath) : defaultPath;

fs.writeFileSync(finalPath, jsonString, 'utf8');
}

export function importSMTFromJsonFile(filePath?: string): SMT | null {
try {
const jsonString = fs.readFileSync(path.resolve(process.cwd(), filePath), 'utf8');

const data = JSON.parse(jsonString);

const hash2 = (childNodes: ChildNodes) => (childNodes.length === 2 ? poseidon2(childNodes) : poseidon3(childNodes));
const smt = new SMT(hash2, true);
smt.import(data.smt);

console.log('Successfully imported SMT from JSON file');
return smt;
} catch (error) {
console.error('Failed to import SMT from JSON file:', error);
return null;
}
}

function processPassport(passno : string, index: number): bigint {
if (passno.length > 9) {
console.log('passport length is greater than 9:', index, passno)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ ignition/parameters.json
ignition/deployments

#Local verifier
contracts/verifiers/local/*
contracts/verifiers/local/*
20 changes: 20 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,23 @@ npx hardhat node
npx hardhat run scripts/mint.ts
npx hardhat ignition deploy ignition/modules/Deploy_All.ts --network <network>
```

## When you run test
If you want to test in your local environment

```shell
cd ../circuits
./scripts/build_prove_circuits.sh
cd ../contracts
yarn run test:local
```

If you want to test in production environment
```shell
cd ../circuits
./scripts/download_circuits_from_aws.sh
cd ../contracts
yarn run test:prod
```

If you want to generate your own proof or when you update circuits, pls delete json files in test/integrationTest
1 change: 1 addition & 0 deletions contracts/contracts/OneTimeSBT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ contract OneTimeSBT is ERC721Enumerable {
using Strings for uint256;
using Base64 for *;

//TODO: add ownable and functions to update verifiersmanager
IVerifiersManager public verifiersManager;
Formatter public formatter;

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions contracts/contracts/verifiers/VerifiersManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ contract VerifiersManager is IVerifiersManager, Ownable {
Dsc
}

// TODO: Key of these mappings are keccak256 hash of parameters in each signature algorithm
mapping(uint256 => address) public prove_verifiers;
mapping(uint256 => address) public dsc_verifiers;

Expand Down Expand Up @@ -46,6 +47,7 @@ contract VerifiersManager is IVerifiersManager, Ownable {
return result;
}

// TODO: add batch update function
function updateVerifier(
VerificationType v_type,
uint256 verifier_id,
Expand Down
Loading

0 comments on commit 8d1cc18

Please sign in to comment.