Sfoglia il codice sorgente

zkas: Add binary decoder skeleton.

parazyd 4 anni fa
parent
commit
2418684555
4 ha cambiato i file con 82 aggiunte e 2 eliminazioni
  1. 1 1
      zkas/src/compiler.rs
  2. 73 0
      zkas/src/decoder.rs
  3. 2 0
      zkas/src/lib.rs
  4. 6 1
      zkas/src/main.rs

+ 1 - 1
zkas/src/compiler.rs

@@ -51,7 +51,7 @@ impl Compiler {
             tmp_stack.push(i.name.as_str());
             bincode.push(i.typ as u8);
             bincode.extend_from_slice(&serialize(&VarInt(stack_idx)));
-            bincode.extend_from_slice(i.name.as_bytes());
+            bincode.extend_from_slice(&serialize(&i.name));
             stack_idx += 1;
         }
 

+ 73 - 0
zkas/src/decoder.rs

@@ -0,0 +1,73 @@
+use darkfi::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 opcodes: Vec<(Opcode, u64, Vec<u64>)>,
+}
+
+impl ZkBinary {
+    pub fn decode(bytes: &[u8]) -> Result<Self> {
+        let magic_bytes = &bytes[0..4];
+        if magic_bytes != MAGIC_BYTES {
+            panic!()
+        }
+
+        let _binary_version = &bytes[4];
+
+        let constants_offset = match find_subslice(bytes, b".constant") {
+            Some(v) => v,
+            None => panic!(),
+        };
+
+        let contract_offset = match find_subslice(bytes, b".contract") {
+            Some(v) => v,
+            None => panic!(),
+        };
+
+        let circuit_offset = match find_subslice(bytes, b".circuit") {
+            Some(v) => v,
+            None => panic!(),
+        };
+
+        let debug_offset = match find_subslice(bytes, b".debug") {
+            Some(v) => v,
+            None => bytes.len(),
+        };
+
+        assert!(constants_offset < contract_offset);
+        assert!(contract_offset < circuit_offset);
+        assert!(circuit_offset < debug_offset);
+
+        let constants_section = &bytes[constants_offset + b".constant".len()..contract_offset];
+        let contract_section = &bytes[contract_offset + b".contract".len()..circuit_offset];
+        let circuit_section = &bytes[circuit_offset + b".circuit".len()..debug_offset];
+
+        let constants = ZkBinary::parse_constants(constants_section)?;
+        let witnesses = ZkBinary::parse_contract(contract_section)?;
+        let opcodes = ZkBinary::parse_circuit(circuit_section)?;
+        // TODO: Debug info
+
+        Ok(Self { constants, witnesses, opcodes })
+    }
+
+    fn parse_constants(_bytes: &[u8]) -> Result<Vec<(Type, u64, String)>> {
+        unimplemented!();
+    }
+
+    fn parse_contract(_bytes: &[u8]) -> Result<Vec<(Type, u64)>> {
+        unimplemented!();
+    }
+
+    fn parse_circuit(_bytes: &[u8]) -> Result<Vec<(Opcode, u64, Vec<u64>)>> {
+        unimplemented!();
+    }
+}
+
+// https://stackoverflow.com/questions/35901547/how-can-i-find-a-subsequence-in-a-u8-slice
+fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
+    haystack.windows(needle.len()).position(|window| window == needle)
+}

+ 2 - 0
zkas/src/lib.rs

@@ -4,6 +4,8 @@ pub mod analyzer;
 pub mod ast;
 /// Compiler
 pub mod compiler;
+/// Binary decoder
+pub mod decoder;
 /// Lexer module
 pub mod lexer;
 /// Language opcodes

+ 6 - 1
zkas/src/main.rs

@@ -5,7 +5,9 @@ use std::{
     io::Write,
 };
 
-use zkas::{analyzer::Analyzer, compiler::Compiler, lexer::Lexer, parser::Parser};
+use zkas::{
+    analyzer::Analyzer, compiler::Compiler, decoder::ZkBinary, lexer::Lexer, parser::Parser,
+};
 
 #[derive(clap::Parser)]
 #[clap(name = "zkas", version)]
@@ -72,5 +74,8 @@ fn main() -> Result<()> {
     file.write_all(&bincode)?;
     println!("Wrote output to {}", &output);
 
+    let zkbin = ZkBinary::decode(&bincode)?;
+    println!("{:#?}", zkbin);
+
     Ok(())
 }