Writing a Singaporean NRIC Validator in Circom

125 Views Asked by At

I am intending to write a Circom circuit to validate a Singaporean NRIC, details on the validation checksum algorithm is found here.

In brief, it is a string that contains

  1. prefix which is an enum of variants, S, T, G, F and M,
  2. suffix, again an enum of some alphabets as its variants,
  3. 7 digits

The steps to validate a NRIC is as follows:

  1. Multiply each digit in the NRIC number by its weight i.e. 2 7 6 5 4 3 2 in order.
  2. Add together the above products.
  3. If the prefix of the NRIC is T or G, add 4 to the total.
  4. Find the remainder of (sum calculated above) mod 11.
  5. If the prefix of the NRIC is F or G: 0=X, 1=W, 2=U, 3=T, 4=R, 5=Q, 6=P, 7=N, 8=M, 9=L, 10=K
  6. If the prefix of the NRIC is S or T: 0=J, 1=Z, 2=I, 3=H, 4=G, 5=F, 6=E, 7=D, 8=C, 9=B, 10=A
  7. Valid if reminder == number that the prefix represents.

Here is my circom circuit:

pragma circom 2.1.5;

template NRICValidator() {
  signal input prefix; // 1 or 2
  signal input suffix; // 3 or 4
  signal input digits[7];
  var sumWithOffset;
  var weights[5] = [2, 7, 6, 5, 4, 3, 2];
  var checkDigits[11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  var offset = 4;

  var weightedSum = digits[0]*weights[0] + digits[1]*weights[1] + digits[2]*weights[2] + digits[3]*weights[3] + digits[4]*weights[4] + digits[5]*weights[5] + digits[6]*weights[6];
  if (prefix == 1 && prefix == 2) { // T or G
    var sumWithOffset = weightedSum + offset;
  } else {
    var sumWithOffset = weightedSum; // S or F
  }
  
  signal output valid;
  sumWithOffset %= 11; // <=== a number between 0 to 10
  valid <== (checkDigits[sumWithOffset] == suffix); // <=== Error: Non-quadratic constraint was detected statically, using unknown index will cause the constraint to be non-quadratic
}

component main = NRICValidator();

I'd tried compiling it and the error that I got is:

error[T20462]: Typing error found
   ┌─ "main.circom":21:14
   │
21 │   valid <== (checkDigits[sumWithOffset] == suffix); // <=== Error: Non-quadratic constraint was detected statically, using unknown index will cause the constraint to be non-quadratic
   │              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Non-quadratic constraint was detected statically, using unknown index will cause the constraint to be non-quadratic

previous errors were found

Is there any solution to circumvent this?

0

There are 0 best solutions below