Skip to content

Commit

Permalink
feat: Added tests for invaild mrz, signature and econtent.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVikasRushi committed Jul 28, 2024
1 parent 33fa8c7 commit adf661f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
78 changes: 78 additions & 0 deletions circuits/tests/register/register_sha1_ecdsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ describe('Register - SHA1 WITH ECDSA', function () {
k_dsc
);

let qx = BigInt(hexToDecimal(inputs.dsc_modulus[0]));
let qy = BigInt(hexToDecimal(inputs.dsc_modulus[1]));
let dsc_modulus = [BigintToArray(43, 6, qx), BigintToArray(43, 6, qy)];

let signature = inputs.signature;
let { r, s } = extractRSFromSignature(signature);
let signature_r = BigintToArray(43, 6, BigInt(hexToDecimal(r)));
let signature_s = BigintToArray(43, 6, BigInt(hexToDecimal(s)));

before(async () => {
circuit = await wasm_tester(
path.join(__dirname, '../../circuits/register/register_ecdsaWithSHA1Encryption.circom'),
Expand Down Expand Up @@ -105,4 +114,73 @@ describe('Register - SHA1 WITH ECDSA', function () {
const commitment_js = commitment_bytes.toString();
expect(commitment_circom).to.be.equal(commitment_js);
});

it('should fail to calculate witness with invalid econtent', async function () {
try {
const invalidInputs = {
secret: inputs.secret,
mrz: inputs.mrz,
dg1_hash_offset: inputs.dg1_hash_offset[0],
econtent: inputs.econtent.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)),
datahashes_padded_length: inputs.datahashes_padded_length[0],
signed_attributes: inputs.signed_attributes,
signature_r: signature_r,
signature_s: signature_s,
dsc_modulus: dsc_modulus,
dsc_secret: inputs.dsc_secret,
attestation_id: inputs.attestation_id,
};
await circuit.calculateWitness(invalidInputs);
expect.fail('Expected an error but none was thrown.');
} catch (error) {
expect(error.message).to.include('Assert Failed');
}
});

it('should fail to calculate witness with invalid mrz', async function () {
try {
const invalidInputs = {
secret: inputs.secret,
mrz: Array(93)
.fill(0)
.map((byte) => BigInt(byte).toString()),
dg1_hash_offset: inputs.dg1_hash_offset[0],
econtent: inputs.econtent,
datahashes_padded_length: inputs.datahashes_padded_length[0],
signed_attributes: inputs.signed_attributes,
signature_r: signature_r,
signature_s: signature_s,
dsc_modulus: dsc_modulus,
dsc_secret: inputs.dsc_secret,
attestation_id: inputs.attestation_id,
};
await circuit.calculateWitness(invalidInputs);
expect.fail('Expected an error but none was thrown.');
} catch (error) {
expect(error.message).to.include('Assert Failed');
}
});

it('should fail to calculate witness with invalid signature', async function () {
let wrong_signature_s = BigintToArray(43, 6, BigInt(hexToDecimal(s) + 1));
try {
const invalidInputs = {
secret: inputs.secret,
mrz: inputs.mrz,
dg1_hash_offset: inputs.dg1_hash_offset[0],
econtent: inputs.econtent,
datahashes_padded_length: inputs.datahashes_padded_length[0],
signed_attributes: inputs.signed_attributes,
signature_r: signature_r,
signature_s: wrong_signature_s,
dsc_modulus: dsc_modulus,
dsc_secret: inputs.dsc_secret,
attestation_id: inputs.attestation_id,
};
await circuit.calculateWitness(invalidInputs);
expect.fail('Expected an error but none was thrown.');
} catch (error) {
expect(error.message).to.include('Assert Failed');
}
});
});
5 changes: 0 additions & 5 deletions common/src/utils/pubkeyTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ export function getLeaf(pubkey: any, i?: number): bigint {
sigAlgFormattedForCircuit === 'ecdsa_with_SHA512'
) {
try {
// this will be replaced by just X and Y or pubkey in publicKeyQ

if (!pubkey.publicKeyQ) {
throw new Error('publicKeyQ is undefined');
}
Expand All @@ -79,12 +77,9 @@ export function getLeaf(pubkey: any, i?: number): bigint {
let qx = BigintToArray(43, 6, BigInt(hexToDecimal(x)));
let qy = BigintToArray(43, 6, BigInt(hexToDecimal(y)));

// bigint_to_array();
let poseidon_hasher_dsc_modules_x = poseidon6(qx);
let poseidon_hasher_dsc_modules_y = poseidon6(qy);
console.log(SignatureAlgorithm[sigAlgFormattedForCircuit], 's');

// ! @TODO check if this is correct
return poseidon3([
SignatureAlgorithm[sigAlgFormattedForCircuit],
poseidon_hasher_dsc_modules_x, // pub.x
Expand Down

0 comments on commit adf661f

Please sign in to comment.