Skip to content

Commit

Permalink
feat(aztec-nr): do not compile functions with a private public macro …
Browse files Browse the repository at this point in the history
…and unconstrained (#11815)

fixes: AztecProtocol/aztec-packages#11725
  • Loading branch information
Maddiaa0 authored and AztecBot committed Feb 8, 2025
1 parent cd08228 commit df91212
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 0 deletions.
16 changes: 16 additions & 0 deletions aztec/src/macros/functions/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ pub comptime fn private(f: FunctionDefinition) -> Quoted {
let fn_stub = stub_fn(f);
register_stub(f.module(), fn_stub);

// If a function is further modified as unconstrained, we throw an error
if f.is_unconstrained() {
let name = f.name();
panic(
f"Function {name} is annotated with #[private] but marked as unconstrained, remove unconstrained keyword",
);
}

let module_has_initializer = module_has_initializer(f.module());
let module_has_storage = module_has_storage(f.module());

Expand Down Expand Up @@ -244,6 +252,14 @@ comptime fn transform_public(f: FunctionDefinition) -> Quoted {
let fn_stub = stub_fn(f);
register_stub(f.module(), fn_stub);

// If a function is further modified as unconstrained, we throw an error
if f.is_unconstrained() {
let name = f.name();
panic(
f"Function {name} is annotated with #[public] but marked as unconstrained, remove unconstrained keyword",
);
}

let module_has_initializer = module_has_initializer(f.module());
let module_has_storage = module_has_storage(f.module());

Expand Down
6 changes: 6 additions & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function test {
while ! nc -z 127.0.0.1 45730 &>/dev/null; do sleep 1; done

test_cmds | (cd $root; NARGO_FOREIGN_CALL_TIMEOUT=300000 parallel --bar --halt now,fail=1 'dump_fail {} >/dev/null')

# Run the macro compilation failure tests
./macro_compilation_failure_tests/assert_macro_compilation_failure.sh
}

case "$cmd" in
Expand All @@ -33,6 +36,9 @@ case "$cmd" in
"test-cmds")
test_cmds
;;
"test-macro-compilation-failure")
./macro_compilation_failure_tests/assert_macro_compilation_failure.sh
;;
*)
echo_stderr "Unknown command: $cmd"
exit 1
Expand Down
1 change: 1 addition & 0 deletions macro_compilation_failure_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
# Within failure_contracts, we have contracts that should fail compilation due to code in the macros
# This script will test that compilation fails for each of the contracts in failure_contracts

REPO=$(git rev-parse --show-toplevel)
NARGO=${NARGO:-"$REPO/noir/noir-repo/target/release/nargo"}

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Initialize counters
total_tests=0
passed_tests=0

# Function to test compilation failure
test_compilation_failure() {
local contract_dir=$1
((total_tests++))

echo "Testing compilation failure for: $contract_dir"

# Try to compile the contract
if cd "$contract_dir" && $NARGO compile 2>/dev/null; then
echo -e "${RED}❌ Test failed: Compilation succeeded when it should have failed for $contract_dir${NC}"
cd - > /dev/null
return 1
else
echo -e "${GREEN}✓ Test passed: Compilation failed as expected for $contract_dir${NC}"
cd - > /dev/null
((passed_tests++))
return 0
fi
}

# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
FAILURE_CONTRACTS_DIR="$SCRIPT_DIR/failure_contracts"

# Test each contract in the failure_contracts directory
for contract in "$FAILURE_CONTRACTS_DIR"/*; do
if [ -d "$contract" ]; then
test_compilation_failure "$contract"
fi
done

# Print summary
echo -e "\nTest Summary:"
echo -e "Total tests: $total_tests"
echo -e "Passed tests: $passed_tests"
echo -e "Failed tests: $((total_tests - passed_tests))"

# Exit with failure if any test didn't pass
[ "$total_tests" -eq "$passed_tests" ] || exit 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "marked_private_unconstrained"
type = "contract"
authors = [""]

[dependencies]
aztec = { path = "../../../aztec" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Private functions that are also marked as unconstrained are not allowed.
///
use aztec::macros::aztec;


#[aztec]
contract MarkedPrivateUnconstrained {
use aztec::macros::functions::private;


#[private]
unconstrained fn unconstrained_private_function() {}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "marked_public_unconstrained"
type = "contract"
authors = [""]

[dependencies]
aztec = { path = "../../../aztec" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Public functions that are also marked as unconstrained are not allowed.
///
use aztec::macros::aztec;


#[aztec]
contract MarkedPublicUnconstrained {
use aztec::macros::functions::public;


#[public]
unconstrained fn unconstrained_public_function() {}
}

0 comments on commit df91212

Please sign in to comment.