fix: sha256 signed attr tests (#1058)

This commit is contained in:
Nesopie
2025-09-12 08:32:45 +05:30
committed by GitHub
parent 00a09670e1
commit 36ffe36bbd
6 changed files with 87 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -65,6 +65,7 @@ export {
genMockIdDocAndInitDataParsing,
generateCircuitInputsDSC,
generateCircuitInputsRegister,
generateCircuitInputsRegisterForTests,
generateCircuitInputsVCandDisclose,
generateCommitment,
generateMockDSC,

View File

@@ -131,6 +131,14 @@ export const MAX_PADDED_ECONTENT_LEN: Partial<Record<(typeof hashAlgos)[number],
sha512: 896,
};
export const MAX_PADDED_SIGNED_ATTR_LEN_FOR_TESTS: Record<(typeof hashAlgos)[number], number> = {
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,

View File

@@ -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,

View File

@@ -54,6 +54,7 @@ export {
export {
generateCircuitInputsDSC,
generateCircuitInputsRegister,
generateCircuitInputsRegisterForTests,
generateCircuitInputsVCandDisclose,
} from './circuits/generateInputs.js';
export { getCircuitNameFromPassportData } from './circuits/circuitsName.js';