Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers for ECDSA in A3 #364

Merged
merged 3 commits into from
Apr 20, 2023
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
3 changes: 3 additions & 0 deletions cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ if(WASM)
$<TARGET_OBJECTS:stdlib_pedersen_commitment_objects>
$<TARGET_OBJECTS:stdlib_blake2s_objects>
$<TARGET_OBJECTS:stdlib_blake3s_objects>
$<TARGET_OBJECTS:stdlib_keccak_objects>
$<TARGET_OBJECTS:stdlib_sha256_objects>
$<TARGET_OBJECTS:stdlib_aes128_objects>
$<TARGET_OBJECTS:stdlib_merkle_tree_objects>
Expand Down Expand Up @@ -192,6 +193,7 @@ if(WASM)
$<TARGET_OBJECTS:stdlib_pedersen_commitment_objects>
$<TARGET_OBJECTS:stdlib_blake2s_objects>
$<TARGET_OBJECTS:stdlib_blake3s_objects>
$<TARGET_OBJECTS:stdlib_keccak_objects>
$<TARGET_OBJECTS:stdlib_sha256_objects>
$<TARGET_OBJECTS:stdlib_aes128_objects>
$<TARGET_OBJECTS:stdlib_merkle_tree_objects>
Expand Down Expand Up @@ -227,6 +229,7 @@ else()
$<TARGET_OBJECTS:stdlib_pedersen_commitment_objects>
$<TARGET_OBJECTS:stdlib_blake2s_objects>
$<TARGET_OBJECTS:stdlib_blake3s_objects>
$<TARGET_OBJECTS:stdlib_keccak_objects>
$<TARGET_OBJECTS:stdlib_sha256_objects>
$<TARGET_OBJECTS:stdlib_aes128_objects>
$<TARGET_OBJECTS:stdlib_merkle_tree_objects>
Expand Down
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
29 changes: 29 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,34 @@ 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) {
// Sample a random x-coordinate and check if it satisfies curve equation.
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;

// Negate the y-coordinate based on a randomly sampled bit.
bool random_bit = (engine->get_random_uint8() & 1);
if (random_bit) {
y = -y;
}

found_one = found_root;
}
return affine_element<Fq, Fr, T>(x, y);
}

} // namespace group_elements
} // namespace barretenberg
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ template <typename Composer, typename T> class bigfield {
field_t<Composer> lo = binary_basis_limbs[0].element + (binary_basis_limbs[1].element * shift_1);
field_t<Composer> hi = binary_basis_limbs[2].element + (binary_basis_limbs[3].element * shift_1);
// n.b. this only works if NUM_LIMB_BITS * 2 is divisible by 8
ASSERT((NUM_LIMB_BITS / 8) * 8 == NUM_LIMB_BITS);
ASSERT((NUM_LIMB_BITS * 2 / 8) * 8 == NUM_LIMB_BITS * 2);
result.write(byte_array<Composer>(hi, 32 - (NUM_LIMB_BITS / 4)));
result.write(byte_array<Composer>(lo, (NUM_LIMB_BITS / 4)));
return result;
Expand Down