Skip to content

Commit 15aa408

Browse files
vo-nilAndreyMlashkin
authored andcommitted
This commit addresses TODOs in algebra code, including, but not limited (#88)
to: - handling errors from marshalling; - removal of extended_integral_type from fields; - final_exponentiation in pairings can fail when dealing with zero; - updated type_traits system to handle types properly; - fixed multiexp implementation and added tests for it; - added documentation references to constants origins; - removed old r1cs, ppzksnark code; - excluded evm-assigner and zkevm-framework from builds as they are deprecated and subject for removal;
1 parent e5c61e8 commit 15aa408

File tree

489 files changed

+1528
-81888
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

489 files changed

+1528
-81888
lines changed

crypto3/libs/algebra/include/nil/crypto3/algebra/algorithms/pair.hpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,12 @@
2828

2929
#include <nil/crypto3/algebra/pairing/pairing_policy.hpp>
3030

31+
#include <optional>
32+
3133
namespace nil {
3234
namespace crypto3 {
3335
namespace algebra {
3436

35-
// template<typename PairingCurveType>
36-
// typename PairingCurveType::pairing::affine_ate_g1_precomp
37-
// affine_ate_precompute_g1(const typename PairingCurveType::pairing::g1_type::value_type &P) {
38-
39-
// return PairingCurveType::pairing::affine_ate_precompute_g1(P);
40-
// }
41-
42-
// template<typename PairingCurveType>
43-
// typename PairingCurveType::pairing::affine_ate_g2_precomp
44-
// affine_ate_precompute_g2(const typename PairingCurveType::pairing::g2_type::value_type &P) {
45-
46-
// return PairingCurveType::pairing::affine_ate_precompute_g2(P);
47-
// }
48-
49-
// template<typename PairingCurveType>
50-
// typename PairingCurveType::pairing::gt_type::value_type
51-
// affine_ate_miller_loop(const typename PairingCurveType::pairing::affine_ate_g1_precomp &prec_P,
52-
// const typename PairingCurveType::pairing::affine_ate_g2_precomp &prec_Q) {
53-
54-
// return PairingCurveType::pairing::affine_ate_miller_loop(prec_P, prec_Q);
55-
// }
56-
5737
template<typename PairingCurveType, typename PairingPolicy = pairing::pairing_policy<PairingCurveType>>
5838
typename PairingPolicy::g1_precomputed_type
5939
precompute_g1(const typename PairingCurveType::template g1_type<>::value_type &P) {
@@ -89,7 +69,7 @@ namespace nil {
8969
#endif
9070

9171
template<typename PairingCurveType, typename PairingPolicy = pairing::pairing_policy<PairingCurveType>>
92-
typename PairingCurveType::gt_type::value_type
72+
std::optional<typename PairingCurveType::gt_type::value_type>
9373
pair_reduced(const typename PairingCurveType::template g1_type<>::value_type &v1,
9474
const typename PairingCurveType::template g2_type<>::value_type &v2) {
9575

@@ -111,7 +91,7 @@ namespace nil {
11191
}
11292

11393
template<typename PairingCurveType, typename PairingPolicy = pairing::pairing_policy<PairingCurveType>>
114-
typename PairingCurveType::gt_type::value_type
94+
std::optional<typename PairingCurveType::gt_type::value_type>
11595
final_exponentiation(const typename PairingCurveType::gt_type::value_type &elt) {
11696

11797
return PairingPolicy::final_exponentiation::process(elt);

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/alt_bn128/254/short_weierstrass_params.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ namespace nil {
4343
using base_field_type = typename alt_bn128_types<254>::base_field_type;
4444
using scalar_field_type = typename alt_bn128_types<254>::scalar_field_type;
4545

46-
constexpr static const typename alt_bn128_types<254>::integral_type a =
47-
typename alt_bn128_types<254>::integral_type(
48-
0x00); ///< coefficient of short Weierstrass curve $y^2=x^3+a*x+b$
49-
constexpr static const typename alt_bn128_types<254>::integral_type b =
50-
typename alt_bn128_types<254>::integral_type(
51-
0x03); ///< coefficient of short Weierstrass curve $y^2=x^3+a*x+b$
46+
///< coefficients of short Weierstrass curve $y^2=x^3+a*x+b$
47+
constexpr static const typename alt_bn128_types<254>::base_field_type::value_type
48+
a = 0x00;
49+
constexpr static const typename alt_bn128_types<254>::base_field_type::value_type
50+
b = 0x03;
5251
};
5352

5453
template<>
@@ -104,9 +103,9 @@ namespace nil {
104103
0x90689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B_cppui_modular254)};
105104
};
106105

107-
constexpr typename alt_bn128_types<254>::integral_type const
106+
constexpr typename alt_bn128_types<254>::base_field_type::value_type const
108107
alt_bn128_params<254, forms::short_weierstrass>::a;
109-
constexpr typename alt_bn128_types<254>::integral_type const
108+
constexpr typename alt_bn128_types<254>::base_field_type::value_type const
110109
alt_bn128_params<254, forms::short_weierstrass>::b;
111110

112111
constexpr std::array<

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/babyjubjub/params.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,35 @@ namespace nil {
4242
using scalar_field_type = typename babyjubjub_types::scalar_field_type;
4343

4444
// Edwards representation constants a and d
45-
constexpr static const typename babyjubjub_types::integral_type
45+
constexpr static const typename babyjubjub_types::base_field_type::value_type
4646
a = ///< twisted Edwards elliptic curve
4747
0x292FC_cppui_modular18; ///< described by equation ax^2 + y^2 = 1 + dx^2y^2
48-
constexpr static const typename babyjubjub_types::integral_type
48+
constexpr static const typename babyjubjub_types::base_field_type::value_type
4949
d = ///< twisted Edwards elliptic curve
5050
0x292F8_cppui_modular18; ///< described by equation ax^2 + y^2 = 1 + dx^2y^2
5151
};
5252

5353
constexpr
54-
typename babyjubjub_types::integral_type const babyjubjub_params<forms::twisted_edwards>::a;
54+
typename babyjubjub_types::base_field_type::value_type const babyjubjub_params<forms::twisted_edwards>::a;
5555
constexpr
56-
typename babyjubjub_types::integral_type const babyjubjub_params<forms::twisted_edwards>::d;
56+
typename babyjubjub_types::base_field_type::value_type const babyjubjub_params<forms::twisted_edwards>::d;
5757

5858
template<>
5959
struct babyjubjub_params<forms::montgomery> {
6060
using base_field_type = typename babyjubjub_types::base_field_type;
6161
using scalar_field_type = typename babyjubjub_types::scalar_field_type;
6262

6363
// Montgomery representation constants A and B
64-
constexpr static const typename babyjubjub_types::integral_type
64+
constexpr static const typename babyjubjub_types::base_field_type::value_type
6565
A = ///< Montgomery elliptic curve
6666
0x292FA_cppui_modular18; ///< described by equation b*y^2 = x^3 + a*x^2 + x
67-
constexpr static const typename babyjubjub_types::integral_type
67+
constexpr static const typename babyjubjub_types::base_field_type::value_type
6868
B = ///< Montgomery elliptic curve
6969
0x01; ///< described by equation b*y^2 = x^3 + a*x^2 + x
7070
};
7171

72-
constexpr typename babyjubjub_types::integral_type const babyjubjub_params<forms::montgomery>::A;
73-
constexpr typename babyjubjub_types::integral_type const babyjubjub_params<forms::montgomery>::B;
72+
constexpr typename babyjubjub_types::base_field_type::value_type const babyjubjub_params<forms::montgomery>::A;
73+
constexpr typename babyjubjub_types::base_field_type::value_type const babyjubjub_params<forms::montgomery>::B;
7474

7575
template<>
7676
struct babyjubjub_g1_params<forms::twisted_edwards>

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/bls12/377/short_weierstrass_params.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ namespace nil {
4343
using base_field_type = typename bls12_types<377>::base_field_type;
4444
using scalar_field_type = typename bls12_types<377>::scalar_field_type;
4545

46-
constexpr static const typename bls12_types<377>::integral_type a =
47-
typename bls12_types<377>::integral_type(
48-
0x00); ///< coefficient of short Weierstrass curve $y^2=x^3+a*x+b$
49-
constexpr static const typename bls12_types<377>::integral_type b =
50-
typename bls12_types<377>::integral_type(
51-
0x01); ///< coefficient of short Weierstrass curve $y^2=x^3+a*x+b$
46+
///< coefficients of short Weierstrass curve $y^2=x^3+a*x+b$
47+
constexpr static const typename bls12_types<377>::base_field_type::value_type
48+
a = 0x00;
49+
constexpr static const typename bls12_types<377>::base_field_type::value_type
50+
b = 0x01;
5251
};
5352

5453
template<>
@@ -109,9 +108,9 @@ namespace nil {
109108
};
110109

111110
constexpr
112-
typename bls12_types<377>::integral_type const bls12_params<377, forms::short_weierstrass>::a;
111+
typename bls12_types<377>::base_field_type::value_type const bls12_params<377, forms::short_weierstrass>::a;
113112
constexpr
114-
typename bls12_types<377>::integral_type const bls12_params<377, forms::short_weierstrass>::b;
113+
typename bls12_types<377>::base_field_type::value_type const bls12_params<377, forms::short_weierstrass>::b;
115114

116115
constexpr std::array<
117116
typename bls12_g1_params<377, forms::short_weierstrass>::field_type::value_type,

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/bls12/381/short_weierstrass_params.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ namespace nil {
4343
using base_field_type = typename bls12_types<381>::base_field_type;
4444
using scalar_field_type = typename bls12_types<381>::scalar_field_type;
4545

46-
constexpr static const typename bls12_types<381>::integral_type a =
47-
typename bls12_types<381>::integral_type(
48-
0x00); ///< coefficient of short Weierstrass curve $y^2=x^3+a*x+b$
49-
constexpr static const typename bls12_types<381>::integral_type b =
50-
typename bls12_types<381>::integral_type(
51-
0x04); ///< coefficient of short Weierstrass curve $y^2=x^3+a*x+b$
46+
///< coefficients of short Weierstrass curve $y^2=x^3+a*x+b$
47+
constexpr static const typename bls12_types<381>::base_field_type::value_type
48+
a = 0x00;
49+
constexpr static const typename bls12_types<381>::base_field_type::value_type
50+
b = 0x04;
5251
};
5352

5453
template<>
@@ -109,9 +108,9 @@ namespace nil {
109108
};
110109

111110
constexpr
112-
typename bls12_types<381>::integral_type const bls12_params<381, forms::short_weierstrass>::a;
111+
typename bls12_types<381>::base_field_type::value_type const bls12_params<381, forms::short_weierstrass>::a;
113112
constexpr
114-
typename bls12_types<381>::integral_type const bls12_params<381, forms::short_weierstrass>::b;
113+
typename bls12_types<381>::base_field_type::value_type const bls12_params<381, forms::short_weierstrass>::b;
115114

116115
constexpr typename bls12_g2_params<381, forms::short_weierstrass>::field_type::value_type const
117116
bls12_g2_params<381, forms::short_weierstrass>::b;

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/curve25519/params.hpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,14 @@ namespace nil {
8383
using scalar_field_type = typename curve25519_types::scalar_field_type;
8484
#ifdef __ZKLLVM__
8585
#else
86-
constexpr static typename curve25519_types::integral_type a = typename curve25519_types::integral_type(
87-
0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec_cppui_modular255); ///< coefficient
88-
///< of
89-
///< Twisted
90-
///< Edwards
91-
///< curves
92-
///< $a*x^2+y^2=1+d*x^2*y^2$
93-
constexpr static typename curve25519_types::integral_type d = typename curve25519_types::integral_type(
94-
0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3_cppui_modular255); ///< coefficient
95-
///< of
96-
///< Twisted
97-
///< Edwards
98-
///< curves
99-
///< $a*x^2+y^2=1+d*x^2*y^2$
86+
/* Coefficients of Twisted Edwards form:
87+
* a*x^2 + y^2 = 1 + d * x^2 * y^2
88+
* a = -1, d = -121665/121666
89+
* */
90+
constexpr static typename base_field_type::value_type a =
91+
base_field_type::modulus - 1;
92+
constexpr static typename base_field_type::value_type d =
93+
- base_field_type::value_type(121665) / base_field_type::value_type(121666);
10094
#endif
10195
};
10296

@@ -136,8 +130,8 @@ namespace nil {
136130
constexpr std::array<typename curve25519_g1_params<forms::montgomery>::field_type::value_type, 2>
137131
curve25519_g1_params<forms::montgomery>::one_fill;
138132

139-
constexpr typename curve25519_types::integral_type curve25519_params<forms::twisted_edwards>::a;
140-
constexpr typename curve25519_types::integral_type curve25519_params<forms::twisted_edwards>::d;
133+
constexpr typename curve25519_types::base_field_type::value_type curve25519_params<forms::twisted_edwards>::a;
134+
constexpr typename curve25519_types::base_field_type::value_type curve25519_params<forms::twisted_edwards>::d;
141135

142136
constexpr std::array<typename curve25519_g1_params<forms::twisted_edwards>::field_type::value_type,
143137
2>

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/forms/montgomery/element_g1_affine.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,20 @@ namespace nil {
273273
* @return doubled element from group G1
274274
*/
275275
constexpr void double_inplace() {
276-
if ( 2 * params_type::B * this->Y == field_value_type::zero() ) {
276+
const field_value_type two(2u);
277+
const field_value_type twoBY = two * params_type::B * this->Y;
278+
279+
if ( twoBY == field_value_type::zero() ) {
277280
this->is_inf_point = true;
281+
return;
278282
}
283+
279284
if (!this->is_zero()) {
280-
const field_value_type two(2u);
281285
const field_value_type three(3u);
282286
const field_value_type A(params_type::A);
283287
const field_value_type B(params_type::B);
284288

285-
const field_value_type temp1i = (two * B * this->Y).inversed();
289+
const field_value_type temp1i = twoBY.inversed();
286290
const field_value_type temp2 =
287291
three * this->X.squared() + two * A * this->X + field_value_type::one();
288292
const field_value_type temp1i_sqr = temp1i.squared();

crypto3/libs/algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ namespace nil {
229229
return *this;
230230
}
231231

232-
static curve_element from_affine(curve_element<params_type, form, curves::coordinates::affine> const &other) {
233-
return curve_element(other.X, other.Y, other.X*other.Y, field_value_type::one());
234-
}
235-
236232
template<typename Backend,
237233
boost::multiprecision::expression_template_option ExpressionTemplates>
238234
constexpr const curve_element& operator=(

0 commit comments

Comments
 (0)