Parcourir la source

zkas/decoder: Implement circuit decoding.

parazyd il y a 4 ans
Parent
commit
bf78e0dcac
2 fichiers modifiés avec 38 ajouts et 3 suppressions
  1. 23 3
      zkas/src/decoder.rs
  2. 15 0
      zkas/src/opcode.rs

+ 23 - 3
zkas/src/decoder.rs

@@ -9,7 +9,7 @@ use crate::{compiler::MAGIC_BYTES, opcode::Opcode, types::Type};
 pub struct ZkBinary {
     pub constants: Vec<(Type, String)>,
     pub witnesses: Vec<Type>,
-    pub opcodes: Vec<(Opcode, u64, Vec<u64>)>,
+    pub opcodes: Vec<(Opcode, Vec<u64>)>,
 }
 
 impl ZkBinary {
@@ -87,8 +87,28 @@ impl ZkBinary {
         Ok(witnesses)
     }
 
-    fn parse_circuit(_bytes: &[u8]) -> Result<Vec<(Opcode, u64, Vec<u64>)>> {
-        Ok(vec![])
+    fn parse_circuit(bytes: &[u8]) -> Result<Vec<(Opcode, Vec<u64>)>> {
+        let mut opcodes = vec![];
+
+        let mut iter_offset = 0;
+        while iter_offset < bytes.len() {
+            let opcode = Opcode::from_repr(bytes[iter_offset]);
+            iter_offset += 1;
+
+            let (arg_num, offset) = deserialize_partial::<VarInt>(&bytes[iter_offset..])?;
+            iter_offset += offset;
+
+            let mut args = vec![];
+            for _ in 0..arg_num.0 {
+                let (stack_index, offset) = deserialize_partial::<VarInt>(&bytes[iter_offset..])?;
+                iter_offset += offset;
+                args.push(stack_index.0);
+            }
+
+            opcodes.push((opcode, args));
+        }
+
+        Ok(opcodes)
     }
 }
 

+ 15 - 0
zkas/src/opcode.rs

@@ -51,4 +51,19 @@ impl Opcode {
             Opcode::Noop => (vec![], vec![]),
         }
     }
+
+    pub fn from_repr(b: u8) -> Self {
+        match b {
+            0 => Self::EcAdd,
+            1 => Self::EcMul,
+            2 => Self::EcMulBase,
+            3 => Self::EcMulShort,
+            8 => Self::EcGetX,
+            9 => Self::EcGetY,
+            16 => Self::PoseidonHash,
+            32 => Self::CalculateMerkleRoot,
+            240 => Self::ConstrainInstance,
+            _ => unimplemented!(),
+        }
+    }
 }