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

refactor: Refactor R1CS for in-place operations and Vec inputs #121

Merged
merged 1 commit into from
Nov 15, 2023
Merged
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tracing-texray = "0.2.0"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
cfg-if = "1.0.0"
once_cell = "1.18.0"
vecshard = "0.2.1"

[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
pasta-msm = { git="https://github.com/lurk-lab/pasta-msm", branch="dev", version = "0.1.4" }
Expand Down
14 changes: 9 additions & 5 deletions src/bellpepper/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use crate::{
};
use bellpepper_core::{Index, LinearCombination};
use ff::PrimeField;
use vecshard::ShardExt;

/// `NovaWitness` provide a method for acquiring an `R1CSInstance` and `R1CSWitness` from implementers.
pub trait NovaWitness<G: Group> {
/// Return an instance and witness, given a shape and ck.
fn r1cs_instance_and_witness(
&self,
self,
shape: &R1CSShape<G>,
ck: &CommitmentKey<G>,
) -> Result<(R1CSInstance<G>, R1CSWitness<G>), NovaError>;
Expand All @@ -39,16 +40,19 @@ pub trait NovaShape<G: Group> {

impl<G: Group> NovaWitness<G> for SatisfyingAssignment<G> {
fn r1cs_instance_and_witness(
&self,
self,
shape: &R1CSShape<G>,
ck: &CommitmentKey<G>,
) -> Result<(R1CSInstance<G>, R1CSWitness<G>), NovaError> {
let W = R1CSWitness::<G>::new(shape, self.aux_assignment())?;
let X = &self.input_assignment()[1..];
let (input_assignment, aux_assignment) = self.to_assignments();

let W = R1CSWitness::<G>::new(shape, aux_assignment)?;

let (_, X) = input_assignment.split_inplace_at(1);

let comm_W = W.commit(ck);

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

Ok((instance, W))
}
Expand Down
4 changes: 2 additions & 2 deletions src/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ mod tests {
};

let W = {
let res = R1CSWitness::new(&S, &vars);
let res = R1CSWitness::new(&S, vars);
assert!(res.is_ok());
res.unwrap()
};
let U = {
let comm_W = W.commit(ck);
let res = R1CSInstance::new(&S, &comm_W, &X);
let res = R1CSInstance::new(&S, &comm_W, X);
assert!(res.is_ok());
res.unwrap()
};
Expand Down
11 changes: 4 additions & 7 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ impl<G: Group> R1CSShape<G> {

impl<G: Group> R1CSWitness<G> {
/// A method to create a witness object using a vector of scalars
pub fn new(S: &R1CSShape<G>, W: &[G::Scalar]) -> Result<R1CSWitness<G>, NovaError> {
pub fn new(S: &R1CSShape<G>, W: Vec<G::Scalar>) -> Result<R1CSWitness<G>, NovaError> {
if S.num_vars != W.len() {
Err(NovaError::InvalidWitnessLength)
} else {
Ok(R1CSWitness { W: W.to_owned() })
Ok(R1CSWitness { W })
}
}

Expand All @@ -405,15 +405,12 @@ impl<G: Group> R1CSInstance<G> {
pub fn new(
S: &R1CSShape<G>,
comm_W: &Commitment<G>,
X: &[G::Scalar],
X: Vec<G::Scalar>,
) -> Result<R1CSInstance<G>, NovaError> {
if S.num_io != X.len() {
Err(NovaError::InvalidInputLength)
} else {
Ok(R1CSInstance {
comm_W: *comm_W,
X: X.to_owned(),
})
Ok(R1CSInstance { comm_W: *comm_W, X })
}
}
}
Expand Down
Loading