Skip to content

Commit

Permalink
1090 - add unit tests for private kernel circuit on malformed array
Browse files Browse the repository at this point in the history
validation
  • Loading branch information
jeanmon committed Sep 28, 2023
1 parent 435dd32 commit 72f1198
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,95 @@ TEST_F(native_private_kernel_init_tests, basic_contract_deployment)
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::NO_ERROR);
}

TEST_F(native_private_kernel_init_tests, input_validation_malformed_arrays_commitments)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

std::array<fr, MAX_NEW_COMMITMENTS_PER_CALL> malformed_commitments{ fr(0), fr(9123) };
private_inputs.private_call.call_stack_item.public_inputs.new_commitments = malformed_commitments;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_commitments");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_init_tests, input_validation_malformed_arrays_nullifiers)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

std::array<fr, MAX_NEW_NULLIFIERS_PER_CALL> malformed_nullifiers{};
malformed_nullifiers[MAX_NEW_NULLIFIERS_PER_CALL - 1] = fr(12);
private_inputs.private_call.call_stack_item.public_inputs.new_nullifiers = malformed_nullifiers;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_nullifiers");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_init_tests, input_validation_malformed_arrays_nullified_commitments)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

std::array<fr, MAX_NEW_NULLIFIERS_PER_CALL> malformed_nullified_commitments{ fr(0),
fr(0),
EMPTY_NULLIFIED_COMMITMENT };
private_inputs.private_call.call_stack_item.public_inputs.nullified_commitments = malformed_nullified_commitments;

DummyBuilder builder =
DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_nullified_commitments");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_init_tests, input_validation_malformed_arrays_private_call_stack)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

std::array<fr, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL> malformed_private_call_stack{ fr(0), fr(888) };
private_inputs.private_call.call_stack_item.public_inputs.private_call_stack = malformed_private_call_stack;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_private_call_stack");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_init_tests, input_validation_malformed_arrays_public_call_stack)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

std::array<fr, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL> malformed_public_call_stack{ fr(0), fr(888) };
private_inputs.private_call.call_stack_item.public_inputs.public_call_stack = malformed_public_call_stack;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_public_call_stack");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_init_tests, input_validation_malformed_arrays_new_l2_to_l1_msgs)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

std::array<fr, MAX_NEW_L2_TO_L1_MSGS_PER_CALL> malformed_new_l2_to_l1_msgs{};
malformed_new_l2_to_l1_msgs[MAX_NEW_L2_TO_L1_MSGS_PER_CALL - 1] = fr(1);
private_inputs.private_call.call_stack_item.public_inputs.new_l2_to_l1_msgs = malformed_new_l2_to_l1_msgs;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_new_l2_to_l1_msgs");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_init_tests, contract_deployment_call_stack_item_hash_mismatch_fails)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,94 @@ TEST_F(native_private_kernel_inner_tests, private_function_incorrect_call_stack_
CircuitErrorCode::PRIVATE_KERNEL__CALCULATED_PRIVATE_CALL_HASH_AND_PROVIDED_PRIVATE_CALL_HASH_MISMATCH);
}

TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_commitments)
{
auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args());

std::array<fr, MAX_NEW_COMMITMENTS_PER_CALL> malformed_commitments{ fr(0), fr(9123) };
private_inputs.private_call.call_stack_item.public_inputs.new_commitments = malformed_commitments;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_commitments");
native_private_kernel_circuit_inner(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}
TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_nullifiers)
{
auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args());

std::array<fr, MAX_NEW_NULLIFIERS_PER_CALL> malformed_nullifiers{};
malformed_nullifiers[MAX_NEW_NULLIFIERS_PER_CALL - 1] = fr(12);
private_inputs.private_call.call_stack_item.public_inputs.new_nullifiers = malformed_nullifiers;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_nullifiers");
native_private_kernel_circuit_inner(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_nullified_commitments)
{
auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args());

std::array<fr, MAX_NEW_NULLIFIERS_PER_CALL> malformed_nullified_commitments{ fr(0),
fr(0),
EMPTY_NULLIFIED_COMMITMENT };
private_inputs.private_call.call_stack_item.public_inputs.nullified_commitments = malformed_nullified_commitments;

DummyBuilder builder =
DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_nullified_commitments");
native_private_kernel_circuit_inner(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_private_call_stack)
{
auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args());

std::array<fr, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL> malformed_private_call_stack{ fr(0), fr(888) };
private_inputs.private_call.call_stack_item.public_inputs.private_call_stack = malformed_private_call_stack;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_private_call_stack");
native_private_kernel_circuit_inner(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_public_call_stack)
{
auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args());

std::array<fr, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL> malformed_public_call_stack{ fr(0), fr(888) };
private_inputs.private_call.call_stack_item.public_inputs.public_call_stack = malformed_public_call_stack;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_public_call_stack");
native_private_kernel_circuit_inner(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_new_l2_to_l1_msgs)
{
auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args());

std::array<fr, MAX_NEW_L2_TO_L1_MSGS_PER_CALL> malformed_new_l2_to_l1_msgs{};
malformed_new_l2_to_l1_msgs[MAX_NEW_L2_TO_L1_MSGS_PER_CALL - 1] = fr(1);
private_inputs.private_call.call_stack_item.public_inputs.new_l2_to_l1_msgs = malformed_new_l2_to_l1_msgs;

DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_new_l2_to_l1_msgs");
native_private_kernel_circuit_inner(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED);
}

TEST_F(native_private_kernel_inner_tests, private_kernel_should_fail_if_aggregating_too_many_commitments)
{
// Negative test to check if push_array_to_array fails if two many commitments are merged together
Expand Down

0 comments on commit 72f1198

Please sign in to comment.