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

zkas: Move toolchain into main library and the binary into bin/ namespace.

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

+ 3 - 1
.gitignore

@@ -2,7 +2,9 @@
 *.sage.py
 target/*
 
-/zkas/zkas
+/proofs/*.bin
+
+/zkas
 /drk
 /darkfid
 /cashierd

+ 10 - 4
Cargo.lock

@@ -1376,6 +1376,8 @@ dependencies = [
  "halo2_gadgets",
  "hex",
  "incrementalmerkletree",
+ "indexmap",
+ "itertools",
  "lazy_static",
  "libsqlite3-sys",
  "log",
@@ -5681,6 +5683,13 @@ version = "0.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
 
+[[package]]
+name = "vm"
+version = "0.3.0"
+dependencies = [
+ "darkfi",
+]
+
 [[package]]
 name = "wait-timeout"
 version = "0.2.0"
@@ -6004,14 +6013,11 @@ dependencies = [
 
 [[package]]
 name = "zkas"
-version = "0.0.1"
+version = "0.3.0"
 dependencies = [
  "anyhow",
  "clap 3.0.7",
  "darkfi",
- "indexmap",
- "itertools",
- "termion",
 ]
 
 [[package]]

+ 17 - 1
Cargo.toml

@@ -13,13 +13,14 @@ name = "darkfi"
 
 [workspace]
 members = [
+    "bin/zkas",
     "bin/cashierd",
     "bin/darkfid",
     "bin/drk",
     "bin/gatewayd",
     "bin/ircd",
     "bin/map",
-    "zkas",
+    "bin/vm",
 ]
 
 [dependencies]
@@ -55,6 +56,8 @@ dirs = {version = "4.0.0", optional = true}
 subtle = {version = "2.4.1", optional = true}
 lazy_static = {version = "1.4.0", optional = true}
 clap = {version = "3.0.7", features = ["derive"], optional = true}
+indexmap = {version = "1.7.0", optional = true}
+itertools = {version = "0.10.3", optional = true}
 
 # Misc
 termion = {version = "1.5.6", optional = true}
@@ -213,6 +216,14 @@ node = [
 	"net",
 ]
 
+zkas = [
+    "termion",
+    "indexmap",
+    "itertools",
+
+    "util",
+]
+
 
 [[example]]
 name = "net"
@@ -243,3 +254,8 @@ required-features = ["node"]
 name = "tree"
 path = "example/tree.rs"
 required-features = ["crypto"]
+
+[[example]]
+name = "vm2"
+path = "example/vm2.rs"
+required-features = ["crypto"]

+ 1 - 2
Makefile

@@ -7,14 +7,13 @@ PREFIX = /usr/local
 CARGO = cargo
 
 # Binaries to be built
-BINS = drk darkfid gatewayd
+BINS = drk darkfid gatewayd zkas
 
 # Common dependencies which should force the binaries to be rebuilt
 BINDEPS = \
 	Cargo.toml \
 	$(shell find bin/*/src -type f) \
 	$(shell find bin -type f -name '*.toml') \
-	$(shell find zkas -type f) \
 	$(shell find src -type f) \
 	$(shell find sql -type f) \
 	$(shell find contrib/token -type f)

+ 13 - 0
bin/zkas/Cargo.toml

@@ -0,0 +1,13 @@
+[package]
+name = "zkas"
+version = "0.3.0"
+edition = "2021"
+
+[dependencies.darkfi]
+path= "../../"
+features = ["zkas"]
+
+[dependencies]
+# Misc
+anyhow = "1.0.52"
+clap = {version = "3.0.7", features = ["derive"]}

+ 15 - 16
zkas/src/main.rs → bin/zkas/src/main.rs

@@ -1,17 +1,18 @@
-use anyhow::Result;
-use clap::Parser as ClapParser;
 use std::{
     fs::{read_to_string, File},
     io::Write,
 };
 
-use zkas::{
+use anyhow::Result;
+use clap::Parser as ClapParser;
+
+use darkfi::zkas::{
     analyzer::Analyzer, compiler::Compiler, decoder::ZkBinary, lexer::Lexer, parser::Parser,
 };
 
 #[derive(clap::Parser)]
 #[clap(name = "zkas", version)]
-struct Cli {
+struct Args {
     /// Place the output into <FILE>
     #[clap(short, value_name = "FILE")]
     output: Option<String>,
@@ -37,9 +38,9 @@ struct Cli {
 }
 
 fn main() -> Result<()> {
-    let cli = Cli::parse();
+    let args = Args::parse();
 
-    let filename = cli.input.as_str();
+    let filename = args.input.as_str();
     let source = read_to_string(filename)?;
 
     let lexer = Lexer::new(filename, source.chars());
@@ -51,11 +52,11 @@ fn main() -> Result<()> {
     let mut analyzer = Analyzer::new(filename, source.chars(), constants, witnesses, statements);
     analyzer.analyze_types();
 
-    if cli.interactive {
+    if args.interactive {
         analyzer.analyze_semantic();
     }
 
-    if cli.evaluate {
+    if args.evaluate {
         println!("{:#?}", analyzer.constants);
         println!("{:#?}", analyzer.witnesses);
         println!("{:#?}", analyzer.statements);
@@ -69,23 +70,21 @@ fn main() -> Result<()> {
         analyzer.constants,
         analyzer.witnesses,
         analyzer.statements,
-        !cli.strip,
+        !args.strip,
     );
 
     let bincode = compiler.compile();
 
-    let output: String;
-    if let Some(o) = cli.output {
-        output = o;
-    } else {
-        output = format!("{}.bin", cli.input);
-    }
+    let output = match args.output {
+        Some(o) => o,
+        None => format!("{}.bin", args.input),
+    };
 
     let mut file = File::create(&output)?;
     file.write_all(&bincode)?;
     println!("Wrote output to {}", &output);
 
-    if cli.examine {
+    if args.examine {
         let zkbin = ZkBinary::decode(&bincode)?;
         println!("{:#?}", zkbin);
     }

+ 0 - 1
book/src/zkas/zkas.md

@@ -1 +0,0 @@
-../../../zkas/README.md

+ 16 - 0
book/src/zkas/zkas.md

@@ -0,0 +1,16 @@
+zkas
+====
+
+zkas is a compiler for the Halo2 zkVM language used in
+[DarkFi](https://github.com/darkrenaissance/darkfi).
+
+The current implementation found in the DarkFi repository inside
+[`src/zkas`](https://github.com/darkrenaissance/darkfi/tree/master/src/zkas)
+is the reference compiler and language implementation. It is a
+toolchain consisting of a lexer, parser, static and semantic analyzers,
+and a binary code compiler.
+
+The
+[`main.rs`](https://github.com/darkrenaissance/darkfi/blob/master/bin/zkas/src/main.rs)
+file shows how this toolchain is put together to produce binary code
+from source code.

+ 0 - 0
zkas/proofs/burn.zk → proofs/burn.zk


+ 0 - 0
zkas/proofs/mint.zk → proofs/mint.zk


+ 15 - 0
proofs/poseidonhash.zk

@@ -0,0 +1,15 @@
+constant "Poseidon" {
+	EcFixedPoint VALUE_COMMIT_VALUE,
+	EcFixedPoint VALUE_COMMIT_RANDOM,
+}
+
+contract "Poseidon" {
+	Base foo,
+	Base bar,
+}
+
+circuit "Poseidon" {
+	# Poseidon hash of the coin
+	C = poseidon_hash(foo, bar);
+	constrain_instance(C);
+}

+ 0 - 0
zkas/proofs/voting.zk → proofs/voting.zk


+ 3 - 0
src/lib.rs

@@ -33,3 +33,6 @@ pub mod util;
 
 #[cfg(feature = "rpc")]
 pub mod rpc;
+
+#[cfg(feature = "zkas")]
+pub mod zkas;

+ 1 - 1
zkas/src/analyzer.rs → src/zkas/analyzer.rs

@@ -7,7 +7,7 @@ use std::{
 
 use termion::{color, style};
 
-use crate::{
+use super::{
     ast::{
         Constant, Constants, StatementType, Statements, Var, Variable, Variables, Witness,
         Witnesses,

+ 1 - 1
zkas/src/ast.rs → src/zkas/ast.rs

@@ -1,6 +1,6 @@
 use indexmap::IndexMap;
 
-use crate::{lexer::Token, opcode::Opcode, types::Type};
+use super::{lexer::Token, opcode::Opcode, types::Type};
 
 #[derive(Copy, PartialEq, Clone, Debug)]
 #[repr(u8)]

+ 2 - 2
zkas/src/compiler.rs → src/zkas/compiler.rs

@@ -1,9 +1,9 @@
 use std::{io, io::Write, process, str::Chars};
 
-use darkfi::util::serial::{serialize, VarInt};
 use termion::{color, style};
 
-use crate::ast::{Constants, StatementType, Statements, Witnesses};
+use super::ast::{Constants, StatementType, Statements, Witnesses};
+use crate::util::serial::{serialize, VarInt};
 
 /// Version of the binary
 pub const BINARY_VERSION: u8 = 1;

+ 2 - 3
zkas/src/decoder.rs → src/zkas/decoder.rs

@@ -1,11 +1,10 @@
-use darkfi::{
+use super::{compiler::MAGIC_BYTES, opcode::Opcode, types::Type};
+use crate::{
     util::serial::{deserialize_partial, VarInt},
     Error::ZkasDecoderError,
     Result,
 };
 
-use crate::{compiler::MAGIC_BYTES, opcode::Opcode, types::Type};
-
 #[derive(Debug)]
 pub struct ZkBinary {
     pub constants: Vec<(Type, String)>,

+ 0 - 0
zkas/src/lexer.rs → src/zkas/lexer.rs


+ 0 - 0
zkas/src/lib.rs → src/zkas/mod.rs


+ 1 - 1
zkas/src/opcode.rs → src/zkas/opcode.rs

@@ -1,4 +1,4 @@
-use crate::types::Type;
+use super::types::Type;
 
 /// Opcodes supported by the VM
 #[derive(Copy, Clone, Debug)]

+ 1 - 1
zkas/src/parser.rs → src/zkas/parser.rs

@@ -4,7 +4,7 @@ use indexmap::IndexMap;
 use itertools::Itertools;
 use termion::{color, style};
 
-use crate::{
+use super::{
     ast::{
         Constant, Constants, Statement, StatementType, Statements, UnparsedConstants,
         UnparsedWitnesses, Variable, Witness, Witnesses,

+ 0 - 0
zkas/src/types.rs → src/zkas/types.rs


+ 0 - 1
zkas/.gitignore

@@ -1 +0,0 @@
-*.bin

+ 0 - 19
zkas/Cargo.toml

@@ -1,19 +0,0 @@
-[package]
-name = "zkas"
-version = "0.0.1"
-homepage = "https://dark.fi"
-authors = ["darkfi <dev@dark.fi>"]
-repository = "https://github.com/darkrenaissance/darkfi"
-license = "AGPL-3.0-only"
-edition = "2021"
-
-[dependencies.darkfi]
-path = "../"
-features = ["util"]
-
-[dependencies]
-anyhow = "1.0.52"
-clap = {version = "3.0.0", features = ["derive"]}
-indexmap = "1.7.0"
-itertools = "0.10.3"
-termion = "1.5.6"

+ 0 - 34
zkas/Makefile

@@ -1,34 +0,0 @@
-.POSIX:
-
-# Cargo binary
-CARGO = cargo
-
-BINS = zkas
-
-# Dependencies which should force the binaries to be rebuilt
-BINDEPS = \
-	Cargo.toml \
-	$(shell find src -type f)
-
-all: $(BINS)
-
-$(BINS): $(BINDEPS)
-	$(CARGO) build --release --all-features --bin $@
-	cp -f ../target/release/$@ $@
-
-test:
-	$(CARGO) test --release --all-features
-
-fix:
-	$(CARGO) clippy --release --all-features --fix --allow-dirty
-
-clippy:
-	$(CARGO) clippy --release --all-features
-
-clean:
-	rm -f $(BINS)
-
-distclean: clean
-	rm -rf target
-
-.PHONY: all test fix clippy clean distclean

+ 0 - 18
zkas/README.md

@@ -1,18 +0,0 @@
-zkas
-====
-
-zkas is a compiler for the Halo2 zkVM langage used in
-[DarkFi](https://github.com/darkrenaissance/darkfi).
-
-The documentation on both the compiler and the language can be found
-in the book: <https://darkrenaissance.github.io/darkfi/zkas/zkas.html>
-
-The current implementation found in the DarkFi repository inside
-<https://github.com/darkrenaissance/darkfi/tree/master/zkas> is the
-reference compiler and language implementation. It is a toolchain
-consisting of a lexer, parser, static and semantic analyzers, and a
-binary code compiler.
-
-The [`main.rs`](https://github.com/darkrenaissance/darkfi/blob/master/zkas/src/main.rs)
-file shows how this toolchain is put together to produce binary code
-from source code.