Skip to content

Commit

Permalink
Merge branch 'master' into use-prover-config-pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef authored Nov 9, 2023
2 parents 9b43d0f + 8bd7937 commit b1d8c43
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 173 deletions.
57 changes: 32 additions & 25 deletions src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,11 @@ impl Value {
}
}

pub(crate) fn to_repr(&self) -> impl Iterator<Item = u64> {
let us = match &self {
Value::Native(f) => f.0 .0.to_vec(),
Value::ExoNative(fs) => fs.iter().flat_map(|f| f.0 .0.iter()).cloned().collect(),
Value::BigInt(_) => todo!(),
};
us.into_iter()
}

pub(crate) fn to_bytes(&self) -> Vec<u8> {
match &self {
Value::Native(f) => f.0 .0.iter().flat_map(|u| u.to_be_bytes()).collect(),
Value::ExoNative(fs) => fs
.iter()
.flat_map(|f| f.0 .0.iter().flat_map(|u| u.to_be_bytes()))
.collect(),
Value::BigInt(bi) => bi.to_bytes_be().1,
Value::Native(f) => f.into_bigint().to_bytes_be(),
Value::ExoNative(_) => todo!(),
}
}

Expand Down Expand Up @@ -376,7 +364,7 @@ impl Value {
// }
impl std::default::Default for Value {
fn default() -> Value {
Value::BigInt(BigInt::zero())
Value::zero()
}
}
impl From<BigInt> for Value {
Expand Down Expand Up @@ -443,7 +431,7 @@ impl From<Value> for BigInt {
impl Pretty for Value {
fn pretty(&self) -> String {
match self {
Value::BigInt(i) => format!("ε{}", i),
Value::BigInt(i) => format!("{}", i),
Value::Native(f) => f.pretty(),
Value::ExoNative(fs) => fs.iter().map(|f| f.pretty()).join("/"),
}
Expand All @@ -453,7 +441,7 @@ impl Pretty for Value {
match self {
Value::BigInt(i) => {
format!(
"ε{}",
"{}",
match base {
Base::Dec => i.to_str_radix(10),
Base::Hex => format!("0x{}", i.to_str_radix(16)),
Expand Down Expand Up @@ -510,7 +498,7 @@ impl std::cmp::PartialEq for Value {
impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Value::BigInt(i) => write!(f, "ε{}", i),
Value::BigInt(i) => write!(f, "{}", i),
Value::Native(fr) => write!(f, "{}", fr.pretty()),
Value::ExoNative(fs) => {
write!(f, "{:?}", fs.iter().map(|f| f.pretty()).collect::<Vec<_>>())
Expand Down Expand Up @@ -649,16 +637,16 @@ impl ValueBacking {
}
}
.cloned(),
ValueBacking::Expression { e, spilling } => e.eval(
i + spilling,
ValueBacking::Expression { e, .. } => e.eval(
i,
|handle, j, _| {
cs.get(handle, j, false)
.or_else(|| cs.column(handle).unwrap().padding_value.as_ref().cloned())
},
&mut None,
&EvalSettings { wrap: false },
),
ValueBacking::Function { f, spilling } => f(i + spilling, cs),
ValueBacking::Function { f, .. } => f(i, cs),
}
}

Expand Down Expand Up @@ -689,10 +677,11 @@ impl ValueBacking {
}
}

pub fn iter<'a>(&'a self, columns: &'a ColumnSet) -> ValueBackingIter<'a> {
pub fn iter<'a>(&'a self, columns: &'a ColumnSet, len: isize) -> ValueBackingIter<'a> {
ValueBackingIter {
value: self,
i: 0,
len,
columns,
}
}
Expand Down Expand Up @@ -724,6 +713,7 @@ impl ValueBacking {
pub struct ValueBackingIter<'a> {
value: &'a ValueBacking,
columns: &'a ColumnSet,
len: isize,
i: isize,
}

Expand All @@ -741,9 +731,17 @@ impl<'a> Iterator for ValueBackingIter<'a> {
v.get(self.i as usize).cloned()
}
}
ValueBacking::Expression { .. } => {
self.i += 1;
self.value.get_raw(self.i - 1, false, self.columns)
ValueBacking::Expression { spilling, .. } => {
if self.i >= self.len {
None
} else {
self.i += 1;
Some(
self.value
.get(self.i - 1, false, self.columns)
.unwrap_or_default(),
)
}
}
ValueBacking::Function { f, .. } => {
self.i += 1;
Expand Down Expand Up @@ -964,6 +962,15 @@ impl ColumnSet {
}
}

pub(crate) fn mark_used(&mut self, h: &ColumnRef) -> Result<()> {
if let Some(ref mut column) = self.get_col_mut(h) {
column.used = true;
Ok(())
} else {
bail!("{} can not be found", h.pretty())
}
}

pub fn get_col_mut(&mut self, h: &ColumnRef) -> Option<&mut Column> {
if h.is_id() {
self._cols.get_mut(h.as_id())
Expand Down
49 changes: 44 additions & 5 deletions src/compiler/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::sync::atomic::AtomicUsize;

use super::node::ColumnRef;
use super::tables::{ComputationTable, Scope};
use super::{common::*, CompileSettings, Expression, Magma, Node, Type};
use super::{common::*, CompileSettings, Conditioning, Expression, Magma, Node, Type};
use crate::column::{Column, ColumnSet, Computation, RegisterID, Value, ValueBacking};
use crate::compiler::parser::*;
use crate::dag::ComputationDag;
Expand Down Expand Up @@ -55,7 +55,6 @@ pub enum Constraint {
handle: Handle,
from: Vec<ColumnRef>,
to: Vec<ColumnRef>,
signs: Vec<bool>,
},
InRange {
handle: Handle,
Expand Down Expand Up @@ -641,6 +640,41 @@ impl ConstraintSet {
&self.columns.column(h).unwrap().handle
}

pub(crate) fn insert_constraint(&mut self, c: Constraint) {
match &c {
Constraint::Vanishes { expr, .. } => {
for c in expr.dependencies().into_iter() {
self.columns.get_col_mut(&c).unwrap().used = true
}
}
Constraint::Lookup {
including,
included,
..
} => {
for c in including
.iter()
.flat_map(Node::dependencies)
.chain(included.iter().flat_map(Node::dependencies))
{
self.columns.mark_used(&c).unwrap();
}
}
Constraint::Permutation { from, to, .. } => {
for c in from.iter().chain(to.iter()) {
self.columns.mark_used(&c).unwrap();
}
}
Constraint::InRange { exp, .. } => {
for c in exp.dependencies().into_iter() {
self.columns.mark_used(&c).unwrap();
}
}
Constraint::Normalization { .. } => {}
}
self.constraints.push(c);
}

pub(crate) fn insert_perspective(
&mut self,
module: &str,
Expand Down Expand Up @@ -839,6 +873,7 @@ impl ConstraintSet {
let empty_backing: ValueBacking = ValueBacking::default();
while let Some((r, column)) = current_col.next() {
let handle = &column.handle;
let module_size = self.effective_len_for(&handle.module).unwrap();
trace!("Writing {}", handle);
let backing = self.columns.backing(&r).unwrap_or_else(|| &empty_backing);
let padding: Value = if let Some(v) = column.padding_value.as_ref() {
Expand Down Expand Up @@ -870,7 +905,7 @@ impl ConstraintSet {
out.write_all(format!("\"{}\":{{\n", handle).as_bytes())?;
out.write_all("\"values\":[".as_bytes())?;

let mut value = backing.iter(&self.columns).peekable();
let mut value = backing.iter(&self.columns, module_size).peekable();
while let Some(x) = value.next() {
out.write_all(
cache
Expand Down Expand Up @@ -1565,7 +1600,12 @@ fn reduce_toplevel(
let body = if let Some(guard) = guard {
let guard_expr = reduce(guard, &mut ctx, settings)?
.with_context(|| anyhow!("guard `{:?}` is empty", guard))?;
Intrinsic::Mul.call(&[guard_expr, body])?
match guard_expr.t().c() {
Conditioning::Loobean => {
bail!("unexpected loobean guard in {}", handle.pretty())
}
_ => Intrinsic::IfNotZero.call(&[guard_expr, body])?,
}
} else {
body
};
Expand Down Expand Up @@ -1739,7 +1779,6 @@ fn reduce_toplevel(
),
from: froms,
to: tos,
signs: signs.clone(),
}))
}
Token::DefInterleaving { .. } => {
Expand Down
16 changes: 14 additions & 2 deletions src/compiler/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,20 @@ impl Display for Node {

match self.e() {
Expression::Const(x) => write!(f, "{}", x),
Expression::Column { handle, .. } | Expression::ExoColumn { handle, .. } => {
write!(f, "{}", handle.to_string_short())
Expression::Column { handle, shift, .. }
| Expression::ExoColumn { handle, shift, .. } => {
write!(
f,
"{}{}",
handle.to_string_short(),
if *shift > 0 {
format!("₊{}", crate::pretty::subscript(&shift.to_string()))
} else if *shift < 0 {
crate::pretty::subscript(&shift.to_string())
} else {
Default::default()
}
)
}
Expression::ArrayColumn { handle, domain, .. } => {
write!(f, "{}{}", handle.to_string_short(), domain)
Expand Down
12 changes: 5 additions & 7 deletions src/compiler/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use anyhow::*;
use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::{cmp::Ordering, sync::OnceLock};

use crate::{column::Value, errors::RuntimeError};

Expand Down Expand Up @@ -281,10 +281,8 @@ impl TryFrom<&str> for Conditioning {
}
}

lazy_static::lazy_static! {
static ref F_15: Value = Value::from(15);
static ref F_255: Value = Value::from(255);
}
static F_15: OnceLock<Value> = OnceLock::new();
static F_255: OnceLock<Value> = OnceLock::new();

// TODO: implement PartialOrd
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -331,14 +329,14 @@ impl RawMagma {
}
}
RawMagma::Nibble => {
if x.le(&F_15) {
if x.le(F_15.get_or_init(|| Value::from(15))) {
Ok(x)
} else {
bail!(RuntimeError::InvalidValue("nibble", x))
}
}
RawMagma::Byte => {
if x.le(&F_255) {
if x.le(F_255.get_or_init(|| Value::from(255))) {
Ok(x)
} else {
bail!(RuntimeError::InvalidValue("byte", x))
Expand Down
23 changes: 20 additions & 3 deletions src/exporters/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,19 @@ fn render_constraints(
match c {
Constraint::Vanishes {
handle,
domain: _,
domain,
expr,
} => {
let mut tty = Tty::new().with_guides();
println!("\n{} :=", handle.pretty());
println!(
"\n{}{} :=",
handle.pretty(),
if let Some(domain) = domain {
domain.to_string()
} else {
String::new()
}
);
pretty_expr(expr, None, &mut tty, show_types);
println!("{}", tty.page_feed());
}
Expand All @@ -223,7 +231,16 @@ fn render_constraints(
.join(", "),
)
}
Constraint::Permutation { .. } => (),
Constraint::Permutation {
handle, from, to, ..
} => {
println!("\n{}", handle.pretty());
println!(
"[{}] perm. [{}]",
to.iter().map(|c| c.pretty()).join(", "),
from.iter().map(|c| c.pretty()).join(", ")
)
}
Constraint::InRange { handle, exp, max } => {
let mut tty = Tty::new().with_guides();
pretty_expr(exp, None, &mut tty, false);
Expand Down
5 changes: 1 addition & 4 deletions src/exporters/wizardiop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ fn render_constraints(cs: &ConstraintSet) -> Vec<String> {
.join(", ")
)],
Constraint::Permutation {
handle,
from,
to,
signs: _,
handle, from, to, ..
} => vec![format!(
"build.Permutation(\"{}\", []zkevm.Handle{{{}}}, []zkevm.Handle{{{}}})",
handle.mangle().to_case(Case::Snake),
Expand Down
Loading

0 comments on commit b1d8c43

Please sign in to comment.