remove poseidon from circuit

This commit is contained in:
0xturboblitz
2023-12-30 00:08:20 +01:00
parent 37b0fff72a
commit 4994b41add
4 changed files with 40 additions and 18 deletions

View File

@@ -20,6 +20,12 @@ yarn
./scripts/build_circuit.sh
```
#### Build only to use the app, not for running tests (dev only, not secure)
```bash
./scripts/build_circuit.sh app-only
```
#### Run tests
```bash

View File

@@ -33,24 +33,16 @@ template ProofOfPassport(n, k) {
// we take nullifier = signature[0, 1] which it 64 + 64 bits long, so chance of collision is 2^128
signal output nullifier <== signature[0] * 2**64 + signature[1];
// Calculate the Poseidon hash of public public key and outputs it
// This can be used to verify the public key is correct in contract without requiring the actual key
// We are converting pub_key (modulus) in to 9 chunks of 242 bits, assuming original n, k are 121 and 17.
// This is because Posiedon circuit only support array of 16 elements.
// Otherwise we would have to output the ceil(256/31) = 9 field elements of the public key
var k2_chunked_size = k >> 1;
if(k % 2 == 1) {
k2_chunked_size += 1;
}
signal pubkey_hash_input[k2_chunked_size];
for(var i = 0; i < k2_chunked_size; i++) {
if(i==k2_chunked_size-1 && k2_chunked_size % 2 == 1) {
pubkey_hash_input[i] <== pubkey[2*i];
// we don't do Poseidon hash cuz it makes arkworks crash for obscure reasons
// we output the pubkey as 11 field elements. 9 is doable also cuz ceil(254/31) = 9
signal output pubkey_packed[11];
for (var i = 0; i < 11; i++) {
if (i < 10) {
pubkey_packed[i] <== pubkey[3*i] * 64 * 64 + pubkey[3*i + 1] * 64 + pubkey[3*i + 2];
} else {
pubkey_hash_input[i] <== pubkey[2*i] + (1<<n) * pubkey[2*i+1];
pubkey_packed[i] <== pubkey[3*i] * 64 * 64;
}
}
signal output pubkey_hash <== Poseidon(k2_chunked_size)(pubkey_hash_input);
}
component main { public [ address ] } = ProofOfPassport(64, 32);

View File

@@ -1,3 +1,12 @@
#!/bin/bash
# Check if the first argument is "app-only"
if [ "$1" == "app-only" ]; then
echo "Building only for the app"
APP_ONLY=1
else
APP_ONLY=0
fi
mkdir -p build
cd build
@@ -13,6 +22,19 @@ cd ..
echo "compiling circuit"
circom circuits/proof_of_passport.circom --r1cs --sym --wasm --output build
mkdir -p ../app/ark-circom-passport/passport/
cp build/proof_of_passport.r1cs ../app/ark-circom-passport/passport/
cp build/proof_of_passport_js/proof_of_passport.wasm ../app/ark-circom-passport/passport/
echo "copied proof_of_passport.r1cs and proof_of_passport.wasm to ark-circom-passport"
echo "file sizes:"
echo "Size of proof_of_passport.r1cs: $(wc -c <../app/ark-circom-passport/passport/proof_of_passport.r1cs) bytes"
echo "Size of proof_of_passport.wasm: $(wc -c <../app/ark-circom-passport/passport/proof_of_passport.wasm) bytes"
# If APP_ONLY is 1, exit the script here
if [ $APP_ONLY -eq 1 ]; then
exit 0
fi
echo "building zkey"
yarn snarkjs groth16 setup build/proof_of_passport.r1cs build/powersOfTau28_hez_final_20.ptau build/proof_of_passport.zkey
@@ -20,4 +42,6 @@ echo "building vkey"
echo "test random" | yarn snarkjs zkey contribute build/proof_of_passport.zkey build/proof_of_passport_final.zkey
yarn snarkjs zkey export verificationkey build/proof_of_passport_final.zkey build/verification_key.json
yarn snarkjs zkey export solidityverifier build/proof_of_passport_final.zkey build/Verifier.sol
yarn snarkjs zkey export solidityverifier build/proof_of_passport_final.zkey build/Verifier.sol
cp build/Verifier.sol ../contracts/contracts/Verifier.sol
echo "copied Verifier.sol to contracts"

View File

@@ -3,14 +3,14 @@ import chai, { assert, expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { hash, toUnsignedByte, arraysAreEqual, bytesToBigDecimal, formatAndConcatenateDataHashes, formatMrz, splitToWords } from '../../common/src/utils/utils'
import { groth16 } from 'snarkjs'
import { DataHash, PassportData } from '../../common/src/utils/types'
import { DataHash } from '../../common/src/utils/types'
import { getPassportData } from '../../common/src/utils/passportData'
import { attributeToPosition } from '../../common/src/constants/constants'
const fs = require('fs');
chai.use(chaiAsPromised)
console.log("The snarkjs following error logs are normal and expected if the tests pass.")
console.log("The following snarkjs error logs are normal and expected if the tests pass.")
describe('Circuit tests', function () {
this.timeout(0)