Skip to content

Commit

Permalink
feat: Pedersen in typescript. (AztecProtocol#3111)
Browse files Browse the repository at this point in the history
As title.
  • Loading branch information
charlielye authored Oct 27, 2023
1 parent c8e1d8b commit 933f1b2
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 1 deletion.
2 changes: 2 additions & 0 deletions barretenberg/cpp/scripts/bb-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ TESTS=(
crypto_ecdsa_tests
crypto_schnorr_tests
crypto_sha256_tests
crypto_pedersen_commitment_tests
crypto_pedersen_hash_tests
ecc_tests
numeric_tests
plonk_tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "pedersen.hpp"
#include "barretenberg/crypto/generators/generator_data.hpp"
#include <gtest/gtest.h>

namespace crypto {

using barretenberg::fr;

TEST(Pedersen, Commitment)
{
auto x = pedersen_commitment::Fq::one();
auto r = pedersen_commitment::commit_native({ x, x });
auto expected =
grumpkin::g1::affine_element(fr(uint256_t("2f7a8f9a6c96926682205fb73ee43215bf13523c19d7afe36f12760266cdfe15")),
fr(uint256_t("01916b316adbbf0e10e39b18c1d24b33ec84b46daddf72f43878bcc92b6057e6")));
EXPECT_EQ(r, expected);
}

} // namespace crypto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WASM_EXPORT void pedersen__hash_with_hash_index(uint8_t const* inputs_buffer, ui
{
std::vector<grumpkin::fq> to_hash;
read(inputs_buffer, to_hash);
crypto::GeneratorContext<curve::Grumpkin> ctx; // todo fix
crypto::GeneratorContext<curve::Grumpkin> ctx;
ctx.offset = static_cast<size_t>(hash_index);
auto r = crypto::pedersen_hash::hash(to_hash, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "pedersen.hpp"
#include "barretenberg/crypto/generators/generator_data.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include <gtest/gtest.h>

namespace crypto {

using barretenberg::fr;

TEST(Pedersen, Hash)
{
auto x = pedersen_hash::Fq::one();
auto r = pedersen_hash::hash({ x, x });
EXPECT_EQ(r, fr(uint256_t("07ebfbf4df29888c6cd6dca13d4bb9d1a923013ddbbcbdc3378ab8845463297b")));
}

TEST(Pedersen, HashWithIndex)
{
auto x = pedersen_hash::Fq::one();
auto r = pedersen_hash::hash({ x, x }, 5);
EXPECT_EQ(r, fr(uint256_t("1c446df60816b897cda124524e6b03f36df0cec333fad87617aab70d7861daa6")));
}

} // namespace crypto
1 change: 1 addition & 0 deletions yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"@koa/cors": "^4.0.0",
"@noble/curves": "^1.2.0",
"debug": "^4.3.4",
"detect-node": "^2.1.0",
"hash.js": "^1.1.7",
Expand Down
22 changes: 22 additions & 0 deletions yarn-project/foundation/src/crypto/pedersen/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { toBufferBE } from '../../bigint-buffer/index.js';
import { pedersenCommit, pedersenHashWithHashIndex } from './index.js';

describe('pedersen', () => {
it('pedersen commit', () => {
const r = pedersenCommit([toBufferBE(1n, 32), toBufferBE(1n, 32)]);
expect(r).toEqual([
Buffer.from('2f7a8f9a6c96926682205fb73ee43215bf13523c19d7afe36f12760266cdfe15', 'hex'),
Buffer.from('01916b316adbbf0e10e39b18c1d24b33ec84b46daddf72f43878bcc92b6057e6', 'hex'),
]);
});

it('pedersen hash', () => {
const r = pedersenHashWithHashIndex([toBufferBE(1n, 32), toBufferBE(1n, 32)]);
expect(r).toEqual(Buffer.from('07ebfbf4df29888c6cd6dca13d4bb9d1a923013ddbbcbdc3378ab8845463297b', 'hex'));
});

it('pedersen hash with index', () => {
const r = pedersenHashWithHashIndex([toBufferBE(1n, 32), toBufferBE(1n, 32)], 5);
expect(r).toEqual(Buffer.from('1c446df60816b897cda124524e6b03f36df0cec333fad87617aab70d7861daa6', 'hex'));
});
});
Loading

0 comments on commit 933f1b2

Please sign in to comment.