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: Complete, auditors: [Federico], commit: 05a381f8b31ae4648e480f1369e911b148216e8b}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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 auto result_is_infinity = result.is_point_at_infinity();
135 result_is_infinity.assert_equal(
136 bool_t<Builder>(false), "ECDSA validation: the result of the batch multiplication is the point at infinity.");
137
138 // Step 8.
139 // 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
140 // avoids possible edge cases
141 result.x().reduce_mod_target_modulus();
142
143 // Transfer Fq value result.x() to Fr (this is just moving from a C++ class to another)
144 Fr result_x_mod_r = Fr::unsafe_construct_from_limbs(result.x().binary_basis_limbs[0].element,
145 result.x().binary_basis_limbs[1].element,
146 result.x().binary_basis_limbs[2].element,
147 result.x().binary_basis_limbs[3].element);
148 // Copy maximum limb values from Fq to Fr: this is needed by the subtraction happening in the == operator
149 for (size_t idx = 0; idx < 4; idx++) {
150 result_x_mod_r.binary_basis_limbs[idx].maximum_value = result.x().binary_basis_limbs[idx].maximum_value;
151 }
152
153 // Check result.x() = r mod n AND result is not point at infinity
154 bool_t<Builder> x_matches = result_x_mod_r == r;
155 bool_t<Builder> is_not_infinity = !result_is_infinity;
156 bool_t<Builder> is_signature_valid = x_matches && is_not_infinity;
157
158 // Logging
159 if (is_signature_valid.get_value()) {
160 vinfo("ECDSA signature verification succeeded.");
161 } else {
162 vinfo("ECDSA signature verification failed");
163 }
164
165 return is_signature_valid;
166}
167
175template <typename Builder> void generate_ecdsa_verification_test_circuit(Builder& builder, size_t num_iterations)
176{
178
179 // Native types
180 using FrNative = typename Curve::fr;
181 using FqNative = typename Curve::fq;
182 using G1Native = typename Curve::g1;
183
184 // Stdlib types
185 using Fr = typename Curve::bigfr_ct;
186 using Fq = typename Curve::fq_ct;
187 using G1 = typename Curve::g1_bigfr_ct;
188
189 std::string message_string = "Instructions unclear, ask again later.";
190
192 for (size_t i = 0; i < num_iterations; i++) {
193 // Generate unique signature for each iteration
194 account.private_key = FrNative::random_element(&engine);
195 account.public_key = G1Native::one * account.private_key;
196
197 crypto::ecdsa_signature signature =
198 crypto::ecdsa_construct_signature<crypto::Sha256Hasher, FqNative, FrNative, G1Native>(message_string,
199 account);
200
201 bool native_verification = crypto::ecdsa_verify_signature<crypto::Sha256Hasher, FqNative, FrNative, G1Native>(
202 message_string, account.public_key, signature);
203 BB_ASSERT_EQ(native_verification, true, "Native ECDSA verification failed while generating test circuit.");
204
205 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
206 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
207
208 G1 public_key = G1::from_witness(&builder, account.public_key);
209
211
212 // Compute H(m) natively and pass as witness (mirrors ACIR which takes pre-hashed message)
213 auto hash_arr = crypto::sha256(std::vector<uint8_t>(message_string.begin(), message_string.end()));
214 stdlib::byte_array<Builder> hashed_message(&builder, std::vector<uint8_t>(hash_arr.begin(), hash_arr.end()));
215
216 // Verify ecdsa signature
217 bool_t<Builder> result =
218 stdlib::ecdsa_verify_signature<Builder, Curve, Fq, Fr, G1>(hashed_message, public_key, sig);
219 result.assert_equal(bool_t<Builder>(true));
220 }
221}
222
223} // namespace bb::stdlib
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
Implements boolean logic in-circuit.
Definition bool.hpp:60
bool get_value() const
Definition bool.hpp:125
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:433
Represents a dynamic array of bytes in-circuit.
Builder * get_context() const
#define vinfo(...)
Definition log.hpp:94
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:212
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:17
@ 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