Search code examples
arrayscircuitcircomzkpzk-snark

How to access array element with an "Unknown" index in Circom?


I have the following Circom (circuit compiler language) program:

pragma circom 2.0.0;


template MAIN() {

    signal input array[2512];
    signal output d;

    signal v;
    v <== 168;

    d <== array[v];
}

component main = MAIN();

I want to access an array element at an arbitrary index and return it as a signal.

I'm getting the following error:

   ┌─ "/Users/ilia/compiling/main-circom/main.circom":85:11
   │
85 │     d <== array[v];
   │           ^^^^^^^^ Non-quadratic constraint was detected statically, using unknown index will cause the constraint to be non-quadratic

How can I access an array element with an "Unknown" index?

What I am trying to build is a program which takes in an array of bytes, seeks to a specific index and then takes SHA256 hash of array[index:]. I don't know what the index will be, the index will be computed inside the program based on the contents of the array.

I've asked my previous Circom question on crypto.stackexchange.com, but got directed here.


Solution

  • I can use QuinSelector like so:

        component quinSelector = QuinSelector(2512);
        for (k=0; k< 2512; k++) {
            quinSelector.in[k] <== array[k];
        }
        quinSelector.index <== v;
        d <== quinSelector.out;