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

Allocation free synthesis #138

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
6 changes: 3 additions & 3 deletions src/bellpepper/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ impl<E: Engine> NovaWitness<E> for SatisfyingAssignment<E> {
shape: &R1CSShape<E>,
ck: &CommitmentKey<E>,
) -> Result<(R1CSInstance<E>, R1CSWitness<E>), NovaError> {
let W = R1CSWitness::<E>::new(shape, self.aux_assignment())?;
let X = &self.input_assignment()[1..];
let W = R1CSWitness::<E>::new(shape, self.aux_assignment().to_owned())?;
let X = self.input_assignment()[1..].to_owned();

let comm_W = W.commit(ck);

let instance = R1CSInstance::<E>::new(shape, &comm_W, X)?;
let instance = R1CSInstance::<E>::new(shape, comm_W, X)?;

Ok((instance, W))
}
Expand Down
142 changes: 140 additions & 2 deletions src/bellpepper/solver.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,146 @@
//! Support for generating R1CS witness using bellpepper.

use crate::traits::Engine;
use bellpepper_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable};
use ff::PrimeField;

use bellpepper::util_cs::witness_cs::WitnessCS;

/// A `ConstraintSystem` which calculates witness values for a concrete instance of an R1CS circuit.
pub type SatisfyingAssignment<E> = WitnessCS<<E as Engine>::Scalar>;

#[derive(Debug, PartialEq, Eq)]
/// A `ConstraintSystem` which calculates witness values for a concrete instance of an R1CS circuit.
pub struct WitnessViewCS<'a, Scalar>
where
Scalar: PrimeField,
{
// Assignments of variables
pub(crate) input_assignment: &'a mut Vec<Scalar>,
pub(crate) aux_assignment: &'a mut Vec<Scalar>,
}

impl<'a, Scalar> ConstraintSystem<Scalar> for WitnessViewCS<'a, Scalar>
where
Scalar: PrimeField,
{
type Root = Self;

fn new() -> Self {
unimplemented!()
}

fn alloc<F, A, AR>(&mut self, _: A, f: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<Scalar, SynthesisError>,
A: FnOnce() -> AR,
AR: Into<String>,
{
self.aux_assignment.push(f()?);

Ok(Variable(Index::Aux(self.aux_assignment.len() - 1)))
}

fn alloc_input<F, A, AR>(&mut self, _: A, f: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<Scalar, SynthesisError>,
A: FnOnce() -> AR,
AR: Into<String>,
{
self.input_assignment.push(f()?);

Ok(Variable(Index::Input(self.input_assignment.len() - 1)))
}

fn enforce<A, AR, LA, LB, LC>(&mut self, _: A, _a: LA, _b: LB, _c: LC)
where
A: FnOnce() -> AR,
AR: Into<String>,
LA: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>,
LB: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>,
LC: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>,
{
// Do nothing: we don't care about linear-combination evaluations in this context.
}

fn push_namespace<NR, N>(&mut self, _: N)
where
NR: Into<String>,
N: FnOnce() -> NR,
{
// Do nothing; we don't care about namespaces in this context.
}

fn pop_namespace(&mut self) {
// Do nothing; we don't care about namespaces in this context.
}

fn get_root(&mut self) -> &mut Self::Root {
self
}

////////////////////////////////////////////////////////////////////////////////
// Extensible
fn is_extensible() -> bool {
true
}

/// This should not be used because the whole point of [`WitnessViewCS`] is to
/// hold the witness in a external buffer, in which case we shouldn't have
/// two [`WitnessViewCS`]s.
fn extend(&mut self, _other: &Self) {
panic!("WitnessViewCS::extend");
}

////////////////////////////////////////////////////////////////////////////////
// Witness generator
fn is_witness_generator(&self) -> bool {
true
}

fn extend_inputs(&mut self, new_inputs: &[Scalar]) {
self.input_assignment.extend_from_slice(new_inputs);
}

fn extend_aux(&mut self, new_aux: &[Scalar]) {
self.aux_assignment.extend_from_slice(new_aux);
}

fn allocate_empty(&mut self, aux_n: usize, inputs_n: usize) -> (&mut [Scalar], &mut [Scalar]) {
let allocated_aux = {
let i = self.aux_assignment.len();
self.aux_assignment.resize(aux_n + i, Scalar::ZERO);
&mut self.aux_assignment[i..]
};

let allocated_inputs = {
let i = self.input_assignment.len();
self.input_assignment.resize(inputs_n + i, Scalar::ZERO);
&mut self.input_assignment[i..]
};

(allocated_aux, allocated_inputs)
}

fn inputs_slice(&self) -> &[Scalar] {
self.input_assignment
}

fn aux_slice(&self) -> &[Scalar] {
self.aux_assignment
}
}

impl<'a, Scalar: PrimeField> WitnessViewCS<'a, Scalar> {
pub fn new_view(
input_assignment: &'a mut Vec<Scalar>,
aux_assignment: &'a mut Vec<Scalar>,
) -> Self {
input_assignment.clear();
input_assignment.push(Scalar::ONE);
aux_assignment.clear();

Self {
input_assignment,
aux_assignment,
}
}
}
Loading