narodnik 5 лет назад
Родитель
Сommit
bd29e9a12d
2 измененных файлов с 30 добавлено и 34 удалено
  1. 21 1
      scripts/pism.py
  2. 9 33
      src/simple.rs

+ 21 - 1
scripts/pism.py

@@ -179,6 +179,22 @@ class Contract:
         self.params = params
         self.program = program
 
+    def _includes(self):
+        return \
+r"""use bellman::{
+    gadgets::{
+        boolean,
+        boolean::{AllocatedBit, Boolean},
+        multipack,
+    },
+    groth16, Circuit, ConstraintSystem, SynthesisError,
+};
+use bls12_381::Bls12;
+use ff::{PrimeField, Field};
+use group::Curve;
+use zcash_proofs::circuit::ecc;
+"""
+
     def _compile_header(self):
         code = "pub struct %s {\n" % to_initial_caps(self.name)
         for param_name, param_type in self.params.items():
@@ -293,7 +309,7 @@ r"""let %s = ecc::fixed_base_multiplication(
     cs.namespace(|| "%s"),
     &%s,
     &%s,
-)?;""" % (out, line, fr, base)
+)?;""" % (out, line, base, fr)
         elif command == "emit_ec":
             point = args[0]
             return '%s.inputize(cs.namespace(|| "%s"))?;' % (point, line)
@@ -323,6 +339,8 @@ r"""let %s = ecc::fixed_base_multiplication(
         self.constants = constants
         code = ""
 
+        code += self._includes()
+
         self.rename_consts = {}
         if "constants" in aux:
             for const_name, value in aux["constants"].items():
@@ -353,7 +371,9 @@ r"""impl Circuit<bls12_381::Scalar> for %s {
         if (body := self._compile_body()) is None:
             return None
         code += body
+        code += "Ok(())\n"
 
+        code += "    }\n"
         code += "}\n"
 
         return code

+ 9 - 33
src/simple.rs

@@ -7,54 +7,30 @@ use bellman::{
     groth16, Circuit, ConstraintSystem, SynthesisError,
 };
 use bls12_381::Bls12;
-use ff::{PrimeField, Field};
+use ff::{Field, PrimeField};
 use group::Curve;
-use rand::rngs::OsRng;
-
-use zcash_proofs::constants::{
-    SPENDING_KEY_GENERATOR
-};
-
-//pub const CRH_IVK_PERSONALIZATION: &[u8; 8] = b"Zcashivk";
-
-struct MyCircuit {
-    secret: Option<jubjub::Fr>
-}
-
-impl Circuit<bls12_381::Scalar> for MyCircuit {
-    fn synthesize<CS: ConstraintSystem<bls12_381::Scalar>>(
-        self, cs: &mut CS) -> Result<(), SynthesisError> {
-
-        let secret = boolean::field_into_boolean_vec_le(cs.namespace(|| "secret"), self.secret)?;
-
-        let public = zcash_proofs::circuit::ecc::fixed_base_multiplication(
-            cs.namespace(|| "public"),
-            &SPENDING_KEY_GENERATOR,
-            &secret,
-        )?;
-
-        public.inputize(cs.namespace(|| "public"))
-    }
-}
+mod simple_circuit;
+use simple_circuit::InputSpend;
 
 fn main() {
-    use jubjub::*;
-    use jubjub::SubgroupPoint;
-    use core::ops::{MulAssign, Mul};
+    use core::ops::{Mul, MulAssign};
     use ff::PrimeField;
     use group::{Group, GroupEncoding};
+    use jubjub::SubgroupPoint;
+    use jubjub::*;
+    use rand::rngs::OsRng;
     //let ak = jubjub::SubgroupPoint::random(&mut OsRng);
 
     let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
     let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
 
     let params = {
-        let c = MyCircuit { secret: None };
+        let c = InputSpend { secret: None };
         groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap()
     };
     let pvk = groth16::prepare_verifying_key(&params.vk);
 
-    let c = MyCircuit {
+    let c = InputSpend {
         secret: Some(secret),
     };