Skip to content

Commit

Permalink
Some changes related to multiprecision changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed May 15, 2024
1 parent cc48e2e commit ec27c02
Show file tree
Hide file tree
Showing 20 changed files with 1,707 additions and 1,706 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,4 @@ elseif(${CMAKE_TARGET_ARCHITECTURE} STREQUAL "ppc64")
"${CMAKE_UPPER_WORKSPACE_NAME}_HAS_POWER8")
endif()

include(CMTest)
cm_add_test_subdirectory(test)
2 changes: 1 addition & 1 deletion include/nil/crypto3/detail/static_pow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace nil {
namespace detail {
template<typename T, typename U>
constexpr T pow(T x, U n) {
T result = 1;
T result = 1u;
while (n > 0) {
if (n % 2 == 0) {
// n is even
Expand Down
155 changes: 77 additions & 78 deletions include/nil/crypto3/hash/detail/h2c/h2c_functions.hpp

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions include/nil/crypto3/hash/detail/h2c/h2c_suites.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ namespace nil {
typedef typename field_type::value_type field_value_type;

constexpr static inline const field_value_type Ai = field_value_type(
0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d_cppui381);
0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d_cppui_modular381);
constexpr static inline const field_value_type Bi = field_value_type(
0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0_cppui381);
constexpr static inline const field_value_type Z = field_value_type(11);
constexpr static integral_type h_eff = 0xd201000000010001_cppui381;
0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0_cppui_modular381);
constexpr static inline const field_value_type Z = field_value_type(11u);
constexpr static integral_type h_eff = 0xd201000000010001_cppui_modular381;
};

template<typename Coordinates, typename Form>
Expand All @@ -71,11 +71,11 @@ namespace nil {
typedef typename group_type::field_type field_type;
typedef typename field_type::value_type field_value_type;

constexpr static inline field_value_type Ai = field_value_type(0, 240);
constexpr static inline field_value_type Bi = field_value_type(1012, 1012);
constexpr static inline field_value_type Z = []() { return -field_value_type(2, 1); }();
constexpr static inline field_value_type Ai = field_value_type(0u, 240u);
constexpr static inline field_value_type Bi = field_value_type(1012u, 1012u);
constexpr static inline field_value_type Z = []() { return -field_value_type(2u, 1u); }();
constexpr static inline auto h_eff =
0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551_cppui636;
0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551_cppui_modular636;
};
} // namespace hashes
} // namespace crypto3
Expand Down
5 changes: 4 additions & 1 deletion include/nil/crypto3/hash/detail/pedersen/lookup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ namespace nil {

template<typename BitRange>
static inline result_type process(const BitRange &bits) {
return (1 - 2 * bits[2]) * (1 + bits[0] + 2 * bits[1]);
int result = (1 - 2 * bits[2]) * (1 + bits[0] + 2 * bits[1]);
if (result > 0)
return static_cast<result_type>(unsigned(result));
return result_type::modulus - unsigned(-result);
}
};
}
Expand Down
696 changes: 348 additions & 348 deletions include/nil/crypto3/hash/detail/poseidon/kimchi_constants.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace nil {
// When you permute, the last element becomes first, the others zero out.
state_[0] = state_[state_words - 1];
for (size_t i = 1; i < state_words; ++i) {
state_[i] = 0;
state_[i] = 0u;
}
state_count_ = 1;
}
Expand All @@ -90,7 +90,7 @@ namespace nil {
}

void reset() {
state_.fill(0);
state_.fill(0u);
state_count_ = 1;
}

Expand Down
2,276 changes: 1,138 additions & 1,138 deletions include/nil/crypto3/hash/detail/poseidon/original_constants.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef CRYPTO3_HASH_POSEIDON_LFSR_HPP
#define CRYPTO3_HASH_POSEIDON_LFSR_HPP

#include <nil/crypto3/multiprecision/cpp_int.hpp>
#include <nil/crypto3/multiprecision/cpp_int_modular.hpp>
#include <nil/crypto3/algebra/vector/vector.hpp>

#include <nil/crypto3/algebra/random_element.hpp>
Expand All @@ -19,7 +19,7 @@ namespace nil {
namespace crypto3 {
namespace hashes {
namespace detail {
using namespace nil::crypto3::multiprecision;
using namespace boost::multiprecision;

// Uses Grain-LFSR stream cipher for constants generation.
template<typename poseidon_policy_type>
Expand Down Expand Up @@ -48,10 +48,7 @@ namespace nil {
typedef typename poseidon_constants_type::state_vector_type state_vector_type;

constexpr static const std::size_t lfsr_state_bits = 80;
typedef number<backends::cpp_int_backend<
lfsr_state_bits, lfsr_state_bits, cpp_integer_type::unsigned_magnitude,
cpp_int_check_type::unchecked, void>>
lfsr_state_type;
typedef number<backends::cpp_int_modular_backend<lfsr_state_bits>> lfsr_state_type;

typedef typename poseidon_constants_type::round_constants_type round_constants_type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace nil {
return {};
}
for (std::size_t idx_to_zero = words_filled; idx_to_zero < block_words; ++idx_to_zero) {
block[idx_to_zero] = 0;
block[idx_to_zero] = 0u;
}
return {block};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace nil {
static state_type generate() {
static const state_type H0 = []() {
state_type H;
H.fill(word_type(0));
H.fill(word_type(0u));
return H;
}();
return H0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
#ifndef CRYPTO3_HASH_REINFORCED_CONCRETE_LFSR_HPP
#define CRYPTO3_HASH_REINFORCED_CONCRETE_LFSR_HPP

#include <nil/crypto3/multiprecision/cpp_int.hpp>
#include <nil/crypto3/multiprecision/cpp_int_modular.hpp>
#include <nil/crypto3/algebra/vector/vector.hpp>
#include <nil/crypto3/hash/detail/reinforced_concrete/reinforced_concrete_policy.hpp>

namespace nil {
namespace crypto3 {
namespace hashes {
namespace detail {
using namespace nil::crypto3::multiprecision;
using namespace boost::multiprecision;

// round constants generating is taken from poseidon_lsfr
template<typename FieldType>
Expand All @@ -50,10 +50,7 @@ namespace nil {
constexpr static const integral_type modulus = FieldType::modulus;

constexpr static const std::size_t lfsr_state_bits = 80;
typedef number<backends::cpp_int_backend<lfsr_state_bits, lfsr_state_bits,
cpp_integer_type::unsigned_magnitude,
cpp_int_check_type::unchecked, void>>
lfsr_state_type;
typedef number<backends::cpp_int_modular_backend<lfsr_state_bits>> lfsr_state_type;

constexpr static const std::size_t constants_number = full_rounds * state_words;
typedef algebra::vector<element_type, constants_number> round_constants_type;
Expand Down
9 changes: 5 additions & 4 deletions include/nil/crypto3/hash/h2f.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <nil/crypto3/hash/detail/h2f/h2f_suites.hpp>
#include <nil/crypto3/hash/detail/h2f/h2f_functions.hpp>
#include <nil/crypto3/hash/detail/stream_processors/stream_processors_enum.hpp>
#include <nil/crypto3/multiprecision/cpp_int_modular.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -139,9 +140,9 @@ namespace nil {
std::cbegin(uniform_bytes) + elm_offset + L,
std::back_inserter(imported_octets));
nil::marshalling::status_type status;
multiprecision::cpp_int tmp =
boost::multiprecision::number<boost::multiprecision::cpp_int_modular_backend<L * CHAR_BIT>> tmp =
nil::marshalling::pack<nil::marshalling::option::big_endian>(imported_octets, status);
coordinates[j] = modular_type(tmp, field_type::modulus);
coordinates[j] = modular_type(typename modular_type::backend_type(tmp.backend()));
}
result[i] = coordinates[0];
}
Expand All @@ -167,9 +168,9 @@ namespace nil {
std::cbegin(uniform_bytes) + elm_offset + L,
std::back_inserter(imported_octets));
nil::marshalling::status_type status;
multiprecision::cpp_int tmp =
boost::multiprecision::number<boost::multiprecision::cpp_int_modular_backend<L * CHAR_BIT>> tmp =
nil::marshalling::pack<nil::marshalling::option::big_endian>(imported_octets, status);
coordinates[j] = modular_type(tmp, field_type::modulus);
coordinates[j] = modular_type(typename modular_type::backend_type(tmp.backend()));
}
result[i] = field_value_type(coordinates[0], coordinates[1]);
}
Expand Down
8 changes: 8 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# http://www.boost.org/LICENSE_1_0.txt
#---------------------------------------------------------------------------#

include(CMTest)

cm_find_package(${CMAKE_WORKSPACE_NAME}_block)

if(NOT Boost_UNIT_TEST_FRAMEWORK_FOUND)
Expand Down Expand Up @@ -62,6 +64,12 @@ macro(define_hash_test name)
string(CONCAT TEST_DATA ${CMAKE_CURRENT_SOURCE_DIR} "/data/" "${name}" ".json")
target_compile_definitions(${test_name} PRIVATE TEST_DATA="${TEST_DATA}")

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(${test_name} PRIVATE "-fconstexpr-steps=2147483647")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(${test_name} PRIVATE "-fconstexpr-ops-limit=4294967295")
endif()

endmacro()

set(TESTS_NAMES
Expand Down
1 change: 0 additions & 1 deletion test/find_group_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <nil/crypto3/hash/algorithm/hash.hpp>

using namespace nil::crypto3;
using namespace nil::crypto3::multiprecision;
using namespace nil::crypto3::algebra;

template<typename FieldParams>
Expand Down
11 changes: 5 additions & 6 deletions test/h2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <nil/crypto3/hash/type_traits.hpp>

using namespace nil::crypto3;
using namespace nil::crypto3::multiprecision;
using namespace nil::crypto3::algebra;

template<typename FieldParams>
Expand Down Expand Up @@ -138,19 +137,19 @@ BOOST_AUTO_TEST_CASE(hash_to_curve_bls12_381_g1_h2c_sha256_test) {
"20925451441746926395141598720928151969"),
integral_type("1343412193624222137939591894701031123123641958980729764240763391191550653712"
"890272928110356903136085217047453540965"),
1)},
1u)},
{"abc",
group_value_type(integral_type("5137384602176159439212852477034485676478758747455673727961641554723831277"
"56567780059136521508428662765965997467907"),
integral_type("1786897908129645780825838873875416513994655004408749907941296449131605892"
"957529391590865627492442562626458913769565"),
1)},
1u)},
{"abcdef0123456789",
group_value_type(integral_type("275162876137213708468320729543710526816637518402774837215695"
"2770986741873369176463286511518644061904904607431667096"),
integral_type("563036982304416203921640398061260377444881693369806087719971"
"277317609936727208012968659302318886963927918562170633"),
1)},
1u)},
{"q128_"
"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
"qqqqqqqqqqqqqqqqqqq",
Expand All @@ -159,7 +158,7 @@ BOOST_AUTO_TEST_CASE(hash_to_curve_bls12_381_g1_h2c_sha256_test) {
"65460350679208315690319508336723080"),
integral_type("36985267390728644087495710822706285617644155774454041155969909198015237931383482"
"54443092179877354467167123794222392"),
1)},
1u)},
{"a512_"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Expand All @@ -171,7 +170,7 @@ BOOST_AUTO_TEST_CASE(hash_to_curve_bls12_381_g1_h2c_sha256_test) {
"29514132077573887342346961531624702"),
integral_type("88037208240369454347695990925650426721558805545001688510379770085674653213458594"
"2561958795215862304181527267736264"),
1)},
1u)},
// {"",
// group_value_type(
// integral_type(""),
Expand Down
1 change: 0 additions & 1 deletion test/h2f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <nil/crypto3/hash/type_traits.hpp>

using namespace nil::crypto3;
using namespace nil::crypto3::multiprecision;
using namespace nil::crypto3::algebra;

template<typename FieldParams>
Expand Down
1 change: 0 additions & 1 deletion test/pedersen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <nil/crypto3/hash/pedersen.hpp>

using namespace nil::crypto3;
using namespace nil::crypto3::multiprecision;
using namespace nil::crypto3::algebra;

template<typename FieldParams>
Expand Down
Loading

0 comments on commit ec27c02

Please sign in to comment.