bn/README.md

81 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

2016-09-11 12:19:45 -07:00
# bn [![Crates.io](https://img.shields.io/crates/v/bn.svg)](https://crates.io/crates/bn) [![Build status](https://api.travis-ci.org/zcash/bn.svg)](https://travis-ci.org/zcash/bn)
2016-06-28 21:07:14 -07:00
2016-09-18 10:49:10 -07:00
This is a [pairing cryptography](https://en.wikipedia.org/wiki/Pairing-based_cryptography) library written in pure Rust. It makes use of the Barreto-Naehrig (BN) curve construction from [[BCTV2015]](https://eprint.iacr.org/2013/879.pdf) to provide two cyclic groups **G<sub>1</sub>** and **G<sub>2</sub>**, with an efficient bilinear pairing:
2016-06-28 21:07:14 -07:00
*e: G<sub>1</sub> × G<sub>2</sub> → G<sub>T</sub>*
2016-09-18 10:49:10 -07:00
## Security warnings
This library, like other pairing cryptography libraries implementing this construction, is not resistant to side-channel attacks.
2016-06-28 21:07:14 -07:00
## Usage
Add the `bn` crate to your dependencies in `Cargo.toml`...
```toml
[dependencies]
2017-04-02 12:50:16 -07:00
bn = "0.4.3"
2016-06-28 21:07:14 -07:00
```
...and add an `extern crate` declaration to your crate root:
```rust
extern crate bn;
```
## API
* `Fr` is an element of F<sub>r</sub>
2016-06-28 21:07:14 -07:00
* `G1` is a point on the BN curve E/Fq : y^2 = x^3 + b
* `G2` is a point on the twisted BN curve E'/Fq2 : y^2 = x^3 + b/xi
* `Gt` is a group element (written multiplicatively) obtained with the `pairing` function over `G1` and `G2`.
### Examples
#### Joux's key agreement protocol
In a typical Diffie-Hellman key exchange, relying on ECDLP, a three-party key exchange requires two rounds. A single round protocol is possible through the use of a bilinear pairing: given Alice's public key *a*P<sub>1</sub> and Bob's public key *b*P<sub>2</sub>, Carol can compute the shared secret with her private key *c* by *e*(*a*P<sub>1</sub>, *b*P<sub>2</sub>)<sup>c</sup>.
2016-07-04 08:24:01 -07:00
(See `examples/joux.rs` for the full example.)
2016-06-28 21:07:14 -07:00
2016-07-04 08:24:01 -07:00
```rust
2016-06-28 21:07:14 -07:00
// Generate private keys
let alice_sk = Fr::random(rng);
let bob_sk = Fr::random(rng);
let carol_sk = Fr::random(rng);
2016-06-28 21:07:14 -07:00
// Generate public keys in G1 and G2
let (alice_pk1, alice_pk2) = (G1::one() * alice_sk, G2::one() * alice_sk);
let (bob_pk1, bob_pk2) = (G1::one() * bob_sk, G2::one() * bob_sk);
let (carol_pk1, carol_pk2) = (G1::one() * carol_sk, G2::one() * carol_sk);
2016-06-28 21:07:14 -07:00
// Each party computes the shared secret
2016-09-11 12:22:40 -07:00
let alice_ss = pairing(bob_pk1, carol_pk2).pow(alice_sk);
let bob_ss = pairing(carol_pk1, alice_pk2).pow(bob_sk);
let carol_ss = pairing(alice_pk1, bob_pk2).pow(carol_sk);
2016-06-28 21:07:14 -07:00
2016-07-04 08:37:48 -07:00
assert!(alice_ss == bob_ss && bob_ss == carol_ss);
2016-06-28 21:07:14 -07:00
```
## License
Licensed under either of
* MIT license, ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Copyright 2016 [Zcash Electric Coin Company](https://z.cash/). The Zcash Company promises to maintain the "bn" crate on crates.io under this MIT/Apache-2.0 dual license.
### Authors
* [Sean Bowe](https://github.com/ebfull)
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.