Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static std::vector<fr> VALUES = []() {
void hash(State& state) noexcept
{
for (auto _ : state) {
hash_pair_native({ 0, 0, 0, 0 }, { 1, 1, 1, 1 });
PedersenHashPolicy::hash_pair({ 0, 0, 0, 0 }, { 1, 1, 1, 1 });
}
}
BENCHMARK(hash)->MinTime(5);
Expand Down
54 changes: 0 additions & 54 deletions barretenberg/cpp/src/barretenberg/crypto/merkle_tree/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
// =====================

#pragma once
#include "barretenberg/common/assert.hpp"
#include "barretenberg/common/net.hpp"
#include "barretenberg/crypto/blake2s/blake2s.hpp"
#include "barretenberg/crypto/pedersen_commitment/pedersen.hpp"
#include "barretenberg/crypto/pedersen_hash/pedersen.hpp"
#include "barretenberg/crypto/poseidon2/poseidon2.hpp"
#include "barretenberg/numeric/bitop/pow.hpp"
#include <vector>

namespace bb::crypto::merkle_tree {
Expand All @@ -35,53 +30,4 @@ struct Poseidon2HashPolicy {
static fr zero_hash() { return fr::zero(); }
};

inline bb::fr hash_pair_native(bb::fr const& lhs, bb::fr const& rhs)
{
return crypto::pedersen_hash::hash({ lhs, rhs }); // uses lookup tables
}

inline bb::fr hash_native(std::vector<bb::fr> const& inputs)
{
return crypto::pedersen_hash::hash(inputs); // uses lookup tables
}

/**
* Computes the root of a tree with leaves given as the vector `input`.
*
* @param input: vector of leaf values.
* @returns root as field
*/
inline bb::fr compute_tree_root_native(std::vector<bb::fr> const& input)
{
BB_ASSERT(numeric::is_power_of_two(input.size()), "Check if the input vector size is a power of 2.");
auto layer = input;
while (layer.size() > 1) {
std::vector<bb::fr> next_layer(layer.size() / 2);
for (size_t i = 0; i < next_layer.size(); ++i) {
next_layer[i] = crypto::pedersen_hash::hash({ layer[i * 2], layer[i * 2 + 1] });
}
layer = std::move(next_layer);
}

return layer[0];
}

// TODO write test
inline std::vector<bb::fr> compute_tree_native(std::vector<bb::fr> const& input)
{
BB_ASSERT(numeric::is_power_of_two(input.size()), "Check if the input vector size is a power of 2.");
auto layer = input;
std::vector<bb::fr> tree(input);
while (layer.size() > 1) {
std::vector<bb::fr> next_layer(layer.size() / 2);
for (size_t i = 0; i < next_layer.size(); ++i) {
next_layer[i] = crypto::pedersen_hash::hash({ layer[i * 2], layer[i * 2 + 1] });
tree.push_back(next_layer[i]);
}
layer = std::move(next_layer);
}

return tree;
}

} // namespace bb::crypto::merkle_tree
31 changes: 0 additions & 31 deletions barretenberg/cpp/src/barretenberg/crypto/merkle_tree/hash_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ using fr_hash_path = std::vector<std::pair<fr, fr>>;
using fr_sibling_path = std::vector<fr>;
template <typename Ctx> using hash_path = std::vector<std::pair<bb::stdlib::field_t<Ctx>, bb::stdlib::field_t<Ctx>>>;

inline fr_hash_path get_new_hash_path(fr_hash_path const& old_path, uint128_t index, fr const& value)
{
fr_hash_path path = old_path;
fr current = value;
for (size_t i = 0; i < old_path.size(); ++i) {
bool path_bit = static_cast<bool>(index & 0x1);
if (path_bit) {
path[i].second = current;
} else {
path[i].first = current;
}
current = hash_pair_native(path[i].first, path[i].second);
index /= 2;
}
return path;
}

inline fr_hash_path get_random_hash_path(size_t const& tree_depth)
{
fr_hash_path path;
Expand All @@ -52,20 +35,6 @@ template <typename Ctx> inline hash_path<Ctx> create_witness_hash_path(Ctx& ctx,
return result;
}

inline fr get_hash_path_root(fr_hash_path const& input)
{
return hash_pair_native(input[input.size() - 1].first, input[input.size() - 1].second);
}

inline fr zero_hash_at_height(size_t height)
{
auto current = fr(0);
for (size_t i = 0; i < height; ++i) {
current = hash_pair_native(current, current);
}
return current;
}

} // namespace bb::crypto::merkle_tree

// We add to std namespace as fr_hash_path is actually a std::vector, and this is the only way
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool check_hash_path(const fr& root,
for (size_t i = 0; i < depth_; ++i) {
fr left = (index & 1) ? path[i].first : current;
fr right = (index & 1) ? current : path[i].second;
current = hash_pair_native(left, right);
current = HashPolicy::hash_pair(left, right);
index >>= 1;
}
return current == root;
Expand Down
4 changes: 2 additions & 2 deletions boxes/boxes/vanilla/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub contract PrivateVoting {
storage::storage,
};
use dep::aztec::state_vars::{Map, PublicImmutable, PublicMutable};
use dep::aztec::protocol_types::{address::AztecAddress, traits::{Hash, ToField}};
use dep::aztec::protocol_types::{address::AztecAddress, hash::poseidon2_hash, traits::{Hash, ToField}};

#[storage]
struct Storage<Context> {
Expand All @@ -39,7 +39,7 @@ pub contract PrivateVoting {
let msg_sender_npk_m_hash = get_public_keys(self.msg_sender()).npk_m.hash();

let secret = self.context.request_nsk_app(msg_sender_npk_m_hash); // get secret key of caller of function
let nullifier = std::hash::pedersen_hash([self.msg_sender().to_field(), secret]); // derive nullifier from sender and secret
let nullifier = poseidon2_hash([self.msg_sender().to_field(), secret]); // derive nullifier from sender and secret
self.context.push_nullifier(nullifier);
self.enqueue_self.add_to_tally_public(candidate);
}
Expand Down
7 changes: 1 addition & 6 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ use dep::protocol_types::{
DOM_SEP__PUBLIC_CALLDATA, DOM_SEP__SECRET_HASH, MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS,
},
hash::{poseidon2_hash_subarray, poseidon2_hash_with_separator, sha256_to_field},
point::Point,
traits::ToField,
};

pub use dep::protocol_types::hash::{compute_siloed_nullifier, pedersen_hash};

pub fn pedersen_commitment<let N: u32>(inputs: [Field; N], hash_index: u32) -> Point {
std::hash::pedersen_commitment_with_separator(inputs, hash_index)
}
pub use dep::protocol_types::hash::compute_siloed_nullifier;

pub fn compute_secret_hash(secret: Field) -> Field {
poseidon2_hash_with_separator([secret], DOM_SEP__SECRET_HASH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use dep::aztec::macros::aztec;

#[aztec]
pub contract CardGame {
use dep::aztec::{hash::pedersen_hash, state_vars::{Map, PublicMutable}};
use dep::aztec::protocol_types::address::AztecAddress;
use dep::aztec::protocol_types::hash::poseidon2_hash;
use dep::aztec::state_vars::{Map, PublicMutable};

use dep::aztec::note::constants::MAX_NOTES_PER_PAGE;

Expand Down Expand Up @@ -103,7 +104,7 @@ pub contract CardGame {

let mut collection = self.storage.collections;
let _inserted_cards = collection.add_cards(cards, player);
self.enqueue_self.on_cards_claimed(game, player, pedersen_hash(cards_fields, 0));
self.enqueue_self.on_cards_claimed(game, player, poseidon2_hash(cards_fields));
}

#[external("public")]
Expand All @@ -117,7 +118,7 @@ pub contract CardGame {

assert_eq(
cards_hash,
pedersen_hash(game_data.rounds_cards.map(|card: Card| card.to_field()), 0),
poseidon2_hash(game_data.rounds_cards.map(|card: Card| card.to_field())),
);

let winner = game_data.winner();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::aztec::hash::pedersen_hash;
use dep::aztec::protocol_types::hash::poseidon2_hash;
use std::ops::{Add, Sub};

// Utility used to easily get a "id" for a private user that sits in the same
Expand All @@ -8,7 +8,7 @@ pub fn compute_identifier(secret: Field, on_behalf_of: Field, this: Field) -> Fi
// EITHER secret OR on_behalf_of MUST be set. But not both
assert(!((secret == 0) as bool & (on_behalf_of == 0) as bool));
if (secret != 0) {
pedersen_hash([this, secret], 0)
poseidon2_hash([this, secret])
} else {
on_behalf_of
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub contract Test {
},
// Event related
messages::message_delivery::MessageDelivery,
// Hashing
hash::pedersen_hash,
// History and inclusion proofs
history::note_inclusion::ProveNoteInclusion,
// Key management
Expand All @@ -51,6 +49,7 @@ pub contract Test {
// Contract instance management
publish_contract_instance::publish_contract_instance_for_public_execution,
};
use std::hash::pedersen_hash_with_separator;
use std::meta::derive;
use token_portal_content_hash_lib::get_mint_to_private_content_hash;

Expand Down Expand Up @@ -427,7 +426,7 @@ pub contract Test {
}

fn get_commitment(self) -> Field {
pedersen_hash([self.amount, self.secret_hash], 0)
pedersen_hash_with_separator([self.amount, self.secret_hash], 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,6 @@ pub fn accumulate_sha256(v0: Field, v1: Field) -> Field {
sha256_to_field(hash_input_flattened)
}

// TODO: remove this. The protocol doesn't need it.
#[inline_always]
pub fn pedersen_hash<let N: u32>(inputs: [Field; N], hash_index: u32) -> Field {
std::hash::pedersen_hash_with_separator(inputs, hash_index)
}

pub fn poseidon2_hash<let N: u32>(inputs: [Field; N]) -> Field {
poseidon::poseidon2::Poseidon2::hash(inputs, N)
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/simulators/lending_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Fr } from '@aztec/aztec.js/fields';
import { CheatCodes } from '@aztec/aztec/testing';
import type { RollupContract } from '@aztec/ethereum/contracts';
import { SlotNumber } from '@aztec/foundation/branded-types';
import { pedersenHash } from '@aztec/foundation/crypto/pedersen';
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
import type { TestDateProvider } from '@aztec/foundation/timer';
import type { LendingContract } from '@aztec/noir-contracts.js/Lending';

Expand All @@ -29,7 +29,7 @@ export class LendingAccount {
* @returns Key in public space
*/
public key() {
return pedersenHash([this.address, this.secret]);
return poseidon2Hash([this.address, this.secret]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { pedersenMerkleHash, poseidonMerkleHash, shaMerkleHash } from './hasher.js';
import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';

export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);

export const computeBalancedPedersenRoot = async (leaves: Buffer[]) =>
await computeBalancedMerkleTreeRootAsync(leaves, pedersenMerkleHash);

export const computeBalancedPoseidonRoot = async (leaves: Buffer[]) =>
await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);

Expand Down Expand Up @@ -33,7 +30,7 @@ export function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher = shaMerk

/**
* Computes the Merkle root with the provided leaves **asynchronously**.
* This method uses an asynchronous hash function (defaults to `pedersenHash`).
* This method uses an asynchronous hash function (defaults to `poseidon2Hash`).
*
* @throws If the number of leaves is not a power of two.
*/
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/foundation/src/trees/hasher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { pedersenHash as pedersenHashArray } from '../crypto/pedersen/index.js';
import { poseidon2Hash } from '../crypto/poseidon/index.js';
import { sha256Trunc } from '../crypto/sha256/index.js';

Expand Down Expand Up @@ -45,8 +44,5 @@ export interface AsyncHasher {
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;

export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;

export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
4 changes: 2 additions & 2 deletions yarn-project/foundation/src/trees/merkle_tree_calculator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pedersenHash } from '@aztec/foundation/crypto/pedersen';
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';

import type { AsyncHasher } from './hasher.js';
import { MerkleTree } from './merkle_tree.js';
Expand All @@ -19,7 +19,7 @@ export class MerkleTreeCalculator {
height: number,
zeroLeaf: Buffer = Buffer.alloc(32),
hasher = async (left: Buffer, right: Buffer) =>
(await pedersenHash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
) {
const zeroHashes = [zeroLeaf];
for (let i = 0; i < height; i++) {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/foundation/src/trees/sibling_path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeTuple } from '../array/array.js';
import { pedersenHash } from '../crypto/pedersen/index.js';
import { poseidon2Hash } from '../crypto/poseidon/index.js';
import { Fr } from '../curves/bn254/index.js';
import { schemas } from '../schemas/index.js';
import {
Expand Down Expand Up @@ -172,7 +172,7 @@ export async function computeRootFromSiblingPath(
leaf: Buffer,
siblingPath: Buffer[],
index: number,
hasher = async (left: Buffer, right: Buffer) => (await pedersenHash([left, right])).toBuffer(),
hasher = async (left: Buffer, right: Buffer) => (await poseidon2Hash([left, right])).toBuffer(),
) {
let result = leaf;
for (const sibling of siblingPath) {
Expand Down
1 change: 0 additions & 1 deletion yarn-project/merkle-tree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from './interfaces/append_only_tree.js';
export * from './interfaces/indexed_tree.js';
export * from './interfaces/merkle_tree.js';
export * from './interfaces/update_only_tree.js';
export * from './pedersen.js';
export * from './poseidon.js';
export * from './sparse_tree/sparse_tree.js';
export { StandardIndexedTree } from './standard_indexed_tree/standard_indexed_tree.js';
Expand Down
30 changes: 0 additions & 30 deletions yarn-project/merkle-tree/src/pedersen.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FromBuffer } from '@aztec/foundation/serialize';
import type { AztecKVStore } from '@aztec/kv-store';
import { openTmpStore } from '@aztec/kv-store/lmdb';

import { Pedersen, StandardTree, newTree } from '../index.js';
import { Poseidon, StandardTree, newTree } from '../index.js';
import { AppendOnlySnapshotBuilder } from './append_only_snapshot.js';
import { describeSnapshotBuilderTestSuite } from './snapshot_builder_test_suite.js';

Expand All @@ -14,7 +14,7 @@ describe('AppendOnlySnapshot', () => {

beforeEach(async () => {
db = openTmpStore();
const hasher = new Pedersen();
const hasher = new Poseidon();
const deserializer: FromBuffer<Buffer> = { fromBuffer: b => b };
tree = await newTree(StandardTree, db, hasher, 'test', deserializer, 4);
snapshotBuilder = new AppendOnlySnapshotBuilder(db, tree, hasher, deserializer);
Expand Down
Loading
Loading