Skip to content

Commit c04d91c

Browse files
committed
Add algebraic cap hash
1 parent 4ca3cd6 commit c04d91c

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

libiop/bcs/hashing/algebraic_sponge.hpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ class algebraic_hashchain : public hashchain<FieldT, MT_root_type>
9999
void absorb_internal(const typename libff::enable_if<std::is_same<MT_root_type, FieldT>::value, MT_root_type>::type new_input);
100100
};
101101

102+
/** The algebraic_vector_hash is used for both the algebraic leaf hash and cap hash. */
102103
template<typename FieldT>
103-
class algebraic_leafhash : public leafhash<FieldT, FieldT>
104+
class algebraic_vector_hash : public leafhash<FieldT, FieldT>
104105
{
105106
protected:
106107
std::shared_ptr<algebraic_sponge<FieldT>> sponge_;
107108

108109
public:
109-
algebraic_leafhash(
110+
algebraic_vector_hash(
110111
std::shared_ptr<algebraic_sponge<FieldT>> sponge,
111112
size_t security_parameter);
112113
FieldT hash(const std::vector<FieldT> &leaf);
@@ -115,6 +116,9 @@ class algebraic_leafhash : public leafhash<FieldT, FieldT>
115116
const zk_salt_type &zk_salt);
116117
};
117118

119+
template<typename FieldT>
120+
using algebraic_leafhash = algebraic_vector_hash<FieldT>;
121+
118122
template<typename FieldT>
119123
class algebraic_two_to_one_hash
120124
{

libiop/bcs/hashing/algebraic_sponge.tcc

+3-3
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ MT_root_type algebraic_hashchain<FieldT, MT_root_type>::squeeze_root_type()
206206
}
207207

208208
template<typename FieldT>
209-
algebraic_leafhash<FieldT>::algebraic_leafhash(
209+
algebraic_vector_hash<FieldT>::algebraic_vector_hash(
210210
std::shared_ptr<algebraic_sponge<FieldT>> sponge,
211211
size_t security_parameter) :
212212
sponge_(sponge->new_sponge())
@@ -218,7 +218,7 @@ algebraic_leafhash<FieldT>::algebraic_leafhash(
218218
}
219219

220220
template<typename FieldT>
221-
FieldT algebraic_leafhash<FieldT>::hash(
221+
FieldT algebraic_vector_hash<FieldT>::hash(
222222
const std::vector<FieldT> &leaf)
223223
{
224224
this->sponge_->absorb(leaf);
@@ -228,7 +228,7 @@ FieldT algebraic_leafhash<FieldT>::hash(
228228
}
229229

230230
template<typename FieldT>
231-
FieldT algebraic_leafhash<FieldT>::zk_hash(
231+
FieldT algebraic_vector_hash<FieldT>::zk_hash(
232232
const std::vector<FieldT> &leaf,
233233
const zk_salt_type &zk_salt)
234234
{

libiop/bcs/hashing/hash_enum.tcc

+22-23
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ std::shared_ptr<leafhash<FieldT, leaf_hash_type>> get_leafhash_internal(
100100
poseidon_params<FieldT> params = get_poseidon_parameters<FieldT>(hash_enum);
101101
/* security parameter is -1 b/c */
102102
std::shared_ptr<poseidon<FieldT>> permutation = std::make_shared<poseidon<FieldT>>(params);
103-
std::shared_ptr<leafhash<FieldT, leaf_hash_type>> leafhasher = std::make_shared<algebraic_leafhash<FieldT>>(
104-
permutation,
105-
security_parameter - 1);
103+
std::shared_ptr<leafhash<FieldT, leaf_hash_type>> leafhasher =
104+
std::make_shared<algebraic_leafhash<FieldT>>(permutation, security_parameter - 1);
106105
return leafhasher;
107106
}
108107
throw std::invalid_argument("bcs_hash_type unknown (algebraic leaf hash)");
@@ -149,7 +148,7 @@ two_to_one_hash_function<FieldT> get_two_to_one_hash_internal(
149148
as this reference has to live after the function terminates */
150149
std::shared_ptr<algebraic_two_to_one_hash<FieldT>> hash_class =
151150
std::make_shared<algebraic_two_to_one_hash<FieldT>>(permutation, security_parameter - 1);
152-
std::function<FieldT(const FieldT&, const FieldT&, const std::size_t)> f = [permutation, hash_class](const FieldT& left, const FieldT& right, const std::size_t unused) -> FieldT
151+
std::function<FieldT(const FieldT&, const FieldT&, const std::size_t)> f = [permutation, hash_class](const FieldT& left, const FieldT& right, const std::size_t unused) -> FieldT
153152
{
154153
return hash_class->hash(left, right);
155154
};
@@ -185,25 +184,25 @@ cap_hash_function<FieldT> get_cap_hash_internal(
185184
const bcs_hash_type hash_enum,
186185
const size_t security_parameter)
187186
{
188-
// if (hash_enum == starkware_poseidon_type || hash_enum == high_alpha_poseidon_type)
189-
// {
190-
// if (security_parameter != 128)
191-
// {
192-
// throw std::invalid_argument("Poseidon only supported for 128 bit soundness.");
193-
// }
194-
// poseidon_params<FieldT> params = get_poseidon_parameters<FieldT>(hash_enum);
195-
// /* security parameter is -1 b/c */
196-
// std::shared_ptr<algebraic_sponge<FieldT>> permutation = std::make_shared<poseidon<FieldT>>(params);
197-
// /* We explicitly place this on heap with no destructor,
198-
// as this reference has to live after the function terminates */
199-
// std::shared_ptr<algebraic_two_to_one_hash<FieldT>> hash_class =
200-
// std::make_shared<algebraic_two_to_one_hash<FieldT>>(permutation, security_parameter - 1);
201-
// std::function<FieldT(const FieldT&, const FieldT&, const std::size_t)> f = [permutation, hash_class](const FieldT& left, const FieldT& right, const std::size_t unused) -> FieldT
202-
// {
203-
// return hash_class->hash(left, right);
204-
// };
205-
// return f;
206-
// }
187+
if (hash_enum == starkware_poseidon_type || hash_enum == high_alpha_poseidon_type)
188+
{
189+
if (security_parameter != 128)
190+
{
191+
throw std::invalid_argument("Poseidon only supported for 128 bit soundness.");
192+
}
193+
poseidon_params<FieldT> params = get_poseidon_parameters<FieldT>(hash_enum);
194+
/* security parameter is -1 b/c */
195+
std::shared_ptr<algebraic_sponge<FieldT>> permutation = std::make_shared<poseidon<FieldT>>(params);
196+
/* We explicitly place this on heap with no destructor,
197+
as this reference has to live after the function terminates */
198+
std::shared_ptr<algebraic_vector_hash<FieldT>> hash_class =
199+
std::make_shared<algebraic_vector_hash<FieldT>>(permutation, security_parameter - 1);
200+
std::function<FieldT(const std::vector<FieldT> &leaf, const std::size_t)> f = [permutation, hash_class](const std::vector<FieldT> &leaf, const std::size_t unused) -> FieldT
201+
{
202+
return hash_class->hash(leaf);
203+
};
204+
return f;
205+
}
207206
throw std::invalid_argument("bcs_hash_type unknown (algebraic cap hash)");
208207
}
209208

0 commit comments

Comments
 (0)