Skip to content

Commit

Permalink
Add negative unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Feb 12, 2025
1 parent a03cc7c commit b4bc442
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 1 deletion.
4 changes: 3 additions & 1 deletion barretenberg/cpp/pil/vm2/bc_decomposition.pil
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ include "precomputed.pil";

// This file owns the bytecode columns, and other helper columns.
// TODO: This does NOT support empty bytecode.
// TODO: Fully constrain.
// TODO: Fully constrain. In particular, the last bytecode of the trace might be truncated
// and not finish with last_of_contract being activated. We expect this to be constrained
// by bytecode hashing.
namespace bc_decomposition;

pol commit sel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,203 @@ TEST(BytecodeDecompositionConstrainingTest, MultipleBytecodesWithShortOnes)
check_relation<bc_decomposition>(trace);
}

TEST(BytecodeDecompositionConstrainingTest, NegativeDeactivatedSel)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_bytes_rem_inv = FF(33).invert(),
.bc_decomposition_bytes_remaining = 33,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_bytes_rem_inv = FF(32).invert(),
.bc_decomposition_bytes_remaining = 32,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_bytes_rem_inv = FF(31).invert(),
.bc_decomposition_bytes_remaining = 31,
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_SEL_BYTES_REM_NON_ZERO);
trace.set(C::bc_decomposition_sel, 2, 0); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_SEL_BYTES_REM_NON_ZERO),
"BC_DEC_SEL_BYTES_REM_NON_ZERO");
}

TEST(BytecodeDecompositionConstrainingTest, NegativeDeactivateLastContract)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_bytes_rem_min_one_inv = FF(2).invert(),
.bc_decomposition_bytes_remaining = 3,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_bytes_rem_min_one_inv = 1,
.bc_decomposition_bytes_remaining = 2,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_bytes_rem_min_one_inv = 0,
.bc_decomposition_bytes_remaining = 1,
.bc_decomposition_last_of_contract = 1,
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_LAST_CONTRACT_BYTES_REM_ONE);
trace.set(C::bc_decomposition_last_of_contract, 2, 0); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_LAST_CONTRACT_BYTES_REM_ONE),
"BC_DEC_LAST_CONTRACT_BYTES_REM_ONE");
}

TEST(BytecodeDecompositionConstrainingTest, NegativePcWrongInitializationFirstRow)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{ .precomputed_first_row = 1 },
{
.bc_decomposition_pc = 0,
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_PC_ZERO_INITIALIZATION);
trace.set(C::bc_decomposition_pc, 1, 7); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_PC_ZERO_INITIALIZATION),
"BC_DEC_PC_ZERO_INITIALIZATION");
}

TEST(BytecodeDecompositionConstrainingTest, NegativePcWrongInitializationInside)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{ .bc_decomposition_last_of_contract = 1 },
{
.bc_decomposition_pc = 0,
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_PC_ZERO_INITIALIZATION);
trace.set(C::bc_decomposition_pc, 1, 32); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_PC_ZERO_INITIALIZATION),
"BC_DEC_PC_ZERO_INITIALIZATION");
}

TEST(BytecodeDecompositionConstrainingTest, NegativePcWrongIncrement)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_pc = 5,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_pc = 6,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_last_of_contract = 1, // Required otherwise the test passes trivially
.bc_decomposition_pc = 7,
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_PC_INCREMENT);
trace.set(C::bc_decomposition_pc, 2, 6); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_PC_INCREMENT),
"BC_DEC_PC_INCREMENT");
}

TEST(BytecodeDecompositionConstrainingTest, NegativeBytesRemWrongDecrement)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_bytes_remaining = 5,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_bytes_remaining = 4,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_bytes_remaining = 3,
.bc_decomposition_last_of_contract = 1, // Required otherwise the test passes trivially
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_BYTES_REMAINING_DECREMENT);
trace.set(C::bc_decomposition_bytes_remaining, 0, 4); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_BYTES_REMAINING_DECREMENT),
"BC_DEC_BYTES_REMAINING_DECREMENT");
}

TEST(BytecodeDecompositionConstrainingTest, NegativeMutateBytecodeId)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_id = 147,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_id = 147,
.bc_decomposition_sel = 1,
},
{
.bc_decomposition_id = 147,
.bc_decomposition_last_of_contract = 1, // Required otherwise the test passes trivially
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_ID_CONSTANT);
trace.set(C::bc_decomposition_id, 2, 77); // Mutate to wrong value
EXPECT_THROW_WITH_MESSAGE(check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_ID_CONSTANT),
"BC_DEC_ID_CONSTANT");
}

TEST(BytecodeDecompositionConstrainingTest, NegativeWrongBytesToReadNoCorrection)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_bytes_remaining = 75,
.bc_decomposition_bytes_to_read = tracegen::DECOMPOSE_WINDOW_SIZE,
.bc_decomposition_sel = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_OVERFLOW_CORRECTION_VALUE);
trace.set(C::bc_decomposition_bytes_to_read, 0, 75); // Mutate to wrong value (bytes_remaining)
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_OVERFLOW_CORRECTION_VALUE),
"BC_DEC_OVERFLOW_CORRECTION_VALUE");
}

TEST(BytecodeDecompositionConstrainingTest, NegativeWrongBytesToReadWithCorrection)
{
TestTraceContainer trace = TestTraceContainer::from_rows({
{
.bc_decomposition_bytes_remaining = 13,
.bc_decomposition_bytes_to_read = 13,
.bc_decomposition_sel = 1,
.bc_decomposition_sel_overflow_correction_needed = 1,
},
});

check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_OVERFLOW_CORRECTION_VALUE);
trace.set(
C::bc_decomposition_bytes_to_read, 0, tracegen::DECOMPOSE_WINDOW_SIZE); // Mutate to wrong value (WINDOWS_SIZE)
EXPECT_THROW_WITH_MESSAGE(
check_relation<bc_decomposition>(trace, bc_decomposition::SR_BC_DEC_OVERFLOW_CORRECTION_VALUE),
"BC_DEC_OVERFLOW_CORRECTION_VALUE");
}

} // namespace
} // namespace bb::avm2::constraining

0 comments on commit b4bc442

Please sign in to comment.