浏览代码

Fix and enable arithmetic halo2 chip.

parazyd 4 年之前
父节点
当前提交
9a58c7b8b3
共有 3 个文件被更改,包括 27 次插入20 次删除
  1. 0 2
      src/crypto/mod.rs
  2. 22 17
      src/zk/arith_chip.rs
  3. 5 1
      src/zk/mod.rs

+ 0 - 2
src/crypto/mod.rs

@@ -1,5 +1,4 @@
 pub mod address;
-//pub mod arith_chip;
 pub mod coin;
 pub mod constants;
 pub mod diffie_hellman;
@@ -10,7 +9,6 @@ pub mod mint_proof;
 pub mod note;
 pub mod nullifier;
 pub mod proof;
-//pub mod redpallas;
 pub mod schnorr;
 pub mod spend_proof;
 pub mod token_id;

+ 22 - 17
src/crypto/arith_chip.rs → src/zk/arith_chip.rs

@@ -1,12 +1,11 @@
-use halo2_gadgets::utilities::Var;
 use halo2_proofs::{
-    circuit::{Chip, Layouter},
+    circuit::{AssignedCell, Chip, Layouter},
     plonk::{Advice, Column, ConstraintSystem, Error, Selector},
     poly::Rotation,
 };
 use pasta_curves::pallas;
 
-type Variable = CellValue<pallas::Base>;
+type Variable = AssignedCell<pallas::Base, pallas::Base>;
 
 // Replace with use pasta::Fp and pasta::Fq
 type Fp = pallas::Base;
@@ -48,8 +47,8 @@ impl ArithmeticChip {
         let a_col = cs.advice_column();
         let b_col = cs.advice_column();
 
-        cs.enable_equality(a_col.into());
-        cs.enable_equality(b_col.into());
+        cs.enable_equality(a_col);
+        cs.enable_equality(b_col);
 
         //let instance = cs.instance_column();
 
@@ -110,26 +109,29 @@ impl ArithmeticChip {
                     || "lhs",
                     self.config.a_col,
                     0,
-                    || a.value().ok_or(Error::SynthesisError),
+                    || Ok(*a.value().ok_or(Error::Synthesis)?),
                 )?;
+
                 let rhs = region.assign_advice(
                     || "rhs",
                     self.config.b_col,
                     0,
-                    || b.value().ok_or(Error::SynthesisError),
+                    || Ok(*b.value().ok_or(Error::Synthesis)?),
                 )?;
-                region.constrain_equal(a.cell(), lhs)?;
-                region.constrain_equal(b.cell(), rhs)?;
+
+                region.constrain_equal(a.cell(), lhs.cell())?;
+                region.constrain_equal(b.cell(), rhs.cell())?;
 
                 let value = a.value().and_then(|a| b.value().map(|b| a + b));
+
                 let cell = region.assign_advice(
                     || "lhs + rhs",
                     self.config.a_col,
                     1,
-                    || value.ok_or(Error::SynthesisError),
+                    || value.ok_or(Error::Synthesis),
                 )?;
 
-                out = Some(Var::new(cell, value));
+                out = Some(cell);
                 Ok(())
             },
         )?;
@@ -144,6 +146,7 @@ impl ArithmeticChip {
         b: Variable,
     ) -> Result<Variable, Error> {
         let mut out = None;
+
         layouter.assign_region(
             || "mul",
             |mut region| {
@@ -153,26 +156,28 @@ impl ArithmeticChip {
                     || "lhs",
                     self.config.a_col,
                     0,
-                    || a.value().ok_or(Error::SynthesisError),
+                    || Ok(*a.value().ok_or(Error::Synthesis)?),
                 )?;
+
                 let rhs = region.assign_advice(
                     || "rhs",
                     self.config.b_col,
                     0,
-                    || b.value().ok_or(Error::SynthesisError),
+                    || Ok(*b.value().ok_or(Error::Synthesis)?),
                 )?;
-                region.constrain_equal(a.cell(), lhs)?;
-                region.constrain_equal(b.cell(), rhs)?;
+
+                region.constrain_equal(a.cell(), lhs.cell())?;
+                region.constrain_equal(b.cell(), rhs.cell())?;
 
                 let value = a.value().and_then(|a| b.value().map(|b| a * b));
                 let cell = region.assign_advice(
                     || "lhs * rhs",
                     self.config.a_col,
                     1,
-                    || value.ok_or(Error::SynthesisError),
+                    || value.ok_or(Error::Synthesis),
                 )?;
 
-                out = Some(Var::new(cell, value));
+                out = Some(cell);
                 Ok(())
             },
         )?;

+ 5 - 1
src/zk/mod.rs

@@ -1,4 +1,8 @@
-pub mod circuit;
+/// Halo2 arithmetic chip
+pub mod arith_chip;
+
 /// Halo2 zkas virtual machine
 pub mod vm;
 mod vm_stack;
+
+pub mod circuit;