From 72931bdb8202c34042cdfb8cee2ef44b75939879 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 20 May 2024 13:52:19 +0100 Subject: [PATCH] chore: add c++ tests for generator derivation (#6528) This PR adds some tests just to give ground truth values for `hash_to_curve` and `derive_generators` which can replicated in test suites for reimplementations of this logic [elsewhere](https://github.com/noir-lang/noir/pull/4871) --- .../src/barretenberg/crypto/CMakeLists.txt | 1 + .../crypto/generators/CMakeLists.txt | 1 + .../crypto/generators/generator_data.test.cpp | 45 +++++++++++++++++++ .../crypto/pedersen_hash/pedersen.test.cpp | 11 +++++ .../ecc/groups/affine_element.test.cpp | 30 ++++++++++++- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 barretenberg/cpp/src/barretenberg/crypto/generators/CMakeLists.txt create mode 100644 barretenberg/cpp/src/barretenberg/crypto/generators/generator_data.test.cpp diff --git a/barretenberg/cpp/src/barretenberg/crypto/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/crypto/CMakeLists.txt index 5ea623e144e..1319f7bc668 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/crypto/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(hmac) add_subdirectory(blake2s) add_subdirectory(blake3s) add_subdirectory(blake3s_full) +add_subdirectory(generators) add_subdirectory(keccak) add_subdirectory(pedersen_commitment) add_subdirectory(pedersen_hash) diff --git a/barretenberg/cpp/src/barretenberg/crypto/generators/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/crypto/generators/CMakeLists.txt new file mode 100644 index 00000000000..73215b66e35 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/crypto/generators/CMakeLists.txt @@ -0,0 +1 @@ +barretenberg_module(crypto_generator_data ecc) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/crypto/generators/generator_data.test.cpp b/barretenberg/cpp/src/barretenberg/crypto/generators/generator_data.test.cpp new file mode 100644 index 00000000000..c3bea7555f3 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/crypto/generators/generator_data.test.cpp @@ -0,0 +1,45 @@ +#include "generator_data.hpp" +#include "barretenberg/crypto/pedersen_commitment/c_bind.hpp" +#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" +#include +#include + +namespace bb::crypto { + +TEST(GeneratorContext, DeriveDefaultGenerators) +{ + auto default_generators = generator_data::make_precomputed_generators(); + std::vector expected_default_generators; + + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("083e7911d835097629f0067531fc15cafd79a89beecb39903f69572c636f4a5a")), + fr(uint256_t("1a7f5efaad7f315c25a918f30cc8d7333fccab7ad7c90f14de81bcc528f9935d")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402")), + fr(uint256_t("209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("1c44f2a5207c81c28a8321a5815ce8b1311024bbed131819bbdaf5a2ada84748")), + fr(uint256_t("03aaee36e6422a1d0191632ac6599ae9eba5ac2c17a8c920aa3caf8b89c5f8a8")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("26d8b1160c6821a30c65f6cb47124afe01c29f4338f44d4a12c9fccf22fb6fb2")), + fr(uint256_t("05c70c3b9c0d25a4c100e3a27bf3cc375f8af8cdd9498ec4089a823d7464caff")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("20ed9c6a1d27271c4498bfce0578d59db1adbeaa8734f7facc097b9b994fcf6e")), + fr(uint256_t("29cd7d370938b358c62c4a00f73a0d10aba7e5aaa04704a0713f891ebeb92371")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("0224a8abc6c8b8d50373d64cd2a1ab1567bf372b3b1f7b861d7f01257052d383")), + fr(uint256_t("2358629b90eafb299d6650a311e79914b0215eb0a790810b26da5a826726d711")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("0f106f6d46bc904a5290542490b2f238775ff3c445b2f8f704c466655f460a2a")), + fr(uint256_t("29ab84d472f1d33f42fe09c47b8f7710f01920d6155250126731e486877bcf27")) })); + expected_default_generators.emplace_back(grumpkin::g1::affine_element( + { fr(uint256_t("0298f2e42249f0519c8a8abd91567ebe016e480f219b8c19461d6a595cc33696")), + fr(uint256_t("035bec4b8520a4ece27bd5aafabee3dfe1390d7439c419a8c55aceb207aac83b")) })); + + EXPECT_EQ(default_generators.size(), expected_default_generators.size()); + for (size_t i = 0; i < default_generators.size(); ++i) { + EXPECT_EQ(default_generators[i], expected_default_generators[i]); + } +} + +} // namespace bb::crypto \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/pedersen.test.cpp b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/pedersen.test.cpp index 0e4ade6fca1..e920357626f 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/pedersen.test.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/pedersen.test.cpp @@ -1,5 +1,6 @@ #include "pedersen.hpp" #include "barretenberg/crypto/generators/generator_data.hpp" +#include "barretenberg/crypto/pedersen_commitment/c_bind.hpp" #include "barretenberg/numeric/uint256/uint256.hpp" #include @@ -7,6 +8,16 @@ namespace bb::crypto { using bb::fr; +TEST(Pedersen, DeriveLengthGenerator) +{ + auto generator = pedersen_hash::length_generator; + std::cout << generator << std::endl; + EXPECT_EQ(generator, + grumpkin::g1::affine_element( + fr(uint256_t("0x2df8b940e5890e4e1377e05373fae69a1d754f6935e6a780b666947431f2cdcd")), + fr(uint256_t("0x2ecd88d15967bc53b885912e0d16866154acb6aac2d3f85e27ca7eefb2c19083")))); +} + TEST(Pedersen, Hash) { auto x = pedersen_hash::Fq::one(); diff --git a/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp b/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp index 4c36b16c9d4..94b24d95872 100644 --- a/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp @@ -12,6 +12,7 @@ #include #include #include +#include using ::testing::Each; using ::testing::ElementsAreArray; @@ -254,4 +255,31 @@ TYPED_TEST(TestAffineElement, BatchEndomoprhismByMinusOne) } else { GTEST_SKIP(); } -} \ No newline at end of file +} + +TEST(AffineElement, HashToCurve) +{ + std::vector, grumpkin::g1::affine_element>> test_vectors; + test_vectors.emplace_back(std::vector(), + grumpkin::g1::affine_element( + fr(uint256_t("24c4cb9c1206ab5470592f237f1698abe684dadf0ab4d7a132c32b2134e2c12e")), + fr(uint256_t("0668b8d61a317fb34ccad55c930b3554f1828a0e5530479ecab4defe6bbc0b2e")))); + + test_vectors.emplace_back(std::vector{ 1 }, + grumpkin::g1::affine_element( + fr(uint256_t("107f1b633c6113f3222f39f6256f0546b41a4880918c86864b06471afb410454")), + fr(uint256_t("050cd3823d0c01590b6a50adcc85d2ee4098668fd28805578aa05a423ea938c6")))); + + // "hello world" + test_vectors.emplace_back(std::vector{ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 }, + grumpkin::g1::affine_element( + fr(uint256_t("037c5c229ae495f6e8d1b4bf7723fafb2b198b51e27602feb8a4d1053d685093")), + fr(uint256_t("10cf9596c5b2515692d930efa2cf3817607e4796856a79f6af40c949b066969f")))); + + for (std::tuple, grumpkin::g1::affine_element> test_case : test_vectors) { + auto result = grumpkin::g1::affine_element::hash_to_curve(std::get<0>(test_case), 0); + auto expected_result = std::get<1>(test_case); + std::cout << result << std::endl; + EXPECT_TRUE(result == expected_result); + } +}