delta_local_laws/macros.rs
1/// Return a [crate::LocalLawsError] error with the given message when a
2/// condition is not met.
3///
4/// This macro only accepts literal format strings (with optional arguments)
5/// for consistent diagnostics.
6///
7/// # Example
8/// ```rust
9/// use delta_local_laws::{
10/// LocalLawsError,
11/// require,
12/// };
13///
14/// fn check_amount(amount: u64) -> Result<(), LocalLawsError> {
15/// require!(amount > 0, "amount must be > 0, got {}", amount);
16/// Ok(())
17/// }
18/// ```
19#[macro_export]
20macro_rules! require {
21 ($condition:expr, $fmt:literal $(, $arg:expr )* $(,)?) => {
22 if !($condition) {
23 return Err($crate::LocalLawsError::new(format!($fmt $(, $arg )*)));
24 }
25 };
26}
27
28/// Return a [crate::LocalLawsError] error with the given message when an
29/// [Option] is [None], otherwise return the inner value.
30///
31/// # Example
32/// ```rust
33/// use delta_local_laws::{
34/// LocalLawsError,
35/// require_some,
36/// };
37///
38/// fn value_or_error(value: Option<u64>) -> Result<u64, LocalLawsError> {
39/// let value = require_some!(value, "missing value");
40/// Ok(value)
41/// }
42/// ```
43#[macro_export]
44macro_rules! require_some {
45 ($option:expr, $fmt:literal $(, $arg:expr )* $(,)?) => {
46 $option.ok_or_else(|| $crate::LocalLawsError::new(format!($fmt $(, $arg )*)))?
47 };
48}
49
50/// Define the entrypoint of a local laws proof program.
51///
52/// # Parameters
53///
54/// - `$local_laws`: Path to the local law type to execute. The type must
55/// implement [crate::LocalLaws].
56///
57/// # Requirements
58///
59/// - The `sp1-zkvm` crate must be available in the crate calling this macro.
60///
61/// # Example
62///
63/// The `main.rs` of a local laws proof program:
64/// ```ignore
65/// #![no_main]
66/// use delta_local_laws::examples::min_max_spending::MinMaxSpendingLocalLaw;
67/// delta_local_laws::entrypoint!(MinMaxSpendingLocalLaw);
68/// ```
69#[macro_export]
70macro_rules! entrypoint {
71 ($local_laws:path $(,)?) => {
72 ::sp1_zkvm::entrypoint!(main);
73
74 fn main() {
75 use ::sp1_zkvm::io::{
76 commit,
77 read,
78 };
79 use ::std::vec::Vec;
80 use $crate::{
81 verifiable::types::{
82 VerifiableWithDiffs,
83 VerificationContext,
84 },
85 LocalLaws,
86 };
87
88 // Read the standard inputs
89 let verifiables = read::<Vec<VerifiableWithDiffs>>();
90 let verification_context = read::<VerificationContext>();
91
92 // Read the custom input of the local laws
93 let input = read();
94
95 // Verify the local laws
96 <$local_laws as LocalLaws>::validate(&verifiables, &verification_context, &input)
97 .expect("The local laws are not satisfied by the verifiables.");
98
99 // Commit standard inputs
100 commit(&verifiables);
101 commit(&verification_context);
102 }
103 };
104}