Skip to content

Commit

Permalink
Merge branch 'master' into sync-noir
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm authored Feb 7, 2025
2 parents 5f316b7 + ee5fc45 commit 129abec
Show file tree
Hide file tree
Showing 116 changed files with 1,522 additions and 460 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace bb::crypto {
template <typename Params>
typename Poseidon2<Params>::FF Poseidon2<Params>::hash(const std::vector<typename Poseidon2<Params>::FF>& input)
{
return Sponge::hash_fixed_length(input);
return Sponge::hash_internal(input);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutat
* @brief Use the sponge to hash an input string
*
* @tparam out_len
* @tparam is_variable_length. Distinguishes between hashes where the preimage length is constant/not constant
* @param input
* @return std::array<FF, out_len>
*/
template <size_t out_len, bool is_variable_length>
static std::array<FF, out_len> hash_internal(std::span<const FF> input)
template <size_t out_len> static std::array<FF, out_len> hash_internal(std::span<const FF> input)
{
size_t in_len = input.size();
const uint256_t iv = (static_cast<uint256_t>(in_len) << 64) + out_len - 1;
Expand All @@ -140,30 +138,13 @@ template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutat
sponge.absorb(input[i]);
}

// In the case where the hash preimage is variable-length, we append `1` to the end of the input, to distinguish
// from fixed-length hashes. (the combination of this additional field element + the hash IV ensures
// fixed-length and variable-length hashes do not collide)
if constexpr (is_variable_length) {
sponge.absorb(1);
}

std::array<FF, out_len> output;
for (size_t i = 0; i < out_len; ++i) {
output[i] = sponge.squeeze();
}
return output;
}

template <size_t out_len> static std::array<FF, out_len> hash_fixed_length(std::span<const FF> input)
{
return hash_internal<out_len, false>(input);
}
static FF hash_fixed_length(std::span<const FF> input) { return hash_fixed_length<1>(input)[0]; }

template <size_t out_len> static std::array<FF, out_len> hash_variable_length(std::span<FF> input)
{
return hash_internal<out_len, true>(input);
}
static FF hash_variable_length(std::span<FF> input) { return hash_variable_length<1>(input)[0]; }
static FF hash_internal(std::span<const FF> input) { return hash_internal<1>(input)[0]; }
};
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ template <typename C> field_t<C> poseidon2<C>::hash(C& builder, const std::vecto
* This should just call the sponge variable length hash function
*
*/
return Sponge::hash_fixed_length(builder, inputs);
return Sponge::hash_internal(builder, inputs);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ template <size_t rate, size_t capacity, size_t t, typename Permutation, typename
* @param input
* @return std::array<field_t, out_len>
*/
template <size_t out_len, bool is_variable_length>
template <size_t out_len>
static std::array<field_t, out_len> hash_internal(Builder& builder, std::span<const field_t> input)
{
size_t in_len = input.size();
Expand All @@ -145,38 +145,16 @@ template <size_t rate, size_t capacity, size_t t, typename Permutation, typename
sponge.absorb(input[i]);
}

// In the case where the hash preimage is variable-length, we append `1` to the end of the input, to distinguish
// from fixed-length hashes. (the combination of this additional field element + the hash IV ensures
// fixed-length and variable-length hashes do not collide)
if constexpr (is_variable_length) {
sponge.absorb(1);
}

std::array<field_t, out_len> output;
for (size_t i = 0; i < out_len; ++i) {
output[i] = sponge.squeeze();
}
return output;
}

template <size_t out_len>
static std::array<field_t, out_len> hash_fixed_length(Builder& builder, std::span<const field_t> input)
{
return hash_internal<out_len, false>(builder, input);
}
static field_t hash_fixed_length(Builder& builder, std::span<const field_t> input)
{
return hash_fixed_length<1>(builder, input)[0];
}

template <size_t out_len>
static std::array<field_t, out_len> hash_variable_length(Builder& builder, std::span<field_t> input)
{
return hash_internal<out_len, true>(builder, input);
}
static field_t hash_variable_length(Builder& builder, std::span<field_t> input)
static field_t hash_internal(Builder& builder, std::span<const field_t> input)
{
return hash_variable_length<1>(builder, input)[0];
return hash_internal<1>(builder, input)[0];
}
};
} // namespace bb::stdlib
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ template <typename FF_>
class lookup_pow_2_0_relation : public GenericLookupRelation<lookup_pow_2_0_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_pow_2_0_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.alu_sel_shift_which.is_zero() && in.main_sel_rng_8.is_zero();
}
};
template <typename FF_> using lookup_pow_2_0 = GenericLookup<lookup_pow_2_0_lookup_settings, FF_>;

Expand Down Expand Up @@ -144,6 +149,11 @@ template <typename FF_>
class lookup_pow_2_1_relation : public GenericLookupRelation<lookup_pow_2_1_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_pow_2_1_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.alu_sel_shift_which.is_zero() && in.main_sel_rng_8.is_zero();
}
};
template <typename FF_> using lookup_pow_2_1 = GenericLookup<lookup_pow_2_1_lookup_settings, FF_>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ template <typename FF_>
class lookup_byte_lengths_relation : public GenericLookupRelation<lookup_byte_lengths_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_byte_lengths_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.binary_start.is_zero() && in.byte_lookup_sel_bin.is_zero();
}
};
template <typename FF_> using lookup_byte_lengths = GenericLookup<lookup_byte_lengths_lookup_settings, FF_>;

Expand Down Expand Up @@ -152,6 +157,11 @@ template <typename FF_>
class lookup_byte_operations_relation : public GenericLookupRelation<lookup_byte_operations_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_byte_operations_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.binary_sel_bin.is_zero() && in.byte_lookup_sel_bin.is_zero();
}
};
template <typename FF_> using lookup_byte_operations = GenericLookup<lookup_byte_operations_lookup_settings, FF_>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ template <typename FF_>
class lookup_opcode_gas_relation : public GenericLookupRelation<lookup_opcode_gas_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_opcode_gas_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.main_is_gas_accounted.is_zero() && in.gas_sel_gas_cost.is_zero();
}
};
template <typename FF_> using lookup_opcode_gas = GenericLookup<lookup_opcode_gas_lookup_settings, FF_>;

Expand Down Expand Up @@ -153,6 +158,11 @@ template <typename FF_>
class lookup_l2_gas_rng_chk_0_relation : public GenericLookupRelation<lookup_l2_gas_rng_chk_0_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_l2_gas_rng_chk_0_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.main_is_gas_accounted.is_zero() && in.main_sel_rng_16.is_zero();
}
};
template <typename FF_> using lookup_l2_gas_rng_chk_0 = GenericLookup<lookup_l2_gas_rng_chk_0_lookup_settings, FF_>;

Expand Down Expand Up @@ -218,6 +228,11 @@ template <typename FF_>
class lookup_l2_gas_rng_chk_1_relation : public GenericLookupRelation<lookup_l2_gas_rng_chk_1_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_l2_gas_rng_chk_1_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.main_is_gas_accounted.is_zero() && in.main_sel_rng_16.is_zero();
}
};
template <typename FF_> using lookup_l2_gas_rng_chk_1 = GenericLookup<lookup_l2_gas_rng_chk_1_lookup_settings, FF_>;

Expand Down Expand Up @@ -283,6 +298,11 @@ template <typename FF_>
class lookup_da_gas_rng_chk_0_relation : public GenericLookupRelation<lookup_da_gas_rng_chk_0_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_da_gas_rng_chk_0_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.main_is_gas_accounted.is_zero() && in.main_sel_rng_16.is_zero();
}
};
template <typename FF_> using lookup_da_gas_rng_chk_0 = GenericLookup<lookup_da_gas_rng_chk_0_lookup_settings, FF_>;

Expand Down Expand Up @@ -348,6 +368,11 @@ template <typename FF_>
class lookup_da_gas_rng_chk_1_relation : public GenericLookupRelation<lookup_da_gas_rng_chk_1_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_da_gas_rng_chk_1_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.main_is_gas_accounted.is_zero() && in.main_sel_rng_16.is_zero();
}
};
template <typename FF_> using lookup_da_gas_rng_chk_1 = GenericLookup<lookup_da_gas_rng_chk_1_lookup_settings, FF_>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ template <typename FF_>
class incl_main_tag_err_relation : public GenericLookupRelation<incl_main_tag_err_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = incl_main_tag_err_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.mem_tag_err.is_zero() && in.main_tag_err.is_zero();
}
};
template <typename FF_> using incl_main_tag_err = GenericLookup<incl_main_tag_err_lookup_settings, FF_>;

Expand Down Expand Up @@ -137,6 +142,11 @@ template <typename FF_>
class incl_mem_tag_err_relation : public GenericLookupRelation<incl_mem_tag_err_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = incl_mem_tag_err_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.main_tag_err.is_zero() && in.mem_tag_err.is_zero();
}
};
template <typename FF_> using incl_mem_tag_err = GenericLookup<incl_mem_tag_err_lookup_settings, FF_>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ template <typename FF_>
class lookup_mem_rng_chk_0_relation : public GenericLookupRelation<lookup_mem_rng_chk_0_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_mem_rng_chk_0_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.mem_sel_rng_chk.is_zero() && in.main_sel_rng_16.is_zero();
}
};
template <typename FF_> using lookup_mem_rng_chk_0 = GenericLookup<lookup_mem_rng_chk_0_lookup_settings, FF_>;

Expand Down Expand Up @@ -137,6 +142,11 @@ template <typename FF_>
class lookup_mem_rng_chk_1_relation : public GenericLookupRelation<lookup_mem_rng_chk_1_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_mem_rng_chk_1_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.mem_sel_rng_chk.is_zero() && in.main_sel_rng_16.is_zero();
}
};
template <typename FF_> using lookup_mem_rng_chk_1 = GenericLookup<lookup_mem_rng_chk_1_lookup_settings, FF_>;

Expand Down Expand Up @@ -202,6 +212,11 @@ template <typename FF_>
class lookup_mem_rng_chk_2_relation : public GenericLookupRelation<lookup_mem_rng_chk_2_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_mem_rng_chk_2_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.mem_sel_rng_chk.is_zero() && in.main_sel_rng_8.is_zero();
}
};
template <typename FF_> using lookup_mem_rng_chk_2 = GenericLookup<lookup_mem_rng_chk_2_lookup_settings, FF_>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ template <typename FF_>
class lookup_cd_value_relation : public GenericLookupRelation<lookup_cd_value_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_cd_value_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.slice_sel_cd_cpy.is_zero() && in.main_sel_calldata.is_zero();
}
};
template <typename FF_> using lookup_cd_value = GenericLookup<lookup_cd_value_lookup_settings, FF_>;

Expand Down Expand Up @@ -143,6 +148,11 @@ template <typename FF_>
class lookup_ret_value_relation : public GenericLookupRelation<lookup_ret_value_lookup_settings, FF_> {
public:
static constexpr std::string_view NAME = lookup_ret_value_lookup_settings::NAME;

template <typename AllEntities> inline static bool skip(const AllEntities& in)
{
return in.slice_sel_return.is_zero() && in.main_sel_returndata.is_zero();
}
};
template <typename FF_> using lookup_ret_value = GenericLookup<lookup_ret_value_lookup_settings, FF_>;

Expand Down
Loading

0 comments on commit 129abec

Please sign in to comment.