浏览代码

Make the dumping of the Params to stdout a compile-time feature

so that the dumping code doesn't end up in the binary if you don't want it
Ian Goldberg 5 月之前
父节点
当前提交
36d4c43189
共有 2 个文件被更改,包括 84 次插入51 次删除
  1. 6 0
      sigma_compiler_derive/Cargo.toml
  2. 78 51
      sigma_compiler_derive/src/lib.rs

+ 6 - 0
sigma_compiler_derive/Cargo.toml

@@ -9,3 +9,9 @@ proc-macro = true
 [dependencies]
 syn = { version = "2.0", features = ["extra-traits"] }
 quote = "1.0"
+
+[features]
+# Dump (to stdout) the public params on both the prover's and verifier's
+# side.  They should match.
+dump = []
+# default = ["dump"]

+ 78 - 51
sigma_compiler_derive/src/lib.rs

@@ -182,57 +182,63 @@ fn sigma_compiler_impl(
         pub_params_fields.push_scalars(&spec.pub_scalars);
 
         let decls = pub_params_fields.field_decls();
-        let dump_chunks = pub_params_fields.fields.iter().map(|f| match f {
-            StructField::Scalar(id) => quote! {
-                print!("  {}: ", stringify!(#id));
-                Params::dump_scalar(&self.#id);
-                println!("");
-            },
-            StructField::VecScalar(id) => quote! {
-                print!("  {}: [", stringify!(#id));
-                for s in self.#id.iter() {
-                    print!("    ");
-                    Params::dump_scalar(s);
-                    println!(",");
-                }
-                println!("  ]");
-            },
-            StructField::Point(id) => quote! {
-                print!("  {}: ", stringify!(#id));
-                Params::dump_point(&self.#id);
-                println!("");
-            },
-            StructField::VecPoint(id) => quote! {
-                print!("  {}: [", stringify!(#id));
-                for p in self.#id.iter() {
-                    print!("    ");
-                    Params::dump_point(p);
-                    println!(",");
+        let dump_impl = if cfg!(feature = "dump") {
+            let dump_chunks = pub_params_fields.fields.iter().map(|f| match f {
+                StructField::Scalar(id) => quote! {
+                    print!("  {}: ", stringify!(#id));
+                    Params::dump_scalar(&self.#id);
+                    println!("");
+                },
+                StructField::VecScalar(id) => quote! {
+                    print!("  {}: [", stringify!(#id));
+                    for s in self.#id.iter() {
+                        print!("    ");
+                        Params::dump_scalar(s);
+                        println!(",");
+                    }
+                    println!("  ]");
+                },
+                StructField::Point(id) => quote! {
+                    print!("  {}: ", stringify!(#id));
+                    Params::dump_point(&self.#id);
+                    println!("");
+                },
+                StructField::VecPoint(id) => quote! {
+                    print!("  {}: [", stringify!(#id));
+                    for p in self.#id.iter() {
+                        print!("    ");
+                        Params::dump_point(p);
+                        println!(",");
+                    }
+                    println!("  ]");
+                },
+            });
+            quote! {
+                impl Params {
+                    fn dump_scalar(s: &Scalar) {
+                        let bytes: &[u8] = &s.to_repr();
+                        print!("{:02x?}", bytes);
+                    }
+
+                    fn dump_point(p: &Point) {
+                        let bytes: &[u8] = &p.to_bytes();
+                        print!("{:02x?}", bytes);
+                    }
+
+                    pub fn dump(&self) {
+                        #(#dump_chunks)*
+                    }
                 }
-                println!("  ]");
-            },
-        });
+            }
+        } else {
+            quote! {}
+        };
         quote! {
-            #[derive(Debug)]
             pub struct Params {
                 #decls
             }
 
-            impl Params {
-                fn dump_scalar(s: &Scalar) {
-                    let bytes: &[u8] = &s.to_repr();
-                    print!("{:02x?}", bytes);
-                }
-
-                fn dump_point(p: &Point) {
-                    let bytes: &[u8] = &p.to_bytes();
-                    print!("{:02x?}", bytes);
-                }
-
-                pub fn dump(&self) {
-                    #(#dump_chunks)*
-                }
-            }
+            #dump_impl
         }
     };
 
@@ -254,11 +260,18 @@ fn sigma_compiler_impl(
 
     // Generate the (currently dummy) prove function
     let prove_func = if emit_prover {
-        quote! {
-            pub fn prove(params: &Params, witness: &Witness) -> Result<Vec<u8>,()> {
+        let dumper = if cfg!(feature = "dump") {
+            quote! {
                 println!("prover params = {{");
                 params.dump();
                 println!("}}");
+            }
+        } else {
+            quote! {}
+        };
+        quote! {
+            pub fn prove(params: &Params, witness: &Witness) -> Result<Vec<u8>,()> {
+                #dumper
                 Ok(Vec::<u8>::default())
             }
         }
@@ -268,11 +281,18 @@ fn sigma_compiler_impl(
 
     // Generate the (currently dummy) verify function
     let verify_func = if emit_verifier {
-        quote! {
-            pub fn verify(params: &Params, proof: &[u8]) -> Result<(),()> {
+        let dumper = if cfg!(feature = "dump") {
+            quote! {
                 println!("verifier params = {{");
                 params.dump();
                 println!("}}");
+            }
+        } else {
+            quote! {}
+        };
+        quote! {
+            pub fn verify(params: &Params, proof: &[u8]) -> Result<(),()> {
+                #dumper
                 Ok(())
             }
         }
@@ -281,11 +301,18 @@ fn sigma_compiler_impl(
     };
 
     // Output the generated module for this protocol
+    let dump_use = if cfg!(feature = "dump") {
+        quote! {
+            use ff::PrimeField;
+            use group::GroupEncoding;
+        }
+    } else {
+        quote! {}
+    };
     quote! {
         #[allow(non_snake_case)]
         pub mod #proto_name {
-            use ff::PrimeField;
-            use group::GroupEncoding;
+            #dump_use
 
             #group_types
             #params_def