diff --git a/circuits/tests/register/register.test.ts b/circuits/tests/register/register.test.ts index dfd2dbda7..f4572667e 100644 --- a/circuits/tests/register/register.test.ts +++ b/circuits/tests/register/register.test.ts @@ -8,7 +8,7 @@ import serialized_dsc_tree from '@selfxyz/common/pubkeys/serialized_dsc_tree.jso import { PASSPORT_ATTESTATION_ID } from '@selfxyz/common/constants/constants'; import { parseCertificateSimple } from '@selfxyz/common/utils/certificate_parsing/parseCertificateSimple'; import { getCircuitNameFromPassportData } from '@selfxyz/common/utils/circuits/circuitsName'; -import { generateCircuitInputsRegister } from '@selfxyz/common/utils/circuits/generateInputs'; +import { generateCircuitInputsRegisterForTests } from '@selfxyz/common/utils/circuits/generateInputs'; import { genAndInitMockPassportData } from '@selfxyz/common/utils/passports/genMockPassportData'; import { generateCommitment, generateNullifier } from '@selfxyz/common/utils/passports/passport'; import { SignatureAlgorithm } from '@selfxyz/common/utils/types'; @@ -48,7 +48,7 @@ testSuite.forEach( const secret = poseidon6('SECRET'.split('').map((x) => BigInt(x.charCodeAt(0)))).toString(); - const inputs = generateCircuitInputsRegister( + const inputs = generateCircuitInputsRegisterForTests( secret, passportData, serialized_dsc_tree as string diff --git a/circuits/tests/register_id/register_id.test.ts b/circuits/tests/register_id/register_id.test.ts index 2d2c57554..dfab1d468 100644 --- a/circuits/tests/register_id/register_id.test.ts +++ b/circuits/tests/register_id/register_id.test.ts @@ -3,7 +3,7 @@ import { describe } from 'mocha'; import { expect } from 'chai'; import path from 'path'; import { wasm as wasm_tester } from 'circom_tester'; -import { generateCircuitInputsRegister } from '@selfxyz/common/utils/circuits/generateInputs'; +import { generateCircuitInputsRegisterForTests } from '@selfxyz/common/utils/circuits/generateInputs'; import { SignatureAlgorithm } from '@selfxyz/common/utils/types'; import { getCircuitNameFromPassportData } from '@selfxyz/common/utils/circuits/circuitsName'; import { sigAlgs, fullSigAlgs } from './test_cases.js'; @@ -45,7 +45,7 @@ testSuite.forEach( }); const secret = poseidon6('SECRET'.split('').map((x) => BigInt(x.charCodeAt(0)))).toString(); - const inputs = generateCircuitInputsRegister( + const inputs = generateCircuitInputsRegisterForTests( secret, passportData, serialized_dsc_tree as string diff --git a/common/index.ts b/common/index.ts index add454242..15ef480ca 100644 --- a/common/index.ts +++ b/common/index.ts @@ -65,6 +65,7 @@ export { genMockIdDocAndInitDataParsing, generateCircuitInputsDSC, generateCircuitInputsRegister, + generateCircuitInputsRegisterForTests, generateCircuitInputsVCandDisclose, generateCommitment, generateMockDSC, diff --git a/common/src/constants/constants.ts b/common/src/constants/constants.ts index ae6a93d93..d80134cd8 100644 --- a/common/src/constants/constants.ts +++ b/common/src/constants/constants.ts @@ -131,6 +131,14 @@ export const MAX_PADDED_ECONTENT_LEN: Partial = { + sha1: 128, + sha224: 128, + sha256: 256, + sha384: 256, + sha512: 256, +}; + export const MAX_PADDED_SIGNED_ATTR_LEN: Record<(typeof hashAlgos)[number], number> = { sha1: 128, sha224: 128, diff --git a/common/src/utils/circuits/generateInputs.ts b/common/src/utils/circuits/generateInputs.ts index 0c863db03..35a0d3ab6 100644 --- a/common/src/utils/circuits/generateInputs.ts +++ b/common/src/utils/circuits/generateInputs.ts @@ -4,6 +4,7 @@ import { max_dsc_bytes, MAX_PADDED_ECONTENT_LEN, MAX_PADDED_SIGNED_ATTR_LEN, + MAX_PADDED_SIGNED_ATTR_LEN_FOR_TESTS, OFAC_TREE_LEVELS, } from '../../constants/constants.js'; import { getCurrentDateYYMMDD } from '../date.js'; @@ -200,6 +201,78 @@ export function generateCircuitInputsOfac( }; } +export function generateCircuitInputsRegisterForTests( + secret: string, + passportData: PassportData, + serializedDscTree: string +) { + const { mrz, eContent, signedAttr } = passportData; + const passportMetadata = passportData.passportMetadata; + const dscParsed = passportData.dsc_parsed; + + const [dscTbsBytesPadded] = pad(dscParsed.hashAlgorithm)(dscParsed.tbsBytes, max_dsc_bytes); + + const { pubKey, signature, signatureAlgorithmFullName } = getPassportSignatureInfos(passportData); + const mrz_formatted = formatMrz(mrz); + + if (eContent.length > MAX_PADDED_ECONTENT_LEN[signatureAlgorithmFullName]) { + console.error( + `eContent too long (${eContent.length} bytes). Max length is ${MAX_PADDED_ECONTENT_LEN[signatureAlgorithmFullName]} bytes.` + ); + throw new Error( + `This length of datagroups (${eContent.length} bytes) is currently unsupported. Please contact us so we add support!` + ); + } + + const [eContentPadded, eContentLen] = pad(passportMetadata.eContentHashFunction)( + eContent, + MAX_PADDED_ECONTENT_LEN[passportMetadata.dg1HashFunction] + ); + const [signedAttrPadded, signedAttrPaddedLen] = pad(passportMetadata.signedAttrHashFunction)( + signedAttr, + MAX_PADDED_SIGNED_ATTR_LEN_FOR_TESTS[passportMetadata.eContentHashFunction] + ); + + const dsc_leaf = getLeafDscTree(dscParsed, passportData.csca_parsed); // TODO: WRONG + const [root, path, siblings, leaf_depth] = getDscTreeInclusionProof(dsc_leaf, serializedDscTree); + const csca_tree_leaf = getLeafCscaTree(passportData.csca_parsed); + + // Get start index of DSC pubkey based on algorithm + const [startIndex, keyLength] = findStartPubKeyIndex( + dscParsed, + dscTbsBytesPadded, + dscParsed.signatureAlgorithm + ); + + const inputs = { + raw_dsc: dscTbsBytesPadded.map((x) => x.toString()), + raw_dsc_actual_length: [BigInt(dscParsed.tbsBytes.length).toString()], + dsc_pubKey_offset: startIndex, + dsc_pubKey_actual_size: [BigInt(keyLength).toString()], + dg1: mrz_formatted, + dg1_hash_offset: passportMetadata.dg1HashOffset, + eContent: eContentPadded, + eContent_padded_length: eContentLen, + signed_attr: signedAttrPadded, + signed_attr_padded_length: signedAttrPaddedLen, + signed_attr_econtent_hash_offset: passportMetadata.eContentHashOffset, + pubKey_dsc: pubKey, + signature_passport: signature, + merkle_root: [BigInt(root).toString()], + leaf_depth: leaf_depth, + path: path, + siblings: siblings, + csca_tree_leaf: csca_tree_leaf, + secret: secret, + }; + + return Object.entries(inputs) + .map(([key, value]) => ({ + [key]: formatInput(value), + })) + .reduce((acc, curr) => ({ ...acc, ...curr }), {}); +} + export function generateCircuitInputsRegister( secret: string, passportData: PassportData, diff --git a/common/src/utils/index.ts b/common/src/utils/index.ts index e9fcef915..d408abb1f 100644 --- a/common/src/utils/index.ts +++ b/common/src/utils/index.ts @@ -54,6 +54,7 @@ export { export { generateCircuitInputsDSC, generateCircuitInputsRegister, + generateCircuitInputsRegisterForTests, generateCircuitInputsVCandDisclose, } from './circuits/generateInputs.js'; export { getCircuitNameFromPassportData } from './circuits/circuitsName.js';