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 },
177TEST_F(AluTraceGenerationTest, TraceGenerationAddTagError)
181 { .operation = AluOperation::ADD,
182 .a = MemoryValue::from<uint128_t>(1),
183 .b = MemoryValue::from<uint64_t>(2),
184 .c = MemoryValue::from<uint128_t>(3),
185 .error = AluError::TAG_ERROR },
186 { .operation = AluOperation::ADD,
187 .a = MemoryValue::from<uint128_t>(1),
188 .b = MemoryValue::from<uint128_t>(2),
189 .c = MemoryValue::from<uint64_t>(3) },
213 alu_ab_tags_diff_inv,
253class AluSubTraceGenerationTest :
public AluTraceGenerationTest,
254 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
258TEST_P(AluSubTraceGenerationTest, TraceGenerationSub)
260 auto [
a,
b, c] = GetParam();
261 auto tag =
a.get_tag();
264 { .operation = AluOperation::SUB, .a =
a, .b =
b, .c = c },
305class AluMulTraceGenerationTest :
public AluTraceGenerationTest,
306 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
310TEST_P(AluMulTraceGenerationTest, TraceGenerationMul)
312 auto [
a,
b, c] = GetParam();
313 auto tag =
a.get_tag();
325 cf = hi_operand != 0;
326 c_hi = (c_hi - hi_operand) % (
uint256_t(1) << 64);
330 { .operation = AluOperation::MUL, .a =
a, .b =
b, .c = c },
353 is_u128 ? 0 :
FF(static_cast<uint8_t>(
tag) - static_cast<uint8_t>(
MemoryTag::
U128)).invert()),
360TEST_F(AluTraceGenerationTest, TraceGenerationMulU128)
365 { .operation = AluOperation::MUL,
366 .a = MemoryValue::from<uint128_t>(2),
367 .b = MemoryValue::from<uint128_t>(3),
368 .c = MemoryValue::from<uint128_t>(6) },
369 { .operation = AluOperation::MUL,
370 .a = MemoryValue::from<uint128_t>(u128_max),
371 .b = MemoryValue::from<uint128_t>(u128_max - 2),
372 .c = MemoryValue::from<uint128_t>(3) },
451class AluDivTraceGenerationTest :
public AluTraceGenerationTest,
452 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
456TEST_P(AluDivTraceGenerationTest, TraceGenerationDiv)
458 auto [
a,
b, c] = GetParam();
459 auto tag =
a.get_tag();
460 bool div_0_error =
b.as_ff() ==
FF(0);
464 { .operation = AluOperation::DIV,
490 is_u128 ? 0 :
FF(static_cast<uint8_t>(
tag) - static_cast<uint8_t>(
MemoryTag::
U128)).invert()),
493 FF(static_cast<uint8_t>(
tag) - static_cast<uint8_t>(
MemoryTag::
FF)).invert()),
495 ROW_FIELD_EQ(alu_sel_div_no_0_err, div_0_error ? 0 : 1),
502TEST_F(AluTraceGenerationTest, TraceGenerationDivU128)
507 { .operation = AluOperation::DIV,
508 .a = MemoryValue::from<uint128_t>(6),
509 .b = MemoryValue::from<uint128_t>(3),
510 .c = MemoryValue::from<uint128_t>(2) },
511 { .operation = AluOperation::DIV,
512 .a = MemoryValue::from<uint128_t>(u128_max),
513 .b = MemoryValue::from<uint128_t>(u128_max - 2),
514 .c = MemoryValue::from<uint128_t>(1) },
572TEST_F(AluTraceGenerationTest, TraceGenerationDivTagError)
579 { .operation = AluOperation::DIV,
580 .a = MemoryValue::from<FF>(6),
581 .b = MemoryValue::from<FF>(3),
582 .c = MemoryValue::from<FF>(2),
583 .error = AluError::TAG_ERROR },
584 { .operation = AluOperation::DIV,
585 .a = MemoryValue::from<FF>(6),
586 .b = MemoryValue::from<uint128_t>(3),
587 .c = MemoryValue::from<FF>(2),
588 .error = AluError::TAG_ERROR },
643 alu_ab_tags_diff_inv,
647TEST_F(AluTraceGenerationTest, TraceGenerationDivByZeroError)
651 { .operation = AluOperation::DIV,
652 .a = MemoryValue::from<uint64_t>(6),
653 .b = MemoryValue::from<uint64_t>(0),
654 .error = AluError::DIV_0_ERROR },
710class AluFDivTraceGenerationTest :
public AluTraceGenerationTest,
711 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
715TEST_P(AluFDivTraceGenerationTest, TraceGenerationFDiv)
717 auto [
a,
b, c] = GetParam();
721 auto tag =
a.get_tag();
723 bool div_0_error =
b.as_ff() ==
FF(0);
727 { .operation = AluOperation::FDIV,
756TEST_F(AluTraceGenerationTest, TraceGenerationFDivTagError)
763 { .operation = AluOperation::FDIV,
764 .a = MemoryValue::from<uint128_t>(6),
765 .b = MemoryValue::from<uint128_t>(3),
766 .c = MemoryValue::from<uint128_t>(2),
767 .error = AluError::TAG_ERROR },
768 { .operation = AluOperation::FDIV,
769 .a = MemoryValue::from<uint64_t>(6),
770 .b = MemoryValue::from<FF>(3),
771 .c = MemoryValue::from<uint64_t>(2),
772 .error = AluError::TAG_ERROR },
822TEST_F(AluTraceGenerationTest, TraceGenerationFDivByZeroError)
826 { .operation = AluOperation::FDIV,
827 .a = MemoryValue::from<FF>(6),
828 .b = MemoryValue::from<FF>(0),
829 .error = AluError::DIV_0_ERROR },
868class AluEQTraceGenerationTest :
public AluTraceGenerationTest,
869 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
873TEST_P(AluEQTraceGenerationTest, TraceGenerationEQ)
875 auto [
a, _b, _c] = GetParam();
876 auto tag =
a.get_tag();
881 .operation = AluOperation::EQ,
904TEST_P(AluEQTraceGenerationTest, TraceGenerationInEQ)
906 auto [
a,
b, c] = GetParam();
907 auto tag =
a.get_tag();
912 .operation = AluOperation::EQ,
927 ROW_FIELD_EQ(alu_helper1, c.as_ff() == 1 ? 0 :
FF(
a.as_ff() -
b.as_ff()).invert()),
949class AluLTTraceGenerationTest :
public AluTraceGenerationTest,
950 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
954TEST_P(AluLTTraceGenerationTest, TraceGenerationLT)
956 auto [
a,
b, c] = GetParam();
957 auto tag =
a.get_tag();
961 { .operation = AluOperation::LT, .a =
a, .b =
b, .c = c },
988 is_ff ? 0 :
FF(static_cast<uint8_t>(
tag) - static_cast<uint8_t>(
MemoryTag::
FF)).invert()),
1007class AluLTETraceGenerationTest :
public AluTraceGenerationTest,
1008 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
1012TEST_P(AluLTETraceGenerationTest, TraceGenerationLTE)
1014 auto [
a,
b, c] = GetParam();
1015 auto tag =
a.get_tag();
1019 { .operation = AluOperation::LTE, .a =
a, .b =
b, .c = c },
1039 ROW_FIELD_EQ(alu_lt_ops_result_c, c.as_ff() == 1 ? 0 : 1),
1046 is_ff ? 0 :
FF(static_cast<uint8_t>(
tag) - static_cast<uint8_t>(
MemoryTag::
FF)).invert()),
1055class AluNOTTraceGenerationTest :
public AluTraceGenerationTest,
public ::testing::WithParamInterface<MemoryValue> {};
1059TEST_P(AluNOTTraceGenerationTest, TraceGenerationNOT)
1061 auto a = GetParam();
1062 auto tag =
a.get_tag();
1068 { .operation = AluOperation::NOT,
1090 is_ff ? 0 :
FF(static_cast<uint8_t>(
tag) - static_cast<uint8_t>(
MemoryTag::
FF)).invert()),
1114class AluShlTraceGenerationTest :
public AluTraceGenerationTest,
1115 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
1119TEST_P(AluShlTraceGenerationTest, TraceGenerationShl)
1121 auto [
a,
b, c] = GetParam();
1122 auto tag =
a.get_tag();
1124 auto b_num =
static_cast<uint128_t>(
b.as_ff());
1126 auto overflow = b_num > tag_bits;
1127 uint128_t shift_lo_bits = overflow ? tag_bits : tag_bits - b_num;
1128 uint128_t shift_hi_bits = overflow ? tag_bits : b_num;
1129 auto two_pow_shift_lo_bits =
static_cast<uint128_t>(1) << shift_lo_bits;
1130 auto a_lo = overflow ? b_num - tag_bits :
static_cast<uint128_t>(
a.as_ff()) % two_pow_shift_lo_bits;
1131 auto a_hi =
static_cast<uint128_t>(
a.as_ff()) >> shift_lo_bits;
1134 .operation = AluOperation::SHL,
1162 FF(
static_cast<uint8_t
>(tag) -
static_cast<uint8_t
>(
MemoryTag::FF)).invert()),
1164 ROW_FIELD_EQ(alu_sel_shift_ops_no_overflow, overflow ? 0 : 1),
1166 ROW_FIELD_EQ(alu_two_pow_shift_lo_bits, two_pow_shift_lo_bits),
1172TEST_F(AluShlTraceGenerationTest, TraceGenerationShlTagError)
1179 { .operation = AluOperation::SHL,
1180 .a = MemoryValue::from<FF>(6),
1181 .b = MemoryValue::from<FF>(3),
1182 .c = MemoryValue::from<FF>(48),
1183 .error = AluError::TAG_ERROR },
1184 { .operation = AluOperation::SHL,
1185 .a = MemoryValue::from<FF>(6),
1186 .b = MemoryValue::from<uint128_t>(3),
1187 .c = MemoryValue::from<FF>(48),
1188 .error = AluError::TAG_ERROR },
1230 alu_ab_tags_diff_inv,
1254class AluShrTraceGenerationTest :
public AluTraceGenerationTest,
1255 public ::testing::WithParamInterface<ThreeOperandTestParams> {};
1259TEST_P(AluShrTraceGenerationTest, TraceGenerationShr)
1261 auto [
a,
b, c] = GetParam();
1262 auto tag =
a.get_tag();
1264 auto b_num =
static_cast<uint128_t>(
b.as_ff());
1266 auto overflow = b_num > tag_bits;
1267 uint128_t shift_lo_bits = overflow ? tag_bits : b_num;
1268 uint128_t shift_hi_bits = overflow ? tag_bits : tag_bits - b_num;
1269 auto two_pow_shift_lo_bits =
static_cast<uint128_t>(1) << shift_lo_bits;
1270 auto a_lo = overflow ? b_num - tag_bits :
static_cast<uint128_t>(
a.as_ff()) % two_pow_shift_lo_bits;
1271 auto a_hi =
static_cast<uint128_t>(
a.as_ff()) >> shift_lo_bits;
1274 .operation = AluOperation::SHR,
1301 FF(
static_cast<uint8_t
>(tag) -
static_cast<uint8_t
>(
MemoryTag::FF)).invert()),
1303 ROW_FIELD_EQ(alu_sel_shift_ops_no_overflow, overflow ? 0 : 1),
1305 ROW_FIELD_EQ(alu_two_pow_shift_lo_bits, two_pow_shift_lo_bits),
1311TEST_F(AluShrTraceGenerationTest, TraceGenerationShrTagError)
1318 { .operation = AluOperation::SHR,
1319 .a = MemoryValue::from<FF>(6),
1320 .b = MemoryValue::from<FF>(3),
1321 .c = MemoryValue::from<FF>(0),
1322 .error = AluError::TAG_ERROR },
1323 { .operation = AluOperation::SHR,
1324 .a = MemoryValue::from<FF>(6),
1325 .b = MemoryValue::from<uint128_t>(3),
1326 .c = MemoryValue::from<FF>(0),
1327 .error = AluError::TAG_ERROR },
1367 alu_ab_tags_diff_inv,
1374struct TruncateTrivialTestParams {
1384 .expected_result = 1,
1389 .expected_result = 7,
1394 .expected_result = 123456789,
1399 .expected_result = 1234567890123456789ULL,
1404 .expected_result = (
uint256_t(1) << 127) + 982739482,
1413class AluTruncateTrivialTraceGenerationTest :
public AluTraceGenerationTest,
1414 public ::testing::WithParamInterface<TruncateTrivialTestParams> {};
1417 AluTruncateTrivialTraceGenerationTest,
1418 ::testing::ValuesIn(TRUNCATE_TRIVIAL_TEST_PARAMS));
1420TEST_P(AluTruncateTrivialTraceGenerationTest, TraceGenerationTruncateTrivial)
1428 { .operation = AluOperation::TRUNCATE, .a =
a, .b =
b, .c = c },
1433 ElementsAre(AllOf(
ROW_FIELD_EQ(alu_sel_op_truncate, 1),
1452struct TruncateNonTrivialTestParams {
1465 .expected_result = 123456789987654321ULL,
1466 .expected_lo_128 = (
uint256_t(98263) << 64) + 123456789987654321ULL,
1473 .expected_result = 1234567,
1474 .expected_lo_128 = (98263ULL << 32) + 1234567ULL,
1481 .expected_result = 1234,
1482 .expected_lo_128 = (98263ULL << 32) + 1234ULL,
1489 .expected_result = 7,
1490 .expected_lo_128 = 263,
1491 .expected_hi_128 = 0,
1497 .expected_result = 1,
1498 .expected_lo_128 = 999,
1499 .expected_hi_128 = 0,
1500 .expected_mid = 499,
1504class AluTruncateNonTrivialLT128TraceGenerationTest
1505 :
public AluTraceGenerationTest,
1506 public ::testing::WithParamInterface<TruncateNonTrivialTestParams> {};
1509 AluTruncateNonTrivialLT128TraceGenerationTest,
1510 ::testing::ValuesIn(TRUNCATE_LESS_THAN_128_TEST_PARAMS));
1512TEST_P(AluTruncateNonTrivialLT128TraceGenerationTest, TraceGenerationTruncateNonTrivialLT128)
1520 { .operation = AluOperation::TRUNCATE, .a =
a, .b =
b, .c = c },
1525 ElementsAre(AllOf(
ROW_FIELD_EQ(alu_sel_op_truncate, 1),
1546 .a = MemoryValue::from<FF>((
uint256_t(98263) << 128) + (
uint256_t(1111) << 64) + 123456789987654321ULL),
1548 .expected_result = 123456789987654321ULL,
1549 .expected_lo_128 = (
uint256_t(1111) << 64) + 123456789987654321ULL,
1554 .a = MemoryValue::from<FF>((
uint256_t(98263) << 128) + (
uint256_t(1111) << 64) + 123456789),
1556 .expected_result = 123456789,
1557 .expected_lo_128 = (
uint256_t(1111) << 64) + 123456789,
1562 .a = MemoryValue::from<FF>((
uint256_t(98263) << 128) + (
uint256_t(1111) << 64) + 1234),
1564 .expected_result = 1234,
1565 .expected_lo_128 = (
uint256_t(1111) << 64) + 1234,
1570 .a = MemoryValue::from<FF>((
uint256_t(98263) << 150) + (
uint256_t(123456789987654321ULL) << 8) + 234),
1572 .expected_result = 234,
1573 .expected_lo_128 = (
uint256_t(123456789987654321ULL) << 8) + 234,
1579class AluTruncateNonTrivialGT128TraceGenerationTest
1580 :
public AluTraceGenerationTest,
1581 public ::testing::WithParamInterface<TruncateNonTrivialTestParams> {};
1584 AluTruncateNonTrivialGT128TraceGenerationTest,
1585 ::testing::ValuesIn(TRUNCATE_GREATER_THAN_128_TEST_PARAMS));
1587TEST_P(AluTruncateNonTrivialGT128TraceGenerationTest, TraceGenerationTruncateNonTrivialGT128)
1595 { .operation = AluOperation::TRUNCATE, .a =
a, .b =
b, .c = c },
1600 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)
std::vector< AvmFullRowConstRef > as_rows() const
#define ROW_FIELD_EQ(field_name, expression)
U256Decomposition decompose(const uint256_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
std::integral_constant< size_t, I > tag
constexpr auto tuple_cat(T &&... ts)
unsigned __int128 uint128_t
static constexpr uint256_t modulus