Assessment reports>Barretenberg Bigfield>Discussion>Unreachable code

Unreachable code

Some parts of the code are not reachable and should be removed.

Unreachable branch in assert_equal function

The assert_equal function has several nested if, else-if, and else statements:

if (is_constant() && other.is_constant()) {
    std::cerr << "bigfield: calling assert equal on 2 CONSTANT bigfield elements...is this intended?"
        << std::endl;
    return;
} else if (other.is_constant()) {
    // TODO(https://github.com/AztecProtocol/barretenberg/issues/998): Something is fishy here
    // evaluate a strict equality - make sure *this is reduced first, or an honest prover
    // might not be able to satisfy these constraints.
    field_t<Builder> t0 = (binary_basis_limbs[0].element - other.binary_basis_limbs[0].element);
    field_t<Builder> t1 = (binary_basis_limbs[1].element - other.binary_basis_limbs[1].element);
    field_t<Builder> t2 = (binary_basis_limbs[2].element - other.binary_basis_limbs[2].element);
    field_t<Builder> t3 = (binary_basis_limbs[3].element - other.binary_basis_limbs[3].element);
    field_t<Builder> t4 = (prime_basis_limb - other.prime_basis_limb);
    t0.assert_is_zero();
    t1.assert_is_zero();
    t2.assert_is_zero();
    t3.assert_is_zero();
    t4.assert_is_zero();
    return;
} else if (is_constant()) {
    other.assert_equal(*this);
    return;
} else {
    if (is_constant() && other.is_constant()) {
        std::cerr << "bigfield: calling assert equal on 2 CONSTANT bigfield elements...is this intended?"
                    << std::endl;
        return;
    } else if (other.is_constant()) {
        // evaluate a strict equality - make sure *this is reduced first, or an honest prover
        // might not be able to satisfy these constraints.
        field_t<Builder> t0 = (binary_basis_limbs[0].element - other.binary_basis_limbs[0].element);
        field_t<Builder> t1 = (binary_basis_limbs[1].element - other.binary_basis_limbs[1].element);
        field_t<Builder> t2 = (binary_basis_limbs[2].element - other.binary_basis_limbs[2].element);
        field_t<Builder> t3 = (binary_basis_limbs[3].element - other.binary_basis_limbs[3].element);
        field_t<Builder> t4 = (prime_basis_limb - other.prime_basis_limb);
        t0.assert_is_zero();
        t1.assert_is_zero();
        t2.assert_is_zero();
        t3.assert_is_zero();
        t4.assert_is_zero();
        return;
    } else if (is_constant()) {
        other.assert_equal(*this);
        return;
    }

After else if (is_constant()), in the else condition, both this and other must be nonconstants. So the following code is not reachable through either of the following conditions.

Unreachable branch in unsafe_evaluate_square_add function

In the unsafe_evaluate_square_add function, there is a first check:

void bigfield<Builder, T>::unsafe_evaluate_square_add(const bigfield& left, const std::vector<bigfield>& to_add, const bigfield& quotient, const bigfield& remainder)
{
    if (HasPlookup<Builder>) {
        unsafe_evaluate_multiply_add(left, left, to_add, quotient, { remainder });
        return;
    }
    ...

Then later in the function, there is another identical check:

if constexpr (HasPlookup<Builder>) {
        carry_lo = carry_lo.normalize();
        carry_hi = carry_hi.normalize();
        ctx->decompose_into_default_range(carry_lo.witness_index, static_cast<size_t>(carry_lo_msb));
        ctx->decompose_into_default_range(carry_hi.witness_index, static_cast<size_t>(carry_hi_msb));
}

This condition cannot be reached since the function would have returned during the previous check. To simplify the code, the second if block may be removed.

Unused variables

The variables prime_basis_maximum_limb and negative_prime_modulus_mod_binary_basis are defined but not used.

Zellic © 2025Back to top ↑