Browse Source

update package name; make modules public and add docs

main
Srinath Setty 3 years ago
parent
commit
25913f659a
5 changed files with 32 additions and 4 deletions
  1. +1
    -1
      Cargo.toml
  2. +2
    -0
      src/errors.rs
  3. +3
    -3
      src/lib.rs
  4. +14
    -0
      src/r1cs.rs
  5. +12
    -0
      src/traits.rs

+ 1
- 1
Cargo.toml

@ -1,5 +1,5 @@
[package] [package]
name = "nova" name = "nova-snark"
version = "0.1.0" version = "0.1.0"
authors = ["Srinath Setty <srinath@microsoft.com>"] authors = ["Srinath Setty <srinath@microsoft.com>"]
edition = "2018" edition = "2018"

+ 2
- 0
src/errors.rs

@ -1,5 +1,7 @@
//! This module defines errors returned by the library.
use core::fmt::Debug; use core::fmt::Debug;
/// Errors returned by Nova
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum NovaError { pub enum NovaError {
/// returned if the supplied row or col in (row,col,val) tuple is out of range /// returned if the supplied row or col in (row,col,val) tuple is out of range

+ 3
- 3
src/lib.rs

@ -5,9 +5,9 @@
#![deny(missing_docs)] #![deny(missing_docs)]
mod commitments; mod commitments;
mod errors; pub mod errors;
mod r1cs; pub mod r1cs;
mod traits; pub mod traits;
use std::marker::PhantomData; use std::marker::PhantomData;

+ 14
- 0
src/r1cs.rs

@ -1,3 +1,4 @@
//! This module defines R1CS related types and a folding scheme for (relaxed) R1CS
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
use super::commitments::{CommitGens, CommitTrait, Commitment, CompressedCommitment}; use super::commitments::{CommitGens, CommitTrait, Commitment, CompressedCommitment};
use super::errors::NovaError; use super::errors::NovaError;
@ -5,11 +6,13 @@ use super::traits::{Group, PrimeField};
use itertools::concat; use itertools::concat;
use rayon::prelude::*; use rayon::prelude::*;
/// Public parameters for a given R1CS
pub struct R1CSGens<G: Group> { pub struct R1CSGens<G: Group> {
gens_W: CommitGens<G>, gens_W: CommitGens<G>,
gens_E: CommitGens<G>, gens_E: CommitGens<G>,
} }
/// A type that holds the shape of the R1CS matrices
#[derive(Debug)] #[derive(Debug)]
pub struct R1CSShape<G: Group> { pub struct R1CSShape<G: Group> {
num_cons: usize, num_cons: usize,
@ -20,12 +23,14 @@ pub struct R1CSShape {
C: Vec<(usize, usize, G::Scalar)>, C: Vec<(usize, usize, G::Scalar)>,
} }
/// A type that holds a witness for a given R1CS instance
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct R1CSWitness<G: Group> { pub struct R1CSWitness<G: Group> {
W: Vec<G::Scalar>, W: Vec<G::Scalar>,
E: Vec<G::Scalar>, E: Vec<G::Scalar>,
} }
/// A type that holds an R1CS instance
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct R1CSInstance<G: Group> { pub struct R1CSInstance<G: Group> {
comm_W: Commitment<G>, comm_W: Commitment<G>,
@ -35,6 +40,7 @@ pub struct R1CSInstance {
} }
impl<G: Group> R1CSGens<G> { impl<G: Group> R1CSGens<G> {
/// Samples public parameters for the specified number of constraints and variables in an R1CS
pub fn new(num_cons: usize, num_vars: usize) -> R1CSGens<G> { pub fn new(num_cons: usize, num_vars: usize) -> R1CSGens<G> {
// generators to commit to witness vector `W` // generators to commit to witness vector `W`
let gens_W = CommitGens::new(b"gens_W", num_vars); let gens_W = CommitGens::new(b"gens_W", num_vars);
@ -47,6 +53,7 @@ impl R1CSGens {
} }
impl<G: Group> R1CSShape<G> { impl<G: Group> R1CSShape<G> {
/// Create an object of type `R1CSShape` from the explicitly specified R1CS matrices
pub fn new( pub fn new(
num_cons: usize, num_cons: usize,
num_vars: usize, num_vars: usize,
@ -129,6 +136,7 @@ impl R1CSShape {
Ok((Az, Bz, Cz)) Ok((Az, Bz, Cz))
} }
/// Checks if the R1CS instance is satisfiable given a witness and its shape
pub fn is_sat( pub fn is_sat(
&self, &self,
gens: &R1CSGens<G>, gens: &R1CSGens<G>,
@ -175,6 +183,7 @@ impl R1CSShape {
} }
} }
/// A method to compute a commitment to the cross-term `T` given two R1CS instance-witness pairs
pub fn commit_T( pub fn commit_T(
&self, &self,
gens: &R1CSGens<G>, gens: &R1CSGens<G>,
@ -227,6 +236,7 @@ impl R1CSShape {
} }
impl<G: Group> R1CSWitness<G> { impl<G: Group> R1CSWitness<G> {
/// A method to create a witness object using a vector of scalars
pub fn new( pub fn new(
S: &R1CSShape<G>, S: &R1CSShape<G>,
W: &[G::Scalar], W: &[G::Scalar],
@ -242,10 +252,12 @@ impl R1CSWitness {
} }
} }
/// Commits to the witness using the supplied generators
pub fn commit(&self, gens: &R1CSGens<G>) -> (Commitment<G>, Commitment<G>) { pub fn commit(&self, gens: &R1CSGens<G>) -> (Commitment<G>, Commitment<G>) {
(self.W.commit(&gens.gens_W), self.E.commit(&gens.gens_E)) (self.W.commit(&gens.gens_W), self.E.commit(&gens.gens_E))
} }
/// Folds an incoming R1CSWitness into the current one
pub fn fold( pub fn fold(
&self, &self,
W2: &R1CSWitness<G>, W2: &R1CSWitness<G>,
@ -275,6 +287,7 @@ impl R1CSWitness {
} }
impl<G: Group> R1CSInstance<G> { impl<G: Group> R1CSInstance<G> {
/// A method to create an instance object using consitituent elements
pub fn new( pub fn new(
S: &R1CSShape<G>, S: &R1CSShape<G>,
comm_W: &Commitment<G>, comm_W: &Commitment<G>,
@ -294,6 +307,7 @@ impl R1CSInstance {
} }
} }
/// Folds an incoming R1CSInstance into the current one
pub fn fold( pub fn fold(
&self, &self,
U2: &R1CSInstance<G>, U2: &R1CSInstance<G>,

+ 12
- 0
src/traits.rs

@ -1,3 +1,4 @@
//! This module defines various traits required by the users of the library to implement.
use core::borrow::Borrow; use core::borrow::Borrow;
use core::fmt::Debug; use core::fmt::Debug;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
@ -53,9 +54,13 @@ pub trait Group:
+ ScalarMul<<Self as Group>::Scalar> + ScalarMul<<Self as Group>::Scalar>
+ ScalarMulOwned<<Self as Group>::Scalar> + ScalarMulOwned<<Self as Group>::Scalar>
{ {
/// A type representing an element of the scalar field of the group
type Scalar: PrimeField + ChallengeTrait; type Scalar: PrimeField + ChallengeTrait;
/// A type representing the compressed version of the group element
type CompressedGroupElement: CompressedGroup<GroupElement = Self>; type CompressedGroupElement: CompressedGroup<GroupElement = Self>;
/// A method to compute a multiexponentation
fn vartime_multiscalar_mul<I, J>(scalars: I, points: J) -> Self fn vartime_multiscalar_mul<I, J>(scalars: I, points: J) -> Self
where where
I: IntoIterator, I: IntoIterator,
@ -64,22 +69,29 @@ pub trait Group:
J::Item: Borrow<Self>, J::Item: Borrow<Self>,
Self: Clone; Self: Clone;
/// Compresses the group element
fn compress(&self) -> Self::CompressedGroupElement; fn compress(&self) -> Self::CompressedGroupElement;
/// Attempts to create a group element from a sequence of bytes,
/// failing with a `None` if the supplied bytes do not encode the group element
fn from_uniform_bytes(bytes: &[u8]) -> Option<Self>; fn from_uniform_bytes(bytes: &[u8]) -> Option<Self>;
} }
/// Represents a compressed version of a group element /// Represents a compressed version of a group element
pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static { pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static {
/// A type that holds the decompressed version of the compressed group element
type GroupElement: Group; type GroupElement: Group;
/// Decompresses the compressed group element
fn decompress(&self) -> Option<Self::GroupElement>; fn decompress(&self) -> Option<Self::GroupElement>;
/// Returns a byte array representing the compressed group element
fn as_bytes(&self) -> &[u8]; fn as_bytes(&self) -> &[u8];
} }
/// A helper trait to generate challenges using a transcript object /// A helper trait to generate challenges using a transcript object
pub trait ChallengeTrait { pub trait ChallengeTrait {
/// Returns a Scalar representing the challenge using the transcript
fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self; fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self;
} }

|||||||
x
 
000:0
Loading…
Cancel
Save