Browse Source

feat: automatically reuse the reserved identity and generator elements

Michele Orrù 1 month ago
parent
commit
9b66900708
1 changed files with 16 additions and 5 deletions
  1. 16 5
      sigma-compiler-core/src/sigma/codegen.rs

+ 16 - 5
sigma-compiler-core/src/sigma/codegen.rs

@@ -324,13 +324,24 @@ impl<'a> CodeGen<'a> {
                     }
                     AExprType::Point { is_vec: false, .. } => {
                         if allocated_vars.insert(id.clone()) {
+                            // sigma-proofs reserves element indices for the
+                            // identity and the canonical group generator.
+                            // Reuse them whenever a declared point has one of
+                            // those values; otherwise keep the point as an
+                            // ordinary instance element.
                             element_allocs = quote! {
                                 #element_allocs
-                                let #id = #lr_var.allocate_element();
-                            };
-                            element_assigns = quote! {
-                                #element_assigns
-                                #lr_var.set_element(#id, #instance_var.#id);
+                                let #id = if #instance_var.#id
+                                    == <Point as group::Group>::identity()
+                                {
+                                    #lr_var.identity()
+                                } else if #instance_var.#id
+                                    == <Point as group::Group>::generator()
+                                {
+                                    #lr_var.generator()
+                                } else {
+                                    #lr_var.allocate_element_with(#instance_var.#id)
+                                };
                             };
                         }
                         Ok(quote! {#id})