First, let's define the name of this elliptic curve. Multiple names for this curve are defined, that may become confusing. The elliptic curve we are talking about can be named1:
- BN128: "BN" stands for Barreto-Naehrig, and "128" is the theoretical bits of security
- alt_BN128: Why? Why not.
- BN254: "254" referring to the number of bits in the prime associated to the base field.
Note: The elliptic curve we are talking about is not the one referenced at this link: https://neuromancer.sk/std/bn/bn254
BN128 is a Barreto-Naehrig curve that is known as pairing friendly. This curve was previously considered with 128-bit of security. But this number of bits dropped to around 100 bits after new algorithms were published in 2015 by Taechan Kim and Razvan Barbulescu2.
It was previously used by ZCash and is currently implemented on Ethereum34 through 3 precompiled contracts (1 for addition, 1 for multiplication and 1 for pairing). It is the most pairing friendly curve used for verifying on-chain zkSNARKs using proof schemes such as Groth165 and Plonk6.
Another similar curve is BLS12-3817 which provides more bits of security.
A Barreto-Naherig8 curve is an elliptic curve E of the form:
It is defined over a prime field
The
The equation of the BN128 elliptic curve is:
It is defined over the field
The parameter
Then, the following equation must be true:
Then, the curve order
alt_bn128 | BLS12-381 | Note | |
---|---|---|---|
254 bits (32 bytes) | 381 bits (48 bytes) | has leading zero bits | |
64 bytes | 96 bytes | ||
|
64 bytes | 96 bytes | x and y coordinates as |
|
128 bytes | 192 bytes | x and y coordinates as |
But
alt_bn128 | BLS12-381 | |
---|---|---|
|
32 bytes | 48 bytes |
|
64 bytes | 96 bytes |
Note: This part assumes knowledge of elliptic curve pairing.
BLS signature works with two groups
The developer is able to choose between two options:
- Small
$\Bbb G_1$ public keys with big$\Bbb G_2$ signatures - Big
$\Bbb G_2$ public keys with small$\Bbb G_1$ signatures
In the following details, we take
First, the message is mapped to an element of G1.
Secret Key:
Public Key:
We take
Requirements:
pip3 install py_ecc pycryptodome
Python scripts are available:
bls-signature.py
: uses the BLS signature scheme of py_eccbn128_bls-signature.py
: implements its own BLS signature algorithm, with G1 signature and G2 pubkey for verificationbn128_bls-multisig.py
: implements its own BLS multisignature scheme, with G2 signature and G1 pubkey for verificationbn128_bls-multisig-handling-nonsigners.py
: implements its own BLS multisignature scheme, with G1 signature and G2 pubkey for verification. It shows how to handle non-signers in the multisignature scheme.
A Solidity example9 is available.
bn128_bls-multisignature-solidity-args.py
is a python script that generates a G1 aggregated signature and print the G1 and G2 aggregated public keys.
These can be imported in BN128.t.sol
to verify the signature with Solidity.
This Solidity example uses Ethereum precompiled contracts[^10] to verify the BLS signature.
It is based on Foundry.
Requirements:
cd bn128-solidity
# Install Foundry
curl -L https://foundry.paradigm.xyz | bash
# Update Foundry
foundryup
# Then launch test
forge test
Footnotes
-
"BN254 For The Rest Of Us", https://hackmd.io/@jpw/bn254 ↩
-
"Extended Tower Number Field Sieve: A New Complexity for the Medium Prime Case", https://eprint.iacr.org/2015/1027.pdf ↩
-
"EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128", https://eips.ethereum.org/EIPS/eip-196 ↩
-
"EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128", https://eips.ethereum.org/EIPS/eip-197 ↩
-
"On the Size of Pairing-based Non-interactive Arguments", https://eprint.iacr.org/2016/260.pdf ↩
-
"PlonK: Permutations over Lagrange-bases for Oecumenical Noninteractive arguments of Knowledge", https://eprint.iacr.org/2019/953.pdf ↩
-
"BLS12-381 For The Rest Of Us", https://hackmd.io/@benjaminion/bls12-381 ↩
-
"Pairing-Friendly Elliptic Curves of Prime Order", https://www.cryptojedi.org/papers/pfcpo.pdf ↩
-
"BLS Signatures in Solidity", https://hackmd.io/@liangcc/bls-solidity ↩