From fa8f589ee8aefef158cfee96e1a9e0e6a93abbff Mon Sep 17 00:00:00 2001 From: Noah Kim Date: Tue, 13 Aug 2024 14:04:56 +0900 Subject: [PATCH 01/25] fix: fix typo --- third_party/icicle/icicle.BUILD | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/third_party/icicle/icicle.BUILD b/third_party/icicle/icicle.BUILD index 53378435aa..a19c38cb8a 100644 --- a/third_party/icicle/icicle.BUILD +++ b/third_party/icicle/icicle.BUILD @@ -70,7 +70,7 @@ tachyon_cuda_library( ]), hdrs = ["icicle/src/polynomials/cuda_backend/kernels.cu.h"], include_prefix = "third_party/icicle/src", - includes = ["includes/src/polynomials"], + includes = ["icicle/src/polynomials"], local_defines = icicle_defines(field), strip_include_prefix = "icicle/src", deps = [ @@ -90,7 +90,7 @@ tachyon_cuda_library( "icicle/src/poseidon/poseidon.cu.cc", ], include_prefix = "third_party/icicle/src", - includes = ["includes/src/poseidon"], + includes = ["icicle/src/poseidon"], local_defines = icicle_defines(field), strip_include_prefix = "icicle/src", deps = [":hdrs"], @@ -104,7 +104,7 @@ tachyon_cuda_library( "icicle/src/poseidon2/poseidon.cu.cc", ], include_prefix = "third_party/icicle/src", - includes = ["includes/src/poseidon2"], + includes = ["icicle/src/poseidon2"], local_defines = icicle_defines(field), strip_include_prefix = "icicle/src", deps = [":hdrs"], From c0d4b3e5930488985aefe3ac52ca589293bd80d8 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 15:32:46 +0900 Subject: [PATCH 02/25] feat(crypto): add `Poseidon2Vendor` --- .../hashes/sponge/poseidon2/BUILD.bazel | 9 +++++++++ .../sponge/poseidon2/poseidon2_vendor.cc | 20 +++++++++++++++++++ .../sponge/poseidon2/poseidon2_vendor.h | 19 ++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.cc create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h diff --git a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel index 14e1eb948a..97bde8e77d 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel @@ -94,6 +94,15 @@ tachyon_cc_library( ], ) +tachyon_cc_library( + name = "poseidon2_vendor", + hdrs = ["poseidon2_vendor.h"], + deps = [ + "//tachyon:export", + "//tachyon/base:logging", + ], +) + tachyon_cc_unittest( name = "poseidon2_unittests", srcs = [ diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.cc b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.cc new file mode 100644 index 0000000000..a854078fa2 --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.cc @@ -0,0 +1,20 @@ +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h" + +#include + +#include "tachyon/base/logging.h" + +namespace tachyon::crypto { + +std::string Poseidon2VendorToString(Poseidon2Vendor vendor) { + switch (vendor) { + case Poseidon2Vendor::kHorizen: + return "horizen"; + case Poseidon2Vendor::kPlonky3: + return "plonky3"; + } + NOTREACHED(); + return ""; +} + +} // namespace tachyon::crypto diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h new file mode 100644 index 0000000000..294ebb531f --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h @@ -0,0 +1,19 @@ +#ifndef TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_POSEIDON2_VENDOR_H_ +#define TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_POSEIDON2_VENDOR_H_ + +#include + +#include "tachyon/export.h" + +namespace tachyon::crypto { + +enum class Poseidon2Vendor { + kHorizen, + kPlonky3, +}; + +TACHYON_EXPORT std::string Poseidon2VendorToString(Poseidon2Vendor vendor); + +} // namespace tachyon::crypto + +#endif // TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_POSEIDON2_VENDOR_H_ From 166f4127b77ba450e71f0bd533536637724f7859 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 15:38:21 +0900 Subject: [PATCH 03/25] refac(c/zk): remove redundant namespace --- benchmark/poseidon2/poseidon2_benchmark.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/poseidon2/poseidon2_benchmark.cc b/benchmark/poseidon2/poseidon2_benchmark.cc index 9013ebb7c6..684e7c52ea 100644 --- a/benchmark/poseidon2/poseidon2_benchmark.cc +++ b/benchmark/poseidon2/poseidon2_benchmark.cc @@ -42,18 +42,18 @@ void Run(SimpleReporter& reporter, const Poseidon2Config& config, Fn horizen_fn, if (base::Contains(config.vendors(), Vendor::Plonky3())) { using Params = Poseidon2Params; auto poseidon2_config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalShiftArray()); + GetPoseidon2InternalShiftArray()); result = runner.Run(poseidon2_config); } else { using Params = Poseidon2Params; auto poseidon2_config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalDiagonalArray()); + GetPoseidon2InternalDiagonalArray()); result = runner.Run(poseidon2_config); } } else { using Params = Poseidon2Params; auto poseidon2_config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalDiagonalArray()); + GetPoseidon2InternalDiagonalArray()); result = runner.Run(poseidon2_config); } for (const Vendor vendor : config.vendors()) { From f3928bdb233e0c027f5a9f5c5fdbfb92c6bedbc3 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 15:48:28 +0900 Subject: [PATCH 04/25] refac(crypto): remove redundant type aliases --- .../merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index 6a485bc97e..73e23261dc 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -41,8 +41,6 @@ namespace { class FieldMerkleTreeTest : public math::FiniteFieldTest { public: - using Params = Poseidon2Params; - using PackedParams = Poseidon2Params; void SetUp() override { auto config = Poseidon2Config::Create( GetPoseidon2InternalShiftArray()); From 86a771411a0a220a57da36a69caa86aeef088f7b Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 15:52:22 +0900 Subject: [PATCH 05/25] refac: add `Poseidon2Vendor` to `PoseidonParams` --- benchmark/fri/fri_benchmark.cc | 12 ++++++++++-- benchmark/poseidon2/poseidon2_benchmark.cc | 12 +++++++++--- .../baby_bear_poseidon2_duplex_challenger.cc | 4 +++- ...ar_poseidon2_duplex_challenger_type_traits.h | 4 +++- ...bear_poseidon2_duplex_challenger_unittest.cc | 6 ++++-- .../air/sp1/baby_bear_poseidon2_two_adic_fri.cc | 7 +++++-- ...by_bear_poseidon2_two_adic_fri_type_traits.h | 10 ++++++++-- .../challenger/duplex_challenger_unittest.cc | 5 ++++- .../challenger/hash_challenger_unittest.cc | 5 ++++- .../multi_field32_challenger_unittest.cc | 5 ++++- .../commitments/fri/two_adic_fri_unittest.cc | 8 ++++++-- ...extension_field_merkle_tree_mmcs_unittest.cc | 6 ++++-- .../field_merkle_tree_mmcs_unittest.cc | 9 +++++++-- .../field_merkle_tree_unittest.cc | 9 +++++++-- .../sponge/padding_free_sponge_unittest.cc | 3 ++- .../crypto/hashes/sponge/poseidon2/BUILD.bazel | 1 + .../hashes/sponge/poseidon2/poseidon2_params.h | 9 +++++++-- .../sponge/poseidon2/poseidon2_unittest.cc | 17 ++++++++++++----- .../sponge/truncated_permutation_unittest.cc | 4 +++- 19 files changed, 103 insertions(+), 33 deletions(-) diff --git a/benchmark/fri/fri_benchmark.cc b/benchmark/fri/fri_benchmark.cc index 032e42d022..0e91965c4a 100644 --- a/benchmark/fri/fri_benchmark.cc +++ b/benchmark/fri/fri_benchmark.cc @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "absl/strings/substitute.h" @@ -50,8 +53,13 @@ void Run(const FRIConfig& config) { using ExtF = math::BabyBear4; using PackedF = math::PackedBabyBear; using ExtPackedF = math::PackedBabyBear4; - using Params = crypto::Poseidon2Params; - using PackedParams = crypto::Poseidon2Params; + using Params = + crypto::Poseidon2Params; + using PackedParams = + crypto::Poseidon2Params; using Poseidon2 = crypto::Poseidon2Sponge>, diff --git a/benchmark/poseidon2/poseidon2_benchmark.cc b/benchmark/poseidon2/poseidon2_benchmark.cc index 684e7c52ea..6172cf6953 100644 --- a/benchmark/poseidon2/poseidon2_benchmark.cc +++ b/benchmark/poseidon2/poseidon2_benchmark.cc @@ -40,18 +40,24 @@ void Run(SimpleReporter& reporter, const Poseidon2Config& config, Fn horizen_fn, Field result; if constexpr (std::is_same_v) { if (base::Contains(config.vendors(), Vendor::Plonky3())) { - using Params = Poseidon2Params; + using Params = + Poseidon2Params; auto poseidon2_config = crypto::Poseidon2Config::Create( GetPoseidon2InternalShiftArray()); result = runner.Run(poseidon2_config); } else { - using Params = Poseidon2Params; + using Params = + Poseidon2Params; auto poseidon2_config = crypto::Poseidon2Config::Create( GetPoseidon2InternalDiagonalArray()); result = runner.Run(poseidon2_config); } } else { - using Params = Poseidon2Params; + using Params = + Poseidon2Params; auto poseidon2_config = crypto::Poseidon2Config::Create( GetPoseidon2InternalDiagonalArray()); result = runner.Run(poseidon2_config); diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc index b6466740c1..291979a33e 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc @@ -15,7 +15,9 @@ using namespace tachyon; using F = math::BabyBear; using Params = - crypto::Poseidon2Params; using Poseidon2 = crypto::Poseidon2Sponge< crypto::Poseidon2ExternalMatrix>, diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h index a768846b9d..fa5bf15244 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h @@ -15,7 +15,9 @@ namespace tachyon::c::base { namespace { using Params = tachyon::crypto::Poseidon2Params< - tachyon::math::BabyBear, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, + tachyon::crypto::Poseidon2Vendor::kPlonky3, + tachyon::crypto::Poseidon2Vendor::kPlonky3, tachyon::math::BabyBear, + TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_ALPHA>; } // namespace diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc index 5c3f3bad58..bb67a0b5a5 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc @@ -11,8 +11,10 @@ namespace tachyon::crypto { using F = math::BabyBear; -using Params = Poseidon2Params; +using Params = + Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri.cc index e97eb11e4a..1c1274b242 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri.cc @@ -34,10 +34,13 @@ using ExtMMCS = c::zk::air::sp1::baby_bear::ExtMMCS; using ChallengeMMCS = c::zk::air::sp1::baby_bear::ChallengeMMCS; using PCS = c::zk::air::sp1::baby_bear::PCS; using Params = - crypto::Poseidon2Params; using PackedParams = - crypto::Poseidon2Params; diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h index 0fa4d42f86..7645f859d6 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h @@ -21,11 +21,17 @@ namespace tachyon::c { namespace zk::air::sp1::baby_bear { using Params = tachyon::crypto::Poseidon2Params< - F, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, + tachyon::crypto::Poseidon2Vendor::kPlonky3, + tachyon::crypto::Poseidon2Vendor::kPlonky3, F, + TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_ALPHA>; + using PackedParams = tachyon::crypto::Poseidon2Params< - PackedF, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, + tachyon::crypto::Poseidon2Vendor::kPlonky3, + tachyon::crypto::Poseidon2Vendor::kPlonky3, PackedF, + TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_ALPHA>; + using Poseidon2 = tachyon::crypto::Poseidon2Sponge< tachyon::crypto::Poseidon2ExternalMatrix< tachyon::crypto::Poseidon2Plonky3ExternalMatrix>, diff --git a/tachyon/crypto/challenger/duplex_challenger_unittest.cc b/tachyon/crypto/challenger/duplex_challenger_unittest.cc index 522e3a6db8..68635af695 100644 --- a/tachyon/crypto/challenger/duplex_challenger_unittest.cc +++ b/tachyon/crypto/challenger/duplex_challenger_unittest.cc @@ -5,6 +5,8 @@ #include "tachyon/crypto/challenger/duplex_challenger.h" +#include + #include "gtest/gtest.h" #include "tachyon/base/bits.h" @@ -17,7 +19,8 @@ namespace tachyon::crypto { using F = math::BabyBear; -using Params = Poseidon2Params; +using Params = Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; diff --git a/tachyon/crypto/challenger/hash_challenger_unittest.cc b/tachyon/crypto/challenger/hash_challenger_unittest.cc index cc72e0f504..faeb21d856 100644 --- a/tachyon/crypto/challenger/hash_challenger_unittest.cc +++ b/tachyon/crypto/challenger/hash_challenger_unittest.cc @@ -6,6 +6,8 @@ #include "tachyon/crypto/challenger/hash_challenger.h" #include +#include +#include #include "gtest/gtest.h" @@ -19,7 +21,8 @@ namespace tachyon::crypto { using F = math::BabyBear; -using Params = Poseidon2Params; +using Params = Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; diff --git a/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc b/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc index 5074118e94..a9d1c7bb56 100644 --- a/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc +++ b/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc @@ -6,6 +6,7 @@ #include "tachyon/crypto/challenger/multi_field32_challenger.h" #include +#include #include "gtest/gtest.h" @@ -17,7 +18,9 @@ namespace tachyon::crypto { -using Params = Poseidon2Params; +using Params = + Poseidon2Params; using Poseidon2 = Poseidon2Sponge< Poseidon2ExternalMatrix>, Params>; diff --git a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc index 1de1c49211..8eeedec587 100644 --- a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc +++ b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc @@ -1,6 +1,8 @@ #include "tachyon/crypto/commitments/fri/two_adic_fri.h" #include +#include +#include #include "gtest/gtest.h" @@ -28,8 +30,10 @@ using F = math::BabyBear; using ExtF = math::BabyBear4; using PackedF = math::PackedBabyBear; using ExtPackedF = math::PackedBabyBear4; -using Params = Poseidon2Params; -using PackedParams = Poseidon2Params; +using Params = Poseidon2Params; +using PackedParams = Poseidon2Params; using Domain = TwoAdicMultiplicativeCoset; using Poseidon2 = Poseidon2Sponge>, diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc index c971665611..d506be3882 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc @@ -28,8 +28,10 @@ constexpr size_t kN = 2; using F = math::BabyBear; using ExtF = math::BabyBear4; using PackedF = math::PackedBabyBear; -using Params = Poseidon2Params; -using PackedParams = Poseidon2Params; +using Params = Poseidon2Params; +using PackedParams = Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc index 13fa2f45cd..1d74f63c4b 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc @@ -5,6 +5,9 @@ #include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs.h" +#include +#include + #include "gtest/gtest.h" #include "tachyon/crypto/hashes/sponge/padding_free_sponge.h" @@ -23,8 +26,10 @@ constexpr size_t kN = 2; using F = math::BabyBear; using PackedF = math::PackedBabyBear; -using Params = Poseidon2Params; -using PackedParams = Poseidon2Params; +using Params = Poseidon2Params; +using PackedParams = Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index 73e23261dc..13b53925d7 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -5,6 +5,9 @@ #include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree.h" +#include +#include + #include "gtest/gtest.h" #include "tachyon/crypto/hashes/sponge/padding_free_sponge.h" @@ -23,8 +26,10 @@ constexpr size_t kN = 2; using F = math::BabyBear; using PackedF = math::PackedBabyBear; -using Params = Poseidon2Params; -using PackedParams = Poseidon2Params; +using Params = Poseidon2Params; +using PackedParams = Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc index 7a17fafa51..1236151ab9 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc @@ -22,7 +22,8 @@ class PaddingFreeSpongeTest : public math::FiniteFieldTest {}; } // namespace TEST_F(PaddingFreeSpongeTest, Hash) { - using Params = Poseidon2Params; + using Params = Poseidon2Params; using Poseidon2 = Poseidon2Sponge< Poseidon2ExternalMatrix>, Params>; constexpr size_t kRate = 8; diff --git a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel index 97bde8e77d..a6ce392ee0 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel @@ -71,6 +71,7 @@ tachyon_cc_library( name = "poseidon2_params", hdrs = ["poseidon2_params.h"], deps = [ + ":poseidon2_vendor", "//tachyon/base/types:always_false", "//tachyon/math/elliptic_curves/bn/bn254:fr", "//tachyon/math/finite_fields:finite_field_traits", diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h index 541c112bb3..f2a82c8bdb 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h @@ -8,6 +8,7 @@ #include "tachyon/base/bits.h" #include "tachyon/base/types/always_false.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h" #include "tachyon/math/elliptic_curves/bn/bn254/fr.h" #include "tachyon/math/finite_fields/finite_field_traits.h" @@ -58,13 +59,17 @@ constexpr size_t GetPoseidon2PartialRounds() { return size_t{56}; } -template ())> struct Poseidon2Params { using Field = _Field; + constexpr static Poseidon2Vendor kExternalMatrixVendor = ExternalMatrixVendor; + constexpr static Poseidon2Vendor kInternalMatrixVendor = InternalMatrixVendor; + // The rate (in terms of number of field elements). // See https://iacr.org/archive/eurocrypt2008/49650180/49650180.pdf constexpr static size_t kRate = Rate; diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc index cabbd3c5ad..8fb19f8377 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc @@ -5,6 +5,7 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" +#include #include #include "gtest/gtest.h" @@ -27,7 +28,8 @@ class Poseidon2GoldilocksTest : public math::FiniteFieldTest { TEST_F(Poseidon2GoldilocksTest, Permute) { using F = math::Goldilocks; - using Params = Poseidon2Params; + using Params = Poseidon2Params; auto config = Poseidon2Config::Create( crypto::GetPoseidon2InternalDiagonalArray()); @@ -50,7 +52,8 @@ TEST_F(Poseidon2GoldilocksTest, Permute) { TEST_F(Poseidon2GoldilocksTest, Copyable) { using F = math::Goldilocks; - using Params = Poseidon2Params; + using Params = Poseidon2Params; auto config = Poseidon2Config::Create( crypto::GetPoseidon2InternalDiagonalArray()); @@ -82,7 +85,8 @@ class Poseidon2BabyBearTest TEST_F(Poseidon2BabyBearTest, Permute) { using F = math::BabyBear; - using Params = Poseidon2Params; + using Params = Poseidon2Params; auto config = Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); @@ -106,8 +110,11 @@ TEST_F(Poseidon2BabyBearTest, Permute) { TEST_F(Poseidon2BabyBearTest, PermutePacked) { using F = math::BabyBear; using PackedF = math::PackedBabyBear; - using Params = Poseidon2Params; - using PackedParams = Poseidon2Params; + using Params = Poseidon2Params; + using PackedParams = + Poseidon2Params; auto packed_config = Poseidon2Config::Create( GetPoseidon2InternalShiftArray()); diff --git a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc index c6dc12b782..e44f954de7 100644 --- a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc +++ b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc @@ -1,5 +1,6 @@ #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" +#include #include #include "gtest/gtest.h" @@ -14,7 +15,8 @@ namespace tachyon::crypto { using F = math::BabyBear; -using Params = Poseidon2Params; +using Params = Poseidon2Params; using Poseidon2 = Poseidon2Sponge>, Params>; From 3e907d44e29030137be9d94e7ab47768274aa0ee Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 15:56:09 +0900 Subject: [PATCH 06/25] perf(crypto): remove runtime check using `use_plonky3_internal_matrix` --- .../hashes/sponge/poseidon2/BUILD.bazel | 1 + .../hashes/sponge/poseidon2/poseidon2.h | 20 +++++++++---------- .../sponge/poseidon2/poseidon2_config.h | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel index a6ce392ee0..b564b3bf4d 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel @@ -9,6 +9,7 @@ tachyon_cc_library( ":poseidon2_config", ":poseidon2_horizen_internal_matrix", ":poseidon2_plonky3_internal_matrix", + ":poseidon2_vendor", "//tachyon/base/buffer:copyable", "//tachyon/crypto/hashes/sponge:sponge_state", "//tachyon/crypto/hashes/sponge/poseidon:poseidon_sponge_base", diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h index 795756377b..05e993cfd8 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h @@ -14,6 +14,7 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_internal_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_internal_matrix.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h" #include "tachyon/crypto/hashes/sponge/sponge_state.h" #include "tachyon/math/finite_fields/finite_field_traits.h" @@ -81,17 +82,16 @@ struct Poseidon2Sponge final void ApplyMixPartial(SpongeState& state) const { using PrimeField = math::MaybeUnpack; - if constexpr (PrimeField::Config::kModulusBits <= 32) { - if (config.use_plonky3_internal_matrix) { - if constexpr (math::FiniteFieldTraits::kIsPackedPrimeField) { - Poseidon2Plonky3InternalMatrix::Apply( - state.elements, config.internal_diagonal_minus_one); - } else { - Poseidon2Plonky3InternalMatrix::Apply(state.elements, - config.internal_shifts); - } - return; + if constexpr (PrimeField::Config::kModulusBits <= 32 && + Params::kInternalMatrixVendor == Poseidon2Vendor::kPlonky3) { + if constexpr (math::FiniteFieldTraits::kIsPackedPrimeField) { + Poseidon2Plonky3InternalMatrix::Apply( + state.elements, config.internal_diagonal_minus_one); + } else { + Poseidon2Plonky3InternalMatrix::Apply(state.elements, + config.internal_shifts); } + return; } Poseidon2HorizenInternalMatrix::Apply( state.elements, config.internal_diagonal_minus_one); diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h index 85bb2d0362..ba6d95e9eb 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h @@ -52,7 +52,6 @@ struct Poseidon2Config : public PoseidonConfigBase { math::Vector internal_diagonal_minus_one; math::Vector internal_shifts; - bool use_plonky3_internal_matrix = false; Poseidon2Config() = default; Poseidon2Config(const PoseidonConfigBase& base, @@ -109,7 +108,6 @@ struct Poseidon2Config : public PoseidonConfigBase { const std::array& internal_shifts, math::Matrix&& ark) { Poseidon2Config ret = config_entry.ToPoseidon2Config(); - ret.use_plonky3_internal_matrix = true; if constexpr (math::FiniteFieldTraits::kIsPackedPrimeField) { ret.internal_diagonal_minus_one = math::Vector(Params::kWidth); ret.internal_diagonal_minus_one[0] = F(PrimeField::Config::kModulus - 2); From 00c17812c5232d54f3484b8e374a11dfea64f8c8 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 16:21:25 +0900 Subject: [PATCH 07/25] refac: remove `ExternalMatrix` from `Poseidon2Sponge` --- benchmark/fri/BUILD.bazel | 2 - benchmark/fri/fri_benchmark.cc | 11 +---- benchmark/poseidon2/BUILD.bazel | 1 - .../poseidon2/poseidon2_benchmark_runner.h | 7 +--- tachyon/c/zk/air/sp1/BUILD.bazel | 2 - .../baby_bear_poseidon2_duplex_challenger.cc | 4 +- ..._poseidon2_duplex_challenger_type_traits.h | 13 +----- ...ar_poseidon2_duplex_challenger_unittest.cc | 4 +- ..._bear_poseidon2_two_adic_fri_type_traits.h | 11 +---- tachyon/crypto/challenger/BUILD.bazel | 1 - .../challenger/duplex_challenger_unittest.cc | 5 +-- .../challenger/hash_challenger_unittest.cc | 5 +-- .../multi_field32_challenger_unittest.cc | 5 +-- tachyon/crypto/commitments/fri/BUILD.bazel | 1 - .../commitments/fri/two_adic_fri_unittest.cc | 9 +---- .../merkle_tree/field_merkle_tree/BUILD.bazel | 1 - ...tension_field_merkle_tree_mmcs_unittest.cc | 9 +---- .../field_merkle_tree_mmcs_unittest.cc | 9 +---- .../field_merkle_tree_unittest.cc | 9 +---- tachyon/crypto/hashes/sponge/BUILD.bazel | 1 - .../sponge/padding_free_sponge_unittest.cc | 4 +- .../hashes/sponge/poseidon2/BUILD.bazel | 5 +-- .../hashes/sponge/poseidon2/poseidon2.h | 40 ++++++++++--------- .../sponge/poseidon2/poseidon2_unittest.cc | 26 +++--------- .../sponge/truncated_permutation_unittest.cc | 5 +-- 25 files changed, 52 insertions(+), 138 deletions(-) diff --git a/benchmark/fri/BUILD.bazel b/benchmark/fri/BUILD.bazel index 5db1d1ca7b..d6e81e9a82 100644 --- a/benchmark/fri/BUILD.bazel +++ b/benchmark/fri/BUILD.bazel @@ -26,7 +26,6 @@ tachyon_cc_library( "//tachyon/base/containers:container_util", "//tachyon/base/time", "//tachyon/c/math/matrix", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_horizen_external_matrix", "//tachyon/math/matrix:matrix_types", ], ) @@ -51,7 +50,6 @@ tachyon_cc_binary( "//tachyon/crypto/hashes/sponge:padding_free_sponge", "//tachyon/crypto/hashes/sponge:truncated_permutation", "//tachyon/crypto/hashes/sponge/poseidon2", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_horizen_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", "//tachyon/math/finite_fields:packed_field_traits_forward", diff --git a/benchmark/fri/fri_benchmark.cc b/benchmark/fri/fri_benchmark.cc index 0e91965c4a..ed91410aba 100644 --- a/benchmark/fri/fri_benchmark.cc +++ b/benchmark/fri/fri_benchmark.cc @@ -21,7 +21,6 @@ #include "tachyon/crypto/hashes/sponge/padding_free_sponge.h" #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_external_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" #include "tachyon/math/finite_fields/baby_bear/baby_bear4.h" @@ -60,14 +59,8 @@ void Run(const FRIConfig& config) { crypto::Poseidon2Params; - using Poseidon2 = - crypto::Poseidon2Sponge>, - Params>; - using PackedPoseidon2 = crypto::Poseidon2Sponge< - crypto::Poseidon2ExternalMatrix< - crypto::Poseidon2HorizenExternalMatrix>, - PackedParams>; + using Poseidon2 = crypto::Poseidon2Sponge; + using PackedPoseidon2 = crypto::Poseidon2Sponge; using MyHasher = crypto::PaddingFreeSponge; using MyPackedHasher = crypto::PaddingFreeSponge; diff --git a/benchmark/poseidon2/BUILD.bazel b/benchmark/poseidon2/BUILD.bazel index 5461b6825d..a0f5c83e43 100644 --- a/benchmark/poseidon2/BUILD.bazel +++ b/benchmark/poseidon2/BUILD.bazel @@ -29,7 +29,6 @@ tachyon_cc_library( "//tachyon/base/time", "//tachyon/c/base:type_traits_forward", "//tachyon/crypto/hashes/sponge/poseidon2", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_horizen_external_matrix", ], ) diff --git a/benchmark/poseidon2/poseidon2_benchmark_runner.h b/benchmark/poseidon2/poseidon2_benchmark_runner.h index 3ac9027c17..cf4ebe1a03 100644 --- a/benchmark/poseidon2/poseidon2_benchmark_runner.h +++ b/benchmark/poseidon2/poseidon2_benchmark_runner.h @@ -16,7 +16,6 @@ #include "tachyon/base/time/time.h" #include "tachyon/c/base/type_traits_forward.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_external_matrix.h" namespace tachyon::benchmark { @@ -36,11 +35,7 @@ class Poseidon2BenchmarkRunner { reporter_.AddVendor(Vendor::Tachyon()); Field ret = Field::Zero(); for (size_t i = 0; i < config_.repeating_num(); ++i) { - crypto::Poseidon2Sponge< - crypto::Poseidon2ExternalMatrix< - crypto::Poseidon2HorizenExternalMatrix>, - Params> - sponge(config); + crypto::Poseidon2Sponge sponge(config); crypto::SpongeState state; base::TimeTicks start = base::TimeTicks::Now(); for (size_t j = 0; j < 10000; ++j) { diff --git a/tachyon/c/zk/air/sp1/BUILD.bazel b/tachyon/c/zk/air/sp1/BUILD.bazel index dee7240a99..79e70318da 100644 --- a/tachyon/c/zk/air/sp1/BUILD.bazel +++ b/tachyon/c/zk/air/sp1/BUILD.bazel @@ -72,7 +72,6 @@ tachyon_cc_library( "//tachyon/crypto/challenger:duplex_challenger", "//tachyon/crypto/hashes/sponge/poseidon2", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", ], ) @@ -223,7 +222,6 @@ tachyon_cc_library( "//tachyon/crypto/hashes/sponge:truncated_permutation", "//tachyon/crypto/hashes/sponge/poseidon2", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", ], ) diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc index 291979a33e..7ac9098067 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc @@ -19,9 +19,7 @@ using Params = crypto::Poseidon2Vendor::kPlonky3, F, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_ALPHA>; -using Poseidon2 = crypto::Poseidon2Sponge< - crypto::Poseidon2ExternalMatrix>, - Params>; +using Poseidon2 = crypto::Poseidon2Sponge; tachyon_sp1_baby_bear_poseidon2_duplex_challenger* tachyon_sp1_baby_bear_poseidon2_duplex_challenger_create() { diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h index fa5bf15244..b3f4347b19 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h @@ -7,7 +7,6 @@ #include "tachyon/crypto/challenger/duplex_challenger.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/math/finite_fields/baby_bear/baby_bear.h" namespace tachyon::c::base { @@ -24,11 +23,7 @@ using Params = tachyon::crypto::Poseidon2Params< template <> struct TypeTraits>, - Params>, + tachyon::crypto::Poseidon2Sponge, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE>> { using CType = tachyon_sp1_baby_bear_poseidon2_duplex_challenger; }; @@ -36,11 +31,7 @@ struct TypeTraits struct TypeTraits { using NativeType = tachyon::crypto::DuplexChallenger< - tachyon::crypto::Poseidon2Sponge< - tachyon::crypto::Poseidon2ExternalMatrix< - tachyon::crypto::Poseidon2Plonky3ExternalMatrix< - tachyon::math::BabyBear>>, - Params>, + tachyon::crypto::Poseidon2Sponge, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE>; }; diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc index bb67a0b5a5..0e46ab6caa 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_unittest.cc @@ -15,9 +15,7 @@ using Params = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; +using Poseidon2 = Poseidon2Sponge; class DuplexChallengerTest : public math::FiniteFieldTest { public: diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h index 7645f859d6..d597004d8e 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h @@ -14,7 +14,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" namespace tachyon::c { @@ -32,15 +31,9 @@ using PackedParams = tachyon::crypto::Poseidon2Params< TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH - 1, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_ALPHA>; -using Poseidon2 = tachyon::crypto::Poseidon2Sponge< - tachyon::crypto::Poseidon2ExternalMatrix< - tachyon::crypto::Poseidon2Plonky3ExternalMatrix>, - Params>; +using Poseidon2 = tachyon::crypto::Poseidon2Sponge; -using PackedPoseidon2 = tachyon::crypto::Poseidon2Sponge< - tachyon::crypto::Poseidon2ExternalMatrix< - tachyon::crypto::Poseidon2Plonky3ExternalMatrix>, - PackedParams>; +using PackedPoseidon2 = tachyon::crypto::Poseidon2Sponge; using Hasher = tachyon::crypto::PaddingFreeSponge< Poseidon2, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE, diff --git a/tachyon/crypto/challenger/BUILD.bazel b/tachyon/crypto/challenger/BUILD.bazel index 4ce095ff46..7485123957 100644 --- a/tachyon/crypto/challenger/BUILD.bazel +++ b/tachyon/crypto/challenger/BUILD.bazel @@ -75,7 +75,6 @@ tachyon_cc_unittest( "//tachyon/crypto/hashes/sponge:padding_free_sponge", "//tachyon/crypto/hashes/sponge/poseidon2", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_bn254", "//tachyon/math/finite_fields/test:finite_field_test", diff --git a/tachyon/crypto/challenger/duplex_challenger_unittest.cc b/tachyon/crypto/challenger/duplex_challenger_unittest.cc index 68635af695..5577b908f8 100644 --- a/tachyon/crypto/challenger/duplex_challenger_unittest.cc +++ b/tachyon/crypto/challenger/duplex_challenger_unittest.cc @@ -13,7 +13,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" namespace tachyon::crypto { @@ -21,9 +20,7 @@ namespace tachyon::crypto { using F = math::BabyBear; using Params = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; +using Poseidon2 = Poseidon2Sponge; namespace { diff --git a/tachyon/crypto/challenger/hash_challenger_unittest.cc b/tachyon/crypto/challenger/hash_challenger_unittest.cc index faeb21d856..a6f2c18679 100644 --- a/tachyon/crypto/challenger/hash_challenger_unittest.cc +++ b/tachyon/crypto/challenger/hash_challenger_unittest.cc @@ -15,7 +15,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" namespace tachyon::crypto { @@ -23,9 +22,7 @@ namespace tachyon::crypto { using F = math::BabyBear; using Params = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; +using Poseidon2 = Poseidon2Sponge; using MyHasher = PaddingFreeSponge; namespace { diff --git a/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc b/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc index a9d1c7bb56..6055b1c495 100644 --- a/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc +++ b/tachyon/crypto/challenger/multi_field32_challenger_unittest.cc @@ -14,16 +14,13 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_bn254.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" namespace tachyon::crypto { using Params = Poseidon2Params; -using Poseidon2 = Poseidon2Sponge< - Poseidon2ExternalMatrix>, - Params>; +using Poseidon2 = Poseidon2Sponge; namespace { diff --git a/tachyon/crypto/commitments/fri/BUILD.bazel b/tachyon/crypto/commitments/fri/BUILD.bazel index 7bec6231ee..2b21185e22 100644 --- a/tachyon/crypto/commitments/fri/BUILD.bazel +++ b/tachyon/crypto/commitments/fri/BUILD.bazel @@ -138,7 +138,6 @@ tachyon_cc_unittest( "//tachyon/crypto/hashes/sponge:truncated_permutation", "//tachyon/crypto/hashes/sponge/poseidon2", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", "//tachyon/crypto/transcripts:simple_transcript", "//tachyon/math/finite_fields/baby_bear:baby_bear4", diff --git a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc index 8eeedec587..24e4717b96 100644 --- a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc +++ b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc @@ -13,7 +13,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" #include "tachyon/math/finite_fields/baby_bear/baby_bear4.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -35,12 +34,8 @@ using Params = Poseidon2Params; using Domain = TwoAdicMultiplicativeCoset; -using Poseidon2 = - Poseidon2Sponge>, - Params>; -using PackedPoseidon2 = Poseidon2Sponge< - Poseidon2ExternalMatrix>, - PackedParams>; +using Poseidon2 = Poseidon2Sponge; +using PackedPoseidon2 = Poseidon2Sponge; using MyHasher = PaddingFreeSponge; using MyPackedHasher = PaddingFreeSponge; using MyCompressor = TruncatedPermutation; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel index aad10a1cc2..dbf59e5e69 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel @@ -54,7 +54,6 @@ tachyon_cc_unittest( "//tachyon/crypto/hashes/sponge:truncated_permutation", "//tachyon/crypto/hashes/sponge/poseidon2", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", "//tachyon/math/finite_fields/baby_bear:baby_bear4", "//tachyon/math/finite_fields/test:finite_field_test", diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc index d506be3882..89237a1587 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc @@ -14,7 +14,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" #include "tachyon/math/finite_fields/baby_bear/baby_bear4.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -32,12 +31,8 @@ using Params = Poseidon2Params; using PackedParams = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; -using PackedPoseidon2 = Poseidon2Sponge< - Poseidon2ExternalMatrix>, - PackedParams>; +using Poseidon2 = Poseidon2Sponge; +using PackedPoseidon2 = Poseidon2Sponge; using MyHasher = PaddingFreeSponge; using MyPackedHasher = PaddingFreeSponge; using MyCompressor = TruncatedPermutation; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc index 1d74f63c4b..312c76537d 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc @@ -14,7 +14,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -30,12 +29,8 @@ using Params = Poseidon2Params; using PackedParams = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; -using PackedPoseidon2 = Poseidon2Sponge< - Poseidon2ExternalMatrix>, - PackedParams>; +using Poseidon2 = Poseidon2Sponge; +using PackedPoseidon2 = Poseidon2Sponge; using MyHasher = PaddingFreeSponge; using MyPackedHasher = PaddingFreeSponge; using MyCompressor = TruncatedPermutation; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index 13b53925d7..816fe03f75 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -14,7 +14,6 @@ #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/crypto/hashes/sponge/truncated_permutation.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -30,12 +29,8 @@ using Params = Poseidon2Params; using PackedParams = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; -using PackedPoseidon2 = Poseidon2Sponge< - Poseidon2ExternalMatrix>, - PackedParams>; +using Poseidon2 = Poseidon2Sponge; +using PackedPoseidon2 = Poseidon2Sponge; using MyHasher = PaddingFreeSponge; using MyPackedHasher = PaddingFreeSponge; using MyCompressor = TruncatedPermutation; diff --git a/tachyon/crypto/hashes/sponge/BUILD.bazel b/tachyon/crypto/hashes/sponge/BUILD.bazel index 4e13f1b462..c729fbe894 100644 --- a/tachyon/crypto/hashes/sponge/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/BUILD.bazel @@ -62,7 +62,6 @@ tachyon_cc_unittest( "//tachyon/base/buffer:vector_buffer", "//tachyon/base/containers:container_util", "//tachyon/crypto/hashes/sponge/poseidon2", - "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_horizen_external_matrix", "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", "//tachyon/math/finite_fields/test:finite_field_test", diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc index 1236151ab9..6c7ebfa377 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc @@ -7,7 +7,6 @@ #include "tachyon/base/containers/container_util.h" #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_external_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -24,8 +23,7 @@ class PaddingFreeSpongeTest : public math::FiniteFieldTest {}; TEST_F(PaddingFreeSpongeTest, Hash) { using Params = Poseidon2Params; - using Poseidon2 = Poseidon2Sponge< - Poseidon2ExternalMatrix>, Params>; + using Poseidon2 = Poseidon2Sponge; constexpr size_t kRate = 8; constexpr size_t kOut = 8; diff --git a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel index b564b3bf4d..258f11dff1 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel @@ -7,7 +7,9 @@ tachyon_cc_library( hdrs = ["poseidon2.h"], deps = [ ":poseidon2_config", + ":poseidon2_horizen_external_matrix", ":poseidon2_horizen_internal_matrix", + ":poseidon2_plonky3_external_matrix", ":poseidon2_plonky3_internal_matrix", ":poseidon2_vendor", "//tachyon/base/buffer:copyable", @@ -114,10 +116,7 @@ tachyon_cc_unittest( ], deps = [ ":poseidon2", - ":poseidon2_horizen_external_matrix", ":poseidon2_params", - ":poseidon2_plonky3_external_matrix", - ":poseidon2_plonky3_internal_matrix", "//tachyon/base/buffer:vector_buffer", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_goldilocks", diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h index 05e993cfd8..e763837773 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h @@ -12,7 +12,9 @@ #include "tachyon/base/logging.h" #include "tachyon/crypto/hashes/sponge/poseidon/poseidon_sponge_base.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_external_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_internal_matrix.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_internal_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h" #include "tachyon/crypto/hashes/sponge/sponge_state.h" @@ -28,9 +30,9 @@ namespace crypto { // 2. Apply S-Box (xᵅ) to |state|. // 3. Apply external and internal matrices to |state|. // Squeeze: Squeeze elements out of the sponge. -template +template struct Poseidon2Sponge final - : public PoseidonSpongeBase> { + : public PoseidonSpongeBase> { using Params = _Params; using F = typename Params::Field; @@ -76,7 +78,13 @@ struct Poseidon2Sponge final private: void ApplyMixFull(SpongeState& state) const { - ExternalMatrix::template Apply(state.elements); + if constexpr (Params::kExternalMatrixVendor == Poseidon2Vendor::kHorizen) { + Poseidon2ExternalMatrix>:: + template Apply(state.elements); + } else { + Poseidon2ExternalMatrix>:: + template Apply(state.elements); + } } void ApplyMixPartial(SpongeState& state) const { @@ -98,8 +106,8 @@ struct Poseidon2Sponge final } }; -template -struct CryptographicSpongeTraits> { +template +struct CryptographicSpongeTraits> { using Params = _Params; using F = typename Params::Field; }; @@ -107,32 +115,28 @@ struct CryptographicSpongeTraits> { } // namespace crypto namespace base { -template -class Copyable> { +template +class Copyable> { public: - using F = typename ExternalMatrix::Field; + using F = typename Params::Field; - static bool WriteTo( - const crypto::Poseidon2Sponge& poseidon, - Buffer* buffer) { + static bool WriteTo(const crypto::Poseidon2Sponge& poseidon, + Buffer* buffer) { return buffer->WriteMany(poseidon.config); } - static bool ReadFrom( - const ReadOnlyBuffer& buffer, - crypto::Poseidon2Sponge* poseidon) { + static bool ReadFrom(const ReadOnlyBuffer& buffer, + crypto::Poseidon2Sponge* poseidon) { crypto::Poseidon2Config config; if (!buffer.ReadMany(&config)) { return false; } - *poseidon = - crypto::Poseidon2Sponge(std::move(config)); + *poseidon = crypto::Poseidon2Sponge(std::move(config)); return true; } - static size_t EstimateSize( - const crypto::Poseidon2Sponge& poseidon) { + static size_t EstimateSize(const crypto::Poseidon2Sponge& poseidon) { return base::EstimateSize(poseidon.config); } }; diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc index 8fb19f8377..a2fdf96586 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc @@ -13,7 +13,6 @@ #include "tachyon/base/buffer/vector_buffer.h" #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_goldilocks.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_external_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -33,9 +32,7 @@ TEST_F(Poseidon2GoldilocksTest, Permute) { auto config = Poseidon2Config::Create( crypto::GetPoseidon2InternalDiagonalArray()); - Poseidon2Sponge>, - Params> - sponge(std::move(config)); + Poseidon2Sponge sponge(std::move(config)); SpongeState state; for (size_t i = 0; i < 8; ++i) { state.elements[i] = F(i); @@ -57,9 +54,7 @@ TEST_F(Poseidon2GoldilocksTest, Copyable) { auto config = Poseidon2Config::Create( crypto::GetPoseidon2InternalDiagonalArray()); - Poseidon2Sponge>, - Params> - expected(std::move(config)); + Poseidon2Sponge expected(std::move(config)); base::Uint8VectorBuffer write_buf; ASSERT_TRUE(write_buf.Grow(base::EstimateSize(expected))); @@ -68,9 +63,7 @@ TEST_F(Poseidon2GoldilocksTest, Copyable) { write_buf.set_buffer_offset(0); - Poseidon2Sponge>, - Params> - value; + Poseidon2Sponge value; ASSERT_TRUE(write_buf.Read(&value)); EXPECT_EQ(value, expected); @@ -90,9 +83,7 @@ TEST_F(Poseidon2BabyBearTest, Permute) { auto config = Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); - Poseidon2Sponge>, - Params> - sponge(std::move(config)); + Poseidon2Sponge sponge(std::move(config)); SpongeState state; for (size_t i = 0; i < 16; ++i) { state.elements[i] = F(i); @@ -118,10 +109,7 @@ TEST_F(Poseidon2BabyBearTest, PermutePacked) { auto packed_config = Poseidon2Config::Create( GetPoseidon2InternalShiftArray()); - Poseidon2Sponge< - Poseidon2ExternalMatrix>, - PackedParams> - packed_sponge(std::move(packed_config)); + Poseidon2Sponge packed_sponge(std::move(packed_config)); SpongeState packed_state; for (size_t i = 0; i < 16; ++i) { packed_state.elements[i] = PackedF(i); @@ -130,9 +118,7 @@ TEST_F(Poseidon2BabyBearTest, PermutePacked) { auto config = Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); - Poseidon2Sponge>, - Params> - sponge(std::move(config)); + Poseidon2Sponge sponge(std::move(config)); SpongeState state; for (size_t i = 0; i < 16; ++i) { state.elements[i] = F(i); diff --git a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc index e44f954de7..a6871fbc95 100644 --- a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc +++ b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc @@ -8,7 +8,6 @@ #include "tachyon/base/containers/container_util.h" #include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" -#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_horizen_external_matrix.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -17,9 +16,7 @@ namespace tachyon::crypto { using F = math::BabyBear; using Params = Poseidon2Params; -using Poseidon2 = - Poseidon2Sponge>, - Params>; +using Poseidon2 = Poseidon2Sponge; namespace { From 48191d21271f941a0fa8d61eb3da28296ba1e009 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 17:01:26 +0900 Subject: [PATCH 08/25] refac: add `Poseidon2Config::CreateDefault()` --- benchmark/fri/fri_benchmark.cc | 6 ++---- benchmark/poseidon2/poseidon2_benchmark.cc | 9 +++----- .../baby_bear_poseidon2_duplex_challenger.cc | 3 +-- .../sp1/baby_bear_poseidon2_two_adic_fri.cc | 6 ++---- .../challenger/duplex_challenger_unittest.cc | 3 +-- .../challenger/hash_challenger_unittest.cc | 3 +-- .../commitments/fri/two_adic_fri_unittest.cc | 6 ++---- ...tension_field_merkle_tree_mmcs_unittest.cc | 6 ++---- .../field_merkle_tree_mmcs_unittest.cc | 6 ++---- .../field_merkle_tree_unittest.cc | 6 ++---- .../sponge/padding_free_sponge_unittest.cc | 3 +-- .../sponge/poseidon2/poseidon2_config.h | 21 +++++++++++++++++++ .../sponge/poseidon2/poseidon2_unittest.cc | 15 +++++-------- .../sponge/truncated_permutation_unittest.cc | 3 +-- 14 files changed, 46 insertions(+), 50 deletions(-) diff --git a/benchmark/fri/fri_benchmark.cc b/benchmark/fri/fri_benchmark.cc index ed91410aba..52a0977ebe 100644 --- a/benchmark/fri/fri_benchmark.cc +++ b/benchmark/fri/fri_benchmark.cc @@ -80,14 +80,12 @@ void Run(const FRIConfig& config) { ExtF::Init(); ExtPackedF::Init(); - auto poseidon2_config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalShiftArray()); + auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(poseidon2_config)); MyHasher hasher(sponge); MyCompressor compressor(sponge); - auto packed_config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalShiftArray()); + auto packed_config = crypto::Poseidon2Config::CreateDefault(); PackedPoseidon2 packed_sponge(std::move(packed_config)); MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); diff --git a/benchmark/poseidon2/poseidon2_benchmark.cc b/benchmark/poseidon2/poseidon2_benchmark.cc index 6172cf6953..3216a2e576 100644 --- a/benchmark/poseidon2/poseidon2_benchmark.cc +++ b/benchmark/poseidon2/poseidon2_benchmark.cc @@ -43,23 +43,20 @@ void Run(SimpleReporter& reporter, const Poseidon2Config& config, Fn horizen_fn, using Params = Poseidon2Params; - auto poseidon2_config = crypto::Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); result = runner.Run(poseidon2_config); } else { using Params = Poseidon2Params; - auto poseidon2_config = crypto::Poseidon2Config::Create( - GetPoseidon2InternalDiagonalArray()); + auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); result = runner.Run(poseidon2_config); } } else { using Params = Poseidon2Params; - auto poseidon2_config = crypto::Poseidon2Config::Create( - GetPoseidon2InternalDiagonalArray()); + auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); result = runner.Run(poseidon2_config); } for (const Vendor vendor : config.vendors()) { diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc index 7ac9098067..f3cad9f7e6 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc @@ -32,8 +32,7 @@ tachyon_sp1_baby_bear_poseidon2_duplex_challenger_create() { } } - auto config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalShiftArray(), std::move(ark)); + auto config = crypto::Poseidon2Config::CreateDefault(std::move(ark)); Poseidon2 sponge(std::move(config)); return c::base::c_cast( new crypto::DuplexChallenger::Create( - crypto::GetPoseidon2InternalShiftArray(), std::move(ark)); + auto config = crypto::Poseidon2Config::CreateDefault(std::move(ark)); Poseidon2 sponge(std::move(config)); Hasher hasher(sponge); Compressor compressor(std::move(sponge)); - auto packed_config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalShiftArray(), + auto packed_config = crypto::Poseidon2Config::CreateDefault( std::move(packed_ark)); PackedPoseidon2 packed_sponge(std::move(packed_config)); PackedHasher packed_hasher(packed_sponge); diff --git a/tachyon/crypto/challenger/duplex_challenger_unittest.cc b/tachyon/crypto/challenger/duplex_challenger_unittest.cc index 5577b908f8..b338d41e69 100644 --- a/tachyon/crypto/challenger/duplex_challenger_unittest.cc +++ b/tachyon/crypto/challenger/duplex_challenger_unittest.cc @@ -29,8 +29,7 @@ class DuplexChallengerTest : public math::FiniteFieldTest { constexpr static size_t kRate = 4; void SetUp() override { - auto config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); challenger_ = DuplexChallenger(std::move(sponge)); } diff --git a/tachyon/crypto/challenger/hash_challenger_unittest.cc b/tachyon/crypto/challenger/hash_challenger_unittest.cc index a6f2c18679..ba0ff23bab 100644 --- a/tachyon/crypto/challenger/hash_challenger_unittest.cc +++ b/tachyon/crypto/challenger/hash_challenger_unittest.cc @@ -30,8 +30,7 @@ namespace { class HashChallengerTest : public math::FiniteFieldTest { public: void SetUp() override { - auto config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); MyHasher hasher(std::move(sponge)); diff --git a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc index 24e4717b96..69279b58c9 100644 --- a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc +++ b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc @@ -59,14 +59,12 @@ class TwoAdicFRITest : public testing::Test { } void SetUp() override { - auto config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); MyHasher hasher(sponge); MyCompressor compressor(sponge); - auto packed_config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto packed_config = Poseidon2Config::CreateDefault(); PackedPoseidon2 packed_sponge(std::move(packed_config)); MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc index 89237a1587..ff7d27f231 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc @@ -55,14 +55,12 @@ class ExtensionFieldMerkleTreeMMCSTest : public math::FiniteFieldTest { } void SetUp() override { - auto config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); MyHasher hasher(sponge); MyCompressor compressor(std::move(sponge)); - auto packed_config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto packed_config = Poseidon2Config::CreateDefault(); PackedPoseidon2 packed_sponge(std::move(packed_config)); MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc index 312c76537d..e4bd264637 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc @@ -44,14 +44,12 @@ namespace { class FieldMerkleTreeMMCSTest : public math::FiniteFieldTest { public: void SetUp() override { - auto config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); MyHasher hasher(sponge); MyCompressor compressor(std::move(sponge)); - auto packed_config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto packed_config = Poseidon2Config::CreateDefault(); PackedPoseidon2 packed_sponge(std::move(packed_config)); MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index 816fe03f75..3c3e33cfea 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -42,14 +42,12 @@ namespace { class FieldMerkleTreeTest : public math::FiniteFieldTest { public: void SetUp() override { - auto config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); hasher_ = MyHasher(sponge); compressor_ = MyCompressor(std::move(sponge)); - auto packed_config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto packed_config = Poseidon2Config::CreateDefault(); PackedPoseidon2 packed_sponge(std::move(packed_config)); packed_hasher_ = MyPackedHasher(packed_sponge); packed_compressor_ = MyPackedCompressor(std::move(packed_sponge)); diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc index 6c7ebfa377..4f58e5318f 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc @@ -27,8 +27,7 @@ TEST_F(PaddingFreeSpongeTest, Hash) { constexpr size_t kRate = 8; constexpr size_t kOut = 8; - auto config = - Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); PaddingFreeSponge hasher(std::move(sponge)); std::vector inputs = diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h index ba6d95e9eb..f099f00d99 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h @@ -16,6 +16,7 @@ #include "tachyon/base/ranges/algorithm.h" #include "tachyon/crypto/hashes/sponge/poseidon/poseidon_config_base.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config_entry.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" #include "tachyon/math/finite_fields/finite_field_traits.h" #include "tachyon/math/matrix/matrix_utils.h" @@ -63,6 +64,26 @@ struct Poseidon2Config : public PoseidonConfigBase { : PoseidonConfigBase(std::move(base)), internal_diagonal_minus_one(std::move(internal_diagonal_minus_one)) {} + constexpr static Poseidon2Config CreateDefault() { + // TODO(chokobole): Only |BabyBear| has both shift and diagonal arrays. + // We assume the Plonky3 team developed the concept of the shift array, + // and will use it in most cases, so we set it as the default. + // Other small prime fields have only a shift array, while larger prime + // fields have only a diagonal array. For more details, refer to + // tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_xxx.h. + if constexpr (Params::kInternalMatrixVendor == Poseidon2Vendor::kPlonky3 && + PrimeField::Config::kModulusBits <= 32) { + return Create(GetPoseidon2InternalShiftArray()); + } else { + return Create(GetPoseidon2InternalDiagonalArray()); + } + } + + constexpr static Poseidon2Config CreateDefault(math::Matrix&& ark) { + static_assert(Params::kInternalMatrixVendor == Poseidon2Vendor::kPlonky3); + return Create(GetPoseidon2InternalShiftArray(), std::move(ark)); + } + constexpr static Poseidon2Config Create( const std::array& internal_diagonal_minus_one) { diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc index a2fdf96586..6c916039ce 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc @@ -30,8 +30,7 @@ TEST_F(Poseidon2GoldilocksTest, Permute) { using Params = Poseidon2Params; - auto config = Poseidon2Config::Create( - crypto::GetPoseidon2InternalDiagonalArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2Sponge sponge(std::move(config)); SpongeState state; for (size_t i = 0; i < 8; ++i) { @@ -52,8 +51,7 @@ TEST_F(Poseidon2GoldilocksTest, Copyable) { using Params = Poseidon2Params; - auto config = Poseidon2Config::Create( - crypto::GetPoseidon2InternalDiagonalArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2Sponge expected(std::move(config)); base::Uint8VectorBuffer write_buf; @@ -81,8 +79,7 @@ TEST_F(Poseidon2BabyBearTest, Permute) { using Params = Poseidon2Params; - auto config = - Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2Sponge sponge(std::move(config)); SpongeState state; for (size_t i = 0; i < 16; ++i) { @@ -107,8 +104,7 @@ TEST_F(Poseidon2BabyBearTest, PermutePacked) { Poseidon2Params; - auto packed_config = Poseidon2Config::Create( - GetPoseidon2InternalShiftArray()); + auto packed_config = Poseidon2Config::CreateDefault(); Poseidon2Sponge packed_sponge(std::move(packed_config)); SpongeState packed_state; for (size_t i = 0; i < 16; ++i) { @@ -116,8 +112,7 @@ TEST_F(Poseidon2BabyBearTest, PermutePacked) { } packed_sponge.Permute(packed_state); - auto config = - Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2Sponge sponge(std::move(config)); SpongeState state; for (size_t i = 0; i < 16; ++i) { diff --git a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc index a6871fbc95..09f36697eb 100644 --- a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc +++ b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc @@ -28,8 +28,7 @@ TEST_F(TruncatedPermutationTest, Hash) { constexpr size_t kChunk = 8; constexpr size_t kN = 2; - auto config = - Poseidon2Config::Create(GetPoseidon2InternalShiftArray()); + auto config = Poseidon2Config::CreateDefault(); Poseidon2 sponge(std::move(config)); TruncatedPermutation compressor(std::move(sponge)); std::vector> inputs = base::CreateVector(kN, [](uint32_t i) { From 8b15f8c2d7346d4f7efb8b554c9ae261f930b5e5 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 17:07:49 +0900 Subject: [PATCH 09/25] refac: make `Poseidon2Sponge` use `Poseidon2Config::CreateDefault()` --- benchmark/fri/fri_benchmark.cc | 6 ++---- benchmark/poseidon2/poseidon2_benchmark.cc | 9 +++------ benchmark/poseidon2/poseidon2_benchmark_runner.h | 4 ++-- .../air/sp1/baby_bear_poseidon2_hintable_unittest.cc | 5 ++--- .../crypto/challenger/duplex_challenger_unittest.cc | 3 +-- .../crypto/challenger/hash_challenger_unittest.cc | 3 +-- .../crypto/commitments/fri/two_adic_fri_unittest.cc | 6 ++---- .../extension_field_merkle_tree_mmcs_unittest.cc | 6 ++---- .../field_merkle_tree_mmcs_unittest.cc | 6 ++---- .../field_merkle_tree/field_merkle_tree_unittest.cc | 6 ++---- .../hashes/sponge/padding_free_sponge_unittest.cc | 3 +-- tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h | 2 +- .../hashes/sponge/poseidon2/poseidon2_unittest.cc | 12 ++++-------- .../hashes/sponge/truncated_permutation_unittest.cc | 3 +-- 14 files changed, 26 insertions(+), 48 deletions(-) diff --git a/benchmark/fri/fri_benchmark.cc b/benchmark/fri/fri_benchmark.cc index 52a0977ebe..ee5ae7f651 100644 --- a/benchmark/fri/fri_benchmark.cc +++ b/benchmark/fri/fri_benchmark.cc @@ -80,13 +80,11 @@ void Run(const FRIConfig& config) { ExtF::Init(); ExtPackedF::Init(); - auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(poseidon2_config)); + Poseidon2 sponge; MyHasher hasher(sponge); MyCompressor compressor(sponge); - auto packed_config = crypto::Poseidon2Config::CreateDefault(); - PackedPoseidon2 packed_sponge(std::move(packed_config)); + PackedPoseidon2 packed_sponge; MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); MMCS mmcs(hasher, packed_hasher, compressor, packed_compressor); diff --git a/benchmark/poseidon2/poseidon2_benchmark.cc b/benchmark/poseidon2/poseidon2_benchmark.cc index 3216a2e576..9fafcdd363 100644 --- a/benchmark/poseidon2/poseidon2_benchmark.cc +++ b/benchmark/poseidon2/poseidon2_benchmark.cc @@ -43,21 +43,18 @@ void Run(SimpleReporter& reporter, const Poseidon2Config& config, Fn horizen_fn, using Params = Poseidon2Params; - auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); - result = runner.Run(poseidon2_config); + result = runner.template Run(); } else { using Params = Poseidon2Params; - auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); - result = runner.Run(poseidon2_config); + result = runner.template Run(); } } else { using Params = Poseidon2Params; - auto poseidon2_config = crypto::Poseidon2Config::CreateDefault(); - result = runner.Run(poseidon2_config); + result = runner.template Run(); } for (const Vendor vendor : config.vendors()) { Field result_vendor; diff --git a/benchmark/poseidon2/poseidon2_benchmark_runner.h b/benchmark/poseidon2/poseidon2_benchmark_runner.h index cf4ebe1a03..8f9337ceca 100644 --- a/benchmark/poseidon2/poseidon2_benchmark_runner.h +++ b/benchmark/poseidon2/poseidon2_benchmark_runner.h @@ -31,11 +31,11 @@ class Poseidon2BenchmarkRunner { : reporter_(reporter), config_(config) {} template - Field Run(const crypto::Poseidon2Config& config) { + Field Run() { reporter_.AddVendor(Vendor::Tachyon()); Field ret = Field::Zero(); for (size_t i = 0; i < config_.repeating_num(); ++i) { - crypto::Poseidon2Sponge sponge(config); + crypto::Poseidon2Sponge sponge; crypto::SpongeState state; base::TimeTicks start = base::TimeTicks::Now(); for (size_t j = 0; j < 10000; ++j) { diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc index 03655233a1..0a36212dc1 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc @@ -1,6 +1,7 @@ #include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable.h" #include +#include #include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" @@ -17,9 +18,7 @@ constexpr size_t kRate = 4; class HintableTest : public math::FiniteFieldTest {}; TEST_F(HintableTest, DuplexChallenger) { - auto config = crypto::Poseidon2Config::Create( - crypto::GetPoseidon2InternalShiftArray()); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; crypto::DuplexChallenger challenger(std::move(sponge)); for (size_t i = 0; i < Params::kWidth; ++i) { challenger.state_[i] = F(i); diff --git a/tachyon/crypto/challenger/duplex_challenger_unittest.cc b/tachyon/crypto/challenger/duplex_challenger_unittest.cc index b338d41e69..546fe50046 100644 --- a/tachyon/crypto/challenger/duplex_challenger_unittest.cc +++ b/tachyon/crypto/challenger/duplex_challenger_unittest.cc @@ -29,8 +29,7 @@ class DuplexChallengerTest : public math::FiniteFieldTest { constexpr static size_t kRate = 4; void SetUp() override { - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; challenger_ = DuplexChallenger(std::move(sponge)); } diff --git a/tachyon/crypto/challenger/hash_challenger_unittest.cc b/tachyon/crypto/challenger/hash_challenger_unittest.cc index ba0ff23bab..61cb79a5b1 100644 --- a/tachyon/crypto/challenger/hash_challenger_unittest.cc +++ b/tachyon/crypto/challenger/hash_challenger_unittest.cc @@ -30,8 +30,7 @@ namespace { class HashChallengerTest : public math::FiniteFieldTest { public: void SetUp() override { - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; MyHasher hasher(std::move(sponge)); std::vector initial_state = diff --git a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc index 69279b58c9..cb7e6eadf2 100644 --- a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc +++ b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc @@ -59,13 +59,11 @@ class TwoAdicFRITest : public testing::Test { } void SetUp() override { - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; MyHasher hasher(sponge); MyCompressor compressor(sponge); - auto packed_config = Poseidon2Config::CreateDefault(); - PackedPoseidon2 packed_sponge(std::move(packed_config)); + PackedPoseidon2 packed_sponge; MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); MMCS mmcs(hasher, packed_hasher, compressor, packed_compressor); diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc index ff7d27f231..bab190415b 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc @@ -55,13 +55,11 @@ class ExtensionFieldMerkleTreeMMCSTest : public math::FiniteFieldTest { } void SetUp() override { - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; MyHasher hasher(sponge); MyCompressor compressor(std::move(sponge)); - auto packed_config = Poseidon2Config::CreateDefault(); - PackedPoseidon2 packed_sponge(std::move(packed_config)); + PackedPoseidon2 packed_sponge; MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); ext_mmcs_.reset(new ExtMMCS( diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc index e4bd264637..16225cb651 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc @@ -44,13 +44,11 @@ namespace { class FieldMerkleTreeMMCSTest : public math::FiniteFieldTest { public: void SetUp() override { - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; MyHasher hasher(sponge); MyCompressor compressor(std::move(sponge)); - auto packed_config = Poseidon2Config::CreateDefault(); - PackedPoseidon2 packed_sponge(std::move(packed_config)); + PackedPoseidon2 packed_sponge; MyPackedHasher packed_hasher(packed_sponge); MyPackedCompressor packed_compressor(std::move(packed_sponge)); mmcs_ = MMCS(std::move(hasher), std::move(packed_hasher), diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index 3c3e33cfea..d3c5b18dde 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -42,13 +42,11 @@ namespace { class FieldMerkleTreeTest : public math::FiniteFieldTest { public: void SetUp() override { - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; hasher_ = MyHasher(sponge); compressor_ = MyCompressor(std::move(sponge)); - auto packed_config = Poseidon2Config::CreateDefault(); - PackedPoseidon2 packed_sponge(std::move(packed_config)); + PackedPoseidon2 packed_sponge; packed_hasher_ = MyPackedHasher(packed_sponge); packed_compressor_ = MyPackedCompressor(std::move(packed_sponge)); } diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc index 4f58e5318f..5ea687d000 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc @@ -27,8 +27,7 @@ TEST_F(PaddingFreeSpongeTest, Hash) { constexpr size_t kRate = 8; constexpr size_t kOut = 8; - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; PaddingFreeSponge hasher(std::move(sponge)); std::vector inputs = base::CreateVector(100, [](uint32_t i) { return F(i); }); diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h index e763837773..9dd8f2b040 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h @@ -39,7 +39,7 @@ struct Poseidon2Sponge final // Sponge Config Poseidon2Config config; - Poseidon2Sponge() = default; + Poseidon2Sponge() : config(Poseidon2Config::CreateDefault()) {} explicit Poseidon2Sponge(const Poseidon2Config& config) : config(config) {} explicit Poseidon2Sponge(Poseidon2Config&& config) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc index 6c916039ce..ff8f585e6e 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_unittest.cc @@ -51,8 +51,7 @@ TEST_F(Poseidon2GoldilocksTest, Copyable) { using Params = Poseidon2Params; - auto config = Poseidon2Config::CreateDefault(); - Poseidon2Sponge expected(std::move(config)); + Poseidon2Sponge expected; base::Uint8VectorBuffer write_buf; ASSERT_TRUE(write_buf.Grow(base::EstimateSize(expected))); @@ -79,8 +78,7 @@ TEST_F(Poseidon2BabyBearTest, Permute) { using Params = Poseidon2Params; - auto config = Poseidon2Config::CreateDefault(); - Poseidon2Sponge sponge(std::move(config)); + Poseidon2Sponge sponge; SpongeState state; for (size_t i = 0; i < 16; ++i) { state.elements[i] = F(i); @@ -104,16 +102,14 @@ TEST_F(Poseidon2BabyBearTest, PermutePacked) { Poseidon2Params; - auto packed_config = Poseidon2Config::CreateDefault(); - Poseidon2Sponge packed_sponge(std::move(packed_config)); + Poseidon2Sponge packed_sponge; SpongeState packed_state; for (size_t i = 0; i < 16; ++i) { packed_state.elements[i] = PackedF(i); } packed_sponge.Permute(packed_state); - auto config = Poseidon2Config::CreateDefault(); - Poseidon2Sponge sponge(std::move(config)); + Poseidon2Sponge sponge; SpongeState state; for (size_t i = 0; i < 16; ++i) { state.elements[i] = F(i); diff --git a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc index 09f36697eb..d82e3d4626 100644 --- a/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc +++ b/tachyon/crypto/hashes/sponge/truncated_permutation_unittest.cc @@ -28,8 +28,7 @@ TEST_F(TruncatedPermutationTest, Hash) { constexpr size_t kChunk = 8; constexpr size_t kN = 2; - auto config = Poseidon2Config::CreateDefault(); - Poseidon2 sponge(std::move(config)); + Poseidon2 sponge; TruncatedPermutation compressor(std::move(sponge)); std::vector> inputs = base::CreateVector(kN, [](uint32_t i) { return base::CreateVector(kChunk, From c99c7af14e4183bbe22711007b3412fcfa413f26 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 17:21:29 +0900 Subject: [PATCH 10/25] refac: simplify code by using default constructors --- benchmark/fri/fri_benchmark.cc | 17 ++--------- .../baby_bear_poseidon2_hintable_unittest.cc | 3 +- .../challenger/hash_challenger_unittest.cc | 3 +- .../commitments/fri/two_adic_fri_unittest.cc | 15 ++-------- ...tension_field_merkle_tree_mmcs_unittest.cc | 30 ++++++------------- .../field_merkle_tree_mmcs_unittest.cc | 13 -------- .../field_merkle_tree_unittest.cc | 11 ------- .../sponge/padding_free_sponge_unittest.cc | 3 +- 8 files changed, 17 insertions(+), 78 deletions(-) diff --git a/benchmark/fri/fri_benchmark.cc b/benchmark/fri/fri_benchmark.cc index ee5ae7f651..563de35377 100644 --- a/benchmark/fri/fri_benchmark.cc +++ b/benchmark/fri/fri_benchmark.cc @@ -80,23 +80,12 @@ void Run(const FRIConfig& config) { ExtF::Init(); ExtPackedF::Init(); - Poseidon2 sponge; - MyHasher hasher(sponge); - MyCompressor compressor(sponge); - - PackedPoseidon2 packed_sponge; - MyPackedHasher packed_hasher(packed_sponge); - MyPackedCompressor packed_compressor(std::move(packed_sponge)); - MMCS mmcs(hasher, packed_hasher, compressor, packed_compressor); - - ChallengeMMCS challenge_mmcs( - ExtMMCS(std::move(hasher), std::move(packed_hasher), - std::move(compressor), std::move(packed_compressor))); - + MMCS mmcs; + ChallengeMMCS challenge_mmcs; crypto::FRIConfig fri_config{config.log_blowup(), 10, 8, challenge_mmcs}; MyPCS pcs = MyPCS(std::move(mmcs), std::move(fri_config)); - Challenger challenger = Challenger(std::move(sponge)); + Challenger challenger; SimpleReporter reporter; std::string name; diff --git a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc index 0a36212dc1..28d65ca63c 100644 --- a/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc +++ b/tachyon/c/zk/air/sp1/baby_bear_poseidon2_hintable_unittest.cc @@ -18,8 +18,7 @@ constexpr size_t kRate = 4; class HintableTest : public math::FiniteFieldTest {}; TEST_F(HintableTest, DuplexChallenger) { - Poseidon2 sponge; - crypto::DuplexChallenger challenger(std::move(sponge)); + crypto::DuplexChallenger challenger; for (size_t i = 0; i < Params::kWidth; ++i) { challenger.state_[i] = F(i); } diff --git a/tachyon/crypto/challenger/hash_challenger_unittest.cc b/tachyon/crypto/challenger/hash_challenger_unittest.cc index 61cb79a5b1..f32dc52cd8 100644 --- a/tachyon/crypto/challenger/hash_challenger_unittest.cc +++ b/tachyon/crypto/challenger/hash_challenger_unittest.cc @@ -30,8 +30,7 @@ namespace { class HashChallengerTest : public math::FiniteFieldTest { public: void SetUp() override { - Poseidon2 sponge; - MyHasher hasher(std::move(sponge)); + MyHasher hasher; std::vector initial_state = base::CreateVector(10, [](size_t i) { return F(i + 1); }); diff --git a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc index cb7e6eadf2..8011dfc89d 100644 --- a/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc +++ b/tachyon/crypto/commitments/fri/two_adic_fri_unittest.cc @@ -59,24 +59,13 @@ class TwoAdicFRITest : public testing::Test { } void SetUp() override { - Poseidon2 sponge; - MyHasher hasher(sponge); - MyCompressor compressor(sponge); - - PackedPoseidon2 packed_sponge; - MyPackedHasher packed_hasher(packed_sponge); - MyPackedCompressor packed_compressor(std::move(packed_sponge)); - MMCS mmcs(hasher, packed_hasher, compressor, packed_compressor); - - ChallengeMMCS challenge_mmcs( - ExtMMCS(std::move(hasher), std::move(packed_hasher), - std::move(compressor), std::move(packed_compressor))); + MMCS mmcs; + ChallengeMMCS challenge_mmcs; // TODO(ashjeong): Include separate test for |log_blowup| = 2 FRIConfig fri_config{1, 10, 8, std::move(challenge_mmcs)}; pcs_ = MyPCS(std::move(mmcs), std::move(fri_config)); - challenger_ = Challenger(std::move(sponge)); } void TestProtocol(std::vector> log_degrees_by_round) { diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc index bab190415b..0d989aa81c 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs_unittest.cc @@ -5,7 +5,8 @@ #include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/extension_field_merkle_tree_mmcs.h" -#include +#include +#include #include "gtest/gtest.h" @@ -54,21 +55,8 @@ class ExtensionFieldMerkleTreeMMCSTest : public math::FiniteFieldTest { ExtF::Init(); } - void SetUp() override { - Poseidon2 sponge; - MyHasher hasher(sponge); - MyCompressor compressor(std::move(sponge)); - - PackedPoseidon2 packed_sponge; - MyPackedHasher packed_hasher(packed_sponge); - MyPackedCompressor packed_compressor(std::move(packed_sponge)); - ext_mmcs_.reset(new ExtMMCS( - InnerMMCS(std::move(hasher), std::move(packed_hasher), - std::move(compressor), std::move(packed_compressor)))); - } - protected: - std::unique_ptr ext_mmcs_; + ExtMMCS ext_mmcs_; }; } // namespace @@ -93,10 +81,10 @@ TEST_F(ExtensionFieldMerkleTreeMMCSTest, CommitAndVerify) { std::move(ext_matrix)}; std::array ext_commitment; ExtTree ext_prover_data; - ASSERT_TRUE(ext_mmcs_->CommitOwned(std::move(ext_matrices), &ext_commitment, - &ext_prover_data)); + ASSERT_TRUE(ext_mmcs_.CommitOwned(std::move(ext_matrices), &ext_commitment, + &ext_prover_data)); - const InnerMMCS& inner_mmcs = ext_mmcs_->inner(); + const InnerMMCS& inner_mmcs = ext_mmcs_.inner(); MMCS mmcs(inner_mmcs.hasher(), inner_mmcs.packed_hasher(), inner_mmcs.compressor(), inner_mmcs.packed_compressor()); std::vector> matrices = {std::move(matrix)}; @@ -110,11 +98,11 @@ TEST_F(ExtensionFieldMerkleTreeMMCSTest, CommitAndVerify) { std::vector> openings; std::vector> proof; ASSERT_TRUE( - ext_mmcs_->CreateOpeningProof(index, ext_prover_data, &openings, &proof)); + ext_mmcs_.CreateOpeningProof(index, ext_prover_data, &openings, &proof)); std::vector dimensions_vec; dimensions_vec.push_back({kCols, kRows}); - ASSERT_TRUE(ext_mmcs_->VerifyOpeningProof(ext_commitment, dimensions_vec, - index, openings, proof)); + ASSERT_TRUE(ext_mmcs_.VerifyOpeningProof(ext_commitment, dimensions_vec, + index, openings, proof)); } } // namespace tachyon::crypto diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc index 16225cb651..d3a89fb195 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs_unittest.cc @@ -42,19 +42,6 @@ using MMCS = FieldMerkleTreeMMCS { - public: - void SetUp() override { - Poseidon2 sponge; - MyHasher hasher(sponge); - MyCompressor compressor(std::move(sponge)); - - PackedPoseidon2 packed_sponge; - MyPackedHasher packed_hasher(packed_sponge); - MyPackedCompressor packed_compressor(std::move(packed_sponge)); - mmcs_ = MMCS(std::move(hasher), std::move(packed_hasher), - std::move(compressor), std::move(packed_compressor)); - } - protected: MMCS mmcs_; }; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index d3c5b18dde..2d5d26b095 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -40,17 +40,6 @@ using Tree = FieldMerkleTree; namespace { class FieldMerkleTreeTest : public math::FiniteFieldTest { - public: - void SetUp() override { - Poseidon2 sponge; - hasher_ = MyHasher(sponge); - compressor_ = MyCompressor(std::move(sponge)); - - PackedPoseidon2 packed_sponge; - packed_hasher_ = MyPackedHasher(packed_sponge); - packed_compressor_ = MyPackedCompressor(std::move(packed_sponge)); - } - protected: MyHasher hasher_; MyCompressor compressor_; diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc index 5ea687d000..d4392563da 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge_unittest.cc @@ -27,8 +27,7 @@ TEST_F(PaddingFreeSpongeTest, Hash) { constexpr size_t kRate = 8; constexpr size_t kOut = 8; - Poseidon2 sponge; - PaddingFreeSponge hasher(std::move(sponge)); + PaddingFreeSponge hasher; std::vector inputs = base::CreateVector(100, [](uint32_t i) { return F(i); }); std::array hash = hasher.Hash(inputs); From 4ffd801e8ed3a3917f424b84fb97d0308bd1373b Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 18:18:39 +0900 Subject: [PATCH 11/25] refac(math): add `_cuda` suffix --- .../univariate/icicle/icicle_ntt_baby_bear.cc | 12 ++++++------ .../univariate/icicle/icicle_ntt_baby_bear.h | 4 ++-- .../univariate/icicle/icicle_ntt_bls12_381.cc | 13 +++++++------ .../univariate/icicle/icicle_ntt_bls12_381.h | 4 ++-- .../univariate/icicle/icicle_ntt_bn254.cc | 13 +++++++------ .../univariate/icicle/icicle_ntt_bn254.h | 4 ++-- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc index 168d0c1856..67ac489b49 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc @@ -8,7 +8,7 @@ #include "tachyon/device/gpu/gpu_logging.h" #include "tachyon/math/polynomials/univariate/icicle/icicle_ntt.h" -cudaError_t tachyon_babybear_initialize_domain( +cudaError_t tachyon_babybear_initialize_domain_cuda( const ::babybear::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode) { return ::ntt::init_domain(primitive_root, ctx, fast_twiddles_mode); @@ -21,7 +21,7 @@ cudaError_t tachyon_babybear_ntt_cuda( return ::ntt::ntt(input, size, dir, config, output); } -cudaError_t tachyon_babybear_release_domain( +cudaError_t tachyon_babybear_release_domain_cuda( ::device_context::DeviceContext& ctx) { return ::ntt::release_domain<::babybear::scalar_t>(ctx); } @@ -42,11 +42,11 @@ bool IcicleNTT::Init(const BabyBear& group_gen, // 2. |fast_twiddles_mode| consumes a lot of memory, so we need to disable it // if the ram of the GPU is not enough. See // https://github.com/ingonyama-zk/icicle/blob/4fef542/icicle/include/ntt/ntt.cuh#L26-L40. - gpuError_t error = tachyon_babybear_initialize_domain( + gpuError_t error = tachyon_babybear_initialize_domain_cuda( reinterpret_cast(group_gen_big_int), ctx, options.fast_twiddles_mode); if (error != gpuSuccess) { - GPU_LOG(ERROR, error) << "Failed tachyon_babybear_initialize_domain()"; + GPU_LOG(ERROR, error) << "Failed tachyon_babybear_initialize_domain_cuda()"; return false; } VLOG(1) << "IcicleNTT is initialized"; @@ -106,9 +106,9 @@ bool IcicleNTT::Release() { #endif ::device_context::DeviceContext ctx{stream_, /*device_id=*/0, mem_pool_}; - gpuError_t error = tachyon_babybear_release_domain(ctx); + gpuError_t error = tachyon_babybear_release_domain_cuda(ctx); if (error != gpuSuccess) { - GPU_LOG(ERROR, error) << "Failed tachyon_babybear_release_domain()"; + GPU_LOG(ERROR, error) << "Failed tachyon_babybear_release_domain_cuda()"; return false; } return true; diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h index 6e80f19914..2d3b5e1c97 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h @@ -6,7 +6,7 @@ #include "third_party/icicle/include/fields/stark_fields/babybear.cu.h" #include "third_party/icicle/include/ntt/ntt.cu.h" -extern "C" cudaError_t tachyon_babybear_initialize_domain( +extern "C" cudaError_t tachyon_babybear_initialize_domain_cuda( const ::babybear::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode); @@ -15,7 +15,7 @@ extern "C" cudaError_t tachyon_babybear_ntt_cuda( ::ntt::NTTConfig<::babybear::scalar_t>& config, ::babybear::scalar_t* output); -extern "C" cudaError_t tachyon_babybear_release_domain( +extern "C" cudaError_t tachyon_babybear_release_domain_cuda( ::device_context::DeviceContext& ctx); #endif // TACHYON_MATH_POLYNOMIALS_UNIVARIATE_ICICLE_ICICLE_NTT_BABY_BEAR_H_ diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc index ef5b4bbb1c..dc1bfdd660 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc @@ -8,7 +8,7 @@ #include "tachyon/device/gpu/gpu_logging.h" #include "tachyon/math/polynomials/univariate/icicle/icicle_ntt.h" -cudaError_t tachyon_bls12_381_initialize_domain( +cudaError_t tachyon_bls12_381_initialize_domain_cuda( const ::bls12_381::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode) { return ::ntt::init_domain(primitive_root, ctx, fast_twiddles_mode); @@ -21,7 +21,7 @@ cudaError_t tachyon_bls12_381_ntt_cuda( return ::ntt::ntt(input, size, dir, config, output); } -cudaError_t tachyon_bls12_381_release_domain( +cudaError_t tachyon_bls12_381_release_domain_cuda( ::device_context::DeviceContext& ctx) { return ::ntt::release_domain<::bls12_381::scalar_t>(ctx); } @@ -42,11 +42,12 @@ bool IcicleNTT::Init(const bls12_381::Fr& group_gen, // 2. |fast_twiddles_mode| consumes a lot of memory, so we need to disable it // if the ram of the GPU is not enough. See // https://github.com/ingonyama-zk/icicle/blob/4fef542/icicle/include/ntt/ntt.cuh#L26-L40. - gpuError_t error = tachyon_bls12_381_initialize_domain( + gpuError_t error = tachyon_bls12_381_initialize_domain_cuda( reinterpret_cast(group_gen_big_int), ctx, options.fast_twiddles_mode); if (error != gpuSuccess) { - GPU_LOG(ERROR, error) << "Failed tachyon_bls12_381_initialize_domain()"; + GPU_LOG(ERROR, error) + << "Failed tachyon_bls12_381_initialize_domain_cuda()"; return false; } VLOG(1) << "IcicleNTT is initialized"; @@ -106,9 +107,9 @@ bool IcicleNTT::Release() { #endif ::device_context::DeviceContext ctx{stream_, /*device_id=*/0, mem_pool_}; - gpuError_t error = tachyon_bls12_381_release_domain(ctx); + gpuError_t error = tachyon_bls12_381_release_domain_cuda(ctx); if (error != gpuSuccess) { - GPU_LOG(ERROR, error) << "Failed tachyon_bls12_381_release_domain()"; + GPU_LOG(ERROR, error) << "Failed tachyon_bls12_381_release_domain_cuda()"; return false; } return true; diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h index bd8e8287fd..865ce5271e 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h @@ -6,7 +6,7 @@ #include "third_party/icicle/include/curves/params/bls12_381.cu.h" #include "third_party/icicle/include/ntt/ntt.cu.h" -extern "C" cudaError_t tachyon_bls12_381_initialize_domain( +extern "C" cudaError_t tachyon_bls12_381_initialize_domain_cuda( const ::bls12_381::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode); @@ -15,7 +15,7 @@ extern "C" cudaError_t tachyon_bls12_381_ntt_cuda( ::ntt::NTTConfig<::bls12_381::scalar_t>& config, ::bls12_381::scalar_t* output); -extern "C" cudaError_t tachyon_bls12_381_release_domain( +extern "C" cudaError_t tachyon_bls12_381_release_domain_cuda( ::device_context::DeviceContext& ctx); #endif // TACHYON_MATH_POLYNOMIALS_UNIVARIATE_ICICLE_ICICLE_NTT_BLS12_381_H_ diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc index 3c25a1035f..0e9f7f40f0 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc @@ -8,7 +8,7 @@ #include "tachyon/device/gpu/gpu_logging.h" #include "tachyon/math/polynomials/univariate/icicle/icicle_ntt.h" -cudaError_t tachyon_bn254_initialize_domain( +cudaError_t tachyon_bn254_initialize_domain_cuda( const ::bn254::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode) { return ::ntt::init_domain(primitive_root, ctx, fast_twiddles_mode); @@ -21,7 +21,8 @@ cudaError_t tachyon_bn254_ntt_cuda(const ::bn254::scalar_t* input, int size, return ::ntt::ntt(input, size, dir, config, output); } -cudaError_t tachyon_bn254_release_domain(::device_context::DeviceContext& ctx) { +cudaError_t tachyon_bn254_release_domain_cuda( + ::device_context::DeviceContext& ctx) { return ::ntt::release_domain<::bn254::scalar_t>(ctx); } @@ -41,11 +42,11 @@ bool IcicleNTT::Init(const bn254::Fr& group_gen, // 2. |fast_twiddles_mode| consumes a lot of memory, so we need to disable it // if the ram of the GPU is not enough. See // https://github.com/ingonyama-zk/icicle/blob/4fef542/icicle/include/ntt/ntt.cuh#L26-L40. - gpuError_t error = tachyon_bn254_initialize_domain( + gpuError_t error = tachyon_bn254_initialize_domain_cuda( reinterpret_cast(group_gen_big_int), ctx, options.fast_twiddles_mode); if (error != gpuSuccess) { - GPU_LOG(ERROR, error) << "Failed tachyon_bn254_initialize_domain()"; + GPU_LOG(ERROR, error) << "Failed tachyon_bn254_initialize_domain_cuda()"; return false; } VLOG(1) << "IcicleNTT is initialized"; @@ -106,9 +107,9 @@ bool IcicleNTT::Release() { #endif ::device_context::DeviceContext ctx{stream_, /*device_id=*/0, mem_pool_}; - gpuError_t error = tachyon_bn254_release_domain(ctx); + gpuError_t error = tachyon_bn254_release_domain_cuda(ctx); if (error != gpuSuccess) { - GPU_LOG(ERROR, error) << "Failed tachyon_bn254_release_domain()"; + GPU_LOG(ERROR, error) << "Failed tachyon_bn254_release_domain_cuda()"; return false; } return true; diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h index c4f6f938c1..93f864ddac 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h @@ -6,7 +6,7 @@ #include "third_party/icicle/include/curves/params/bn254.cu.h" #include "third_party/icicle/include/ntt/ntt.cu.h" -extern "C" cudaError_t tachyon_bn254_initialize_domain( +extern "C" cudaError_t tachyon_bn254_initialize_domain_cuda( const ::bn254::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode); @@ -14,7 +14,7 @@ extern "C" cudaError_t tachyon_bn254_ntt_cuda( const ::bn254::scalar_t* input, int size, ::ntt::NTTDir dir, ::ntt::NTTConfig<::bn254::scalar_t>& config, ::bn254::scalar_t* output); -extern "C" cudaError_t tachyon_bn254_release_domain( +extern "C" cudaError_t tachyon_bn254_release_domain_cuda( ::device_context::DeviceContext& ctx); #endif // TACHYON_MATH_POLYNOMIALS_UNIVARIATE_ICICLE_ICICLE_NTT_BN254_H_ From 563c3e26ab878d9cca1db801d79e5dbff64ca48c Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 12:53:23 +0900 Subject: [PATCH 12/25] refac(math): use `gpuError_t` instead of `cudaError_t` --- .../msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc | 9 ++++----- .../msm/algorithms/icicle/icicle_msm_bls12_381_g1.h | 4 +++- .../msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc | 8 ++++---- .../msm/algorithms/icicle/icicle_msm_bls12_381_g2.h | 4 +++- .../msm/algorithms/icicle/icicle_msm_bn254_g1.cc | 8 ++++---- .../msm/algorithms/icicle/icicle_msm_bn254_g1.h | 4 +++- .../msm/algorithms/icicle/icicle_msm_bn254_g2.cc | 8 ++++---- .../msm/algorithms/icicle/icicle_msm_bn254_g2.h | 4 +++- .../univariate/icicle/icicle_ntt_baby_bear.cc | 6 +++--- .../univariate/icicle/icicle_ntt_baby_bear.h | 8 +++++--- .../univariate/icicle/icicle_ntt_bls12_381.cc | 6 +++--- .../univariate/icicle/icicle_ntt_bls12_381.h | 8 +++++--- .../univariate/icicle/icicle_ntt_bn254.cc | 12 ++++++------ .../polynomials/univariate/icicle/icicle_ntt_bn254.h | 8 +++++--- 14 files changed, 55 insertions(+), 42 deletions(-) diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc index 8b7e841a9d..798d5cc036 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc @@ -9,11 +9,10 @@ #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm.h" #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_utils.h" -cudaError_t tachyon_bls12_381_g1_msm_cuda(const ::bls12_381::scalar_t* scalars, - const ::bls12_381::affine_t* points, - int msm_size, - ::msm::MSMConfig& config, - ::bls12_381::projective_t* out) { +gpuError_t tachyon_bls12_381_g1_msm_cuda(const ::bls12_381::scalar_t* scalars, + const ::bls12_381::affine_t* points, + int msm_size, ::msm::MSMConfig& config, + ::bls12_381::projective_t* out) { return ::msm::msm(scalars, points, msm_size, config, out); } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.h b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.h index 1ea69c2d80..85e9ca4e40 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.h +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.h @@ -4,7 +4,9 @@ #include "third_party/icicle/include/curves/params/bls12_381.cu.h" #include "third_party/icicle/include/msm/msm.cu.h" -extern "C" cudaError_t tachyon_bls12_381_g1_msm_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bls12_381_g1_msm_cuda( const ::bls12_381::scalar_t* scalars, const ::bls12_381::affine_t* points, int msm_size, ::msm::MSMConfig& config, ::bls12_381::projective_t* out); diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc index 5158dddf79..fd9ed0f4bd 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc @@ -9,10 +9,10 @@ #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm.h" #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_utils.h" -cudaError_t tachyon_bls12_381_g2_msm_cuda( - const ::bls12_381::scalar_t* scalars, - const ::bls12_381::g2_affine_t* points, int msm_size, - ::msm::MSMConfig& config, ::bls12_381::g2_projective_t* out) { +gpuError_t tachyon_bls12_381_g2_msm_cuda(const ::bls12_381::scalar_t* scalars, + const ::bls12_381::g2_affine_t* points, + int msm_size, ::msm::MSMConfig& config, + ::bls12_381::g2_projective_t* out) { return ::msm::msm(scalars, points, msm_size, config, out); } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.h b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.h index 2f18dea36e..f251765023 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.h +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.h @@ -4,7 +4,9 @@ #include "third_party/icicle/include/curves/params/bls12_381.cu.h" #include "third_party/icicle/include/msm/msm.cu.h" -extern "C" cudaError_t tachyon_bls12_381_g2_msm_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bls12_381_g2_msm_cuda( const ::bls12_381::scalar_t* scalars, const ::bls12_381::g2_affine_t* points, int msm_size, ::msm::MSMConfig& config, ::bls12_381::g2_projective_t* out); diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc index c6c6ec63d4..d419f842bd 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc @@ -9,10 +9,10 @@ #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm.h" #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_utils.h" -cudaError_t tachyon_bn254_g1_msm_cuda(const ::bn254::scalar_t* scalars, - const ::bn254::affine_t* points, - int msm_size, ::msm::MSMConfig& config, - ::bn254::projective_t* out) { +gpuError_t tachyon_bn254_g1_msm_cuda(const ::bn254::scalar_t* scalars, + const ::bn254::affine_t* points, + int msm_size, ::msm::MSMConfig& config, + ::bn254::projective_t* out) { return ::msm::msm(scalars, points, msm_size, config, out); } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.h b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.h index f90235f090..eba92e86aa 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.h +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.h @@ -4,7 +4,9 @@ #include "third_party/icicle/include/curves/params/bn254.cu.h" #include "third_party/icicle/include/msm/msm.cu.h" -extern "C" cudaError_t tachyon_bn254_g1_msm_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bn254_g1_msm_cuda( const ::bn254::scalar_t* scalars, const ::bn254::affine_t* points, int msm_size, ::msm::MSMConfig& config, ::bn254::projective_t* out); diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc index effea17570..83e149509c 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc @@ -9,10 +9,10 @@ #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm.h" #include "tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_utils.h" -cudaError_t tachyon_bn254_g2_msm_cuda(const ::bn254::scalar_t* scalars, - const ::bn254::g2_affine_t* points, - int msm_size, ::msm::MSMConfig& config, - ::bn254::g2_projective_t* out) { +gpuError_t tachyon_bn254_g2_msm_cuda(const ::bn254::scalar_t* scalars, + const ::bn254::g2_affine_t* points, + int msm_size, ::msm::MSMConfig& config, + ::bn254::g2_projective_t* out) { return ::msm::msm(scalars, points, msm_size, config, out); } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.h b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.h index b1b734b14d..8f8f0776db 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.h +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.h @@ -4,7 +4,9 @@ #include "third_party/icicle/include/curves/params/bn254.cu.h" #include "third_party/icicle/include/msm/msm.cu.h" -extern "C" cudaError_t tachyon_bn254_g2_msm_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bn254_g2_msm_cuda( const ::bn254::scalar_t* scalars, const ::bn254::g2_affine_t* points, int msm_size, ::msm::MSMConfig& config, ::bn254::g2_projective_t* out); diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc index 67ac489b49..969715df38 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc @@ -8,20 +8,20 @@ #include "tachyon/device/gpu/gpu_logging.h" #include "tachyon/math/polynomials/univariate/icicle/icicle_ntt.h" -cudaError_t tachyon_babybear_initialize_domain_cuda( +gpuError_t tachyon_babybear_initialize_domain_cuda( const ::babybear::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode) { return ::ntt::init_domain(primitive_root, ctx, fast_twiddles_mode); } -cudaError_t tachyon_babybear_ntt_cuda( +gpuError_t tachyon_babybear_ntt_cuda( const ::babybear::scalar_t* input, int size, ::ntt::NTTDir dir, ::ntt::NTTConfig<::babybear::scalar_t>& config, ::babybear::scalar_t* output) { return ::ntt::ntt(input, size, dir, config, output); } -cudaError_t tachyon_babybear_release_domain_cuda( +gpuError_t tachyon_babybear_release_domain_cuda( ::device_context::DeviceContext& ctx) { return ::ntt::release_domain<::babybear::scalar_t>(ctx); } diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h index 2d3b5e1c97..a63d8ddd8c 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.h @@ -6,16 +6,18 @@ #include "third_party/icicle/include/fields/stark_fields/babybear.cu.h" #include "third_party/icicle/include/ntt/ntt.cu.h" -extern "C" cudaError_t tachyon_babybear_initialize_domain_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_babybear_initialize_domain_cuda( const ::babybear::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode); -extern "C" cudaError_t tachyon_babybear_ntt_cuda( +extern "C" gpuError_t tachyon_babybear_ntt_cuda( const ::babybear::scalar_t* input, int size, ::ntt::NTTDir dir, ::ntt::NTTConfig<::babybear::scalar_t>& config, ::babybear::scalar_t* output); -extern "C" cudaError_t tachyon_babybear_release_domain_cuda( +extern "C" gpuError_t tachyon_babybear_release_domain_cuda( ::device_context::DeviceContext& ctx); #endif // TACHYON_MATH_POLYNOMIALS_UNIVARIATE_ICICLE_ICICLE_NTT_BABY_BEAR_H_ diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc index dc1bfdd660..fd2097109f 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc @@ -8,20 +8,20 @@ #include "tachyon/device/gpu/gpu_logging.h" #include "tachyon/math/polynomials/univariate/icicle/icicle_ntt.h" -cudaError_t tachyon_bls12_381_initialize_domain_cuda( +gpuError_t tachyon_bls12_381_initialize_domain_cuda( const ::bls12_381::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode) { return ::ntt::init_domain(primitive_root, ctx, fast_twiddles_mode); } -cudaError_t tachyon_bls12_381_ntt_cuda( +gpuError_t tachyon_bls12_381_ntt_cuda( const ::bls12_381::scalar_t* input, int size, ::ntt::NTTDir dir, ::ntt::NTTConfig<::bls12_381::scalar_t>& config, ::bls12_381::scalar_t* output) { return ::ntt::ntt(input, size, dir, config, output); } -cudaError_t tachyon_bls12_381_release_domain_cuda( +gpuError_t tachyon_bls12_381_release_domain_cuda( ::device_context::DeviceContext& ctx) { return ::ntt::release_domain<::bls12_381::scalar_t>(ctx); } diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h index 865ce5271e..07852f2840 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.h @@ -6,16 +6,18 @@ #include "third_party/icicle/include/curves/params/bls12_381.cu.h" #include "third_party/icicle/include/ntt/ntt.cu.h" -extern "C" cudaError_t tachyon_bls12_381_initialize_domain_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bls12_381_initialize_domain_cuda( const ::bls12_381::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode); -extern "C" cudaError_t tachyon_bls12_381_ntt_cuda( +extern "C" gpuError_t tachyon_bls12_381_ntt_cuda( const ::bls12_381::scalar_t* input, int size, ::ntt::NTTDir dir, ::ntt::NTTConfig<::bls12_381::scalar_t>& config, ::bls12_381::scalar_t* output); -extern "C" cudaError_t tachyon_bls12_381_release_domain_cuda( +extern "C" gpuError_t tachyon_bls12_381_release_domain_cuda( ::device_context::DeviceContext& ctx); #endif // TACHYON_MATH_POLYNOMIALS_UNIVARIATE_ICICLE_ICICLE_NTT_BLS12_381_H_ diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc index 0e9f7f40f0..2303fcbba5 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc @@ -8,20 +8,20 @@ #include "tachyon/device/gpu/gpu_logging.h" #include "tachyon/math/polynomials/univariate/icicle/icicle_ntt.h" -cudaError_t tachyon_bn254_initialize_domain_cuda( +gpuError_t tachyon_bn254_initialize_domain_cuda( const ::bn254::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode) { return ::ntt::init_domain(primitive_root, ctx, fast_twiddles_mode); } -cudaError_t tachyon_bn254_ntt_cuda(const ::bn254::scalar_t* input, int size, - ::ntt::NTTDir dir, - ::ntt::NTTConfig<::bn254::scalar_t>& config, - ::bn254::scalar_t* output) { +gpuError_t tachyon_bn254_ntt_cuda(const ::bn254::scalar_t* input, int size, + ::ntt::NTTDir dir, + ::ntt::NTTConfig<::bn254::scalar_t>& config, + ::bn254::scalar_t* output) { return ::ntt::ntt(input, size, dir, config, output); } -cudaError_t tachyon_bn254_release_domain_cuda( +gpuError_t tachyon_bn254_release_domain_cuda( ::device_context::DeviceContext& ctx) { return ::ntt::release_domain<::bn254::scalar_t>(ctx); } diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h index 93f864ddac..77c33bf937 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.h @@ -6,15 +6,17 @@ #include "third_party/icicle/include/curves/params/bn254.cu.h" #include "third_party/icicle/include/ntt/ntt.cu.h" -extern "C" cudaError_t tachyon_bn254_initialize_domain_cuda( +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bn254_initialize_domain_cuda( const ::bn254::scalar_t& primitive_root, ::device_context::DeviceContext& ctx, bool fast_twiddles_mode); -extern "C" cudaError_t tachyon_bn254_ntt_cuda( +extern "C" gpuError_t tachyon_bn254_ntt_cuda( const ::bn254::scalar_t* input, int size, ::ntt::NTTDir dir, ::ntt::NTTConfig<::bn254::scalar_t>& config, ::bn254::scalar_t* output); -extern "C" cudaError_t tachyon_bn254_release_domain_cuda( +extern "C" gpuError_t tachyon_bn254_release_domain_cuda( ::device_context::DeviceContext& ctx); #endif // TACHYON_MATH_POLYNOMIALS_UNIVARIATE_ICICLE_ICICLE_NTT_BN254_H_ From 770d4a656a2322d005985e1037f1cbab3850c0f7 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Fri, 27 Sep 2024 14:26:54 +0900 Subject: [PATCH 13/25] refac(crypto): remove unused header --- tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel | 1 - tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h | 2 -- 2 files changed, 3 deletions(-) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel index 258f11dff1..82901ac979 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel @@ -76,7 +76,6 @@ tachyon_cc_library( deps = [ ":poseidon2_vendor", "//tachyon/base/types:always_false", - "//tachyon/math/elliptic_curves/bn/bn254:fr", "//tachyon/math/finite_fields:finite_field_traits", ], ) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h index f2a82c8bdb..919d7cd43f 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h @@ -6,10 +6,8 @@ #include -#include "tachyon/base/bits.h" #include "tachyon/base/types/always_false.h" #include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h" -#include "tachyon/math/elliptic_curves/bn/bn254/fr.h" #include "tachyon/math/finite_fields/finite_field_traits.h" namespace tachyon::crypto { From 8d819b83b825c8cb44b2d0424957f6048cb7c768 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 10:33:24 +0900 Subject: [PATCH 14/25] feat(crypto): expose `Derived` of `Compressor` and `Hasher` This is needed to retrieve `Poseidon2Params` from those. --- tachyon/crypto/hashes/compressor.h | 4 +++- tachyon/crypto/hashes/hasher.h | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tachyon/crypto/hashes/compressor.h b/tachyon/crypto/hashes/compressor.h index bf0a39ad83..6dd49a623d 100644 --- a/tachyon/crypto/hashes/compressor.h +++ b/tachyon/crypto/hashes/compressor.h @@ -8,9 +8,11 @@ namespace tachyon::crypto { -template +template class Compressor { public: + using Derived = _Derived; + // TODO(chokobole): Make this accept iterator as an argument. template auto Compress(const T& input) const { diff --git a/tachyon/crypto/hashes/hasher.h b/tachyon/crypto/hashes/hasher.h index 930025ce6b..edcfbf34ef 100644 --- a/tachyon/crypto/hashes/hasher.h +++ b/tachyon/crypto/hashes/hasher.h @@ -8,9 +8,11 @@ namespace tachyon::crypto { -template +template class Hasher { public: + using Derived = _Derived; + // TODO(chokobole): Make this accept iterator as an argument. template auto Hash(const T& input) const { From d375c3378b4f764b1b5d5ea7561f521fd2a11ec8 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 10:34:29 +0900 Subject: [PATCH 15/25] feat(crypto): expose params of `PaddingFreeSponge` and `TruncatedPermutation` This is needed to retrieve `Rate` from `PaddingFreeSponge` when configuring `Poseidon2Icicle`. --- tachyon/crypto/hashes/sponge/padding_free_sponge.h | 3 +++ tachyon/crypto/hashes/sponge/truncated_permutation.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge.h b/tachyon/crypto/hashes/sponge/padding_free_sponge.h index 8a0bb17733..f45678696b 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge.h +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge.h @@ -20,6 +20,9 @@ template class PaddingFreeSponge final : public Hasher> { public: + constexpr static size_t kRate = Rate; + constexpr static size_t kOut = Out; + using Params = typename Derived::Params; using F = typename CryptographicSpongeTraits::F; diff --git a/tachyon/crypto/hashes/sponge/truncated_permutation.h b/tachyon/crypto/hashes/sponge/truncated_permutation.h index 5dc9a1e107..88e95c0810 100644 --- a/tachyon/crypto/hashes/sponge/truncated_permutation.h +++ b/tachyon/crypto/hashes/sponge/truncated_permutation.h @@ -20,6 +20,9 @@ template class TruncatedPermutation final : public Compressor> { public: + constexpr static size_t kChunk = Chunk; + constexpr static size_t kN = N; + using Params = typename Derived::Params; using F = typename CryptographicSpongeTraits::F; From 79360dbeb702c3a415970acdeb29ff8b795c2646 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 14:27:45 +0900 Subject: [PATCH 16/25] feat(crypto): add getter to retrieve `Derived` --- tachyon/crypto/hashes/sponge/padding_free_sponge.h | 2 ++ tachyon/crypto/hashes/sponge/truncated_permutation.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tachyon/crypto/hashes/sponge/padding_free_sponge.h b/tachyon/crypto/hashes/sponge/padding_free_sponge.h index f45678696b..40aa7c1bcb 100644 --- a/tachyon/crypto/hashes/sponge/padding_free_sponge.h +++ b/tachyon/crypto/hashes/sponge/padding_free_sponge.h @@ -31,6 +31,8 @@ class PaddingFreeSponge final explicit PaddingFreeSponge(Derived&& derived) : derived_(std::move(derived)) {} + const Derived& derived() const { return derived_; } + private: friend class Hasher>; diff --git a/tachyon/crypto/hashes/sponge/truncated_permutation.h b/tachyon/crypto/hashes/sponge/truncated_permutation.h index 88e95c0810..b6f5a7f691 100644 --- a/tachyon/crypto/hashes/sponge/truncated_permutation.h +++ b/tachyon/crypto/hashes/sponge/truncated_permutation.h @@ -31,6 +31,8 @@ class TruncatedPermutation final explicit TruncatedPermutation(Derived&& derived) : derived_(std::move(derived)) {} + const Derived& derived() const { return derived_; } + private: friend class Compressor>; From f14608e9107b0313bbee5bdad4219e5dda0c570c Mon Sep 17 00:00:00 2001 From: Noah Kim Date: Fri, 9 Aug 2024 01:52:06 +0900 Subject: [PATCH 17/25] build: update icicle version --- .../icicle/icicle_msm_bls12_381_g1.cc | 4 +- .../icicle/icicle_msm_bls12_381_g2.cc | 4 +- .../algorithms/icicle/icicle_msm_bn254_g1.cc | 4 +- .../algorithms/icicle/icicle_msm_bn254_g2.cc | 4 +- .../univariate/icicle/icicle_ntt_baby_bear.cc | 10 +- .../univariate/icicle/icicle_ntt_bls12_381.cc | 9 +- .../univariate/icicle/icicle_ntt_bn254.cc | 9 +- third_party/icicle/build_defs.bzl | 2 + third_party/icicle/icicle.BUILD | 21 +- third_party/icicle/rename.patch | 897 +++++++++++++----- third_party/icicle/workspace.bzl | 6 +- 11 files changed, 695 insertions(+), 275 deletions(-) diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc index 798d5cc036..3b68f06a22 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g1.cc @@ -73,7 +73,9 @@ bool IcicleMSM::Run( final_value = final_value + ret; } final_value = ::bls12_381::projective_t::to_montgomery(final_value); - *cpu_result = base::bit_cast>(final_value); + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bls12_381::projective_t| becomes trivially copyable. + *cpu_result = *reinterpret_cast*>(&final_value); return true; } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc index fd9ed0f4bd..ae2a16e65a 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bls12_381_g2.cc @@ -74,7 +74,9 @@ bool IcicleMSM::Run( final_value = final_value + ret; } final_value = ::bls12_381::g2_projective_t::to_montgomery(final_value); - *cpu_result = base::bit_cast>(final_value); + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bls12_381::g2_projective_t| becomes trivially copyable. + *cpu_result = *reinterpret_cast*>(&final_value); return true; } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc index d419f842bd..3d12e3a332 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g1.cc @@ -72,7 +72,9 @@ bool IcicleMSM::Run( final_value = final_value + ret; } final_value = ::bn254::projective_t::to_montgomery(final_value); - *cpu_result = base::bit_cast>(final_value); + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bn254::projective_t| becomes trivially copyable. + *cpu_result = *reinterpret_cast*>(&final_value); return true; } diff --git a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc index 83e149509c..c7fe4ec6fe 100644 --- a/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc +++ b/tachyon/math/elliptic_curves/msm/algorithms/icicle/icicle_msm_bn254_g2.cc @@ -72,7 +72,9 @@ bool IcicleMSM::Run( final_value = final_value + ret; } final_value = ::bn254::g2_projective_t::to_montgomery(final_value); - *cpu_result = base::bit_cast>(final_value); + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bn254::g2_projective_t| becomes trivially copyable. + *cpu_result = *reinterpret_cast*>(&final_value); return true; } diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc index 969715df38..770db0f687 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_baby_bear.cc @@ -51,9 +51,12 @@ bool IcicleNTT::Init(const BabyBear& group_gen, } VLOG(1) << "IcicleNTT is initialized"; + auto one = ::babybear::scalar_t::one(); config_.reset(new ::ntt::NTTConfig{ ctx, - base::bit_cast(::babybear::scalar_t::one()), + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::babybear::scalar_t| becomes trivially copyable. + *reinterpret_cast(&one), options.batch_size, options.columns_batch, options.ordering, @@ -77,9 +80,12 @@ bool IcicleNTT::Run(::ntt::NttAlgorithm algorithm, // |sizeof(::babybear::scalar_t)| and |sizeof(BabyBear)| are same. This // is because their alignments are different. See // https://github.com/ingonyama-zk/icicle/blob/4fef542/icicle/include/fields/storage.cuh. + uint32_t coset_gen = static_cast(coset[0]); ::ntt::NTTConfig<::babybear::scalar_t> config{ config_->ctx, - base::bit_cast<::babybear::scalar_t>(static_cast(coset[0])), + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::babybear::scalar_t| becomes trivially copyable. + *reinterpret_cast<::babybear::scalar_t*>(&coset_gen), config_->batch_size, config_->columns_batch, config_->ordering, diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc index fd2097109f..58a72e8efe 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bls12_381.cc @@ -52,9 +52,12 @@ bool IcicleNTT::Init(const bls12_381::Fr& group_gen, } VLOG(1) << "IcicleNTT is initialized"; + auto one = ::bls12_381::scalar_t::one(); config_.reset(new ::ntt::NTTConfig{ ctx, - base::bit_cast(::bls12_381::scalar_t::one()), + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bls12_381::scalar_t| becomes trivially copyable. + *reinterpret_cast(&one), options.batch_size, options.columns_batch, options.ordering, @@ -80,7 +83,9 @@ bool IcicleNTT::Run(::ntt::NttAlgorithm algorithm, // https://github.com/ingonyama-zk/icicle/blob/4fef542/icicle/include/fields/storage.cuh. ::ntt::NTTConfig<::bls12_381::scalar_t> config{ config_->ctx, - base::bit_cast<::bls12_381::scalar_t>(coset), + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bls12_381::scalar_t| becomes trivially copyable. + *reinterpret_cast<::bls12_381::scalar_t*>(const_cast(&coset)), config_->batch_size, config_->columns_batch, config_->ordering, diff --git a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc index 2303fcbba5..b08d1ff30f 100644 --- a/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc +++ b/tachyon/math/polynomials/univariate/icicle/icicle_ntt_bn254.cc @@ -51,9 +51,12 @@ bool IcicleNTT::Init(const bn254::Fr& group_gen, } VLOG(1) << "IcicleNTT is initialized"; + auto one = ::bn254::scalar_t::one(); config_.reset(new ::ntt::NTTConfig{ ctx, - base::bit_cast(::bn254::scalar_t::one()), + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bn254::scalar_t| becomes trivially copyable. + *reinterpret_cast(&one), options.batch_size, options.columns_batch, options.ordering, @@ -80,7 +83,9 @@ bool IcicleNTT::Run(::ntt::NttAlgorithm algorithm, // https://github.com/ingonyama-zk/icicle/blob/4fef542/icicle/include/fields/storage.cuh. ::ntt::NTTConfig<::bn254::scalar_t> config{ config_->ctx, - base::bit_cast<::bn254::scalar_t>(coset), + // TODO(chokobole): Change it to |base::bit_cast| again if the + // |::bn254::scalar_t| becomes trivially copyable. + *reinterpret_cast<::bn254::scalar_t*>(const_cast(&coset)), config_->batch_size, config_->columns_batch, config_->ordering, diff --git a/third_party/icicle/build_defs.bzl b/third_party/icicle/build_defs.bzl index e33b4ec8a8..2dd07ef48d 100644 --- a/third_party/icicle/build_defs.bzl +++ b/third_party/icicle/build_defs.bzl @@ -26,6 +26,8 @@ FIELDS = [ M31, ] +FIELDS_WITH_MERKLE_TREE = FIELDS + FIELDS_WITH_NTT = [ BN254, BLS12_381, diff --git a/third_party/icicle/icicle.BUILD b/third_party/icicle/icicle.BUILD index a19c38cb8a..e9bdbad59e 100644 --- a/third_party/icicle/icicle.BUILD +++ b/third_party/icicle/icicle.BUILD @@ -2,6 +2,7 @@ load( "@icicle//:build_defs.bzl", "CURVES", "FIELDS", + "FIELDS_WITH_MERKLE_TREE", "FIELDS_WITH_NTT", "FIELDS_WITH_POSEIDON", "FIELDS_WITH_POSEIDON2", @@ -35,6 +36,19 @@ tachyon_cuda_library( deps = [":hdrs"], ) +[tachyon_cuda_library( + name = "merkle_tree_{}".format(field), + hdrs = [ + "icicle/src/merkle-tree/merkle.cu.cc", + "icicle/src/merkle-tree/mmcs.cu.cc", + ], + include_prefix = "third_party/icicle/src", + includes = ["icicle/src/merkle-tree"], + local_defines = icicle_defines(field), + strip_include_prefix = "icicle/src", + deps = [":hdrs"], +) for field in FIELDS_WITH_MERKLE_TREE] + [tachyon_cuda_library( name = "msm_{}".format(field), hdrs = ["icicle/src/msm/msm.cu.cc"], @@ -81,13 +95,8 @@ tachyon_cuda_library( [tachyon_cuda_library( name = "poseidon_{}".format(field), - srcs = if_gpu_is_configured([ - "icicle/src/poseidon/tree/merkle.cu.cc", - ]), hdrs = [ "icicle/src/poseidon/constants.cu.cc", - "icicle/src/poseidon/kernels.cu.cc", - "icicle/src/poseidon/poseidon.cu.cc", ], include_prefix = "third_party/icicle/src", includes = ["icicle/src/poseidon"], @@ -100,8 +109,6 @@ tachyon_cuda_library( name = "poseidon2_{}".format(field), hdrs = [ "icicle/src/poseidon2/constants.cu.cc", - "icicle/src/poseidon2/kernels.cu.cc", - "icicle/src/poseidon2/poseidon.cu.cc", ], include_prefix = "third_party/icicle/src", includes = ["icicle/src/poseidon2"], diff --git a/third_party/icicle/rename.patch b/third_party/icicle/rename.patch index ad626786fa..7ab6ef4494 100644 --- a/third_party/icicle/rename.patch +++ b/third_party/icicle/rename.patch @@ -1,5 +1,5 @@ diff --git a/docs/docs/icicle/polynomials/overview.md b/docs/docs/icicle/polynomials/overview.md -index 06058ffe..726157c8 100644 +index 25ba9dee..c77bce76 100644 --- a/docs/docs/icicle/polynomials/overview.md +++ b/docs/docs/icicle/polynomials/overview.md @@ -61,7 +61,7 @@ Initialization with an appropriate factory is required to configure the computat @@ -45,19 +45,24 @@ similarity index 100% rename from examples/c++/msm/example.cu rename to examples/c++/msm/example.cu.cc diff --git a/examples/c++/multi-gpu-poseidon/example.cu b/examples/c++/multi-gpu-poseidon/example.cu.cc -similarity index 99% +similarity index 98% rename from examples/c++/multi-gpu-poseidon/example.cu rename to examples/c++/multi-gpu-poseidon/example.cu.cc -index 32f0c920..10712521 100644 +index fcc3d527..5706f195 100644 --- a/examples/c++/multi-gpu-poseidon/example.cu +++ b/examples/c++/multi-gpu-poseidon/example.cu.cc -@@ -4,7 +4,7 @@ +@@ -4,10 +4,10 @@ #include #include "api/bn254.h" -#include "gpu-utils/error_handler.cuh" +#include "gpu-utils/error_handler.cu.h" +-#include "poseidon/poseidon.cuh" +-#include "hash/hash.cuh" ++#include "poseidon/poseidon.cu.h" ++#include "hash/hash.cu.h" + using namespace poseidon; using namespace bn254; diff --git a/examples/c++/multiply/README.md b/examples/c++/multiply/README.md @@ -138,22 +143,20 @@ diff --git a/examples/c++/polynomial-api/example.cu b/examples/c++/polynomial-ap similarity index 99% rename from examples/c++/polynomial-api/example.cu rename to examples/c++/polynomial-api/example.cu.cc -index d19178b2..8df88e1b 100644 +index 76229834..fb04a55d 100644 --- a/examples/c++/polynomial-api/example.cu +++ b/examples/c++/polynomial-api/example.cu.cc -@@ -1,9 +1,9 @@ +@@ -1,8 +1,8 @@ #include #include #include "polynomials/polynomials.h" -#include "polynomials/cuda_backend/polynomial_cuda_backend.cuh" -#include "ntt/ntt.cuh" --#include "poseidon/tree/merkle.cuh" +#include "polynomials/cuda_backend/polynomial_cuda_backend.cu.h" +#include "ntt/ntt.cu.h" -+#include "poseidon/tree/merkle.cu.h" + #include "api/bn254.h" #include - diff --git a/examples/c++/polynomial_multiplication/example.cu b/examples/c++/polynomial_multiplication/example.cu.cc similarity index 99% rename from examples/c++/polynomial_multiplication/example.cu @@ -184,21 +187,43 @@ index 7c936694..e4bf8b0b 100644 poseidon_hash(input, output, n, constants, config); ``` diff --git a/examples/c++/poseidon/example.cu b/examples/c++/poseidon/example.cu.cc -similarity index 99% +similarity index 98% rename from examples/c++/poseidon/example.cu rename to examples/c++/poseidon/example.cu.cc -index 17119621..02304875 100644 +index edc408a0..de6afc6e 100644 --- a/examples/c++/poseidon/example.cu +++ b/examples/c++/poseidon/example.cu.cc -@@ -3,7 +3,7 @@ +@@ -3,9 +3,9 @@ #include #include "api/bn254.h" -#include "curves/params/bn254.cuh" +-#include "poseidon/poseidon.cuh" +-#include "hash/hash.cuh" +#include "curves/params/bn254.cu.h" ++#include "poseidon/poseidon.cu.h" ++#include "hash/hash.cu.h" using namespace poseidon; using namespace bn254; +diff --git a/examples/c++/risc0/example.cu b/examples/c++/risc0/example.cu.cc +similarity index 99% +rename from examples/c++/risc0/example.cu +rename to examples/c++/risc0/example.cu.cc +index e07128e8..b7c18a32 100644 +--- a/examples/c++/risc0/example.cu ++++ b/examples/c++/risc0/example.cu.cc +@@ -5,8 +5,8 @@ + #include + + #include "polynomials/polynomials.h" +-#include "polynomials/cuda_backend/polynomial_cuda_backend.cuh" +-#include "ntt/ntt.cuh" ++#include "polynomials/cuda_backend/polynomial_cuda_backend.cu.h" ++#include "ntt/ntt.cu.h" + + using namespace polynomials; + diff --git a/icicle/benchmarks/benches.cu b/icicle/benchmarks/benches.cu deleted file mode 100644 index f7ff8635..00000000 @@ -258,7 +283,7 @@ index cf5f2a24..b2d56744 100644 using namespace field_config; using namespace benchmark; diff --git a/icicle/include/api/babybear.h b/icicle/include/api/babybear.h -index 8bb27f4b..8b060b27 100644 +index 64815ceb..e56ccd6e 100644 --- a/icicle/include/api/babybear.h +++ b/icicle/include/api/babybear.h @@ -8,13 +8,13 @@ @@ -266,181 +291,218 @@ index 8bb27f4b..8b060b27 100644 #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "fields/stark_fields/babybear.cuh" -#include "ntt/ntt.cuh" -#include "vec_ops/vec_ops.cuh" --#include "poseidon/poseidon.cuh" --#include "poseidon/tree/merkle.cuh" -#include "poseidon2/poseidon2.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "fields/stark_fields/babybear.cu.h" +#include "ntt/ntt.cu.h" +#include "vec_ops/vec_ops.cu.h" -+#include "poseidon/poseidon.cu.h" -+#include "poseidon/tree/merkle.cu.h" +#include "poseidon2/poseidon2.cu.h" extern "C" cudaError_t babybear_extension_ntt_cuda( const babybear::extension_t* input, int size, ntt::NTTDir dir, ntt::NTTConfig& config, babybear::extension_t* output); diff --git a/icicle/include/api/bls12_377.h b/icicle/include/api/bls12_377.h -index bf6f8d1a..6c59074e 100644 +index 75a113d2..b8cc01bc 100644 --- a/icicle/include/api/bls12_377.h +++ b/icicle/include/api/bls12_377.h -@@ -8,13 +8,13 @@ +@@ -8,14 +8,14 @@ #define BLS12_377_API_H #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "curves/params/bls12_377.cuh" -#include "ntt/ntt.cuh" -#include "msm/msm.cuh" -#include "vec_ops/vec_ops.cuh" -#include "poseidon/poseidon.cuh" --#include "poseidon/tree/merkle.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "curves/params/bls12_377.cu.h" +#include "ntt/ntt.cu.h" +#include "msm/msm.cu.h" +#include "vec_ops/vec_ops.cu.h" +#include "poseidon/poseidon.cu.h" -+#include "poseidon/tree/merkle.cu.h" extern "C" cudaError_t bls12_377_g2_precompute_msm_bases_cuda( bls12_377::g2_affine_t* bases, diff --git a/icicle/include/api/bls12_381.h b/icicle/include/api/bls12_381.h -index 8972349b..48ee4a97 100644 +index 9f1a49cd..3814e01a 100644 --- a/icicle/include/api/bls12_381.h +++ b/icicle/include/api/bls12_381.h -@@ -8,13 +8,13 @@ +@@ -8,14 +8,14 @@ #define BLS12_381_API_H #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "curves/params/bls12_381.cuh" -#include "ntt/ntt.cuh" -#include "msm/msm.cuh" -#include "vec_ops/vec_ops.cuh" -#include "poseidon/poseidon.cuh" --#include "poseidon/tree/merkle.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "curves/params/bls12_381.cu.h" +#include "ntt/ntt.cu.h" +#include "msm/msm.cu.h" +#include "vec_ops/vec_ops.cu.h" +#include "poseidon/poseidon.cu.h" -+#include "poseidon/tree/merkle.cu.h" extern "C" cudaError_t bls12_381_g2_precompute_msm_bases_cuda( bls12_381::g2_affine_t* bases, diff --git a/icicle/include/api/bn254.h b/icicle/include/api/bn254.h -index 20d8e6d1..160a120a 100644 +index 1a9a46b5..16ce4cb4 100644 --- a/icicle/include/api/bn254.h +++ b/icicle/include/api/bn254.h -@@ -8,14 +8,14 @@ +@@ -8,15 +8,15 @@ #define BN254_API_H #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "curves/params/bn254.cuh" -#include "ntt/ntt.cuh" -#include "msm/msm.cuh" -#include "vec_ops/vec_ops.cuh" -#include "poseidon/poseidon.cuh" --#include "poseidon/tree/merkle.cuh" -#include "poseidon2/poseidon2.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "curves/params/bn254.cu.h" +#include "ntt/ntt.cu.h" +#include "msm/msm.cu.h" +#include "vec_ops/vec_ops.cu.h" +#include "poseidon/poseidon.cu.h" -+#include "poseidon/tree/merkle.cu.h" +#include "poseidon2/poseidon2.cu.h" extern "C" cudaError_t bn254_g2_precompute_msm_bases_cuda( bn254::g2_affine_t* bases, diff --git a/icicle/include/api/bw6_761.h b/icicle/include/api/bw6_761.h -index 8d072218..9e7b0b3b 100644 +index 531e4514..e311c1cd 100644 --- a/icicle/include/api/bw6_761.h +++ b/icicle/include/api/bw6_761.h -@@ -8,13 +8,13 @@ +@@ -8,14 +8,14 @@ #define BW6_761_API_H #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "curves/params/bw6_761.cuh" -#include "ntt/ntt.cuh" -#include "msm/msm.cuh" -#include "vec_ops/vec_ops.cuh" -#include "poseidon/poseidon.cuh" --#include "poseidon/tree/merkle.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "curves/params/bw6_761.cu.h" +#include "ntt/ntt.cu.h" +#include "msm/msm.cu.h" +#include "vec_ops/vec_ops.cu.h" +#include "poseidon/poseidon.cu.h" -+#include "poseidon/tree/merkle.cu.h" extern "C" cudaError_t bw6_761_g2_precompute_msm_bases_cuda( bw6_761::g2_affine_t* bases, diff --git a/icicle/include/api/grumpkin.h b/icicle/include/api/grumpkin.h -index 9caaddb1..bb8eecd0 100644 +index 40241e59..c9b302f4 100644 --- a/icicle/include/api/grumpkin.h +++ b/icicle/include/api/grumpkin.h -@@ -8,12 +8,12 @@ +@@ -8,13 +8,13 @@ #define GRUMPKIN_API_H #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "curves/params/grumpkin.cuh" -#include "msm/msm.cuh" -#include "vec_ops/vec_ops.cuh" -#include "poseidon/poseidon.cuh" --#include "poseidon/tree/merkle.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "curves/params/grumpkin.cu.h" +#include "msm/msm.cu.h" +#include "vec_ops/vec_ops.cu.h" +#include "poseidon/poseidon.cu.h" -+#include "poseidon/tree/merkle.cu.h" extern "C" cudaError_t grumpkin_precompute_msm_bases_cuda( grumpkin::affine_t* bases, diff --git a/icicle/include/api/hash.h b/icicle/include/api/hash.h -index a85e1b6c..46323618 100644 +index ffa40d81..2c9436e2 100644 --- a/icicle/include/api/hash.h +++ b/icicle/include/api/hash.h -@@ -4,8 +4,8 @@ +@@ -4,9 +4,9 @@ #define HASH_API_H #include -#include "gpu-utils/device_context.cuh" -#include "hash/keccak/keccak.cuh" +-#include "merkle-tree/merkle.cuh" +#include "gpu-utils/device_context.cu.h" +#include "hash/keccak/keccak.cu.h" ++#include "merkle-tree/merkle.cu.h" extern "C" cudaError_t - keccak256_cuda(uint8_t* input, int input_block_size, int number_of_blocks, uint8_t* output, keccak::KeccakConfig& config); + keccak256_cuda(uint8_t* input, int input_block_size, int number_of_blocks, uint8_t* output, keccak::HashConfig& config); +diff --git a/icicle/include/api/m31.h b/icicle/include/api/m31.h +index 277268a1..19879db0 100644 +--- a/icicle/include/api/m31.h ++++ b/icicle/include/api/m31.h +@@ -8,11 +8,11 @@ + #define M31_API_H + + #include +-#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" +-#include "fields/stark_fields/m31.cuh" +-#include "vec_ops/vec_ops.cuh" ++#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" ++#include "fields/stark_fields/m31.cu.h" ++#include "vec_ops/vec_ops.cu.h" + + extern "C" cudaError_t m31_build_merkle_tree( + const m31::scalar_t* leaves, diff --git a/icicle/include/api/stark252.h b/icicle/include/api/stark252.h -index f6248af7..197aa39f 100644 +index 867f98e6..24f8dbd5 100644 --- a/icicle/include/api/stark252.h +++ b/icicle/include/api/stark252.h -@@ -8,10 +8,10 @@ +@@ -8,12 +8,12 @@ #define STARK252_API_H #include -#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" -#include "fields/stark_fields/stark252.cuh" -#include "ntt/ntt.cuh" -#include "vec_ops/vec_ops.cuh" +#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" +#include "fields/stark_fields/stark252.cu.h" +#include "ntt/ntt.cu.h" +#include "vec_ops/vec_ops.cu.h" - extern "C" cudaError_t stark252_mul_cuda( - stark252::scalar_t* vec_a, stark252::scalar_t* vec_b, int n, vec_ops::VecOpsConfig& config, stark252::scalar_t* result); + extern "C" cudaError_t stark252_build_merkle_tree( + const stark252::scalar_t* leaves, diff --git a/icicle/include/curves/affine.cuh b/icicle/include/curves/affine.cu.h similarity index 93% rename from icicle/include/curves/affine.cuh @@ -653,7 +715,7 @@ diff --git a/icicle/include/fields/field.cuh b/icicle/include/fields/field.cu.h similarity index 99% rename from icicle/include/fields/field.cuh rename to icicle/include/fields/field.cu.h -index 52a39128..39f13b07 100644 +index 6fe3e069..fbde1b33 100644 --- a/icicle/include/fields/field.cuh +++ b/icicle/include/fields/field.cu.h @@ -18,12 +18,12 @@ @@ -679,7 +741,7 @@ diff --git a/icicle/include/fields/field_config.cuh b/icicle/include/fields/fiel similarity index 65% rename from icicle/include/fields/field_config.cuh rename to icicle/include/fields/field_config.cu.h -index 51729b64..9aca1da8 100644 +index d9ec18c0..72e1250e 100644 --- a/icicle/include/fields/field_config.cuh +++ b/icicle/include/fields/field_config.cu.h @@ -3,7 +3,7 @@ @@ -691,7 +753,7 @@ index 51729b64..9aca1da8 100644 /** * @namespace field_config -@@ -11,27 +11,27 @@ +@@ -11,30 +11,30 @@ * with the `-DFIELD` env variable passed during build. */ #if FIELD_ID == BN254 @@ -724,24 +786,48 @@ index 51729b64..9aca1da8 100644 -#include "fields/stark_fields/stark252.cuh" +#include "fields/stark_fields/stark252.cu.h" namespace field_config = stark252; + #elif FIELD_ID == M31 +-#include "fields/stark_fields/m31.cuh" ++#include "fields/stark_fields/m31.cu.h" + namespace field_config = m31; #endif diff --git a/icicle/include/fields/host_math.cuh b/icicle/include/fields/host_math.cu.h similarity index 98% rename from icicle/include/fields/host_math.cuh rename to icicle/include/fields/host_math.cu.h -index 47a712fd..deb59e3d 100644 +index 0cdde0ca..7669fd42 100644 --- a/icicle/include/fields/host_math.cuh +++ b/icicle/include/fields/host_math.cu.h -@@ -4,7 +4,7 @@ +@@ -4,8 +4,8 @@ #include #include -#include "gpu-utils/modifiers.cuh" +-#include "storage.cuh" +#include "gpu-utils/modifiers.cu.h" ++#include "storage.cu.h" + namespace host_math { - // return x + y with uint32_t operands +diff --git a/icicle/include/fields/params_gen.cuh b/icicle/include/fields/params_gen.cu.h +similarity index 99% +rename from icicle/include/fields/params_gen.cuh +rename to icicle/include/fields/params_gen.cu.h +index 6b21c5e0..638a9391 100644 +--- a/icicle/include/fields/params_gen.cuh ++++ b/icicle/include/fields/params_gen.cu.h +@@ -2,8 +2,8 @@ + #ifndef PARAMS_GEN_H + #define PARAMS_GEN_H + +-#include "storage.cuh" +-#include "host_math.cuh" ++#include "storage.cu.h" ++#include "host_math.cu.h" + + namespace params_gen { + template diff --git a/icicle/include/fields/ptx.cuh b/icicle/include/fields/ptx.cu.h similarity index 100% rename from icicle/include/fields/ptx.cuh @@ -750,7 +836,7 @@ diff --git a/icicle/include/fields/quadratic_extension.cuh b/icicle/include/fiel similarity index 98% rename from icicle/include/fields/quadratic_extension.cuh rename to icicle/include/fields/quadratic_extension.cu.h -index 36249a39..493047f7 100644 +index 10065386..6c4f0dff 100644 --- a/icicle/include/fields/quadratic_extension.cuh +++ b/icicle/include/fields/quadratic_extension.cu.h @@ -1,8 +1,8 @@ @@ -763,13 +849,13 @@ index 36249a39..493047f7 100644 +#include "gpu-utils/modifiers.cu.h" +#include "gpu-utils/sharedmem.cu.h" - template + template class ExtensionField diff --git a/icicle/include/fields/quartic_extension.cuh b/icicle/include/fields/quartic_extension.cu.h similarity index 99% rename from icicle/include/fields/quartic_extension.cuh rename to icicle/include/fields/quartic_extension.cu.h -index 1f73adc8..c9b1f276 100644 +index 8fead58c..609ecf36 100644 --- a/icicle/include/fields/quartic_extension.cuh +++ b/icicle/include/fields/quartic_extension.cu.h @@ -1,8 +1,8 @@ @@ -782,29 +868,31 @@ index 1f73adc8..c9b1f276 100644 +#include "gpu-utils/modifiers.cu.h" +#include "gpu-utils/sharedmem.cu.h" - template + template class ExtensionField diff --git a/icicle/include/fields/snark_fields/bls12_377_base.cuh b/icicle/include/fields/snark_fields/bls12_377_base.cu.h -similarity index 99% +similarity index 92% rename from icicle/include/fields/snark_fields/bls12_377_base.cuh rename to icicle/include/fields/snark_fields/bls12_377_base.cu.h -index fe9f3df3..bd5e6ad2 100644 +index dc7a13fe..abf9b89f 100644 --- a/icicle/include/fields/snark_fields/bls12_377_base.cuh +++ b/icicle/include/fields/snark_fields/bls12_377_base.cu.h -@@ -2,7 +2,7 @@ +@@ -2,8 +2,8 @@ #ifndef BLS12_377_BASE_PARAMS_H #define BLS12_377_BASE_PARAMS_H -#include "fields/storage.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" ++#include "fields/params_gen.cu.h" namespace bls12_377 { struct fq_config { diff --git a/icicle/include/fields/snark_fields/bls12_377_scalar.cuh b/icicle/include/fields/snark_fields/bls12_377_scalar.cu.h -similarity index 99% +similarity index 86% rename from icicle/include/fields/snark_fields/bls12_377_scalar.cuh rename to icicle/include/fields/snark_fields/bls12_377_scalar.cu.h -index 9fce0370..eb071233 100644 +index 3924f59f..0b0ded12 100644 --- a/icicle/include/fields/snark_fields/bls12_377_scalar.cuh +++ b/icicle/include/fields/snark_fields/bls12_377_scalar.cu.h @@ -2,9 +2,9 @@ @@ -813,34 +901,36 @@ index 9fce0370..eb071233 100644 -#include "fields/storage.cuh" -#include "fields/field.cuh" --#include "fields/quadratic_extension.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" +#include "fields/field.cu.h" -+#include "fields/quadratic_extension.cu.h" ++#include "fields/params_gen.cu.h" namespace bls12_377 { struct fp_config { diff --git a/icicle/include/fields/snark_fields/bls12_381_base.cuh b/icicle/include/fields/snark_fields/bls12_381_base.cu.h -similarity index 99% +similarity index 89% rename from icicle/include/fields/snark_fields/bls12_381_base.cuh rename to icicle/include/fields/snark_fields/bls12_381_base.cu.h -index 8b0a8bbb..b0bdc2dc 100644 +index 54fc0ab5..cef89c40 100644 --- a/icicle/include/fields/snark_fields/bls12_381_base.cuh +++ b/icicle/include/fields/snark_fields/bls12_381_base.cu.h -@@ -2,7 +2,7 @@ +@@ -2,8 +2,8 @@ #ifndef BLS12_381_BASE_PARAMS_H #define BLS12_381_BASE_PARAMS_H -#include "fields/storage.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" ++#include "fields/params_gen.cu.h" namespace bls12_381 { struct fq_config { diff --git a/icicle/include/fields/snark_fields/bls12_381_scalar.cuh b/icicle/include/fields/snark_fields/bls12_381_scalar.cu.h -similarity index 99% +similarity index 87% rename from icicle/include/fields/snark_fields/bls12_381_scalar.cuh rename to icicle/include/fields/snark_fields/bls12_381_scalar.cu.h -index 3e6e1508..dac4fbc8 100644 +index c6bd12fe..61e9f8bc 100644 --- a/icicle/include/fields/snark_fields/bls12_381_scalar.cuh +++ b/icicle/include/fields/snark_fields/bls12_381_scalar.cu.h @@ -2,9 +2,9 @@ @@ -849,34 +939,36 @@ index 3e6e1508..dac4fbc8 100644 -#include "fields/storage.cuh" -#include "fields/field.cuh" --#include "fields/quadratic_extension.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" +#include "fields/field.cu.h" -+#include "fields/quadratic_extension.cu.h" ++#include "fields/params_gen.cu.h" namespace bls12_381 { struct fp_config { diff --git a/icicle/include/fields/snark_fields/bn254_base.cuh b/icicle/include/fields/snark_fields/bn254_base.cu.h -similarity index 99% +similarity index 89% rename from icicle/include/fields/snark_fields/bn254_base.cuh rename to icicle/include/fields/snark_fields/bn254_base.cu.h -index 891b91e8..4c1234fb 100644 +index 95f18ed3..da993cbd 100644 --- a/icicle/include/fields/snark_fields/bn254_base.cuh +++ b/icicle/include/fields/snark_fields/bn254_base.cu.h -@@ -2,7 +2,7 @@ +@@ -2,8 +2,8 @@ #ifndef BN254_BASE_PARAMS_H #define BN254_BASE_PARAMS_H -#include "fields/storage.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" ++#include "fields/params_gen.cu.h" namespace bn254 { struct fq_config { diff --git a/icicle/include/fields/snark_fields/bn254_scalar.cuh b/icicle/include/fields/snark_fields/bn254_scalar.cu.h -similarity index 99% +similarity index 87% rename from icicle/include/fields/snark_fields/bn254_scalar.cuh rename to icicle/include/fields/snark_fields/bn254_scalar.cu.h -index a5c9b00c..9bb9cba1 100644 +index 67478e2d..8911357c 100644 --- a/icicle/include/fields/snark_fields/bn254_scalar.cuh +++ b/icicle/include/fields/snark_fields/bn254_scalar.cu.h @@ -2,9 +2,9 @@ @@ -885,47 +977,47 @@ index a5c9b00c..9bb9cba1 100644 -#include "fields/storage.cuh" -#include "fields/field.cuh" --#include "fields/quadratic_extension.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" +#include "fields/field.cu.h" -+#include "fields/quadratic_extension.cu.h" ++#include "fields/params_gen.cu.h" namespace bn254 { struct fp_config { diff --git a/icicle/include/fields/snark_fields/bw6_761_base.cuh b/icicle/include/fields/snark_fields/bw6_761_base.cu.h -similarity index 99% +similarity index 89% rename from icicle/include/fields/snark_fields/bw6_761_base.cuh rename to icicle/include/fields/snark_fields/bw6_761_base.cu.h -index 24615786..ddbd4391 100644 +index 4ec110ff..6ab52e19 100644 --- a/icicle/include/fields/snark_fields/bw6_761_base.cuh +++ b/icicle/include/fields/snark_fields/bw6_761_base.cu.h -@@ -2,7 +2,7 @@ +@@ -2,8 +2,8 @@ #ifndef BW6_761_BASE_BASE_H #define BW6_761_BASE_BASE_H -#include "fields/storage.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" ++#include "fields/params_gen.cu.h" namespace bw6_761 { struct fq_config { diff --git a/icicle/include/fields/snark_fields/bw6_761_scalar.cuh b/icicle/include/fields/snark_fields/bw6_761_scalar.cu.h -similarity index 61% +similarity index 68% rename from icicle/include/fields/snark_fields/bw6_761_scalar.cuh rename to icicle/include/fields/snark_fields/bw6_761_scalar.cu.h -index f5fc5dad..a1d180b9 100644 +index 304595a7..cb9205ab 100644 --- a/icicle/include/fields/snark_fields/bw6_761_scalar.cuh +++ b/icicle/include/fields/snark_fields/bw6_761_scalar.cu.h -@@ -2,10 +2,10 @@ +@@ -2,9 +2,9 @@ #ifndef BW6_761_SCALAR_PARAMS_H #define BW6_761_SCALAR_PARAMS_H -#include "fields/storage.cuh" -#include "fields/field.cuh" --#include "fields/quadratic_extension.cuh" -#include "fields/snark_fields/bls12_377_base.cuh" +#include "fields/storage.cu.h" +#include "fields/field.cu.h" -+#include "fields/quadratic_extension.cu.h" +#include "fields/snark_fields/bls12_377_base.cu.h" namespace bw6_761 { @@ -969,38 +1061,61 @@ index 4354c8de..87287e1b 100644 namespace grumpkin { typedef bn254::fq_config fp_config; diff --git a/icicle/include/fields/stark_fields/babybear.cuh b/icicle/include/fields/stark_fields/babybear.cu.h -similarity index 96% +similarity index 83% rename from icicle/include/fields/stark_fields/babybear.cuh rename to icicle/include/fields/stark_fields/babybear.cu.h -index ad3db0e2..6b17c285 100644 +index 57f3f06e..76ea9691 100644 --- a/icicle/include/fields/stark_fields/babybear.cuh +++ b/icicle/include/fields/stark_fields/babybear.cu.h -@@ -1,8 +1,8 @@ +@@ -1,9 +1,9 @@ #pragma once -#include "fields/storage.cuh" -#include "fields/field.cuh" -#include "fields/quartic_extension.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" +#include "fields/field.cu.h" +#include "fields/quartic_extension.cu.h" ++#include "fields/params_gen.cu.h" namespace babybear { struct fp_config { +diff --git a/icicle/include/fields/stark_fields/m31.cuh b/icicle/include/fields/stark_fields/m31.cu.h +similarity index 98% +rename from icicle/include/fields/stark_fields/m31.cuh +rename to icicle/include/fields/stark_fields/m31.cu.h +index b45592ce..b7fe5e5d 100644 +--- a/icicle/include/fields/stark_fields/m31.cuh ++++ b/icicle/include/fields/stark_fields/m31.cu.h +@@ -1,8 +1,8 @@ + #pragma once + +-#include "fields/storage.cuh" +-#include "fields/field.cuh" +-#include "fields/quartic_extension.cuh" ++#include "fields/storage.cu.h" ++#include "fields/field.cu.h" ++#include "fields/quartic_extension.cu.h" + + namespace m31 { + template diff --git a/icicle/include/fields/stark_fields/stark252.cuh b/icicle/include/fields/stark_fields/stark252.cu.h -similarity index 99% +similarity index 85% rename from icicle/include/fields/stark_fields/stark252.cuh rename to icicle/include/fields/stark_fields/stark252.cu.h -index 6557ea0c..0d38e0b5 100644 +index d61f757e..6e45386c 100644 --- a/icicle/include/fields/stark_fields/stark252.cuh +++ b/icicle/include/fields/stark_fields/stark252.cu.h -@@ -1,7 +1,7 @@ +@@ -1,8 +1,8 @@ #pragma once -#include "fields/storage.cuh" -#include "fields/field.cuh" +-#include "fields/params_gen.cuh" +#include "fields/storage.cu.h" +#include "fields/field.cu.h" ++#include "fields/params_gen.cu.h" // modulus = 3618502788666131213697322783095070105623107215331596699973092056135872020481 (2^251+17*2^192+1) namespace stark252 { @@ -1009,9 +1124,21 @@ similarity index 100% rename from icicle/include/fields/storage.cuh rename to icicle/include/fields/storage.cu.h diff --git a/icicle/include/gpu-utils/device_context.cuh b/icicle/include/gpu-utils/device_context.cu.h -similarity index 100% +similarity index 97% rename from icicle/include/gpu-utils/device_context.cuh rename to icicle/include/gpu-utils/device_context.cu.h +index b1448a5a..31658426 100644 +--- a/icicle/include/gpu-utils/device_context.cuh ++++ b/icicle/include/gpu-utils/device_context.cu.h +@@ -3,7 +3,7 @@ + #define DEVICE_CONTEXT_H + + #include +-#include "gpu-utils/error_handler.cuh" ++#include "gpu-utils/error_handler.cu.h" + + namespace device_context { + diff --git a/icicle/include/gpu-utils/error_handler.cuh b/icicle/include/gpu-utils/error_handler.cu.h similarity index 100% rename from icicle/include/gpu-utils/error_handler.cuh @@ -1024,14 +1151,34 @@ diff --git a/icicle/include/gpu-utils/sharedmem.cuh b/icicle/include/gpu-utils/s similarity index 100% rename from icicle/include/gpu-utils/sharedmem.cuh rename to icicle/include/gpu-utils/sharedmem.cu.h +diff --git a/icicle/include/hash/hash.cuh b/icicle/include/hash/hash.cu.h +similarity index 98% +rename from icicle/include/hash/hash.cuh +rename to icicle/include/hash/hash.cu.h +index 338d337d..7ca414cc 100644 +--- a/icicle/include/hash/hash.cuh ++++ b/icicle/include/hash/hash.cu.h +@@ -2,9 +2,9 @@ + #ifndef HASH_H + #define HASH_H + +-#include "gpu-utils/device_context.cuh" +-#include "gpu-utils/error_handler.cuh" +-#include "matrix/matrix.cuh" ++#include "gpu-utils/device_context.cu.h" ++#include "gpu-utils/error_handler.cu.h" ++#include "matrix/matrix.cu.h" + #include + + using matrix::Matrix; diff --git a/icicle/include/hash/keccak/keccak.cuh b/icicle/include/hash/keccak/keccak.cu.h -similarity index 96% +similarity index 87% rename from icicle/include/hash/keccak/keccak.cuh rename to icicle/include/hash/keccak/keccak.cu.h -index 251ace3b..47dbca8d 100644 +index d95c22a4..eee2f806 100644 --- a/icicle/include/hash/keccak/keccak.cuh +++ b/icicle/include/hash/keccak/keccak.cu.h -@@ -3,8 +3,8 @@ +@@ -3,10 +3,10 @@ #define KECCAK_H #include @@ -1040,8 +1187,38 @@ index 251ace3b..47dbca8d 100644 +#include "gpu-utils/device_context.cu.h" +#include "gpu-utils/error_handler.cu.h" - namespace keccak { - /** +-#include "hash/hash.cuh" ++#include "hash/hash.cu.h" + + using namespace hash; + +diff --git a/icicle/include/matrix/matrix.cuh b/icicle/include/matrix/matrix.cu.h +similarity index 100% +rename from icicle/include/matrix/matrix.cuh +rename to icicle/include/matrix/matrix.cu.h +diff --git a/icicle/include/merkle-tree/merkle.cuh b/icicle/include/merkle-tree/merkle.cu.h +similarity index 96% +rename from icicle/include/merkle-tree/merkle.cuh +rename to icicle/include/merkle-tree/merkle.cu.h +index ba50ff76..d0a020db 100644 +--- a/icicle/include/merkle-tree/merkle.cuh ++++ b/icicle/include/merkle-tree/merkle.cu.h +@@ -2,11 +2,11 @@ + #ifndef MERKLE_H + #define MERKLE_H + +-#include "gpu-utils/device_context.cuh" +-#include "gpu-utils/error_handler.cuh" ++#include "gpu-utils/device_context.cu.h" ++#include "gpu-utils/error_handler.cu.h" + #include "utils/utils.h" +-#include "hash/hash.cuh" +-#include "matrix/matrix.cuh" ++#include "hash/hash.cu.h" ++#include "matrix/matrix.cu.h" + + #include + #include diff --git a/icicle/include/msm/msm.cuh b/icicle/include/msm/msm.cu.h similarity index 98% rename from icicle/include/msm/msm.cuh @@ -1134,14 +1311,36 @@ index 7916b837..f61f794f 100644 #include "polynomial_context.h" #include "polynomial_backend.h" -diff --git a/icicle/include/poseidon/poseidon.cuh b/icicle/include/poseidon/poseidon.cu.h +diff --git a/icicle/include/poseidon/constants.cuh b/icicle/include/poseidon/constants.cu.h +similarity index 100% +rename from icicle/include/poseidon/constants.cuh +rename to icicle/include/poseidon/constants.cu.h +diff --git a/icicle/include/poseidon/kernels.cuh b/icicle/include/poseidon/kernels.cu.h similarity index 98% +rename from icicle/include/poseidon/kernels.cuh +rename to icicle/include/poseidon/kernels.cu.h +index 4afa9878..b9ca6972 100644 +--- a/icicle/include/poseidon/kernels.cuh ++++ b/icicle/include/poseidon/kernels.cu.h +@@ -2,8 +2,8 @@ + #ifndef POSEIDON_KERNELS_H + #define POSEIDON_KERNELS_H + +-#include "gpu-utils/modifiers.cuh" +-#include "poseidon/constants.cuh" ++#include "gpu-utils/modifiers.cu.h" ++#include "poseidon/constants.cu.h" + + namespace poseidon { + template +diff --git a/icicle/include/poseidon/poseidon.cuh b/icicle/include/poseidon/poseidon.cu.h +similarity index 94% rename from icicle/include/poseidon/poseidon.cuh rename to icicle/include/poseidon/poseidon.cu.h -index 9dd3beea..7fab8512 100644 +index 24727c21..3b1d6b3b 100644 --- a/icicle/include/poseidon/poseidon.cuh +++ b/icicle/include/poseidon/poseidon.cu.h -@@ -4,8 +4,8 @@ +@@ -4,13 +4,13 @@ #include #include @@ -1151,36 +1350,61 @@ index 9dd3beea..7fab8512 100644 +#include "gpu-utils/error_handler.cu.h" #include "utils/utils.h" +-#include "poseidon/kernels.cuh" +-#include "poseidon/constants.cuh" +-#include "hash/hash.cuh" ++#include "poseidon/kernels.cu.h" ++#include "poseidon/constants.cu.h" ++#include "hash/hash.cu.h" + using namespace hash; + /** -diff --git a/icicle/include/poseidon/tree/merkle.cuh b/icicle/include/poseidon/tree/merkle.cu.h -similarity index 95% -rename from icicle/include/poseidon/tree/merkle.cuh -rename to icicle/include/poseidon/tree/merkle.cu.h -index 73405d36..86db832c 100644 ---- a/icicle/include/poseidon/tree/merkle.cuh -+++ b/icicle/include/poseidon/tree/merkle.cu.h -@@ -2,10 +2,10 @@ - #ifndef MERKLE_H - #define MERKLE_H +diff --git a/icicle/include/poseidon2/constants.cuh b/icicle/include/poseidon2/constants.cu.h +similarity index 97% +rename from icicle/include/poseidon2/constants.cuh +rename to icicle/include/poseidon2/constants.cu.h +index 35bc2ebf..5b20ef6b 100644 +--- a/icicle/include/poseidon2/constants.cuh ++++ b/icicle/include/poseidon2/constants.cu.h +@@ -2,7 +2,7 @@ + #ifndef POSEIDON2_CONSTANTS_H + #define POSEIDON2_CONSTANTS_H -#include "gpu-utils/device_context.cuh" --#include "gpu-utils/error_handler.cuh" +#include "gpu-utils/device_context.cu.h" -+#include "gpu-utils/error_handler.cu.h" + + namespace poseidon2 { + /** +diff --git a/icicle/include/poseidon2/kernels.cuh b/icicle/include/poseidon2/kernels.cu.h +similarity index 98% +rename from icicle/include/poseidon2/kernels.cuh +rename to icicle/include/poseidon2/kernels.cu.h +index 5f34be92..409098fc 100644 +--- a/icicle/include/poseidon2/kernels.cuh ++++ b/icicle/include/poseidon2/kernels.cu.h +@@ -3,10 +3,10 @@ + #define POSEIDON2_KERNELS_H + #include "utils/utils.h" --#include "poseidon/poseidon.cuh" -+#include "poseidon/poseidon.cu.h" +-#include "hash/hash.cuh" +-#include "matrix/matrix.cuh" +-#include "poseidon2/constants.cuh" +-#include "gpu-utils/modifiers.cuh" ++#include "hash/hash.cu.h" ++#include "matrix/matrix.cu.h" ++#include "poseidon2/constants.cu.h" ++#include "gpu-utils/modifiers.cu.h" + + using matrix::Matrix; - #include - #include diff --git a/icicle/include/poseidon2/poseidon2.cuh b/icicle/include/poseidon2/poseidon2.cu.h -similarity index 98% +similarity index 96% rename from icicle/include/poseidon2/poseidon2.cuh rename to icicle/include/poseidon2/poseidon2.cu.h -index 9a98a5d9..158bae7f 100644 +index 016740e7..c85e7d7c 100644 --- a/icicle/include/poseidon2/poseidon2.cuh +++ b/icicle/include/poseidon2/poseidon2.cu.h -@@ -4,8 +4,8 @@ +@@ -4,15 +4,15 @@ #include #include @@ -1190,7 +1414,18 @@ index 9a98a5d9..158bae7f 100644 +#include "gpu-utils/error_handler.cu.h" #include "utils/utils.h" - /** +-#include "hash/hash.cuh" +-#include "matrix/matrix.cuh" ++#include "hash/hash.cu.h" ++#include "matrix/matrix.cu.h" + +-#include "poseidon2/constants.cuh" +-#include "poseidon2/kernels.cuh" ++#include "poseidon2/constants.cu.h" ++#include "poseidon2/kernels.cu.h" + + using matrix::Matrix; + diff --git a/icicle/include/utils/mont.cuh b/icicle/include/utils/mont.cu.h similarity index 100% rename from icicle/include/utils/mont.cuh @@ -1207,7 +1442,7 @@ diff --git a/icicle/include/vec_ops/vec_ops.cuh b/icicle/include/vec_ops/vec_ops similarity index 99% rename from icicle/include/vec_ops/vec_ops.cuh rename to icicle/include/vec_ops/vec_ops.cu.h -index 909c6886..3eca9925 100644 +index ee624a07..00c4aac9 100644 --- a/icicle/include/vec_ops/vec_ops.cuh +++ b/icicle/include/vec_ops/vec_ops.cu.h @@ -2,7 +2,7 @@ @@ -1303,34 +1538,229 @@ index 8bc7bc5e..f8a1c822 100644 extern "C" void CONCAT_EXPAND(FIELD, extension_generate_scalars)(extension_t* scalars, int size) { +diff --git a/icicle/src/hash/keccak/extern.cu b/icicle/src/hash/keccak/extern.cu.cc +similarity index 86% +rename from icicle/src/hash/keccak/extern.cu +rename to icicle/src/hash/keccak/extern.cu.cc +index b1e6d6aa..404ecac5 100644 +--- a/icicle/src/hash/keccak/extern.cu ++++ b/icicle/src/hash/keccak/extern.cu.cc +@@ -1,11 +1,11 @@ + #include "utils/utils.h" +-#include "gpu-utils/error_handler.cuh" ++#include "gpu-utils/error_handler.cu.h" + +-#include "hash/hash.cuh" +-#include "hash/keccak/keccak.cuh" +-#include "keccak.cu" +-#include "../../merkle-tree/merkle.cu" +-#include "merkle-tree/merkle.cuh" ++#include "hash/hash.cu.h" ++#include "hash/keccak/keccak.cu.h" ++#include "keccak.cu.cc" ++#include "../../merkle-tree/merkle.cu.cc" ++#include "merkle-tree/merkle.cu.h" + + namespace keccak { + extern "C" cudaError_t diff --git a/icicle/src/hash/keccak/keccak.cu b/icicle/src/hash/keccak/keccak.cu.cc -similarity index 99% +similarity index 98% rename from icicle/src/hash/keccak/keccak.cu rename to icicle/src/hash/keccak/keccak.cu.cc -index 8655eed5..343e86ea 100644 +index e805bcf6..ca3e52ba 100644 --- a/icicle/src/hash/keccak/keccak.cu +++ b/icicle/src/hash/keccak/keccak.cu.cc -@@ -1,4 +1,4 @@ +@@ -1,10 +1,10 @@ + #include +-#include "gpu-utils/device_context.cuh" +-#include "gpu-utils/error_handler.cuh" +-#include "gpu-utils/modifiers.cuh" ++#include "gpu-utils/device_context.cu.h" ++#include "gpu-utils/error_handler.cu.h" ++#include "gpu-utils/modifiers.cu.h" + +-#include "hash/hash.cuh" -#include "hash/keccak/keccak.cuh" ++#include "hash/hash.cu.h" +#include "hash/keccak/keccak.cu.h" - namespace keccak { - #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) + using namespace hash; + diff --git a/icicle/src/hash/keccak/test.cu b/icicle/src/hash/keccak/test.cu.cc similarity index 96% rename from icicle/src/hash/keccak/test.cu rename to icicle/src/hash/keccak/test.cu.cc -index 8149dc03..26559d93 100644 +index 03293f1e..2aac59a4 100644 --- a/icicle/src/hash/keccak/test.cu +++ b/icicle/src/hash/keccak/test.cu.cc @@ -1,5 +1,5 @@ -#include "gpu-utils/device_context.cuh" --#include "keccak.cu" +-#include "extern.cu" +#include "gpu-utils/device_context.cu.h" -+#include "keccak.cu.cc" ++#include "extern.cu.cc" // #define DEBUG +diff --git a/icicle/src/hash/keccak/test_tree.cu b/icicle/src/hash/keccak/test_tree.cu.cc +similarity index 96% +rename from icicle/src/hash/keccak/test_tree.cu +rename to icicle/src/hash/keccak/test_tree.cu.cc +index ed1de95f..b4efe1c2 100644 +--- a/icicle/src/hash/keccak/test_tree.cu ++++ b/icicle/src/hash/keccak/test_tree.cu.cc +@@ -1,6 +1,6 @@ +-#include "gpu-utils/device_context.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "extern.cu" ++#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "extern.cu.cc" + + #ifndef __CUDA_ARCH__ + #include +diff --git a/icicle/src/merkle-tree/extern.cu b/icicle/src/merkle-tree/extern.cu.cc +similarity index 75% +rename from icicle/src/merkle-tree/extern.cu +rename to icicle/src/merkle-tree/extern.cu.cc +index 060afe87..688a4366 100644 +--- a/icicle/src/merkle-tree/extern.cu ++++ b/icicle/src/merkle-tree/extern.cu.cc +@@ -1,12 +1,12 @@ + #include "utils/utils.h" + +-#include "gpu-utils/error_handler.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "merkle.cu" ++#include "gpu-utils/error_handler.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "merkle.cu.cc" + +-#include "hash/hash.cuh" ++#include "hash/hash.cu.h" + +-#include "fields/field_config.cuh" ++#include "fields/field_config.cu.h" + using namespace field_config; + + namespace merkle_tree { +diff --git a/icicle/src/merkle-tree/extern_mmcs.cu b/icicle/src/merkle-tree/extern_mmcs.cu.cc +similarity index 71% +rename from icicle/src/merkle-tree/extern_mmcs.cu +rename to icicle/src/merkle-tree/extern_mmcs.cu.cc +index 3b6fc5cc..3c2ba192 100644 +--- a/icicle/src/merkle-tree/extern_mmcs.cu ++++ b/icicle/src/merkle-tree/extern_mmcs.cu.cc +@@ -1,13 +1,13 @@ + #include "utils/utils.h" + +-#include "gpu-utils/error_handler.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" +-#include "mmcs.cu" ++#include "gpu-utils/error_handler.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" ++#include "mmcs.cu.cc" + +-#include "hash/hash.cuh" ++#include "hash/hash.cu.h" + +-#include "fields/field_config.cuh" ++#include "fields/field_config.cu.h" + using namespace field_config; + + using matrix::Matrix; +diff --git a/icicle/src/merkle-tree/merkle.cu b/icicle/src/merkle-tree/merkle.cu.cc +similarity index 99% +rename from icicle/src/merkle-tree/merkle.cu +rename to icicle/src/merkle-tree/merkle.cu.cc +index 53f3b8f6..1f783ab3 100644 +--- a/icicle/src/merkle-tree/merkle.cu ++++ b/icicle/src/merkle-tree/merkle.cu.cc +@@ -1,5 +1,5 @@ +-#include "hash/hash.cuh" +-#include "merkle-tree/merkle.cuh" ++#include "hash/hash.cu.h" ++#include "merkle-tree/merkle.cu.h" + + namespace merkle_tree { + /// Constructs merkle subtree without parallelization +diff --git a/icicle/src/merkle-tree/mmcs.cu b/icicle/src/merkle-tree/mmcs.cu.cc +similarity index 99% +rename from icicle/src/merkle-tree/mmcs.cu +rename to icicle/src/merkle-tree/mmcs.cu.cc +index f7acbb65..1d695483 100644 +--- a/icicle/src/merkle-tree/mmcs.cu ++++ b/icicle/src/merkle-tree/mmcs.cu.cc +@@ -1,7 +1,7 @@ +-#include "hash/hash.cuh" +-#include "merkle-tree/merkle.cuh" +-#include "matrix/matrix.cuh" +-#include "vec_ops/vec_ops.cuh" ++#include "hash/hash.cu.h" ++#include "merkle-tree/merkle.cu.h" ++#include "matrix/matrix.cu.h" ++#include "vec_ops/vec_ops.cu.h" + + #include + +diff --git a/icicle/src/merkle-tree/tests/merkle/test.cu b/icicle/src/merkle-tree/tests/merkle/test.cu.cc +similarity index 99% +rename from icicle/src/merkle-tree/tests/merkle/test.cu +rename to icicle/src/merkle-tree/tests/merkle/test.cu.cc +index 0fb34867..92401862 100644 +--- a/icicle/src/merkle-tree/tests/merkle/test.cu ++++ b/icicle/src/merkle-tree/tests/merkle/test.cu.cc +@@ -6,9 +6,9 @@ + #include + + #define DEBUG +-#include "merkle-tree/merkle.cuh" ++#include "merkle-tree/merkle.cu.h" + +-#include "poseidon/poseidon.cuh" ++#include "poseidon/poseidon.cu.h" + + #include "api/bls12_381.h" + using namespace bls12_381; +diff --git a/icicle/src/merkle-tree/tests/merkle/test_poseidon2.cu b/icicle/src/merkle-tree/tests/merkle/test_poseidon2.cu.cc +similarity index 98% +rename from icicle/src/merkle-tree/tests/merkle/test_poseidon2.cu +rename to icicle/src/merkle-tree/tests/merkle/test_poseidon2.cu.cc +index 7bd35c92..c06a1bd2 100644 +--- a/icicle/src/merkle-tree/tests/merkle/test_poseidon2.cu ++++ b/icicle/src/merkle-tree/tests/merkle/test_poseidon2.cu.cc +@@ -5,9 +5,9 @@ + #include + #include + +-#include "merkle-tree/merkle.cuh" ++#include "merkle-tree/merkle.cu.h" + +-#include "poseidon2/poseidon2.cuh" ++#include "poseidon2/poseidon2.cu.h" + + #include "api/babybear.h" + using namespace babybear; +diff --git a/icicle/src/merkle-tree/tests/mmcs/test_poseidon2.cu b/icicle/src/merkle-tree/tests/mmcs/test_poseidon2.cu.cc +similarity index 98% +rename from icicle/src/merkle-tree/tests/mmcs/test_poseidon2.cu +rename to icicle/src/merkle-tree/tests/mmcs/test_poseidon2.cu.cc +index 9de240db..09434e63 100644 +--- a/icicle/src/merkle-tree/tests/mmcs/test_poseidon2.cu ++++ b/icicle/src/merkle-tree/tests/mmcs/test_poseidon2.cu.cc +@@ -5,9 +5,9 @@ + #include + #include + +-#include "merkle-tree/merkle.cuh" ++#include "merkle-tree/merkle.cu.h" + +-#include "poseidon2/poseidon2.cuh" ++#include "poseidon2/poseidon2.cu.h" + + #include "api/babybear.h" + using namespace babybear; diff --git a/icicle/src/msm/extern.cu b/icicle/src/msm/extern.cu.cc similarity index 95% rename from icicle/src/msm/extern.cu @@ -1377,7 +1807,7 @@ diff --git a/icicle/src/msm/msm.cu b/icicle/src/msm/msm.cu.cc similarity index 99% rename from icicle/src/msm/msm.cu rename to icicle/src/msm/msm.cu.cc -index 18a99f21..03389457 100644 +index 7b6f8851..6bb2139a 100644 --- a/icicle/src/msm/msm.cu +++ b/icicle/src/msm/msm.cu.cc @@ -1,4 +1,4 @@ @@ -1529,7 +1959,7 @@ diff --git a/icicle/src/ntt/ntt.cu b/icicle/src/ntt/ntt.cu.cc similarity index 99% rename from icicle/src/ntt/ntt.cu rename to icicle/src/ntt/ntt.cu.cc -index dce454c3..2acd3e89 100644 +index 0afe4a29..a82522a0 100644 --- a/icicle/src/ntt/ntt.cu +++ b/icicle/src/ntt/ntt.cu.cc @@ -1,22 +1,22 @@ @@ -1678,12 +2108,14 @@ diff --git a/icicle/src/poseidon/constants.cu b/icicle/src/poseidon/constants.cu similarity index 98% rename from icicle/src/poseidon/constants.cu rename to icicle/src/poseidon/constants.cu.cc -index 97d67490..8f3bdca2 100644 +index bed5a9be..95a51673 100644 --- a/icicle/src/poseidon/constants.cu +++ b/icicle/src/poseidon/constants.cu.cc -@@ -1,4 +1,4 @@ --#include "poseidon/poseidon.cuh" -+#include "poseidon/poseidon.cu.h" +@@ -1,5 +1,5 @@ +-#include "poseidon/constants.cuh" +-#include "gpu-utils/device_context.cuh" ++#include "poseidon/constants.cu.h" ++#include "gpu-utils/device_context.cu.h" /// These are pre-calculated constants for different curves #include "fields/id.h" @@ -1691,7 +2123,7 @@ diff --git a/icicle/src/poseidon/extern.cu b/icicle/src/poseidon/extern.cu.cc similarity index 92% rename from icicle/src/poseidon/extern.cu rename to icicle/src/poseidon/extern.cu.cc -index 66eac378..4955eda4 100644 +index 16145bb2..b10db1d8 100644 --- a/icicle/src/poseidon/extern.cu +++ b/icicle/src/poseidon/extern.cu.cc @@ -1,11 +1,11 @@ @@ -1700,9 +2132,9 @@ index 66eac378..4955eda4 100644 using namespace field_config; --#include "poseidon.cu" +-#include "poseidon/poseidon.cuh" -#include "constants.cu" -+#include "poseidon.cu.cc" ++#include "poseidon/poseidon.cu.h" +#include "constants.cu.cc" -#include "gpu-utils/device_context.cuh" @@ -1710,49 +2142,14 @@ index 66eac378..4955eda4 100644 #include "utils/utils.h" namespace poseidon { -diff --git a/icicle/src/poseidon/kernels.cu b/icicle/src/poseidon/kernels.cu.cc -similarity index 98% -rename from icicle/src/poseidon/kernels.cu -rename to icicle/src/poseidon/kernels.cu.cc -index 85359292..86665a32 100644 ---- a/icicle/src/poseidon/kernels.cu -+++ b/icicle/src/poseidon/kernels.cu.cc -@@ -1,5 +1,5 @@ --#include "poseidon/poseidon.cuh" --#include "gpu-utils/modifiers.cuh" -+#include "poseidon/poseidon.cu.h" -+#include "gpu-utils/modifiers.cu.h" - - namespace poseidon { - template -diff --git a/icicle/src/poseidon/poseidon.cu b/icicle/src/poseidon/poseidon.cu.cc -similarity index 96% -rename from icicle/src/poseidon/poseidon.cu -rename to icicle/src/poseidon/poseidon.cu.cc -index 835f9124..4d04cf9c 100644 ---- a/icicle/src/poseidon/poseidon.cu -+++ b/icicle/src/poseidon/poseidon.cu.cc -@@ -1,9 +1,9 @@ --#include "fields/field_config.cuh" -+#include "fields/field_config.cu.h" - - using namespace field_config; - --#include "poseidon/poseidon.cuh" --#include "kernels.cu" -+#include "poseidon/poseidon.cu.h" -+#include "kernels.cu.cc" - - namespace poseidon { - template diff --git a/icicle/src/poseidon/test.cu b/icicle/src/poseidon/test.cu.cc similarity index 99% rename from icicle/src/poseidon/test.cu rename to icicle/src/poseidon/test.cu.cc -index 52998415..af4d3853 100644 +index 461b57ae..4384da3d 100644 --- a/icicle/src/poseidon/test.cu +++ b/icicle/src/poseidon/test.cu.cc -@@ -1,10 +1,10 @@ +@@ -1,9 +1,9 @@ // #define DEBUG -#include "curves/curve_config.cuh" @@ -1760,47 +2157,37 @@ index 52998415..af4d3853 100644 using namespace curve_config; -#include "gpu-utils/device_context.cuh" --#include "poseidon.cu" +#include "gpu-utils/device_context.cu.h" -+#include "poseidon.cu.cc" #ifndef __CUDA_ARCH__ #include -diff --git a/icicle/src/poseidon/tree/merkle.cu b/icicle/src/poseidon/tree/merkle.cu.cc -similarity index 99% -rename from icicle/src/poseidon/tree/merkle.cu -rename to icicle/src/poseidon/tree/merkle.cu.cc -index 076da24e..ede67889 100644 ---- a/icicle/src/poseidon/tree/merkle.cu -+++ b/icicle/src/poseidon/tree/merkle.cu.cc -@@ -1,8 +1,8 @@ --#include "fields/field_config.cuh" -+#include "fields/field_config.cu.h" +@@ -14,7 +14,7 @@ using namespace curve_config; + #include "api/bls12_381.h" + using namespace bls12_381; - using namespace field_config; - --#include "poseidon/tree/merkle.cuh" -+#include "poseidon/tree/merkle.cu.h" +-#include "poseidon/poseidon.cuh" ++#include "poseidon/poseidon.cu.h" + using namespace poseidon; - namespace merkle { - /// Flattens the tree digests and sum them up to get -diff --git a/icicle/src/poseidon/tree/test.cu b/icicle/src/poseidon/tree/test.cu.cc -similarity index 99% -rename from icicle/src/poseidon/tree/test.cu -rename to icicle/src/poseidon/tree/test.cu.cc -index e487d56b..0624c797 100644 ---- a/icicle/src/poseidon/tree/test.cu -+++ b/icicle/src/poseidon/tree/test.cu.cc -@@ -1,9 +1,9 @@ + #define A 2 +diff --git a/icicle/src/poseidon/test_m31.cu b/icicle/src/poseidon/test_m31.cu.cc +similarity index 94% +rename from icicle/src/poseidon/test_m31.cu +rename to icicle/src/poseidon/test_m31.cu.cc +index 7f6bd7ce..cbf22de7 100644 +--- a/icicle/src/poseidon/test_m31.cu ++++ b/icicle/src/poseidon/test_m31.cu.cc +@@ -1,10 +1,10 @@ // #define DEBUG - #define MERKLE_DEBUG --#include "curves/curve_config.cuh" --#include "../poseidon.cu" --#include "merkle.cu" -+#include "curves/curve_config.cu.h" -+#include "../poseidon.cu.cc" -+#include "merkle.cu.cc" +-#include "fields/field_config.cuh" ++#include "fields/field_config.cu.h" + using namespace field_config; + +-#include "gpu-utils/device_context.cuh" +-#include "poseidon/poseidon.cuh" ++#include "gpu-utils/device_context.cu.h" ++#include "poseidon/poseidon.cu.h" #ifndef __CUDA_ARCH__ #include @@ -1808,84 +2195,76 @@ diff --git a/icicle/src/poseidon2/constants.cu b/icicle/src/poseidon2/constants. similarity index 98% rename from icicle/src/poseidon2/constants.cu rename to icicle/src/poseidon2/constants.cu.cc -index 7cbe2632..6e7e8e4f 100644 +index 3fa157d2..c30fab04 100644 --- a/icicle/src/poseidon2/constants.cu +++ b/icicle/src/poseidon2/constants.cu.cc -@@ -1,4 +1,4 @@ --#include "poseidon2/poseidon2.cuh" -+#include "poseidon2/poseidon2.cu.h" +@@ -1,5 +1,5 @@ +-#include "poseidon2/constants.cuh" +-#include "gpu-utils/device_context.cuh" ++#include "poseidon2/constants.cu.h" ++#include "gpu-utils/device_context.cu.h" /// These are pre-calculated constants for different curves #include "fields/id.h" diff --git a/icicle/src/poseidon2/extern.cu b/icicle/src/poseidon2/extern.cu.cc -similarity index 96% +similarity index 92% rename from icicle/src/poseidon2/extern.cu rename to icicle/src/poseidon2/extern.cu.cc -index d9eceeb1..f1fddf25 100644 +index 7741d29a..d2397255 100644 --- a/icicle/src/poseidon2/extern.cu +++ b/icicle/src/poseidon2/extern.cu.cc -@@ -1,9 +1,9 @@ +@@ -1,11 +1,11 @@ #include "utils/utils.h" -#include "fields/field_config.cuh" +#include "fields/field_config.cu.h" using namespace field_config; --#include "poseidon.cu" -+#include "poseidon.cu.cc" - - namespace poseidon2 { - extern "C" cudaError_t CONCAT_EXPAND(FIELD, create_poseidon2_constants_cuda)( -diff --git a/icicle/src/poseidon2/kernels.cu b/icicle/src/poseidon2/kernels.cu.cc -similarity index 98% -rename from icicle/src/poseidon2/kernels.cu -rename to icicle/src/poseidon2/kernels.cu.cc -index bf836b43..58cc5d4e 100644 ---- a/icicle/src/poseidon2/kernels.cu -+++ b/icicle/src/poseidon2/kernels.cu.cc -@@ -1,5 +1,5 @@ --#include "poseidon/poseidon.cuh" --#include "gpu-utils/modifiers.cuh" -+#include "poseidon/poseidon.cu.h" -+#include "gpu-utils/modifiers.cu.h" - - namespace poseidon2 { - template -diff --git a/icicle/src/poseidon2/poseidon.cu b/icicle/src/poseidon2/poseidon.cu.cc -similarity index 96% -rename from icicle/src/poseidon2/poseidon.cu -rename to icicle/src/poseidon2/poseidon.cu.cc -index 2ae0512f..803bf6fb 100644 ---- a/icicle/src/poseidon2/poseidon.cu -+++ b/icicle/src/poseidon2/poseidon.cu.cc -@@ -1,6 +1,6 @@ +-#include "gpu-utils/error_handler.cuh" -#include "poseidon2/poseidon2.cuh" --#include "constants.cu" --#include "kernels.cu" +-#include "./constants.cu" ++#include "gpu-utils/error_handler.cu.h" +#include "poseidon2/poseidon2.cu.h" -+#include "constants.cu.cc" -+#include "kernels.cu.cc" ++#include "./constants.cu.cc" namespace poseidon2 { - static int poseidon_block_size = 128; + template class Poseidon2; diff --git a/icicle/src/poseidon2/test.cu b/icicle/src/poseidon2/test.cu.cc similarity index 99% rename from icicle/src/poseidon2/test.cu rename to icicle/src/poseidon2/test.cu.cc -index 1fc7923b..00ca62a8 100644 +index c98bfd63..91e8da8c 100644 --- a/icicle/src/poseidon2/test.cu +++ b/icicle/src/poseidon2/test.cu.cc -@@ -1,7 +1,7 @@ --#include "curves/curve_config.cuh" -+#include "curves/curve_config.cu.h" - using namespace curve_config; +@@ -1,4 +1,4 @@ +-#include "gpu-utils/device_context.cuh" ++#include "gpu-utils/device_context.cu.h" + + #ifndef __CUDA_ARCH__ + #include +@@ -6,7 +6,7 @@ + #include + #include +-#include "poseidon2/poseidon2.cuh" ++#include "poseidon2/poseidon2.cu.h" + using namespace poseidon2; + + #include "api/bn254.h" +diff --git a/icicle/src/poseidon2/test_m31.cu b/icicle/src/poseidon2/test_m31.cu.cc +similarity index 95% +rename from icicle/src/poseidon2/test_m31.cu +rename to icicle/src/poseidon2/test_m31.cu.cc +index 378d281f..ed77de1f 100644 +--- a/icicle/src/poseidon2/test_m31.cu ++++ b/icicle/src/poseidon2/test_m31.cu.cc +@@ -1,4 +1,4 @@ -#include "gpu-utils/device_context.cuh" +#include "gpu-utils/device_context.cu.h" #ifndef __CUDA_ARCH__ #include -@@ -9,7 +9,7 @@ using namespace curve_config; +@@ -6,13 +6,13 @@ #include #include @@ -1893,7 +2272,15 @@ index 1fc7923b..00ca62a8 100644 +#include "poseidon2/poseidon2.cu.h" using namespace poseidon2; - #define T 3 +-#include "fields/field_config.cuh" ++#include "fields/field_config.cu.h" + using namespace field_config; + +-#include "hash/hash.cuh" ++#include "hash/hash.cu.h" + + #define T 16 + diff --git a/icicle/src/vec_ops/extern.cu b/icicle/src/vec_ops/extern.cu.cc similarity index 97% rename from icicle/src/vec_ops/extern.cu @@ -1936,7 +2323,7 @@ diff --git a/icicle/src/vec_ops/vec_ops.cu b/icicle/src/vec_ops/vec_ops.cu.cc similarity index 98% rename from icicle/src/vec_ops/vec_ops.cu rename to icicle/src/vec_ops/vec_ops.cu.cc -index fef581ec..f667c5a2 100644 +index 9cfa85e6..c813dafa 100644 --- a/icicle/src/vec_ops/vec_ops.cu +++ b/icicle/src/vec_ops/vec_ops.cu.cc @@ -1,9 +1,9 @@ @@ -2012,7 +2399,7 @@ diff --git a/icicle/tests/polynomial_test.cu b/icicle/tests/polynomial_test.cu.c similarity index 99% rename from icicle/tests/polynomial_test.cu rename to icicle/tests/polynomial_test.cu.cc -index 219cc1c9..7f1c7034 100644 +index 9b433fa1..55c9942e 100644 --- a/icicle/tests/polynomial_test.cu +++ b/icicle/tests/polynomial_test.cu.cc @@ -5,10 +5,10 @@ diff --git a/third_party/icicle/workspace.bzl b/third_party/icicle/workspace.bzl index 36151c060d..2f3afc68bf 100644 --- a/third_party/icicle/workspace.bzl +++ b/third_party/icicle/workspace.bzl @@ -5,9 +5,9 @@ load("//third_party:repo.bzl", "tachyon_http_archive", "tf_mirror_urls") def repo(): tachyon_http_archive( name = "icicle", - urls = tf_mirror_urls("https://github.com/ingonyama-zk/icicle/archive/4fef5423462a72a37fe66cee89e24eae083cc112.tar.gz"), - sha256 = "ae88dccb706668aa2f323de53734b325f26e9995f8a73559ea1c87daf36c5b06", - strip_prefix = "icicle-4fef5423462a72a37fe66cee89e24eae083cc112", + urls = tf_mirror_urls("https://github.com/ingonyama-zk/icicle/archive/53f34aade57ff2b54a7eb1afadd563c143d4aa69.tar.gz"), + sha256 = "eac59b94592d3409e83b47a414f77d67d80780a5cf1461e62c0d5213c9b10c50", + strip_prefix = "icicle-53f34aade57ff2b54a7eb1afadd563c143d4aa69", patch_file = [ "@kroma_network_tachyon//third_party/icicle:rename.patch", "@kroma_network_tachyon//third_party/icicle:pragma.patch", From 23f3b85293ac7434de4ec6142daa579fb6283e08 Mon Sep 17 00:00:00 2001 From: Noah Kim Date: Tue, 20 Aug 2024 18:14:06 +0900 Subject: [PATCH 18/25] feat(crypto): add `IciclePoseidon2` --- .../sponge/poseidon2/icicle/BUILD.bazel | 55 +++++ .../poseidon2/icicle/icicle_poseidon2.h | 138 ++++++++++++ .../icicle/icicle_poseidon2_baby_bear.cc | 202 ++++++++++++++++++ .../icicle/icicle_poseidon2_baby_bear.h | 33 +++ .../icicle/icicle_poseidon2_bn254.cc | 200 +++++++++++++++++ .../poseidon2/icicle/icicle_poseidon2_bn254.h | 33 +++ third_party/icicle/separate_hash_config.patch | 72 +++++++ third_party/icicle/workspace.bzl | 1 + 8 files changed, 734 insertions(+) create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.cc create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.h create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.cc create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.h create mode 100644 third_party/icicle/separate_hash_config.patch diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel new file mode 100644 index 0000000000..b88d90dcbf --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel @@ -0,0 +1,55 @@ +load("@icicle//:build_defs.bzl", "BABY_BEAR", "BN254", "icicle_defines") +load("@local_config_cuda//cuda:build_defs.bzl", "if_cuda") +load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cuda_library") + +tachyon_cuda_library( + name = "icicle_poseidon2_baby_bear", + srcs = if_cuda(["icicle_poseidon2_baby_bear.cc"]), + hdrs = ["icicle_poseidon2_baby_bear.h"], + force_exceptions = True, + local_defines = icicle_defines(BABY_BEAR), + deps = [ + ":icicle_poseidon2", + "//tachyon/base:openmp_util", + "//tachyon/base/containers:container_util", + "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_vendor", + "//tachyon/device/gpu:gpu_enums", + "//tachyon/device/gpu:gpu_logging", + "@icicle//:poseidon2_baby_bear", + ] + if_cuda([ + "@local_config_cuda//cuda:cudart_static", + ]), +) + +tachyon_cuda_library( + name = "icicle_poseidon2_bn254", + srcs = if_cuda(["icicle_poseidon2_bn254.cc"]), + hdrs = ["icicle_poseidon2_bn254.h"], + force_exceptions = True, + local_defines = icicle_defines(BN254), + deps = [ + ":icicle_poseidon2", + "//tachyon/base:openmp_util", + "//tachyon/base/containers:container_util", + "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_vendor", + "//tachyon/device/gpu:gpu_enums", + "//tachyon/device/gpu:gpu_logging", + "@icicle//:poseidon2_bn254", + ] + if_cuda([ + "@local_config_cuda//cuda:cudart_static", + ]), +) + +tachyon_cc_library( + name = "icicle_poseidon2", + hdrs = ["icicle_poseidon2.h"], + deps = [ + "//tachyon:export", + "//tachyon/base:logging", + "//tachyon/device/gpu:gpu_device_functions", + "//tachyon/math/elliptic_curves/bn/bn254:fr", + "//tachyon/math/finite_fields/baby_bear", + "@com_google_absl//absl/types:span", + "@icicle//:hdrs", + ], +) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h new file mode 100644 index 0000000000..8adb2502b7 --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h @@ -0,0 +1,138 @@ +#ifndef TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_H_ +#define TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_H_ + +#include + +#include "absl/types/span.h" +#include "third_party/icicle/include/hash/hash_config.h" + +#include "tachyon/base/logging.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_vendor.h" +#include "tachyon/device/gpu/gpu_device_functions.h" +#include "tachyon/export.h" +#include "tachyon/math/elliptic_curves/bn/bn254/fr.h" +#include "tachyon/math/finite_fields/baby_bear/baby_bear.h" + +namespace tachyon::crypto { + +template +struct IsIciclePoseidon2SupportedImpl { + constexpr static bool value = false; +}; + +template <> +struct IsIciclePoseidon2SupportedImpl { + constexpr static bool value = true; +}; + +template <> +struct IsIciclePoseidon2SupportedImpl { + constexpr static bool value = true; +}; + +template +constexpr bool IsIciclePoseidon2Supported = + IsIciclePoseidon2SupportedImpl::value; + +struct TACHYON_EXPORT IciclePoseidon2Options { + bool are_inputs_on_device = false; + bool are_outputs_on_device = false; + bool is_async = false; +}; + +template +class IciclePoseidon2 { + public: + IciclePoseidon2( + gpuMemPool_t mem_pool, gpuStream_t stream, + const IciclePoseidon2Options& options = IciclePoseidon2Options()) + : mem_pool_(mem_pool), stream_(stream) { + ::device_context::DeviceContext ctx{stream_, /*device_id=*/0, mem_pool_}; + config_.reset(new ::hash::HashConfig{ + ctx, + options.are_inputs_on_device, + options.are_outputs_on_device, + options.is_async, + }); + VLOG(1) << "IciclePoseidon2 is created"; + } + IciclePoseidon2(const IciclePoseidon2& other) = delete; + IciclePoseidon2& operator=(const IciclePoseidon2& other) = delete; + ~IciclePoseidon2() { CHECK(Delete()); } + + void* impl() { return impl_; } + const void* impl() const { return impl_; } + + [[nodiscard]] bool Create(unsigned int rate, unsigned int width, + unsigned int alpha, unsigned int external_rounds, + unsigned int internal_rounds, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor, + absl::Span round_constants, + absl::Span internal_matrix_diag); + + [[nodiscard]] bool Load(unsigned int rate, unsigned int width, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor); + + [[nodiscard]] bool Hash(unsigned int rate, absl::Span inputs, + absl::Span outputs); + + [[nodiscard]] bool Delete(); + + private: + gpuMemPool_t mem_pool_ = nullptr; + gpuStream_t stream_ = nullptr; + void* impl_ = nullptr; + std::unique_ptr<::hash::HashConfig> config_; +}; + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Create( + unsigned int rate, unsigned int width, unsigned int alpha, + unsigned int external_rounds, unsigned int internal_rounds, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor, + absl::Span round_constants, + absl::Span internal_matrix_diag); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Load( + unsigned int rate, unsigned int width, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Hash( + unsigned int rate, absl::Span inputs, + absl::Span outputs); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Delete(); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Create( + unsigned int rate, unsigned int width, unsigned int alpha, + unsigned int external_rounds, unsigned int internal_rounds, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor, + absl::Span round_constants, + absl::Span internal_matrix_diag); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Load( + unsigned int rate, unsigned int width, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Hash( + unsigned int rate, absl::Span inputs, + absl::Span outputs); + +template <> +TACHYON_EXPORT bool IciclePoseidon2::Delete(); + +} // namespace tachyon::crypto + +#endif // TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_H_ diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.cc b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.cc new file mode 100644 index 0000000000..8cdec24b61 --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.cc @@ -0,0 +1,202 @@ +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.h" + +#include +#include + +#include "third_party/icicle/include/fields/id.h" +#include "third_party/icicle/include/gpu-utils/error_handler.cu.h" +#include "third_party/icicle/src/poseidon2/constants.cu.cc" // NOLINT(build/include) + +#include "tachyon/base/containers/container_util.h" +#include "tachyon/base/openmp_util.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h" +#include "tachyon/device/gpu/gpu_enums.h" +#include "tachyon/device/gpu/gpu_logging.h" + +gpuError_t tachyon_babybear_poseidon2_create_cuda( + unsigned int width, unsigned int rate, unsigned int alpha, + unsigned int internal_rounds, unsigned int external_rounds, + const ::babybear::scalar_t* round_constants, + const ::babybear::scalar_t* internal_matrix_diag, + ::poseidon2::MdsType mds_type, ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2<::babybear::scalar_t>** poseidon) { + try { + *poseidon = new ::poseidon2::Poseidon2<::babybear::scalar_t>( + width, rate, alpha, internal_rounds, external_rounds, round_constants, + internal_matrix_diag, mds_type, diffusion, ctx); + return gpuSuccess; + } catch (const ::IcicleError& error) { + LOG(ERROR) << "Failed tachyon_babybear_poseidon2_create_cuda(): " + << error.what(); + return cudaErrorUnknown; + } +} + +gpuError_t tachyon_babybear_poseidon2_load_cuda( + unsigned int width, unsigned int rate, ::poseidon2::MdsType mds_type, + ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2<::babybear::scalar_t>** poseidon) { + try { + *poseidon = new ::poseidon2::Poseidon2<::babybear::scalar_t>( + width, rate, mds_type, diffusion, ctx); + return gpuSuccess; + } catch (const IcicleError& error) { + LOG(ERROR) << "Failed tachyon_babybear_poseidon2_load_cuda(): " + << error.what(); + return cudaErrorUnknown; + } +} + +gpuError_t tachyon_babybear_poseidon2_hash_many_cuda( + const ::poseidon2::Poseidon2<::babybear::scalar_t>* poseidon, + const ::babybear::scalar_t* inputs, ::babybear::scalar_t* output, + unsigned int number_of_states, unsigned int input_block_len, + unsigned int output_len, ::hash::HashConfig& cfg) { + return poseidon->hash_many(inputs, output, number_of_states, input_block_len, + output_len, cfg); +} + +gpuError_t tachyon_babybear_poseidon2_delete_cuda( + ::poseidon2::Poseidon2<::babybear::scalar_t>* poseidon) { + try { + delete poseidon; + return gpuSuccess; + } catch (const IcicleError& error) { + LOG(ERROR) << "Failed tachyon_babybear_poseidon2_delete_cuda(): " + << error.what(); + return cudaErrorUnknown; + } +} + +namespace tachyon::crypto { + +template <> +bool IciclePoseidon2::Create( + unsigned int rate, unsigned int width, unsigned int alpha, + unsigned int external_rounds, unsigned int internal_rounds, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor, + absl::Span round_constants, + absl::Span internal_matrix_diag) { +#if FIELD_ID != BABY_BEAR +#error Only BABY_BEAR is supported +#endif + if (impl_ != nullptr) { + VLOG(1) << "IciclePoseidon2 was already initialized"; + return true; + } + + std::vector<::babybear::scalar_t> round_constants_tmp = + base::Map(round_constants, [](const math::BabyBear& f) { + return ::babybear::scalar_t::from_montgomery( + reinterpret_cast(f)); + }); + std::vector<::babybear::scalar_t> internal_matrix_diag_tmp = + base::Map(internal_matrix_diag, [](const math::BabyBear& f) { + return ::babybear::scalar_t::from_montgomery( + reinterpret_cast(f)); + }); + ::poseidon2::Poseidon2<::babybear::scalar_t>* icicle_poseidon = nullptr; + gpuError_t error = tachyon_babybear_poseidon2_create_cuda( + width, rate, alpha, internal_rounds, external_rounds, + round_constants_tmp.data(), internal_matrix_diag_tmp.data(), + external_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_MDS + : ::poseidon2::PLONKY, + internal_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_DIFFUSION + : ::poseidon2::MONTGOMERY, + config_->ctx, &icicle_poseidon); + if (error != gpuSuccess) return false; + + impl_ = static_cast(icicle_poseidon); + return true; +} + +template <> +bool IciclePoseidon2::Load( + unsigned int rate, unsigned int width, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor) { +#if FIELD_ID != BABY_BEAR +#error Only BABY_BEAR is supported +#endif + if (impl_ != nullptr) { + VLOG(1) << "IciclePoseidon2 was already initialized"; + return true; + } + + ::poseidon2::Poseidon2<::babybear::scalar_t>* icicle_poseidon = nullptr; + gpuError_t error = tachyon_babybear_poseidon2_load_cuda( + width, rate, + external_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_MDS + : ::poseidon2::PLONKY, + internal_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_DIFFUSION + : ::poseidon2::MONTGOMERY, + config_->ctx, &icicle_poseidon); + if (error != gpuSuccess) return false; + + impl_ = static_cast(icicle_poseidon); + return true; +} + +template <> +bool IciclePoseidon2::Hash( + unsigned int rate, absl::Span inputs, + absl::Span outputs) { +#if FIELD_ID != BABY_BEAR +#error Only BABY_BEAR is supported +#endif + if (impl_ == nullptr) { + VLOG(1) << "IciclePoseidon2 is not initialized"; + return false; + } + + // TODO(chokobole): Change it to allocate just once across many |Hash()| + // calls. + std::vector<::babybear::scalar_t> inputs_tmp(inputs.size()); + OMP_PARALLEL_FOR(size_t i = 0; i < inputs_tmp.size(); ++i) { + inputs_tmp[i] = ::babybear::scalar_t::from_montgomery( + reinterpret_cast(inputs[i])); + } + + size_t num_states = inputs.size() / rate; + gpuError_t error = tachyon_babybear_poseidon2_hash_many_cuda( + reinterpret_cast<::poseidon2::Poseidon2<::babybear::scalar_t>*>(impl_), + inputs_tmp.data(), + reinterpret_cast<::babybear::scalar_t*>(outputs.data()), num_states, rate, + outputs.size() / num_states, *config_); + if (error != gpuSuccess) { + GPU_LOG(ERROR, error) + << "Failed tachyon_babybear_poseidon2_hash_many_cuda()"; + return false; + } + + OMP_PARALLEL_FOR(size_t i = 0; i < outputs.size(); ++i) { + reinterpret_cast<::babybear::scalar_t&>(outputs[i]) = + ::babybear::scalar_t::to_montgomery( + reinterpret_cast(outputs[i])); + } + return true; +} + +template <> +bool IciclePoseidon2::Delete() { +#if FIELD_ID != BABY_BEAR +#error Only BABY_BEAR is supported +#endif + if (!impl_) { + VLOG(1) << "IciclePoseidon2 is not initialized"; + return false; + } + gpuError_t error = tachyon_babybear_poseidon2_delete_cuda( + reinterpret_cast<::poseidon2::Poseidon2<::babybear::scalar_t>*>( + std::exchange(impl_, nullptr))); + return error == gpuSuccess; +} + +} // namespace tachyon::crypto diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.h b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.h new file mode 100644 index 0000000000..c558e15b15 --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_baby_bear.h @@ -0,0 +1,33 @@ +#ifndef TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_BABY_BEAR_H_ +#define TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_BABY_BEAR_H_ + +#include "third_party/icicle/include/fields/stark_fields/babybear.cu.h" +#include "third_party/icicle/include/poseidon2/poseidon2.cu.h" + +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_babybear_poseidon2_create_cuda( + unsigned int width, unsigned int rate, unsigned int alpha, + unsigned int internal_rounds, unsigned int external_rounds, + const ::babybear::scalar_t* round_constants, + const ::babybear::scalar_t* internal_matrix_diag, + ::poseidon2::MdsType mds_type, ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2** poseidon); + +extern "C" gpuError_t tachyon_babybear_poseidon2_load_cuda( + unsigned int width, unsigned int rate, ::poseidon2::MdsType mds_type, + ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2** poseidon); + +extern "C" gpuError_t tachyon_babybear_poseidon2_hash_many_cuda( + const ::poseidon2::Poseidon2<::babybear::scalar_t>* poseidon, + const ::babybear::scalar_t* inputs, ::babybear::scalar_t* output, + unsigned int number_of_states, unsigned int input_block_len, + unsigned int output_len, ::hash::HashConfig& cfg); + +extern "C" gpuError_t tachyon_babybear_poseidon2_delete_cuda( + ::poseidon2::Poseidon2<::babybear::scalar_t>* poseidon); + +#endif // TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_BABY_BEAR_H_ diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.cc b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.cc new file mode 100644 index 0000000000..7e03ad832b --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.cc @@ -0,0 +1,200 @@ +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.h" + +#include +#include + +#include "third_party/icicle/include/fields/id.h" +#include "third_party/icicle/include/gpu-utils/error_handler.cu.h" +#include "third_party/icicle/src/poseidon2/constants.cu.cc" // NOLINT(build/include) + +#include "tachyon/base/containers/container_util.h" +#include "tachyon/base/openmp_util.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h" +#include "tachyon/device/gpu/gpu_enums.h" +#include "tachyon/device/gpu/gpu_logging.h" + +gpuError_t tachyon_bn254_poseidon2_create_cuda( + unsigned int width, unsigned int rate, unsigned int alpha, + unsigned int internal_rounds, unsigned int external_rounds, + const ::bn254::scalar_t* round_constants, + const ::bn254::scalar_t* internal_matrix_diag, + ::poseidon2::MdsType mds_type, ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2<::bn254::scalar_t>** poseidon) { + try { + *poseidon = new ::poseidon2::Poseidon2<::bn254::scalar_t>( + width, rate, alpha, internal_rounds, external_rounds, round_constants, + internal_matrix_diag, mds_type, diffusion, ctx); + return gpuSuccess; + } catch (const ::IcicleError& error) { + LOG(ERROR) << "Failed tachyon_bn254_poseidon2_create_cuda(): " + << error.what(); + return cudaErrorUnknown; + } +} + +gpuError_t tachyon_bn254_poseidon2_load_cuda( + unsigned int width, unsigned int rate, ::poseidon2::MdsType mds_type, + ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2<::bn254::scalar_t>** poseidon) { + try { + *poseidon = new ::poseidon2::Poseidon2<::bn254::scalar_t>( + width, rate, mds_type, diffusion, ctx); + return gpuSuccess; + } catch (const IcicleError& error) { + LOG(ERROR) << "Failed tachyon_bn254_poseidon2_load_cuda(): " + << error.what(); + return cudaErrorUnknown; + } +} + +gpuError_t tachyon_bn254_poseidon2_hash_many_cuda( + const ::poseidon2::Poseidon2<::bn254::scalar_t>* poseidon, + const ::bn254::scalar_t* inputs, ::bn254::scalar_t* output, + unsigned int number_of_states, unsigned int input_block_len, + unsigned int output_len, ::hash::HashConfig& cfg) { + return poseidon->hash_many(inputs, output, number_of_states, input_block_len, + output_len, cfg); +} + +gpuError_t tachyon_bn254_poseidon2_delete_cuda( + ::poseidon2::Poseidon2<::bn254::scalar_t>* poseidon) { + try { + delete poseidon; + return gpuSuccess; + } catch (const IcicleError& error) { + LOG(ERROR) << "Failed tachyon_bn254_poseidon2_delete_cuda(): " + << error.what(); + return cudaErrorUnknown; + } +} + +namespace tachyon::crypto { + +template <> +bool IciclePoseidon2::Create( + unsigned int rate, unsigned int width, unsigned int alpha, + unsigned int external_rounds, unsigned int internal_rounds, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor, + absl::Span round_constants, + absl::Span internal_matrix_diag) { +#if FIELD_ID != BN254 +#error Only BN254 is supported +#endif + if (impl_ != nullptr) { + VLOG(1) << "IciclePoseidon2 was already initialized"; + return true; + } + + std::vector<::bn254::scalar_t> round_constants_tmp = + base::Map(round_constants, [](const math::bn254::Fr& f) { + return ::bn254::scalar_t::from_montgomery( + reinterpret_cast(f)); + }); + std::vector<::bn254::scalar_t> internal_matrix_diag_tmp = + base::Map(internal_matrix_diag, [](const math::bn254::Fr& f) { + return ::bn254::scalar_t::from_montgomery( + reinterpret_cast(f)); + }); + ::poseidon2::Poseidon2<::bn254::scalar_t>* icicle_poseidon = nullptr; + gpuError_t error = tachyon_bn254_poseidon2_create_cuda( + width, rate, alpha, internal_rounds, external_rounds, + round_constants_tmp.data(), internal_matrix_diag_tmp.data(), + external_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_MDS + : ::poseidon2::PLONKY, + internal_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_DIFFUSION + : ::poseidon2::MONTGOMERY, + config_->ctx, &icicle_poseidon); + if (error != gpuSuccess) return false; + + impl_ = static_cast(icicle_poseidon); + return true; +} + +template <> +bool IciclePoseidon2::Load( + unsigned int rate, unsigned int width, + Poseidon2Vendor external_matrix_vendor, + Poseidon2Vendor internal_matrix_vendor) { +#if FIELD_ID != BN254 +#error Only BN254 is supported +#endif + if (impl_ != nullptr) { + VLOG(1) << "IciclePoseidon2 was already initialized"; + return true; + } + + ::poseidon2::Poseidon2<::bn254::scalar_t>* icicle_poseidon = nullptr; + gpuError_t error = tachyon_bn254_poseidon2_load_cuda( + width, rate, + external_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_MDS + : ::poseidon2::PLONKY, + internal_matrix_vendor == Poseidon2Vendor::kHorizen + ? ::poseidon2::DEFAULT_DIFFUSION + : ::poseidon2::MONTGOMERY, + config_->ctx, &icicle_poseidon); + if (error != gpuSuccess) return false; + + impl_ = static_cast(icicle_poseidon); + return true; +} + +template <> +bool IciclePoseidon2::Hash( + unsigned int rate, absl::Span inputs, + absl::Span outputs) { +#if FIELD_ID != BN254 +#error Only BN254 is supported +#endif + if (impl_ == nullptr) { + VLOG(1) << "IciclePoseidon2 is not initialized"; + return false; + } + + // TODO(chokobole): Change it to allocate just once across many |Hash()| + // calls. + std::vector<::bn254::scalar_t> inputs_tmp(inputs.size()); + OMP_PARALLEL_FOR(size_t i = 0; i < inputs_tmp.size(); ++i) { + inputs_tmp[i] = ::bn254::scalar_t::from_montgomery( + reinterpret_cast(inputs[i])); + } + + size_t num_states = inputs.size() / rate; + gpuError_t error = tachyon_bn254_poseidon2_hash_many_cuda( + reinterpret_cast<::poseidon2::Poseidon2<::bn254::scalar_t>*>(impl_), + inputs_tmp.data(), reinterpret_cast<::bn254::scalar_t*>(outputs.data()), + num_states, rate, outputs.size() / num_states, *config_); + if (error != gpuSuccess) { + GPU_LOG(ERROR, error) << "Failed tachyon_bn254_poseidon2_hash_many_cuda()"; + return false; + } + + OMP_PARALLEL_FOR(size_t i = 0; i < outputs.size(); ++i) { + reinterpret_cast<::bn254::scalar_t&>(outputs[i]) = + ::bn254::scalar_t::to_montgomery( + reinterpret_cast(outputs[i])); + } + return true; +} + +template <> +bool IciclePoseidon2::Delete() { +#if FIELD_ID != BN254 +#error Only BN254 is supported +#endif + if (!impl_) { + VLOG(1) << "IciclePoseidon2 is not initialized"; + return false; + } + gpuError_t error = tachyon_bn254_poseidon2_delete_cuda( + reinterpret_cast<::poseidon2::Poseidon2<::bn254::scalar_t>*>( + std::exchange(impl_, nullptr))); + return error == gpuSuccess; +} + +} // namespace tachyon::crypto diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.h b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.h new file mode 100644 index 0000000000..8ef68c4f64 --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_bn254.h @@ -0,0 +1,33 @@ +#ifndef TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_BN254_H_ +#define TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_BN254_H_ + +#include "third_party/icicle/include/curves/params/bn254.cu.h" +#include "third_party/icicle/include/poseidon2/poseidon2.cu.h" + +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bn254_poseidon2_create_cuda( + unsigned int width, unsigned int rate, unsigned int alpha, + unsigned int internal_rounds, unsigned int external_rounds, + const ::bn254::scalar_t* round_constants, + const ::bn254::scalar_t* internal_matrix_diag, + ::poseidon2::MdsType mds_type, ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2** poseidon); + +extern "C" gpuError_t tachyon_bn254_poseidon2_load_cuda( + unsigned int width, unsigned int rate, ::poseidon2::MdsType mds_type, + ::poseidon2::DiffusionStrategy diffusion, + ::device_context::DeviceContext& ctx, + ::poseidon2::Poseidon2** poseidon); + +extern "C" gpuError_t tachyon_bn254_poseidon2_hash_many_cuda( + const ::poseidon2::Poseidon2<::bn254::scalar_t>* poseidon, + const ::bn254::scalar_t* inputs, ::bn254::scalar_t* output, + unsigned int number_of_states, unsigned int input_block_len, + unsigned int output_len, ::hash::HashConfig& cfg); + +extern "C" gpuError_t tachyon_bn254_poseidon2_delete_cuda( + ::poseidon2::Poseidon2<::bn254::scalar_t>* poseidon); + +#endif // TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_BN254_H_ diff --git a/third_party/icicle/separate_hash_config.patch b/third_party/icicle/separate_hash_config.patch new file mode 100644 index 0000000000..3ea2f047fc --- /dev/null +++ b/third_party/icicle/separate_hash_config.patch @@ -0,0 +1,72 @@ +diff --git a/icicle/include/hash/hash.cu.h b/icicle/include/hash/hash.cu.h +index 7ca414cc..85693c9f 100644 +--- a/icicle/include/hash/hash.cu.h ++++ b/icicle/include/hash/hash.cu.h +@@ -2,8 +2,8 @@ + #ifndef HASH_H + #define HASH_H + +-#include "gpu-utils/device_context.cu.h" + #include "gpu-utils/error_handler.cu.h" ++#include "hash/hash_config.h" + #include "matrix/matrix.cu.h" + #include + +@@ -14,22 +14,6 @@ using matrix::Matrix; + * Includes classes and methods for describing hash functions. + */ + namespace hash { +- +- /** +- * @struct HashConfig +- * Encodes hash operations parameters. +- */ +- struct HashConfig { +- device_context::DeviceContext ctx; /**< Details related to the device such as its id and stream id. */ +- bool are_inputs_on_device; /**< True if inputs are on device and false if they're on host. Default value: false. */ +- bool +- are_outputs_on_device; /**< True if outputs are on device and false if they're on host. Default value: false. */ +- bool is_async; /**< Whether to run the hash operations asynchronously. If set to `true`, the functions will be +- * non-blocking and you'd need to synchronize it explicitly by running +- * `cudaStreamSynchronize` or `cudaDeviceSynchronize`. If set to false, +- * functions will block the current CPU thread. */ +- }; +- + /** + * A function that returns the default value of [HashConfig](@ref HashConfig) for the [Hasher](@ref + * Hasher) class. +@@ -171,4 +155,4 @@ namespace hash { + }; + } // namespace hash + +-#endif ++#endif +diff --git a/icicle/include/hash/hash_config.h b/icicle/include/hash/hash_config.h +new file mode 100644 +index 00000000..2f5b05c7 +--- /dev/null ++++ b/icicle/include/hash/hash_config.h +@@ -0,0 +1,23 @@ ++#ifndef HASH_CONFIG_H ++#define HASH_CONFIG_H ++ ++#include "gpu-utils/device_context.cu.h" ++ ++namespace hash { ++ /** ++ * @struct HashConfig ++ * Encodes hash operations parameters. ++ */ ++ struct HashConfig { ++ device_context::DeviceContext ctx; /**< Details related to the device such as its id and stream id. */ ++ bool are_inputs_on_device; /**< True if inputs are on device and false if they're on host. Default value: false. */ ++ bool ++ are_outputs_on_device; /**< True if outputs are on device and false if they're on host. Default value: false. */ ++ bool is_async; /**< Whether to run the hash operations asynchronously. If set to `true`, the functions will be ++ * non-blocking and you'd need to synchronize it explicitly by running ++ * `cudaStreamSynchronize` or `cudaDeviceSynchronize`. If set to false, ++ * functions will block the current CPU thread. */ ++ }; ++} // namespace hash ++ ++#endif // HASH_CONFIG_H diff --git a/third_party/icicle/workspace.bzl b/third_party/icicle/workspace.bzl index 2f3afc68bf..fbba8b5ae7 100644 --- a/third_party/icicle/workspace.bzl +++ b/third_party/icicle/workspace.bzl @@ -15,6 +15,7 @@ def repo(): "@kroma_network_tachyon//third_party/icicle:remove_kernels_from_header.patch", "@kroma_network_tachyon//third_party/icicle:separate_msm_config.patch", "@kroma_network_tachyon//third_party/icicle:separate_ntt_algorithm.patch", + "@kroma_network_tachyon//third_party/icicle:separate_hash_config.patch", ], build_file = "//third_party/icicle:icicle.BUILD", link_files = {"//third_party/icicle:build_defs.bzl": "build_defs.bzl"}, From 6aae71fe6283d2d8aaa48e67595433c2629143ec Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 10:43:17 +0900 Subject: [PATCH 19/25] feat(crypto): add `IciclePoseidon2Holder` --- .../sponge/poseidon2/icicle/BUILD.bazel | 13 +++ .../icicle/icicle_poseidon2_holder.h | 109 ++++++++++++++++++ .../sponge/poseidon2/poseidon2_config.h | 2 + 3 files changed, 124 insertions(+) create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_holder.h diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel index b88d90dcbf..7f84b17871 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/BUILD.bazel @@ -53,3 +53,16 @@ tachyon_cc_library( "@icicle//:hdrs", ], ) + +tachyon_cc_library( + name = "icicle_poseidon2_holder", + hdrs = ["icicle_poseidon2_holder.h"], + visibility = ["//visibility:public"], + deps = [ + ":icicle_poseidon2_baby_bear", + ":icicle_poseidon2_bn254", + "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_config", + "//tachyon/device/gpu:scoped_mem_pool", + "//tachyon/device/gpu:scoped_stream", + ], +) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_holder.h b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_holder.h new file mode 100644 index 0000000000..2a565bfdf5 --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_holder.h @@ -0,0 +1,109 @@ +#ifndef TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_HOLDER_H_ +#define TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_HOLDER_H_ + +#include +#include +#include +#include + +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h" +#include "tachyon/device/gpu/scoped_mem_pool.h" +#include "tachyon/device/gpu/scoped_stream.h" + +namespace tachyon::crypto { + +template +class IciclePoseidon2Holder { + public: + IciclePoseidon2Holder() = default; + + const IciclePoseidon2& operator*() const { return *get(); } + IciclePoseidon2& operator*() { return *get(); } + + const IciclePoseidon2* operator->() const { return get(); } + IciclePoseidon2* operator->() { return get(); } + + operator bool() const { return !!icicle_; } + + const IciclePoseidon2* get() const { return icicle_.get(); } + IciclePoseidon2* get() { return icicle_.get(); } + + template + static IciclePoseidon2Holder Create(const Poseidon2Config& config) { + gpuMemPoolProps props = {gpuMemAllocationTypePinned, + gpuMemHandleTypeNone, + {gpuMemLocationTypeDevice, 0}}; + device::gpu::ScopedMemPool mem_pool = device::gpu::CreateMemPool(&props); + + uint64_t mem_pool_threshold = std::numeric_limits::max(); + gpuError_t error = gpuMemPoolSetAttribute( + mem_pool.get(), gpuMemPoolAttrReleaseThreshold, &mem_pool_threshold); + CHECK_EQ(error, gpuSuccess); + device::gpu::ScopedStream stream = device::gpu::CreateStream(); + + auto icicle = + std::make_unique>(mem_pool.get(), stream.get()); + + if constexpr (Params::kInternalMatrixVendor == Poseidon2Vendor::kPlonky3) { + // NOTE(chokobole): It's impossible to determine if |config| was + // constructed from |Poseidon2Config::Create()|, which accepts optimized + // constants, so |internal_shifts| and |ark| are copied. + std::vector internal_diagonal_minus_one(Params::kWidth); + internal_diagonal_minus_one[0] = F(F::Config::kModulus - 2); + for (size_t i = 1; i < internal_diagonal_minus_one.size(); ++i) { + internal_diagonal_minus_one[i] = + F(uint32_t{1} << config.internal_shifts[i - 1]); + } + + std::vector ark; + ark.reserve(Params::kFullRounds * Params::kWidth + + Params::kPartialRounds); + Eigen::Index partial_rounds_start = Params::kFullRounds / 2; + Eigen::Index partial_rounds_end = + Params::kFullRounds / 2 + Params::kPartialRounds; + for (Eigen::Index i = 0; i < config.ark.rows(); ++i) { + if (i < partial_rounds_start || i >= partial_rounds_end) { + for (Eigen::Index j = 0; j < config.ark.cols(); ++j) { + ark.push_back(config.ark(i, j)); + } + } else { + ark.push_back(config.ark(i, 0)); + } + } + if (!icicle->Create( + Rate, Params::kWidth, Params::kAlpha, Params::kFullRounds, + Params::kPartialRounds, Params::kExternalMatrixVendor, + Params::kInternalMatrixVendor, ark, internal_diagonal_minus_one)) + return {}; + } else { + if (!icicle->Load(Rate, Params::kWidth, Params::kExternalMatrixVendor, + Params::kInternalMatrixVendor)) + return {}; + } + + return {std::move(mem_pool), std::move(stream), std::move(icicle)}; + } + + void Release() { + icicle_.reset(); + stream_.reset(); + mem_pool_.reset(); + } + + private: + IciclePoseidon2Holder(device::gpu::ScopedMemPool mem_pool, + device::gpu::ScopedStream stream, + std::unique_ptr> icicle) + : mem_pool_(std::move(mem_pool)), + stream_(std::move(stream)), + icicle_(std::move(icicle)) {} + + device::gpu::ScopedMemPool mem_pool_; + device::gpu::ScopedStream stream_; + std::unique_ptr> icicle_; +}; + +} // namespace tachyon::crypto + +#endif // TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON2_ICICLE_ICICLE_POSEIDON2_HOLDER_H_ diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h index f099f00d99..ca0dfeae44 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_config.h @@ -114,6 +114,8 @@ struct Poseidon2Config : public PoseidonConfigBase { return Create(config_entry, internal_shifts, std::move(ark)); } + // NOTE(chokobole): If another variant method that accepts ark is added, + // remember to update the code in icicle_poseidon2_holder.h as well. constexpr static Poseidon2Config Create( const std::array& internal_shifts, math::Matrix&& ark) { From 1a044149bf8a42187dca7109b1702792e04e8cbe Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 17:54:46 +0900 Subject: [PATCH 20/25] test(crypto): add poseidon2 gpu unittest --- .../hashes/sponge/poseidon2/BUILD.bazel | 14 +++++ .../poseidon2/poseidon2_gpu_unittest.cc | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tachyon/crypto/hashes/sponge/poseidon2/poseidon2_gpu_unittest.cc diff --git a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel index 82901ac979..bf1e662dc5 100644 --- a/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel +++ b/tachyon/crypto/hashes/sponge/poseidon2/BUILD.bazel @@ -1,3 +1,4 @@ +load("//bazel:tachyon.bzl", "if_gpu_is_configured") load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest") package(default_visibility = ["//visibility:public"]) @@ -123,3 +124,16 @@ tachyon_cc_unittest( "//tachyon/math/finite_fields/test:finite_field_test", ], ) + +tachyon_cc_unittest( + name = "poseidon2_gpu_unittests", + srcs = if_gpu_is_configured(["poseidon2_gpu_unittest.cc"]), + deps = [ + ":poseidon2", + ":poseidon2_params", + "//tachyon/crypto/hashes/sponge/poseidon2/icicle:icicle_poseidon2_holder", + "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", + "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_bn254", + "//tachyon/math/finite_fields/test:finite_field_test", + ], +) diff --git a/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_gpu_unittest.cc b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_gpu_unittest.cc new file mode 100644 index 0000000000..305be76fdc --- /dev/null +++ b/tachyon/crypto/hashes/sponge/poseidon2/poseidon2_gpu_unittest.cc @@ -0,0 +1,63 @@ +#include + +#include "gtest/gtest.h" + +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_holder.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_bn254.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" +#include "tachyon/math/finite_fields/test/finite_field_test.h" + +namespace tachyon::crypto { + +namespace { + +template +class Poseidon2GpuTest : public math::FiniteFieldTest { +}; + +} // namespace + +using ParamsTypes = testing::Types< + Poseidon2Params, + Poseidon2Params>; +TYPED_TEST_SUITE(Poseidon2GpuTest, ParamsTypes); + +TYPED_TEST(Poseidon2GpuTest, Poseidon2Correctness) { + using Params = TypeParam; + using F = typename Params::Field; + + constexpr size_t kLen = 16; + constexpr size_t kRate = std::is_same_v ? 8 : 2; + constexpr size_t kOutput = std::is_same_v ? 6 : 1; + + Poseidon2Config config = Poseidon2Config::CreateDefault(); + IciclePoseidon2Holder holder = + IciclePoseidon2Holder::template Create(config); + + std::vector inputs = + base::CreateVector(kLen * kRate, []() { return F::Random(); }); + std::vector expected; + expected.reserve(kLen * kOutput); + + Poseidon2Sponge poseidon2_cpu(config); + for (size_t i = 0; i < kLen; ++i) { + SpongeState state; + for (size_t j = 0; j < kRate; ++j) { + state[j] = inputs[i * kRate + j]; + } + poseidon2_cpu.Permute(state); + for (size_t j = 0; j < kOutput; ++j) { + expected.push_back(state.elements[j]); + } + } + + std::vector outputs(kLen * kOutput); + ASSERT_TRUE((*holder).Hash(kRate, inputs, absl::MakeSpan(outputs))); + EXPECT_EQ(expected, outputs); +} + +} // namespace tachyon::crypto From 7516dc270dd35ca223b517ba95f779ad6cb756b9 Mon Sep 17 00:00:00 2001 From: GideokKim Date: Thu, 5 Sep 2024 17:10:01 +0900 Subject: [PATCH 21/25] feat(crypto): add `IcicleMMCS` --- .../field_merkle_tree/icicle/BUILD.bazel | 57 +++++++++ .../field_merkle_tree/icicle/icicle_mmcs.h | 101 +++++++++++++++ .../icicle/icicle_mmcs_baby_bear.cc | 118 ++++++++++++++++++ .../icicle/icicle_mmcs_baby_bear.h | 19 +++ .../icicle/icicle_mmcs_bn254.cc | 116 +++++++++++++++++ .../icicle/icicle_mmcs_bn254.h | 18 +++ third_party/icicle/change_sort_function.patch | 19 +++ .../icicle/fix_first_digest_copy_bug.patch | 13 ++ .../icicle/separate_merkle_tree_config.patch | 76 +++++++++++ third_party/icicle/workspace.bzl | 3 + 10 files changed, 540 insertions(+) create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.cc create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.h create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.cc create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.h create mode 100644 third_party/icicle/change_sort_function.patch create mode 100644 third_party/icicle/fix_first_digest_copy_bug.patch create mode 100644 third_party/icicle/separate_merkle_tree_config.patch diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel new file mode 100644 index 0000000000..8b6334b493 --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel @@ -0,0 +1,57 @@ +load("@icicle//:build_defs.bzl", "BABY_BEAR", "BN254", "icicle_defines") +load("@local_config_cuda//cuda:build_defs.bzl", "if_cuda") +load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cuda_library") + +tachyon_cuda_library( + name = "icicle_mmcs_baby_bear", + srcs = if_cuda(["icicle_mmcs_baby_bear.cc"]), + hdrs = ["icicle_mmcs_baby_bear.h"], + local_defines = icicle_defines(BABY_BEAR), + deps = [ + ":icicle_mmcs", + "//tachyon/base:bits", + "//tachyon/base:openmp_util", + "//tachyon/base/containers:container_util", + "//tachyon/device/gpu:gpu_enums", + "//tachyon/device/gpu:gpu_logging", + "@com_google_absl//absl/numeric:bits", + "@icicle//:merkle_tree_baby_bear", + ] + if_cuda([ + "@local_config_cuda//cuda:cudart_static", + ]), +) + +tachyon_cuda_library( + name = "icicle_mmcs_bn254", + srcs = if_cuda(["icicle_mmcs_bn254.cc"]), + hdrs = ["icicle_mmcs_bn254.h"], + local_defines = icicle_defines(BN254), + deps = [ + ":icicle_mmcs", + "//tachyon/base:bits", + "//tachyon/base:openmp_util", + "//tachyon/base/containers:container_util", + "//tachyon/device/gpu:gpu_enums", + "//tachyon/device/gpu:gpu_logging", + "@com_google_absl//absl/numeric:bits", + "@icicle//:merkle_tree_bn254", + ] + if_cuda([ + "@local_config_cuda//cuda:cudart_static", + ]), +) + +tachyon_cc_library( + name = "icicle_mmcs", + hdrs = ["icicle_mmcs.h"], + deps = [ + "//tachyon:export", + "//tachyon/base:logging", + "//tachyon/device/gpu:gpu_device_functions", + "//tachyon/math/elliptic_curves/bls12/bls12_381:fr", + "//tachyon/math/elliptic_curves/bn/bn254:fr", + "//tachyon/math/finite_fields/baby_bear", + "//tachyon/math/matrix:matrix_types", + "@com_google_absl//absl/types:span", + "@icicle//:hdrs", + ], +) diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h new file mode 100644 index 0000000000..1a45c2710a --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h @@ -0,0 +1,101 @@ +#ifndef TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_H_ +#define TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_H_ + +#include +#include + +#include "absl/types/span.h" +#include "third_party/icicle/include/merkle-tree/merkle_tree_config.h" + +#include "tachyon/base/logging.h" +#include "tachyon/device/gpu/gpu_device_functions.h" +#include "tachyon/export.h" +#include "tachyon/math/elliptic_curves/bn/bn254/fr.h" +#include "tachyon/math/finite_fields/baby_bear/baby_bear.h" +#include "tachyon/math/matrix/matrix_types.h" + +namespace tachyon::crypto { + +template +struct IsIcicleMMCSSupportedImpl { + constexpr static bool value = false; +}; + +template <> +struct IsIcicleMMCSSupportedImpl { + constexpr static bool value = true; +}; + +template <> +struct IsIcicleMMCSSupportedImpl { + constexpr static bool value = true; +}; + +template +constexpr bool IsIcicleMMCSSupported = IsIcicleMMCSSupportedImpl::value; + +struct TACHYON_EXPORT IcicleMMCSOptions { + unsigned int arity = 2; + unsigned int keep_rows = 0; + unsigned int digest_elements = 1; + bool are_inputs_on_device = false; + bool are_outputs_on_device = false; + bool is_async = false; +}; + +template +class IcicleMMCS { + public: + IcicleMMCS(gpuMemPool_t mem_pool, gpuStream_t stream, const void* hasher, + const void* compressor, size_t rate, + const IcicleMMCSOptions& options = IcicleMMCSOptions()) + : mem_pool_(mem_pool), + stream_(stream), + hasher_(hasher), + compressor_(compressor), + rate_(rate) { + ::device_context::DeviceContext ctx{stream_, /*device_id=*/0, mem_pool_}; + config_.reset(new ::merkle_tree::TreeBuilderConfig{ + ctx, + options.arity, + options.keep_rows, + options.digest_elements, + options.are_inputs_on_device, + options.are_outputs_on_device, + options.is_async, + }); + VLOG(1) << "IcicleMMCS is created"; + } + IcicleMMCS(const IcicleMMCS& other) = delete; + IcicleMMCS& operator=(const IcicleMMCS& other) = delete; + + [[nodiscard]] bool Commit( + std::vector>>&& matrices, + std::vector>>* outputs); + + private: + gpuMemPool_t mem_pool_ = nullptr; + gpuStream_t stream_ = nullptr; + // not owned + const void* hasher_ = nullptr; + // not owned + const void* compressor_ = nullptr; + size_t rate_ = 0; + std::unique_ptr<::merkle_tree::TreeBuilderConfig> config_; +}; + +template <> +TACHYON_EXPORT bool IcicleMMCS::Commit( + std::vector>>&& + matrices, + std::vector>>* outputs); + +template <> +TACHYON_EXPORT bool IcicleMMCS::Commit( + std::vector>>&& + matrices, + std::vector>>* outputs); + +} // namespace tachyon::crypto + +#endif // TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_H_ diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.cc new file mode 100644 index 0000000000..86e931c248 --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.cc @@ -0,0 +1,118 @@ +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.h" + +#include +#include +#include +#include + +#include "absl/numeric/bits.h" +#include "third_party/icicle/include/poseidon2/poseidon2.cu.h" +#include "third_party/icicle/src/merkle-tree/merkle.cu.cc" // NOLINT(build/include) +#include "third_party/icicle/src/merkle-tree/mmcs.cu.cc" // NOLINT(build/include) + +#include "tachyon/base/bits.h" +#include "tachyon/base/containers/container_util.h" +#include "tachyon/base/openmp_util.h" +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h" +#include "tachyon/device/gpu/gpu_enums.h" +#include "tachyon/device/gpu/gpu_logging.h" + +gpuError_t tachyon_babybear_mmcs_commit_cuda( + const ::matrix::Matrix<::babybear::scalar_t>* leaves, + unsigned int number_of_inputs, ::babybear::scalar_t* digests, + const ::hash::Hasher<::babybear::scalar_t, ::babybear::scalar_t>* hasher, + const ::hash::Hasher<::babybear::scalar_t, ::babybear::scalar_t>* + compressor, + const ::merkle_tree::TreeBuilderConfig& tree_config) { + // NOTE(GideokKim): The internal logic of the icicle Merkle tree always + // assumes that the leaves exist in multiples of 2. + return ::merkle_tree::mmcs_commit<::babybear::scalar_t, ::babybear::scalar_t>( + leaves, number_of_inputs, digests, *hasher, *compressor, tree_config); +} + +namespace tachyon::crypto { + +template <> +bool IcicleMMCS::Commit( + std::vector>>&& + matrices, + std::vector>>* outputs) { +#if FIELD_ID != BABY_BEAR +#error Only BABY_BEAR is supported +#endif + std::vector> matrices_tmp(matrices.size()); + for (size_t i = 0; i < matrices.size(); ++i) { + matrices_tmp[i].resize(matrices[i].rows() * matrices[i].cols()); + OMP_PARALLEL_FOR(size_t j = 0; j < matrices[i].size(); ++j) { + matrices_tmp[i][j] = ::babybear::scalar_t::from_montgomery( + reinterpret_cast(matrices[i].data())[j]); + } + } + + std::vector<::matrix::Matrix<::babybear::scalar_t>> leaves = + base::CreateVector(matrices.size(), [&matrices_tmp, &matrices](size_t i) { + return ::matrix::Matrix<::babybear::scalar_t>{ + matrices_tmp[i].data(), + static_cast(matrices[i].cols()), + static_cast(matrices[i].rows()), + }; + }); + + size_t max_tree_height = 0; + for (const auto& matrix : matrices) { + size_t tree_height = base::bits::Log2Ceiling( + absl::bit_ceil(static_cast(matrix.rows()))); + max_tree_height = std::max(max_tree_height, tree_height); + } + config_->keep_rows = max_tree_height + 1; + config_->digest_elements = rate_; + size_t digests_len = ::merkle_tree::get_digests_len( + config_->keep_rows - 1, config_->arity, config_->digest_elements); + + std::unique_ptr<::babybear::scalar_t[]> icicle_digest( + new ::babybear::scalar_t[digests_len]); + + gpuError_t error = tachyon_babybear_mmcs_commit_cuda( + leaves.data(), leaves.size(), icicle_digest.get(), + reinterpret_cast< + const ::hash::Hasher<::babybear::scalar_t, ::babybear::scalar_t>*>( + hasher_), + reinterpret_cast< + const ::hash::Hasher<::babybear::scalar_t, ::babybear::scalar_t>*>( + compressor_), + *config_); + if (error != gpuSuccess) { + GPU_LOG(ERROR, error) << "Failed tachyon_babybear_mmcs_commit_cuda()"; + return false; + } + + // TODO(GideokKim): Optimize this. + outputs->reserve(config_->keep_rows); + size_t previous_number_of_element = 0; + for (size_t layer_idx = 0; layer_idx <= max_tree_height; ++layer_idx) { + std::vector> digest_layer; + size_t number_of_node = 1 << (max_tree_height - layer_idx); + digest_layer.reserve(number_of_node); + + for (size_t node_idx = 0; node_idx < number_of_node; ++node_idx) { + std::vector digest; + digest.reserve(config_->digest_elements); + + for (size_t element_idx = 0; element_idx < config_->digest_elements; + ++element_idx) { + size_t idx = previous_number_of_element + + config_->digest_elements * node_idx + element_idx; + icicle_digest[idx] = + ::babybear::scalar_t::to_montgomery(icicle_digest[idx]); + digest.emplace_back( + *reinterpret_cast(&icicle_digest[idx])); + } + digest_layer.emplace_back(std::move(digest)); + } + outputs->emplace_back(std::move(digest_layer)); + previous_number_of_element += number_of_node * config_->digest_elements; + } + return true; +} + +} // namespace tachyon::crypto diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.h b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.h new file mode 100644 index 0000000000..dfb0a81452 --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_baby_bear.h @@ -0,0 +1,19 @@ +#ifndef TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_BABY_BEAR_H_ +#define TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_BABY_BEAR_H_ + +#include "third_party/icicle/include/fields/stark_fields/babybear.cu.h" +#include "third_party/icicle/include/hash/hash.cu.h" +#include "third_party/icicle/include/matrix/matrix.cu.h" +#include "third_party/icicle/include/merkle-tree/merkle_tree_config.h" + +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_babybear_mmcs_commit_cuda( + const ::matrix::Matrix<::babybear::scalar_t>* leaves, + unsigned int number_of_inputs, ::babybear::scalar_t* digests, + const ::hash::Hasher<::babybear::scalar_t, ::babybear::scalar_t>* hasher, + const ::hash::Hasher<::babybear::scalar_t, ::babybear::scalar_t>* + compressor, + const ::merkle_tree::TreeBuilderConfig& tree_config); + +#endif // TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_BABY_BEAR_H_ diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.cc new file mode 100644 index 0000000000..25f0532538 --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.cc @@ -0,0 +1,116 @@ +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.h" + +#include +#include +#include +#include + +#include "absl/numeric/bits.h" +#include "third_party/icicle/include/poseidon2/poseidon2.cu.h" +#include "third_party/icicle/src/merkle-tree/merkle.cu.cc" // NOLINT(build/include) +#include "third_party/icicle/src/merkle-tree/mmcs.cu.cc" // NOLINT(build/include) + +#include "tachyon/base/bits.h" +#include "tachyon/base/containers/container_util.h" +#include "tachyon/base/openmp_util.h" +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h" +#include "tachyon/device/gpu/gpu_enums.h" +#include "tachyon/device/gpu/gpu_logging.h" + +gpuError_t tachyon_bn254_mmcs_commit_cuda( + const ::matrix::Matrix<::bn254::scalar_t>* leaves, + unsigned int number_of_inputs, ::bn254::scalar_t* digests, + const ::hash::Hasher<::bn254::scalar_t, ::bn254::scalar_t>* hasher, + const ::hash::Hasher<::bn254::scalar_t, ::bn254::scalar_t>* compressor, + const ::merkle_tree::TreeBuilderConfig& tree_config) { + // NOTE(GideokKim): The internal logic of the icicle Merkle tree always + // assumes that the leaves exist in multiples of 2. + return ::merkle_tree::mmcs_commit<::bn254::scalar_t, ::bn254::scalar_t>( + leaves, number_of_inputs, digests, *hasher, *compressor, tree_config); +} + +namespace tachyon::crypto { + +template <> +bool IcicleMMCS::Commit( + std::vector>>&& + matrices, + std::vector>>* outputs) { +#if FIELD_ID != BN254 +#error Only BN254 is supported +#endif + std::vector> matrices_tmp(matrices.size()); + for (size_t i = 0; i < matrices.size(); ++i) { + matrices_tmp[i].resize(matrices[i].rows() * matrices[i].cols()); + OMP_PARALLEL_FOR(size_t j = 0; j < matrices[i].size(); ++j) { + matrices_tmp[i][j] = ::bn254::scalar_t::from_montgomery( + reinterpret_cast(matrices[i].data())[j]); + } + } + + std::vector<::matrix::Matrix<::bn254::scalar_t>> leaves = + base::CreateVector(matrices.size(), [&matrices_tmp, &matrices](size_t i) { + return ::matrix::Matrix<::bn254::scalar_t>{ + matrices_tmp[i].data(), + static_cast(matrices[i].cols()), + static_cast(matrices[i].rows()), + }; + }); + + size_t max_tree_height = 0; + for (const auto& matrix : matrices) { + size_t tree_height = base::bits::Log2Ceiling( + absl::bit_ceil(static_cast(matrix.rows()))); + max_tree_height = std::max(max_tree_height, tree_height); + } + config_->keep_rows = max_tree_height + 1; + config_->digest_elements = rate_; + size_t digests_len = ::merkle_tree::get_digests_len( + config_->keep_rows - 1, config_->arity, config_->digest_elements); + + std::unique_ptr<::bn254::scalar_t[]> icicle_digest( + new ::bn254::scalar_t[digests_len]); + + gpuError_t error = tachyon_bn254_mmcs_commit_cuda( + leaves.data(), leaves.size(), icicle_digest.get(), + reinterpret_cast< + const ::hash::Hasher<::bn254::scalar_t, ::bn254::scalar_t>*>(hasher_), + reinterpret_cast< + const ::hash::Hasher<::bn254::scalar_t, ::bn254::scalar_t>*>( + compressor_), + *config_); + if (error != gpuSuccess) { + GPU_LOG(ERROR, error) << "Failed tachyon_bn254_mmcs_commit_cuda()"; + return false; + } + + // TODO(GideokKim): Optimize this. + outputs->reserve(config_->keep_rows); + size_t previous_number_of_element = 0; + for (size_t layer_idx = 0; layer_idx <= max_tree_height; ++layer_idx) { + std::vector> digest_layer; + size_t number_of_node = 1 << (max_tree_height - layer_idx); + digest_layer.reserve(number_of_node); + + for (size_t node_idx = 0; node_idx < number_of_node; ++node_idx) { + std::vector digest; + digest.reserve(config_->digest_elements); + + for (size_t element_idx = 0; element_idx < config_->digest_elements; + ++element_idx) { + size_t idx = previous_number_of_element + + config_->digest_elements * node_idx + element_idx; + icicle_digest[idx] = + ::bn254::scalar_t::to_montgomery(icicle_digest[idx]); + digest.emplace_back( + *reinterpret_cast(&icicle_digest[idx])); + } + digest_layer.emplace_back(std::move(digest)); + } + outputs->emplace_back(std::move(digest_layer)); + previous_number_of_element += number_of_node * config_->digest_elements; + } + return true; +} + +} // namespace tachyon::crypto diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.h b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.h new file mode 100644 index 0000000000..2fb6f6534e --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_bn254.h @@ -0,0 +1,18 @@ +#ifndef TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_BN254_H_ +#define TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_BN254_H_ + +#include "third_party/icicle/include/curves/params/bn254.cu.h" +#include "third_party/icicle/include/hash/hash.cu.h" +#include "third_party/icicle/include/matrix/matrix.cu.h" +#include "third_party/icicle/include/merkle-tree/merkle_tree_config.h" + +#include "tachyon/device/gpu/gpu_device_functions.h" + +extern "C" gpuError_t tachyon_bn254_mmcs_commit_cuda( + const ::matrix::Matrix<::bn254::scalar_t>* leaves, + unsigned int number_of_inputs, ::bn254::scalar_t* digests, + const ::hash::Hasher<::bn254::scalar_t, ::bn254::scalar_t>* hasher, + const ::hash::Hasher<::bn254::scalar_t, ::bn254::scalar_t>* compressor, + const ::merkle_tree::TreeBuilderConfig& tree_config); + +#endif // TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_BN254_H_ diff --git a/third_party/icicle/change_sort_function.patch b/third_party/icicle/change_sort_function.patch new file mode 100644 index 0000000000..8ee17c5155 --- /dev/null +++ b/third_party/icicle/change_sort_function.patch @@ -0,0 +1,19 @@ +diff --git a/icicle/src/merkle-tree/mmcs.cu.cc b/icicle/src/merkle-tree/mmcs.cu.cc +index 1d695483..e0a0cf76 100644 +--- a/icicle/src/merkle-tree/mmcs.cu.cc ++++ b/icicle/src/merkle-tree/mmcs.cu.cc +@@ -203,10 +203,10 @@ namespace merkle_tree { + IcicleError_t::InvalidArgument, + "Hash max preimage length does not match merkle tree arity multiplied by digest elements"); + +- std::vector> sorted_inputs(number_of_inputs); +- std::partial_sort_copy( +- inputs, inputs + number_of_inputs, sorted_inputs.begin(), sorted_inputs.end(), +- [](const Matrix& left, const Matrix& right) { return left.height > right.height; }); ++ std::vector> sorted_inputs(inputs, inputs + number_of_inputs); ++ std::stable_sort(sorted_inputs.begin(), sorted_inputs.end(), [](const Matrix& left, const Matrix& right) { ++ return left.height > right.height; ++ }); + + // Check that the height of any two given matrices either rounds up + // to the same next power of two or otherwise equal diff --git a/third_party/icicle/fix_first_digest_copy_bug.patch b/third_party/icicle/fix_first_digest_copy_bug.patch new file mode 100644 index 0000000000..2851e0b3d4 --- /dev/null +++ b/third_party/icicle/fix_first_digest_copy_bug.patch @@ -0,0 +1,13 @@ +diff --git a/icicle/src/merkle-tree/mmcs.cu.cc b/icicle/src/merkle-tree/mmcs.cu.cc +index 1d695483..1c123caa 100644 +--- a/icicle/src/merkle-tree/mmcs.cu.cc ++++ b/icicle/src/merkle-tree/mmcs.cu.cc +@@ -165,7 +165,7 @@ namespace merkle_tree { + d_leaves_info, params.number_of_leaves_to_inject, params.number_of_rows, states, params.digest_elements, + *params.hasher, *params.ctx)); + +- CHK_IF_RETURN(maybe_copy_digests(digests, big_tree_digests, params)); ++ CHK_IF_RETURN(maybe_copy_digests(states, big_tree_digests, params)); + + params.number_of_rows_padded /= params.arity; + params.segment_size /= params.arity; diff --git a/third_party/icicle/separate_merkle_tree_config.patch b/third_party/icicle/separate_merkle_tree_config.patch new file mode 100644 index 0000000000..3a96771208 --- /dev/null +++ b/third_party/icicle/separate_merkle_tree_config.patch @@ -0,0 +1,76 @@ +diff --git a/icicle/include/merkle-tree/merkle.cu.h b/icicle/include/merkle-tree/merkle.cu.h +index d0a020db..2be4e4d0 100644 +--- a/icicle/include/merkle-tree/merkle.cu.h ++++ b/icicle/include/merkle-tree/merkle.cu.h +@@ -2,7 +2,7 @@ + #ifndef MERKLE_H + #define MERKLE_H + +-#include "gpu-utils/device_context.cu.h" ++#include "merkle-tree/merkle_tree_config.h" + #include "gpu-utils/error_handler.cu.h" + #include "utils/utils.h" + #include "hash/hash.cu.h" +@@ -57,27 +57,6 @@ namespace merkle_tree { + return height; + } + +- /** +- * @struct TreeBuilderConfig +- * Struct that encodes various Tree builder parameters. +- */ +- struct TreeBuilderConfig { +- device_context::DeviceContext ctx; /**< Details related to the device such as its id and stream id. */ +- unsigned int arity; +- unsigned int +- keep_rows; /**< How many rows of the Merkle tree rows should be written to output. '0' means all of them */ +- unsigned int +- digest_elements; /** @param digest_elements the size of output for each bottom layer hash and compression. +- * Will also be equal to the size of the root of the tree. Default value 1 */ +- bool are_inputs_on_device; /**< True if inputs are on device and false if they're on host. Default value: false. */ +- bool +- are_outputs_on_device; /**< True if outputs are on device and false if they're on host. Default value: false. */ +- bool is_async; /**< Whether to run the tree builder asynchronously. If set to `true`, the build_merkle_tree +- * function will be non-blocking and you'd need to synchronize it explicitly by running +- * `cudaStreamSynchronize` or `cudaDeviceSynchronize`. If set to false, the +- * function will block the current CPU thread. */ +- }; +- + static TreeBuilderConfig + default_merkle_config(const device_context::DeviceContext& ctx = device_context::get_default_device_context()) + { +diff --git a/icicle/include/merkle-tree/merkle_tree_config.h b/icicle/include/merkle-tree/merkle_tree_config.h +new file mode 100644 +index 00000000..bdccb892 +--- /dev/null ++++ b/icicle/include/merkle-tree/merkle_tree_config.h +@@ -0,0 +1,29 @@ ++#ifndef MERKLE_TREE_CONFIG_H ++#define MERKLE_TREE_CONFIG_H ++ ++#include "gpu-utils/device_context.cu.h" ++ ++namespace merkle_tree { ++ /** ++ * @struct TreeBuilderConfig ++ * Struct that encodes various Tree builder parameters. ++ */ ++ struct TreeBuilderConfig { ++ device_context::DeviceContext ctx; /**< Details related to the device such as its id and stream id. */ ++ unsigned int arity; ++ unsigned int ++ keep_rows; /**< How many rows of the Merkle tree rows should be written to output. '0' means all of them */ ++ unsigned int ++ digest_elements; /** @param digest_elements the size of output for each bottom layer hash and compression. ++ * Will also be equal to the size of the root of the tree. Default value 1 */ ++ bool are_inputs_on_device; /**< True if inputs are on device and false if they're on host. Default value: false. */ ++ bool ++ are_outputs_on_device; /**< True if outputs are on device and false if they're on host. Default value: false. */ ++ bool is_async; /**< Whether to run the tree builder asynchronously. If set to `true`, the build_merkle_tree ++ * function will be non-blocking and you'd need to synchronize it explicitly by running ++ * `cudaStreamSynchronize` or `cudaDeviceSynchronize`. If set to false, the ++ * function will block the current CPU thread. */ ++ }; ++} // namespace merkle_tree ++ ++#endif // MERKLE_TREE_CONFIG_H diff --git a/third_party/icicle/workspace.bzl b/third_party/icicle/workspace.bzl index fbba8b5ae7..22eac14524 100644 --- a/third_party/icicle/workspace.bzl +++ b/third_party/icicle/workspace.bzl @@ -16,6 +16,9 @@ def repo(): "@kroma_network_tachyon//third_party/icicle:separate_msm_config.patch", "@kroma_network_tachyon//third_party/icicle:separate_ntt_algorithm.patch", "@kroma_network_tachyon//third_party/icicle:separate_hash_config.patch", + "@kroma_network_tachyon//third_party/icicle:separate_merkle_tree_config.patch", + "@kroma_network_tachyon//third_party/icicle:change_sort_function.patch", + "@kroma_network_tachyon//third_party/icicle:fix_first_digest_copy_bug.patch", ], build_file = "//third_party/icicle:icicle.BUILD", link_files = {"//third_party/icicle:build_defs.bzl": "build_defs.bzl"}, From 0776ea2fdd84a038b4e8042a5952df9acca21519 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 10:27:11 +0900 Subject: [PATCH 22/25] feat(crypto): add `IcicleMMCSHolder` --- .../field_merkle_tree/icicle/BUILD.bazel | 12 ++++ .../icicle/icicle_mmcs_holder.h | 69 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_holder.h diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel index 8b6334b493..a657d929c6 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/BUILD.bazel @@ -55,3 +55,15 @@ tachyon_cc_library( "@icicle//:hdrs", ], ) + +tachyon_cc_library( + name = "icicle_mmcs_holder", + hdrs = ["icicle_mmcs_holder.h"], + visibility = ["//visibility:public"], + deps = [ + ":icicle_mmcs_baby_bear", + ":icicle_mmcs_bn254", + "//tachyon/device/gpu:scoped_mem_pool", + "//tachyon/device/gpu:scoped_stream", + ], +) diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_holder.h b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_holder.h new file mode 100644 index 0000000000..81f9bfefb2 --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_holder.h @@ -0,0 +1,69 @@ +#ifndef TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_HOLDER_H_ +#define TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_HOLDER_H_ + +#include +#include +#include + +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs.h" +#include "tachyon/device/gpu/scoped_mem_pool.h" +#include "tachyon/device/gpu/scoped_stream.h" + +namespace tachyon::crypto { + +template +class IcicleMMCSHolder { + public: + IcicleMMCSHolder() = default; + + const IcicleMMCS& operator*() const { return *get(); } + IcicleMMCS& operator*() { return *get(); } + + const IcicleMMCS* operator->() const { return get(); } + IcicleMMCS* operator->() { return get(); } + + operator bool() const { return !!icicle_; } + + const IcicleMMCS* get() const { return icicle_.get(); } + IcicleMMCS* get() { return icicle_.get(); } + + template + static IcicleMMCSHolder Create(const void* hasher, const void* compressor) { + gpuMemPoolProps props = {gpuMemAllocationTypePinned, + gpuMemHandleTypeNone, + {gpuMemLocationTypeDevice, 0}}; + device::gpu::ScopedMemPool mem_pool = device::gpu::CreateMemPool(&props); + + uint64_t mem_pool_threshold = std::numeric_limits::max(); + gpuError_t error = gpuMemPoolSetAttribute( + mem_pool.get(), gpuMemPoolAttrReleaseThreshold, &mem_pool_threshold); + CHECK_EQ(error, gpuSuccess); + device::gpu::ScopedStream stream = device::gpu::CreateStream(); + + return {std::move(mem_pool), std::move(stream), + std::make_unique>(mem_pool.get(), stream.get(), + hasher, compressor, Rate)}; + } + + void Release() { + icicle_.reset(); + stream_.reset(); + mem_pool_.reset(); + } + + private: + IcicleMMCSHolder(device::gpu::ScopedMemPool mem_pool, + device::gpu::ScopedStream stream, + std::unique_ptr> icicle) + : mem_pool_(std::move(mem_pool)), + stream_(std::move(stream)), + icicle_(std::move(icicle)) {} + + device::gpu::ScopedMemPool mem_pool_; + device::gpu::ScopedStream stream_; + std::unique_ptr> icicle_; +}; + +} // namespace tachyon::crypto + +#endif // TACHYON_CRYPTO_COMMITMENTS_MERKLE_TREE_FIELD_MERKLE_TREE_ICICLE_ICICLE_MMCS_HOLDER_H_ From 5582c24391143b1290a214963158f9c73c2a9a49 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 13:32:36 +0900 Subject: [PATCH 23/25] feat(crypto): apply `IcicleMMCS` to `FieldMerkleTreeMMCS` --- .../merkle_tree/field_merkle_tree/BUILD.bazel | 1 + .../field_merkle_tree/field_merkle_tree.h | 196 ++++++++++++------ .../field_merkle_tree_mmcs.h | 28 ++- .../field_merkle_tree_mmcs_unittest.cc | 10 +- .../field_merkle_tree_unittest.cc | 24 +-- 5 files changed, 177 insertions(+), 82 deletions(-) diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel index dbf59e5e69..bc7ae41523 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel @@ -18,6 +18,7 @@ tachyon_cc_library( "//tachyon/base:sort", "//tachyon/base/buffer:copyable", "//tachyon/base/containers:container_util", + "//tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle:icicle_mmcs_holder", "//tachyon/math/finite_fields:extension_field_traits_forward", "//tachyon/math/finite_fields:finite_field_traits", "//tachyon/math/finite_fields:packed_field_traits_forward", diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree.h b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree.h index f4430fe3c7..308be0f4f4 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree.h +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree.h @@ -28,6 +28,10 @@ #include "tachyon/math/matrix/matrix_types.h" #include "tachyon/math/matrix/matrix_utils.h" +#if TACHYON_CUDA +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/icicle/icicle_mmcs_holder.h" +#endif + namespace tachyon { namespace crypto { @@ -47,92 +51,91 @@ class FieldMerkleTree { template - static FieldMerkleTree Build( + static FieldMerkleTree BuildCpu( const Hasher& hasher, const PackedHasher& packed_hasher, const Compressor& compressor, const PackedCompressor& packed_compressor, std::vector>>&& leaves) { - TRACE_EVENT("Utils", "FieldMerkleTree::Build"); CHECK(!leaves.empty()); + return DoBuildCpu(hasher, packed_hasher, compressor, packed_compressor, + std::move(leaves)); + } - std::vector sorted_leaves = base::Map( - leaves, [](Eigen::Map>& matrix) { - return RowMajorMatrixView(&matrix); + template + static FieldMerkleTree BuildOwnedCpu( + const Hasher& hasher, const PackedHasher& packed_hasher, + const Compressor& compressor, const PackedCompressor& packed_compressor, + std::vector>&& owned_leaves) { + std::vector>> leaves = + base::Map(owned_leaves, [](const math::RowMajorMatrix& owned_leaf) { + return math::Map(owned_leaf); }); - base::StableSort(sorted_leaves.begin(), sorted_leaves.end()); - -#if DCHECK_IS_ON() - { - for (size_t i = 0; i < sorted_leaves.size() - 1; ++i) { - size_t a = static_cast(sorted_leaves[i]->rows()); - size_t b = static_cast(sorted_leaves[i + 1]->rows()); - CHECK(a == b || absl::bit_ceil(a) != absl::bit_ceil(b)) - << "matrix rows that round up to the same power of two must be " - "equal"; - } - } -#endif // DCHECK_IS_ON() - - size_t first_layer_rows = sorted_leaves.front()->rows(); - size_t first_layer_size = 1; - for (size_t i = 1; i < sorted_leaves.size(); ++i) { - if (first_layer_rows == static_cast(sorted_leaves[i]->rows())) { - ++first_layer_size; - } else { - break; - } - } - absl::Span tallest_matrices = - absl::MakeSpan(sorted_leaves.data(), first_layer_size); - absl::Span remaining_leaves = - absl::MakeSpan(sorted_leaves.data() + first_layer_size, - sorted_leaves.size() - first_layer_size); + FieldMerkleTree ret = BuildCpu(hasher, packed_hasher, compressor, + packed_compressor, std::move(leaves)); + ret.owned_leaves_ = std::move(owned_leaves); + return ret; + } - std::vector> digest_layers; - digest_layers.emplace_back( - CreateFirstDigestLayer(hasher, packed_hasher, tallest_matrices)); +#if TACHYON_CUDA + template + static FieldMerkleTree MaybeBuildGpu( + const Hasher& hasher, const PackedHasher& packed_hasher, + const Compressor& compressor, const PackedCompressor& packed_compressor, + std::vector>>&& leaves, + IcicleMMCSHolder* icicle) { + CHECK(!leaves.empty()); - while (true) { - const std::vector& prev_layer = digest_layers.back(); - if (prev_layer.size() == 1) break; - size_t next_layer_rows = prev_layer.size() / 2; - size_t next_layer_size = 0; - for (size_t i = 0; i < remaining_leaves.size(); ++i) { - if (absl::bit_ceil(static_cast(remaining_leaves[i]->rows())) == - next_layer_rows) { - ++next_layer_size; - } else { - break; +#if TACHYON_CUDA + if constexpr (IsIcicleMMCSSupported) { + if (icicle) { + TRACE_EVENT("Utils", "DoBuildGpu"); + std::vector>> digest_layers_icicle; + CHECK((*icicle)->Commit(std::move(leaves), &digest_layers_icicle)); + // TODO(GideokKim): Optimize this. + std::vector> digest_layers; + digest_layers.resize(digest_layers_icicle.size()); + for (size_t i = 0; i < digest_layers_icicle.size(); ++i) { + digest_layers[i].resize(digest_layers_icicle[i].size()); + for (size_t j = 0; j < digest_layers_icicle[i].size(); ++j) { + if (digest_layers_icicle[i][j].size() == N) { + std::array arr; + + std::move(digest_layers_icicle[i][j].begin(), + digest_layers_icicle[i][j].end(), arr.begin()); + + digest_layers[i][j] = std::move(arr); + } + } } + return {std::move(leaves), std::move(digest_layers)}; } - absl::Span matrices_to_inject; - if (next_layer_size > 0) { - matrices_to_inject = remaining_leaves.subspan(0, next_layer_size); - remaining_leaves.remove_prefix(next_layer_size); - } - - digest_layers.push_back( - CompressAndInject(hasher, packed_hasher, compressor, - packed_compressor, prev_layer, matrices_to_inject)); } - return {std::move(leaves), std::move(digest_layers)}; +#endif + return DoBuildCpu(hasher, packed_hasher, compressor, packed_compressor, + std::move(leaves)); } template - static FieldMerkleTree BuildOwned( + static FieldMerkleTree MaybeBuildOwnedGpu( const Hasher& hasher, const PackedHasher& packed_hasher, const Compressor& compressor, const PackedCompressor& packed_compressor, - std::vector>&& owned_leaves) { + std::vector>&& owned_leaves, + IcicleMMCSHolder* icicle) { std::vector>> leaves = base::Map(owned_leaves, [](const math::RowMajorMatrix& owned_leaf) { return math::Map(owned_leaf); }); - FieldMerkleTree ret = Build(hasher, packed_hasher, compressor, - packed_compressor, std::move(leaves)); + FieldMerkleTree ret = + MaybeBuildGpu(hasher, packed_hasher, compressor, packed_compressor, + std::move(leaves), icicle); ret.owned_leaves_ = std::move(owned_leaves); return ret; } +#endif + const std::vector>>& leaves() const { return leaves_; } @@ -187,6 +190,77 @@ class FieldMerkleTree { std::vector>&& digest_layers) : leaves_(std::move(leaves)), digest_layers_(std::move(digest_layers)) {} + template + static FieldMerkleTree DoBuildCpu( + const Hasher& hasher, const PackedHasher& packed_hasher, + const Compressor& compressor, const PackedCompressor& packed_compressor, + std::vector>>&& leaves) { + TRACE_EVENT("Utils", "DoBuildCpu"); + + std::vector sorted_leaves = base::Map( + leaves, [](Eigen::Map>& matrix) { + return RowMajorMatrixView(&matrix); + }); + base::StableSort(sorted_leaves.begin(), sorted_leaves.end()); + +#if DCHECK_IS_ON() + { + for (size_t i = 0; i < sorted_leaves.size() - 1; ++i) { + size_t a = static_cast(sorted_leaves[i]->rows()); + size_t b = static_cast(sorted_leaves[i + 1]->rows()); + CHECK(a == b || absl::bit_ceil(a) != absl::bit_ceil(b)) + << "matrix rows that round up to the same power of two must be " + "equal"; + } + } +#endif // DCHECK_IS_ON() + + size_t first_layer_rows = sorted_leaves.front()->rows(); + size_t first_layer_size = 1; + for (size_t i = 1; i < sorted_leaves.size(); ++i) { + if (first_layer_rows == static_cast(sorted_leaves[i]->rows())) { + ++first_layer_size; + } else { + break; + } + } + absl::Span tallest_matrices = + absl::MakeSpan(sorted_leaves.data(), first_layer_size); + absl::Span remaining_leaves = + absl::MakeSpan(sorted_leaves.data() + first_layer_size, + sorted_leaves.size() - first_layer_size); + + std::vector> digest_layers; + digest_layers.emplace_back( + CreateFirstDigestLayer(hasher, packed_hasher, tallest_matrices)); + + while (true) { + const std::vector& prev_layer = digest_layers.back(); + if (prev_layer.size() == 1) break; + size_t next_layer_rows = prev_layer.size() / 2; + size_t next_layer_size = 0; + for (size_t i = 0; i < remaining_leaves.size(); ++i) { + if (absl::bit_ceil(static_cast(remaining_leaves[i]->rows())) == + next_layer_rows) { + ++next_layer_size; + } else { + break; + } + } + absl::Span matrices_to_inject; + if (next_layer_size > 0) { + matrices_to_inject = remaining_leaves.subspan(0, next_layer_size); + remaining_leaves.remove_prefix(next_layer_size); + } + + digest_layers.push_back( + CompressAndInject(hasher, packed_hasher, compressor, + packed_compressor, prev_layer, matrices_to_inject)); + } + return {std::move(leaves), std::move(digest_layers)}; + } + template static std::vector CreateFirstDigestLayer( const Hasher& hasher, const PackedHasher& packed_hasher, diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs.h b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs.h index 0f1e6ca478..20829be664 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs.h +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_mmcs.h @@ -61,6 +61,10 @@ class FieldMerkleTreeMMCS final return packed_compressor_; } +#if TACHYON_CUDA + void set_icicle(IcicleMMCSHolder* icicle) { icicle_ = icicle; } +#endif + private: friend class MixedMatrixCommitmentScheme>; @@ -90,9 +94,15 @@ class FieldMerkleTreeMMCS final std::vector>>&& matrices, Commitment* commitment, ProverData* prover_data) const { TRACE_EVENT("ProofGeneration", "FieldMerkleTreeMMCS::DoCommit"); - *prover_data = - FieldMerkleTree::Build(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices)); +#if TACHYON_CUDA + *prover_data = FieldMerkleTree::MaybeBuildGpu( + hasher_, packed_hasher_, compressor_, packed_compressor_, + std::move(matrices), icicle_); +#else + *prover_data = FieldMerkleTree::BuildCpu( + hasher_, packed_hasher_, compressor_, packed_compressor_, + std::move(matrices)); +#endif *commitment = prover_data->GetRoot(); return true; @@ -102,9 +112,15 @@ class FieldMerkleTreeMMCS final std::vector>&& owned_matrices, Commitment* commitment, ProverData* prover_data) const { TRACE_EVENT("ProofGeneration", "FieldMerkleTreeMMCS::DoCommitOwned"); - *prover_data = FieldMerkleTree::BuildOwned( +#if TACHYON_CUDA + *prover_data = FieldMerkleTree::MaybeBuildOwnedGpu( + hasher_, packed_hasher_, compressor_, packed_compressor_, + std::move(owned_matrices), icicle_); +#else + *prover_data = FieldMerkleTree::BuildOwnedCpu( hasher_, packed_hasher_, compressor_, packed_compressor_, std::move(owned_matrices)); +#endif *commitment = prover_data->GetRoot(); return true; @@ -255,6 +271,10 @@ class FieldMerkleTreeMMCS final PackedHasher packed_hasher_; Compressor compressor_; PackedCompressor packed_compressor_; +#if TACHYON_CUDA + // not owned + IcicleMMCSHolder* icicle_ = nullptr; +#endif }; template >> matrices_tmp = matrices; Tree tree = - Tree::Build(mmcs_.hasher(), mmcs_.packed_hasher(), mmcs_.compressor(), - mmcs_.packed_compressor(), std::move(matrices_tmp)); + Tree::BuildCpu(mmcs_.hasher(), mmcs_.packed_hasher(), mmcs_.compressor(), + mmcs_.packed_compressor(), std::move(matrices_tmp)); { std::array commitment; @@ -90,9 +90,9 @@ TEST_F(FieldMerkleTreeMMCSTest, CommitOwned) { std::vector> matrices = {matrix}; std::vector> matrices_tmp = matrices; - Tree tree = Tree::BuildOwned(mmcs_.hasher(), mmcs_.packed_hasher(), - mmcs_.compressor(), mmcs_.packed_compressor(), - std::move(matrices_tmp)); + Tree tree = Tree::BuildOwnedCpu(mmcs_.hasher(), mmcs_.packed_hasher(), + mmcs_.compressor(), mmcs_.packed_compressor(), + std::move(matrices_tmp)); { std::array commitment; diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc index 2d5d26b095..709c36a9b3 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_unittest.cc @@ -55,8 +55,8 @@ TEST_F(FieldMerkleTreeTest, CommitSingle1x8) { }; std::vector> matrices = {matrix}; - Tree tree = Tree::BuildOwned(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices)); + Tree tree = Tree::BuildOwnedCpu(hasher_, packed_hasher_, compressor_, + packed_compressor_, std::move(matrices)); auto h0_1 = compressor_.Compress(std::vector>{ hasher_.Hash(std::vector{matrix(0, 0)}), @@ -86,8 +86,8 @@ TEST_F(FieldMerkleTreeTest, CommitSingle2x2) { }; std::vector> matrices = {matrix}; - Tree tree = Tree::BuildOwned(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices)); + Tree tree = Tree::BuildOwnedCpu(hasher_, packed_hasher_, compressor_, + packed_compressor_, std::move(matrices)); auto expected = compressor_.Compress(std::vector>{ hasher_.Hash(std::vector{matrix(0, 0), matrix(0, 1)}), @@ -103,8 +103,8 @@ TEST_F(FieldMerkleTreeTest, CommitSingle2x3) { }; std::vector> matrices = {matrix}; - Tree tree = Tree::BuildOwned(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices)); + Tree tree = Tree::BuildOwnedCpu(hasher_, packed_hasher_, compressor_, + packed_compressor_, std::move(matrices)); std::array default_digest = base::CreateArray([]() { return F::Zero(); }); auto h0_3 = compressor_.Compress(std::vector>{ @@ -130,8 +130,8 @@ TEST_F(FieldMerkleTreeTest, CommitMixed) { }; std::vector> matrices = {matrix, matrix2}; - Tree tree = Tree::BuildOwned(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices)); + Tree tree = Tree::BuildOwnedCpu(hasher_, packed_hasher_, compressor_, + packed_compressor_, std::move(matrices)); std::array default_digest = base::CreateArray([]() { return F::Zero(); }); auto h0_3 = compressor_.Compress(std::vector>{ @@ -158,12 +158,12 @@ TEST_F(FieldMerkleTreeTest, CommitEitherOrder) { math::RowMajorMatrix matrix2 = math::RowMajorMatrix::Random(2, 3); std::vector> matrices = {matrix, matrix2}; - Tree tree = Tree::BuildOwned(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices)); + Tree tree = Tree::BuildOwnedCpu(hasher_, packed_hasher_, compressor_, + packed_compressor_, std::move(matrices)); std::vector> matrices2 = {matrix2, matrix}; - Tree tree2 = Tree::BuildOwned(hasher_, packed_hasher_, compressor_, - packed_compressor_, std::move(matrices2)); + Tree tree2 = Tree::BuildOwnedCpu(hasher_, packed_hasher_, compressor_, + packed_compressor_, std::move(matrices2)); EXPECT_EQ(tree.GetRoot(), tree2.GetRoot()); } From 12e12e2c3b5e0914ab36c80de03e9f5ce66e3537 Mon Sep 17 00:00:00 2001 From: Ryan Kim Date: Mon, 30 Sep 2024 19:41:51 +0900 Subject: [PATCH 24/25] test(crypto): add mmcs gpu unittest --- .../merkle_tree/field_merkle_tree/BUILD.bazel | 16 +++ .../field_merkle_tree_gpu_unittest.cc | 104 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_gpu_unittest.cc diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel index bc7ae41523..97c80f9f81 100644 --- a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/BUILD.bazel @@ -1,3 +1,4 @@ +load("//bazel:tachyon.bzl", "if_gpu_is_configured") load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest") package(default_visibility = ["//visibility:public"]) @@ -60,3 +61,18 @@ tachyon_cc_unittest( "//tachyon/math/finite_fields/test:finite_field_test", ], ) + +tachyon_cc_unittest( + name = "field_merkle_tree_gpu_unittests", + srcs = if_gpu_is_configured(["field_merkle_tree_gpu_unittest.cc"]), + deps = [ + ":field_merkle_tree_mmcs", + "//tachyon/crypto/hashes/sponge:padding_free_sponge", + "//tachyon/crypto/hashes/sponge:truncated_permutation", + "//tachyon/crypto/hashes/sponge/poseidon2", + "//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_params", + "//tachyon/crypto/hashes/sponge/poseidon2/icicle:icicle_poseidon2_holder", + "//tachyon/crypto/hashes/sponge/poseidon2/param_traits:poseidon2_baby_bear", + "//tachyon/math/finite_fields/test:finite_field_test", + ], +) diff --git a/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_gpu_unittest.cc b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_gpu_unittest.cc new file mode 100644 index 0000000000..d5a49d17be --- /dev/null +++ b/tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree_gpu_unittest.cc @@ -0,0 +1,104 @@ +#include +#include + +#include "gtest/gtest.h" + +#include "tachyon/crypto/commitments/merkle_tree/field_merkle_tree/field_merkle_tree.h" +#include "tachyon/crypto/hashes/sponge/padding_free_sponge.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/param_traits/poseidon2_baby_bear.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h" +#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_params.h" +#include "tachyon/crypto/hashes/sponge/truncated_permutation.h" +#include "tachyon/math/finite_fields/test/finite_field_test.h" +#include "tachyon/math/geometry/dimensions.h" + +#if TACHYON_CUDA +#include "tachyon/crypto/hashes/sponge/poseidon2/icicle/icicle_poseidon2_holder.h" +#endif + +namespace tachyon::crypto { + +constexpr size_t kRate = 8; +constexpr size_t kChunk = 8; +constexpr size_t kN = 2; + +using F = math::BabyBear; +using PackedF = math::PackedBabyBear; +using Params = Poseidon2Params; +using PackedParams = Poseidon2Params; +using Poseidon2 = Poseidon2Sponge; +using PackedPoseidon2 = Poseidon2Sponge; +using MyHasher = PaddingFreeSponge; +using MyPackedHasher = PaddingFreeSponge; +using MyCompressor = TruncatedPermutation; +using MyPackedCompressor = TruncatedPermutation; +using Tree = FieldMerkleTree; + +namespace { + +class FieldMerkleTreeGpuTest : public math::FiniteFieldTest { + protected: + MyHasher hasher_; + MyCompressor compressor_; + MyPackedHasher packed_hasher_; + MyPackedCompressor packed_compressor_; +}; + +} // namespace + +TEST_F(FieldMerkleTreeGpuTest, CommitGpu) { + struct Config { + size_t num; + math::Dimensions dimensions; + }; + + struct { + std::vector configs; + } tests[] = { + // NOTE(chokobole): Icicle supports only rows that are powers of 2. + {{{4, {8, 1024}}, {5, {8, 128}}, {6, {8, 8}}}}, + { + {{1, {1, 32}}, + {1, {2, 32}}, + {1, {3, 32}}, + {1, {4, 32}}, + {1, {5, 32}}, + {1, {6, 32}}, + {1, {7, 32}}, + {1, {8, 32}}, + {1, {9, 32}}, + {1, {10, 32}}}, + }, + }; + + IciclePoseidon2Holder poseidon2_holder = + IciclePoseidon2Holder::Create(hasher_.derived().config); + IcicleMMCSHolder mmcs_holder = IcicleMMCSHolder::Create( + poseidon2_holder.get()->impl(), poseidon2_holder.get()->impl()); + + for (const auto& test : tests) { + std::vector> matrices; + for (size_t i = 0; i < test.configs.size(); ++i) { + math::Dimensions dimensions = test.configs[i].dimensions; + for (size_t j = 0; j < test.configs[i].num; ++j) { + matrices.push_back(math::RowMajorMatrix::Random(dimensions.height, + dimensions.width)); + } + } + + std::vector> matrices_tmp = matrices; + Tree tree_cpu = Tree::MaybeBuildOwnedGpu(hasher_, packed_hasher_, + compressor_, packed_compressor_, + std::move(matrices_tmp), nullptr); + Tree tree_gpu = Tree::MaybeBuildOwnedGpu(hasher_, packed_hasher_, + compressor_, packed_compressor_, + std::move(matrices), &mmcs_holder); + EXPECT_EQ(tree_cpu.GetRoot(), tree_gpu.GetRoot()); + EXPECT_EQ(tree_cpu.leaves(), tree_gpu.leaves()); + EXPECT_EQ(tree_cpu.digest_layers(), tree_gpu.digest_layers()); + } +} + +} // namespace tachyon::crypto From 06601b5866afb2e5ec9b05dc1301b48b87dc6c4c Mon Sep 17 00:00:00 2001 From: GideokKim Date: Tue, 8 Oct 2024 12:47:33 +0900 Subject: [PATCH 25/25] ci: add `whitespace/indent_namespace` filter Since cpplint 2.0.0, indent inspection has been added but still contains a bug. This commit filters out the indent inspection to avoid issues caused by a bug. See https://github.com/cpplint/cpplint/pull/235 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b1f34165d..d8b9960a02 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -134,7 +134,7 @@ jobs: - name: Run cpplint if: github.event_name == 'pull_request' - run: git diff --name-status HEAD origin/${{ github.base_ref }} | cut -f 2,3 | xargs cpplint --filter=-legal/copyright,-whitespace/line_length,-build/namespaces,-runtime/references + run: git diff --name-status HEAD origin/${{ github.base_ref }} | cut -f 2,3 | xargs cpplint --filter=-legal/copyright,-whitespace/line_length,-whitespace/indent_namespace,-build/namespaces,-runtime/references - name: Run clang-format lint uses: jidicula/clang-format-action@v4.11.0