diff --git a/.gitignore b/.gitignore index 7cae081..38d60c8 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ test/passports/* test/inputs/* test/registerIdentityTest.ts test/circuits/* -!test/passports/readme.txt \ No newline at end of file +!test/passports/readme.txt +circuits/scripts/* \ No newline at end of file diff --git a/circuits/activeAuth/rsaActiveAuthentication.circom b/circuits/activeAuth/rsaActiveAuthentication.circom index 6cc1578..b4e1c3c 100644 --- a/circuits/activeAuth/rsaActiveAuthentication.circom +++ b/circuits/activeAuth/rsaActiveAuthentication.circom @@ -19,10 +19,6 @@ template RsaSha1ActiveAuthentication(CHUNK_SIZE, CHUNK_NUMBER, E_BITS) { rsaDecryptor.modulus[i] <== modulus[i]; } - for (var i = 0; i < CHUNK_NUMBER; i++) { - log(rsaDecryptor.out[i]); - } - } component main = RsaSha1ActiveAuthentication(64, 16, 17); \ No newline at end of file diff --git a/circuits/ecdsa/p320main.circom b/circuits/ecdsa/p320main.circom index 30f4965..dfc0318 100644 --- a/circuits/ecdsa/p320main.circom +++ b/circuits/ecdsa/p320main.circom @@ -2,4 +2,4 @@ pragma circom 2.1.6; include "./brainpoolP320r1/signatureVerification.circom"; -component main = verifyBrainpool320(32,10,256); +component main = verifyBrainpool320(32, 10, 256); diff --git a/circuits/identityManagement/circuits/registerIdentityBuilder.circom b/circuits/identityManagement/circuits/registerIdentityBuilder.circom index 2ce2bf4..0113756 100644 --- a/circuits/identityManagement/circuits/registerIdentityBuilder.circom +++ b/circuits/identityManagement/circuits/registerIdentityBuilder.circom @@ -15,6 +15,7 @@ include "circomlib/circuits/poseidon.circom"; // SIGNATURE_TYPE: // - 1: RSA 2048 bits + SHA2-256 + e = 65537 // - 2: RSA 4096 bits + SHA2-256 + e = 65537 +// - 3: RSA 2048 bits + SHA1 + e = 65537 // - 10: RSASSA-PSS 2048 bits MGF1 (SHA2-256) + SHA2-256 + e = 3 + salt = 32 // - 11: RSASSA-PSS 2048 bits MGF1 (SHA2-256) + SHA2-256 + e = 65537 + salt = 32 @@ -59,6 +60,15 @@ template RegisterIdentityBuilder ( CHUNK_NUMBER = 64; } + if (SIGNATURE_TYPE == 3){ + HASH_TYPE = 160; + } + + if (SIGNATURE_TYPE == 4){ + HASH_TYPE = 160; + CHUNK_NUMBER = 48; + } + if (SIGNATURE_TYPE == 13){ HASH_TYPE = 384; } @@ -138,6 +148,7 @@ template RegisterIdentityBuilder ( // ------- // PASSPORT VERIFICATION // ------- + component passportVerifier = PassportVerificationBuilder( SIGNATURE_TYPE, // 1, 2.. (list above) ^^^ DG_HASH_TYPE, // 160, 224, 256, 384, 512 (list above)^^^ diff --git a/circuits/passportVerification/passportVerificationBuilder.circom b/circuits/passportVerification/passportVerificationBuilder.circom index 146993f..bf899b5 100644 --- a/circuits/passportVerification/passportVerificationBuilder.circom +++ b/circuits/passportVerification/passportVerificationBuilder.circom @@ -18,7 +18,13 @@ template PassportVerificationBuilder(SIGNATURE_TYPE,DG_HASH_TYPE,EC_BLOCK_NUMBER if (SIGNATURE_TYPE == 2){ CHUNK_NUMBER = 64; } - + if (SIGNATURE_TYPE == 3){ + HASH_TYPE = 160; + } + if (SIGNATURE_TYPE == 4){ + HASH_TYPE = 160; + CHUNK_NUMBER = 48; + } if (SIGNATURE_TYPE == 13){ HASH_TYPE = 384; } @@ -46,7 +52,7 @@ template PassportVerificationBuilder(SIGNATURE_TYPE,DG_HASH_TYPE,EC_BLOCK_NUMBER CHUNK_SIZE = 32; HASH_TYPE = 224; } - + var DG_HASH_BLOCK_SIZE = 1024; if (DG_HASH_TYPE <= 256){ DG_HASH_BLOCK_SIZE = 512; @@ -55,8 +61,8 @@ template PassportVerificationBuilder(SIGNATURE_TYPE,DG_HASH_TYPE,EC_BLOCK_NUMBER if (HASH_TYPE <= 256){ HASH_BLOCK_SIZE = 512; } - - + + var DG1_LEN = 1024; var SIGNED_ATTRIBUTES_LEN = 1024; @@ -135,8 +141,7 @@ template PassportVerificationBuilder(SIGNATURE_TYPE,DG_HASH_TYPE,EC_BLOCK_NUMBER if (AA_SIGNATURE_ALGO != 0){ DG15_ACTUAL_SHIFT = DG15_SHIFT; } - - passportVerificationFlow = PassportVerificationFlow(ENCAPSULATED_CONTENT_LEN,DG_HASH_TYPE,EC_HASH_TYPE,DG1_SHIFT,DG15_ACTUAL_SHIFT,EC_SHIFT,AA_SIGNATURE_ALGO); + passportVerificationFlow = PassportVerificationFlow(ENCAPSULATED_CONTENT_LEN,DG_HASH_TYPE,EC_HASH_TYPE,DG1_SHIFT,DG15_ACTUAL_SHIFT,EC_SHIFT,AA_SIGNATURE_ALGO); passportVerificationFlow.dg1Hash <== dg1Hash; passportVerificationFlow.dg15Hash <== dg15Hash; @@ -183,15 +188,15 @@ template PassportVerificationBuilder(SIGNATURE_TYPE,DG_HASH_TYPE,EC_BLOCK_NUMBER } //ECDSA SIG else { - + var EC_FIELD_SIZE = CHUNK_NUMBER * CHUNK_SIZE; var DIFF = 0; if (EC_FIELD_SIZE > 248){ - DIFF = 248 - EC_FIELD_SIZE; + DIFF = EC_FIELD_SIZE - 248; } component xToNum = Bits2Num(EC_FIELD_SIZE - DIFF); component yToNum = Bits2Num(EC_FIELD_SIZE - DIFF); - + for (var i = 0; i < EC_FIELD_SIZE - DIFF; i++) { xToNum.in[EC_FIELD_SIZE - DIFF - 1 - i] <== pubkey[i + DIFF]; yToNum.in[EC_FIELD_SIZE - DIFF - 1 - i] <== pubkey[EC_FIELD_SIZE + i + DIFF]; diff --git a/circuits/rsa/powMod.circom b/circuits/rsa/powMod.circom index 208c557..998e729 100644 --- a/circuits/rsa/powMod.circom +++ b/circuits/rsa/powMod.circom @@ -2,8 +2,6 @@ pragma circom 2.1.6; include "../bigInt/bigInt.circom"; -// CHUNK_SIZE = 32 -// E_BITS = 17 // CHUNK_NUMBER is the length of the base and modulus // calculates (base^exp) % modulus, exp = 2^(E_BITS - 1) + 1 = 2^16 + 1 template PowerMod(CHUNK_SIZE, CHUNK_NUMBER, E_BITS) { @@ -45,3 +43,172 @@ template PowerMod(CHUNK_SIZE, CHUNK_NUMBER, E_BITS) { out[i] <== muls[E_BITS - 1].out[i]; } } + +// Deprecated +// template PowerMod37187(CHUNK_SIZE, CHUNK_NUMBER) { + +// signal input base[CHUNK_NUMBER]; +// signal input modulus[CHUNK_NUMBER]; + +// signal output out[CHUNK_NUMBER]; + +// component muls[15]; +// component resultMuls[5]; + +// for (var i = 0; i < 15; i++) { +// muls[i] = BigMultModP(CHUNK_SIZE, CHUNK_NUMBER); + +// for (var j = 0; j < CHUNK_NUMBER; j++) { +// muls[i].p[j] <== modulus[j]; +// } +// } + +// for (var i = 0; i < 5; i++) { +// resultMuls[i] = BigMultModP(CHUNK_SIZE, CHUNK_NUMBER); + +// for (var j = 0; j < CHUNK_NUMBER; j++) { +// resultMuls[i].p[j] <== modulus[j]; +// } +// } + + +// for (var i = 0; i < CHUNK_NUMBER; i++) { +// muls[0].a[i] <== base[i]; +// muls[0].b[i] <== base[i]; +// } + +// for (var i = 1; i < 15; i++) { +// for (var j = 0; j < CHUNK_NUMBER; j++) { +// muls[i].a[j] <== muls[i - 1].out[j]; +// muls[i].b[j] <== muls[i - 1].out[j]; +// } +// } + +// resultMuls[0].a <== muls[14].out; // 32768 +// resultMuls[0].b <== muls[11].out; // 4096 +// resultMuls[1].a <== resultMuls[0].out; // 32768 + 4096 +// resultMuls[1].b <== muls[7].out; // 256 +// resultMuls[2].a <== resultMuls[1].out; // 32768 + 4096 + 256 +// resultMuls[2].b <== muls[5].out; // 64 +// resultMuls[3].a <== resultMuls[2].out; // 32768 + 4096 + 256 + 64 +// resultMuls[3].b <== muls[0].out; // 2 +// resultMuls[4].a <== resultMuls[3].out; // 32768 + 4096 + 256 + 64 + 2 +// resultMuls[4].b <== base; // 1 + + +// for (var i = 0; i < CHUNK_NUMBER; i++) { +// out[i] <== resultMuls[4].out[i]; +// log(out[i]); +// } + + +// } + +template PowerModAnyExp(CHUNK_SIZE, CHUNK_NUMBER, EXP) { + assert(EXP >= 3); + + signal input base[CHUNK_NUMBER]; + signal input modulus[CHUNK_NUMBER]; + + signal output out[CHUNK_NUMBER]; + + var exp_process[256] = exp_to_bits(EXP); + + component muls[exp_process[0]]; + component resultMuls[exp_process[1] - 1]; + + for (var i = 0; i < exp_process[0]; i++){ + muls[i] = BigMultModP(CHUNK_SIZE, CHUNK_NUMBER); + muls[i].p <== modulus; + } + + for (var i = 0; i < exp_process[1] - 1; i++){ + resultMuls[i] = BigMultModP(CHUNK_SIZE, CHUNK_NUMBER); + resultMuls[i].p <== modulus; + } + + muls[0].a <== base; + muls[0].b <== base; + + for (var i = 1; i < exp_process[0]; i++){ + muls[i].a <== muls[i - 1].out; + muls[i].b <== muls[i - 1].out; + } + + for (var i = 0; i < exp_process[1] - 1; i++){ + if (i == 0){ + if (exp_process[i + 2] == 0){ + resultMuls[i].a <== base; + } else { + resultMuls[i].a <== muls[exp_process[i + 2] - 1].out; + } + resultMuls[i].b <== muls[exp_process[i + 3] - 1].out; + } + else { + resultMuls[i].a <== resultMuls[i - 1].out; + resultMuls[i].b <== muls[exp_process[i + 3] - 1].out; + } + } + + out <== resultMuls[exp_process[1] - 2].out; +} + + +template GetLastBit(){ + signal input in; + signal output bit; + signal output div; + + bit <-- in % 2; + div <-- in \ 2; + + (1 - bit) * bit === 0; + div * 2 + bit * bit === in; +} + +template GetLastNBits(N){ + signal input in; + signal output div; + signal output out[N]; + + component getLastBit[N]; + for (var i = 0; i < N; i++){ + getLastBit[i] = GetLastBit(); + if (i == 0){ + getLastBit[i].in <== in; + } else { + getLastBit[i].in <== getLastBit[i - 1].div; + } + out[i] <== getLastBit[i].bit; + } + + div <== getLastBit[N - 1].div; +} + + +function exp_to_bits(exp){ + var mul_num = 0; + var result_mul_num = 0; + var indexes[256]; + var bits[254]; + + var exp_clone = exp; + var counter = 0; + var result_counter; + while (exp > 0){ + bits[counter] = exp % 2; + exp = exp \ 2; + if (bits[counter] == 1) { + result_mul_num += 1; + indexes[result_counter+2] = counter; + result_counter += 1; + } + mul_num += 1; + counter++; + } + indexes[0] = mul_num - 1; + indexes[1] = result_mul_num; + + return indexes; + +} \ No newline at end of file diff --git a/circuits/rsa/rsa.circom b/circuits/rsa/rsa.circom index 4c05df3..7f24ffd 100644 --- a/circuits/rsa/rsa.circom +++ b/circuits/rsa/rsa.circom @@ -55,3 +55,82 @@ template RsaVerifyPkcs1v15(CHUNK_SIZE, CHUNK_NUMBER, E_BITS, HASH_TYPE) { pm.out[i] === 18446744073709551615; // 0b1111111111111111111111111111111111111111111111111111111111111111 } } + +// Pkcs1v15 + Sha160, e = 65537 +template RsaVerifyPkcs1v15Sha1(CHUNK_SIZE, CHUNK_NUMBER, E_BITS, HASH_TYPE) { + signal input signature[CHUNK_NUMBER]; + signal input pubkey[CHUNK_NUMBER]; //aka modulus + + signal input hashed[HASH_TYPE]; + + // signature ** exp mod modulus + component pm = PowerMod(CHUNK_SIZE, CHUNK_NUMBER, E_BITS); + for (var i = 0; i < CHUNK_NUMBER; i++) { + pm.base[i] <== signature[i]; + pm.modulus[i] <== pubkey[i]; + } + + signal hashed_chunks[2]; + + component bits2num[2]; + for (var i = 0; i < 2; i++){ + bits2num[i] = Bits2Num(64); + for (var j = 0; j < 64; j++){ + bits2num[i].in[j] <== hashed[159 - j - i * 64]; + } + } + + component getBits = GetLastNBits(32); + getBits.in <== pm.out[2]; + for (var i = 0; i < 32; i++){ + getBits.out[i] === hashed[31 - i]; + } + getBits.div === 83887124; //0x5000414 + + pm.out[3] === 650212878678426138; + pm.out[4] === 18446744069417738544; + for (var i = 5; i < CHUNK_NUMBER-1; i++) { + pm.out[i] === 18446744073709551615; // 0b1111111111111111111111111111111111111111111111111111111111111111 + } + pm.out[CHUNK_NUMBER - 1] === 562949953421311; +} + +// TODO: research this moment https://www.youtube.com/watch?v=XfELJU1mRMg, +// optimisation may be possible +template RsaVerifyPkcs1v15Sha1E37817(CHUNK_SIZE, CHUNK_NUMBER, HASH_TYPE) { + signal input signature[CHUNK_NUMBER]; + signal input pubkey[CHUNK_NUMBER]; //aka modulus + + signal input hashed[HASH_TYPE]; + + // signature ** exp mod modulus + component pm = PowerModAnyExp(CHUNK_SIZE, CHUNK_NUMBER, 37187); + for (var i = 0; i < CHUNK_NUMBER; i++) { + pm.base[i] <== signature[i]; + pm.modulus[i] <== pubkey[i]; + } + + signal hashed_chunks[2]; + + component bits2num[2]; + for (var i = 0; i < 2; i++){ + bits2num[i] = Bits2Num(64); + for (var j = 0; j < 64; j++){ + bits2num[i].in[j] <== hashed[159 - j - i * 64]; + } + } + + component getBits = GetLastNBits(32); + getBits.in <== pm.out[2]; + for (var i = 0; i < 32; i++){ + getBits.out[i] === hashed[31 - i]; + } + getBits.div === 83887124; //0x5000414 + + pm.out[3] === 650212878678426138; + pm.out[4] === 18446744069417738544; + for (var i = 5; i < CHUNK_NUMBER-1; i++) { + pm.out[i] === 18446744073709551615; // 0b1111111111111111111111111111111111111111111111111111111111111111 + } + pm.out[CHUNK_NUMBER - 1] === 562949953421311; +} diff --git a/circuits/rsa/rsaVerify.circom b/circuits/rsa/rsaVerify.circom index 3838fa1..47ed098 100644 --- a/circuits/rsa/rsaVerify.circom +++ b/circuits/rsa/rsaVerify.circom @@ -2,4 +2,4 @@ pragma circom 2.1.6; include "./rsa.circom"; -component main = RsaVerifyPkcs1v15(64, 32, 17, 256); \ No newline at end of file +component main = PowerModAnyExp(64, 48, 37187); diff --git a/circuits/rsaPss/verifyRsaSigs64e17.circom b/circuits/rsaPss/verifyRsaSigs64e17.circom index 95a4bbc..670bd0e 100644 --- a/circuits/rsaPss/verifyRsaSigs64e17.circom +++ b/circuits/rsaPss/verifyRsaSigs64e17.circom @@ -2,4 +2,4 @@ pragma circom 2.1.6; include "./rsaPss.circom"; -component main = VerifyRsaSig(64, 32, 64, 17, 256); +component main = VerifyRsaSig(64, 48, 32, 17, 256); diff --git a/circuits/scripts/compile-circuit.sh b/circuits/scripts/compile-circuit.sh index 3c0587b..6282f9b 100755 --- a/circuits/scripts/compile-circuit.sh +++ b/circuits/scripts/compile-circuit.sh @@ -31,7 +31,7 @@ mkdir -p ${BUILD_DIR} # Compiling circuit with .r1cs and .wasm files as result echo -e "\nCompiling the circuits..." -time circom ${CIRCUIT_FILE} --r1cs --wasm --c --sym -o ${BUILD_DIR} -l ../../node_modules --O0 +time circom ${CIRCUIT_FILE} --r1cs --wasm --c --sym -o ${BUILD_DIR} -l ../../node_modules --O2 mv ${BUILD_DIR}/${CIRCUIT_NAME}_js/${CIRCUIT_NAME}.wasm ${BUILD_DIR}/${CIRCUIT_NAME}.wasm diff --git a/circuits/signatureVerifier/signatureVerification.circom b/circuits/signatureVerifier/signatureVerification.circom index cbeaa3e..c161f00 100644 --- a/circuits/signatureVerifier/signatureVerification.circom +++ b/circuits/signatureVerifier/signatureVerification.circom @@ -10,7 +10,7 @@ include "../rsaPss/rsaPss.circom"; template VerifySignature(SIG_ALGO){ - assert(((SIG_ALGO >= 1)&&(SIG_ALGO <= 2))||((SIG_ALGO >= 10)&&(SIG_ALGO <= 14))||((SIG_ALGO >= 20)&&(SIG_ALGO <= 24))); + assert(((SIG_ALGO >= 1)&&(SIG_ALGO <= 4))||((SIG_ALGO >= 10)&&(SIG_ALGO <= 14))||((SIG_ALGO >= 20)&&(SIG_ALGO <= 24))); var CHUNK_SIZE = 64; var CHUNK_NUMBER = 32; @@ -31,6 +31,19 @@ template VerifySignature(SIG_ALGO){ SIGNATURE_LEN = CHUNK_NUMBER; HASH_LEN = 256; } + if (SIG_ALGO == 3){ + PUBKEY_LEN = CHUNK_NUMBER; + SIGNATURE_LEN = CHUNK_NUMBER; + HASH_LEN = 160; + } + if (SIG_ALGO == 4){ + CHUNK_NUMBER = 48; + PUBKEY_LEN = CHUNK_NUMBER; + SIGNATURE_LEN = CHUNK_NUMBER; + HASH_LEN = 160; + } + + if (SIG_ALGO == 10){ PUBKEY_LEN = CHUNK_NUMBER; SIGNATURE_LEN = CHUNK_NUMBER; @@ -55,7 +68,7 @@ template VerifySignature(SIG_ALGO){ HASH_LEN = 384; SALT_LEN = 48; } - if (SIG_ALGO == 14){ + if (SIG_ALGO == 14){ CHUNK_NUMBER = 48; PUBKEY_LEN = CHUNK_NUMBER; SIGNATURE_LEN = CHUNK_NUMBER; @@ -111,6 +124,18 @@ template VerifySignature(SIG_ALGO){ rsa4096Sha256Verification.signature <== signature; rsa4096Sha256Verification.hashed <== hashed; } + if (SIG_ALGO == 3){ + component rsa2048Sha160Verification = RsaVerifyPkcs1v15Sha1(CHUNK_SIZE, CHUNK_NUMBER, E_BITS, HASH_LEN); + rsa2048Sha160Verification.pubkey <== pubkey; + rsa2048Sha160Verification.signature <== signature; + rsa2048Sha160Verification.hashed <== hashed; + } + if (SIG_ALGO == 4){ + component verifyRsa3072Sha1E37817 = RsaVerifyPkcs1v15Sha1E37817(CHUNK_SIZE, CHUNK_NUMBER, HASH_LEN); + verifyRsa3072Sha1E37817.pubkey <== pubkey; + verifyRsa3072Sha1E37817.signature <== signature; + verifyRsa3072Sha1E37817.hashed <== hashed; + } if (SIG_ALGO == 10){ component rsa2048PssSha256Verification = VerifyRsaSig(CHUNK_SIZE, CHUNK_NUMBER, SALT_LEN, E_BITS, HASH_LEN); rsa2048PssSha256Verification.pubkey <== pubkey; diff --git a/tests/tests/autocurve.py b/tests/tests/autocurve.py index 8b3221c..01742a5 100644 --- a/tests/tests/autocurve.py +++ b/tests/tests/autocurve.py @@ -662,13 +662,11 @@ def write_pows(n, k, curve_name): ecdsa_func_str = get_ecdsa_func_str(n, k, stride_list) f = open('tests/tests/circuits/testCurve/{curve_name}Pows.circom'.format(curve_name = curve_name, n = n, k = k), 'w') - orig_stdout = sys.stdout sys.stdout = f print(ecdsa_func_str) - def write_func(n, k, curve_name): ecdsa_func_str = get_func_str(n, k, curve_name) diff --git a/tests/tests/automatisationTest.js b/tests/tests/automatisationTest.js index 52bf2b9..daca22a 100644 --- a/tests/tests/automatisationTest.js +++ b/tests/tests/automatisationTest.js @@ -12,9 +12,19 @@ const Fr = new F1Field(exports.p); const assert = chai.assert; +let files = []; function generateFilesForAll(filenames, done) { let index = 0; + const tmpfilePath = path.join(__dirname, `./inputs/tmp.txt`); + fs.writeFile(tmpfilePath, '', (err) => { + if (err) { + console.error('Error emptying file:', err); + } else { + console.log('File emptied successfully'); + } + }); + function executeNext() { if (index >= filenames.length) { return done(); // All files have been processed @@ -32,7 +42,9 @@ function generateFilesForAll(filenames, done) { console.error(`Script stderr: ${stderr}`); return done(new Error(stderr)); } - + const tmp_txt = path.join(__dirname, `./inputs/tmp.txt`); + const short_fileneme = fs.promises.readFile(tmp_txt, 'utf8'); + files.push(short_fileneme); index++; setTimeout(executeNext, 2000); // 2-second delay before processing the next file }); @@ -53,6 +65,7 @@ describe("File generation test", function () { generateFilesForAll(filenames.map(file => path.join(passportDir, file)), done); }); + let counter = 0; filenames.forEach(filename => { // it("Verification passport test", async function () { @@ -89,9 +102,10 @@ describe("File generation test", function () { it("Register identity test", async function () { const tmp_txt = path.join(__dirname, `./inputs/tmp.txt`); - const short_fileneme = await fs.promises.readFile(tmp_txt, 'utf8'); - + const short_filenemes = await fs.promises.readFile(tmp_txt, 'utf8'); + let short_fileneme = short_filenemes.split("\n")[counter]; + console.log("Executing " + short_fileneme + ".circom"); const testJson = path.join(__dirname, `./inputs/generated/input_${short_fileneme}_2.dev.json`); try { @@ -118,6 +132,7 @@ describe("File generation test", function () { console.error('Error:', err); throw err; } + counter+=1; }); }); diff --git a/tests/tests/circuits/identityManagement/registerIdentity_1_256_3_4_600_248_1_1496_3_256.circom b/tests/tests/circuits/identityManagement/registerIdentity_1_256_3_4_600_248_1_1496_3_256.circom deleted file mode 100644 index 0e26e96..0000000 --- a/tests/tests/circuits/identityManagement/registerIdentity_1_256_3_4_600_248_1_1496_3_256.circom +++ /dev/null @@ -1,16 +0,0 @@ -pragma circom 2.1.6; - -include "../../../../circuits/identityManagement/circuits/registerIdentityBuilder.circom"; - -component main { public [slaveMerkleRoot] } = RegisterIdentityBuilder( - 1, //sig_algo - 256, //dg hash algo - 3, //document type - 4, //encapsulated content len in blocks - 600, //encapsulated content shift in bits - 248, //dg1 shift in bits - 1, //dg15 sig algo (0 if not present) - 1496, //dg15 shift in bits - 3, //dg15 blocks - 256 //AA shift in bits -); \ No newline at end of file diff --git a/tests/tests/circuits/identityManagement/registerIdentity_21_256_1_7_576_248_21_3056_6_2008.circom b/tests/tests/circuits/identityManagement/registerIdentity_21_256_1_7_576_248_21_3056_6_2008.circom deleted file mode 100644 index 702857c..0000000 --- a/tests/tests/circuits/identityManagement/registerIdentity_21_256_1_7_576_248_21_3056_6_2008.circom +++ /dev/null @@ -1,16 +0,0 @@ -pragma circom 2.1.6; - -include "../../../../circuits/identityManagement/circuits/registerIdentityBuilder.circom"; - -component main { public [slaveMerkleRoot] } = RegisterIdentityBuilder( - 21, //sig_algo - 256, //dg hash algo - 1, //document type - 7, //encapsulated content len in blocks - 576, //encapsulated content shift in bits - 248, //dg1 shift in bits - 21, //dg15 sig algo (0 if not present) - 3056, //dg15 shift in bits - 6, //dg15 blocks - 2008 //AA shift in bits -); \ No newline at end of file diff --git a/tests/tests/circuits/identityManagement/registerIdentity_24_256_3_4_336_232_NA.circom b/tests/tests/circuits/identityManagement/registerIdentity_24_256_3_4_336_232_NA.circom deleted file mode 100644 index 2de06b9..0000000 --- a/tests/tests/circuits/identityManagement/registerIdentity_24_256_3_4_336_232_NA.circom +++ /dev/null @@ -1,16 +0,0 @@ -pragma circom 2.1.6; - -include "../../../../circuits/identityManagement/circuits/registerIdentityBuilder.circom"; - -component main { public [slaveMerkleRoot] } = RegisterIdentityBuilder( - 24, //sig_algo - 256, //dg hash algo - 3, //document type - 4, //encapsulated content len in blocks - 336, //encapsulated content shift in bits - 232, //dg1 shift in bits - 0, //dg15 sig algo (0 if not present) - 0, //dg15 shift in bits - 0, //dg15 blocks - 0 //AA shift in bits -); \ No newline at end of file diff --git a/tests/tests/circuits/identityManagement/registerIdentity_14_256_3_3_576_248_1_1184_3_256.circom b/tests/tests/circuits/identityManagement/registerIdentity_4_160_3_3_336_216_1_1296_3_256.circom similarity index 70% rename from tests/tests/circuits/identityManagement/registerIdentity_14_256_3_3_576_248_1_1184_3_256.circom rename to tests/tests/circuits/identityManagement/registerIdentity_4_160_3_3_336_216_1_1296_3_256.circom index bdb6111..1b40281 100644 --- a/tests/tests/circuits/identityManagement/registerIdentity_14_256_3_3_576_248_1_1184_3_256.circom +++ b/tests/tests/circuits/identityManagement/registerIdentity_4_160_3_3_336_216_1_1296_3_256.circom @@ -3,14 +3,14 @@ pragma circom 2.1.6; include "../../../../circuits/identityManagement/circuits/registerIdentityBuilder.circom"; component main { public [slaveMerkleRoot] } = RegisterIdentityBuilder( - 14, //sig_algo - 256, //dg hash algo + 4, //sig_algo + 160, //dg hash algo 3, //document type 3, //encapsulated content len in blocks - 576, //encapsulated content shift in bits - 248, //dg1 shift in bits + 336, //encapsulated content shift in bits + 216, //dg1 shift in bits 1, //dg15 sig algo (0 if not present) - 1184, //dg15 shift in bits + 1296, //dg15 shift in bits 3, //dg15 blocks 256 //AA shift in bits ); \ No newline at end of file diff --git a/tests/tests/circuits/passportVerification/passportVerification_1_256_3_4_600_248_1_1496_3_256.circom b/tests/tests/circuits/passportVerification/passportVerification_1_256_3_4_600_248_1_1496_3_256.circom deleted file mode 100644 index 3983e24..0000000 --- a/tests/tests/circuits/passportVerification/passportVerification_1_256_3_4_600_248_1_1496_3_256.circom +++ /dev/null @@ -1,17 +0,0 @@ -pragma circom 2.1.6; - -include "../../../../circuits/passportVerification/passportVerificationBuilder.circom"; - -component main { public [slaveMerkleRoot] } = PassportVerificationBuilder( - 8, //dg15 chunk number - 8, //encapsulated content chunk number - 1, //sig_algo - 256, //dg hash algo - 4, //encapsulated content len in blocks - 600, ///encapsulated content shift in bits - 248, //dg1 shift in bits - 1, //dg15 sig algo (0 if not present) - 1496, //dg15 shift in bits - 3, //dg15 blocks - 256 //AA shift in bits -); \ No newline at end of file diff --git a/tests/tests/circuits/passportVerification/passportVerification_21_256_1_7_576_248_21_3056_6_2008.circom b/tests/tests/circuits/passportVerification/passportVerification_21_256_1_7_576_248_21_3056_6_2008.circom deleted file mode 100644 index bcb4510..0000000 --- a/tests/tests/circuits/passportVerification/passportVerification_21_256_1_7_576_248_21_3056_6_2008.circom +++ /dev/null @@ -1,17 +0,0 @@ -pragma circom 2.1.6; - -include "../../../../circuits/passportVerification/passportVerificationBuilder.circom"; - -component main { public [slaveMerkleRoot] } = PassportVerificationBuilder( - 8, //dg15 chunk number - 8, //encapsulated content chunk number - 21, //sig_algo - 256, //dg hash algo - 7, //encapsulated content len in blocks - 576, ///encapsulated content shift in bits - 248, //dg1 shift in bits - 21, //dg15 sig algo (0 if not present) - 3056, //dg15 shift in bits - 6, //dg15 blocks - 2008 //AA shift in bits -); \ No newline at end of file diff --git a/tests/tests/circuits/passportVerification/passportVerification_24_256_3_4_336_232_NA.circom b/tests/tests/circuits/passportVerification/passportVerification_24_256_3_4_336_232_NA.circom deleted file mode 100644 index d61530a..0000000 --- a/tests/tests/circuits/passportVerification/passportVerification_24_256_3_4_336_232_NA.circom +++ /dev/null @@ -1,17 +0,0 @@ -pragma circom 2.1.6; - -include "../../../../circuits/passportVerification/passportVerificationBuilder.circom"; - -component main { public [slaveMerkleRoot] } = PassportVerificationBuilder( - 8, //dg15 chunk number - 8, //encapsulated content chunk number - 24, //sig_algo - 256, //dg hash algo - 4, //encapsulated content len in blocks - 336, ///encapsulated content shift in bits - 232, //dg1 shift in bits - 0, //dg15 sig algo (0 if not present) - 0, //dg15 shift in bits - 0, //dg15 blocks - 0 //AA shift in bits -); \ No newline at end of file diff --git a/tests/tests/circuits/passportVerification/passportVerification_14_256_3_3_576_248_1_1184_3_256.circom b/tests/tests/circuits/passportVerification/passportVerification_4_160_3_3_336_216_1_1296_3_256.circom similarity index 72% rename from tests/tests/circuits/passportVerification/passportVerification_14_256_3_3_576_248_1_1184_3_256.circom rename to tests/tests/circuits/passportVerification/passportVerification_4_160_3_3_336_216_1_1296_3_256.circom index 854b243..e94c1d8 100644 --- a/tests/tests/circuits/passportVerification/passportVerification_14_256_3_3_576_248_1_1184_3_256.circom +++ b/tests/tests/circuits/passportVerification/passportVerification_4_160_3_3_336_216_1_1296_3_256.circom @@ -5,13 +5,13 @@ include "../../../../circuits/passportVerification/passportVerificationBuilder.c component main { public [slaveMerkleRoot] } = PassportVerificationBuilder( 8, //dg15 chunk number 8, //encapsulated content chunk number - 14, //sig_algo - 256, //dg hash algo + 4, //sig_algo + 160, //dg hash algo 3, //encapsulated content len in blocks - 576, ///encapsulated content shift in bits - 248, //dg1 shift in bits + 336, ///encapsulated content shift in bits + 216, //dg1 shift in bits 1, //dg15 sig algo (0 if not present) - 1184, //dg15 shift in bits + 1296, //dg15 shift in bits 3, //dg15 blocks 256 //AA shift in bits ); \ No newline at end of file diff --git a/tests/tests/process_passport.py b/tests/tests/process_passport.py index fc746bb..e43bea4 100644 --- a/tests/tests/process_passport.py +++ b/tests/tests/process_passport.py @@ -10,6 +10,8 @@ from utils import * def get_new_sig_type(sig_type, salt, e_bits): + if sig_type == 8: + sig_type = 3 if sig_type == 7: sig_type = 21 if sig_type == 6: @@ -24,6 +26,8 @@ def get_new_sig_type(sig_type, salt, e_bits): sig_type = 13 if sig_type == 4: sig_type = 14 + if e_bits == 37187: + sig_type = 4 return sig_type def get_AA_shift_and_pubkey(dg15_hex, dg15_sig_algo, dg15_base64): @@ -42,7 +46,6 @@ def get_AA_shift_and_pubkey(dg15_hex, dg15_sig_algo, dg15_base64): file.write(dg15_bytes) decoded = parse_asn1("temp_asn1.der") - print(decoded) pos = 0 hl= 0 for line in decoded.split('\n'): @@ -118,23 +121,31 @@ def get_sa(sod_hex, sa_locations, chunk_size): n = sa_locations[-3][0] l = sa_locations[-3][1] sa = "31" + sod_hex[n*2+2:n*2+l*2] + if (len(sa) > 2000): + n = sa_locations[-1][0] + l = sa_locations[-1][1] + sa = "31" + sod_hex[n*2+2:n*2+l*2] _, sa_padded, _ = process_and_pad_hex(sa, chunk_size) sa_res = format_bit_string(sa_padded) + print(sa) + return sa, sa_res def process_sa(asn1_data): lines = asn1_data.split('\n') + print(asn1_data) cont_lines = [line for line in lines if 'cont [ 0 ]' in line] sa_locations = [] for line in cont_lines: filtered_list = [s for s in line.split("l=")[2].split(" ") if s] sa_locations.append([int(line.split(":")[0]), int(filtered_list[0]) + 2]) + print(sa_locations) return sa_locations def process_pubkey(asn1_data): lines = asn1_data.split('\n') - # print(asn1_data)s + # print(asn1_data) pubkey_ecdsa_location = 0 @@ -164,7 +175,7 @@ def process_algo(asn1_data): lines = asn1_data.split('\n') hash_locations = [line for line in lines if 'sha' in str.lower(line)] - hash_type = int(str.lower(hash_locations[-1]).split("sha")[1][:3]) + hash_type = int(str.lower(hash_locations[-1]).split("sha")[1][:3].replace("1wi", "160")) chunk_size = 512 if hash_type <= 256 else 1024 return hash_type, chunk_size @@ -248,7 +259,11 @@ def get_sig_algo(sod_hex, salt, signature, hash_algo): return 3 if hash_algo == 256 else 5 return 4 if (len(signature) == 512): + if hash_algo == 160: + return 8 return 1 + if len(signature) == 768: + return 9 return 2 def get_ecdsa_params(sod_hex, pubkey_ecdsa_location, signature): @@ -307,12 +322,13 @@ def get_rsa_3072_rsa_pss_params(sod_hex, rsa_pubkey_location, rsa_pubkey_len, si signature_arr = bigint_to_array(64, 48, int(signature, 16)) chunk_num = 48 - print(sod_hex) pubkey = sod_hex[rsa_pubkey_location * 2 -1: rsa_pubkey_location *2 + rsa_pubkey_len*2 + 2].split("82018100")[1][0:768] - print(pubkey) pubkey_arr = bigint_to_array(64, chunk_num, int(pubkey, 16)) - - e_bits = 17 + print(sod_hex[rsa_pubkey_location *2 + rsa_pubkey_len*2 + 2: rsa_pubkey_location *2 + rsa_pubkey_len*2 + 8]) + if (sod_hex[rsa_pubkey_location *2 + rsa_pubkey_len*2 + 2: rsa_pubkey_location *2 + rsa_pubkey_len*2 + 8] == "009143"): + e_bits = 37187 + else: + e_bits = 17 pk_hash = hash_pk_rsa(chunk_num, pubkey) @@ -338,8 +354,6 @@ def get_shifts(dg1_hex, dg15_hex, ec_hex, dg_hash_algo, hash_algo, sa_hex): dg1_hash = "" dg15_hash = "" ec_hash = "" - print(dg_hash_algo) - print(hash_algo) dg1_shift = 0 dg15_shift = dg_hash_algo ec_shift = 0 @@ -440,7 +454,7 @@ def process_passport(file_path): signature_arr = [] chunk_number = 0 e_bits = 0 - + print(sig_algo) if sig_algo == 6 or sig_algo == 7: pubkey_arr, signature_arr, chunk_number, pk_hash = get_ecdsa_params(sod_hex, pubkey_ecdsa_location, signature) @@ -450,10 +464,10 @@ def process_passport(file_path): if sig_algo == 2: pubkey_arr, signature_arr, chunk_number, e_bits, pk_hash = get_rsa_4096_params(sod_hex, rsa_pubkey_location, rsa_pubkey_len, signature) - if sig_algo == 4: + if sig_algo == 4 or sig_algo == 9: pubkey_arr, signature_arr, chunk_number, e_bits, pk_hash = get_rsa_3072_rsa_pss_params(sod_hex, rsa_pubkey_location, rsa_pubkey_len, signature) - if sig_algo == 1 or sig_algo == 3 or sig_algo == 5: + if sig_algo == 1 or sig_algo == 3 or sig_algo == 5 or sig_algo == 8: pubkey_arr, signature_arr, chunk_number, e_bits, pk_hash = get_rsa_2048_rsa_pss_params(sod_hex, rsa_pubkey_location, rsa_pubkey_len, signature) dg1_shift, dg15_shift, ec_shift = get_shifts(dg1_hex, dg15_hex, ec_hex, dg_hash_algo, hash_algo, sa_hex) @@ -469,7 +483,6 @@ def process_passport(file_path): short_file_path = get_short_file_path(file_path) sig_algo = get_new_sig_type(sig_algo, salt, e_bits) - if isdg15 == 0: dg15_shift = 0 @@ -480,8 +493,6 @@ def process_passport(file_path): AA_pubkey = "" if isdg15!=0: (AA_shift, AA_pubkey) = get_AA_shift_and_pubkey(dg15_hex, isdg15, dg15_base64) - print(AA_shift) - print(AA_pubkey) real_circuit_name = "" if isdg15 == 0: @@ -489,9 +500,6 @@ def process_passport(file_path): else: real_circuit_name = "registerIdentity_"+ str(sig_algo) + "_" + str(dg_hash_algo) + "_" + str(document_type) + "_" + str(ec_blocks) + "_" + str(ec_shift) + "_" + str(dg1_shift) + "_" + str(isdg15) + "_" + str(dg15_shift) + "_" + str(dg15_blocks) + "_" + str(AA_shift) - - - write_tmp_to_file(real_circuit_name) write_results_to_register_identity(sig_algo, dg_hash_algo, document_type, dg1_shift, dg15_shift, ec_shift, dg15_blocks, ec_blocks, isdg15, AA_shift, real_circuit_name) diff --git a/tests/tests/write_to_files.py b/tests/tests/write_to_files.py index 9e6035d..1d0cf6c 100644 --- a/tests/tests/write_to_files.py +++ b/tests/tests/write_to_files.py @@ -73,5 +73,5 @@ def write_to_json(dg1_res, dg15_res, sa_res, ec_res, pubkey_arr, signature_arr, }, f_out, indent=4) def write_tmp_to_file(real_name): - with open('./tests/tests/inputs/tmp.txt', 'w') as file: - file.write(real_name) \ No newline at end of file + with open('./tests/tests/inputs/tmp.txt', 'a') as file: + file.write(real_name+"\n") \ No newline at end of file