Closed shreyas-londhe closed 4 days ago
This is the naive implementation:
/**
* Selects an element from an array based on an index.
* @param arr The array to select from.
* @param index The index of the element to select.
* @returns The selected element.
*/
export const quinSelector = (arr: Field[], index: Field) => {
index.lessThan(Field(arr.length)).assertTrue();
let out = [];
for (let i = 0; i < arr.length; i++) {
out.push(arr[i].mul(index.equals(Field(i)).toField()));
}
let res = Field(0);
for (let i = 0; i < arr.length; i++) {
res = res.add(out[i]);
}
return res;
};
Yep, I have used the same pattern. I don't think there's anything better without lookup based array access (which will come to snarkyjs within the next months)
closing in favor of #1086
Hi! I was looking for a way to do array access using
Field
elements, but could not find any documentation for it. I would be more than happy to implement a naivequin_selector
-- circom implementation for the time being, but I believe that's not a long term solution.Any help is much appreciated!