فهرست منبع

zkas: Add basic debug prints

parazyd 3 سال پیش
والد
کامیت
a326a67901
9فایلهای تغییر یافته به همراه22 افزوده شده و 11 حذف شده
  1. 1 0
      contrib/zk.lang
  2. 1 1
      contrib/zk.lua
  3. 1 1
      contrib/zk.vim
  4. 8 0
      src/zk/vm.rs
  5. 3 5
      src/zkas/analyzer.rs
  6. 1 1
      src/zkas/decoder.rs
  7. 2 2
      src/zkas/lexer.rs
  8. 1 1
      src/zkas/opcode.rs
  9. 4 0
      src/zkas/types.rs

+ 1 - 0
contrib/zk.lang

@@ -72,6 +72,7 @@
       <keyword>constrain_equal_base</keyword>
       <keyword>constrain_equal_base</keyword>
       <keyword>constrain_equal_point</keyword>
       <keyword>constrain_equal_point</keyword>
       <keyword>constrain_instance</keyword>
       <keyword>constrain_instance</keyword>
+      <keyword>debug</keyword>
     </context>
     </context>
     
     
     <context id="zkas" class="no-spell-check">
     <context id="zkas" class="no-spell-check">

+ 1 - 1
contrib/zk.lua

@@ -45,7 +45,7 @@ local instruction = token('instruction', word_match{
   'range_check', 'less_than_strict', 'less_than_loose', 'bool_check',
   'range_check', 'less_than_strict', 'less_than_loose', 'bool_check',
   'witness_base',
   'witness_base',
   'constrain_equal_base', 'constrain_equal_point',
   'constrain_equal_base', 'constrain_equal_point',
-  'constrain_instance',
+  'constrain_instance', 'debug',
 })
 })
 
 
 -- Identifiers.
 -- Identifiers.

+ 1 - 1
contrib/zk.vim

@@ -25,7 +25,7 @@ syn keyword zkasInstruction
     \ range_check less_than_strict less_than_loose  bool_check
     \ range_check less_than_strict less_than_loose  bool_check
     \ witness_base
     \ witness_base
     \ constrain_equal_base constrain_equal_point
     \ constrain_equal_base constrain_equal_point
-    \ constrain_instance
+    \ constrain_instance debug
 
 
 syn region zkasString start='"' end='"' contained
 syn region zkasString start='"' end='"' contained
 
 

+ 8 - 0
src/zk/vm.rs

@@ -849,6 +849,14 @@ impl Circuit<pallas::Base> for ZkCircuit {
                     public_inputs_offset += 1;
                     public_inputs_offset += 1;
                 }
                 }
 
 
+                Opcode::DebugPrint => {
+                    trace!(target: "zk::vm", "Executing `DebugPrint{:?}` opcode", opcode.1);
+                    let args = &opcode.1;
+
+                    println!("[ZKVM DEBUG] HEAP INDEX: {}", args[0].1);
+                    println!("[ZKVM DEBUG] {:#?}", heap[args[0].1]);
+                }
+
                 _ => {
                 _ => {
                     error!(target: "zk::vm", "Unsupported opcode");
                     error!(target: "zk::vm", "Unsupported opcode");
                     return Err(plonk::Error::Synthesis)
                     return Err(plonk::Error::Synthesis)

+ 3 - 5
src/zkas/analyzer.rs

@@ -122,8 +122,8 @@ impl Analyzer {
                 // opcode has a return value. When executed we will push
                 // opcode has a return value. When executed we will push
                 // this value onto the heap and use it as a reference to
                 // this value onto the heap and use it as a reference to
                 // the actual statement we're parsing at this moment.
                 // the actual statement we're parsing at this moment.
-                // TODO: FIXME: This needs a recursive algorithm, as this
-                //              only allows a single nested function.
+                // TODO: This needs a recursive algorithm, as this only
+                //       allows a single nested function.
                 if let Arg::Func(func) = arg {
                 if let Arg::Func(func) = arg {
                     let (f_return_types, f_arg_types) = func.opcode.arg_types();
                     let (f_return_types, f_arg_types) = func.opcode.arg_types();
                     if f_return_types.is_empty() {
                     if f_return_types.is_empty() {
@@ -144,7 +144,6 @@ impl Analyzer {
                         column: func.lhs.clone().unwrap().column,
                         column: func.lhs.clone().unwrap().column,
                     };
                     };
 
 
-                    // FIXME: Needs better *Array handling.
                     if arg_types[0] == VarType::BaseArray {
                     if arg_types[0] == VarType::BaseArray {
                         if f_return_types[0] != VarType::Base {
                         if f_return_types[0] != VarType::Base {
                             self.error.abort(
                             self.error.abort(
@@ -280,7 +279,6 @@ impl Analyzer {
                             Var::Variable(c) => (c.typ, c.line, c.column),
                             Var::Variable(c) => (c.typ, c.line, c.column),
                         };
                         };
 
 
-                        // FIXME: Better array handling
                         if arg_types[0] == VarType::BaseArray {
                         if arg_types[0] == VarType::BaseArray {
                             if var_type != VarType::Base {
                             if var_type != VarType::Base {
                                 self.error.abort(
                                 self.error.abort(
@@ -305,7 +303,7 @@ impl Analyzer {
                                     v.column,
                                     v.column,
                                 );
                                 );
                             }
                             }
-                        } else if var_type != arg_types[idx] {
+                        } else if var_type != arg_types[idx] && arg_types[idx] != VarType::Any {
                             self.error.abort(
                             self.error.abort(
                                 &format!(
                                 &format!(
                                     "Incorrect argument type. Expected `{:?}`, got `{:?}`.",
                                     "Incorrect argument type. Expected `{:?}`, got `{:?}`.",

+ 1 - 1
src/zkas/decoder.rs

@@ -208,7 +208,7 @@ impl ZkBinary {
                         return Err(ZkasErr(format!("Could not decode HeapType from {}", heap_type)))
                         return Err(ZkasErr(format!("Could not decode HeapType from {}", heap_type)))
                     }
                     }
                 };
                 };
-                args.push((heap_type, heap_index.0 as usize)); // FIXME, why?
+                args.push((heap_type, heap_index.0 as usize));
             }
             }
 
 
             opcodes.push((opcode, args));
             opcodes.push((opcode, args));

+ 2 - 2
src/zkas/lexer.rs

@@ -161,8 +161,8 @@ impl<'a> Lexer<'a> {
                 }
                 }
 
 
                 if in_string {
                 if in_string {
-                    // TODO: Perhaps forbid whitespace.
-                    buf.push(c);
+                    // For now we forbid whitespace in strings.
+                    self.error.abort("Strings/Namespaces can't contain whitespace", lineno, column);
                 }
                 }
 
 
                 continue
                 continue

+ 1 - 1
src/zkas/opcode.rs

@@ -200,7 +200,7 @@ impl Opcode {
 
 
             Opcode::ConstrainInstance => (vec![], vec![VarType::Base]),
             Opcode::ConstrainInstance => (vec![], vec![VarType::Base]),
 
 
-            Opcode::DebugPrint => (vec![], vec![]),
+            Opcode::DebugPrint => (vec![], vec![VarType::Any]),
         }
         }
     }
     }
 }
 }

+ 4 - 0
src/zkas/types.rs

@@ -76,6 +76,9 @@ pub enum VarType {
 
 
     /// Unsigned 64-bit integer
     /// Unsigned 64-bit integer
     Uint64 = 0x31,
     Uint64 = 0x31,
+
+    /// Catch-all for any type
+    Any = 0xff,
 }
 }
 
 
 impl VarType {
 impl VarType {
@@ -93,6 +96,7 @@ impl VarType {
             0x20 => Some(Self::MerklePath),
             0x20 => Some(Self::MerklePath),
             0x30 => Some(Self::Uint32),
             0x30 => Some(Self::Uint32),
             0x31 => Some(Self::Uint64),
             0x31 => Some(Self::Uint64),
+            0xff => Some(Self::Any),
             _ => None,
             _ => None,
         }
         }
     }
     }