Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

addchain

> 编程语言
Open source

Cryptographic Addition Chain Generation in Go

197 stars0 likes2 views
WebsiteGitHub

About

Cryptographic Addition Chain Generation in Go

Cryptographic Addition Chain Generation in Go

addchain generates short addition chains for exponents of cryptographic interest with results rivaling the best hand-optimized chains. Intended as a building block in elliptic curve or other cryptographic code generators.

  • Suite of algorithms from academic research: continued fractions, dictionary-based and Bos-Coster heuristics
  • Custom run-length techniques exploit structure of cryptographic exponents with excellent results on Solinas primes
  • Generic optimization methods eliminate redundant operations
  • Simple domain-specific language for addition chain computations
  • Command-line interface or library
  • Code generation and templated output support

Table of Contents

  • Background
  • Results
  • Usage
    • Command-line Interface
    • Library
  • Algorithms
    • Binary
    • Continued Fractions
    • Bos-Coster Heuristics
    • Dictionary
    • Runs
    • Optimization
  • Citing
  • Thanks
  • Contributing
  • License

Background

An addition chain for a target integer n is a sequence of numbers starting at 1 and ending at n such that every term is a sum of two numbers appearing earlier in the sequence. For example, an addition chain for 29 is

1, 2, 4, 8, 9, 17, 25, 29

Addition chains arise in the optimization of exponentiation algorithms with fixed exponents. For example, the addition chain above corresponds to the following sequence of multiplications to compute x29

x2 = x1 * x1 x4 = x2 * x2 x8 = x4 * x4 x9 = x1 * x8 x17 = x8 * x9 x25 = x8 * x17 x29 = x4 * x25

An exponentiation algorithm for a fixed exponent n reduces to finding a minimal length addition chain for n. This is especially relevent in cryptography where exponentiation by huge fixed exponents forms a performance-critical component of finite-field arithmetic. In particular, constant-time inversion modulo a prime p is performed by computing xp-2 (mod p), thanks to Fermat's Little Theorem. Square root also reduces to exponentiation for some prime moduli. Finding short addition chains for these exponents is one important part of high-performance finite field implementations required for elliptic curve cryptography or RSA.

Minimal addition chain search is famously hard. No practical optimal algorithm is known, especially for cryptographic exponents of size 256-bits and up. Given its importance for the performance of cryptographic implementations, implementers devote significant effort to hand-tune addition chains. The goal of the addchain project is to match or exceed the best hand-optimized addition chains using entirely automated approaches, building on extensive academic research and applying new tweaks that exploit the unique nature of cryptographic exponents.

Results

The following table shows the results of the addchain library on popular cryptographic exponents. For each one we also show the length of the best known hand-optimized addition chain, and the delta from the library result.

Name This Library Best Known Delta
Curve25519 Field Inversion 266 265 +1
NIST P-256 Field Inversion 266 266 +0
NIST P-384 Field Inversion 397 396 +1
secp256k1 (Bitcoin) Field Inversion 269 269 +0
Curve25519 Scalar Inversion 283 284 -1
NIST P-256 Scalar Inversion 294 292 +2
NIST P-384 Scalar Inversion 434 433 +1
secp256k1 (Bitcoin) Scalar Inversion 293 290 +3

See full results listing for more detail and results for less common exponents.

These results demonstrate that addchain is competitive with hand-optimized chains, often with equivalent or better performance. Even when addchain is slightly sub-optimal, it can still be considered valuable since it fully automates a laborious manual process. As such, addchain can be trusted to produce high quality results in an automated code generation tool.

Usage

Command-line Interface

Install a pre-compiled release binary:

bash
curl -sSfL https://git.io/addchain | sh -s -- -b /usr/local/bin

Alternatively build from source:

go install github.com/mmcloughlin/addchain/cmd/addchain@latest

Search for a curve25519 field inversion addition chain with:

bash
addchain search '2^255 - 19 - 2'

Output:

…

bib @misc{addchain, title = {addchain: Cryptographic Addition Chain Generation in Go}, author = {Michael B. McLoughlin}, year = 2021, month = oct, howpublished = {Repository \url{https://github.com/mmcloughlin/addchain}}, version = {0.4.0}, license = {BSD 3-Clause License}, doi = {10.5281/zenodo.5622943}, url = {https://doi.org/10.5281/zenodo.5622943}, }


If you need to cite a currently unreleased version please consider [filing an
issue](https://github.com/mmcloughlin/addchain/issues/new) to request a new
release, or to discuss an appropriate format for the citation.

## Thanks

Thank you to [Tom Dean](https://web.stanford.edu/~trdean/), [Riad
Wahby](https://wahby.org/), [Brian Smith](https://briansmith.org/) and
[str4d](https://github.com/str4d) for advice and encouragement. Thanks also to
[Damian Gryski](https://github.com/dgryski) and [Martin
Glancy](https://twitter.com/mglancy) for review.

## Contributing

Contributions to `addchain` are welcome:

* [Submit bug reports](https://github.com/mmcloughlin/addchain/issues/new) to
  the issues page.
* Suggest [test cases](https://github.com/mmcloughlin/addchain/blob/e6c070065205efcaa02627ab1b23e8ce6aeea1db/internal/results/results.go#L62)
  or update best-known hand-optimized results.
* Pull requests accepted. Please discuss in the [issues section](https://github.com/mmcloughlin/addchain/issues)
  before starting significant work.

## License

`addchain` is available under the [BSD 3-Clause License](LICENSE).

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Go

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言