Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codegen option for generic constraint evaluation format #104

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ members = [
"air-script",
"parser",
"ir",
"codegen/winterfell"
"codegen/winterfell",
"codegen/gce"
]
1 change: 1 addition & 0 deletions air-script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/main.rs"

[dependencies]
codegen-winter = { package = "air-codegen-winter", path = "../codegen/winterfell", version = "0.2.0" }
codegen-gce = { package = "air-codegen-gce", path = "../codegen/gce", version = "0.2.0" }
env_logger = "0.10.0"
ir = { package = "air-ir", path = "../ir", version = "0.2.0" }
log = { version = "0.4", default-features = false }
Expand Down
5 changes: 4 additions & 1 deletion air-script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ pub use parser::parse;
/// AirScript intermediate representation
pub use ir::AirIR;

/// JSON file generation in generic constraint evaluation format
pub use codegen_gce::CodeGenerator as GceCodeGenerator;

/// Code generation targeting Rust for the Winterfell prover
pub use codegen_winter::CodeGenerator;
pub use codegen_winter::CodeGenerator as WinterfellCodeGenerator;
30 changes: 30 additions & 0 deletions air-script/tests/aux_trace/aux_trace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"num_polys": 7,
"num_variables": 18,
"constants": [1],
"expressions": [
{"op": "SUB", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL", "index": 1}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL", "index": 3}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "VAR", "index": 16}},
{"op": "SUB", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "CONST", "index": 0}},
{"op": "MUL", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "POL", "index": 1}},
{"op": "MUL", "lhs": {"type": "EXPR", "index": 5}, "rhs": {"type": "POL", "index": 2}},
{"op": "ADD", "lhs": {"type": "POL", "index": 1}, "rhs": {"type": "EXPR", "index": 6}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 0}, "rhs": {"type": "EXPR", "index": 7}},
{"op": "ADD", "lhs": {"type": "POL", "index": 2}, "rhs": {"type": "POL_NEXT", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 1}, "rhs": {"type": "EXPR", "index": 9}},
{"op": "ADD", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "POL", "index": 1}},
{"op": "SUB", "lhs": {"type": "POL", "index": 2}, "rhs": {"type": "EXPR", "index": 11}},
{"op": "ADD", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "VAR", "index": 16}},
{"op": "ADD", "lhs": {"type": "EXPR", "index": 13}, "rhs": {"type": "POL", "index": 1}},
{"op": "ADD", "lhs": {"type": "EXPR", "index": 14}, "rhs": {"type": "VAR", "index": 17}},
{"op": "MUL", "lhs": {"type": "POL", "index": 3}, "rhs": {"type": "EXPR", "index": 15}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 3}, "rhs": {"type": "EXPR", "index": 16}},
{"op": "ADD", "lhs": {"type": "POL", "index": 2}, "rhs": {"type": "VAR", "index": 16}},
{"op": "MUL", "lhs": {"type": "POL_NEXT", "index": 4}, "rhs": {"type": "EXPR", "index": 18}},
{"op": "SUB", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "EXPR", "index": 19}},
{"op": "SUB", "lhs": {"type": "POL", "index": 3}, "rhs": {"type": "CONST", "index": 0}}
],
"outputs": [0, 1, 8, 10, 12, 2, 21, 3, 4, 17, 20]
}
15 changes: 15 additions & 0 deletions air-script/tests/binary/binary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"num_polys": 2,
"num_variables": 16,
"constants": [0, 2],
"expressions": [
{"op": "SUB", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "CONST", "index": 0}},
{"op": "MUL", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "POL", "index": 0}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 1}, "rhs": {"type": "POL", "index": 0}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 2}, "rhs": {"type": "CONST", "index": 0}},
{"op": "MUL", "lhs": {"type": "POL", "index": 1}, "rhs": {"type": "POL", "index": 1}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 4}, "rhs": {"type": "POL", "index": 1}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 5}, "rhs": {"type": "CONST", "index": 0}}
],
"outputs": [0, 3, 6]
}
39 changes: 39 additions & 0 deletions air-script/tests/constants/constants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"num_polys": 10,
"num_variables": 32,
"constants": [1, 0, 2],
"expressions": [
{"op": "SUB", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "CONST", "index": 0}},
{"op": "MUL", "lhs": {"type": "CONST", "index": 1}, "rhs": {"type": "CONST", "index": 2}},
{"op": "ADD", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "EXPR", "index": 1}},
{"op": "SUB", "lhs": {"type": "POL", "index": 1}, "rhs": {"type": "EXPR", "index": 2}},
{"op": "SUB", "lhs": {"type": "CONST", "index": 1}, "rhs": {"type": "CONST", "index": 1}},
{"op": "MUL", "lhs": {"type": "EXPR", "index": 4}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL", "index": 2}, "rhs": {"type": "EXPR", "index": 5}},
{"op": "ADD", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "CONST", "index": 1}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 7}, "rhs": {"type": "CONST", "index": 0}},
{"op": "ADD", "lhs": {"type": "EXPR", "index": 8}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 9}, "rhs": {"type": "CONST", "index": 2}},
{"op": "ADD", "lhs": {"type": "EXPR", "index": 10}, "rhs": {"type": "CONST", "index": 2}},
{"op": "SUB", "lhs": {"type": "EXPR", "index": 11}, "rhs": {"type": "CONST", "index": 1}},
{"op": "SUB", "lhs": {"type": "POL", "index": 3}, "rhs": {"type": "EXPR", "index": 12}},
{"op": "SUB", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "EXPR", "index": 2}},
{"op": "MUL", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "EXPR", "index": 15}},
{"op": "SUB", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "EXPR", "index": 16}},
{"op": "ADD", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 0}, "rhs": {"type": "EXPR", "index": 18}},
{"op": "MUL", "lhs": {"type": "CONST", "index": 1}, "rhs": {"type": "POL", "index": 1}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 1}, "rhs": {"type": "EXPR", "index": 20}},
{"op": "ADD", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "CONST", "index": 1}},
{"op": "MUL", "lhs": {"type": "EXPR", "index": 22}, "rhs": {"type": "POL", "index": 2}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 2}, "rhs": {"type": "EXPR", "index": 23}},
{"op": "ADD", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "CONST", "index": 0}},
{"op": "ADD", "lhs": {"type": "EXPR", "index": 25}, "rhs": {"type": "EXPR", "index": 1}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 4}, "rhs": {"type": "EXPR", "index": 26}},
{"op": "MUL", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "CONST", "index": 1}},
{"op": "ADD", "lhs": {"type": "CONST", "index": 0}, "rhs": {"type": "EXPR", "index": 28}},
{"op": "SUB", "lhs": {"type": "POL", "index": 4}, "rhs": {"type": "EXPR", "index": 29}}
],
"outputs": [0, 3, 6, 13, 19, 21, 24, 14, 17, 27, 30]
}
29 changes: 23 additions & 6 deletions air-script/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use codegen_winter::CodeGenerator;
use ir::AirIR;
use parser::parse;
use air_script::{parse, AirIR, GceCodeGenerator, WinterfellCodeGenerator};
use std::fs;

#[derive(Debug)]
pub enum TestError {
IO(String),
Parse(String),
IR(String),
Codegen(String),
}

pub struct Test {
Expand All @@ -19,12 +18,13 @@ impl Test {
Test { input_path }
}

pub fn transpile(&self) -> Result<String, TestError> {
/// Parse data in file at `input_path` and return [AirIR] with this data
fn generate_ir(&self) -> Result<AirIR, TestError> {
// load source input from file
let source = fs::read_to_string(&self.input_path).map_err(|err| {
TestError::IO(format!(
"Failed to open input file `{:?}` - {}",
self.input_path, err
&self.input_path, err
))
})?;

Expand All @@ -43,8 +43,25 @@ impl Test {
))
})?;

Ok(ir)
}

/// Generate Rust code containing a Winterfell Air implementation for the AirIR
pub fn generate_winterfell(&self) -> Result<String, TestError> {
let ir = Self::generate_ir(self)?;
// generate Rust code targeting Winterfell
let codegen = CodeGenerator::new(&ir);
let codegen = WinterfellCodeGenerator::new(&ir);
Ok(codegen.generate())
}

/// Generate JSON file in generic constraint evaluation format
pub fn generate_gce(&self, extension_degree: u8, path: &str) -> Result<(), TestError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doc comment to this function and the generate_winterfell function?

let ir = Self::generate_ir(self)?;
let codegen = GceCodeGenerator::new(&ir, extension_degree).map_err(|err| {
TestError::Codegen(format!("Failed to create GCECodeGenerator: {:?}", err))
})?;
codegen
.generate(path)
.map_err(|err| TestError::Codegen(format!("Failed to generate JSON file: {:?}", err)))
}
}
13 changes: 13 additions & 0 deletions air-script/tests/indexed_trace_access/indexed_trace_access.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"num_polys": 6,
"num_variables": 16,
"constants": [1, 0],
"expressions": [
{"op": "ADD", "lhs": {"type": "POL", "index": 1}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 0}, "rhs": {"type": "EXPR", "index": 0}},
{"op": "ADD", "lhs": {"type": "POL", "index": 3}, "rhs": {"type": "CONST", "index": 0}},
{"op": "SUB", "lhs": {"type": "POL_NEXT", "index": 2}, "rhs": {"type": "EXPR", "index": 2}},
{"op": "SUB", "lhs": {"type": "POL", "index": 0}, "rhs": {"type": "CONST", "index": 1}}
],
"outputs": [4, 1, 3]
}
Loading