Skip to content

Avoid using BigInt for any DL arithmetic - #6135

Merged
randombit merged 10 commits into
masterfrom
jack/dl-element
Nov 17, 2026
Merged

randombit merged 10 commits into
masterfrom
jack/dl-element

Conversation

@randombit

Copy link
Copy Markdown
Owner

Add new (internal) DL_Element and DL_Scalar types which wrap Montgomery_Int, and use them in DH, ElGamal, and DSA

This allows internal code outside of dl_group.cpp to use the group's
precomputed values. DL_Group::_data() returns the shared group data.
An internal type for integers modulo the prime p of a DL group, held in
Montgomery form at a fixed length. Unlike arithmetic using BigInt, the
sizes of intermediate values do not depend on their values.

This does not require the group to have a known subgroup order, since
SRP6 groups and ElGamal keys loaded via FFI do not have one.
Decryption blinding now uses DL_PrivateKey_Exponentiation, which computes
b^x for the private key in Montgomery form with the same squaring and
periodic refresh scheme as Blinder. DH will use the same helper.
Key agreement now uses DL_PrivateKey_Exponentiation, so DH no longer
uses Blinder or Barrett reduction.
An internal type for integers modulo the prime order q of a DL group,
held in Montgomery form at a fixed length, analogous to DL_Element.

Inversion uses Fermat's little theorem, avoiding the BigInt range checks
of inverse_mod_public_prime. The reduction of g^k mod p modulo q does a
fixed amount of work, unlike ct_modulo which depends on the word length
of its input.

This is only used by DSA, and can be removed along with it.
This removes the last in-tree users of the deprecated DL_Group mod q
arithmetic and multi_exponentiate.
Computing these for every group with a known q made constructing groups
such as ffdhe/ietf/4096, where q is almost as long as p, 10-16% slower.
Only DSA, via DL_Scalar, needs fast arithmetic modulo q, and DSA requires
q of at most 256 bits.

For larger q the deprecated DL_Group::multiply_mod_q and square_mod_q
use ct_modulo instead. Nothing in the library calls them.
These are no longer used within the library. Applications should not need to do
arithmetic on DL groups directly.
@randombit
randombit merged commit b27345b into master Nov 17, 2026
51 checks passed
@randombit
randombit deleted the jack/dl-element branch November 17, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant