Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding time check contract #8

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
members = [
"src/token_contract"
"src/token_contract",
"src/time_check_contract"
]
8 changes: 8 additions & 0 deletions src/time_check_contract/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "time_check_contract"
authors = [""]
compiler_version = ">=1.0.0"
type = "contract"

[dependencies]
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "aztec-packages-v0.72.1", directory = "noir-projects/aztec-nr/aztec" }
20 changes: 20 additions & 0 deletions src/time_check_contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Timestamp verification in private circuits

## Overview

In private circuits, timestamps are challenging to use since circuits are timeless, and proofs cannot depend on when they are generated. Aztec’s PrivateContext does not include a timestamp variable due to the transaction lifecycle: proving → broadcasting → inclusion on-chain.

However, many DeFi applications require time-based logic, such as vesting schedules. The usual alternative block numbers may not be suitable in all cases.

## Solution

To verify timestamps privately:

- Users optimistically input the timestamp in the circuit.
- A call to a public function is enqueued to check its validity against the execution timestamp.
- If incorrect, the transaction and state changes (private and public) revert.
- To prevent privacy leaks, this intermediary contract TimeCheck is used:
- Instead of `ContractA(private)` → `ContractA(public)`,
- Transaction flows as `ContractA(private)` → `TimeCheck(private)` → `TimeCheck(public)`.

This prevents observers from linking the timestamp check to a specific contract, ensuring confidentiality.
54 changes: 54 additions & 0 deletions src/time_check_contract/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use dep::aztec::macros::aztec;

#[aztec]
// Timestamp validation in private circuits, for secure and anonymous time-based logic execution.
contract TimeCheck {

/** ==========================================================
* ========================= IMPORTS =========================
* ======================================================== */
Comment on lines +10 to +12
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about using these banners and order of declaration?

i'd suggest sth like:

  • imports (ofc)
  • storage
  • constructor
  • private entrypoints
  • public entrypoints
  • public (internal) methods
  • libraries


use dep::aztec::macros::functions::{internal, private, public};

/** ==========================================================
* ==================== PRIVATE ENTRYPOINTS ==================
* ======================================================== */

#[private]
fn timestamp_gt(t: u64) {
TimeCheck::at(context.this_address()).assert_timestamp_gt(t).enqueue(&mut context);
}

#[private]
fn timestamp_lt(t: u64) {
TimeCheck::at(context.this_address()).assert_timestamp_lt(t).enqueue(&mut context);
}

/** ==========================================================
* ===================== PUBLIC ENTRYPOINTS ==================
* ======================================================== */

#[public]
#[internal] // TODO: is internal needed here?
fn assert_timestamp_gt(t: u64) {
let current_t = context.timestamp();
// TODO: define if = should pass or not
assert(current_t >= t, "Timestamp not greater than current");
}

#[public]
#[internal] // TODO: is internal needed here?
fn assert_timestamp_lt(t: u64) {
let current_t = context.timestamp();
// TODO: define if = should pass or not
assert(current_t <= t, "Timestamp not lesser than current");
}

// NOTE: assert_timestamp_eq has no actual application (prone to fail)
/* TODO:
* Implement:
* - timestamp_gt_by_delta(t:64, delta: 64)
* - timestamp_lt_by_delta(t:64, delta: 64)
*/
}