Skip to content

Commit

Permalink
chore: add c++ tests for generator derivation (AztecProtocol#6528)
Browse files Browse the repository at this point in the history
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](noir-lang/noir#4871)
  • Loading branch information
TomAFrench authored May 20, 2024
1 parent 22ad5a5 commit 72931bd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barretenberg_module(crypto_generator_data ecc)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "generator_data.hpp"
#include "barretenberg/crypto/pedersen_commitment/c_bind.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include <gtest/gtest.h>
#include <vector>

namespace bb::crypto {

TEST(GeneratorContext, DeriveDefaultGenerators)
{
auto default_generators = generator_data<curve::Grumpkin>::make_precomputed_generators();
std::vector<grumpkin::g1::affine_element> 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#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 <gtest/gtest.h>

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fstream>
#include <gtest/gtest.h>
#include <iterator>
#include <tuple>

using ::testing::Each;
using ::testing::ElementsAreArray;
Expand Down Expand Up @@ -254,4 +255,31 @@ TYPED_TEST(TestAffineElement, BatchEndomoprhismByMinusOne)
} else {
GTEST_SKIP();
}
}
}

TEST(AffineElement, HashToCurve)
{
std::vector<std::tuple<std::vector<uint8_t>, grumpkin::g1::affine_element>> test_vectors;
test_vectors.emplace_back(std::vector<uint8_t>(),
grumpkin::g1::affine_element(
fr(uint256_t("24c4cb9c1206ab5470592f237f1698abe684dadf0ab4d7a132c32b2134e2c12e")),
fr(uint256_t("0668b8d61a317fb34ccad55c930b3554f1828a0e5530479ecab4defe6bbc0b2e"))));

test_vectors.emplace_back(std::vector<uint8_t>{ 1 },
grumpkin::g1::affine_element(
fr(uint256_t("107f1b633c6113f3222f39f6256f0546b41a4880918c86864b06471afb410454")),
fr(uint256_t("050cd3823d0c01590b6a50adcc85d2ee4098668fd28805578aa05a423ea938c6"))));

// "hello world"
test_vectors.emplace_back(std::vector<uint8_t>{ 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<std::vector<uint8_t>, 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);
}
}

0 comments on commit 72931bd

Please sign in to comment.