Skip to content

Commit

Permalink
Merge pull request #366 from zk-passport/hot-fix-off-by-one
Browse files Browse the repository at this point in the history
hotfix off-by-one issues
  • Loading branch information
0xturboblitz authored Feb 9, 2025
2 parents c5b5ee3 + b825a4e commit 62be8c9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion circuits/circuits/dsc/dsc.circom
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ template DSC(
// this should guarantee the dsc commitment is unique for each commitment
component byte_checks[MAX_DSC_LENGTH];
for (var i = 0; i < MAX_DSC_LENGTH; i++) {
byte_checks[i] = GreaterThan(12);
byte_checks[i] = GreaterEqThan(12);
byte_checks[i].in[0] <== i;
byte_checks[i].in[1] <== raw_dsc_padded_length;

Expand Down
4 changes: 2 additions & 2 deletions circuits/circuits/utils/passport/signatureVerifier.circom
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ template SignatureVerifier(signatureAlgorithm, n, k) {
signal input pubKey[kScaled];
signal input signature[kScaled];

var msg_len = (HASH_LEN_BITS + n) \ n;
var msg_len = (HASH_LEN_BITS + n - 1) \ n;

signal hashParsed[msg_len] <== HashParser(signatureAlgorithm, n, k)(hash);

Expand Down Expand Up @@ -124,7 +124,7 @@ template SignatureVerifier(signatureAlgorithm, n, k) {

template HashParser(signatureAlgorithm, n, k) {
var HASH_LEN_BITS = getHashLength(signatureAlgorithm);
var msg_len = (HASH_LEN_BITS + n) \ n;
var msg_len = (HASH_LEN_BITS + n - 1) \ n;

component hashParser[msg_len];
signal input hash[HASH_LEN_BITS];
Expand Down
13 changes: 13 additions & 0 deletions circuits/tests/dsc/dsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,18 @@ testSuite.forEach(({ sigAlg, hashFunction, domainParameter, keyLength }) => {
expect(error.message).to.include('Assert Failed');
}
});

it('should not allow tampering of raw_dsc[raw_dsc_padded_length]', async () => {
try {
const tamperedInputs = JSON.parse(JSON.stringify(inputs));
const paddedLength = Number(tamperedInputs.raw_dsc_padded_length);
tamperedInputs.raw_dsc[paddedLength] = '255'; // or any nonzero value

await circuit.calculateWitness(tamperedInputs);
expect.fail('Expected an error but none was thrown.');
} catch (error) {
expect(error.message).to.include('Assert Failed');
}
});
});
});

0 comments on commit 62be8c9

Please sign in to comment.