Don't loop over an array without an index

Utilizing encrypted indexes to select an element from an array without disclosing it is inefficient. This approach requires looping over all indexes and comparing for equality secretly, which significantly increases gas costs and should be avoided whenever possible.


contract AvoidContract{
    ctUint32 data;
    ctUint32[] secretArray;
    function setDataAtSecretIndex(gtUint index) public {
        gtUint32 gtData=0;
        for (uint32 i = 0; i < secretArray.length; i++) {
            gtAti = MpcCore.onboard(secretArray[i])
            if (i==0){
                gtData = gtAti;
            gtBool isEqual = MpcCore.eq(index, i);
            gtData = MpcCore.mux(isEqual, gtAti , gtData);
            data = MpcCore.offboard(gtData)

Do : You should aim for clear indexing when accessing secret arrays.

Last updated