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 Ginger-Lib #144

Closed
wants to merge 88 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
5924fde
Refined test circuit for Final Darlin
DanieleDiBenedetto Sep 3, 2021
623740d
Added new benches
DanieleDiBenedetto Sep 3, 2021
d3225fa
Added proof and vk size prints + other logs
DanieleDiBenedetto Sep 3, 2021
17b2fad
Added new bench
DanieleDiBenedetto Sep 4, 2021
2296540
added shell script
UlrichHaboeck75 Sep 5, 2021
7d0c102
added todo for FinalDarlin test circuit
UlrichHaboeck75 Sep 7, 2021
1e93d2f
Adjusted to the refactored PC
phoinic Sep 12, 2021
b911473
Fix marlin branch name
phoinic Sep 12, 2021
d56bd60
Degree bound removed from tests
phoinic Sep 12, 2021
6e33192
Upated to the poly-commit refactoring phase 2
phoinic Sep 26, 2021
f130a13
Additional ops for polynomials of PC refactoring
phoinic Oct 25, 2021
5a71322
Added enforce comparison gadget imported from arkworks with Unit Test
doc78 Oct 28, 2021
adba37d
Merged with development
doc78 Oct 28, 2021
648eaa5
Merge remote-tracking branch 'origin/development' into rc/feat/comp_g…
doc78 Oct 28, 2021
47a3d5a
Enabled local path dependencie to algebra and r1cs-core
doc78 Nov 1, 2021
3c522fe
Added CondSelect Gadget for UInt64
doc78 Nov 4, 2021
e10c4a5
Added conditionally_add in FpGadget and UInt64
doc78 Nov 13, 2021
06e8778
Adjusted to pc refactored optimizations
phoinic Nov 14, 2021
252db67
Darlin benchmark restored
phoinic Nov 14, 2021
ba66f12
print-trace for poly-commit removed
phoinic Nov 14, 2021
d927216
Cleaning unused curves
phoinic Nov 14, 2021
a558c29
Reformat code
phoinic Nov 16, 2021
fbce641
Reformat code (2)
phoinic Nov 16, 2021
61fea62
Merged with development
phoinic Nov 18, 2021
1bfed13
enforce_in_field rewritten using new enforce_smaller_or_equal_than_le
doc78 Nov 22, 2021
ca0bd35
Algebra refactored
phoinic Nov 25, 2021
d99c83a
Group trait for polynomial
phoinic Nov 25, 2021
12a32ab
Unused fields models removed and field became a group
phoinic Nov 27, 2021
0aa9131
Fixed field binding
phoinic Nov 27, 2021
8f1858c
Vector of group items
phoinic Nov 28, 2021
773c114
Native vec operations for GroupVec
phoinic Nov 28, 2021
7b99cbd
to_field_vec returned
phoinic Nov 29, 2021
b7f4342
Updates for marlin
phoinic Nov 30, 2021
0f993e5
r1cs and marlin related bugfixes
phoinic Dec 1, 2021
a842e30
Submodules adjusted to refactored state
phoinic Dec 1, 2021
bc7c216
Cargo local settings commented
phoinic Dec 1, 2021
d890be2
Committer key optimization
phoinic Dec 3, 2021
7ef8626
From/To compressed bits for Jacobian
phoinic Dec 3, 2021
b04df86
ToBytes for curves bugfix
phoinic Dec 7, 2021
1c2f932
ToBytes cannonical
phoinic Dec 7, 2021
18cf717
Fix schnorr normalization
phoinic Dec 8, 2021
c65877f
Merge branch 'refactor_ginger_dev' into refactor_ginger_dev_optimizat…
phoinic Dec 8, 2021
298b225
AffineRep serializations
phoinic Dec 8, 2021
430a771
Broken UTs ignored
phoinic Dec 8, 2021
4b571f5
Secp256k1 tests uncommented and marked as ignored
phoinic Dec 9, 2021
be9e813
Merge branch 'refactor_constraint_system' into refactor_ginger_dev_1
phoinic Dec 14, 2021
3a483a9
optimized enforce_smaller_than_or_equal_le(), added negative test
UlrichHaboeck75 Dec 14, 2021
105c22c
modified enforce_smaller_or_equal() to use the k-ary and over runs of…
UlrichHaboeck75 Dec 15, 2021
f997390
simplified `conditionally_select()` for `UInt64`
UlrichHaboeck75 Dec 16, 2021
ee41f74
patched security issue in `is_smaller_than_unchecked()`
UlrichHaboeck75 Dec 16, 2021
2d9cff2
Commented develop patch. Fixed dependencies
doc78 Dec 17, 2021
ee095a0
Added check with non-constant variable compare in UT
doc78 Dec 17, 2021
49c4a8d
Updated dependencies and other fixes
DanieleDiBenedetto Dec 20, 2021
73664a3
Updated dependencies
DanieleDiBenedetto Dec 20, 2021
c46cc86
Merge with development branch
doc78 Dec 20, 2021
12e4767
Merge with current branch
doc78 Dec 20, 2021
dcbb863
Removed duplicated function
doc78 Dec 20, 2021
5147c08
Merge branch 'development' into rc/feat/comp_gadget
DanieleDiBenedetto Dec 20, 2021
b9d4a5f
Revert "modified enforce_smaller_or_equal() to use the k-ary and over…
DanieleDiBenedetto Dec 21, 2021
46b366c
Revert "optimized enforce_smaller_than_or_equal_le(), added negative …
DanieleDiBenedetto Dec 21, 2021
9f1b0be
Reverted conditionally_select in uint64
doc78 Jan 4, 2022
6e33335
Issues from review after refactoring
phoinic Jan 12, 2022
d117c67
UTs restored
phoinic Jan 19, 2022
2394bc5
Added some utility functions to LinearCombination struct
DanieleDiBenedetto Mar 1, 2022
3ee86e2
Refactored LinearCombination. Removed unneeded Group implementations …
DanieleDiBenedetto Mar 2, 2022
7c4edf5
Moved linear_combination.rs outside group submodule
DanieleDiBenedetto Mar 2, 2022
c4011d6
Fixes to algebra
DanieleDiBenedetto Mar 14, 2022
e2d3a3f
Restored doc in the curve module
DanieleDiBenedetto Mar 14, 2022
1129b55
Other fixes + removed pedersen hash and commitment
DanieleDiBenedetto Mar 14, 2022
98f78e1
Refactored exponentiation functions in GroupGadget trait
DanieleDiBenedetto Mar 15, 2022
df0d5e2
Removed any non poseidon-based merkle tree code
DanieleDiBenedetto Mar 15, 2022
79491f7
Removed non field based Schnorr signature
DanieleDiBenedetto Mar 15, 2022
f3b4c9d
Merge branch 'development' into refactor_ginger_dev
DanieleDiBenedetto Mar 15, 2022
b254ee9
Fixed curve test
DanieleDiBenedetto Mar 15, 2022
d8f93c0
Fixed proof-systems crate and aligned it with the most recent changes…
DanieleDiBenedetto Mar 17, 2022
dc062b0
Fix CI
DanieleDiBenedetto Mar 17, 2022
f2371b6
Purged existing scripts from removed curves
DanieleDiBenedetto Mar 18, 2022
cad81d0
Specialized random transformation curve test for TE curves
DanieleDiBenedetto Mar 18, 2022
de59738
Removed unneeded lifetime parameters
DanieleDiBenedetto Mar 21, 2022
083f383
Addressed some comments by Ulrich.
Apr 4, 2022
1185770
Comments from code review + added tests for GroupVec and LinearCombin…
DanieleDiBenedetto Apr 4, 2022
d654def
Minor fixes + re-introduced from_random_bytes function and renamed it…
DanieleDiBenedetto Apr 5, 2022
7c84be6
Commented broken test + addressed minor comments
DanieleDiBenedetto Apr 19, 2022
dcc62b6
Fix CI
DanieleDiBenedetto Apr 21, 2022
3554b50
Added a small TODO related to r1cs_core::LinearCombination
DanieleDiBenedetto Apr 21, 2022
db24959
Merge branch 'rc/feat/comp_gadget' into refactor_ginger_dev
DanieleDiBenedetto May 2, 2022
c283cf8
Fix automerge error
DanieleDiBenedetto May 2, 2022
741a7b4
Removed short_weierstrass_projective
DanieleDiBenedetto May 2, 2022
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 @@ -38,6 +38,7 @@ debug = true
# [patch.'https://github.com/HorizenOfficial/ginger-lib']
# algebra = { path = './algebra' }
# r1cs-core = { path = "./r1cs/core" }
# r1cs-std = { path = "./r1cs/gadgets/std" }

# [patch.'https://github.com/HorizenLabs/marlin']
# marlin = { path = '../marlin' }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The high-level structure of the repository is as follows:

* [`algebra`](algebra): implements the mathematical base components: large integers, finite fields, elliptic curves, and fast Fourier transform.
* [`primitives`](primitives): serves basic cryptographic primitives (such as hash functions and Merkle trees, signature schemes, verifiable random functions).
* [`proof-systems`](proof-systems): This is the main crate for the Darlin protocol suite. It provides the traits and structs for proof carrying data and the above mentioned proof systems. [Groth16](https://ia.cr/2016/260) and [GM17](https://ia.cr/2017/540) proving systems have been kept too for backward compatibility.
* [`proof-systems`](proof-systems): This is the main crate for the Darlin protocol suite. It provides the traits and structs for proof carrying data and the above mentioned proof systems.
* [`r1cs-core`](r1cs/core): Defines core functionalities for rank-1 constraint systems (the circuit synthesizer).
* [`r1cs-std`](r1cs/gadgets/std): This crate contains elementary "standard" circuits (or, "gadgets"): Boolean operations, native field and elliptic curve arithmetics.
* [`r1cs-crypto`](r1cs/gadgets/crypto): Provides the circuits for various cryptographic primitives, such as the Poseidon hash, signature schemes, and SNARK verifiers.
Expand Down
1 change: 1 addition & 0 deletions algebra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ num-traits = { version = "=0.2.14", default-features = false }
colored = { version = "=2.0.0", optional = true }
rayon = { version = "=1.5.1", optional = true }
clippy = { version = "=0.0.302", optional = true }
itertools = "0.10.2"

unroll = "=0.1.5"

Expand Down
2 changes: 1 addition & 1 deletion algebra/benches/criterion_msm/variable_msm_tweedle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn variable_msm(c: &mut Criterion) {
b.iter_batched(
|| {
let (v, g) = load_data(samples);
(v, DeeJacobian::batch_into_affine(g.as_slice()).unwrap())
(v, DeeJacobian::batch_into_affine(g).unwrap())
},
|(v, g)| {
add_to_trace!(
Expand Down
15 changes: 9 additions & 6 deletions algebra/src/curves/check_curve_parameters.sage
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ filename = sys.argv[1]
with open(filename) as myfile:
readfile = myfile.read()

# TODO: Handle the Montgomery and Twisted Edwards parameters too.
#### Checking if the file contains Short Weierstrass parameters. If not, the check is interrupted.
#### If there are Twisted Edwards and Montgomery parameters, they are discarded.
if 'SWModelParameters' in readfile:
Expand Down Expand Up @@ -199,19 +200,21 @@ if endo_mul_is_used:
print("WARNING! ENDO_COEFF AND ENDO_SCALAR ARE NOT CONSISTENT!")


########## Checking that shortest vector in the lattice ([1,zeta_r),[0,r]) is long enough #########
## The Halo paper (https://eprint.iacr.org/2019/1021.pdf) proves the injectivity of the endo_mul map.
## The injectivity of the map (a,b) |-> a\zeta_r + b for a,b in [0,A] (essential for using add_unsafe)
## is equivalent the lattice condition below.
# ########## Checking that shortest vector in the lattice ([1,zeta_r),[0,r]) is long enough #########
# The security of the endomorphism-based scalar multiplication(as well as its optimized gadget) relies on the injectivity of the map
# L: (a,b) -> a * zeta_r + b,
# restricted to scalars a, b from the interval [0, A], where A = 2^(lambda/2 + 1) + 2^(lambda/2) -1,
# see https://eprint.iacr.org/2019/1021.pdf. Unlike in this paper, we conclude the injectivity from the following lattice argument:
## a*zeta_r + b = a'*zeta_r + b' mod r for a,a',b,b' in [0,A]
## is equivalent to the fact that there are non-zero solutions to
## a * zeta_r = b mod r for a,b in [-A,A].
## Then it would exists c such that
## b = a * zeta_r + c * r.
## Any such solution correspond to a point of the lattice spanned by (1, zeta_r) and (0, r).
## Observe that, if (a,b) is a solution of the equation above, then it is also a point
## of the lattice spanned by (1, zeta_r) and (0, r) of length at most \sqrt(2) * A.
## (a, b) = (a, c) * (1 zeta_r)
## (0 r )
## The injectivity is equivalent to the fact that the intersection between this lattice and [-A, A]^2
## The injectivity of the map L is equivalent to the fact that the intersection between this lattice and [-A, A]^2
## is trivial. To verify this we first compute a LLL reduced basis {v,w} and
## then check if at least one of v, w, v + w, v - w is belongs to such a square.
## If not, there can't be other lattice points in the square.
Expand Down
2 changes: 1 addition & 1 deletion algebra/src/curves/ed25519/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const TE_Y: Fq = field_new!(
])
);

/// The curve ed25519 in https://en.wikipedia.org/wiki/EdDSA#Ed25519
/// The curve ed25519 describe in https://en.wikipedia.org/wiki/Curve25519.
/// is a twisted Edwards curve. These curves have equations of the
/// form: ax² + y² = 1 - dx²y².
/// over some base finite field Fq.
Expand Down
4 changes: 2 additions & 2 deletions algebra/src/curves/ed25519/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod twisted_edwards {
use super::*;
#[test]
fn test_curve() {
curve_tests::<TEEd25519>(true);
curve_tests::<TEEd25519>();
edwards_tests::<Ed25519Parameters>()
}

Expand All @@ -48,7 +48,7 @@ mod short_weierstrass {
use super::*;
#[test]
fn test_curve() {
curve_tests::<SWEd25519>(false);
curve_tests::<SWEd25519>();
sw_jacobian_tests::<Ed25519Parameters>()
}

Expand Down
44 changes: 17 additions & 27 deletions algebra/src/curves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub mod tests;
pub use self::models::*;

/// Projective representation of an elliptic curve point.
UlrichHaboeck75 marked this conversation as resolved.
Show resolved Hide resolved
/// This trait t serves curve-specific functions not covered

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo? This trait t ...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks

/// by the Group trait, in particular representation-specific
/// (i.e. mixed-type) arithmetic functions, that usually are
/// significantly faster.
pub trait Curve:
Group
+ Copy // TODO: Let's consider removing this
Expand Down Expand Up @@ -71,10 +75,10 @@ pub trait Curve:

/// Convert, if possible, a batch of `self` points to their affine equivalent.
#[inline]
fn batch_into_affine<'a>(vec_self: &'a [Self]) -> Result<Vec<Self::AffineRep>, Error> {
fn batch_into_affine(vec_self: Vec<Self>) -> Result<Vec<Self::AffineRep>, Error> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for having a Vec instead of a slice, as in batch_from_affine()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logically yes: you are consuming Self to obtain an Affine (all the usages were like this in the end, and I don't see any reason why Projective and Affine are allowed to co-exist in the same piece of code at a given moment)

vec_self
.iter()
.map(|&projective| projective.into_affine())
.into_iter()
.map(|projective| projective.into_affine())
.collect::<Result<Vec<_>, _>>()
}

Expand All @@ -93,7 +97,8 @@ pub trait Curve:
/// Add assign an affine point `other` to `self`, using mixed addition formulas.
fn add_assign_affine(&mut self, other: &Self::AffineRep);

/// Add, pairwise, the elements of each vector in `to_add`
/// Given a batch of vectors, collapses them into single-element vectors carrying
/// their respective additive totals.
fn add_in_place_affine_many(to_add: &mut [Vec<Self::AffineRep>]);

/// Multiply `self` by the scalar represented by `bits`.
Expand All @@ -117,24 +122,6 @@ pub trait Curve:
/// Multiply `self` by the inverse of the cofactor in `Self::ScalarField`.
fn scale_by_cofactor_inv(&self) -> Self;

/// Normalize `self` so that conversion to affine is cheap. Output the normalized point.
fn normalize(&self) -> Self;

/// Normalize `self` so that conversion to affine is cheap.
fn normalize_assign(&mut self);

/// Return true if `self` is normalized, false otherwise.
fn is_normalized(&self) -> bool;

/// Normalize a slice of projective elements so that conversion to affine is cheap.
fn batch_normalization(v: &mut [Self]);

/// Normalize a slice of projective elements and outputs a vector containing the affine equivalents.
fn batch_normalization_into_affine(mut v: Vec<Self>) -> Result<Vec<Self::AffineRep>, Error> {
Self::batch_normalization(v.as_mut_slice());
Self::batch_into_affine(v.as_slice())
}

/// Returns a fixed generator of unknown exponent.
#[must_use]
fn prime_subgroup_generator() -> Self;
Expand Down Expand Up @@ -164,11 +151,14 @@ pub trait Curve:
/// If and only if `parity` is set will the odd y-coordinate be selected.
fn get_point_from_x_and_parity(x: Self::BaseField, parity: bool) -> Option<Self>;

/// Returns a curve point if the set of bytes forms a valid curve point,
/// otherwise returns None. This function is primarily intended for sampling
/// random group elements from a hash-function or RNG output.
/// The sampled point is not guaranteed to be in the prime order subgroup.
fn from_random_bytes(bytes: &[u8]) -> Option<Self>;
/// Attempts to construct a valid curve point given a set of bytes.
/// It should differ from a classic deserialization function in that it will
/// try to "force" the deserialization of a valid curve point by manipulating
/// the input bytes accordingly (e.g. read a coordinate and sign flag from the
/// bytes muting the bits above the modulus).
/// This function is primarily intended for sampling random curve points from
/// a hash-function or RNG output.
fn force_deserialize(bytes: &[u8]) -> Option<Self>;
}

/// The `EndoMulCurve` trait for curves that have a non-trivial endomorphism
Expand Down
Loading