Skip to content

Commit 24aa1a4

Browse files
authored
Merge pull request #31 from zkcrypto/release-0.1.1
Release 0.1.1
2 parents e32494e + 82e14ed commit 24aa1a4

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ jobs:
4343
uses: actions-rs/cargo@v1
4444
with:
4545
command: build
46-
args: --verbose --release --tests
46+
args: --verbose --release --tests --features endo
4747
- name: Run tests
48+
uses: actions-rs/cargo@v1
49+
with:
50+
command: test
51+
args: --verbose --release --features endo
52+
- name: Build tests (no endomorphism)
53+
uses: actions-rs/cargo@v1
54+
with:
55+
command: build
56+
args: --verbose --release --tests
57+
- name: Run tests (no endomorphism)
4858
uses: actions-rs/cargo@v1
4959
with:
5060
command: test

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ homepage = "https://github.com/zkcrypto/bls12_381"
66
license = "MIT/Apache-2.0"
77
name = "bls12_381"
88
repository = "https://github.com/zkcrypto/bls12_381"
9-
version = "0.1.0"
9+
version = "0.1.1"
1010
edition = "2018"
1111

1212
[package.metadata.docs.rs]
@@ -30,3 +30,6 @@ groups = []
3030
pairings = ["groups"]
3131
alloc = []
3232
nightly = ["subtle/nightly"]
33+
34+
# GLV patents US7110538B2 and US7995752B2 expire in September 2020.
35+
endo = []

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This crate provides an implementation of the BLS12-381 pairing-friendly elliptic
1313
* `pairings` (on by default): Enables some APIs for performing pairings.
1414
* `alloc` (on by default): Enables APIs that require an allocator; these include pairing optimizations.
1515
* `nightly`: Enables `subtle/nightly` which tries to prevent compiler optimizations that could jeopardize constant time operations. Requires the nightly Rust compiler.
16+
* `endo`: Enables optimizations that leverage curve endomorphisms, which may run foul of patents US7110538B2 and US7995752B2 set to expire in September 2020.
1617

1718
## [Documentation](https://docs.rs/bls12_381)
1819

RELEASES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# 0.1.1
2+
3+
Added `clear_cofactor` methods to `G1Projective` and `G2Projective`. If the crate feature `endo`
4+
is enabled the G2 cofactor clearing will use the curve endomorphism technique described by
5+
[Budroni-Pintore](https://ia.cr/2017/419). If the crate feature `endo` is _not_ enabled then
6+
the code will simulate the effects of the Budroni-Pintore cofactor clearing in order to keep
7+
the API consistent. In September 2020, when patents US7110538B2 and US7995752B2 expire, the
8+
endo feature will be made default. However, for now it must be explicitly enabled.
9+
110
# 0.1.0
211

312
Initial release.

src/g2.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ impl G2Projective {
805805
G2Projective::conditional_select(&res, &tmp, (!f1) & (!f2) & (!f3))
806806
}
807807

808-
fn multiply(&self, by: &[u8; 32]) -> G2Projective {
808+
fn multiply(&self, by: &[u8]) -> G2Projective {
809809
let mut acc = G2Projective::identity();
810810

811811
// This is a simple double-and-add implementation of point
@@ -827,6 +827,7 @@ impl G2Projective {
827827
acc
828828
}
829829

830+
#[cfg(feature = "endo")]
830831
fn psi(&self) -> G2Projective {
831832
// 1 / ((u+1) ^ ((q-1)/3))
832833
let psi_coeff_x = Fp2 {
@@ -870,6 +871,7 @@ impl G2Projective {
870871
}
871872
}
872873

874+
#[cfg(feature = "endo")]
873875
fn psi2(&self) -> G2Projective {
874876
// 1 / 2 ^ ((q-1)/3)
875877
let psi2_coeff_x = Fp2 {
@@ -895,6 +897,7 @@ impl G2Projective {
895897
}
896898

897899
/// Multiply `self` by `crate::BLS_X`, using double and add.
900+
#[cfg(feature = "endo")]
898901
fn mul_by_x(&self) -> G2Projective {
899902
let mut xself = G2Projective::identity();
900903
// NOTE: in BLS12-381 we can just skip the first bit.
@@ -918,15 +921,36 @@ impl G2Projective {
918921
/// This is equivalent to multiplying by $h\_\textrm{eff} = 3(z^2 - 1) \cdot
919922
/// h_2$, where $h_2$ is the cofactor of $\mathbb{G}\_2$ and $z$ is the
920923
/// parameter of BLS12-381.
924+
///
925+
/// The endomorphism is only actually used if the crate feature `endo` is
926+
/// enabled, and it is disabled by default to mitigate potential patent
927+
/// issues.
921928
pub fn clear_cofactor(&self) -> G2Projective {
922-
let t1 = self.mul_by_x(); // [x] P
923-
let t2 = self.psi(); // psi(P)
924-
925-
self.double().psi2() // psi^2(2P)
926-
+ (t1 + t2).mul_by_x() // psi^2(2P) + [x^2] P + [x] psi(P)
927-
- t1 // psi^2(2P) + [x^2 - x] P + [x] psi(P)
928-
- t2 // psi^2(2P) + [x^2 - x] P + [x - 1] psi(P)
929-
- self // psi^2(2P) + [x^2 - x - 1] P + [x - 1] psi(P)
929+
#[cfg(feature = "endo")]
930+
fn clear_cofactor(this: &G2Projective) -> G2Projective {
931+
let t1 = this.mul_by_x(); // [x] P
932+
let t2 = this.psi(); // psi(P)
933+
934+
this.double().psi2() // psi^2(2P)
935+
+ (t1 + t2).mul_by_x() // psi^2(2P) + [x^2] P + [x] psi(P)
936+
- t1 // psi^2(2P) + [x^2 - x] P + [x] psi(P)
937+
- t2 // psi^2(2P) + [x^2 - x] P + [x - 1] psi(P)
938+
- this // psi^2(2P) + [x^2 - x - 1] P + [x - 1] psi(P)
939+
}
940+
941+
#[cfg(not(feature = "endo"))]
942+
fn clear_cofactor(this: &G2Projective) -> G2Projective {
943+
this.multiply(&[
944+
0x51, 0x55, 0xa9, 0xaa, 0x5, 0x0, 0x2, 0xe8, 0xb4, 0xf6, 0xbb, 0xde, 0xa, 0x4c,
945+
0x89, 0x59, 0xa3, 0xf6, 0x89, 0x66, 0xc0, 0xcb, 0x54, 0xe9, 0x1a, 0x7c, 0x47, 0xd7,
946+
0x69, 0xec, 0xc0, 0x2e, 0xb0, 0x12, 0x12, 0x5d, 0x1, 0xbf, 0x82, 0x6d, 0x95, 0xdb,
947+
0x31, 0x87, 0x17, 0x2f, 0x9c, 0x32, 0xe1, 0xff, 0x8, 0x15, 0x3, 0xff, 0x86, 0x99,
948+
0x68, 0xd7, 0x5a, 0x14, 0xe9, 0xa8, 0xe2, 0x88, 0x28, 0x35, 0x1b, 0xa9, 0xe, 0x6a,
949+
0x4c, 0x58, 0xb3, 0x75, 0xee, 0xf2, 0x8, 0x9f, 0xc6, 0xb,
950+
])
951+
}
952+
953+
clear_cofactor(self)
930954
}
931955

932956
/// Converts a batch of `G2Projective` elements into `G2Affine` elements. This
@@ -1653,6 +1677,7 @@ fn test_is_torsion_free() {
16531677
assert!(bool::from(G2Affine::generator().is_torsion_free()));
16541678
}
16551679

1680+
#[cfg(feature = "endo")]
16561681
#[test]
16571682
fn test_mul_by_x() {
16581683
// multiplying by `x` a point in G2 is the same as multiplying by
@@ -1669,6 +1694,7 @@ fn test_mul_by_x() {
16691694
assert_eq!(point.mul_by_x(), point * x);
16701695
}
16711696

1697+
#[cfg(feature = "endo")]
16721698
#[test]
16731699
fn test_psi() {
16741700
let generator = G2Projective::generator();

0 commit comments

Comments
 (0)