-
Notifications
You must be signed in to change notification settings - Fork 87
Feat/Improve frontend (rust-interface) of sonobe #96
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
base: main
Are you sure you want to change the base?
Changes from all commits
8cbe0a3
b46092c
fa7b823
32d806c
7ea0950
e887b25
7acba6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ | |
| members = [ | ||
| "folding-schemes", | ||
| "solidity-verifiers", | ||
| "cli" | ||
| "cli", | ||
| "frontend-macro" | ||
| ] | ||
| resolver = "2" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| [package] | ||
| name = "frontend-macro" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| syn = { version = "0.15", features = ["extra-traits"] } | ||
| quote = "0.6" | ||
| proc-macro2 = "0.4" | ||
|
|
||
| [dev-dependencies] | ||
| trybuild = "1.0" | ||
| ark-ff = "^0.4.0" | ||
| ark-bn254 = {version="0.4.0", features=["r1cs"]} | ||
| ark-r1cs-std = { version = "0.4.0", default-features = false } # this is patched at the workspace level | ||
|
|
||
|
|
||
| [lib] | ||
| proc-macro = true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| use proc_macro::TokenStream; | ||
| use quote::quote; | ||
| use syn::{parse_macro_input, DeriveInput}; | ||
|
|
||
| #[proc_macro_derive(Flatten)] | ||
| pub fn derive(input: TokenStream) -> TokenStream { | ||
| let ast = parse_macro_input!(input as DeriveInput); | ||
| let iden = &ast.ident; | ||
| let cs_iden_name = format!("{}Constraint", iden); | ||
| let cs_iden = syn::Ident::new(&cs_iden_name, iden.span()); | ||
|
|
||
| let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); | ||
|
|
||
| let fields = if let syn::Data::Struct(syn::DataStruct { | ||
| fields: syn::Fields::Named(syn::FieldsNamed { ref named, .. }), | ||
| .. | ||
| }) = ast.data | ||
| { | ||
| named | ||
| } else { | ||
| unimplemented!(); | ||
| }; | ||
|
|
||
| let cs_fields_to_vec = fields.iter().map(|f| { | ||
| let name = &f.ident; | ||
| quote! { value.#name } | ||
| }); | ||
|
|
||
| let cs_fields = fields.iter().map(|f| { | ||
| let name = &f.ident; | ||
| quote! {pub #name: ark_r1cs_std::fields::fp::FpVar<F> } | ||
| }); | ||
|
|
||
| let builder_fields = fields.iter().map(|f| { | ||
| let name = &f.ident; | ||
| quote! { value.#name } | ||
| }); | ||
|
|
||
| let cs_vec_to_fields = fields.iter().enumerate().map(|(id, f)| { | ||
| let name = &f.ident; | ||
| quote! {#name: vec[#id].clone()} | ||
| }); | ||
|
|
||
| let vec_to_fields = fields.iter().enumerate().map(|(id, f)| { | ||
| let name = &f.ident; | ||
| quote! {#name: vec[#id]} | ||
| }); | ||
|
|
||
| let state_number = fields.len(); | ||
|
|
||
| let state_macro = quote! { | ||
| impl #impl_generics #iden #ty_generics #where_clause { | ||
| pub fn state_number() -> usize { | ||
| #state_number | ||
| } | ||
| } | ||
|
|
||
| impl #impl_generics From<#iden #ty_generics> for Vec #ty_generics #where_clause { | ||
| fn from(value: #iden #ty_generics) -> Vec #ty_generics { | ||
| vec![#(#builder_fields,)*] | ||
| } | ||
| } | ||
|
|
||
| impl #impl_generics From<Vec #ty_generics> for #iden #ty_generics { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are parts of the code that seem to work with vectors, but when I try to use for example struct State<F: PrimeField> {
a: F,
b: F,
c: Vec<F>, // vector
d: (F, F), // tuple
}it fails with an error for both cases (vector & tuple). Is it expected? To put an example of usage, this is the Grapevine circuit (Circom, but taking it as a real-world sample), where they use mostly vectors for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will check it :D and update it soon |
||
| fn from(vec: Vec #ty_generics) -> #iden #ty_generics { | ||
| assert!(vec.len() == #iden::#ty_generics::state_number()); | ||
| #iden { | ||
| #(#vec_to_fields,)* | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let constraint_macro = quote! { | ||
| pub struct #cs_iden<F: ark_ff::PrimeField> { | ||
| #(#cs_fields,)* | ||
| } | ||
|
|
||
|
|
||
| impl<F: PrimeField> From<Vec<ark_r1cs_std::fields::fp::FpVar<F>>> for #cs_iden<F> { | ||
| fn from(vec: Vec<ark_r1cs_std::fields::fp::FpVar<F>>) -> #cs_iden<F>{ | ||
| #cs_iden { | ||
| #(#cs_vec_to_fields,)* | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<F: PrimeField> From<#cs_iden<F>> for Vec<ark_r1cs_std::fields::fp::FpVar<F>> { | ||
| fn from(value: #cs_iden<F>) -> Vec<ark_r1cs_std::fields::fp::FpVar<F>> { | ||
| vec![#(#cs_fields_to_vec,)*] | ||
| } | ||
| } | ||
|
|
||
|
|
||
| impl<F: ark_ff::PrimeField> #iden<F>{ | ||
| pub fn cs_state(v: Vec< ark_r1cs_std::fields::fp::FpVar<F>>) -> #cs_iden<F> { | ||
| #cs_iden::from(v) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let expanded = quote! { | ||
| #state_macro | ||
|
|
||
| #constraint_macro | ||
| }; | ||
|
|
||
| expanded.into() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use ark_bn254::Fr; | ||
| use ark_ff::PrimeField; | ||
| use frontend_macro::Flatten; | ||
| #[derive(Flatten, Debug)] | ||
| struct State<F: PrimeField> { | ||
| a: F, | ||
| b: F, | ||
| } | ||
|
|
||
| fn main() { | ||
| let s = State::<Fr> { | ||
| a: Fr::from(1u32), | ||
| b: Fr::from(1u32), | ||
| }; | ||
|
|
||
| let v: Vec<Fr> = Vec::from(s); | ||
|
|
||
| println!("{:?}", State::<Fr>::state_number()); | ||
| println!("{:?}", v); | ||
| println!("{:?}", State::from(v)); | ||
|
Comment on lines
+18
to
+20
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe instead of prints, this test can do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only for compile macro. I added runable test for macro. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #[cfg(test)] | ||
| mod test { | ||
|
|
||
| #[test] | ||
| fn try_compile() { | ||
| let t = trybuild::TestCases::new(); | ||
| t.pass("tests/parse.rs"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_run_test() { | ||
| use ark_bn254::Fr; | ||
| use ark_ff::PrimeField; | ||
| use frontend_macro::Flatten; | ||
| #[derive(Flatten, Debug, PartialEq, Clone)] | ||
| struct State<F: PrimeField> { | ||
| a: F, | ||
| b: F, | ||
| } | ||
|
|
||
| let s = State::<Fr> { | ||
| a: Fr::from(1u32), | ||
| b: Fr::from(1u32), | ||
| }; | ||
|
|
||
| let v: Vec<Fr> = Vec::from(s.clone()); | ||
|
|
||
| assert_eq!(2, State::<Fr>::state_number()); | ||
| assert_eq!(vec![s.a, s.b], v); | ||
| assert_eq!(s, State::from(v)); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.