weierstrudel: efficient elliptic curve arithmetic for smart contracts
weierstrudel is a highly optimized smart contract that performs elliptic curve scalar multiplication on the short Weierstrass 254-bit Barreto-Naehrig curve, formerly used by ZCash and currently available as a precompile smart-contract in the Ethereum protocol.
The contract will multiply up to 15 elliptic curve points with up to 15 different scalars.
The current gas schedule for Ethereum's scalar multiplication precompile smart contract is
40,000 gas. When multiplying more than one point, weierstrudel is substantially more efficient than the precompile contract (see Benchmarks).
weierstrudel is written entirely in Huff, a low-level domain-specific language that compiles to Ethereum Virtual Machine opcodes. In addition, the following techniques are used to minimize gas costs:
- Using the GLV technique to exploit a curve endomorphism and reduce the number of 'point doubling' operations in half.
- Using Shamir's trick to combine multiple scalar multiplications into a single algorithm, fixing the number of 'point doubling' operations to ~127
- Using Windowed-Non-Adjacent-Form representations for scalar multipliers, reducing the number of 'point addition' operations to ~50 per point
- Using the difference between the bn254 curve's 254-bit field modulus and the EVM's 256 word size to defer modular reductions until absolutely necessary
weierstrudel makes extensive use of bit-shift opcodes and is only compatible with Ethereum once the Constantinople hard-fork has been activated.
"Hang on...what is Huff?"
Huff enables the construction of composable, EVM assembly macros. Huff also supports a crude form of templating - macros can accept template arguments, which in turn are also Huff macros. This allows for highly optimized, customizable blocks of assembly code.
See the Huff repository for more details.
"What are the implications of weierstrudel?"
Until the gas schedule for Ethereum's precompile contracts changes,
weierstrudel makes zero-knowledge cryptosystems that utilize the bn254 curve, such as the AZTEC protocol substantially cheaper.
"Is there a catch?"
weierstrudel smart contract requires precisely
1 wei to be sent to it or it will refuse to perform elliptic curve scalar multiplication. No more, no less.
Yes. Doing so saves approximately 500 gas per contract call.
Is weierstrudel production ready?
Not yet! We're in the process of applying more rigorous testing to ensure the correctness of
weierstrudel's algorithms. In addition we still need to implement the following:
- Fully supported edge-cases for weierstrudel's point addition formulae - currently the contract will throw an error if the following edge cases are hit:
- Adding two points equal to one another
- Adding a point to the point's negative counterpart
- Montgomery batch inverses in Huff - points are currently expressed in Jacobean form.
- Supplying a point's inverse as a transaction input is the most efficient method of obtaining an inverse (~2,000 gas), but we still want to implement this to maintain a consistent interface when compared to the precompile
- Precomputed point lookup tables for generator points
- There are substantial gas optimizations to be claimed by integrating a lookup table for bn254's fixed generator point
"Can I use weierstrudel in my project?"
weierstrudel is open-source software, licensed under LGPL-3.0. However we would urge caution until we've finished thoroughly validating
weierstrudel's Huff macros.
Gas estimates can be obtained by running
npm run benchmark. For reference, the scalar multiplication precompile costs
40,000 gas per point. This is excluding the overheads of having to make a contract call per point when using the precompiles, as well as calling the point addition precompile to combine points into a single sum.
|Number of points||Approximate gas cost (average of 10 runs)||Cost per point|
weierstrudel is currently deployed on Ropsten.
Run weierstrudel tests via
npm run test
npm run jstest
Run weierstrudel benchmarks via
npm run benchmark
Compile the weierstrudel smart contract via
npm run compile