Bladeren bron

zkas: Implement constants and witness bincode decoding.

parazyd 4 jaren geleden
bovenliggende
commit
1540d52f53
3 gewijzigde bestanden met toevoegingen van 49 en 13 verwijderingen
  1. 0 5
      zkas/src/compiler.rs
  2. 33 8
      zkas/src/decoder.rs
  3. 16 0
      zkas/src/types.rs

+ 0 - 5
zkas/src/compiler.rs

@@ -41,8 +41,6 @@ impl Compiler {
         bincode.extend_from_slice(&MAGIC_BYTES);
         bincode.push(BINARY_VERSION);
 
-        let mut stack_idx: u64 = 0;
-
         // Temporary stack vector for lookups
         let mut tmp_stack = vec![];
 
@@ -51,14 +49,12 @@ impl Compiler {
             tmp_stack.push(i.name.as_str());
             bincode.push(i.typ as u8);
             bincode.extend_from_slice(&serialize(&i.name));
-            stack_idx += 1;
         }
 
         bincode.extend_from_slice(b".contract");
         for i in &self.witnesses {
             tmp_stack.push(i.name.as_str());
             bincode.push(i.typ as u8);
-            stack_idx += 1;
         }
 
         bincode.extend_from_slice(b".circuit");
@@ -66,7 +62,6 @@ impl Compiler {
             match i.typ {
                 StatementType::Assignment => {
                     tmp_stack.push(&i.variable.as_ref().unwrap().name);
-                    stack_idx += 1;
                 }
                 // In case of a simple call, we don't append anything to the stack
                 StatementType::Call => {}

+ 33 - 8
zkas/src/decoder.rs

@@ -1,11 +1,14 @@
-use darkfi::Result;
+use darkfi::{
+    util::serial::{deserialize_partial, VarInt},
+    Result,
+};
 
 use crate::{compiler::MAGIC_BYTES, opcode::Opcode, types::Type};
 
 #[derive(Debug)]
 pub struct ZkBinary {
-    pub constants: Vec<(Type, u64, String)>,
-    pub witnesses: Vec<(Type, u64)>,
+    pub constants: Vec<(Type, String)>,
+    pub witnesses: Vec<Type>,
     pub opcodes: Vec<(Opcode, u64, Vec<u64>)>,
 }
 
@@ -54,16 +57,38 @@ impl ZkBinary {
         Ok(Self { constants, witnesses, opcodes })
     }
 
-    fn parse_constants(_bytes: &[u8]) -> Result<Vec<(Type, u64, String)>> {
-        unimplemented!();
+    fn parse_constants(bytes: &[u8]) -> Result<Vec<(Type, String)>> {
+        let mut constants = vec![];
+
+        let mut iter_offset = 0;
+        while iter_offset < bytes.len() {
+            let c_type = Type::from_repr(bytes[iter_offset]);
+            iter_offset += 1;
+            let (name, offset) = deserialize_partial::<String>(&bytes[iter_offset..])?;
+            iter_offset += offset;
+
+            constants.push((c_type, name));
+        }
+
+        Ok(constants)
     }
 
-    fn parse_contract(_bytes: &[u8]) -> Result<Vec<(Type, u64)>> {
-        unimplemented!();
+    fn parse_contract(bytes: &[u8]) -> Result<Vec<Type>> {
+        let mut witnesses = vec![];
+
+        let mut iter_offset = 0;
+        while iter_offset < bytes.len() {
+            let w_type = Type::from_repr(bytes[iter_offset]);
+            iter_offset += 1;
+
+            witnesses.push(w_type);
+        }
+
+        Ok(witnesses)
     }
 
     fn parse_circuit(_bytes: &[u8]) -> Result<Vec<(Opcode, u64, Vec<u64>)>> {
-        unimplemented!();
+        Ok(vec![])
     }
 }
 

+ 16 - 0
zkas/src/types.rs

@@ -27,3 +27,19 @@ pub enum Type {
     /// Intermediate type, should never appear in the result
     Dummy = 0xff,
 }
+
+impl Type {
+    pub fn from_repr(b: u8) -> Self {
+        match b {
+            0 => Self::EcPoint,
+            1 => Self::EcFixedPoint,
+            16 => Self::Base,
+            17 => Self::BaseArray,
+            18 => Self::Scalar,
+            19 => Self::ScalarArray,
+            32 => Self::MerklePath,
+            48 => Self::Uint32,
+            _ => unimplemented!(),
+        }
+    }
+}