Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 844
Ilia Sidorenko Avatar asked Nov 16 '25 14:11

Ilia Sidorenko


1 Answers

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;
like image 138
Ilia Sidorenko Avatar answered Nov 18 '25 05:11

Ilia Sidorenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!