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

WIP: Modularize backends #30

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.vscode
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ serde_with = "2.0.1" # for serializing arkworks
serde_json = "1.0.85" # to (de)serialize JSON
serde = "1.0.144" # to (de)serialize objects
thiserror = "1.0.31" # helpful error traits
toml = "0.8.8" # to parse manifest files
toml = "0.8.8" # to parse manifest files
62 changes: 31 additions & 31 deletions src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,24 @@ use std::hash::Hash;

use itertools::Itertools;

use crate::backends::Backend;
use crate::circuit_writer::writer::AnnotatedCell;
use crate::circuit_writer::{CircuitWriter, DebugInfo};
use crate::circuit_writer::{CircuitWriter, DebugInfo, Gate};
use crate::compiler::Sources;
use crate::{
circuit_writer::{Gate, Wiring},
constants::{Field, Span},
helpers::PrettyField as _,
};
use crate::constants::{Field, Span};
use crate::helpers::PrettyField;

impl CircuitWriter {
Copy link
Contributor

Choose a reason for hiding this comment

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

so, now I'm wondering if the ASM functions should be implemented directly on each specific Backend, or each specific Backend::CompileCircuit type. I don't think it makes sense to have a generic implementation here

impl<B: Backend> CircuitWriter<B> {
pub fn generate_asm(&self, sources: &Sources, debug: bool) -> String {
let mut res = "".to_string();

// version
res.push_str("@ noname.0.7.0\n\n");

// vars
let mut vars = OrderedHashSet::default();
let mut vars: OrderedHashSet<B::Field> = OrderedHashSet::default();

let gates = self.compiled_gates();

for Gate { coeffs, .. } in gates {
for Gate { coeffs, .. } in self.backend.gates() {
extract_vars_from_coeffs(&mut vars, coeffs);
}

Expand All @@ -65,9 +61,14 @@ impl CircuitWriter {
title(&mut res, "GATES");
}

for (row, (Gate { typ, coeffs }, debug_info)) in
gates.iter().zip(&self.debug_info).enumerate()
for (row, (Gate { typ, coeffs }, debug_info)) in self
.backend
.gates()
.iter()
.zip(self.backend.debug_info())
.enumerate()
{
println!("gate {:?}", row);
// gate #
if debug {
writeln!(res, "╭{s}", s = "─".repeat(80)).unwrap();
Expand All @@ -83,7 +84,7 @@ impl CircuitWriter {
if !coeffs.is_empty() {
res.push('<');
res.push_str(&coeffs.join(","));
res.push_str(">");
res.push('>');
}
}

Expand All @@ -107,25 +108,16 @@ impl CircuitWriter {
title(&mut res, "WIRING");
}

let mut cycles: Vec<_> = self
.wiring
.values()
.map(|w| match w {
Wiring::NotWired(_) => None,
Wiring::Wired(annotated_cells) => Some(annotated_cells),
})
.filter(Option::is_some)
.flatten()
.collect();
let mut cycles: Vec<_> = self.backend.wiring_cycles();

// we must have a deterministic sort for the cycles,
// otherwise the same circuit might have different representations
cycles.sort();

for annotated_cells in cycles {
let (cells, debug_infos): (Vec<_>, Vec<_>) = annotated_cells
.into_iter()
.map(|AnnotatedCell { cell, debug }| (cell.clone(), debug.clone()))
.iter()
.map(|AnnotatedCell { cell, debug }| (*cell, debug.clone()))
.unzip();

if debug {
Expand All @@ -143,8 +135,13 @@ impl CircuitWriter {
res
}

fn display_source(&self, res: &mut String, sources: &Sources, debug_infos: &[DebugInfo]) {
for DebugInfo { span, note } in debug_infos {
fn display_source(
&self,
res: &mut String,
sources: &crate::compiler::Sources,
debug_infos: &[DebugInfo],
) {
for DebugInfo { span, note: _ } in debug_infos {
// find filename and source
let (file, source) = sources.get(&span.filename_id).expect("source not found");

Expand Down Expand Up @@ -177,7 +174,10 @@ impl CircuitWriter {
}
}

fn extract_vars_from_coeffs(vars: &mut OrderedHashSet<Field>, coeffs: &[Field]) {
pub fn extract_vars_from_coeffs<F: Field + PrettyField>(
vars: &mut OrderedHashSet<F>,
coeffs: &[F],
) {
for coeff in coeffs {
let s = coeff.pretty();
if s.len() >= 5 {
Expand All @@ -186,7 +186,7 @@ fn extract_vars_from_coeffs(vars: &mut OrderedHashSet<Field>, coeffs: &[Field])
}
}

fn parse_coeffs(vars: &OrderedHashSet<Field>, coeffs: &[Field]) -> Vec<String> {
fn parse_coeffs<F: Field + PrettyField>(vars: &OrderedHashSet<F>, coeffs: &[F]) -> Vec<String> {
let mut coeffs: Vec<_> = coeffs
.iter()
.map(|x| {
Expand Down Expand Up @@ -287,7 +287,7 @@ uvwx
yz
";
assert_eq!(
find_exact_line(&SRC, Span::new(0, 5, 6)),
find_exact_line(SRC, Span::new(0, 5, 6)),
(2, 5, "efgh\nijkl")
);
}
Expand Down
Loading
Loading