Skip to content

Commit

Permalink
crypto3: add mersenne31, koalabear and babybear
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Feb 18, 2025
1 parent b95c1b9 commit 3660250
Show file tree
Hide file tree
Showing 21 changed files with 905 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2025 Andrey Nefedov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ALGEBRA_FIELDS_BABYBEAR_ARITHMETIC_PARAMS_HPP
#define CRYPTO3_ALGEBRA_FIELDS_BABYBEAR_ARITHMETIC_PARAMS_HPP

#include <nil/crypto3/algebra/fields/params.hpp>

#include <nil/crypto3/algebra/fields/babybear/base_field.hpp>

namespace nil::crypto3::algebra::fields {
template<>
struct arithmetic_params<babybear_base_field> : public params<babybear_base_field> {
constexpr static integral_type arithmetic_generator = 1u;
constexpr static integral_type multiplicative_generator = 31u;
constexpr static integral_type root_of_unity = 0x1a427a41u;
};
} // namespace nil::crypto3::algebra::fields

#endif // CRYPTO3_ALGEBRA_FIELDS_BABYBEAR_ARITHMETIC_PARAMS_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2025 Andrey Nefedov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ALGEBRA_FIELDS_KOALABEAR_ARITHMETIC_PARAMS_HPP
#define CRYPTO3_ALGEBRA_FIELDS_KOALABEAR_ARITHMETIC_PARAMS_HPP

#include <nil/crypto3/algebra/fields/params.hpp>

#include <nil/crypto3/algebra/fields/koalabear/base_field.hpp>

namespace nil::crypto3::algebra::fields {
template<>
struct arithmetic_params<koalabear_base_field> : public params<koalabear_base_field> {
constexpr static integral_type arithmetic_generator = 1u;
constexpr static integral_type multiplicative_generator = 3u;
constexpr static integral_type root_of_unity = 0x6ac49f88u;
};
} // namespace nil::crypto3::algebra::fields

#endif // CRYPTO3_ALGEBRA_FIELDS_KOALABEAR_ARITHMETIC_PARAMS_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2025 Andrey Nefedov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ALGEBRA_FIELDS_MERSENNE31_ARITHMETIC_PARAMS_HPP
#define CRYPTO3_ALGEBRA_FIELDS_MERSENNE31_ARITHMETIC_PARAMS_HPP

#include <nil/crypto3/algebra/fields/params.hpp>

#include <nil/crypto3/algebra/fields/mersenne31/base_field.hpp>

namespace nil::crypto3::algebra::fields {
template<>
struct arithmetic_params<mersenne31_base_field>
: public params<mersenne31_base_field> {
constexpr static integral_type arithmetic_generator = 1u;
constexpr static integral_type multiplicative_generator = 7u;
};
} // namespace nil::crypto3::algebra::fields

#endif // CRYPTO3_ALGEBRA_FIELDS_MERSENNE31_ARITHMETIC_PARAMS_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2025 Andrey Nefedov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ALGEBRA_FIELDS_BABYBEAR_BASE_FIELD_HPP
#define CRYPTO3_ALGEBRA_FIELDS_BABYBEAR_BASE_FIELD_HPP

#include <cstddef>

#include <nil/crypto3/multiprecision/big_mod.hpp>
#include <nil/crypto3/multiprecision/big_uint.hpp>
#include <nil/crypto3/multiprecision/literals.hpp>

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

#include <nil/crypto3/algebra/fields/field.hpp>
#include <nil/crypto3/algebra/fields/params.hpp>

namespace nil::crypto3::algebra::fields {
/**
* @brief A struct representing a Baby Bear field
*/
class babybear_base_field : public field<31> {
public:
using policy_type = field<31>;

constexpr static std::size_t value_bits = modulus_bits;
constexpr static std::size_t arity = 1;

using integral_type = policy_type::integral_type;

// 2^31 - 2^27 + 1
constexpr static integral_type modulus = 0x78000001_big_uint31;
constexpr static integral_type group_order_minus_one_half = (modulus - 1u) / 2;

using modular_type = nil::crypto3::multiprecision::auto_big_mod<modulus>;
using value_type = detail::element_fp<params<babybear_base_field>>;
};
} // namespace nil::crypto3::algebra::fields

#endif // CRYPTO3_ALGEBRA_FIELDS_BABYBEAR_BASE_FIELD_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2025 Andrey Nefedov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ALGEBRA_FIELDS_KOALABEAR_BASE_FIELD_HPP
#define CRYPTO3_ALGEBRA_FIELDS_KOALABEAR_BASE_FIELD_HPP

#include <cstddef>

#include <nil/crypto3/multiprecision/big_mod.hpp>
#include <nil/crypto3/multiprecision/big_uint.hpp>
#include <nil/crypto3/multiprecision/literals.hpp>

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

#include <nil/crypto3/algebra/fields/field.hpp>
#include <nil/crypto3/algebra/fields/params.hpp>

namespace nil::crypto3::algebra::fields {
/**
* @brief A struct representing a Koala Bear field
*/
class koalabear_base_field : public field<31> {
public:
using policy_type = field<31>;

constexpr static std::size_t value_bits = modulus_bits;
constexpr static std::size_t arity = 1;

using integral_type = policy_type::integral_type;

// 2^31 - 2^24 + 1
constexpr static integral_type modulus = 0x7f000001_big_uint31;
constexpr static integral_type group_order_minus_one_half = (modulus - 1u) / 2;

using modular_type = nil::crypto3::multiprecision::auto_big_mod<modulus>;
using value_type = detail::element_fp<params<koalabear_base_field>>;
};
} // namespace nil::crypto3::algebra::fields

#endif // CRYPTO3_ALGEBRA_FIELDS_KOALABEAR_BASE_FIELD_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2025 Andrey Nefedov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ALGEBRA_FIELDS_MERSENNE31_BASE_FIELD_HPP
#define CRYPTO3_ALGEBRA_FIELDS_MERSENNE31_BASE_FIELD_HPP

#include <cstddef>

#include <nil/crypto3/multiprecision/big_mod.hpp>
#include <nil/crypto3/multiprecision/big_uint.hpp>
#include <nil/crypto3/multiprecision/literals.hpp>

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

#include <nil/crypto3/algebra/fields/field.hpp>
#include <nil/crypto3/algebra/fields/params.hpp>

namespace nil::crypto3::algebra::fields {
/**
* @brief A struct representing a Mersenne31 field
*/
class mersenne31_base_field : public field<31> {
public:
using policy_type = field<31>;

constexpr static std::size_t value_bits = modulus_bits;
constexpr static std::size_t arity = 1;

using integral_type = policy_type::integral_type;

// 2^31 - 1
constexpr static integral_type modulus = 0x7fffffff_big_uint31;
constexpr static integral_type group_order_minus_one_half = (modulus - 1u) / 2;

using modular_type = nil::crypto3::multiprecision::auto_big_mod<modulus>;
using value_type = detail::element_fp<params<mersenne31_base_field>>;
};
} // namespace nil::crypto3::algebra::fields

#endif // CRYPTO3_ALGEBRA_FIELDS_MERSENNE31_BASE_FIELD_HPP
51 changes: 51 additions & 0 deletions crypto3/libs/algebra/test/fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@
#include <nil/crypto3/algebra/fields/secp/secp_r1/base_field.hpp>
#include <nil/crypto3/algebra/fields/secp/secp_r1/scalar_field.hpp>

#include <nil/crypto3/algebra/fields/babybear/base_field.hpp>
#include <nil/crypto3/algebra/fields/curve25519/base_field.hpp>
#include <nil/crypto3/algebra/fields/curve25519/scalar_field.hpp>
#include <nil/crypto3/algebra/fields/goldilocks64/base_field.hpp>
#include <nil/crypto3/algebra/fields/koalabear/base_field.hpp>
#include <nil/crypto3/algebra/fields/mersenne31/base_field.hpp>

#include <nil/crypto3/algebra/fields/detail/element/fp.hpp>
#include <nil/crypto3/algebra/fields/detail/element/fp2.hpp>
Expand Down Expand Up @@ -354,6 +357,27 @@ BOOST_DATA_TEST_CASE(field_operation_test_goldilocks64_fq, string_data("field_op
field_operation_test<policy_type>(data_set);
}

// BOOST_DATA_TEST_CASE(field_operation_test_mersenne31,
// string_data("field_operation_test_mersenne31"), data_set) {
// using policy_type = fields::mersenne31;

// field_operation_test<policy_type>(data_set);
// }

// BOOST_DATA_TEST_CASE(field_operation_test_koalabear,
// string_data("field_operation_test_koalabear"), data_set) {
// using policy_type = fields::koalabear;

// field_operation_test<policy_type>(data_set);
// }

// BOOST_DATA_TEST_CASE(field_operation_test_babybear,
// string_data("field_operation_test_babybear"), data_set) {
// using policy_type = fields::babybear;

// field_operation_test<policy_type>(data_set);
// }

BOOST_DATA_TEST_CASE(field_operation_test_bls12_381_fr, string_data("field_operation_test_bls12_381_fr"), data_set) {
using policy_type = fields::bls12_fr<381>;

Expand Down Expand Up @@ -467,6 +491,9 @@ BOOST_DATA_TEST_CASE(field_operation_test_secp256r1_fq, string_data("field_opera
* vesta_base_field
* goldilocks64
* mersenne31
* koalabear
* babybear
*/

Expand Down Expand Up @@ -568,6 +595,30 @@ BOOST_DATA_TEST_CASE(field_not_square_test_goldilocks64_base_field, string_data(
field_not_square_test<policy_type>(data_set);
}

// BOOST_DATA_TEST_CASE(field_not_square_test_mersenne31_base_field,
// string_data("field_not_square_test_mersenne31_base_field"),
// data_set) {
// using policy_type = typename fields::mersenne31_base_field;

// field_not_square_test<policy_type>(data_set);
// }

// BOOST_DATA_TEST_CASE(field_not_square_test_koalabear_base_field,
// string_data("field_not_square_test_koalabear_base_field"),
// data_set) {
// using policy_type = typename fields::koalabear_base_field;

// field_not_square_test<policy_type>(data_set);
// }

// BOOST_DATA_TEST_CASE(field_not_square_test_babybear_base_field,
// string_data("field_not_square_test_babybear_base_field"),
// data_set) {
// using policy_type = typename fields::babybear_base_field;

// field_not_square_test<policy_type>(data_set);
// }

BOOST_AUTO_TEST_CASE(field_not_square_test_secp_k1) {

for(auto const& data_set: string_data("field_not_square_test_secp_k1_160_base_field") ) {
Expand Down
Loading

0 comments on commit 3660250

Please sign in to comment.