Просмотр исходного кода

zkas: Separate constants into specific types.

parazyd 4 лет назад
Родитель
Сommit
e00c8e0147

+ 4 - 3
contrib/zk.lua

@@ -22,9 +22,10 @@ local keyword = token(l.KEYWORD, word_match{
 
 -- Types.
 local type = token(l.TYPE, word_match{
-  'EcPoint', 'EcFixedPoint', 'Base', 'BaseArray',
-  'Scalar', 'ScalarArray', 'MerklePath', 'Uint32',
-  'Uint64',
+  'EcPoint', 'EcFixedPoint', 'EcFixedPointBase', 'EcFixedPointShort',
+  'Base', 'BaseArray', 'Scalar', 'ScalarArray',
+  'MerklePath',
+  'Uint32', 'Uint64',
 })
 
 -- Instructions.

+ 2 - 2
proof/burn.zk

@@ -1,7 +1,7 @@
 constant "Burn" {
-	EcFixedPoint VALUE_COMMIT_VALUE,
+	EcFixedPointShort VALUE_COMMIT_VALUE,
 	EcFixedPoint VALUE_COMMIT_RANDOM,
-	EcFixedPoint NULLIFIER_K,
+	EcFixedPointBase NULLIFIER_K,
 }
 
 contract "Burn" {

+ 1 - 1
proof/mint.zk

@@ -1,5 +1,5 @@
 constant "Mint" {
-	EcFixedPoint VALUE_COMMIT_VALUE,
+	EcFixedPointShort VALUE_COMMIT_VALUE,
 	EcFixedPoint VALUE_COMMIT_RANDOM,
 }
 

+ 1 - 0
src/crypto/constants.rs

@@ -10,6 +10,7 @@ pub const MERKLE_DEPTH_ORCHARD: usize = 32;
 
 pub const L_ORCHARD_MERKLE: usize = 255;
 
+#[allow(dead_code)]
 /// $\ell^\mathsf{Orchard}_\mathsf{base}$
 pub(crate) const L_ORCHARD_BASE: usize = 255;
 

+ 2 - 0
src/crypto/constants/sinsemilla.rs

@@ -19,6 +19,7 @@ pub const INV_TWO_POW_K: [u8; 32] = [
 /// of Pallas.
 pub const C: usize = 253;
 
+#[allow(dead_code)]
 /// $\ell^\mathsf{Orchard}_\mathsf{Merkle}$
 pub(crate) const L_ORCHARD_MERKLE: usize = 255;
 
@@ -61,6 +62,7 @@ pub const Q_MERKLE_CRH: ([u8; 32], [u8; 32]) = (
     ],
 );
 
+#[allow(dead_code)]
 pub(crate) fn lebs2ip_k(bits: &[bool]) -> u32 {
     assert!(bits.len() == K);
     bits.iter().enumerate().fold(0u32, |acc, (i, b)| acc + if *b { 1 << i } else { 0 })

+ 1 - 2
src/crypto/spend_proof.rs

@@ -6,7 +6,7 @@ use halo2_gadgets::primitives::{
 };
 use incrementalmerkletree::Hashable;
 use log::debug;
-use pasta_curves::{arithmetic::CurveAffine, group::Curve, pallas};
+use pasta_curves::{arithmetic::CurveAffine, group::Curve};
 use rand::rngs::OsRng;
 
 use super::{
@@ -158,7 +158,6 @@ pub fn create_spend_proof(
         signature_secret,
     );
 
-    //let merkle_path: Vec<MerkleNode> = merkle_path.iter().map(|node| node.0).collect();
     let leaf_position: u64 = leaf_position.into();
 
     let c = SpendContract {

+ 0 - 4
src/zk/circuit/spend_contract.rs

@@ -245,10 +245,6 @@ impl Circuit<pallas::Base> for SpendContract {
         // Construct the ECC chip.
         let ecc_chip = config.ecc_chip();
 
-        // Construct the merkle chips
-        let merkle_chip_1 = config.merkle_chip_1();
-        let merkle_chip_2 = config.merkle_chip_2();
-
         // =========
         // Nullifier
         // =========

+ 3 - 4
src/zk/vm.rs

@@ -255,7 +255,7 @@ impl Circuit<pallas::Base> for ZkCircuit {
                 "VALUE_COMMIT_VALUE" => {
                     let vcv = ValueCommitV;
                     let vcv = FixedPointShort::from_inner(ecc_chip.clone(), vcv);
-                    stack.push(StackVar::FixedPointShort(vcv));
+                    stack.push(StackVar::EcFixedPointShort(vcv));
                 }
                 "VALUE_COMMIT_RANDOM" => {
                     let vcr = OrchardFixedBasesFull::ValueCommitR;
@@ -368,13 +368,12 @@ impl Circuit<pallas::Base> for ZkCircuit {
                     debug!("Executing `EcMulBase{:?}` opcode", opcode.1);
                     let args = &opcode.1;
 
-                    let lhs: FixedPointShort<pallas::Affine, EccChip<OrchardFixedBases>> =
+                    let lhs: FixedPointBaseField<pallas::Affine, EccChip<OrchardFixedBases>> =
                         stack[args[1]].clone().into();
 
                     let rhs: AssignedCell<Fp, Fp> = stack[args[0]].clone().into();
 
-                    let (ret, _) =
-                        lhs.mul(layouter.namespace(|| "EcMulBase()"), (rhs, one.clone()))?;
+                    let ret = lhs.mul(layouter.namespace(|| "EcMulBase()"), rhs)?;
 
                     debug!("Pushing result to stack index {}", stack.len());
                     stack.push(StackVar::EcPoint(ret));

+ 11 - 2
src/zk/vm_stack.rs

@@ -24,13 +24,13 @@ pub enum Witness {
 pub enum StackVar {
     EcPoint(Point<pallas::Affine, EccChip<OrchardFixedBases>>),
     EcFixedPoint(FixedPoint<pallas::Affine, EccChip<OrchardFixedBases>>),
+    EcFixedPointShort(FixedPointShort<pallas::Affine, EccChip<OrchardFixedBases>>),
     EcFixedPointBase(FixedPointBaseField<pallas::Affine, EccChip<OrchardFixedBases>>),
     Base(AssignedCell<pallas::Base, pallas::Base>),
     Scalar(Option<pallas::Scalar>),
     MerklePath(Option<[pallas::Base; 32]>),
     Uint32(Option<u32>),
     Uint64(Option<u64>),
-    FixedPointShort(FixedPointShort<EpAffine, EccChip<OrchardFixedBases>>),
 }
 
 impl From<StackVar> for Point<pallas::Affine, EccChip<OrchardFixedBases>> {
@@ -90,7 +90,16 @@ impl From<StackVar> for std::option::Option<[pallas::Base; 32]> {
 impl From<StackVar> for FixedPointShort<EpAffine, EccChip<OrchardFixedBases>> {
     fn from(value: StackVar) -> Self {
         match value {
-            StackVar::FixedPointShort(v) => v,
+            StackVar::EcFixedPointShort(v) => v,
+            _ => unimplemented!(),
+        }
+    }
+}
+
+impl From<StackVar> for FixedPointBaseField<EpAffine, EccChip<OrchardFixedBases>> {
+    fn from(value: StackVar) -> Self {
+        match value {
+            StackVar::EcFixedPointBase(v) => v,
             _ => unimplemented!(),
         }
     }

+ 6 - 2
src/zkas/opcode.rs

@@ -6,15 +6,19 @@ use super::types::Type;
 pub enum Opcode {
     /// Elliptic curve addition
     EcAdd = 0x00,
+
     /// Elliptic curve multiplication
     EcMul = 0x01,
+
     /// Elliptic curve multiplication with a Base field element
     EcMulBase = 0x02,
+
     /// Elliptic curve multiplication with a u64 wrapped in a Scalar element
     EcMulShort = 0x03,
 
     /// Get the x coordinate of an elliptic curve point
     EcGetX = 0x08,
+
     /// Get the y coordinate of an elliptic curve point
     EcGetY = 0x09,
 
@@ -39,8 +43,8 @@ impl Opcode {
             // (return_type, opcode_arg_types)
             Opcode::EcAdd => (vec![Type::EcPoint], vec![Type::EcPoint, Type::EcPoint]),
             Opcode::EcMul => (vec![Type::EcPoint], vec![Type::Scalar, Type::EcFixedPoint]),
-            Opcode::EcMulBase => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPoint]),
-            Opcode::EcMulShort => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPoint]),
+            Opcode::EcMulBase => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPointBase]),
+            Opcode::EcMulShort => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPointShort]),
             Opcode::EcGetX => (vec![Type::Base], vec![Type::EcPoint]),
             Opcode::EcGetY => (vec![Type::Base], vec![Type::EcPoint]),
             Opcode::PoseidonHash => (vec![Type::Base], vec![Type::BaseArray]),

+ 18 - 0
src/zkas/parser.rs

@@ -355,6 +355,24 @@ impl Parser {
                     });
                 }
 
+                "EcFixedPointShort" => {
+                    ret.push(Constant {
+                        name: k.to_string(),
+                        typ: Type::EcFixedPointShort,
+                        line: v.0.line,
+                        column: v.0.column,
+                    });
+                }
+
+                "EcFixedPointBase" => {
+                    ret.push(Constant {
+                        name: k.to_string(),
+                        typ: Type::EcFixedPointBase,
+                        line: v.0.line,
+                        column: v.0.column,
+                    });
+                }
+
                 x => {
                     self.error.emit(
                         format!("`{}` is an illegal constant type", x),

+ 10 - 0
src/zkas/types.rs

@@ -8,13 +8,21 @@ pub enum Type {
     /// Elliptic curve fixed point (a constant)
     EcFixedPoint = 0x01,
 
+    /// Elliptic curve fixed point short
+    EcFixedPointShort = 0x02,
+
+    /// Elliptic curve fixed point in base field
+    EcFixedPointBase = 0x03,
+
     /// Base field element
     Base = 0x10,
+
     /// Array of Base field elements
     BaseArray = 0x11,
 
     /// Scalar field element
     Scalar = 0x12,
+
     /// Array of Scalar field elements
     ScalarArray = 0x13,
 
@@ -36,6 +44,8 @@ impl Type {
         match b {
             0x00 => Self::EcPoint,
             0x01 => Self::EcFixedPoint,
+            0x02 => Self::EcFixedPointShort,
+            0x03 => Self::EcFixedPointBase,
             0x10 => Self::Base,
             0x11 => Self::BaseArray,
             0x12 => Self::Scalar,