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"
4#include <gtest/gtest.h>
5
6namespace bb::crypto {
7
8using bb::fr;
9
10// Verifies the domain-seperated "pedersen_hash_length" generator matches the expected
11TEST(Pedersen, DeriveLengthGenerator)
12{
13 auto generator = pedersen_hash::length_generator;
14 std::cout << generator << std::endl;
15 EXPECT_EQ(generator,
17 fr(uint256_t("0x2df8b940e5890e4e1377e05373fae69a1d754f6935e6a780b666947431f2cdcd")),
18 fr(uint256_t("0x2ecd88d15967bc53b885912e0d16866154acb6aac2d3f85e27ca7eefb2c19083"))));
19}
20
21// Verifies that hashing {1, 1} produces the expected result
22TEST(Pedersen, Hash)
23{
24 auto x = pedersen_hash::Fq::one();
25 auto r = pedersen_hash::hash({ x, x });
26 EXPECT_EQ(r, fr(uint256_t("07ebfbf4df29888c6cd6dca13d4bb9d1a923013ddbbcbdc3378ab8845463297b")));
27}
28
29// Verifies that hashing {1, 1} with a generator-offset/context variant produces the expected result
30TEST(Pedersen, HashWithIndex)
31{
32 auto x = pedersen_hash::Fq::one();
33 auto r = pedersen_hash::hash({ x, x }, 5);
34 EXPECT_EQ(r, fr(uint256_t("1c446df60816b897cda124524e6b03f36df0cec333fad87617aab70d7861daa6")));
35}
36
37// Verifies that hashing a 32-byte buffer is equivalent to hashing two field elements via the intended chaining
38TEST(Pedersen, Hash32Bytes)
39{
40 using Fq = pedersen_hash::Fq;
41
42 std::vector<uint8_t> buf(32);
43 for (size_t i = 0; i < buf.size(); ++i) {
44 buf[i] = static_cast<uint8_t>(0xA0 + i);
45 }
46
47 // First 31-byte chunk
48 uint256_t acc0(0);
49 for (size_t i = 0; i < 31; ++i) {
50 acc0 = (acc0 << uint256_t(8));
51 acc0 += uint256_t(buf[i]);
52 }
53 Fq element0(acc0);
54
55 // Last 1-byte chunk
56 uint256_t acc1(0);
57 acc1 = (acc1 << uint256_t(8));
58 acc1 += uint256_t(buf[31]);
59 Fq element1(acc1);
60
61 // For exactly 2 elements, hash_buffer should equal hash({element0, element1})
62 auto expected = pedersen_hash::hash({ element0, element1 });
64
65 EXPECT_EQ(got, expected);
66}
67
68} // 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
field< Bn254FrParams > fr
Definition fr.hpp:155
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13