Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ members = [
"program_analysis",
"program_structure",
"program_structure_tests",
"wasm",
]
2 changes: 1 addition & 1 deletion parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod errors;
mod include_logic;
mod parser_logic;
mod syntax_sugar_traits;
mod syntax_sugar_remover;
pub mod syntax_sugar_remover;

pub use parser_logic::parse_definition;

Expand Down
2 changes: 1 addition & 1 deletion parser/src/syntax_sugar_remover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::syntax_sugar_traits::ContainsExpression;

/// This functions desugars all anonymous components and tuples.
#[must_use]
pub(crate) fn remove_syntactic_sugar(
pub fn remove_syntactic_sugar(
templates: &HashMap<String, TemplateData>,
functions: &HashMap<String, FunctionData>,
file_library: &FileLibrary,
Expand Down
28 changes: 27 additions & 1 deletion program_analysis/src/analysis_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use std::collections::HashMap;

use parser::ParseResult;
use parser::syntax_sugar_remover;

use program_structure::{
writers::{LogWriter, ReportWriter},
Expand All @@ -14,7 +15,6 @@ use program_structure::{
report::{ReportCollection, Report},
};

#[cfg(test)]
use program_structure::template_library::TemplateLibrary;

use crate::{
Expand Down Expand Up @@ -76,6 +76,32 @@ impl AnalysisRunner {
(self, reports)
}

/// Convenience method used for WASM build target.
pub fn with_src_desugar(mut self, file_contents: &[&str]) -> (Self, ReportCollection) {
use parser::parse_definition;

let mut library_contents = HashMap::new();
let mut file_library = FileLibrary::default();
for (file_index, file_source) in file_contents.iter().enumerate() {
let file_name = format!("file-{file_index}.circom");
let file_id = file_library.add_file(file_name, file_source.to_string(), true);
library_contents.insert(file_id, vec![parse_definition(file_source).unwrap()]);
}
let template_library = TemplateLibrary::new(library_contents, file_library.clone());
let mut sugar_reports = ReportCollection::new();
let (new_templates, new_functions) = syntax_sugar_remover::remove_syntactic_sugar(
&template_library.templates,
&template_library.functions,
&template_library.file_library,
&mut sugar_reports,
);
self.template_asts = new_templates;
self.function_asts = new_functions;
self.file_library = template_library.file_library;

(self, sugar_reports)
}

/// Convenience method used to generate a runner for testing purposes.
#[cfg(test)]
pub fn with_src(mut self, file_contents: &[&str]) -> Self {
Expand Down
1 change: 1 addition & 0 deletions program_structure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde-sarif = "0.4"
serde_json = "1.0"
thiserror = "1.0"
termcolor = "1.1.3"
web-time = "1.1.0"

[dev-dependencies]
proptest = "1.1"
Expand Down
2 changes: 1 addition & 1 deletion program_structure/src/control_flow_graph/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use log::debug;
use std::collections::HashSet;
use std::fmt;
use std::time::{Instant, Duration};
use web_time::{Instant, Duration};

use crate::constants::UsefulConstants;
use crate::file_definition::FileID;
Expand Down
27 changes: 25 additions & 2 deletions program_structure/src/program_library/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt::Display;
use std::str::FromStr;

use codespan_reporting::diagnostic::{Diagnostic, Label};
use serde::{Serialize, Serializer};

use super::report_code::ReportCode;
use super::file_definition::{FileID, FileLocation};
Expand All @@ -13,7 +14,7 @@ pub type DiagnosticCode = String;
pub type ReportLabel = Label<FileID>;
type ReportNote = String;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
pub enum MessageCategory {
Error,
Warning,
Expand Down Expand Up @@ -82,17 +83,39 @@ impl MessageCategory {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize)]
pub struct Report {
category: MessageCategory,
message: String,
primary_file_ids: Vec<FileID>,
#[serde(with = "label_serde")]
primary: Vec<ReportLabel>,
#[serde(with = "label_serde")]
secondary: Vec<ReportLabel>,
notes: Vec<ReportNote>,
code: ReportCode,
}

pub mod label_serde {
use super::*;

// Serialize the `ReportLabel` (which is `Label<FileID>`)
pub fn serialize<S>(label: &Vec<ReportLabel>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if label.is_empty() {
return serializer.serialize_str("");
}

let start = label[0].range.start as i32;
let end = label[0].range.end as i32;

serializer.serialize_str(format!("{start},{end}").as_str())
}

}

impl Report {
fn new(category: MessageCategory, message: String, code: ReportCode) -> Report {
Report {
Expand Down
4 changes: 3 additions & 1 deletion program_structure/src/program_library/report_code.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::Serialize;

const DOC_URL: &str = "https://github.com/trailofbits/circomspect/blob/main/doc/analysis_passes.md";

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Serialize)]
pub enum ReportCode {
AssertWrongType,
ParseFail,
Expand Down
32 changes: 32 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "circomspect-wasm"
version = "0.9.0"
edition = "2021"
rust-version = "1.65"
license = "LGPL-3.0-only"
authors = ["Trail of Bits", "numtel <ben@latenightsketches.com>"]
readme = "../README.md"
description = "A static analyzer and linter for the Circom zero-knowledge DSL"
keywords = ["cryptography", "static-analysis", "zero-knowledge", "circom"]
repository = "https://github.com/trailofbits/circomspect"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0"
atty = "0.2"
clap = { version = "4.5", features = ["derive"] }
log = "0.4"
parser = { package = "circomspect-parser", version = "2.1.3", path = "../parser" }
pretty_env_logger = "0.5"
program_analysis = { package = "circomspect-program-analysis", version = "0.8.1", path = "../program_analysis" }
program_structure = { package = "circomspect-program-structure", version = "2.1.3", path = "../program_structure" }
serde_json = "1.0"
termcolor = "1.1"
wasm-bindgen = "0.2"
console_error_panic_hook = "0.1.7"
js-sys = "0.3"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Loading