From 222084acc140e8d1c549662a96189769fd48d9c0 Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Sun, 15 Dec 2024 13:29:37 +0100 Subject: [PATCH 1/4] import mux1.circom from circomlib --- .../circuits/utils/circomlib/mux/mux1.circom | 48 +++++++++++++++++++ .../binary-merkle-root.circom | 32 +------------ 2 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 circuits/circuits/utils/circomlib/mux/mux1.circom diff --git a/circuits/circuits/utils/circomlib/mux/mux1.circom b/circuits/circuits/utils/circomlib/mux/mux1.circom new file mode 100644 index 000000000..a596fd624 --- /dev/null +++ b/circuits/circuits/utils/circomlib/mux/mux1.circom @@ -0,0 +1,48 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ +pragma circom 2.1.6; + +template MultiMux1(n) { + signal input c[n][2]; // Constants + signal input s; // Selector + signal output out[n]; + + for (var i=0; i mux.s; + + mux.out[0] ==> out; +} diff --git a/circuits/circuits/utils/other/binary-merkle-root/binary-merkle-root.circom b/circuits/circuits/utils/other/binary-merkle-root/binary-merkle-root.circom index 68af9b1fe..410633e6a 100644 --- a/circuits/circuits/utils/other/binary-merkle-root/binary-merkle-root.circom +++ b/circuits/circuits/utils/other/binary-merkle-root/binary-merkle-root.circom @@ -1,7 +1,8 @@ -pragma circom 2.1.5; +pragma circom 2.1.6; include "../../circomlib/hasher/hash.circom"; include "../../circomlib/bitify/comparators.circom"; +include "../../circomlib/mux/mux1.circom"; // This circuit is designed to calculate the root of a binary Merkle // tree given a leaf, its depth, and the necessary sibling @@ -42,32 +43,3 @@ template BinaryMerkleRoot(MAX_DEPTH) { out <== root + isDepth * nodes[MAX_DEPTH]; } - -template MultiMux1(n) { - signal input c[n][2]; // Constants - signal input s; // Selector - signal output out[n]; - - for (var i=0; i mux.s; - - mux.out[0] ==> out; -} \ No newline at end of file From 655882fded1391f4ea9b522a5682ad47a4b312b1 Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Sun, 15 Dec 2024 13:42:53 +0100 Subject: [PATCH 2/4] fix ofac tests and move ofac to utils/passport/ofac repo --- circuits/circuits/prove/openpassport_prove.circom | 2 +- .../tests/ofac/ofac_name_dob_tester.circom | 2 +- .../circuits/tests/ofac/ofac_name_tester.circom | 2 +- .../tests/ofac/ofac_passport_number_tester.circom | 2 +- .../{ => utils/passport}/ofac/ofac_name.circom | 10 +++++----- .../passport}/ofac/ofac_name_dob.circom | 15 +++++++-------- .../passport}/ofac/ofac_passport_number.circom | 14 ++++++-------- circuits/tests/ofac/ofac.test.ts | 1 + 8 files changed, 23 insertions(+), 25 deletions(-) rename circuits/circuits/{ => utils/passport}/ofac/ofac_name.circom (72%) rename circuits/circuits/{ => utils/passport}/ofac/ofac_name_dob.circom (66%) rename circuits/circuits/{ => utils/passport}/ofac/ofac_passport_number.circom (55%) diff --git a/circuits/circuits/prove/openpassport_prove.circom b/circuits/circuits/prove/openpassport_prove.circom index abb1bb465..5d6dc5d84 100644 --- a/circuits/circuits/prove/openpassport_prove.circom +++ b/circuits/circuits/prove/openpassport_prove.circom @@ -8,7 +8,7 @@ include "../utils/passport/date/isValid.circom"; include "../utils/passport/passportVerifier.circom"; include "../disclose/disclose.circom"; include "../disclose/proveCountryIsNotInList.circom"; -include "../ofac/ofac_name.circom"; +include "../utils/passport/ofac/ofac_name.circom"; template OPENPASSPORT_PROVE(signatureAlgorithm, n, k, MAX_ECONTENT_PADDED_LEN, MAX_SIGNED_ATTR_PADDED_LEN, FORBIDDEN_COUNTRIES_LIST_LENGTH) { var kLengthFactor = getKLengthFactor(signatureAlgorithm); diff --git a/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom b/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom index ee4d0af15..076393be5 100644 --- a/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom +++ b/circuits/circuits/tests/ofac/ofac_name_dob_tester.circom @@ -1,3 +1,3 @@ pragma circom 2.1.9; -include "../../ofac/ofac_name_dob.circom"; +include "../../utils/passport/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 index cc06772b1..ff65a98c4 100644 --- a/circuits/circuits/tests/ofac/ofac_name_tester.circom +++ b/circuits/circuits/tests/ofac/ofac_name_tester.circom @@ -1,3 +1,3 @@ pragma circom 2.1.9; -include "../../ofac/ofac_name.circom"; +include "../../utils/passport/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 index 59807fcc5..2814495b2 100644 --- a/circuits/circuits/tests/ofac/ofac_passport_number_tester.circom +++ b/circuits/circuits/tests/ofac/ofac_passport_number_tester.circom @@ -1,3 +1,3 @@ pragma circom 2.1.9; -include "../../ofac/ofac_passport_number.circom"; +include "../../utils/passport/ofac/ofac_passport_number.circom"; component main { public [ smt_root ] } = OFAC_PASSPORT_NUMBER(); diff --git a/circuits/circuits/ofac/ofac_name.circom b/circuits/circuits/utils/passport/ofac/ofac_name.circom similarity index 72% rename from circuits/circuits/ofac/ofac_name.circom rename to circuits/circuits/utils/passport/ofac/ofac_name.circom index 2e380b9a7..6c9afd0dd 100644 --- a/circuits/circuits/ofac/ofac_name.circom +++ b/circuits/circuits/utils/passport/ofac/ofac_name.circom @@ -1,10 +1,10 @@ pragma circom 2.1.9; -include "../utils/circomlib/hasher/hash.circom"; -include "../utils/circomlib/bitify/comparators.circom"; -include "../utils/other/binary-merkle-root/binary-merkle-root.circom"; -include "../utils/other/getCommonLength.circom"; -include "../utils/other/smt.circom"; +include "../../circomlib/hasher/hash.circom"; +include "../../circomlib/bitify/comparators.circom"; +include "../../other/binary-merkle-root/binary-merkle-root.circom"; +include "../../other/getCommonLength.circom"; +include "../../other/smt.circom"; template OFAC_NAME() { diff --git a/circuits/circuits/ofac/ofac_name_dob.circom b/circuits/circuits/utils/passport/ofac/ofac_name_dob.circom similarity index 66% rename from circuits/circuits/ofac/ofac_name_dob.circom rename to circuits/circuits/utils/passport/ofac/ofac_name_dob.circom index 74c450311..e6099dbe5 100644 --- a/circuits/circuits/ofac/ofac_name_dob.circom +++ b/circuits/circuits/utils/passport/ofac/ofac_name_dob.circom @@ -1,11 +1,10 @@ pragma circom 2.1.9; -include "../utils/circomlib/hasher/hash.circom"; -include "../utils/circomlib/bitify/comparators.circom"; -include "../utils/circomlib/bitify/bitify.circom"; -include "../utils/other/binary-merkle-root/binary-merkle-root.circom"; -include "../utils/other/getCommonLength.circom"; -include "../utils/other/smt.circom"; +include "../../circomlib/hasher/hash.circom"; +include "../../circomlib/bitify/comparators.circom"; +include "../../other/binary-merkle-root/binary-merkle-root.circom"; +include "../../other/getCommonLength.circom"; +include "../../other/smt.circom"; template OFAC_NAME_DOB() { @@ -19,7 +18,7 @@ template OFAC_NAME_DOB() { for (var j = 0; j < 3; j++) { poseidon_hasher[j] = PoseidonHash(13); for (var i = 0; i < 13; i++) { - poseidon_hasher[j].inputs[i] <== dg1[10 + 13 * j + i]; + poseidon_hasher[j].in[i] <== dg1[10 + 13 * j + i]; } poseidon_hasher[j].dummy <== 0; } @@ -28,7 +27,7 @@ template OFAC_NAME_DOB() { // Dob hash component pos_dob = PoseidonHash(6); for(var i = 0; i < 6; i++) { - pos_dob.inputs[i] <== dg1[62 + i]; + pos_dob.in[i] <== dg1[62 + i]; } pos_dob.dummy <== 0; diff --git a/circuits/circuits/ofac/ofac_passport_number.circom b/circuits/circuits/utils/passport/ofac/ofac_passport_number.circom similarity index 55% rename from circuits/circuits/ofac/ofac_passport_number.circom rename to circuits/circuits/utils/passport/ofac/ofac_passport_number.circom index e3e87afb9..67a6cb493 100644 --- a/circuits/circuits/ofac/ofac_passport_number.circom +++ b/circuits/circuits/utils/passport/ofac/ofac_passport_number.circom @@ -1,12 +1,10 @@ pragma circom 2.1.9; -include "circom-dl/circuits/hasher/hash.circom"; -include "circom-dl/circuits/bitify/comparators.circom"; -include "circom-dl/circuits/bitify/bitify.circom"; -include "../utils/other/array.circom"; -include "binary-merkle-root.circom"; -include "../utils/other/getCommonLength.circom"; -include "../utils/other/smt.circom"; +include "../../circomlib/hasher/hash.circom"; +include "../../circomlib/bitify/comparators.circom"; +include "../../other/binary-merkle-root/binary-merkle-root.circom"; +include "../../other/getCommonLength.circom"; +include "../../other/smt.circom"; template OFAC_PASSPORT_NUMBER() { @@ -19,7 +17,7 @@ template OFAC_PASSPORT_NUMBER() { component poseidon_hasher = PoseidonHash(9); for (var i = 0; i < 9; i++) { - poseidon_hasher.inputs[i] <== dg1[49 + i]; + poseidon_hasher.in[i] <== dg1[49 + i]; } poseidon_hasher.dummy <== 0; signal output ofacCheckResult <== SMTVerify(256)(poseidon_hasher.out, smt_leaf_value, smt_root, smt_siblings, 0); diff --git a/circuits/tests/ofac/ofac.test.ts b/circuits/tests/ofac/ofac.test.ts index d2d107f61..96388ca19 100644 --- a/circuits/tests/ofac/ofac.test.ts +++ b/circuits/tests/ofac/ofac.test.ts @@ -88,6 +88,7 @@ describe('OFAC - Passport number match', function () { const ofacCheckResult = (await circuit.getOutput(w, ['ofacCheckResult'])).ofacCheckResult; expect(ofacCheckResult).to.equal('0'); }); + }); // Level 2: NameDob match in OfacList From bd701f9d6397f381bcf58505a5126fd8d91e91c5 Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Sun, 15 Dec 2024 14:04:53 +0100 Subject: [PATCH 3/4] refactored circomlib repo --- circuits/circuits/disclose/disclose.circom | 2 +- .../disclose/proveCountryIsNotInList.circom | 2 +- .../disclose/verify_commitment.circom | 4 +- circuits/circuits/dsc/openpassport_dsc.circom | 5 +- .../merkle-trees}/binary-merkle-root.circom | 0 .../merkle-trees}/getCommonLength.circom | 4 +- .../merkle-trees}/smt.circom | 10 +- .../{other => circomlib/utils}/array.circom | 97 ++++++++++++++- .../{other => circomlib/utils}/bytes.circom | 117 +++++++----------- .../utils}/constants.circom | 4 +- .../utils}/functions.circom | 4 +- .../utils/passport/computeCommitment.circom | 2 +- .../utils/passport/ofac/ofac_name.circom | 5 +- .../utils/passport/ofac/ofac_name_dob.circom | 5 +- .../passport/ofac/ofac_passport_number.circom | 5 +- .../utils/passport/passportVerifier.circom | 4 +- 16 files changed, 160 insertions(+), 110 deletions(-) rename circuits/circuits/utils/{other/binary-merkle-root => circomlib/merkle-trees}/binary-merkle-root.circom (100%) rename circuits/circuits/utils/{other => circomlib/merkle-trees}/getCommonLength.circom (94%) rename circuits/circuits/utils/{other => circomlib/merkle-trees}/smt.circom (88%) rename circuits/circuits/utils/{other => circomlib/utils}/array.circom (62%) rename circuits/circuits/utils/{other => circomlib/utils}/bytes.circom (68%) rename circuits/circuits/utils/{other => circomlib/utils}/constants.circom (89%) rename circuits/circuits/utils/{other => circomlib/utils}/functions.circom (92%) diff --git a/circuits/circuits/disclose/disclose.circom b/circuits/circuits/disclose/disclose.circom index 2eae1b3a1..f0d661335 100644 --- a/circuits/circuits/disclose/disclose.circom +++ b/circuits/circuits/disclose/disclose.circom @@ -1,6 +1,6 @@ pragma circom 2.1.9; -include "../utils/other/bytes.circom"; +include "../utils/circomlib/utils/bytes.circom"; include "../utils/passport/date/isOlderThan.circom"; template DISCLOSE() { diff --git a/circuits/circuits/disclose/proveCountryIsNotInList.circom b/circuits/circuits/disclose/proveCountryIsNotInList.circom index 2fdc94783..9fac77731 100644 --- a/circuits/circuits/disclose/proveCountryIsNotInList.circom +++ b/circuits/circuits/disclose/proveCountryIsNotInList.circom @@ -1,7 +1,7 @@ pragma circom 2.1.5; include "../utils/circomlib/bitify/comparators.circom"; -include "../utils/other/bytes.circom"; +include "../utils/circomlib/utils/bytes.circom"; template ProveCountryIsNotInList(forbiddenCountriesListLength) { signal input dg1[93]; diff --git a/circuits/circuits/disclose/verify_commitment.circom b/circuits/circuits/disclose/verify_commitment.circom index ec8b5d5ac..f3315bc2a 100644 --- a/circuits/circuits/disclose/verify_commitment.circom +++ b/circuits/circuits/disclose/verify_commitment.circom @@ -1,7 +1,7 @@ pragma circom 2.1.9; -include "../utils/other/bytes.circom"; -include "../utils/other/binary-merkle-root/binary-merkle-root.circom"; +include "../utils/circomlib/utils/bytes.circom"; +include "../utils/circomlib/merkle-trees/binary-merkle-root.circom"; include "../utils/passport/computeCommitment.circom"; template VERIFY_COMMITMENT( nLevels) { diff --git a/circuits/circuits/dsc/openpassport_dsc.circom b/circuits/circuits/dsc/openpassport_dsc.circom index 515c12e9e..e27b35938 100644 --- a/circuits/circuits/dsc/openpassport_dsc.circom +++ b/circuits/circuits/dsc/openpassport_dsc.circom @@ -4,12 +4,11 @@ include "../utils/circomlib/bitify/bitify.circom"; include "../utils/circomlib/hasher/hash.circom"; include "../utils/circomlib/bitify/comparators.circom"; include "../utils/circomlib/hasher/hash.circom"; -include "../utils/other/binary-merkle-root/binary-merkle-root.circom"; +include "../utils/circomlib/merkle-trees/binary-merkle-root.circom"; include "../utils/passport/customHashers.circom"; -include "../utils/other/bytes.circom"; include "../utils/passport/signatureAlgorithm.circom"; include "../utils/passport/signatureVerifier.circom"; -include "../utils/other/bytes.circom"; +include "../utils/circomlib/utils/bytes.circom"; template OPENPASSPORT_DSC(signatureAlgorithm, n_dsc, k_dsc, n_csca, k_csca, max_cert_bytes, dscPubkeyBytesLength, nLevels) { diff --git a/circuits/circuits/utils/other/binary-merkle-root/binary-merkle-root.circom b/circuits/circuits/utils/circomlib/merkle-trees/binary-merkle-root.circom similarity index 100% rename from circuits/circuits/utils/other/binary-merkle-root/binary-merkle-root.circom rename to circuits/circuits/utils/circomlib/merkle-trees/binary-merkle-root.circom diff --git a/circuits/circuits/utils/other/getCommonLength.circom b/circuits/circuits/utils/circomlib/merkle-trees/getCommonLength.circom similarity index 94% rename from circuits/circuits/utils/other/getCommonLength.circom rename to circuits/circuits/utils/circomlib/merkle-trees/getCommonLength.circom index 15bdbda7f..e7f91f9d0 100644 --- a/circuits/circuits/utils/other/getCommonLength.circom +++ b/circuits/circuits/utils/circomlib/merkle-trees/getCommonLength.circom @@ -1,7 +1,7 @@ pragma circom 2.1.9; -include "../circomlib/bitify/comparators.circom"; -include "../circomlib/bitify/bitify.circom"; +include "../bitify/comparators.circom"; +include "../bitify/bitify.circom"; // Computes the first n common bits of the hashes template CommonBitsLengthFromEnd() { diff --git a/circuits/circuits/utils/other/smt.circom b/circuits/circuits/utils/circomlib/merkle-trees/smt.circom similarity index 88% rename from circuits/circuits/utils/other/smt.circom rename to circuits/circuits/utils/circomlib/merkle-trees/smt.circom index 629a47693..174a2e443 100644 --- a/circuits/circuits/utils/other/smt.circom +++ b/circuits/circuits/utils/circomlib/merkle-trees/smt.circom @@ -1,10 +1,10 @@ pragma circom 2.1.9; -include "../circomlib/hasher/hash.circom"; -include "../circomlib/bitify/comparators.circom"; -include "../circomlib/bitify/bitify.circom"; -include "./array.circom"; -include "./binary-merkle-root/binary-merkle-root.circom"; +include "../hasher/hash.circom"; +include "../bitify/comparators.circom"; +include "../bitify/bitify.circom"; +include "../utils/array.circom"; +include "binary-merkle-root.circom"; include "getCommonLength.circom"; template SMTVerify(nLength) { diff --git a/circuits/circuits/utils/other/array.circom b/circuits/circuits/utils/circomlib/utils/array.circom similarity index 62% rename from circuits/circuits/utils/other/array.circom rename to circuits/circuits/utils/circomlib/utils/array.circom index 952587b20..3de12baeb 100644 --- a/circuits/circuits/utils/other/array.circom +++ b/circuits/circuits/utils/circomlib/utils/array.circom @@ -1,9 +1,10 @@ -pragma circom 2.1.9; +pragma circom 2.1.6; -include "../circomlib/bitify/comparators.circom"; -include "../circomlib/bitify/bitify.circom"; +include "../bitify/bitify.circom"; +include "../bitify/comparators.circom"; include "./functions.circom"; + /// @title ItemAtIndex /// @notice Select item at given index from the input array /// @notice This template that the index is valid @@ -161,3 +162,93 @@ template AssertZeroPadding(maxArrayLen) { lessThans[i].out * in[i] === 0; } } + +/// @title Slice +/// @notice Extract a fixed portion of an array +/// @dev Unlike SelectSubArray, Slice uses compile-time known indices and doesn't pad the output +/// @dev Slice is more efficient for fixed ranges, while SelectSubArray offers runtime flexibility +/// @param n The length of the input array +/// @param start The starting index of the slice (inclusive) +/// @param end The ending index of the slice (exclusive) +/// @input in The input array of length n +/// @output out The sliced array of length (end - start) +template Slice(n, start, end) { + assert(n >= end); + assert(start >= 0); + assert(end >= start); + + signal input in[n]; + signal output out[end - start]; + + for (var i = start; i < end; i++) { + out[i - start] <== in[i]; + } +} + +/// @title CheckSubstringMatch +/// @notice Check if a substring matches the input array +/// @param maxSubstringLen The maximum length of the substring +/// @input input The portion of the input array to check +/// @input substring The substring pattern to match +/// @output isMatch 1 if the substring matches, 0 otherwise +template CheckSubstringMatch(maxSubstringLen) { + signal input in[maxSubstringLen]; + signal input substring[maxSubstringLen]; + signal output isMatch; + + // Ensure the first element of the pattern is non-zero + signal firstElementNonZero; + firstElementNonZero <== IsZero()(substring[0]); + firstElementNonZero === 0; + + signal matchAccumulator[maxSubstringLen + 1]; + signal difference[maxSubstringLen]; + signal isZeroDifference[maxSubstringLen]; + + matchAccumulator[0] <== 1; + + for (var i = 0; i < maxSubstringLen; i++) { + difference[i] <== (in[i] - substring[i]) * substring[i]; + isZeroDifference[i] <== IsZero()(difference[i]); + matchAccumulator[i + 1] <== matchAccumulator[i] * isZeroDifference[i]; + } + + isMatch <== matchAccumulator[maxSubstringLen]; +} + +/// @title CountSubstringOccurrences +/// @notice Count the number of times a substring occurs in the input array +/// @param maxLen The maximum length of the input array +/// @param maxSubstringLen The maximum length of the substring +/// @input in The input array to search in +/// @input substring The substring to search for +/// @output count The number of occurrences of the substring in the input +template CountSubstringOccurrences(maxLen, maxSubstringLen) { + assert(maxLen >= maxSubstringLen); + + signal input in[maxLen]; + signal input substring[maxSubstringLen]; + signal output count; + + // Check for matches at each possible starting position + component matches[maxLen]; + for (var i = 0; i < maxLen; i++) { + matches[i] = CheckSubstringMatch(maxSubstringLen); + for (var j = 0; j < maxSubstringLen; j++) { + if (i + j < maxLen) { + matches[i].in[j] <== in[i + j]; + } else { + matches[i].in[j] <== 0; + } + } + matches[i].substring <== substring; + } + + // Sum up all matches to get the total count + component summer = CalculateTotal(maxLen); + for (var i = 0; i < maxLen; i++) { + summer.nums[i] <== matches[i].isMatch; + } + + count <== summer.sum; +} \ No newline at end of file diff --git a/circuits/circuits/utils/other/bytes.circom b/circuits/circuits/utils/circomlib/utils/bytes.circom similarity index 68% rename from circuits/circuits/utils/other/bytes.circom rename to circuits/circuits/utils/circomlib/utils/bytes.circom index d102981a1..a5b3979ae 100644 --- a/circuits/circuits/utils/other/bytes.circom +++ b/circuits/circuits/utils/circomlib/utils/bytes.circom @@ -1,10 +1,11 @@ -pragma circom 2.1.9; +pragma circom 2.1.6; -include "../circomlib/bitify/bitify.circom"; -include "../circomlib/bitify/comparators.circom"; +include "../bitify/bitify.circom"; +include "../bitify/comparators.circom"; include "./array.circom"; -include "./functions.circom"; include "./constants.circom"; +include "./functions.circom"; + function computeIntChunkLength(byteLength) { var packSize = MAX_BYTES_IN_FIELD(); @@ -111,77 +112,8 @@ template DigitBytesToInt(n) { out <== sums[n]; } -/// NOTE: this circuit is unaudited and should not be used in production -/// @title SplitBytesToWords -/// @notice split an array of bytes into an array of words -/// @notice useful for casting a message or modulus before RSA verification -/// @param l: number of bytes in the input array -/// @param n: number of bits in a word -/// @param k: number of words -/// @input in: array of bytes -/// @output out: array of words -template SplitSignalsToWords (t,l,n,k) { - assert(n*k >= t*l); - signal input in[l]; - signal output out[k]; - component num2bits[l]; - for (var i = 0 ; i < l ; i++){ - num2bits[i] = Num2Bits(t); - num2bits[i].in <== in[i]; - } - for (var i = 0 ; i < t ; i ++){ - } - component bits2num[k]; - for (var i = 0 ; i < k ; i++){ - bits2num[i] = Bits2Num(n); - - for(var j = 0 ; j < n ; j++){ - if(i*n + j >= l * t){ - bits2num[i].in[j] <== 0; - } - else{ - bits2num[i].in[j] <== num2bits[ (( i * n + j) \ t) ].out[ ((i * n + j) % t)]; - } - } - } - for( var i = 0 ; i< k ; i++){ - out[i] <== bits2num[i].out; - } - -} - -template SplitSignalsToWordsUnsafe (t,l,n,k) { - - signal input in[l]; - signal output out[k]; - component num2bits[l]; - for (var i = 0 ; i < l ; i++){ - num2bits[i] = Num2Bits(t); - num2bits[i].in <== in[i]; - } - for (var i = 0 ; i < t ; i ++){ - } - component bits2num[k]; - for (var i = 0 ; i < k ; i++){ - bits2num[i] = Bits2Num(n); - - for(var j = 0 ; j < n ; j++){ - if(i*n + j >= l * t){ - bits2num[i].in[j] <== 0; - } - else{ - bits2num[i].in[j] <== num2bits[ (( i * n + j) \ t) ].out[ ((i * n + j) % t)]; - } - } - } - for( var i = 0 ; i< k ; i++){ - out[i] <== bits2num[i].out; - } - -} - -/// NOTE: this circuit is unaudited and should not be used in production +// NOTE: this circuit is unaudited and should not be used in production /// @title SplitBytesToWords /// @notice split an array of bytes into an array of words /// @notice useful for casting a message or modulus before RSA verification @@ -193,6 +125,7 @@ template SplitSignalsToWordsUnsafe (t,l,n,k) { template SplitBytesToWords (l,n,k) { signal input in[l]; signal output out[k]; + component num2bits[l]; for (var i = 0 ; i < l ; i++){ num2bits[i] = Num2Bits(8); @@ -213,4 +146,40 @@ template SplitBytesToWords (l,n,k) { for( var i = 0 ; i< k ; i++){ out[i] <== bits2num[i].out; } +} + +// Asserts that a given input is binary. +// +// Inputs: +// - in: an input signal, expected to be 0 or 1. +template AssertBit() { + signal input in; + in * (in - 1) === 0; +} + +// The ByteMask template masks an input array using a binary mask array. +// Each element in the input array is multiplied by the corresponding element in the mask array. +// The mask array is validated to ensure all elements are binary (0 or 1). +// +// Parameters: +// - maxLength: The maximum length of the input and mask arrays. +// +// Inputs: +// - body: An array of signals representing the body to be masked. +// - mask: An array of signals representing the binary mask. +// +// Outputs: +// - out: An array of signals representing the masked input. +template ByteMask(maxLength) { + signal input in[maxLength]; + signal input mask[maxLength]; + signal output out[maxLength]; + + component bit_check[maxLength]; + + for (var i = 0; i < maxLength; i++) { + bit_check[i] = AssertBit(); + bit_check[i].in <== mask[i]; + out[i] <== in[i] * mask[i]; + } } \ No newline at end of file diff --git a/circuits/circuits/utils/other/constants.circom b/circuits/circuits/utils/circomlib/utils/constants.circom similarity index 89% rename from circuits/circuits/utils/other/constants.circom rename to circuits/circuits/utils/circomlib/utils/constants.circom index 297769044..69edec43b 100644 --- a/circuits/circuits/utils/other/constants.circom +++ b/circuits/circuits/utils/circomlib/utils/constants.circom @@ -1,4 +1,4 @@ -pragma circom 2.1.9; +pragma circom 2.1.6; function EMAIL_ADDR_MAX_BYTES() { @@ -12,4 +12,4 @@ function DOMAIN_MAX_BYTES() { // Field support maximum of ~253 bit function MAX_BYTES_IN_FIELD() { return 31; -} +} \ No newline at end of file diff --git a/circuits/circuits/utils/other/functions.circom b/circuits/circuits/utils/circomlib/utils/functions.circom similarity index 92% rename from circuits/circuits/utils/other/functions.circom rename to circuits/circuits/utils/circomlib/utils/functions.circom index f9bb802ee..b0a695dc1 100644 --- a/circuits/circuits/utils/other/functions.circom +++ b/circuits/circuits/utils/circomlib/utils/functions.circom @@ -1,4 +1,4 @@ -pragma circom 2.1.9; +pragma circom 2.1.6; /// @function log2Ceil /// @notice Calculate log2 of a number and round it up @@ -14,4 +14,4 @@ function log2Ceil(a) { } return r; -} +} \ No newline at end of file diff --git a/circuits/circuits/utils/passport/computeCommitment.circom b/circuits/circuits/utils/passport/computeCommitment.circom index 8c7a4e8fe..928b51dc9 100644 --- a/circuits/circuits/utils/passport/computeCommitment.circom +++ b/circuits/circuits/utils/passport/computeCommitment.circom @@ -1,7 +1,7 @@ pragma circom 2.1.9; include "../circomlib/hasher/hash.circom"; -include "../other/bytes.circom"; +include "../circomlib/utils/bytes.circom"; include "./customHashers.circom"; template ComputeCommitment() { diff --git a/circuits/circuits/utils/passport/ofac/ofac_name.circom b/circuits/circuits/utils/passport/ofac/ofac_name.circom index 6c9afd0dd..c793ca16b 100644 --- a/circuits/circuits/utils/passport/ofac/ofac_name.circom +++ b/circuits/circuits/utils/passport/ofac/ofac_name.circom @@ -1,10 +1,7 @@ pragma circom 2.1.9; include "../../circomlib/hasher/hash.circom"; -include "../../circomlib/bitify/comparators.circom"; -include "../../other/binary-merkle-root/binary-merkle-root.circom"; -include "../../other/getCommonLength.circom"; -include "../../other/smt.circom"; +include "../../circomlib/merkle-trees/smt.circom"; template OFAC_NAME() { diff --git a/circuits/circuits/utils/passport/ofac/ofac_name_dob.circom b/circuits/circuits/utils/passport/ofac/ofac_name_dob.circom index e6099dbe5..d3a99206e 100644 --- a/circuits/circuits/utils/passport/ofac/ofac_name_dob.circom +++ b/circuits/circuits/utils/passport/ofac/ofac_name_dob.circom @@ -1,10 +1,7 @@ pragma circom 2.1.9; include "../../circomlib/hasher/hash.circom"; -include "../../circomlib/bitify/comparators.circom"; -include "../../other/binary-merkle-root/binary-merkle-root.circom"; -include "../../other/getCommonLength.circom"; -include "../../other/smt.circom"; +include "../../circomlib/merkle-trees/smt.circom"; template OFAC_NAME_DOB() { diff --git a/circuits/circuits/utils/passport/ofac/ofac_passport_number.circom b/circuits/circuits/utils/passport/ofac/ofac_passport_number.circom index 67a6cb493..3b297ec60 100644 --- a/circuits/circuits/utils/passport/ofac/ofac_passport_number.circom +++ b/circuits/circuits/utils/passport/ofac/ofac_passport_number.circom @@ -1,10 +1,7 @@ pragma circom 2.1.9; include "../../circomlib/hasher/hash.circom"; -include "../../circomlib/bitify/comparators.circom"; -include "../../other/binary-merkle-root/binary-merkle-root.circom"; -include "../../other/getCommonLength.circom"; -include "../../other/smt.circom"; +include "../../circomlib/merkle-trees/smt.circom"; template OFAC_PASSPORT_NUMBER() { diff --git a/circuits/circuits/utils/passport/passportVerifier.circom b/circuits/circuits/utils/passport/passportVerifier.circom index 5a5b8f672..a58ebdd5a 100644 --- a/circuits/circuits/utils/passport/passportVerifier.circom +++ b/circuits/circuits/utils/passport/passportVerifier.circom @@ -1,7 +1,7 @@ pragma circom 2.1.9; -include "../other/array.circom"; -include "../other/bytes.circom"; +include "../circomlib/utils/array.circom"; +include "../circomlib/utils/bytes.circom"; // include "../shaBytes/shaBytesStatic.circom"; // include "../shaBytes/shaBytesDynamic.circom"; include "../circomlib/hasher/hash.circom"; From e9867588ce36d0b39c81f876bd77766f40b029b1 Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Sun, 15 Dec 2024 14:08:11 +0100 Subject: [PATCH 4/4] Remove unused FormatECDSAInputs template from circom utils --- circuits/circuits/utils/passport/formatECDSAInputs.circom | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 circuits/circuits/utils/passport/formatECDSAInputs.circom diff --git a/circuits/circuits/utils/passport/formatECDSAInputs.circom b/circuits/circuits/utils/passport/formatECDSAInputs.circom deleted file mode 100644 index e2caf70ec..000000000 --- a/circuits/circuits/utils/passport/formatECDSAInputs.circom +++ /dev/null @@ -1,6 +0,0 @@ -pragma circom 2.1.9; - - -template FormatECDSAInputs(signatureAlgorithm, k) { - -}