From 25913f659a65a9403d63a0f00c4ecc36dec9f151 Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Thu, 2 Sep 2021 12:56:51 -0700 Subject: [PATCH] update package name; make modules public and add docs --- Cargo.toml | 2 +- src/errors.rs | 2 ++ src/lib.rs | 6 +++--- src/r1cs.rs | 14 ++++++++++++++ src/traits.rs | 12 ++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cdf275b..16e9e6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "nova" +name = "nova-snark" version = "0.1.0" authors = ["Srinath Setty "] edition = "2018" diff --git a/src/errors.rs b/src/errors.rs index 8a94815..7b63a9e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,7 @@ +//! This module defines errors returned by the library. use core::fmt::Debug; +/// Errors returned by Nova #[derive(Clone, Debug, Eq, PartialEq)] pub enum NovaError { /// returned if the supplied row or col in (row,col,val) tuple is out of range diff --git a/src/lib.rs b/src/lib.rs index 3ff3f64..63d77a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,9 @@ #![deny(missing_docs)] mod commitments; -mod errors; -mod r1cs; -mod traits; +pub mod errors; +pub mod r1cs; +pub mod traits; use std::marker::PhantomData; diff --git a/src/r1cs.rs b/src/r1cs.rs index 4f269fd..7595523 100644 --- a/src/r1cs.rs +++ b/src/r1cs.rs @@ -1,3 +1,4 @@ +//! This module defines R1CS related types and a folding scheme for (relaxed) R1CS #![allow(clippy::type_complexity)] use super::commitments::{CommitGens, CommitTrait, Commitment, CompressedCommitment}; use super::errors::NovaError; @@ -5,11 +6,13 @@ use super::traits::{Group, PrimeField}; use itertools::concat; use rayon::prelude::*; +/// Public parameters for a given R1CS pub struct R1CSGens { gens_W: CommitGens, gens_E: CommitGens, } +/// A type that holds the shape of the R1CS matrices #[derive(Debug)] pub struct R1CSShape { num_cons: usize, @@ -20,12 +23,14 @@ pub struct R1CSShape { C: Vec<(usize, usize, G::Scalar)>, } +/// A type that holds a witness for a given R1CS instance #[derive(Clone, Debug)] pub struct R1CSWitness { W: Vec, E: Vec, } +/// A type that holds an R1CS instance #[derive(Clone, Debug, PartialEq, Eq)] pub struct R1CSInstance { comm_W: Commitment, @@ -35,6 +40,7 @@ pub struct R1CSInstance { } impl R1CSGens { + /// Samples public parameters for the specified number of constraints and variables in an R1CS pub fn new(num_cons: usize, num_vars: usize) -> R1CSGens { // generators to commit to witness vector `W` let gens_W = CommitGens::new(b"gens_W", num_vars); @@ -47,6 +53,7 @@ impl R1CSGens { } impl R1CSShape { + /// Create an object of type `R1CSShape` from the explicitly specified R1CS matrices pub fn new( num_cons: usize, num_vars: usize, @@ -129,6 +136,7 @@ impl R1CSShape { Ok((Az, Bz, Cz)) } + /// Checks if the R1CS instance is satisfiable given a witness and its shape pub fn is_sat( &self, gens: &R1CSGens, @@ -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( &self, gens: &R1CSGens, @@ -227,6 +236,7 @@ impl R1CSShape { } impl R1CSWitness { + /// A method to create a witness object using a vector of scalars pub fn new( S: &R1CSShape, W: &[G::Scalar], @@ -242,10 +252,12 @@ impl R1CSWitness { } } + /// Commits to the witness using the supplied generators pub fn commit(&self, gens: &R1CSGens) -> (Commitment, Commitment) { (self.W.commit(&gens.gens_W), self.E.commit(&gens.gens_E)) } + /// Folds an incoming R1CSWitness into the current one pub fn fold( &self, W2: &R1CSWitness, @@ -275,6 +287,7 @@ impl R1CSWitness { } impl R1CSInstance { + /// A method to create an instance object using consitituent elements pub fn new( S: &R1CSShape, comm_W: &Commitment, @@ -294,6 +307,7 @@ impl R1CSInstance { } } + /// Folds an incoming R1CSInstance into the current one pub fn fold( &self, U2: &R1CSInstance, diff --git a/src/traits.rs b/src/traits.rs index 15d4274..75e08c4 100644 --- a/src/traits.rs +++ b/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::fmt::Debug; use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; @@ -53,9 +54,13 @@ pub trait Group: + ScalarMul<::Scalar> + ScalarMulOwned<::Scalar> { + /// A type representing an element of the scalar field of the group type Scalar: PrimeField + ChallengeTrait; + + /// A type representing the compressed version of the group element type CompressedGroupElement: CompressedGroup; + /// A method to compute a multiexponentation fn vartime_multiscalar_mul(scalars: I, points: J) -> Self where I: IntoIterator, @@ -64,22 +69,29 @@ pub trait Group: J::Item: Borrow, Self: Clone; + /// Compresses the group element 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; } /// Represents a compressed version of a group element 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; + /// Decompresses the compressed group element fn decompress(&self) -> Option; + /// Returns a byte array representing the compressed group element fn as_bytes(&self) -> &[u8]; } /// A helper trait to generate challenges using a transcript object pub trait ChallengeTrait { + /// Returns a Scalar representing the challenge using the transcript fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self; }