Skip to content

Commit

Permalink
Add random_element to affine element.
Browse files Browse the repository at this point in the history
  • Loading branch information
suyash67 committed Apr 16, 2023
1 parent 540bb3a commit 3dddda9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cpp/src/barretenberg/ecc/groups/affine_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ template <typename Fq, typename Fr, typename Params> class alignas(64) affine_el

constexpr bool on_curve() const noexcept;

/**
* @brief Samples a random point on the curve.
*
* @return A randomly chosen point on the curve
*/
static affine_element random_element(numeric::random::Engine* engine = nullptr) noexcept;

/**
* @brief Hash a seed value to curve.
*
Expand Down
21 changes: 21 additions & 0 deletions cpp/src/barretenberg/ecc/groups/affine_element_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,26 @@ affine_element<Fq, Fr, T> affine_element<Fq, Fr, T>::hash_to_curve(const uint64_

return affine_element<Fq, Fr, T>(x_out, y_out_);
}

template <typename Fq, typename Fr, typename T>
affine_element<Fq, Fr, T> affine_element<Fq, Fr, T>::random_element(numeric::random::Engine* engine) noexcept
{
bool found_one = false;
Fq yy;
Fq x;
Fq y;
while (!found_one) {
x = Fq::random_element(engine);
yy = x.sqr() * x + T::b;
if constexpr (T::has_a) {
yy += (x * T::a);
}
auto [found_root, y1] = yy.sqrt();
y = y1;
found_one = found_root;
}
return affine_element<Fq, Fr, T>(x, y);
}

} // namespace group_elements
} // namespace barretenberg

0 comments on commit 3dddda9

Please sign in to comment.