Skip to content

Commit

Permalink
Merge pull request #222 from zksecurity/fix/test-cmd-stdlib
Browse files Browse the repository at this point in the history
a couple fixes
  • Loading branch information
mimoo authored Nov 8, 2024
2 parents 2f4340e + a53967e commit 3a77c86
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 62 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/snarkjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ fi
DIR_PATH=$1
CURVE=$2

# Init stdlib in .noname/release/src/stdlib instead of downloading
echo "Overriding stdlib in .noname/release/src/stdlib..."
mkdir -p ~/.noname/release/src/stdlib/ && cp -r /app/noname/src/stdlib/* ~/.noname/release/src/stdlib/

# Ensure the circuit directory exists and is initialized
echo "Initializing a new Noname package..."
noname new --path circuit_noname
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
name = "noname"
version = "0.7.0"
edition = "2021"
build = "build.rs"
description = "a programming language for writing zkapps"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
fs_extra = "1.3.0"
dirs = "4.0"

[dependencies]
ark-ec = "0.3.0" # elliptic curve library
ark-ff = "0.3.0"
Expand Down
27 changes: 27 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::{env, path::PathBuf};

use fs_extra::dir::{self, CopyOptions};

fn main() {
let home_dir: PathBuf =
dirs::home_dir().expect("could not find home directory of current user");

let noname_dir = home_dir.join(".noname");
let release_dir = noname_dir.join("release");

// If it exists, then remove it
if release_dir.exists() {
fs_extra::remove_items(&[&release_dir]).expect("could not remove release directory");
}

let current_dir = env::current_dir().expect("could not get current directory");

// Set up copy options
let mut options = CopyOptions::new();
options.overwrite = true; // Overwrite existing files
options.copy_inside = true; // Copy contents inside the source directory

// Copy the current folder to the release directory
dir::copy(current_dir, &release_dir, &options)
.expect("could not copy current directory to release directory");
}
2 changes: 1 addition & 1 deletion src/backends/kimchi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,6 @@ impl Backend for KimchiVesta {
msg: String,
span: Span,
) {
todo!()
println!("todo: implement log_var for kimchi backend");
}
}
31 changes: 22 additions & 9 deletions src/cli/cmd_build_and_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use crate::{
};

use super::packages::{
download_stdlib, get_deps_of_package, is_lib, validate_package_and_get_manifest,
DependencyGraph, UserRepo,
get_deps_of_package, is_lib, validate_package_and_get_manifest, DependencyGraph, UserRepo,
};

const COMPILED_DIR: &str = "compiled";
Expand Down Expand Up @@ -165,13 +164,7 @@ fn add_stdlib<B: Backend>(
let mut node_id = node_id;

// check if the release folder exists, otherwise download the latest release
// todo: check the latest version and compare it with the current version, to decide if download is needed
let stdlib_dir = path_to_stdlib();

if !stdlib_dir.exists() {
download_stdlib()?;
}

node_id = init_stdlib_dep(sources, tast, node_id, stdlib_dir.as_ref(), server_mode);

Ok(node_id)
Expand Down Expand Up @@ -456,7 +449,27 @@ fn test_r1cs_backend<F: BackendField>(
where
F: BackendField,
{
let (tast, sources) = typecheck_file(path)?;
let mut sources = Sources::new();
let mut node_id = 0;

let mut tast = TypeChecker::new();

// adding stdlib
node_id = add_stdlib(&mut sources, &mut tast, node_id, &mut None)?;

let code = std::fs::read_to_string(path)
.into_diagnostic()
.wrap_err_with(|| format!("could not read file `{path}`"))?;

typecheck_next_file(
&mut tast,
None,
&mut sources,
path.to_string(),
code,
node_id,
&mut None,
)?;

let compiled_circuit = compile(&sources, tast, r1cs, &mut None)?;

Expand Down
44 changes: 5 additions & 39 deletions src/cli/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,17 @@ pub(crate) fn path_to_package(dep: &UserRepo) -> PathBuf {
}

pub(crate) fn path_to_stdlib() -> PathBuf {
path_to_release_dir().join(STDLIB_DIRECTORY)
}

pub(crate) fn path_to_release_dir() -> PathBuf {
let home_dir: PathBuf = dirs::home_dir()
.expect("could not find home directory of current user")
.try_into()
.expect("invalid UTF8 path");
let noname_dir = home_dir.join(NONAME_DIRECTORY);

noname_dir.join(RELEASE_DIRECTORY).join(STDLIB_DIRECTORY)
noname_dir.join(RELEASE_DIRECTORY)
}

/// download package from github
Expand All @@ -276,44 +280,6 @@ pub fn download_from_github(dep: &UserRepo) -> Result<()> {
Ok(())
}

pub fn download_stdlib() -> Result<()> {
// Hardcoded repository details and target branch
let repo_owner = "zksecurity";
let repo_name = "noname";
let target_branch = "main";
let repo_url = format!(
"https://github.com/{owner}/{repo}.git",
owner = repo_owner,
repo = repo_name
);

let home_dir: PathBuf = dirs::home_dir()
.expect("could not find home directory of current user")
.try_into()
.expect("invalid UTF8 path");
let noname_dir = home_dir.join(NONAME_DIRECTORY);
let release_dir = noname_dir.join("release");

// Clone the repository and checkout the specified branch to the temporary directory
let output = process::Command::new("git")
.arg("clone")
.arg("--branch")
.arg(target_branch)
.arg("--single-branch")
.arg(repo_url)
.arg(release_dir)
.output()
.expect("failed to execute git clone command");

if !output.status.success() {
miette::bail!(format!(
"Could not clone branch `{target_branch}` of repository `{repo_owner}/{repo_name}`."
));
}

Ok(())
}

pub fn is_lib(path: &PathBuf) -> bool {
path.join("src").join("lib.no").exists()
}
Expand Down
10 changes: 8 additions & 2 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::thread;
use tokio::sync::{mpsc, Mutex};
use tower_http::services::ServeDir;

use crate::cli::packages::path_to_release_dir;

//
// The interface for the rest of the compiler which doens't use async/await
//
Expand Down Expand Up @@ -104,7 +106,10 @@ async fn run_server(tx: mpsc::Sender<ServerMessage>, rx: mpsc::Receiver<Compiler
});

// build our application with a route
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
// let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
// load manifest dir from the environment, otherwise use the release directory
let manifest_dir =
std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| path_to_release_dir().to_string());
let assets_dir = std::path::Path::new(&manifest_dir).join("assets");

let app = Router::new()
Expand Down Expand Up @@ -183,7 +188,8 @@ async fn get_state(
mod tests {
use super::*;

#[test]
// #[test]
// disabled for now because it hangs in test runner
fn test_server() {
let (handle, _) = ServerShim::start_server();
// wait on handle
Expand Down
7 changes: 2 additions & 5 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,17 @@ pub fn init_stdlib_dep<B: Backend>(
for lib in libs {
let module = UserRepo::new(&format!("std/{}", lib));
let prefix_stdlib = Path::new(path_prefix);
let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}.no"))).unwrap();
let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}/lib.no"))).unwrap();
node_id = typecheck_next_file(
tast,
Some(module),
sources,
lib.to_string(),
code,
0,
node_id,
server_mode,
)
.unwrap();
let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}/lib.no"))).unwrap();
node_id =
typecheck_next_file(tast, Some(module), sources, lib.to_string(), code, 0).unwrap();
}

node_id
Expand Down
10 changes: 8 additions & 2 deletions src/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,21 @@ fn test_file(
let mut sources = Sources::new();
let mut tast = TypeChecker::new();
let mut node_id = 0;
node_id = init_stdlib_dep(&mut sources, &mut tast, node_id, STDLIB_DIRECTORY);
node_id = init_stdlib_dep(
&mut sources,
&mut tast,
node_id,
STDLIB_DIRECTORY,
&mut None,
);
let this_module = None;
let _node_id = typecheck_next_file(
&mut tast,
this_module,
&mut sources,
file_name.to_string(),
code.clone(),
0,
node_id,
&mut None,
)
.unwrap();
Expand Down

0 comments on commit 3a77c86

Please sign in to comment.