Skip to content

Commit

Permalink
Add goldilocks field
Browse files Browse the repository at this point in the history
  • Loading branch information
akokoshn authored and akokoshn committed Feb 23, 2024
1 parent 1899ca2 commit fe5a1c5
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
92 changes: 92 additions & 0 deletions include/nil/crypto3/algebra/fields/goldilocks64/base_field.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Alexey Kokoshnikov <[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_GOLDILOCKS64_BASE_FIELD_HPP
#define CRYPTO3_ALGEBRA_FIELDS_GOLDILOCKS64_BASE_FIELD_HPP

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

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

#include <nil/crypto3/detail/literals.hpp>

namespace nil {
namespace crypto3 {
namespace algebra {
namespace fields {
/**
* @brief A struct representing a goldilocks 64 bit field.
* https://polygon.technology/blog/plonky2-a-deep-dive#:~:text=Hamish%20Ivey%2DLaw.-,The%20Goldilocks%20Field%C2%A0,-p%20%3D%202%2064
*/
class goldilocks64_base_field : public field<64> {
public:
typedef field<64> policy_type;

constexpr static const std::size_t modulus_bits = policy_type::modulus_bits;
constexpr static const std::size_t number_bits = policy_type::number_bits;
constexpr static const std::size_t value_bits = modulus_bits;
constexpr static const std::size_t arity = 1;

typedef typename policy_type::integral_type integral_type;
typedef typename policy_type::extended_integral_type extended_integral_type;
#ifdef __ZKLLVM__
typedef __zkllvm_field_goldilocks64_base value_type;
#else
// 2^64 - 2^32 + 1
constexpr static const integral_type modulus =
0xFFFFFFFF00000001_cppui64;

typedef typename policy_type::modular_backend modular_backend;
constexpr static const modular_params_type modulus_params = modulus;
typedef nil::crypto3::multiprecision::number<
nil::crypto3::multiprecision::backends::modular_adaptor<
modular_backend,
nil::crypto3::multiprecision::backends::modular_params_ct<modular_backend, modulus_params>>>
modular_type;

typedef typename detail::element_fp<params<goldilocks64_base_field>> value_type;
#endif
};

constexpr typename std::size_t const goldilocks64_base_field::modulus_bits;
constexpr typename std::size_t const goldilocks64_base_field::number_bits;
constexpr typename std::size_t const goldilocks64_base_field::value_bits;

#ifdef __ZKLLVM__
#else
constexpr typename goldilocks64_base_field::integral_type const goldilocks64_base_field::modulus;
constexpr
typename goldilocks64_base_field::modular_params_type const goldilocks64_base_field::modulus_params;
#endif
using goldilocks64_fq = goldilocks64_base_field;

using goldilocks64 = goldilocks64_base_field;

} // namespace fields
} // namespace algebra
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ALGEBRA_FIELDS_GOLDILOCKS64_BASE_FIELD_HPP
22 changes: 21 additions & 1 deletion test/data/fields.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
{
"field_operation_test_goldilocks64_fq": [
{
"constants": [
"2"
],
"elements_values": [
"9223372035000000000",
"4611686018000000000",
"13835058053000000000",
"4611686017000000000",
"16330093920140140161",
"18446744070000000000",
"16795104342396990661",
"13920735931365696001",
"17711595954652362241",
"4611686018000000000",
"9223372034414584321"
]
}
],
"field_operation_test_bls12_381_fr": [
{
"constants": [
Expand Down Expand Up @@ -95751,4 +95771,4 @@
]
}
]
}
}
8 changes: 8 additions & 0 deletions test/fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
// #include <nil/crypto3/algebra/fields/dsa_jce.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/ffdhe_ietf.hpp>
// #include <nil/crypto3/algebra/fields/field.hpp>
// #include <nil/crypto3/algebra/fields/modp_ietf.hpp>
Expand Down Expand Up @@ -450,6 +451,7 @@ void field_not_square_test(const std::vector<std::array<const char *, 2>> &test_
}
}


BOOST_AUTO_TEST_SUITE(fields_manual_tests)

BOOST_AUTO_TEST_CASE(field_operation_perf_test_pallas, *boost::unit_test::disabled()) {
Expand Down Expand Up @@ -513,6 +515,12 @@ BOOST_AUTO_TEST_CASE(field_operation_perf_test_pallas, *boost::unit_test::disabl
<< elapsed.count() / (SAMPLES / 1000) << " ns" << std::endl;
}

BOOST_DATA_TEST_CASE(field_operation_test_goldilocks64_fq, string_data("field_operation_test_goldilocks64_fq"), data_set) {
using policy_type = fields::goldilocks64_fq;

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

0 comments on commit fe5a1c5

Please sign in to comment.