Skip to content

Commit

Permalink
Helpers for ECDSA in A3 (#364)
Browse files Browse the repository at this point in the history
* Add `stdlib_keccak` in cmake.

Correct an assertion in `to_byte_array` in bigfield.

* Add `random_element` to affine element.

* negate y conditionally.
  • Loading branch information
suyash67 committed May 2, 2023
1 parent 7605af9 commit a93f768
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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 @@ -193,6 +194,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 @@ -228,6 +230,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 @@ -62,6 +62,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 @@ -229,5 +229,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

0 comments on commit a93f768

Please sign in to comment.