Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit dedf05e

Browse files
authored
bring repo up-to-date (#1)
* bring repo up-to-date * ignore doctest
1 parent aebc267 commit dedf05e

File tree

24 files changed

+436
-342
lines changed

24 files changed

+436
-342
lines changed

Cargo.lock

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ repository = "https://github.com/Cardinal-Cryptography/zkOS"
1414
[workspace.dependencies]
1515
console_error_panic_hook = { version = "0.1.7" }
1616
criterion = { version = "0.5.1" }
17+
darling = { version = "0.20.10" }
1718
getrandom = { version = "0.2" }
1819
halo2_poseidon = { git = "ssh://git@github.com/Cardinal-Cryptography/poseidon-gadget", branch = "poseidon2-pow7" }
1920
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v0.3.0", default-features = false }
2021
human_bytes = { version = "0.4.3", default-features = false }
2122
itertools = { version = "0.13.0" }
2223
once_cell = { version = "1.20.2" }
24+
proc-macro2 = { version = "1.0.86" }
25+
quote = { version = "1.0.37" }
2326
rand = { version = "0.8.5" }
2427
rand_chacha = { version = "0.3.1" }
2528
rand_core = { version = "0.6.4" }
@@ -30,8 +33,10 @@ sha3 = { version = "0.10" }
3033
static_assertions = { version = "1.1.0" }
3134
strum = { version = "0.26.3" }
3235
strum_macros = { version = "0.26.3" }
36+
syn = { version = "2.0.79" }
3337
thiserror = { version = "1.0.61" }
3438

3539
# Local dependencies
40+
macros = { path = "crates/macros" }
3641
shielder-circuits = { path = "crates/shielder-circuits" }
3742
transcript = { path = "crates/transcript" }

crates/macros/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "macros"
3+
version = "0.1.0"
4+
description = "Procedural macros for zkOS crates"
5+
6+
edition.workspace = true
7+
authors.workspace = true
8+
homepage.workspace = true
9+
license.workspace = true
10+
categories.workspace = true
11+
repository.workspace = true
12+
13+
[lib]
14+
doctest = false
15+
proc-macro = true
16+
17+
[dependencies]
18+
darling = { workspace = true }
19+
proc-macro2 = { workspace = true }
20+
quote = { workspace = true }
21+
syn = { workspace = true, features = ["full"] }

crates/macros/src/embed.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use darling::{ast::NestedMeta, FromMeta};
2+
use proc_macro2::TokenStream as TokenStream2;
3+
use quote::quote;
4+
use syn::ItemStruct;
5+
6+
type SynResult<T> = Result<T, syn::Error>;
7+
8+
#[derive(Debug, FromMeta)]
9+
struct Attributes {
10+
field_type: Option<syn::Path>,
11+
receiver: syn::Type,
12+
impl_generics: String,
13+
embedded: syn::Type,
14+
}
15+
16+
/// Auxiliary function to enter ?-based error propagation.
17+
pub fn embeddable(attr: TokenStream2, item: TokenStream2) -> SynResult<TokenStream2> {
18+
let item_struct = syn::parse2::<ItemStruct>(item)?;
19+
let backed_up_struct = item_struct.clone();
20+
21+
let Attributes {
22+
field_type,
23+
receiver,
24+
impl_generics,
25+
embedded,
26+
} = Attributes::from_list(&NestedMeta::parse_meta_list(attr)?)?;
27+
let field_type = field_type.unwrap_or_else(|| syn::parse_quote!(F));
28+
let impl_generics = syn::parse_str::<syn::Generics>(&impl_generics)?;
29+
30+
let struct_name = item_struct.ident;
31+
32+
let field_embedding = item_struct.fields.iter().map(|field| {
33+
let field_name = &field.ident.clone().expect("Only named fields are supported");
34+
quote! {
35+
#field_name: self.#field_name.embed(&mut layouter, advice_pool, stringify!(#field_name))?
36+
}
37+
});
38+
39+
Ok(quote! {
40+
#backed_up_struct
41+
42+
impl #impl_generics Embed < #field_type > for #receiver {
43+
type Embedded = #embedded;
44+
45+
fn embed(
46+
&self,
47+
layouter: &mut impl halo2_proofs::circuit::Layouter< #field_type >,
48+
advice_pool: &crate::column_pool::ColumnPool<halo2_proofs::plonk::Advice>,
49+
annotation: impl Into<alloc::string::String>,
50+
) -> Result<Self::Embedded, halo2_proofs::plonk::Error> {
51+
let mut layouter = layouter.namespace(|| annotation);
52+
Ok(#struct_name {
53+
#(#field_embedding),*
54+
})
55+
}
56+
}
57+
})
58+
}

crates/macros/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use proc_macro::TokenStream;
2+
3+
mod embed;
4+
5+
/// Derive the `Embed` trait for a struct.
6+
///
7+
/// # Requirements
8+
///
9+
/// 1. All the fields of the struct already implement `Embed`.
10+
/// 2. Only structs with named fields are supported
11+
/// 3. `halo2_proofs` must be in scope.
12+
/// 4. Can be used only in the `shielder_circuits` crate.
13+
///
14+
/// # Attributes
15+
///
16+
/// - `field_type`: The type of the field that will be used in the `Embed` trait implementation.
17+
/// If not provided, `F` is used.
18+
/// - `receiver`: The type of the struct that will implement the `Embed` trait.
19+
/// - `impl_generics`: Generics that will be used in the `Embed` trait implementation.
20+
/// - `embedded`: The type that the struct will be embedded into.
21+
///
22+
/// # Example
23+
///
24+
/// ```no_run
25+
/// use crate::embeddable;
26+
///
27+
/// #[embeddable(
28+
/// receiver = "IntermediateValues<Value<F>>",
29+
/// impl_generics = "<F: FieldExt>",
30+
/// embedded = "IntermediateValues<crate::AssignedCell<F>>"
31+
/// )]
32+
/// pub struct IntermediateValues<F> {
33+
/// /// Account balance after the deposit is made.
34+
/// pub account_new_balance: F,
35+
/// }
36+
/// ```
37+
///
38+
/// This will generate the following code:
39+
///
40+
/// ```rust, no_run
41+
/// impl<F: FieldExt> Embed<F> for IntermediateValues<Value<F>> {
42+
/// type Embedded = IntermediateValues<crate::AssignedCell<F>>;
43+
/// fn embed(
44+
/// &self,
45+
/// layouter: &mut impl halo2_proofs::circuit::Layouter<F>,
46+
/// advice_pool: &crate::column_pool::ColumnPool<halo2_proofs::plonk::Advice>,
47+
/// annotation: impl Into<alloc::string::String>,
48+
/// ) -> Result<Self::Embedded, halo2_proofs::plonk::Error> {
49+
/// let mut layouter = layouter.namespace(|| annotation);
50+
/// Ok(IntermediateValues {
51+
/// account_new_balance: self
52+
/// .account_new_balance
53+
/// .embed(&mut layouter, advice_pool, "account_new_balance")?,
54+
/// })
55+
/// }
56+
/// }
57+
/// ```
58+
#[proc_macro_attribute]
59+
pub fn embeddable(attr: TokenStream, item: TokenStream) -> TokenStream {
60+
match embed::embeddable(attr.into(), item.into()) {
61+
Ok(ts) => ts.into(),
62+
Err(e) => e.to_compile_error().into(),
63+
}
64+
}

crates/shielder-circuits/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ getrandom = { workspace = true, optional = true }
1717
halo2_poseidon = { workspace = true }
1818
halo2_proofs = { workspace = true }
1919
human_bytes = { workspace = true }
20+
macros = { workspace = true }
2021
once_cell = { workspace = true }
2122
rand = { workspace = true, features = ["small_rng"] }
2223
rand_chacha = { workspace = true }

crates/shielder-circuits/src/chips/id_hiding.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ use crate::{
1010
#[derive(Clone, Debug)]
1111
pub struct IdHidingChip<F: FieldExt, const CHUNK_SIZE: usize> {
1212
pub poseidon: PoseidonChip<F>,
13-
pub range_check: LookupRangeCheckChip<F, CHUNK_SIZE>,
13+
pub range_check: LookupRangeCheckChip<CHUNK_SIZE>,
1414
}
1515

1616
impl<F: FieldExt, const CHUNK_SIZE: usize> IdHidingChip<F, CHUNK_SIZE> {
17-
pub fn new(
18-
poseidon: PoseidonChip<F>,
19-
range_check: LookupRangeCheckChip<F, CHUNK_SIZE>,
20-
) -> Self {
17+
pub fn new(poseidon: PoseidonChip<F>, range_check: LookupRangeCheckChip<CHUNK_SIZE>) -> Self {
2118
Self {
2219
poseidon,
2320
range_check,

0 commit comments

Comments
 (0)