From ba4e99461f8a35297b76855cd3c0ebc7a131ce62 Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Wed, 2 Oct 2024 15:03:27 -0700 Subject: [PATCH] update smt check to return 0 instead of creating a constraint --- circuits/circuits/ofac/ofac_name.circom | 5 +- circuits/circuits/ofac/ofac_name_dob.circom | 9 +- .../circuits/ofac/ofac_passport_number.circom | 4 +- .../tests/ofac/ofac_name_dob_tester.circom | 3 + .../tests/ofac/ofac_name_tester.circom | 3 + .../ofac/ofac_passport_number_tester.circom | 3 + circuits/circuits/utils/other/smt.circom | 6 +- circuits/tests/ofac/ofac.test.ts | 100 +++++++----------- 8 files changed, 57 insertions(+), 76 deletions(-) create mode 100644 circuits/circuits/tests/ofac/ofac_name_dob_tester.circom create mode 100644 circuits/circuits/tests/ofac/ofac_name_tester.circom create mode 100644 circuits/circuits/tests/ofac/ofac_passport_number_tester.circom diff --git a/circuits/circuits/ofac/ofac_name.circom b/circuits/circuits/ofac/ofac_name.circom index fa936639d..a909abafe 100644 --- a/circuits/circuits/ofac/ofac_name.circom +++ b/circuits/circuits/ofac/ofac_name.circom @@ -13,7 +13,6 @@ template OFAC_NAME() { signal input smt_leaf_value; signal input smt_root; signal input smt_siblings[256]; - signal output proofLevel <== 1; component poseidon_hasher[3]; for (var j = 0; j < 3; j++) { @@ -25,8 +24,6 @@ template OFAC_NAME() { signal name_hash <== Poseidon(3)([poseidon_hasher[0].out, poseidon_hasher[1].out, poseidon_hasher[2].out]); - SMTVerify(256)(name_hash, smt_leaf_value, smt_root, smt_siblings, 0); - + signal output ofacVerification <== SMTVerify(256)(name_hash, smt_leaf_value, smt_root, smt_siblings, 0); } -component main { public [ smt_root ] } = OFAC_NAME(); diff --git a/circuits/circuits/ofac/ofac_name_dob.circom b/circuits/circuits/ofac/ofac_name_dob.circom index 94a295df3..1bd6da769 100644 --- a/circuits/circuits/ofac/ofac_name_dob.circom +++ b/circuits/circuits/ofac/ofac_name_dob.circom @@ -14,9 +14,6 @@ template OFAC_NAME_DOB() { signal input smt_leaf_value; signal input smt_root; signal input smt_siblings[256]; - signal output proofLevel <== 2; - - // Name Hash component poseidon_hasher[3]; for (var j = 0; j < 3; j++) { @@ -36,7 +33,5 @@ template OFAC_NAME_DOB() { // NameDob hash signal name_dob_hash <== Poseidon(2)([pos_dob.out, name_hash]); - SMTVerify(256)(name_dob_hash, smt_leaf_value, smt_root, smt_siblings, 0); -} - -component main { public [ smt_root ] } = OFAC_NAME_DOB(); + signal output ofacVerification <== SMTVerify(256)(name_dob_hash, smt_leaf_value, smt_root, smt_siblings, 0); +} \ No newline at end of file diff --git a/circuits/circuits/ofac/ofac_passport_number.circom b/circuits/circuits/ofac/ofac_passport_number.circom index f0eac42c6..01b409ce6 100644 --- a/circuits/circuits/ofac/ofac_passport_number.circom +++ b/circuits/circuits/ofac/ofac_passport_number.circom @@ -21,7 +21,5 @@ template OFAC_PASSPORT_NUMBER() { for (var i = 0; i < 9; i++) { poseidon_hasher.inputs[i] <== dg1[49 + i]; } - SMTVerify(256)(poseidon_hasher.out, smt_leaf_value, smt_root, smt_siblings, 0); + signal output ofacVerification <== SMTVerify(256)(poseidon_hasher.out, smt_leaf_value, smt_root, smt_siblings, 0); } - -component main { public [ smt_root ] } = OFAC_PASSPORT_NUMBER(); diff --git a/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom b/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom new file mode 100644 index 000000000..ee4d0af15 --- /dev/null +++ b/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom @@ -0,0 +1,3 @@ +pragma circom 2.1.9; +include "../../ofac/ofac_name_dob.circom"; +component main { public [ smt_root ] } = OFAC_NAME_DOB(); diff --git a/circuits/circuits/tests/ofac/ofac_name_tester.circom b/circuits/circuits/tests/ofac/ofac_name_tester.circom new file mode 100644 index 000000000..cc06772b1 --- /dev/null +++ b/circuits/circuits/tests/ofac/ofac_name_tester.circom @@ -0,0 +1,3 @@ +pragma circom 2.1.9; +include "../../ofac/ofac_name.circom"; +component main { public [ smt_root ] } = OFAC_NAME(); diff --git a/circuits/circuits/tests/ofac/ofac_passport_number_tester.circom b/circuits/circuits/tests/ofac/ofac_passport_number_tester.circom new file mode 100644 index 000000000..59807fcc5 --- /dev/null +++ b/circuits/circuits/tests/ofac/ofac_passport_number_tester.circom @@ -0,0 +1,3 @@ +pragma circom 2.1.9; +include "../../ofac/ofac_passport_number.circom"; +component main { public [ smt_root ] } = OFAC_PASSPORT_NUMBER(); diff --git a/circuits/circuits/utils/other/smt.circom b/circuits/circuits/utils/other/smt.circom index d3358508c..4c1723095 100644 --- a/circuits/circuits/utils/other/smt.circom +++ b/circuits/circuits/utils/other/smt.circom @@ -37,12 +37,14 @@ template SMTVerify(nLength) { // Verification signal computedRoot <== BinaryMerkleRoot(nLength)(leafOrZero, depth, path, siblings); - computedRoot === root; + signal computedRootIsValid <== IsEqual()([computedRoot,root]); // check is leaf equals virtual leaf signal virtualLeaf <== Poseidon(3)([virtualValue, 1,1]); signal areLeafAndVirtualLeafEquals <== IsEqual()([virtualLeaf, leaf]); - mode === areLeafAndVirtualLeafEquals; + signal isInclusionOrNonInclusionValid <== IsEqual()([mode,areLeafAndVirtualLeafEquals]); + signal output out <== computedRootIsValid * isInclusionOrNonInclusionValid; + } diff --git a/circuits/tests/ofac/ofac.test.ts b/circuits/tests/ofac/ofac.test.ts index 000522f69..5f6a40eac 100644 --- a/circuits/tests/ofac/ofac.test.ts +++ b/circuits/tests/ofac/ofac.test.ts @@ -39,7 +39,7 @@ describe('OFAC - Passport number match', function () { before(async () => { circuit = await wasm_tester( - path.join(__dirname, '../../circuits/ofac/ofac_passport_number.circom'), + path.join(__dirname, '../../circuits/ofac/../../circuits/tests/ofac/ofac_passport_number_tester.circom'), { include: [ 'node_modules', @@ -72,33 +72,26 @@ describe('OFAC - Passport number match', function () { // Correct siblings and closest leaf : Everything correct as a proof it('should pass without errors, all conditions satisfied', async function () { let w = await circuit.calculateWitness(nonMemSmtInputs); - const proofLevel = await circuit.getOutput(w, ['proofLevel']); - console.log("proofLevel: ", proofLevel.proofLevel); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('1'); }); // Correct siblings but membership proof: Fail at line 43 assertion it('should fail to calculate witness since trying to generate membership proof, level 3', async function () { - try { - await circuit.calculateWitness(memSmtInputs); - // expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } + let w = await circuit.calculateWitness(memSmtInputs); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('0'); }); // Give wrong closest leaf but correct siblings array : Fail of SMT Verification it('should fail to calculate witness due to wrong leaf provided, level 3', async function () { - try { - const wrongInputs = { - ...nonMemSmtInputs, - smt_leaf_value: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(), - }; - await circuit.calculateWitness(wrongInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - expect(error.message).to.include('SMTVerify'); - } + const wrongInputs = { + ...nonMemSmtInputs, + smt_leaf_value: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(), + }; + let w = await circuit.calculateWitness(wrongInputs); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('0'); }); }); @@ -111,7 +104,7 @@ describe('OFAC - Name and DOB match', function () { let nonMemSmtInputs: any; before(async () => { - circuit = await wasm_tester(path.join(__dirname, '../../circuits/ofac/ofac_name_dob.circom'), { + circuit = await wasm_tester(path.join(__dirname, '../../circuits/tests/ofac/ofac_name_dob_tester.circom'), { include: [ 'node_modules', './node_modules/@zk-kit/binary-merkle-root.circom/src', @@ -144,33 +137,27 @@ describe('OFAC - Name and DOB match', function () { // Correct siblings and closest leaf : Everything correct as a proof it('should pass without errors, all conditions satisfied', async function () { let w = await circuit.calculateWitness(nonMemSmtInputs); - const proofLevel = await circuit.getOutput(w, ['proofLevel']); - console.log(proofLevel); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('1'); }); // Correct siblings but membership proof : Fail at line 54 assertion it('should fail to calculate witness since trying to generate membership proof, level 2', async function () { - try { - await circuit.calculateWitness(memSmtInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } + let w = await circuit.calculateWitness(memSmtInputs); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('0'); }); // Give wrong closest leaf but correct siblings array it('should fail to calculate witness due to wrong leaf provided, level 2', async function () { - try { - const wrongInputs = { - ...nonMemSmtInputs, - smt_leaf_value: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(), - }; - await circuit.calculateWitness(wrongInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - expect(error.message).to.include('SMTVerify'); - } + const wrongInputs = { + ...nonMemSmtInputs, + smt_leaf_value: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(), + }; + + let w = await circuit.calculateWitness(wrongInputs); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('0'); }); }); @@ -182,7 +169,7 @@ describe('OFAC - Name match', function () { let nonMemSmtInputs: any; before(async () => { - circuit = await wasm_tester(path.join(__dirname, '../../circuits/ofac/ofac_name.circom'), { + circuit = await wasm_tester(path.join(__dirname, '../../circuits/tests/ofac/ofac_name_tester.circom'), { include: [ 'node_modules', './node_modules/@zk-kit/binary-merkle-root.circom/src', @@ -215,32 +202,25 @@ describe('OFAC - Name match', function () { // Correct siblings and closest leaf : Everything correct as a proof it('should pass without errors, all conditions satisfied', async function () { let w = await circuit.calculateWitness(nonMemSmtInputs); - const proofLevel = await circuit.getOutput(w, ['proofLevel']); - console.log("proofLevel: " + proofLevel.proofLevel); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('1'); }); // Correct siblings but membership proof : Fail at line 46 assertion it('should fail to calculate witness since trying to generate membership proof, level 1', async function () { - try { - await circuit.calculateWitness(memSmtInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } + let w = await circuit.calculateWitness(memSmtInputs); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('0'); }); // Give wrong closest leaf but correct siblings array it('should fail to calculate witness due to wrong leaf provided, level 1', async function () { - try { - const wrongInputs = { - ...nonMemSmtInputs, - smt_leaf_value: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(), - }; - await circuit.calculateWitness(wrongInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - expect(error.message).to.include('SMTVerify'); - } + const wrongInputs = { + ...nonMemSmtInputs, + smt_leaf_value: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(), + }; + let w = await circuit.calculateWitness(wrongInputs); + const ofacVerification = (await circuit.getOutput(w, ['ofacVerification'])).ofacVerification; + expect(ofacVerification).to.equal('0'); }); });