Browse Source

A little refactoring of the top-level sigma_compiler_core module

to clean up what gets exported publicly
Ian Goldberg 1 month ago
parent
commit
14c998d048
2 changed files with 46 additions and 15 deletions
  1. 45 14
      sigma_compiler_core/src/lib.rs
  2. 1 1
      sigma_compiler_core/src/sigma/combiners.rs

+ 45 - 14
sigma_compiler_core/src/lib.rs

@@ -15,10 +15,50 @@ mod pubscalareq;
 mod rangeproof;
 mod substitution;
 mod syntax;
-pub mod transform;
+mod transform;
 
+pub use codegen::CodeGen;
 pub use syntax::{SigmaCompSpec, TaggedIdent, TaggedPoint, TaggedScalar, TaggedVarDict};
 
+use syn::Result;
+
+/// Transform the [`StatementTree`] so that it satisfies the
+/// [disjunction invariant].
+///
+/// [`StatementTree`]: sigma::combiners::StatementTree
+/// [disjunction invariant]: sigma::combiners::StatementTree::check_disjunction_invariant
+pub fn enforce_disjunction_invariant(
+    codegen: &mut CodeGen,
+    spec: &mut SigmaCompSpec,
+) -> Result<()> {
+    transform::enforce_disjunction_invariant(codegen, &mut spec.statements, &mut spec.vars)
+}
+
+/// Apply all of the compiler transformations.
+///
+/// The [disjunction invariant] must be true before calling this
+/// function, and will remain true after each transformation (and at the
+/// end of this function).  Call [enforce_disjunction_invariant] before
+/// calling this function if you're not sure the disjunction invariant
+/// already holds.
+///
+/// [disjunction invariant]: sigma::combiners::StatementTree::check_disjunction_invariant
+pub fn apply_transformations(codegen: &mut CodeGen, spec: &mut SigmaCompSpec) -> Result<()> {
+    // Apply any substitution transformations
+    substitution::transform(codegen, &mut spec.statements, &mut spec.vars)?;
+
+    // Apply any range statement transformations
+    rangeproof::transform(codegen, &mut spec.statements, &mut spec.vars)?;
+
+    // Apply any not-equals statement transformations
+    notequals::transform(codegen, &mut spec.statements, &mut spec.vars)?;
+
+    // Apply any public scalar equality transformations
+    pubscalareq::transform(codegen, &mut spec.statements, &mut spec.vars)?;
+
+    Ok(())
+}
+
 /// The main function of this macro.
 ///
 /// Parse the macro input with [`parse`](SigmaCompSpec#method.parse) to
@@ -41,20 +81,11 @@ pub fn sigma_compiler_core(
     // Enforce the disjunction invariant (do this before any other
     // transformations, since they assume the invariant holds, and will
     // maintain it)
-    transform::enforce_disjunction_invariant(&mut codegen, &mut spec.statements, &mut spec.vars)
-        .unwrap();
-
-    // Apply any substitution transformations
-    substitution::transform(&mut codegen, &mut spec.statements, &mut spec.vars).unwrap();
-
-    // Apply any range statement transformations
-    rangeproof::transform(&mut codegen, &mut spec.statements, &mut spec.vars).unwrap();
+    enforce_disjunction_invariant(&mut codegen, spec).unwrap();
 
-    // Apply any not-equals statement transformations
-    notequals::transform(&mut codegen, &mut spec.statements, &mut spec.vars).unwrap();
-
-    // Apply any public scalar equality transformations
-    pubscalareq::transform(&mut codegen, &mut spec.statements, &mut spec.vars).unwrap();
+    // Apply the transformations
+    apply_transformations(&mut codegen, spec).unwrap();
 
+    // Generate the code to be output
     codegen.generate(spec, emit_prover, emit_verifier)
 }

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

@@ -275,7 +275,7 @@ impl StatementTree {
     ///
     /// If you don't know that your [`StatementTree`] already satisfies
     /// the invariant, call
-    /// [`enforce_disjunction_invariant`](super::super::transform::enforce_disjunction_invariant),
+    /// [`enforce_disjunction_invariant`](super::super::enforce_disjunction_invariant),
     /// which will transform the [`StatementTree`] so that it does (and
     /// also call this
     /// [`check_disjunction_invariant`](StatementTree::check_disjunction_invariant)