diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp index ea3ee298746..29d1d020a2d 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp @@ -128,24 +128,48 @@ void common_validate_arrays(DummyBuilder& builder, PrivateCircuitPublicInputs const& end) +/** + * @brief We validate that relevant arrays assumed to be zero-padded on the right comply to this format. + * + * @param builder + * @param end Reference to previous_kernel.public_inputs.end. + */ +void common_validate_previous_kernel_arrays(DummyBuilder& builder, CombinedAccumulatedData const& end) +{ + // Each of the following arrays is expected to be zero-padded. + validate_array(builder, end.read_requests, "Accumulated data - Read Requests"); + validate_array(builder, end.new_commitments, "Accumulated data - New commitments"); + validate_array(builder, end.new_nullifiers, "Accumulated data - New nullifiers"); + validate_array(builder, end.nullified_commitments, "Accumulated data - Nullified commitments"); + validate_array(builder, end.private_call_stack, "Accumulated data - Private call stack"); + validate_array(builder, end.public_call_stack, "Accumulated data - Public call stack"); + validate_array(builder, end.new_l2_to_l1_msgs, "Accumulated data - New L2 to L1 messages"); +} + +void common_validate_previous_kernel_0th_nullifier(DummyBuilder& builder, CombinedAccumulatedData const& end) { builder.do_assert(end.new_nullifiers[0] != 0, "The 0th nullifier in the accumulated nullifier array is zero", CircuitErrorCode::PRIVATE_KERNEL__0TH_NULLLIFIER_IS_ZERO); } +void common_validate_previous_kernel_values(DummyBuilder& builder, CombinedAccumulatedData const& end) +{ + common_validate_previous_kernel_arrays(builder, end); + common_validate_previous_kernel_0th_nullifier(builder, end); +} + void common_update_end_values(DummyBuilder& builder, PrivateCallData const& private_call, KernelCircuitPublicInputs& public_inputs) diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/common.hpp b/circuits/cpp/src/aztec3/circuits/kernel/private/common.hpp index 97f3de0843c..4cd4a1793f8 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/common.hpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/common.hpp @@ -37,7 +37,9 @@ void common_validate_read_requests(DummyBuilder& builder, MAX_READ_REQUESTS_PER_CALL> const& read_request_membership_witnesses); void common_validate_arrays(DummyBuilder& builder, PrivateCircuitPublicInputs const& app_public_inputs); -void common_validate_0th_nullifier(DummyBuilder& builder, CombinedAccumulatedData const& end); +void common_validate_previous_kernel_arrays(DummyBuilder& builder, CombinedAccumulatedData const& end); +void common_validate_previous_kernel_values(DummyBuilder& builder, CombinedAccumulatedData const& end); +void common_validate_previous_kernel_0th_nullifier(DummyBuilder& builder, CombinedAccumulatedData const& end); void common_update_end_values(DummyBuilder& builder, PrivateCallData const& private_call, diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.cpp index aac28a78a1c..9d3268e7d6f 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.cpp @@ -91,8 +91,6 @@ void validate_inputs(DummyCircuitBuilder& builder, PrivateKernelInputsInner builder.do_assert(start_private_call_stack_length != 0, "Cannot execute private kernel circuit with an empty private call stack", CircuitErrorCode::PRIVATE_KERNEL__PRIVATE_CALL_STACK_EMPTY); - - common_validate_0th_nullifier(builder, private_inputs.previous_kernel.public_inputs.end); } KernelCircuitPublicInputs native_private_kernel_circuit_inner(DummyCircuitBuilder& builder, @@ -101,6 +99,8 @@ KernelCircuitPublicInputs native_private_kernel_circuit_inner(DummyCircuitBu // We'll be pushing data to this during execution of this circuit. KernelCircuitPublicInputs public_inputs{}; + common_validate_previous_kernel_values(builder, private_inputs.previous_kernel.public_inputs.end); + // Do this before any functions can modify the inputs. initialise_end_values(private_inputs.previous_kernel, public_inputs); diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.test.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.test.cpp index b27ad2ebe69..16988f38d10 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.test.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_inner.test.cpp @@ -323,6 +323,111 @@ TEST_F(native_private_kernel_inner_tests, input_validation_malformed_arrays_new_ EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED); } +TEST_F(native_private_kernel_inner_tests, input_validation_malformed_end_arrays_read_requests) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_read_requests{ fr(0), fr(9123), fr(0), fr(12) }; + private_inputs.previous_kernel.public_inputs.end.read_requests = malformed_read_requests; + + DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_end_arrays_read_requests"); + 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_end_arrays_commitments) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_commitments{ fr(0), fr(9123) }; + private_inputs.previous_kernel.public_inputs.end.new_commitments = malformed_commitments; + + DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_end_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_end_arrays_nullifiers) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_nullifiers{}; + malformed_nullifiers[MAX_NEW_NULLIFIERS_PER_TX - 1] = fr(12); + private_inputs.previous_kernel.public_inputs.end.new_nullifiers = malformed_nullifiers; + + DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_end_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_end_arrays_nullified_commitments) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_nullified_commitments{ fr(0), + fr(0), + EMPTY_NULLIFIED_COMMITMENT }; + private_inputs.previous_kernel.public_inputs.end.nullified_commitments = malformed_nullified_commitments; + + DummyBuilder builder = + DummyBuilder("private_kernel_tests__input_validation_malformed_arrays_end_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_end_arrays_private_call_stack) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_private_call_stack{ fr(0), fr(888) }; + private_inputs.previous_kernel.public_inputs.end.private_call_stack = malformed_private_call_stack; + + DummyBuilder builder = + DummyBuilder("private_kernel_tests__input_validation_malformed_end_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_end_arrays_public_call_stack) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_public_call_stack{ fr(0), fr(888) }; + private_inputs.previous_kernel.public_inputs.end.public_call_stack = malformed_public_call_stack; + + DummyBuilder builder = + DummyBuilder("private_kernel_tests__input_validation_malformed_end_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_end_arrays_l2_to_l1_msgs) +{ + auto private_inputs = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_l2_to_l1_msgs{}; + malformed_l2_to_l1_msgs[MAX_NEW_L2_TO_L1_MSGS_PER_TX - 1] = fr(1); + private_inputs.previous_kernel.public_inputs.end.new_l2_to_l1_msgs = malformed_l2_to_l1_msgs; + + DummyBuilder builder = DummyBuilder("private_kernel_tests__input_validation_malformed_end_arrays_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 diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.cpp index 91d8a9e3381..3576b59e1c7 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.cpp @@ -154,11 +154,11 @@ KernelCircuitPublicInputsFinal native_private_kernel_circuit_ordering( // We'll be pushing data to this during execution of this circuit. KernelCircuitPublicInputsFinal public_inputs{}; + common_validate_previous_kernel_values(builder, private_inputs.previous_kernel.public_inputs.end); + // Do this before any functions can modify the inputs. initialise_end_values(private_inputs.previous_kernel, public_inputs); - common_validate_0th_nullifier(builder, private_inputs.previous_kernel.public_inputs.end); - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1486): validate that `len(new_nullifiers) == // len(nullified_commitments)` diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.test.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.test.cpp index 3af7fcfd34c..e0fa73e550a 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.test.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit_ordering.test.cpp @@ -384,4 +384,127 @@ TEST_F(native_private_kernel_ordering_tests, 0th_nullifier_zero_fails) ASSERT_EQ(failure.code, CircuitErrorCode::PRIVATE_KERNEL__0TH_NULLLIFIER_IS_ZERO); } +TEST_F(native_private_kernel_ordering_tests, input_validation_malformed_end_arrays_read_requests) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_read_requests{ fr(0), fr(9123), fr(0), fr(12) }; + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.read_requests = malformed_read_requests; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = + DummyBuilder("native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_read_requests"); + native_private_kernel_circuit_ordering(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_ordering_tests, input_validation_malformed_end_arrays_commitments) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_commitments{ fr(0), fr(9123) }; + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.new_commitments = malformed_commitments; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = + DummyBuilder("native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_commitments"); + native_private_kernel_circuit_ordering(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_ordering_tests, input_validation_malformed_end_arrays_nullifiers) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_nullifiers{}; + malformed_nullifiers[MAX_NEW_NULLIFIERS_PER_TX - 1] = fr(12); + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.new_nullifiers = malformed_nullifiers; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = + DummyBuilder("native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_nullifiers"); + native_private_kernel_circuit_ordering(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_ordering_tests, input_validation_malformed_end_arrays_nullified_commitments) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_nullified_commitments{ fr(0), + fr(0), + EMPTY_NULLIFIED_COMMITMENT }; + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.nullified_commitments = malformed_nullified_commitments; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = DummyBuilder( + "native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_nullified_commitments"); + native_private_kernel_circuit_ordering(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_ordering_tests, input_validation_malformed_end_arrays_private_call_stack) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_private_call_stack{ fr(0), fr(888) }; + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.private_call_stack = malformed_private_call_stack; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = + DummyBuilder("native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_private_call_stack"); + native_private_kernel_circuit_ordering(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_ordering_tests, input_validation_malformed_end_arrays_public_call_stack) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_public_call_stack{ fr(0), fr(888) }; + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.public_call_stack = malformed_public_call_stack; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = + DummyBuilder("native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_public_call_stack"); + native_private_kernel_circuit_ordering(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_ordering_tests, input_validation_malformed_end_arrays_l2_to_l1_msgs) +{ + auto private_inputs_inner = do_private_call_get_kernel_inputs_inner(false, deposit, standard_test_args()); + + std::array malformed_l2_to_l1_msgs{}; + malformed_l2_to_l1_msgs[MAX_NEW_L2_TO_L1_MSGS_PER_TX - 1] = fr(1); + auto& previous_kernel = private_inputs_inner.previous_kernel; + previous_kernel.public_inputs.end.new_l2_to_l1_msgs = malformed_l2_to_l1_msgs; + PrivateKernelInputsOrdering private_inputs{ .previous_kernel = previous_kernel }; + + DummyBuilder builder = + DummyBuilder("native_private_kernel_ordering_tests__input_validation_malformed_end_arrays_l2_to_l1_msgs"); + native_private_kernel_circuit_ordering(builder, private_inputs); + + EXPECT_EQ(builder.failed(), true); + EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::ARRAY_NOT_ZERO_RIGHT_PADDED); +} + } // namespace aztec3::circuits::kernel::private_kernel