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

Endo #128

Merged
merged 39 commits into from
Nov 3, 2021
Merged

Endo #128

Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ad8851b
Initial functions
DanieleDiBenedetto Feb 16, 2021
99ad511
Endomorphism implementation
phoinic Feb 22, 2021
8b10e09
Tweedle added to r1cs
phoinic Feb 22, 2021
cb4cc6e
Endomorphism implementation for all curves
phoinic Feb 22, 2021
9079f75
mnt6 feature removed from full
phoinic Feb 22, 2021
2ea76ad
Endo gadget experiments
phoinic Feb 28, 2021
5d629da
merge: endo branch merged with audit_fixes
phoinic Jul 13, 2021
5e408d8
endo mul gadget: first steps
phoinic Jul 15, 2021
20b0888
sc_testnet_2 initial commit
DanieleDiBenedetto Sep 2, 2021
2117d44
Merge branch 'proof_size_optimization' into endo
phoinic Sep 26, 2021
b29963e
Endo mul gadget implementation
phoinic Sep 30, 2021
e8b21bd
Fix endo_rep_to_scalar algorithm
phoinic Oct 3, 2021
7b5b53d
Merge branch 'sc_testnet_2' into endo_proof_size_opt
phoinic Oct 5, 2021
8030388
Updating UTs for endo gadget
phoinic Oct 5, 2021
bc56da4
Endo gadget optimization
phoinic Oct 5, 2021
468295a
Cleanup comments
phoinic Oct 5, 2021
8804cf2
Comment for endo_mul updated
phoinic Oct 5, 2021
42aee87
endo merge and optimization
phoinic Oct 9, 2021
d65e6f6
Added the Sage scripts for checking field parameters. With respect to…
Oct 11, 2021
e5b32f7
added endo mul inline docu
UlrichHaboeck75 Oct 18, 2021
7c7545a
Applied the changes suggested by Ulrich about endo_mul parameters.
Oct 19, 2021
1791773
Refactored endo mul native implementation
phoinic Oct 25, 2021
30e0c89
Refactored endo implementation
phoinic Oct 27, 2021
89904fc
Endo mul for 'short_weierstrass_projective' model
phoinic Oct 27, 2021
780ae5f
Additional implementations and bits paddings
phoinic Oct 27, 2021
5007fca
nonnative endo implementation
phoinic Oct 27, 2021
f9a9e44
Modified to check_curve_parameters.sage according to Ulrich's request…
Oct 27, 2021
690abe5
Modified to check_curve_parameters.sage according to Ulrich's request…
Oct 27, 2021
8cf7170
Modified to check_curve_parameters.sage according to Ulrich's request…
Oct 27, 2021
4c9232c
Merge branch 'endo' of https://github.com/HorizenOfficial/ginger-lib …
Oct 27, 2021
7fafe74
Endo generic test
phoinic Oct 28, 2021
40cb120
Merge branch 'rc/audit_chain' into endo_merge
phoinic Oct 28, 2021
116d558
Merge branch 'development' into endo_merge
phoinic Oct 28, 2021
5687c47
Generic endo mul UT optimized
phoinic Oct 28, 2021
6787407
Branch switched to endo
phoinic Oct 28, 2021
177e813
Sync with pc/marlin endo branches
phoinic Oct 28, 2021
bcd8fd6
additional inline doc
UlrichHaboeck75 Nov 2, 2021
3b51d3f
Resolved conflicts in check_curve_parameters.sage
Nov 3, 2021
42470dc
Parameter LAMBDA read from from the curve file, as requested by Ulrich.
Nov 3, 2021
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: 1 addition & 5 deletions algebra/src/curves/bls12_377/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
curves::models::{ModelParameters, SWModelParameters},
fields::{
bls12_377::*,
Field, FpParameters,
Field,
},
};

Expand Down Expand Up @@ -56,10 +56,6 @@ impl SWModelParameters for Bls12_377G1Parameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = FQ_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

/// G1_GENERATOR_X =
Expand Down
6 changes: 1 addition & 5 deletions algebra/src/curves/bls12_377/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
curves::models::{ModelParameters, SWModelParameters},
fields::{
bls12_377::*,
Field, FpParameters,
Field,
},
};

Expand Down Expand Up @@ -80,10 +80,6 @@ impl SWModelParameters for Bls12_377G2Parameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = FQ2_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

pub const G2_GENERATOR_X: Fq2 = field_new!(Fq2, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
Expand Down
6 changes: 1 addition & 5 deletions algebra/src/curves/bls12_381/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
fields::{
bls12_381::*,
Field, FpParameters
Field,
},
};

Expand Down Expand Up @@ -63,10 +63,6 @@ impl SWModelParameters for Bls12_381G1Parameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = FQ_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

/// G1_GENERATOR_X =
Expand Down
6 changes: 1 addition & 5 deletions algebra/src/curves/bls12_381/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
fields::{
bls12_381::*,
Field, FpParameters,
Field,
},
};

Expand Down Expand Up @@ -72,10 +72,6 @@ impl SWModelParameters for Bls12_381G2Parameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = FQ2_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

pub const G2_GENERATOR_X: Fq2 = field_new!(Fq2, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
Expand Down
9 changes: 3 additions & 6 deletions algebra/src/curves/bn_382/g.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::{
models::short_weierstrass_jacobian::{GroupAffine, GroupProjective},
ModelParameters, SWModelParameters,
},
Field, field_new, FpParameters,
fields::bn_382::*
field_new,
fields::bn_382::*,
Field,
};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -60,10 +61,6 @@ impl SWModelParameters for Bn382GParameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = field_new!(Fr, FrParameters::R);

const ENDO_SCALAR: Self::ScalarField = FQ_ONE;
}

/// G_GENERATOR_X =
Expand Down
9 changes: 1 addition & 8 deletions algebra/src/curves/bn_382/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use crate::{
curves::models::{ModelParameters, SWModelParameters},
field_new,
fields::bn_382::*,
curves::{
models::{ModelParameters, SWModelParameters},
},
Field, field_new, FpParameters,
Field,
};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -58,10 +55,6 @@ impl SWModelParameters for Bn382G1Parameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = FQ_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

/// G1_GENERATOR_X =
Expand Down
9 changes: 3 additions & 6 deletions algebra/src/curves/bn_382/g2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
biginteger::BigInteger384 as BigInteger,
curves::models::{ModelParameters, SWModelParameters},
Field, field_new, FpParameters,
fields::bn_382::*
field_new,
fields::bn_382::*,
Field,
};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -75,10 +76,6 @@ impl SWModelParameters for Bn382G2Parameters {
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}

const ENDO_COEFF: Self::BaseField = FQ2_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

pub const G2_GENERATOR_X: Fq2 = field_new!(Fq2, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
Expand Down
65 changes: 43 additions & 22 deletions algebra/src/curves/check_curve_parameters.sage
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# The following Sage script check the consistency of the following curves parameters:
#
<<<<<<< HEAD
# 1) P=(GENERATOR_X,GENERATOR_Y) must belongs to the curve of equation E: y^2 = x^3 + Ax + B
# 2) P must have order equal to the MODULUS of the scalar field
# 3) COFACTOR must be equal to Order(E)/Order(P)
# 4) COFACTOR_INV must be the inverse of COFACTOR in the scalar Field
# 5) ENDO_COEFF must be a cube root in the base field.
# 6) ENDO_SCALAR must be a cube root in the scalar field and satisfy ENDO_SCALAR * (X, Y) == (ENDO_COEFF * X, Y)
# 6) ENDO_SCALAR must be a cube root in the scalar field and satisfy ENDO_SCALAR * (X, Y) == (ENDO_COEFF * X, Y)
# 7) The intersection of the plane lattice spanned by {(1, ENDO_SCALAR), (0, SCALAR_FIELD_MODULUS)} with the square [-A,A]^2 must be empty,
# where A = 2^65 + 2^64 + 1.
UlrichHaboeck75 marked this conversation as resolved.
Show resolved Hide resolved
=======
# 1) P=(GENERATOR_X,GENERATOR_Y) must belongs to the curve of equation E: y^2 = x^3 + Ax + B
# 2) P must have order equal to the MODULUS of the scalar field
# 3) COFACTOR must be equal to Order(E)/Order(P)
# 4) COFACTOR_INV must be the inverse of COFACTOR in the scalar Field
>>>>>>> development
UlrichHaboeck75 marked this conversation as resolved.
Show resolved Hide resolved
# Open Sage Shell in the corresponding folder and run the command
# "sage check_curve_paramaters sage [file_path_curve] [file_path_basefield] [file_path_scalarfield]".

Expand Down Expand Up @@ -67,10 +76,17 @@ scalar_field_name = re.findall(pattern, readfile)[0]
fn = "(?:" + base_field_name + "|" + scalar_field_name + ")" #fn = field name = "(:?Fr|Fq)". Useful declaration for the pattern

#### Reading the big integers list and extracting names and values
<<<<<<< HEAD
pattern = "const\s+(\w+)[:\w\s]*=\s*field_new!\([\s\w,]*\(\s*\[" + "([0-9a-fA-Fxu\s,]+)\s*" + "\]\s*\)"
big_int_ls = re.findall(pattern,readfile) #####list of couples of the form ('[VARIABLE_NAME]',"[u64],..,[u64]")

big_int_names = [b[0] for b in big_int_ls]
=======
pattern = "const\s+(\w+):\s*" + fn + "\s*=\s*field_new!\(\s*" + fn + "\s*,\s*BigInteger\d*\s*\(\s*\[" + "([0-9a-fA-Fxu\s,]+)\s*" + "\]\s*\)"
big_int_ls = re.findall(pattern,readfile) #####list of couples of the form ('[VARIABLE_NAME]',"[u64],..,[u64]")

big_int_names = [b[0] for b in big_int_ls]
>>>>>>> development
big_int_values = [BigInteger_to_number(b[1]) for b in big_int_ls]

BigIntegerLen = BigInteger_len(big_int_ls[0][1])
Expand Down Expand Up @@ -165,6 +181,7 @@ else:
if Fr(COFACTOR) * Fr(COFACTOR_INV) == Fr(SCALAR_FIELD_R):
print("Correct. COFACTOR_INV is the inverse of COFACTOR in the the scalar field.")
else:
<<<<<<< HEAD
print("WARNING! COFACTOR_INV IS NOT THE INVERSE OF COFACTOR IN THE SCALAR FIELD!")
####### Checking the correctness of ENDO_COEFF and ENDO_FACTOR ############
endo_mul_is_used = False
Expand Down Expand Up @@ -195,29 +212,33 @@ if endo_mul_is_used:
## 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 + b for a,b in [0,A] (essential for using add_unsafe)
## is equivalent the lattice condition below.
## In the Halo paper is explained that the condition that a*zeta + b != a'*zeta + b' for a,a',b,b' in [0,A] is equivalent
## to the condition that min({distance(zeta*a,zeta*a'): a,a' in [0,A]}) > A. This can be plainly restated as
## a*zeta notin [-A,A] mod r if a in [-A,A] mod r.
## This can be restated as a lattice condition: suppose in fact that
## a * zeta = b mod r for a,b in [-A,A].
## a*zeta + 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 + c * r.
## Then
## (a, b) = (a, c) * (1 zeta)
## b = a * zeta_r + c * r.
## Any such solution correspond to a point of the lattice spanned by (1, zeta_r) and (0, r).
## (a, b) = (a, c) * (1 zeta_r)
## (0 r )
## would be vector of length <= sqrt(2)*A in a lattice of determinant r (Gaussian expectation for the shortest vector ~sqrt(r)).
## If we show that the shortest vector in the lattice spanned by (1, zeta) and (0, r) is longer than sqrt(2)*A, then we
## have a sufficient (but not necessary) condition.
## We could have indeed also a necessary condition: if there were a Sage functions listing all the vector shorter than sqrt(2)*A
## one can look at them: if none of them has both coordinates in the range [-A,A] then the parameters can be accepted
## even if the shortest vector has length in the range (A, sqrt(2)A].
## Anyway, due to our choice of parameters, A ~ 2^65, while sqrt(r) ~ 2^128.
## In general it is A = 2**(l/2 + 1) + 2**(l/2), where l is the length of the input string (in endo_mul we use l = 128)
## The injectivity 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.
if endo_mul_is_used:
A = 2**65 + 2**64
from sage.modules.free_module_integer import IntegerLattice
L = IntegerLattice([[1,int(zeta_r)],[0,SCALAR_FIELD_MODULUS]])
if L.shortest_vector().norm().n() <= (sqrt(2)*A).n():
print("WARNING! WE CAN'T USE add_unsafe FOR endo_mul")
L = Matrix([[1,Integer(zeta_r)],[0,SCALAR_FIELD_MODULUS]])
Lred = L.LLL()
set = [Lred.row(0), Lred.row(1), Lred.row(0) - Lred.row(1), Lred.row(0) + Lred.row(1)]
add_unsafe = True
for v in set:
if abs(v[0]) <= A and abs(v[1]) <= A:
add_unsafe = False
if add_unsafe:
print("We can use add_unsafe for endo_mul.")
else:
print("WARNING! WE CAN'T USE add_unsafe FOR endo_mul!")
else:
print("endo_mul is not used for this curve.")
print("endo_mul is not used for this curve.")
=======
print("WARNING! COFACTOR_INV IS NOT THE INVERSE OF COFACTOR IN THE SCALAR FIELD!")
>>>>>>> development
13 changes: 2 additions & 11 deletions algebra/src/curves/mnt4753/g1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use crate::field_new;
use crate::{
biginteger::BigInteger768,
curves::{
models::{ModelParameters, SWModelParameters},
},
fields::{
mnt4753::*,
FpParameters,
}
curves::models::{ModelParameters, SWModelParameters},
fields::mnt4753::{Fq, Fr},
};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -83,10 +78,6 @@ impl SWModelParameters for MNT4G1Parameters {
/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(G1_GENERATOR_X, G1_GENERATOR_Y);

const ENDO_COEFF: Self::BaseField = FQ_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

// generator of prime order r
Expand Down
17 changes: 2 additions & 15 deletions algebra/src/curves/mnt4753/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ use crate::curves::models::mnt4::MNT4Parameters;
use crate::field_new;
use crate::{
biginteger::BigInteger768,
curves::{
models::{ModelParameters, SWModelParameters},
},
fields::{
mnt4753::*,
FpParameters,
},
curves::models::{ModelParameters, SWModelParameters},
fields::mnt4753::{Fq, Fq2, Fr},
};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -128,14 +123,6 @@ impl SWModelParameters for MNT4G2Parameters {
fn mul_by_a(elt: &Fq2) -> Fq2 {
field_new!(Fq2, MUL_BY_A_C0 * &elt.c0, MUL_BY_A_C1 * &elt.c1,)
}

const ENDO_COEFF: Self::BaseField = field_new!(
Fq2,
FQ_ONE,
FQ_ZERO
);

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

const G2_GENERATOR_X: Fq2 = field_new!(Fq2, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
Expand Down
10 changes: 1 addition & 9 deletions algebra/src/curves/mnt6/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::{
short_weierstrass_projective::{GroupAffine, GroupProjective},
AffineCurve,
},
fields::{
mnt6::*,
FpParameters,
},
fields::mnt6::*,
};
use crate::{field_new, FromBytes};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -72,11 +69,6 @@ impl SWModelParameters for MNT6G1Parameters {
/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(G1_GENERATOR_X, G1_GENERATOR_Y);


const ENDO_COEFF: Self::BaseField = FQ_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

/// G1_GENERATOR_X =
Expand Down
14 changes: 1 addition & 13 deletions algebra/src/curves/mnt6/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::{
short_weierstrass_projective::{GroupAffine, GroupProjective},
AffineCurve,
},
fields::{
mnt6::*,
FpParameters,
},
fields::mnt6::*,
};
use crate::{field_new, FromBytes};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
Expand Down Expand Up @@ -115,15 +112,6 @@ impl SWModelParameters for MNT6G2Parameters {
MUL_BY_A_C2 * &elt.c0,
)
}

const ENDO_COEFF: Self::BaseField = field_new!(
Fq3,
FQ_ONE,
FQ_ZERO,
FQ_ZERO,
);

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

const G2_GENERATOR_X: Fq3 =
Expand Down
13 changes: 2 additions & 11 deletions algebra/src/curves/mnt6753/g1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use crate::field_new;
use crate::{
biginteger::BigInteger768,
curves::{
models::{ModelParameters, SWModelParameters},
},
fields::{
mnt6753::*,
FpParameters,
},
curves::models::{ModelParameters, SWModelParameters},
fields::mnt6753::{Fq, Fr},
};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -83,10 +78,6 @@ impl SWModelParameters for MNT6G1Parameters {

const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(G1_GENERATOR_X, G1_GENERATOR_Y);

const ENDO_COEFF: Self::BaseField = FQ_ONE;

const ENDO_SCALAR: Self::ScalarField = field_new!(Fr, FrParameters::R);
}

//generator of prime order r
Expand Down
Loading