iden3 / circom_old

Circuit compiler for zkSNARKs
GNU General Public License v3.0
472 stars 82 forks source link

Array bound checking in component signals #60

Open ed255 opened 4 years ago

ed255 commented 4 years ago

The following circuit makes circom crash instead of detecting an out of bounds array access attempt in f.out.

template foo() {
        signal input in[2];
        signal output out[2];
}

template test() {
        signal input a[2];
        signal b[2];

        component f = foo();
        for (var i=0; i<2; i++) { f.in[i] <== a[i]; }

        for (var i=0; i<4; i++) { b[i] <== f.out[i]; }
}

component main = test();

Tested on circom 0.5.11, error:

$ ./node_modules/.bin/circom test1.circom
TypeError: Cannot read property 'o' of undefined
    at joinSignals (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:424:27)
    at execAssignement (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:395:17)
    at execSignalAssignConstrain (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:942:15)
    at exec (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:111:20)
    at execBlock (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:535:9)
    at exec (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:192:16)
    at execLoop (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:746:9)
    at exec (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:196:16)
    at execBlock (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:535:9)
    at instantiateComponent (/root/tmp/circom-test/node_modules/circom/src/construction_phase.js:522:9)
Cannot read property 'o' of undefined

If the out of bounds error is from a signal inside the component, the proper error is reported:

template test() {
        signal input a[2];
        signal b[2];

        for (var i=0; i<4; i++) { b[i] <== a[i]; }
}

component main = test();

Tested on circom 0.5.11, result:

$ ./node_modules/.bin/circom test2.circom
undefined
ERROR at /root/tmp/circom-test/test2.circom:5,36-5,40   Out of Range
{
 "type": "VARIABLE",
 "name": "a",
 "selectors": [
  {
   "type": "VARIABLE",
   "name": "i",
   "selectors": [],
   "first_line": 5,
   "first_column": 38,
   "last_line": 5,
   "last_column": 39,
   "refId": 2
  }
 ],
 "first_line": 5,
 "first_column": 36,
 "last_line": 5,
 "last_column": 40,
 "refId": 0
}