Assessment reports>Barretenberg Bigfield>Discussion>Fix for decompose_into_bits

Fix for decompose_into_bits

The function field_t<Builder>::decompose_into_bits in `cpp/src/barretenberg/stdlib/primitives/field/field.cpp contains the following piece of code:

// TODO: Guido will make a PR that will fix an error here; hard-coded 127 is incorrect when 128 < num_bits < 256.
// Extract bit vector and show that it has the same value as `this`.
for (size_t i = 0; i < num_bits; ++i) {
    bool_t<Builder> bit = get_bit(context, num_bits - 1 - i, val_u256);
    result[num_bits - 1 - i] = bit;
    bb::fr scaling_factor_value = fr(2).pow(static_cast<uint64_t>(num_bits - 1 - i));
    field_t<Builder> scaling_factor(context, scaling_factor_value);

    sum = sum + (scaling_factor * bit);
    if (i == 127)
        shifted_high_limb = sum;
}

As the TODO comment indicates, the function does not function correctly for num_bits > 128. It appears that a fix for this would be to replace the condition i == 127 by num_bits == 128 + 1 + i. We also recommend to expand the decompose_into_bits test in cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp to cover the case of num_bits being a value other than 256.

Zellic © 2025Back to top ↑