Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pedersen.test.cpp
Go to the documentation of this file.
1#include "pedersen.hpp"
5#include <gtest/gtest.h>
6
7namespace bb::crypto {
8
9using bb::fr;
10
11// Verifies the domain-seperated "pedersen_hash_length" generator matches the expected
12TEST(Pedersen, DeriveLengthGenerator)
13{
14 auto generator = pedersen_hash::length_generator;
15 std::cout << generator << std::endl;
16 EXPECT_EQ(generator,
18 fr(uint256_t("0x2df8b940e5890e4e1377e05373fae69a1d754f6935e6a780b666947431f2cdcd")),
19 fr(uint256_t("0x2ecd88d15967bc53b885912e0d16866154acb6aac2d3f85e27ca7eefb2c19083"))));
20}
21
22// Verifies that hashing {1, 1} produces the expected result
24{
25 auto x = pedersen_hash::Fq::one();
26 auto r = pedersen_hash::hash({ x, x });
27 EXPECT_EQ(r, fr(uint256_t("07ebfbf4df29888c6cd6dca13d4bb9d1a923013ddbbcbdc3378ab8845463297b")));
28}
29
30// Verifies that hashing {1, 1} with a generator-offset/context variant produces the expected result
31TEST(Pedersen, HashWithIndex)
32{
33 auto x = pedersen_hash::Fq::one();
34 auto r = pedersen_hash::hash({ x, x }, 5);
35 EXPECT_EQ(r, fr(uint256_t("1c446df60816b897cda124524e6b03f36df0cec333fad87617aab70d7861daa6")));
36}
37
38// Verifies that hashing a 32-byte buffer is equivalent to hashing two field elements via the intended chaining
39TEST(Pedersen, Hash32Bytes)
40{
41 using Fq = pedersen_hash::Fq;
42
43 std::vector<uint8_t> buf(32);
44 for (size_t i = 0; i < buf.size(); ++i) {
45 buf[i] = static_cast<uint8_t>(0xA0 + i);
46 }
47
48 // First 31-byte chunk
49 uint256_t acc0(0);
50 for (size_t i = 0; i < 31; ++i) {
51 acc0 = (acc0 << uint256_t(8));
52 acc0 += uint256_t(buf[i]);
53 }
54 Fq element0(acc0);
55
56 // Last 1-byte chunk
57 uint256_t acc1(0);
58 acc1 = (acc1 << uint256_t(8));
59 acc1 += uint256_t(buf[31]);
60 Fq element1(acc1);
61
62 // For exactly 2 elements, hash_buffer should equal hash({element0, element1})
63 auto expected = pedersen_hash::hash({ element0, element1 });
65
66 EXPECT_EQ(got, expected);
67}
68
69} // namespace bb::crypto
typename Curve::BaseField Fq
Definition pedersen.hpp:34
static Fq hash_buffer(const std::vector< uint8_t > &input, GeneratorContext context={})
Given an arbitrary length of bytes, convert them to fields and hash the result using the default gene...
Definition pedersen.cpp:88
static constexpr AffineElement length_generator
Definition pedersen.hpp:38
static Fq hash(const std::vector< Fq > &inputs, GeneratorContext context={})
Given a vector of fields, generate a pedersen hash using generators from context.
Definition pedersen.cpp:78
uint8_t const * buf
Definition data_store.hpp:9
ContentAddressedIndexedTree< StoreType, PedersenHashPolicy > Pedersen
TEST(GeneratorContext, DeriveDefaultGenerators)
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr field one()