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
- prefix which is an enum of variants, S, T, G, F and M,
- suffix, again an enum of some alphabets as its variants,
- 7 digits
The steps to validate a NRIC is as follows:
- Multiply each digit in the NRIC number by its weight i.e. 2 7 6 5 4 3 2 in order.
- Add together the above products.
- If the prefix of the NRIC is T or G, add 4 to the total.
- Find the remainder of (sum calculated above) mod 11.
- 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
- 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
- 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?