Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ecdsa_impl.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: completed, auditors: [Federico], date: 2025-10-24 }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
13
14namespace bb::stdlib {
15
16namespace {
18}
19
74template <typename Builder, typename Curve, typename Fq, typename Fr, typename G1>
76 const G1& public_key,
77 const ecdsa_signature<Builder>& sig)
78{
79 BB_ASSERT_EQ(Fr::modulus.get_msb() + 1, 256UL, "The implementation assumes that the bit-length of Fr is 256 bits.");
80
81 // Fetch the context
82 Builder* builder = hashed_message.get_context();
83 builder = validate_context(builder, public_key.get_context());
85 BB_ASSERT_EQ(builder != nullptr, true, "At least one of the inputs should be non-constant.");
86
87 // Turn the hashed message into an element of Fr
88 // Note that we don't need to trim the length of the output of the hash function because the bit length of the
89 // scalar fields we work with (secp256k1, secp256r1) is equal to 256.
90 Fr z(hashed_message);
91
92 // Step 1.
93 public_key.assert_coordinates_in_field(
94 "ECDSA input validation: coordinate(s) of the public key bigger than the base field modulus."); // x < q, y < q
95
96 // Step 2.
97 public_key.validate_on_curve("ECDSA input validation: the public key is not a point on the elliptic curve.");
98
99 // Step 3.
100 public_key.is_point_at_infinity().assert_equal(bool_t<Builder>(false),
101 "ECDSA input validation: the public key is the point at infinity.");
102
103 // Step 4.
104 Fr r(sig.r);
105 r.assert_is_in_field("ECDSA input validation: the r component of the signature is bigger than the order of the "
106 "elliptic curve."); // r < n
107 r.assert_is_not_equal(Fr::zero(), "ECDSA input validation: the r component of the signature is zero."); // 0 < r
108
109 // Step 5.
110 Fr s(sig.s);
111 s.assert_less_than(
112 (Fr::modulus + 1) / 2,
113 "ECDSA input validation: the s component of the signature is bigger than (Fr::modulus + 1)/2."); // s < (n+1)/2
114 s.assert_is_not_equal(Fr::zero(), "ECDSA input validation: the s component of the signature is zero."); // 0 < s
115
116 // Step 6.
117 Fr u1 = z.div_without_denominator_check(s);
118 Fr u2 = r.div_without_denominator_check(s);
119
120 G1 result;
121 if constexpr (Curve::type == bb::CurveType::SECP256K1) {
122 result = G1::secp256k1_ecdsa_mul(public_key, u1, u2);
123 } else {
124 // This error comes from the lookup tables used in batch_mul. We could get rid of it by setting with_edgecase =
125 // true. However, this would increase the gate count, and it would handle a case that should not appear in
126 // general: someone using plus or minus the generator as a public key.
127 if ((public_key.get_value().x == Curve::g1::affine_one.x) && (!builder->failed())) {
128 builder->failure("ECDSA input validation: the public key is equal to plus or minus the generator point.");
129 }
130 result = G1::batch_mul({ G1::one(builder), public_key }, { u1, u2 });
131 }
132
133 // Step 7.
134 result.is_point_at_infinity().assert_equal(
135 bool_t<Builder>(false), "ECDSA validation: the result of the batch multiplication is the point at infinity.");
136
137 // Step 8.
138 // We reduce result.x() to 2^s, where s is the smallest s.t. 2^s > q. It is cheap in terms of constraints, and
139 // avoids possible edge cases
140 result.x().reduce_mod_target_modulus();
141
142 // Transfer Fq value result.x() to Fr (this is just moving from a C++ class to another)
143 Fr result_x_mod_r = Fr::unsafe_construct_from_limbs(result.x().binary_basis_limbs[0].element,
144 result.x().binary_basis_limbs[1].element,
145 result.x().binary_basis_limbs[2].element,
146 result.x().binary_basis_limbs[3].element);
147 // Copy maximum limb values from Fq to Fr: this is needed by the subtraction happening in the == operator
148 for (size_t idx = 0; idx < 4; idx++) {
149 result_x_mod_r.binary_basis_limbs[idx].maximum_value = result.x().binary_basis_limbs[idx].maximum_value;
150 }
151
152 // Check result.x() = r mod n
153 bool_t<Builder> is_signature_valid = result_x_mod_r == r;
154
155 // Logging
156 if (is_signature_valid.get_value()) {
157 vinfo("ECDSA signature verification succeeded.");
158 } else {
159 vinfo("ECDSA signature verification failed");
160 }
161
162 return is_signature_valid;
163}
164
172template <typename Builder> void generate_ecdsa_verification_test_circuit(Builder& builder, size_t num_iterations)
173{
175
176 // Native types
177 using FrNative = typename Curve::fr;
178 using FqNative = typename Curve::fq;
179 using G1Native = typename Curve::g1;
180
181 // Stdlib types
182 using Fr = typename Curve::bigfr_ct;
183 using Fq = typename Curve::fq_ct;
184 using G1 = typename Curve::g1_bigfr_ct;
185
186 std::string message_string = "Instructions unclear, ask again later.";
187
189 for (size_t i = 0; i < num_iterations; i++) {
190 // Generate unique signature for each iteration
191 account.private_key = FrNative::random_element(&engine);
192 account.public_key = G1Native::one * account.private_key;
193
194 crypto::ecdsa_signature signature =
195 crypto::ecdsa_construct_signature<crypto::Sha256Hasher, FqNative, FrNative, G1Native>(message_string,
196 account);
197
198 bool native_verification = crypto::ecdsa_verify_signature<crypto::Sha256Hasher, FqNative, FrNative, G1Native>(
199 message_string, account.public_key, signature);
200 BB_ASSERT_EQ(native_verification, true, "Native ECDSA verification failed while generating test circuit.");
201
202 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
203 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
204
205 G1 public_key = G1::from_witness(&builder, account.public_key);
206
208
209 // Compute H(m) natively and pass as witness (mirrors ACIR which takes pre-hashed message)
210 auto hash_arr = crypto::sha256(std::vector<uint8_t>(message_string.begin(), message_string.end()));
211 stdlib::byte_array<Builder> hashed_message(&builder, std::vector<uint8_t>(hash_arr.begin(), hash_arr.end()));
212
213 // Verify ecdsa signature
214 bool_t<Builder> result =
215 stdlib::ecdsa_verify_signature<Builder, Curve, Fq, Fr, G1>(hashed_message, public_key, sig);
216 result.assert_equal(bool_t<Builder>(true));
217 }
218}
219
220} // namespace bb::stdlib
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
Implements boolean logic in-circuit.
Definition bool.hpp:59
bool get_value() const
Definition bool.hpp:124
void assert_equal(const bool_t &rhs, std::string const &msg="bool_t::assert_equal") const
Implements copy constraint for bool_t elements.
Definition bool.cpp:421
Represents a dynamic array of bytes in-circuit.
Builder * get_context() const
#define vinfo(...)
Definition log.hpp:80
AluTraceBuilder builder
Definition alu.test.cpp:124
numeric::RNG & engine
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
Definition sha256.cpp:150
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
void generate_ecdsa_verification_test_circuit(Builder &builder, size_t num_iterations)
Generate a simple ecdsa verification circuit for testing purposes.
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...
T * validate_context(T *ptr)
Definition field.hpp:16
@ SECP256K1
Definition types.hpp:10
Curve::AffineElement G1
G1::affine_element public_key
Definition ecdsa.hpp:20
std::array< uint8_t, 32 > r
Definition ecdsa.hpp:26
std::array< uint8_t, 32 > s
Definition ecdsa.hpp:27
static constexpr uint256_t modulus
static constexpr field zero()
stdlib::byte_array< Builder > s
Definition ecdsa.hpp:16
Builder * get_context() const
Definition ecdsa.hpp:18
stdlib::byte_array< Builder > r
Definition ecdsa.hpp:15