Skip to content

Commit 763b311

Browse files
committed
refactor(public_checks): move caller helpers into aztec-nr
1 parent 46be8da commit 763b311

8 files changed

Lines changed: 74 additions & 30 deletions

File tree

noir-projects/aztec-nr/aztec/src/lib.nr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub use protocol_types as protocol;
4646
pub(crate) mod logging;
4747
pub mod utils;
4848
pub mod authwit;
49+
pub mod public_checks;
4950
pub mod macros;
5051

5152
pub mod test;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::{
2+
context::{calls::PublicStaticCall, PrivateContext},
3+
protocol::{abis::function_selector::FunctionSelector, address::AztecAddress, constants::PUBLIC_CHECKS_ADDRESS},
4+
};
5+
6+
/// Hand-written interface stub for the `PublicChecks` canonical contract.
7+
///
8+
/// The `PublicChecks` contract exposes two view functions that can be enqueued
9+
/// from private context via `enqueue_view_incognito` to assert timestamp or
10+
/// block-number constraints without revealing the calling contract address.
11+
///
12+
/// This stub exists so that the `public_checks_contract` crate itself does not
13+
/// need to depend on `canonical_addresses` (which would create a structural
14+
/// cycle). Consumer contracts call the helpers below directly via
15+
/// `use aztec::public_checks::*` without taking a dep on the contract crate.
16+
///
17+
/// The selectors are derived with `comptime { FunctionSelector::from_signature(...) }`,
18+
/// which matches exactly what the `#[aztec]` macro generates for the real contract.
19+
struct PublicChecksInterface {
20+
target_contract: AztecAddress,
21+
}
22+
23+
impl PublicChecksInterface {
24+
pub fn at(target_contract: AztecAddress) -> Self {
25+
Self { target_contract }
26+
}
27+
28+
/// Returns a `PublicStaticCall` that asserts `timestamp <op> value`.
29+
pub fn check_timestamp(self, operation: u8, value: u64) -> PublicStaticCall<15, 2, ()> {
30+
let selector = comptime { FunctionSelector::from_signature("check_timestamp(u8,u64)") };
31+
PublicStaticCall::new(
32+
self.target_contract,
33+
selector,
34+
"check_timestamp",
35+
[operation as Field, value as Field],
36+
)
37+
}
38+
39+
/// Returns a `PublicStaticCall` that asserts `block_number <op> value`.
40+
pub fn check_block_number(self, operation: u8, value: u32) -> PublicStaticCall<18, 2, ()> {
41+
let selector = comptime { FunctionSelector::from_signature("check_block_number(u8,u32)") };
42+
PublicStaticCall::new(
43+
self.target_contract,
44+
selector,
45+
"check_block_number",
46+
[operation as Field, value as Field],
47+
)
48+
}
49+
}
50+
51+
// docs:start:helper_public_checks_functions
52+
/// Asserts that the current timestamp in the enqueued public call enqueued by `check_timestamp` satisfies
53+
/// the `operation` with respect to the `value. Preserves privacy by performing the check via the public checks
54+
/// contract.
55+
/// This conceals an address of the calling contract by setting `context.msg_sender` to the public checks contract
56+
/// address.
57+
pub fn privately_check_timestamp(operation: u8, value: u64, context: &mut PrivateContext) {
58+
PublicChecksInterface::at(PUBLIC_CHECKS_ADDRESS).check_timestamp(operation, value).enqueue_view_incognito(context);
59+
}
60+
61+
/// Asserts that the current block number in the enqueued public call enqueued by `check_block_number` satisfies
62+
/// the `operation` with respect to the `value. Preserves privacy by performing the check via the public checks
63+
/// contract.
64+
/// This conceals an address of the calling contract by setting `context.msg_sender` to the public checks contract
65+
/// address.
66+
pub fn privately_check_block_number(operation: u8, value: u32, context: &mut PrivateContext) {
67+
// docs:start:enqueueing
68+
PublicChecksInterface::at(PUBLIC_CHECKS_ADDRESS).check_block_number(operation, value).enqueue_view_incognito(context);
69+
// docs:end:enqueueing
70+
}
71+
// docs:end:helper_public_checks_functions

noir-projects/noir-contracts/contracts/app/app_subscription_contract/Nargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ type = "contract"
77
[dependencies]
88
aztec = { path = "../../../../aztec-nr/aztec" }
99
token = { path = "../token_contract" }
10-
public_checks = { path = "../../protocol/public_checks_contract" }

noir-projects/noir-contracts/contracts/app/app_subscription_contract/src/main.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub contract AppSubscription {
5252
state_vars::{Owned, PrivateMutable, PublicImmutable},
5353
utils::comparison::Comparator,
5454
};
55-
use public_checks::utils::privately_check_block_number;
55+
use aztec::public_checks::privately_check_block_number;
5656
use token::Token;
5757

5858
#[storage]

noir-projects/noir-contracts/contracts/app/crowdfunding_contract/Nargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ type = "contract"
88
aztec = { path = "../../../../aztec-nr/aztec" }
99
uint_note = { path = "../../../../aztec-nr/uint-note" }
1010
token = { path = "../token_contract" }
11-
public_checks = { path = "../../protocol/public_checks_contract" }

noir-projects/noir-contracts/contracts/app/crowdfunding_contract/src/main.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub contract Crowdfunding {
1313
utils::{array, comparison::Comparator},
1414
};
1515
use aztec::note::{constants::MAX_NOTES_PER_PAGE, HintedNote, note_getter_options::NoteStatus};
16-
use public_checks::utils::privately_check_timestamp;
16+
use aztec::public_checks::privately_check_timestamp;
1717
use token::Token;
1818
use uint_note::UintNote;
1919

noir-projects/noir-contracts/contracts/protocol/public_checks_contract/src/main.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod test;
2-
pub mod utils;
32

43
use aztec::macros::aztec;
54

noir-projects/noir-contracts/contracts/protocol/public_checks_contract/src/utils.nr

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)