Skip to content

Conversation

@federicobarbacovi
Copy link
Contributor

@federicobarbacovi federicobarbacovi commented Aug 27, 2025

Audit part 5: Define single ecdsa constraint generator

In this PR we merge the two ECDSA constraint generators (secp256k1 and secp256r1) into a single one templated over Curve type. We also restructure the code of the constraint generator to be more similar to other parts of the dsl package.

There are some changes to the circuits. This is due to some assert_equal that have been removed, and normalize calls that are not needed.

We also add documentation and define a utils.hpp file in the dsl package containing some functions that are used multiple times in different files.

@federicobarbacovi federicobarbacovi changed the title chore: Define single ecdsa constaint generator chore: Define single ecdsa constraint generator Aug 27, 2025
@federicobarbacovi federicobarbacovi self-assigned this Aug 27, 2025
pub_key_y_fq.assert_is_in_field();
typename secp256k1_ct::g1_bigfr_ct public_key = typename secp256k1_ct::g1_bigfr_ct(pub_key_x_fq, pub_key_y_fq);
for (size_t i = 0; i < 32; ++i) {
sig.r[i].assert_equal(field_ct::from_witness_index(&builder, input.signature[i]));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assert_equal are asserting equality of sig.r[i] with the witness at index input.signature[i]. In the new code, sig.r[i] is built to be the builder variable whose underlying value is the witness value at index input.signature[i], so this check is not needed.

A similar comment applies to the other asserts.

In this version of the code the asserts were needed because the constructor for sig creates new builder variables out of rr and ss, which we must check are equal to the variables point to the indices representing r and s.

I think the asserts were not needed for message and pub_key_*_byte_array.

typename secp256k1_ct::bigfr_ct,
typename secp256k1_ct::g1_bigfr_ct>(
message, public_key, sig);
bool_ct signature_result_normalized = signature_result.normalize();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalization was required in this case because the assertion in the following lines is checking equality by looking at the witness indices pointed by the variables signature_result_normalized and input.result. In the new code we directly call assert_equal on signature_result, which handles the normalization on its own.

@federicobarbacovi federicobarbacovi changed the title chore: Define single ecdsa constraint generator chore!: Define single ecdsa constraint generator Aug 28, 2025
@federicobarbacovi federicobarbacovi marked this pull request as ready for review August 28, 2025 15:22
return offset;
}

TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not needed, successes/failures are tested in stdlib_ecdsa_tests.

EXPECT_TRUE(CircuitChecker::check(builder));
}

TEST_F(ECDSASecp256k1, TestECDSAConstraintFail)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not needed, successes/failures are tested in stdlib_ecdsa_tests.

EXPECT_TRUE(CircuitChecker::check(builder));
}

TEST(ECDSASecp256r1, TestECDSAConstraintSucceed)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not needed, successes/failures are tested in stdlib_ecdsa_tests.

EXPECT_TRUE(CircuitChecker::check(builder));
}

TEST(ECDSASecp256r1, TestECDSAConstraintFail)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not needed, successes/failures are tested in stdlib_ecdsa_tests.


TYPED_TEST_SUITE(EcdsaConstraintsTest, CurveTypes);

TYPED_TEST(EcdsaConstraintsTest, GenerateVKFromConstraints)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only type of test needed here: checking that the vk generated from ACIR constraints with a witness is the same as the one generated by ACIR constraints without a witness

Copy link
Contributor

@iakovenkos iakovenkos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! thank for cleaning it up

@federicobarbacovi federicobarbacovi merged commit e6e7017 into merge-train/barretenberg Aug 29, 2025
6 checks passed
@federicobarbacovi federicobarbacovi deleted the fb/ecdsa_audit_5 branch August 29, 2025 12:11
@federicobarbacovi federicobarbacovi added the bberg-int-audit All things related to barretenberg internal audit label Aug 29, 2025
github-merge-queue bot pushed a commit that referenced this pull request Aug 29, 2025
BEGIN_COMMIT_OVERRIDE
chore!: Define single ecdsa constraint generator (#16612)
END_COMMIT_OVERRIDE
mralj pushed a commit that referenced this pull request Oct 13, 2025
Audit part 5: Define single ecdsa constraint generator

In this PR we merge the two ECDSA constraint generators (secp256k1 and
secp256r1) into a single one templated over `Curve` type. We also
restructure the code of the constraint generator to be more similar to
other parts of the `dsl` package.

There are some changes to the circuits. This is due to some
`assert_equal` that have been removed, and `normalize` calls that are
not needed.

We also add documentation and define a `utils.hpp` file in the `dsl`
package containing some functions that are used multiple times in
different files.
ludamad pushed a commit that referenced this pull request Dec 16, 2025
Audit part 5: Define single ecdsa constraint generator

In this PR we merge the two ECDSA constraint generators (secp256k1 and
secp256r1) into a single one templated over `Curve` type. We also
restructure the code of the constraint generator to be more similar to
other parts of the `dsl` package.

There are some changes to the circuits. This is due to some
`assert_equal` that have been removed, and `normalize` calls that are
not needed.

We also add documentation and define a `utils.hpp` file in the `dsl`
package containing some functions that are used multiple times in
different files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bberg-int-audit All things related to barretenberg internal audit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants