Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
katat committed Apr 17, 2024
1 parent 1a99e83 commit 06290e5
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 67 deletions.
7 changes: 2 additions & 5 deletions src/backends/kimchi/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ pub fn display_source(
res.push('\n');
}

pub fn extract_vars_from_coeffs(
vars: &mut OrderedHashSet<VestaField>,
coeffs: &[VestaField],
) {
pub fn extract_vars_from_coeffs(vars: &mut OrderedHashSet<VestaField>, coeffs: &[VestaField]) {
for coeff in coeffs {
let s = coeff.pretty();
if s.len() >= 5 {
Expand Down Expand Up @@ -185,4 +182,4 @@ yz
(2, 5, "efgh\nijkl")
);
}
}
}
9 changes: 6 additions & 3 deletions src/backends/kimchi/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use kimchi::circuits::polynomials::poseidon::{POS_ROWS_PER_HASH, ROUNDS_PER_ROW}
use kimchi::mina_poseidon::constants::{PlonkSpongeConstantsKimchi, SpongeConstants};
use kimchi::mina_poseidon::permutation::full_round;

use super::{KimchiVesta, VestaField};
use crate::backends::kimchi::NUM_REGISTERS;
use crate::backends::Backend;
use super::{VestaField, KimchiVesta};

use crate::{
circuit_writer::{CircuitWriter, GateKind, VarInfo},
Expand Down Expand Up @@ -55,8 +55,11 @@ pub fn poseidon(
for const_or_cell in &input.cvars {
match const_or_cell {
ConstOrCell::Const(cst) => {
let cell =
compiler.backend.add_constant(Some("encoding constant input to poseidon"), *cst, span);
let cell = compiler.backend.add_constant(
Some("encoding constant input to poseidon"),
*cst,
span,
);
cells.push(cell);
}
ConstOrCell::Cell(cell) => cells.push(*cell),
Expand Down
2 changes: 1 addition & 1 deletion src/backends/kimchi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::{
writer::{AnnotatedCell, Cell, PendingGate},
DebugInfo, Gate, GateKind, Wiring,
},
constants::Span,
compiler::Sources,
constants::Span,
error::{Error, ErrorKind, Result},
helpers::PrettyField,
var::{CellVar, Value, Var},
Expand Down
2 changes: 1 addition & 1 deletion src/backends/kimchi/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,4 @@ impl VerifierIndex {
.into_diagnostic()
.wrap_err("kimchi: failed to verify the proof")
}
}
}
14 changes: 11 additions & 3 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use std::collections::HashMap;
use ark_ff::{Field, Zero};

use crate::{
circuit_writer::{DebugInfo, GateKind}, compiler::Sources, constants::Span, error::{Error, ErrorKind, Result}, helpers::PrettyField, imports::FnHandle, var::{CellVar, Value, Var}, witness::WitnessEnv
circuit_writer::{DebugInfo, GateKind},
compiler::Sources,
constants::Span,
error::{Error, ErrorKind, Result},
helpers::PrettyField,
imports::FnHandle,
var::{CellVar, Value, Var},
witness::WitnessEnv,
};

pub mod kimchi;
Expand Down Expand Up @@ -99,8 +106,9 @@ pub trait Backend: Clone {
Value::PublicOutput(var) => {
// var can be none. what could be the better way to pass in the span in that case?
// let span = self.main_info().span;
let var =
var.ok_or_else(|| Error::new("runtime", ErrorKind::MissingReturn, Span::default()))?;
let var = var.ok_or_else(|| {
Error::new("runtime", ErrorKind::MissingReturn, Span::default())
})?;
self.compute_var(env, var)
}
Value::Scale(scalar, var) => {
Expand Down
12 changes: 8 additions & 4 deletions src/circuit_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ where
/// Indexes used by the private inputs
/// (this is useful to check that they appear in the circuit)
pub(crate) private_input_indices: Vec<usize>,

}

/// Debug information related to a single row in a circuit.
Expand Down Expand Up @@ -91,7 +90,12 @@ impl<B: Backend> CircuitWriter<B> {
self.typed.size_of(typ)
}

pub fn add_local_var(&self, fn_env: &mut FnEnv<B::Field>, var_name: String, var_info: VarInfo<B::Field>) {
pub fn add_local_var(
&self,
fn_env: &mut FnEnv<B::Field>,
var_name: String,
var_info: VarInfo<B::Field>,
) {
// check for consts first
let qualified = FullyQualified::local(var_name.clone());
if let Some(_cst_info) = self.typed.const_info(&qualified) {
Expand Down Expand Up @@ -149,7 +153,8 @@ impl<B: Backend> CircuitWriter<B> {
double_generic_gate_optimization: bool,
) -> Result<CompiledCircuit<B>> {
// create circuit writer
let mut circuit_writer = CircuitWriter::new(typed, backend, double_generic_gate_optimization);
let mut circuit_writer =
CircuitWriter::new(typed, backend, double_generic_gate_optimization);

// get main function
let qualified = FullyQualified::local("main".to_string());
Expand Down Expand Up @@ -234,7 +239,6 @@ impl<B: Backend> CircuitWriter<B> {
main_span,
)?;


//
Ok(CompiledCircuit::new(circuit_writer))
}
Expand Down
25 changes: 13 additions & 12 deletions src/circuit_writer/writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{
fmt::{self, Display, Formatter},
};
use std::fmt::{self, Display, Formatter};

use ark_ff::{One, Zero};
use kimchi::circuits::wires::Wire;
Expand Down Expand Up @@ -48,8 +46,7 @@ impl From<GateKind> for kimchi::circuits::gate::GateType {

// TODO: this could also contain the span that defined the gate!
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Gate
{
pub struct Gate {
/// Type of gate
pub typ: GateKind,

Expand Down Expand Up @@ -285,8 +282,7 @@ impl<B: Backend> CircuitWriter<B> {
}
}

self
.public_output
self.public_output
.as_ref()
.expect("bug in the compiler: missing public output");

Expand Down Expand Up @@ -702,7 +698,9 @@ impl<B: Backend> CircuitWriter<B> {

for idx in 0..num {
// create the var
let cvar = self.backend.new_internal_var(Value::External(name.clone(), idx), span);
let cvar = self
.backend
.new_internal_var(Value::External(name.clone(), idx), span);
cvars.push(ConstOrCell::Cell(cvar));

// create the associated generic gate
Expand All @@ -726,7 +724,9 @@ impl<B: Backend> CircuitWriter<B> {
let mut cvars = Vec::with_capacity(num);
for _ in 0..num {
// create the var
let cvar = self.backend.new_internal_var(Value::PublicOutput(None), span);
let cvar = self
.backend
.new_internal_var(Value::PublicOutput(None), span);
cvars.push(ConstOrCell::Cell(cvar));

// create the associated generic gate
Expand All @@ -749,7 +749,9 @@ impl<B: Backend> CircuitWriter<B> {

for idx in 0..num {
// create the var
let cvar = self.backend.new_internal_var(Value::External(name.clone(), idx), span);
let cvar = self
.backend
.new_internal_var(Value::External(name.clone(), idx), span);
cvars.push(ConstOrCell::Cell(cvar));
self.private_input_indices.push(cvar.index);
}
Expand All @@ -759,8 +761,7 @@ impl<B: Backend> CircuitWriter<B> {
}

#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub(crate) struct PendingGate
{
pub(crate) struct PendingGate {
pub label: &'static str,
#[serde(skip)]
pub coeffs: Vec<VestaField>,
Expand Down
15 changes: 13 additions & 2 deletions src/cli/cmd_build_and_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use camino::Utf8PathBuf as PathBuf;
use miette::{Context, IntoDiagnostic};

use crate::{
backends::{kimchi::{prover::{ProverIndex, VerifierIndex}, KimchiVesta}, Backend},
backends::{
kimchi::{
prover::{ProverIndex, VerifierIndex},
KimchiVesta,
},
Backend,
},
cli::packages::path_to_package,
compiler::{compile, typecheck_next_file, Sources},
inputs::{parse_inputs, JsonInputs},
Expand Down Expand Up @@ -195,7 +201,12 @@ pub fn build(
let double_generic_gate_optimization = false;

let kimchi_vesta = KimchiVesta::new(double_generic_gate_optimization);
let compiled_circuit = compile(&sources, tast, kimchi_vesta, double_generic_gate_optimization)?;
let compiled_circuit = compile(
&sources,
tast,
kimchi_vesta,
double_generic_gate_optimization,
)?;

if asm {
println!("{}", compiled_circuit.asm(&sources, debug));
Expand Down
16 changes: 5 additions & 11 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ use std::collections::HashMap;
use miette::NamedSource;

use crate::{
backends::Backend,
circuit_writer::CircuitWriter,
cli::packages::UserRepo,
error::Result,
inputs::JsonInputs,
lexer::Token,
name_resolution::NAST,
parser::AST,
type_checker::TypeChecker,
witness::CompiledCircuit,
backends::Backend, circuit_writer::CircuitWriter, cli::packages::UserRepo, error::Result,
inputs::JsonInputs, lexer::Token, name_resolution::NAST, parser::AST,
type_checker::TypeChecker, witness::CompiledCircuit,
};

/// Contains the association between a counter and the corresponding filename and source code.
Expand Down Expand Up @@ -149,7 +142,8 @@ pub fn compile<B: Backend>(
backend: B,
double_generic_gate_optimization: bool,
) -> miette::Result<CompiledCircuit<B>> {
CircuitWriter::generate_circuit(tast, backend, double_generic_gate_optimization).into_miette(sources)
CircuitWriter::generate_circuit(tast, backend, double_generic_gate_optimization)
.into_miette(sources)
}

pub fn generate_witness<B: Backend>(
Expand Down
4 changes: 3 additions & 1 deletion src/constraints/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub fn and<B: Backend>(
// two vars
(ConstOrCell::Cell(lhs), ConstOrCell::Cell(rhs)) => {
// create a new variable to store the result
let res = compiler.backend.new_internal_var(Value::Mul(*lhs, *rhs), span);
let res = compiler
.backend
.new_internal_var(Value::Mul(*lhs, *rhs), span);

// create a gate to constrain the result
let zero = B::Field::zero();
Expand Down
17 changes: 12 additions & 5 deletions src/constraints/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ pub fn add<B: Backend>(
}

// create a new variable to store the result
let res =
compiler.backend.new_internal_var(Value::LinearCombination(vec![(one, *cvar)], *cst), span);
let res = compiler
.backend
.new_internal_var(Value::LinearCombination(vec![(one, *cvar)], *cst), span);

// create a gate to store the result
// TODO: we should use an add_generic function that takes advantage of the double generic gate
Expand Down Expand Up @@ -183,7 +184,9 @@ pub fn mul<B: Backend>(
}

// create a new variable to store the result
let res = compiler.backend.new_internal_var(Value::Scale(*cst, *cvar), span);
let res = compiler
.backend
.new_internal_var(Value::Scale(*cst, *cvar), span);

// create a gate to store the result
// TODO: we should use an add_generic function that takes advantage of the double generic gate
Expand All @@ -200,7 +203,9 @@ pub fn mul<B: Backend>(
// everything is a var
(ConstOrCell::Cell(lhs), ConstOrCell::Cell(rhs)) => {
// create a new variable to store the result
let res = compiler.backend.new_internal_var(Value::Mul(*lhs, *rhs), span);
let res = compiler
.backend
.new_internal_var(Value::Mul(*lhs, *rhs), span);

// create a gate to store the result
compiler.backend.add_generic_gate(
Expand Down Expand Up @@ -359,7 +364,9 @@ fn equal_cells<B: Backend>(
);

// 4. diff_inv * diff = one_minus_res
let diff_inv = compiler.backend.new_internal_var(Value::Inverse(diff), span);
let diff_inv = compiler
.backend
.new_internal_var(Value::Inverse(diff), span);

compiler.backend.add_generic_gate(
"constraint #4 for the equals gadget (diff_inv * diff = one_minus_res)",
Expand Down
11 changes: 9 additions & 2 deletions src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use num_bigint::BigUint;
use thiserror::Error;

use crate::{
backends::{kimchi::VestaField, Backend}, parser::types::TyKind, type_checker::FullyQualified, witness::CompiledCircuit
backends::{kimchi::VestaField, Backend},
parser::types::TyKind,
type_checker::FullyQualified,
witness::CompiledCircuit,
};

//
Expand Down Expand Up @@ -64,7 +67,11 @@ impl<B: Backend> CompiledCircuit<B> {
Ok(vec![cell_value])
}
(TyKind::Bool, Value::Bool(bb)) => {
let ff = if bb { B::Field::one() } else { B::Field::zero() };
let ff = if bb {
B::Field::one()
} else {
B::Field::zero()
};
Ok(vec![ff])
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//! Refer to the [book](https://mimoo.github.io/noname/) for more information.
//!
pub mod circuit_writer;
pub mod backends;
pub mod circuit_writer;
pub mod cli;
pub mod compiler;
pub mod constants;
Expand Down Expand Up @@ -40,7 +40,6 @@ pub mod helpers {

use crate::backends::kimchi::VestaField;


/// A trait to display [Field] in pretty ways.
pub trait PrettyField: ark_ff::PrimeField {
/// Print a field in a negative form if it's past the half point.
Expand Down
10 changes: 8 additions & 2 deletions src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,10 @@ impl Stmt {
#[derive(Debug)]

/// Things you can have in a scope (including the root scope).
pub struct Root<F> where F: Field{
pub struct Root<F>
where
F: Field,
{
pub kind: RootKind<F>,
pub span: Span,
}
Expand Down Expand Up @@ -1185,7 +1188,10 @@ pub enum RootKind<F: Field> {
//

#[derive(Debug)]
pub struct ConstDef<F> where F: Field {
pub struct ConstDef<F>
where
F: Field,
{
pub module: ModulePath, // name resolution
pub name: Ident,
pub value: F,
Expand Down
4 changes: 1 addition & 3 deletions src/stdlib/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::backends::Backend;
use crate::constants::Span;
use crate::imports::FnKind;
use crate::lexer::Token;
use crate::parser::types::FnSig;
use crate::parser::ParserCtx;
use crate::type_checker::FnInfo;
use crate::{
constants::Span,
};

const POSEIDON_FN: &str = "poseidon(input: [Field; 2]) -> [Field; 3]";

Expand Down
Loading

0 comments on commit 06290e5

Please sign in to comment.