Skip to content

Commit

Permalink
implement formatInput() in generateInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
remicolin committed Sep 20, 2024
1 parent 6e12d57 commit a79a46d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
12 changes: 9 additions & 3 deletions circuits/circuits/prove/openpassport_prove.circom
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ template OPENPASSPORT_PROVE(signatureAlgorithm, n, k, MAX_ECONTENT_PADDED_LEN, M
signal input signed_attr_econtent_hash_offset;
signal input pubKey[kScaled];
signal input signature[kScaled];
// diclose related inputs
signal input selector_mode; // 0 - disclose, 1 - registration
// disclose related inputs
signal input selector_dg1[88];
signal input selector_older_than;
signal input current_date[6]; // YYMMDD - num
Expand All @@ -36,6 +37,9 @@ template OPENPASSPORT_PROVE(signatureAlgorithm, n, k, MAX_ECONTENT_PADDED_LEN, M

signal attestation_id <== 1;

// assert selector_mode is 0 or 1
selector_mode * (selector_mode - 1) === 0;

// verify passport signature
PassportVerifier(signatureAlgorithm, n, k, MAX_ECONTENT_PADDED_LEN, MAX_SIGNED_ATTR_PADDED_LEN)(dg1,dg1_hash_offset, dg2_hash, eContent,eContent_padded_length, signed_attr, signed_attr_padded_length, signed_attr_econtent_hash_offset, pubKey, signature);

Expand All @@ -57,9 +61,11 @@ template OPENPASSPORT_PROVE(signatureAlgorithm, n, k, MAX_ECONTENT_PADDED_LEN, M
// REGISTRATION (optional)
// generate the commitment
signal leaf <== LeafHasher(kScaled)(pubKey, signatureAlgorithm);
signal output commitment <== ComputeCommitment()(secret, attestation_id, leaf, dg1, dg2_hash);
signal commitmentPrivate <== ComputeCommitment()(secret, attestation_id, leaf, dg1, dg2_hash);
signal output commitment <== commitmentPrivate * selector_mode;
// blinded dsc commitment
signal pubkeyHash <== CustomHasher(kScaled)(pubKey);
signal output blinded_dsc_commitment <== Poseidon(2)([dsc_secret, pubkeyHash]);
signal blindedDscCommitmenPrivate <== Poseidon(2)([dsc_secret, pubkeyHash]);
signal output blinded_dsc_commitment <== blindedDscCommitmenPrivate * selector_mode;

}
8 changes: 6 additions & 2 deletions circuits/tests/prove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ sigAlgs.forEach(({ sigAlg, hashFunction }) => {
const scope = '@coboyApp';
const selector_dg1 = Array(88).fill('1');
const selector_older_than = '1';
const secret = 0;
const dsc_secret = 0;
const selector_mode = 1;

const inputs = generateCircuitInputsProve(
BigInt(0).toString(),
BigInt(0).toString(),
selector_mode,
secret,
dsc_secret,
passportData,
scope,
selector_dg1,
Expand Down
46 changes: 27 additions & 19 deletions common/src/utils/generateInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ export function findIndexInTree(tree: LeanIMT, commitment: bigint): number {


export function generateCircuitInputsProve(
secret: string,
dsc_secret: string,
selector_mode: number | string,
secret: number | string,
dsc_secret: number | string,
passportData: PassportData,
scope: string,
selector_dg1: string[],
Expand Down Expand Up @@ -173,7 +174,6 @@ export function generateCircuitInputsProve(
)
}

const dg1 = formatMrz(mrz);
const formattedMrz = formatMrz(mrz);
const dg1Hash = hash(hashFunction, formattedMrz);

Expand Down Expand Up @@ -202,28 +202,36 @@ export function generateCircuitInputsProve(
MAX_PADDED_SIGNED_ATTR_LEN[signatureAlgorithmFullName]
);

const current_date = getCurrentDateYYMMDD().map(datePart => BigInt(datePart).toString());
// Ensure majority is at least two digits
const formattedMajority = majority.length === 1 ? `0${majority}` : majority;
const majority_ascii = formattedMajority.split('').map(char => char.charCodeAt(0))
return {
dg1: dg1.map(byte => String(byte)),
dg1_hash_offset: [dg1HashOffset.toString()], // uncomment when adding new circuits
selector_mode: formatInput(selector_mode),
dg1: formatInput(formattedMrz),
dg1_hash_offset: formatInput(dg1HashOffset),
dg2_hash: formatDg2Hash(dg2Hash),
eContent: Array.from(eContentPadded).map((x) => x.toString()),
eContent_padded_length: [eContentLen.toString()],
eContent_padded_length: formatInput(eContentLen),
signed_attr: Array.from(signedAttrPadded).map((x) => x.toString()),
signed_attr_padded_length: [signedAttrPaddedLen.toString()],
signed_attr_econtent_hash_offset: [eContentHashOffset.toString()],
signed_attr_padded_length: formatInput(signedAttrPaddedLen),
signed_attr_econtent_hash_offset: formatInput(eContentHashOffset),
signature: signature,
pubKey: pubKey,
current_date: current_date,
selector_dg1: selector_dg1,
selector_older_than: [BigInt(selector_older_than).toString()],
majority: formattedMajority.split('').map(char => BigInt(char.charCodeAt(0)).toString()),
user_identifier: [parseUIDToBigInt(user_identifier, user_identifier_type)],
scope: [castFromScope(scope)],
secret: [secret],
dsc_secret: [dsc_secret],
current_date: formatInput(getCurrentDateYYMMDD()),
selector_dg1: formatInput(selector_dg1),
selector_older_than: formatInput(selector_older_than),
majority: formatInput(majority_ascii),
user_identifier: formatInput(parseUIDToBigInt(user_identifier, user_identifier_type)),
scope: formatInput(castFromScope(scope)),
secret: formatInput(secret),
dsc_secret: formatInput(dsc_secret),
};

}
}

function formatInput(input: any) {
if (Array.isArray(input)) {
return input.map(item => BigInt(item).toString());
} else {
return [BigInt(input).toString()];
}
}

0 comments on commit a79a46d

Please sign in to comment.