1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
32using testing::ElementsAre;
98 for (
const auto c : out) {
99 ThreeOperandTestParams params =
tuple_cat(TEST_VALUES_IN.at(i), std::make_tuple(c));
100 res.push_back(params);
109 res.reserve(in.size());
110 for (
const auto& c : in) {
116class AluTraceGenerationTest :
public ::testing::Test {
142class AluAddTraceGenerationTest :
public AluTraceGenerationTest,
143 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
147TEST_P(AluAddTraceGenerationTest, TraceGenerationAdd)
149 auto [
a,
b, c] = GetParam();
150 auto tag =
a.get_tag();
153 { .operation = AluOperation::ADD, .a =
a, .b =
b, .c = c },
178TEST_F(AluTraceGenerationTest, TraceGenerationAddTagError)
182 { .operation = AluOperation::ADD,
183 .a = MemoryValue::from<uint128_t>(1),
184 .b = MemoryValue::from<uint64_t>(2),
185 .c = MemoryValue::from<uint128_t>(3),
187 { .operation = AluOperation::ADD,
188 .a = MemoryValue::from<uint128_t>(1),
189 .b = MemoryValue::from<uint128_t>(2),
190 .c = MemoryValue::from<uint64_t>(3) },
214 alu_ab_tags_diff_inv,
254class AluSubTraceGenerationTest :
public AluTraceGenerationTest,
255 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
259TEST_P(AluSubTraceGenerationTest, TraceGenerationSub)
261 auto [
a,
b, c] = GetParam();
262 auto tag =
a.get_tag();
265 { .operation = AluOperation::SUB, .a =
a, .b =
b, .c = c },
308class AluMulTraceGenerationTest :
public AluTraceGenerationTest,
309 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
313TEST_P(AluMulTraceGenerationTest, TraceGenerationMul)
315 auto [
a,
b, c] = GetParam();
316 auto tag =
a.get_tag();
328 cf = hi_operand != 0;
329 c_hi = (c_hi - hi_operand) % (
uint256_t(1) << 64);
333 { .operation = AluOperation::MUL, .a =
a, .b =
b, .c = c },
357 alu_tag_u128_diff_inv,
358 is_u128 ? 0 : (
FF(static_cast<uint8_t>(
tag)) -
FF(static_cast<uint8_t>(
MemoryTag::
U128))).invert()),
364TEST_F(AluTraceGenerationTest, TraceGenerationMulU128)
369 { .operation = AluOperation::MUL,
370 .a = MemoryValue::from<uint128_t>(2),
371 .b = MemoryValue::from<uint128_t>(3),
372 .c = MemoryValue::from<uint128_t>(6) },
373 { .operation = AluOperation::MUL,
374 .a = MemoryValue::from<uint128_t>(u128_max),
375 .b = MemoryValue::from<uint128_t>(u128_max - 2),
376 .c = MemoryValue::from<uint128_t>(3) },
455class AluDivTraceGenerationTest :
public AluTraceGenerationTest,
456 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
460TEST_P(AluDivTraceGenerationTest, TraceGenerationDiv)
462 auto [
a,
b, c] = GetParam();
463 auto tag =
a.get_tag();
464 bool div_0_error =
b.as_ff() ==
FF(0);
468 { .operation = AluOperation::DIV, .a =
a, .b =
b, .c = c, .error = div_0_error },
491 alu_tag_u128_diff_inv,
492 is_u128 ? 0 : (
FF(static_cast<uint8_t>(
tag)) -
FF(static_cast<uint8_t>(
MemoryTag::
U128))).invert()),
504TEST_F(AluTraceGenerationTest, TraceGenerationDivU128)
509 { .operation = AluOperation::DIV,
510 .a = MemoryValue::from<uint128_t>(6),
511 .b = MemoryValue::from<uint128_t>(3),
512 .c = MemoryValue::from<uint128_t>(2) },
513 { .operation = AluOperation::DIV,
514 .a = MemoryValue::from<uint128_t>(u128_max),
515 .b = MemoryValue::from<uint128_t>(u128_max - 2),
516 .c = MemoryValue::from<uint128_t>(1) },
576TEST_F(AluTraceGenerationTest, TraceGenerationDivTagError)
583 { .operation = AluOperation::DIV,
584 .a = MemoryValue::from<FF>(6),
585 .b = MemoryValue::from<FF>(3),
586 .c = MemoryValue::from<FF>(2),
588 { .operation = AluOperation::DIV,
589 .a = MemoryValue::from<FF>(6),
590 .b = MemoryValue::from<uint128_t>(3),
591 .c = MemoryValue::from<FF>(2),
614 alu_tag_u128_diff_inv,
639 alu_tag_u128_diff_inv,
648 alu_ab_tags_diff_inv,
652TEST_F(AluTraceGenerationTest, TraceGenerationDivByZeroError)
656 { .operation = AluOperation::DIV,
657 .a = MemoryValue::from<uint64_t>(6),
658 .b = MemoryValue::from<uint64_t>(0),
681 alu_tag_u128_diff_inv,
716class AluFDivTraceGenerationTest :
public AluTraceGenerationTest,
717 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
721TEST_P(AluFDivTraceGenerationTest, TraceGenerationFDiv)
723 auto [
a,
b, c] = GetParam();
727 auto tag =
a.get_tag();
729 bool div_0_error =
b.as_ff() ==
FF(0);
733 { .operation = AluOperation::FDIV, .a =
a, .b =
b, .c = c, .error = div_0_error },
758TEST_F(AluTraceGenerationTest, TraceGenerationFDivTagError)
765 { .operation = AluOperation::FDIV,
766 .a = MemoryValue::from<uint128_t>(6),
767 .b = MemoryValue::from<uint128_t>(3),
768 .c = MemoryValue::from<uint128_t>(2),
770 { .operation = AluOperation::FDIV,
771 .a = MemoryValue::from<uint64_t>(6),
772 .b = MemoryValue::from<FF>(3),
773 .c = MemoryValue::from<uint64_t>(2),
826TEST_F(AluTraceGenerationTest, TraceGenerationFDivByZeroError)
831 .operation = AluOperation::FDIV,
832 .a = MemoryValue::from<FF>(6),
833 .b = MemoryValue::from<FF>(0),
875class AluEQTraceGenerationTest :
public AluTraceGenerationTest,
876 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
880TEST_P(AluEQTraceGenerationTest, TraceGenerationEQ)
882 auto [
a, _b, _c] = GetParam();
883 auto tag =
a.get_tag();
888 .operation = AluOperation::EQ,
913TEST_P(AluEQTraceGenerationTest, TraceGenerationInEQ)
915 auto [
a,
b, c] = GetParam();
916 auto tag =
a.get_tag();
921 .operation = AluOperation::EQ,
936 ROW_FIELD_EQ(alu_ab_diff_inv, c.as_ff() == 1 ? 0 : (
a.as_ff() -
b.as_ff()).invert()),
960class AluLTTraceGenerationTest :
public AluTraceGenerationTest,
961 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
965TEST_P(AluLTTraceGenerationTest, TraceGenerationLT)
967 auto [
a,
b, c] = GetParam();
968 auto tag =
a.get_tag();
972 { .operation = AluOperation::LT, .a =
a, .b =
b, .c = c },
999 is_ff ? 0 : (
FF(static_cast<uint8_t>(
tag)) -
FF(static_cast<uint8_t>(
MemoryTag::
FF))).invert()),
1018class AluLTETraceGenerationTest :
public AluTraceGenerationTest,
1019 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
1023TEST_P(AluLTETraceGenerationTest, TraceGenerationLTE)
1025 auto [
a,
b, c] = GetParam();
1026 auto tag =
a.get_tag();
1030 { .operation = AluOperation::LTE, .a =
a, .b =
b, .c = c },
1056 alu_tag_ff_diff_inv,
1057 is_ff ? 0 : (
FF(static_cast<uint8_t>(
tag)) -
FF(static_cast<uint8_t>(
MemoryTag::
FF))).invert()),
1066class AluNOTTraceGenerationTest :
public AluTraceGenerationTest,
public ::testing::WithParamInterface<MemoryValue> {};
1070TEST_P(AluNOTTraceGenerationTest, TraceGenerationNOT)
1072 auto a = GetParam();
1073 auto tag =
a.get_tag();
1079 { .operation = AluOperation::NOT, .a =
a, .b =
b, .error = is_ff },
1098 alu_tag_ff_diff_inv,
1099 is_ff ? 0 : (
FF(static_cast<uint8_t>(
tag)) -
FF(static_cast<uint8_t>(
MemoryTag::
FF))).invert()),
1123class AluShlTraceGenerationTest :
public AluTraceGenerationTest,
1124 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
1128TEST_P(AluShlTraceGenerationTest, TraceGenerationShl)
1130 auto [
a,
b, c] = GetParam();
1131 auto tag =
a.get_tag();
1135 bool overflow = b_num > tag_bits;
1136 uint256_t shift_lo_bits = overflow ? tag_bits :
static_cast<uint256_t>(tag_bits) - b_num;
1138 uint256_t two_pow_shift_lo_bits = overflow ? 0 :
static_cast<uint256_t>(1) << shift_lo_bits;
1139 uint256_t a_lo = overflow ? b_num - tag_bits :
static_cast<uint256_t>(
a.as_ff()) % two_pow_shift_lo_bits;
1143 .operation = AluOperation::SHL,
1173 ROW_FIELD_EQ(alu_sel_shift_ops_no_overflow, overflow ? 0 : 1),
1175 ROW_FIELD_EQ(alu_two_pow_shift_lo_bits, two_pow_shift_lo_bits),
1181TEST_F(AluShlTraceGenerationTest, TraceGenerationShlTagError)
1188 { .operation = AluOperation::SHL,
1189 .a = MemoryValue::from<FF>(6),
1190 .b = MemoryValue::from<FF>(3),
1191 .c = MemoryValue::from<FF>(48),
1193 { .operation = AluOperation::SHL,
1194 .a = MemoryValue::from<FF>(6),
1195 .b = MemoryValue::from<uint128_t>(3),
1196 .c = MemoryValue::from<FF>(48),
1264class AluShrTraceGenerationTest :
public AluTraceGenerationTest,
1265 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
1269TEST_P(AluShrTraceGenerationTest, TraceGenerationShr)
1271 auto [
a,
b, c] = GetParam();
1272 auto tag =
a.get_tag();
1276 auto overflow = b_num > tag_bits;
1277 uint256_t shift_lo_bits = overflow ? tag_bits : b_num;
1278 uint256_t shift_hi_bits = overflow ? tag_bits : tag_bits - b_num;
1279 uint256_t two_pow_shift_lo_bits = overflow ? 0 :
static_cast<uint256_t>(1) << shift_lo_bits;
1280 uint256_t a_lo = overflow ? b_num - tag_bits :
static_cast<uint256_t>(
a.as_ff()) % two_pow_shift_lo_bits;
1284 .operation = AluOperation::SHR,
1313 ROW_FIELD_EQ(alu_sel_shift_ops_no_overflow, overflow ? 0 : 1),
1315 ROW_FIELD_EQ(alu_two_pow_shift_lo_bits, two_pow_shift_lo_bits),
1321TEST_F(AluShrTraceGenerationTest, TraceGenerationShrTagError)
1328 { .operation = AluOperation::SHR,
1329 .a = MemoryValue::from<FF>(6),
1330 .b = MemoryValue::from<FF>(3),
1331 .c = MemoryValue::from<FF>(0),
1333 { .operation = AluOperation::SHR,
1334 .a = MemoryValue::from<FF>(6),
1335 .b = MemoryValue::from<uint128_t>(3),
1336 .c = MemoryValue::from<FF>(0),
1385struct TruncateTrivialTestParams {
1395 .expected_result = 1,
1400 .expected_result = 7,
1405 .expected_result = 123456789,
1410 .expected_result = 1234567890123456789ULL,
1415 .expected_result = (
uint256_t(1) << 127) + 982739482,
1424class AluTruncateTrivialTraceGenerationTest :
public AluTraceGenerationTest,
1425 public ::testing::WithParamInterface<TruncateTrivialTestParams> {};
1428 AluTruncateTrivialTraceGenerationTest,
1429 ::testing::ValuesIn(TRUNCATE_TRIVIAL_TEST_PARAMS));
1431TEST_P(AluTruncateTrivialTraceGenerationTest, TraceGenerationTruncateTrivial)
1439 { .operation = AluOperation::TRUNCATE, .a =
a, .b =
b, .c = c },
1444 ElementsAre(AllOf(
ROW_FIELD_EQ(alu_sel_op_truncate, 1),
1465struct TruncateNonTrivialTestParams {
1477 .expected_result = 123456789987654321ULL,
1478 .expected_lo_128 = (
uint256_t(98263) << 64) + 123456789987654321ULL,
1484 .expected_result = 1234567,
1485 .expected_lo_128 = (98263ULL << 32) + 1234567ULL,
1491 .expected_result = 1234,
1492 .expected_lo_128 = (98263ULL << 32) + 1234ULL,
1498 .expected_result = 7,
1499 .expected_lo_128 = 263,
1505 .expected_result = 1,
1506 .expected_lo_128 = 999,
1507 .expected_mid = 499,
1511class AluTruncateNonTrivialLT128TraceGenerationTest
1512 :
public AluTraceGenerationTest,
1513 public ::testing::WithParamInterface<TruncateNonTrivialTestParams> {};
1516 AluTruncateNonTrivialLT128TraceGenerationTest,
1517 ::testing::ValuesIn(TRUNCATE_LESS_THAN_128_TEST_PARAMS));
1519TEST_P(AluTruncateNonTrivialLT128TraceGenerationTest, TraceGenerationTruncateNonTrivialLT128)
1527 { .operation = AluOperation::TRUNCATE, .a =
a, .b =
b, .c = c },
1532 ElementsAre(AllOf(
ROW_FIELD_EQ(alu_sel_op_truncate, 1),
1554 .a = MemoryValue::from<FF>((
uint256_t(98263) << 128) + (
uint256_t(1111) << 64) + 123456789987654321ULL),
1556 .expected_result = 123456789987654321ULL,
1557 .expected_lo_128 = (
uint256_t(1111) << 64) + 123456789987654321ULL,
1561 .a = MemoryValue::from<FF>((
uint256_t(98263) << 128) + (
uint256_t(1111) << 64) + 123456789),
1563 .expected_result = 123456789,
1564 .expected_lo_128 = (
uint256_t(1111) << 64) + 123456789,
1568 .a = MemoryValue::from<FF>((
uint256_t(98263) << 128) + (
uint256_t(1111) << 64) + 1234),
1570 .expected_result = 1234,
1571 .expected_lo_128 = (
uint256_t(1111) << 64) + 1234,
1575 .a = MemoryValue::from<FF>((
uint256_t(98263) << 150) + (
uint256_t(123456789987654321ULL) << 8) + 234),
1577 .expected_result = 234,
1578 .expected_lo_128 = (
uint256_t(123456789987654321ULL) << 8) + 234,
1583class AluTruncateNonTrivialGT128TraceGenerationTest
1584 :
public AluTraceGenerationTest,
1585 public ::testing::WithParamInterface<TruncateNonTrivialTestParams> {};
1588 AluTruncateNonTrivialGT128TraceGenerationTest,
1589 ::testing::ValuesIn(TRUNCATE_GREATER_THAN_128_TEST_PARAMS));
1591TEST_P(AluTruncateNonTrivialGT128TraceGenerationTest, TraceGenerationTruncateNonTrivialGT128)
1599 { .operation = AluOperation::TRUNCATE, .a =
a, .b =
b, .c = c },
1604 ElementsAre(AllOf(
ROW_FIELD_EQ(alu_sel_op_truncate, 1),
INSTANTIATE_TEST_SUITE_P(AcirTests, AcirIntegrationSingleTest, testing::Values("a_1327_concrete_in_generic", "a_1_mul", "a_2_div", "a_3_add", "a_4_sub", "a_5_over", "a_6", "a_6_array", "a_7", "a_7_function", "aes128_encrypt", "arithmetic_binary_operations", "array_dynamic", "array_dynamic_blackbox_input", "array_dynamic_main_output", "array_dynamic_nested_blackbox_input", "array_eq", "array_if_cond_simple", "array_len", "array_neq", "array_sort", "array_to_slice", "array_to_slice_constant_length", "assert", "assert_statement", "assign_ex", "bigint", "bit_and", "bit_not", "bit_shifts_comptime", "bit_shifts_runtime", "blake3", "bool_not", "bool_or", "break_and_continue", "brillig_acir_as_brillig", "brillig_array_eq", "brillig_array_to_slice", "brillig_arrays", "brillig_assert", "brillig_bit_shifts_runtime", "brillig_blake2s", "brillig_blake3", "brillig_calls", "brillig_calls_array", "brillig_calls_conditionals", "brillig_conditional", "brillig_cow", "brillig_cow_assign", "brillig_cow_regression", "brillig_ecdsa_secp256k1", "brillig_ecdsa_secp256r1", "brillig_embedded_curve", "brillig_fns_as_values", "brillig_hash_to_field", "brillig_identity_function", "brillig_keccak", "brillig_loop", "brillig_nested_arrays", "brillig_not", "brillig_oracle", "brillig_pedersen", "brillig_recursion", "brillig_references", "brillig_schnorr", "brillig_sha256", "brillig_signed_cmp", "brillig_signed_div", "brillig_slices", "brillig_to_be_bytes", "brillig_to_bits", "brillig_to_bytes_integration", "brillig_to_le_bytes", "brillig_top_level", "brillig_uninitialized_arrays", "brillig_wrapping", "cast_bool", "closures_mut_ref", "conditional_1", "conditional_2", "conditional_regression_421", "conditional_regression_547", "conditional_regression_661", "conditional_regression_short_circuit", "conditional_regression_underflow", "custom_entry", "databus", "debug_logs", "diamond_deps_0", "double_verify_nested_proof", "double_verify_proof", "ecdsa_secp256k1", "ecdsa_secp256r1", "ecdsa_secp256r1_3x", "eddsa", "embedded_curve_ops", "field_attribute", "generics", "global_consts", "hash_to_field", "hashmap", "higher_order_functions", "if_else_chain", "import", "inline_never_basic", "integer_array_indexing", "keccak256", "main_bool_arg", "main_return", "merkle_insert", "missing_closure_env", "modules", "modules_more", "modulus", "nested_array_dynamic", "nested_array_dynamic_simple", "nested_array_in_slice", "nested_arrays_from_brillig", "no_predicates_basic", "no_predicates_brillig", "no_predicates_numeric_generic_poseidon", "operator_overloading", "pedersen_check", "pedersen_commitment", "pedersen_hash", "poseidon_bn254_hash", "poseidonsponge_x5_254", "pred_eq", "prelude", "references", "regression", "regression_2660", "regression_3051", "regression_3394", "regression_3607", "regression_3889", "regression_4088", "regression_4124", "regression_4202", "regression_4449", "regression_4709", "regression_5045", "regression_capacity_tracker", "regression_mem_op_predicate", "regression_method_cannot_be_found", "regression_struct_array_conditional", "schnorr", "sha256", "sha2_byte", "side_effects_constrain_array", "signed_arithmetic", "signed_comparison", "signed_division", "simple_2d_array", "simple_add_and_ret_arr", "simple_array_param", "simple_bitwise", "simple_comparison", "simple_mut", "simple_not", "simple_print", "simple_program_addition", "simple_radix", "simple_shield", "simple_shift_left_right", "slice_coercion", "slice_dynamic_index", "slice_loop", "slices", "strings", "struct", "struct_array_inputs", "struct_fields_ordering", "struct_inputs", "submodules", "to_be_bytes", "to_bytes_consistent", "to_bytes_integration", "to_le_bytes", "trait_as_return_type", "trait_impl_base_type", "traits_in_crates_1", "traits_in_crates_2", "tuple_inputs", "tuples", "type_aliases", "u128", "u16_support", "unconstrained_empty", "unit_value", "unsafe_range_constraint", "witness_compression", "xor"))
TEST_P(AcirIntegrationSingleTest, DISABLED_ProveAndVerifyProgram)
#define AVM_EXEC_OP_ID_ALU_TRUNCATE
#define AVM_EXEC_OP_ID_ALU_LTE
#define AVM_EXEC_OP_ID_ALU_DIV
#define AVM_EXEC_OP_ID_ALU_ADD
#define AVM_EXEC_OP_ID_ALU_SHL
#define AVM_EXEC_OP_ID_ALU_EQ
#define AVM_EXEC_OP_ID_ALU_SUB
#define AVM_EXEC_OP_ID_ALU_NOT
#define AVM_EXEC_OP_ID_ALU_MUL
#define AVM_EXEC_OP_ID_ALU_FDIV
#define AVM_EXEC_OP_ID_ALU_SHR
#define AVM_EXEC_OP_ID_ALU_LT
static TaggedValue from_tag(ValueTag tag, FF value)
void process(const simulation::EventEmitterInterface< simulation::AluEvent >::Container &events, TraceContainer &trace)
Process the ALU events and populate the ALU relevant columns in the trace.
std::vector< AvmFullRowConstRef > as_rows() const
#define ROW_FIELD_EQ(field_name, expression)
U128Decomposition decompose_128(const uint128_t &x)
uint8_t get_tag_bits(ValueTag tag)
uint256_t get_tag_max_value(ValueTag tag)
TEST_F(IPATest, ChallengesAreZero)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
constexpr auto tuple_cat(T &&... ts)
unsigned __int128 uint128_t
static constexpr uint256_t modulus