Browse Source

Allow sigma::codegen to modify the StatementTree

Ian Goldberg 4 months ago
parent
commit
5d0e38b711

+ 3 - 3
sigma_compiler_core/src/codegen.rs

@@ -220,7 +220,7 @@ impl CodeGen {
     /// [`sigma_compiler_core`](super::sigma_compiler_core).
     pub fn generate(
         &self,
-        spec: &SigmaCompSpec,
+        spec: &mut SigmaCompSpec,
         emit_prover: bool,
         emit_verifier: bool,
     ) -> TokenStream {
@@ -243,11 +243,11 @@ impl CodeGen {
         let sigma_rs_vardict = taggedvardict_to_vardict(&spec.vars);
 
         // Generate the code that uses the underlying sigma_rs API
-        let sigma_rs_codegen = super::sigma::codegen::CodeGen::new(
+        let mut sigma_rs_codegen = super::sigma::codegen::CodeGen::new(
             format_ident!("sigma"),
             format_ident!("Point"),
             &sigma_rs_vardict,
-            &spec.statements,
+            &mut spec.statements,
         );
         let sigma_rs_code = sigma_rs_codegen.generate(emit_prover, emit_verifier);
 

+ 0 - 2
sigma_compiler_core/src/lib.rs

@@ -56,7 +56,5 @@ pub fn sigma_compiler_core(
     });
     */
 
-    spec.statements.dump();
-
     codegen.generate(spec, emit_prover, emit_verifier)
 }

+ 10 - 3
sigma_compiler_core/src/sigma/codegen.rs

@@ -139,7 +139,7 @@ pub struct CodeGen<'a> {
     proto_name: Ident,
     group_name: Ident,
     vars: &'a VarDict,
-    statements: &'a StatementTree,
+    statements: &'a mut StatementTree,
 }
 
 impl<'a> CodeGen<'a> {
@@ -147,7 +147,7 @@ impl<'a> CodeGen<'a> {
         proto_name: Ident,
         group_name: Ident,
         vars: &'a VarDict,
-        statements: &'a StatementTree,
+        statements: &'a mut StatementTree,
     ) -> Self {
         Self {
             proto_name,
@@ -162,7 +162,7 @@ impl<'a> CodeGen<'a> {
     ///
     /// `emit_prover` and `emit_verifier` are as in
     /// [`sigma_compiler_core`](super::super::sigma_compiler_core).
-    pub fn generate(&self, emit_prover: bool, emit_verifier: bool) -> TokenStream {
+    pub fn generate(&mut self, emit_prover: bool, emit_verifier: bool) -> TokenStream {
         let proto_name = &self.proto_name;
         let group_name = &self.group_name;
 
@@ -172,6 +172,13 @@ impl<'a> CodeGen<'a> {
             pub type Point = super::#group_name;
         };
 
+        // Flatten nested "And"s into single "And"s
+        self.statements.flatten_ands();
+
+        println!("Statements = {{");
+        self.statements.dump();
+        println!("}}");
+
         let mut pub_params_fields = StructFieldList::default();
         pub_params_fields.push_vars(self.vars, true);
 

+ 1 - 1
sigma_compiler_core/src/sigma/combiners.rs

@@ -411,7 +411,7 @@ impl StatementTree {
         match self {
             StatementTree::Leaf(e) => {
                 println!(
-                    "{:1$}{2}",
+                    "{:1$}{2},",
                     "",
                     depth * 2,
                     quote! { #e }.to_string().replace('\n', " ")