Skip to content

Commit

Permalink
Added test for is_squared for mnt6_298::fp3 #133
Browse files Browse the repository at this point in the history
Updated and renamed group_order property #133

removed pow call and replaced with precomputed constant #133

Added group_order_minus_one_half for fp #133

Fix test algebra_fields_test

Removed todo and old code #133

boost test included #133

Boos test header include #133

rewrite expression in constexpr to constant to make gcc happy #133
  • Loading branch information
vo-nil committed Feb 27, 2024
1 parent fe5a1c5 commit 2a540ff
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 36 deletions.
2 changes: 1 addition & 1 deletion include/nil/crypto3/algebra/curves/mnt6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#ifndef CRYPTO3_ALGEBRA_CURVES_MNT6_HPP
#define CRYPTO3_ALGEBRA_CURVES_MNT6_HPP

#include <nil/crypto3/algebra/curves/detail/mnt4/types.hpp>
#include <nil/crypto3/algebra/curves/detail/mnt6/types.hpp>
#include <nil/crypto3/algebra/curves/detail/mnt6/g1.hpp>
#include <nil/crypto3/algebra/curves/detail/mnt6/g2.hpp>

Expand Down
5 changes: 5 additions & 0 deletions include/nil/crypto3/algebra/fields/alt_bn128/base_field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace nil {
constexpr static const integral_type modulus =
0x30644E72E131A029B85045B68181585D97816A916871CA8D3C208C16D87CFD47_cppui254;

constexpr static const integral_type group_order_minus_one_half =
0x183227397098D014DC2822DB40C0AC2ECBC0B548B438E5469E10460B6C3E7EA3_cppui254;

typedef typename policy_type::modular_backend modular_backend;
constexpr static const modular_params_type modulus_params = modulus;
typedef nil::crypto3::multiprecision::number<
Expand All @@ -83,6 +86,8 @@ namespace nil {

constexpr typename alt_bn128_base_field<254>::integral_type const alt_bn128_base_field<254>::modulus;

constexpr typename alt_bn128_base_field<254>::integral_type const alt_bn128_base_field<254>::group_order_minus_one_half;

constexpr typename alt_bn128_base_field<254>::modular_params_type const
alt_bn128_base_field<254>::modulus_params;

Expand Down
24 changes: 12 additions & 12 deletions include/nil/crypto3/algebra/fields/arithmetic_params/bls12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace nil {
0xAA6346091755D4D2512D4356572472834C04E5E921E17619A53352A615E29DD315F831E03A7ADF8ED48AC6BD94CA1E0_cppui380;

constexpr static const integral_type modulus = policy_type::modulus;
constexpr static const integral_type group_order =
constexpr static const integral_type group_order_minus_one_half =
0xD0088F51CBFF34D258DD3DB21A5D66BB23BA5C279C2895FB39869507B587B120F55FFFF58A9FFFFDCFF7FFFFFFFD555_cppui380;
};

Expand All @@ -96,7 +96,7 @@ namespace nil {
0x135203E60180A68EE2E9C448D77A2CD91C3DEDD930B1CF60EF396489F61EB45E304466CF3E67FA0AF1EE7B04121BDEA2_cppui381};

constexpr static const integral_type modulus = policy_type::modulus;
constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x1521BD25C61AFE3A5E93C75511792F4F16E48728738235A3372CF249A4F45E82853167E8B6EE5377A98A49984BC77808EB430CE430C2E3D949742D43848D024B35FC8F69F38DBA18B1619C1B1089E7EBE76B58EBB1C1755935500000E38C71C_cppui761;
};

Expand Down Expand Up @@ -129,7 +129,7 @@ namespace nil {
0x6E2A5BB9C8DB33E973D13C71C7B5F4181B3E0D188CF06990C62C1807439B73AF_cppui255;

constexpr static const integral_type modulus = policy_type::modulus;
constexpr static const integral_type group_order =
constexpr static const integral_type group_order_minus_one_half =
0x39F6D3A994CEBEA4199CEC0404D0EC02A9DED2017FFF2DFF7FFFFFFF80000000_cppui254;
};

Expand Down Expand Up @@ -164,7 +164,7 @@ namespace nil {
0x1065AB4C0E7DDA53F72540713590CB96A2A9516C804A20E2BE8B1180449F513E50F4148BE329585581F532F8815DE20_cppui377;

constexpr static const integral_type modulus = policy_type::modulus;
constexpr static const integral_type group_order =
constexpr static const integral_type group_order_minus_one_half =
0xD71D230BE28875631D82E03650A49D8D116CF9807A89C78F79B117DD04A4000B85AEA2180000004284600000000000_cppui376;
};

Expand All @@ -190,7 +190,7 @@ namespace nil {
0x1ABEF7237D62007BB9B2EDA5AFCB52F9D179F23DBD49B8D1B24CF7C1BF8066791317689172D0F4CB90CF47182B7D7B2_cppui377};

constexpr static const integral_type modulus = policy_type::modulus;
constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x16983E85DD7FD912B489DD9B0931E25DC5C3ED257749F8FAF38A09EED26AE04E80C57DC833066E00A7389281526B62305555545DB857018C1925CAC31C64EB5E5FDE91AF8E04D256D1347970DEC00399C692A780000008508C00000000000_cppui753;
};

Expand Down Expand Up @@ -223,7 +223,7 @@ namespace nil {
0x601DFA555C48DDAB1E55EF6F1C9D713624D23FFAE2716996A4295C90F65454C_cppui251;

constexpr static const integral_type modulus = policy_type::modulus;
constexpr static const integral_type group_order =
constexpr static const integral_type group_order_minus_one_half =
0x955B2AF4D1652AB305A268F2E1BD800ACD53B7F680000008508C00000000000_cppui252;
};

Expand Down Expand Up @@ -299,11 +299,11 @@ namespace nil {
arithmetic_params<bls12_scalar_field<381>>::modulus;

constexpr typename arithmetic_params<bls12_base_field<381>>::integral_type const
arithmetic_params<bls12_base_field<381>>::group_order;
arithmetic_params<bls12_base_field<381>>::group_order_minus_one_half;
constexpr typename arithmetic_params<fp2<bls12_base_field<381>>>::extended_integral_type const
arithmetic_params<fp2<bls12_base_field<381>>>::group_order;
arithmetic_params<fp2<bls12_base_field<381>>>::group_order_minus_one_half;
constexpr typename arithmetic_params<bls12_scalar_field<381>>::integral_type const
arithmetic_params<bls12_scalar_field<381>>::group_order;
arithmetic_params<bls12_scalar_field<381>>::group_order_minus_one_half;

/************************* BLS12-377 definitions ***********************************/

Expand Down Expand Up @@ -377,11 +377,11 @@ namespace nil {
arithmetic_params<bls12_scalar_field<377>>::modulus;

constexpr typename arithmetic_params<bls12_base_field<377>>::integral_type const
arithmetic_params<bls12_base_field<377>>::group_order;
arithmetic_params<bls12_base_field<377>>::group_order_minus_one_half;
constexpr typename arithmetic_params<fp2<bls12_base_field<377>>>::extended_integral_type const
arithmetic_params<fp2<bls12_base_field<377>>>::group_order;
arithmetic_params<fp2<bls12_base_field<377>>>::group_order_minus_one_half;
constexpr typename arithmetic_params<bls12_scalar_field<377>>::integral_type const
arithmetic_params<bls12_scalar_field<377>>::group_order;
arithmetic_params<bls12_scalar_field<377>>::group_order_minus_one_half;

} // namespace fields
} // namespace algebra
Expand Down
9 changes: 9 additions & 0 deletions include/nil/crypto3/algebra/fields/bls12/base_field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace nil {
constexpr static const integral_type modulus =
0x1A0111EA397FE69A4B1BA7B6434BACD764774B84F38512BF6730D2A0F6B0F6241EABFFFEB153FFFFB9FEFFFFFFFFAAAB_cppui381;

constexpr static const integral_type group_order_minus_one_half =
0xD0088F51CBFF34D258DD3DB21A5D66BB23BA5C279C2895FB39869507B587B120F55FFFF58A9FFFFDCFF7FFFFFFFD555_cppui381;

typedef typename policy_type::modular_backend modular_backend;
constexpr static const modular_params_type modulus_params = modulus;
typedef nil::crypto3::multiprecision::number<
Expand Down Expand Up @@ -93,6 +96,9 @@ namespace nil {
constexpr static const integral_type modulus =
0x1AE3A4617C510EAC63B05C06CA1493B1A22D9F300F5138F1EF3622FBA094800170B5D44300000008508C00000000001_cppui377;

constexpr static const integral_type group_order_minus_one_half =
0xD71D230BE28875631D82E03650A49D8D116CF9807A89C78F79B117DD04A4000B85AEA2180000004284600000000000_cppui377;

typedef typename policy_type::modular_backend modular_backend;
typedef nil::crypto3::multiprecision::modular_params<modular_backend> modular_params_type;
constexpr static const modular_params_type modulus_params = modulus;
Expand Down Expand Up @@ -120,6 +126,9 @@ namespace nil {
constexpr typename bls12_base_field<381>::integral_type const bls12_base_field<381>::modulus;
constexpr typename bls12_base_field<377>::integral_type const bls12_base_field<377>::modulus;

constexpr typename bls12_base_field<381>::integral_type const bls12_base_field<381>::group_order_minus_one_half;
constexpr typename bls12_base_field<377>::integral_type const bls12_base_field<377>::group_order_minus_one_half;

constexpr
typename bls12_base_field<381>::modular_params_type const bls12_base_field<381>::modulus_params;
constexpr
Expand Down
6 changes: 4 additions & 2 deletions include/nil/crypto3/algebra/fields/detail/element/fp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP_HPP
#define CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP_HPP

#include <iostream>

#include <nil/crypto3/algebra/fields/detail/exponentiation.hpp>
#include <nil/crypto3/algebra/fields/detail/element/operations.hpp>

Expand Down Expand Up @@ -215,9 +217,9 @@ namespace nil {
return element_fp(data * data); // maybe can be done more effective
}

// TODO: maybe error here
constexpr bool is_square() const {
return (this->sqrt() != -1); // maybe can be done more effective
element_fp tmp = this->pow(policy_type::group_order_minus_one_half);
return (tmp.is_one() || tmp.is_zero());
}

template<typename PowerType,
Expand Down
2 changes: 1 addition & 1 deletion include/nil/crypto3/algebra/fields/detail/element/fp2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace nil {
}

constexpr bool is_square() const {
element_fp2 tmp = this->pow(policy_type::group_order);
element_fp2 tmp = this->pow(policy_type::group_order_minus_one_half);
return (tmp.is_one() || tmp.is_zero()); // maybe can be done more effective
}

Expand Down
2 changes: 1 addition & 1 deletion include/nil/crypto3/algebra/fields/detail/element/fp3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace nil {
}

constexpr bool is_square() const {
element_fp3 tmp = this->pow((policy_type::group_order - 1) / 2);
element_fp3 tmp = this->pow(policy_type::group_order_minus_one_half);
return (tmp.is_one() || tmp.is_zero()); // maybe can be done more effective
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace nil {
0xB20DCB5704E326A0DD3ECD4F30515275398A41A4E1DC5D347CFBBEDDA71CF82_cppui252,
0xB1FFEFD8885BF22252522C29527D19F05CFC50E9715370AB0F3A6CA462390C_cppui248};

constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x492E25C3B1E5FCE2CCD37BE01A4690E5805C2A88B1BAB031376FD2E1A6359C682344F4ABD09216425280C4E36CB656E5301039684F560809DAA2C5113AEB4D8_cppui507;

/*constexpr static const std::array<non_residue_type, 2> Frobenius_coeffs_c1 =
Expand Down Expand Up @@ -113,9 +113,8 @@ namespace nil {
2> const fp2_extension_params<alt_bn128_base_field<Version>>::nqr_to_t;

template<std::size_t Version>

constexpr typename fp2_extension_params<alt_bn128_base_field<Version>>::extended_integral_type const
fp2_extension_params<alt_bn128_base_field<Version>>::group_order;
fp2_extension_params<alt_bn128_base_field<Version>>::group_order_minus_one_half;

template<std::size_t Version>
constexpr std::array<typename fp2_extension_params<alt_bn128_base_field<Version>>::integral_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace nil {
0x6AF0E0437FF400B6831E36D6BD17FFE48395DABC2D3435E77F76E17009241C5EE67992F72EC05F4C81084FBEDE3CC09_cppui379,
0x135203E60180A68EE2E9C448D77A2CD91C3DEDD930B1CF60EF396489F61EB45E304466CF3E67FA0AF1EE7B04121BDEA2_cppui381};

constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x1521BD25C61AFE3A5E93C75511792F4F16E48728738235A3372CF249A4F45E82853167E8B6EE5377A98A49984BC77808EB430CE430C2E3D949742D43848D024B35FC8F69F38DBA18B1619C1B1089E7EBE76B58EBB1C1755935500000E38C71C_cppui761;

/*constexpr static const std::array<non_residue_type, 2> Frobenius_coeffs_c1 =
Expand Down Expand Up @@ -121,7 +121,7 @@ namespace nil {
0x00,
0x1ABEF7237D62007BB9B2EDA5AFCB52F9D179F23DBD49B8D1B24CF7C1BF8066791317689172D0F4CB90CF47182B7D7B2_cppui377};

constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x16983E85DD7FD912B489DD9B0931E25DC5C3ED257749F8FAF38A09EED26AE04E80C57DC833066E00A7389281526B62305555545DB857018C1925CAC31C64EB5E5FDE91AF8E04D256D1347970DEC00399C692A780000008508C00000000000_cppui753;

/*constexpr static const std::array<non_residue_type, 2> Frobenius_coeffs_c1 =
Expand Down Expand Up @@ -165,9 +165,9 @@ namespace nil {
fp2_extension_params<bls12_base_field<377>>::nqr_to_t;

constexpr typename fp2_extension_params<bls12_base_field<381>>::extended_integral_type const
fp2_extension_params<bls12_base_field<381>>::group_order;
fp2_extension_params<bls12_base_field<381>>::group_order_minus_one_half;
constexpr typename fp2_extension_params<bls12_base_field<377>>::extended_integral_type const
fp2_extension_params<bls12_base_field<377>>::group_order;
fp2_extension_params<bls12_base_field<377>>::group_order_minus_one_half;

constexpr std::array<typename fp2_extension_params<bls12_base_field<381>>::integral_type, 2> const
fp2_extension_params<bls12_base_field<381>>::Frobenius_coeffs_c1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace nil {
0xB20DCB5704E326A0DD3ECD4F30515275398A41A4E1DC5D347CFBBEDDA71CF82_cppui252,
0xB1FFEFD8885BF22252522C29527D19F05CFC50E9715370AB0F3A6CA462390C_cppui248};

constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x492E25C3B1E5FCE2CCD37BE01A4690E5805C2A88B1BAB031376FD2E1A6359C682344F4ABD09216425280C4E36CB656E5301039684F560809DAA2C5113AEB4D8_cppui507;

/*constexpr static const std::array<non_residue_type, 2> Frobenius_coeffs_c1 =
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace nil {

template<std::size_t Version>
constexpr typename fp2_extension_params<bn128_base_field<Version>>::extended_integral_type const
fp2_extension_params<bn128_base_field<Version>>::group_order;
fp2_extension_params<bn128_base_field<Version>>::group_order_minus_one_half;

template<std::size_t Version>
constexpr std::array<typename fp2_extension_params<bn128_base_field<Version>>::integral_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace nil {
0x1366271F76AB41CEEEE8C1E5E972F3CEC14A25F18B3F4B93642FAD4972356D977470E0FA674_cppui297,
0x00, 0x00};

constexpr static const extended_integral_type group_order =
constexpr static const extended_integral_type group_order_minus_one_half =
0x1A1E3D618BA643D0F7F10B59BD7DB6981AD661CC756DCF7EC82F4F320CF354C814FAB1F72198E11AAE5A65BFAC8866CDA5F25E91FE3405FB619822AE7756E3F1CBC0B60FBD44114FC23E7CC3932D198CBE6F3DF9DF28E58FF8DBDC80329943BF3F003B81A48CADD598E4CEF600000000_cppui893;

/*constexpr static const std::array<non_residue_type, 3> Frobenius_coeffs_c1 =
Expand Down Expand Up @@ -123,7 +123,7 @@ namespace nil {

template<std::size_t Version>
constexpr typename fp3_extension_params<mnt6_base_field<Version>>::extended_integral_type const
fp3_extension_params<mnt6_base_field<Version>>::group_order;
fp3_extension_params<mnt6_base_field<Version>>::group_order_minus_one_half;

template<std::size_t Version>
constexpr typename fp3_extension_params<mnt6_base_field<Version>>::integral_type const
Expand Down
3 changes: 3 additions & 0 deletions include/nil/crypto3/algebra/fields/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef CRYPTO3_ALGEBRA_FIELDS_PARAMS_HPP
#define CRYPTO3_ALGEBRA_FIELDS_PARAMS_HPP

#include <cstddef>

namespace nil {
namespace crypto3 {
namespace algebra {
Expand All @@ -44,6 +46,7 @@ namespace nil {
typedef typename field_type::extended_integral_type extended_integral_type;

constexpr static const integral_type modulus = field_type::modulus;
constexpr static const integral_type group_order_minus_one_half = field_type::group_order_minus_one_half;

constexpr static const modular_params_type modulus_params = field_type::modulus_params;
};
Expand Down
2 changes: 1 addition & 1 deletion include/nil/crypto3/algebra/pairing/alt_bn128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <nil/crypto3/algebra/curves/babyjubjub.hpp>
#include <nil/crypto3/algebra/curves/alt_bn128.hpp>
#include <nil/crypto3/algebra/pairing/detail/alt_bn128/254/params.hpp>
#include <nil/crypto3/algebra/pairing/detail/alt_bn128/params.hpp>
#include <nil/crypto3/algebra/pairing/forms/short_weierstrass/jacobian_with_a4_0/ate_double_miller_loop.hpp>
#include <nil/crypto3/algebra/pairing/forms/short_weierstrass/jacobian_with_a4_0/ate_miller_loop.hpp>
#include <nil/crypto3/algebra/pairing/forms/short_weierstrass/jacobian_with_a4_0/ate_precompute_g1.hpp>
Expand Down
2 changes: 1 addition & 1 deletion test/curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <iostream>
#include <type_traits>

#include <boost/test/unit_test.hpp>
#include <boost/test/included/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/data/monomorphic.hpp>

Expand Down
Loading

0 comments on commit 2a540ff

Please sign in to comment.