79 BB_ASSERT_EQ(
builder !=
nullptr,
true,
"At least one of the inputs should be non-constant.");
87 bool_ct is_x_less_than_modulus = public_key.x().is_less_than(
88 Fq::modulus,
"ECDSA input validation: x coordinate of the public key bigger than the base field modulus.");
89 bool_ct is_y_less_than_modulus = public_key.y().is_less_than(
90 Fq::modulus,
"ECDSA input validation: y coordinate of the public key bigger than the base field modulus.");
93 bool_ct is_point_at_infinity = public_key.is_point_at_infinity();
99 typename Curve::AffineElementNative native_double_generator(Curve::GroupNative::one + Curve::GroupNative::one);
100 G1 double_generator(
Fq(native_double_generator.x),
Fq(native_double_generator.y),
false);
101 G1 corrected_public_key = G1::conditional_assign(
102 is_point_at_infinity || !is_x_less_than_modulus || !is_y_less_than_modulus, double_generator, public_key);
104 corrected_public_key.validate_on_curve(
105 "ECDSA input validation: the public key is not a point on the elliptic curve.",
false) ==
Fq::zero();
109 bool_ct is_r_in_range = r.is_less_than(
110 Fr::modulus,
"ECDSA input validation: the r component of the signature is bigger than Fr::modulus.");
111 bool_ct is_r_zero = r ==
Fr::zero();
115 bool_ct is_s_in_range =
117 "ECDSA input validation: the s component of the signature is bigger than (Fr::modulus + 1)/2.");
118 bool_ct is_s_zero = s ==
Fr::zero();
122 Fr corrected_s = Fr::conditional_assign(is_s_zero,
Fr::one(), s);
124 Fr u1 = z.div_without_denominator_check(corrected_s);
125 Fr u2 = r.div_without_denominator_check(corrected_s);
129 result = G1::secp256k1_ecdsa_mul(corrected_public_key, u1, u2);
134 if ((corrected_public_key.get_value().x == Curve::GroupNative::affine_one.x) && (!
builder->failed())) {
135 builder->failure(
"ECDSA input validation: the public key is equal to plus or minus the generator point.");
137 result = G1::batch_mul(
138 { G1::one(
builder), corrected_public_key }, { u1, u2 }, 0,
false);
142 bool_ct result_is_infinity = result.is_point_at_infinity();
145 result.x().reduce_mod_target_modulus();
148 Fr result_x_mod_r = Fr::unsafe_construct_from_limbs(result.x().binary_basis_limbs[0].element,
149 result.x().binary_basis_limbs[1].element,
150 result.x().binary_basis_limbs[2].element,
151 result.x().binary_basis_limbs[3].element);
153 for (
size_t idx = 0; idx < 4; idx++) {
154 result_x_mod_r.binary_basis_limbs[idx].maximum_value = result.x().binary_basis_limbs[idx].maximum_value;
158 bool_ct x_matches = result_x_mod_r == r;
159 bool_ct is_signature_valid = x_matches && !is_point_at_infinity && !result_is_infinity && is_r_in_range &&
160 !is_r_zero && is_s_in_range && !is_s_zero && is_point_on_curve &&
161 is_x_less_than_modulus && is_y_less_than_modulus;
164 if (is_signature_valid.get_value()) {
165 vinfo(
"ECDSA signature verification succeeded.");
167 vinfo(
"ECDSA signature verification failed");
170 return is_signature_valid;
185 using FrNative =
typename Curve::ScalarFieldNative;
186 using FqNative =
typename Curve::BaseFieldNative;
187 using G1Native =
typename Curve::GroupNative;
194 std::string message_string =
"Instructions unclear, ask again later.";
197 for (
size_t i = 0; i < num_iterations; i++) {
203 crypto::ecdsa_construct_signature<crypto::Sha256Hasher, FqNative, FrNative, G1Native>(message_string,
206 bool native_verification = crypto::ecdsa_verify_signature<crypto::Sha256Hasher, FqNative, FrNative, G1Native>(
207 message_string, account.
public_key, signature);
208 BB_ASSERT_EQ(native_verification,
true,
"Native ECDSA verification failed while generating test circuit.");
210 std::vector<uint8_t> rr(signature.
r.begin(), signature.
r.end());
211 std::vector<uint8_t> ss(signature.
s.begin(), signature.
s.end());
218 auto hash_arr =
crypto::sha256(std::vector<uint8_t>(message_string.begin(), message_string.end()));
223 stdlib::ecdsa_verify_signature<Builder, Curve, Fq, Fr, G1>(hashed_message, public_key, sig);
bool_t< Builder > ecdsa_verify_signature(const stdlib::byte_array< Builder > &hashed_message, const G1 &public_key, const ecdsa_signature< Builder > &sig)
Verify ECDSA signature. Returns bool_t(true/false) depending on whether the signature is valid or not...