From 002173e187ff5ce94a2a5e722300509fcf0ba363 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 2 Jul 2018 20:58:41 +0100 Subject: [PATCH 1/5] Update README.md Closes #4. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4bd82ad45..d08329e60 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ ## Disclaimers * This library does not provide constant-time guarantees. -* This library relies on Rust's `i128_type` feature, which is currently only available in the nightly compiler. ## Usage @@ -13,10 +12,10 @@ Add the `ff` crate to your `Cargo.toml`: ```toml [dependencies] -ff = "0.2" +ff = "0.3" ``` -The `ff` crate contains `Field`, `PrimeField`, `PrimeFieldRepr` and `SqrtField` traits. See the **[documentation](https://docs.rs/ff/0.2.0/ff/)** for more. +The `ff` crate contains `Field`, `PrimeField`, `PrimeFieldRepr` and `SqrtField` traits. See the **[documentation](https://docs.rs/ff/0.3.0/ff/)** for more. ### #![derive(PrimeField)] From 69ce66ae6cf43755e7fd5eea75f6945d0fa6ac2b Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 2 Jul 2018 21:08:24 +0100 Subject: [PATCH 2/5] Place ff_derive re-exports behind a feature Part of #3. --- Cargo.toml | 2 +- README.md | 9 +++++++++ src/lib.rs | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d2af3deca..5f34fa040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,6 @@ rand = "0.4" ff_derive = { version = "0.3.0", path = "ff_derive", optional = true } [features] -default = ["derive"] +default = [] u128-support = [] derive = ["ff_derive"] diff --git a/README.md b/README.md index d08329e60..ae676c162 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,15 @@ The `ff` crate contains `Field`, `PrimeField`, `PrimeFieldRepr` and `SqrtField` If you need an implementation of a prime field, this library also provides a procedural macro that will expand into an efficient implementation of a prime field when supplied with the modulus. `PrimeFieldGenerator` must be an element of Fp of p-1 order, that is also quadratic nonresidue. +First, enable the `derive` crate feature: + +```toml +[dependencies] +ff = { version = "0.3", features = ["derive"] } +``` + +And then use the macro like so: + ```rust extern crate rand; #[macro_use] diff --git a/src/lib.rs b/src/lib.rs index 541fe76a2..d79bc146f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,11 @@ extern crate byteorder; extern crate rand; +#[cfg(feature = "derive")] #[macro_use] extern crate ff_derive; +#[cfg(feature = "derive")] pub use ff_derive::*; use std::error::Error; From 0eb9f5040b8d24703392ba23798347a1163ce802 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 2 Jul 2018 21:19:20 +0100 Subject: [PATCH 3/5] Remove u128-support feature and arithmetic Closes #2. --- Cargo.toml | 1 - src/lib.rs | 80 ------------------------------------------------------ 2 files changed, 81 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f34fa040..0cdd63359 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,4 @@ ff_derive = { version = "0.3.0", path = "ff_derive", optional = true } [features] default = [] -u128-support = [] derive = ["ff_derive"] diff --git a/src/lib.rs b/src/lib.rs index d79bc146f..b662dc442 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -349,7 +349,6 @@ fn test_bit_iterator() { pub use self::arith_impl::*; -#[cfg(feature = "u128-support")] mod arith_impl { /// Calculate a - b - borrow, returning the result and modifying /// the borrow value. @@ -384,82 +383,3 @@ mod arith_impl { tmp as u64 } } - -#[cfg(not(feature = "u128-support"))] -mod arith_impl { - #[inline(always)] - fn split_u64(i: u64) -> (u64, u64) { - (i >> 32, i & 0xFFFFFFFF) - } - - #[inline(always)] - fn combine_u64(hi: u64, lo: u64) -> u64 { - (hi << 32) | lo - } - - /// Calculate a - b - borrow, returning the result and modifying - /// the borrow value. - #[inline(always)] - pub fn sbb(a: u64, b: u64, borrow: &mut u64) -> u64 { - let (a_hi, a_lo) = split_u64(a); - let (b_hi, b_lo) = split_u64(b); - let (b, r0) = split_u64((1 << 32) + a_lo - b_lo - *borrow); - let (b, r1) = split_u64((1 << 32) + a_hi - b_hi - ((b == 0) as u64)); - - *borrow = (b == 0) as u64; - - combine_u64(r1, r0) - } - - /// Calculate a + b + carry, returning the sum and modifying the - /// carry value. - #[inline(always)] - pub fn adc(a: u64, b: u64, carry: &mut u64) -> u64 { - let (a_hi, a_lo) = split_u64(a); - let (b_hi, b_lo) = split_u64(b); - let (carry_hi, carry_lo) = split_u64(*carry); - - let (t, r0) = split_u64(a_lo + b_lo + carry_lo); - let (t, r1) = split_u64(t + a_hi + b_hi + carry_hi); - - *carry = t; - - combine_u64(r1, r0) - } - - /// Calculate a + (b * c) + carry, returning the least significant digit - /// and setting carry to the most significant digit. - #[inline(always)] - pub fn mac_with_carry(a: u64, b: u64, c: u64, carry: &mut u64) -> u64 { - /* - [ b_hi | b_lo ] - [ c_hi | c_lo ] * - ------------------------------------------- - [ b_lo * c_lo ] <-- w - [ b_hi * c_lo ] <-- x - [ b_lo * c_hi ] <-- y - [ b_hi * c_lo ] <-- z - [ a_hi | a_lo ] - [ C_hi | C_lo ] - */ - - let (a_hi, a_lo) = split_u64(a); - let (b_hi, b_lo) = split_u64(b); - let (c_hi, c_lo) = split_u64(c); - let (carry_hi, carry_lo) = split_u64(*carry); - - let (w_hi, w_lo) = split_u64(b_lo * c_lo); - let (x_hi, x_lo) = split_u64(b_hi * c_lo); - let (y_hi, y_lo) = split_u64(b_lo * c_hi); - let (z_hi, z_lo) = split_u64(b_hi * c_hi); - - let (t, r0) = split_u64(w_lo + a_lo + carry_lo); - let (t, r1) = split_u64(t + w_hi + x_lo + y_lo + a_hi + carry_hi); - let (t, r2) = split_u64(t + x_hi + y_hi + z_lo); - let (_, r3) = split_u64(t + z_hi); - - *carry = combine_u64(r3, r2); - - combine_u64(r1, r0) - } -} From 2067360930fbf07b74109fe776c4260766046816 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 2 Jul 2018 21:31:35 +0100 Subject: [PATCH 4/5] Add ScalarEngine trait This is extracted from pairing's Engine trait. --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b662dc442..a9d117f24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,6 +290,14 @@ pub trait PrimeField: Field { fn root_of_unity() -> Self; } +/// An "engine" is a collection of types (fields, elliptic curve groups, etc.) +/// with well-defined relationships. Specific relationships (for example, a +/// pairing-friendly curve) can be defined in a subtrait. +pub trait ScalarEngine: Sized + 'static + Clone { + /// This is the scalar field of the engine's groups. + type Fr: PrimeField + SqrtField; +} + #[derive(Debug)] pub struct BitIterator { t: E, From 526676ecfce49661fe18d0e1635fde331b8e150d Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 2 Jul 2018 23:07:41 +0100 Subject: [PATCH 5/5] Bump version to 0.4.0 --- Cargo.toml | 2 +- README.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0cdd63359..22db67a8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ff" -version = "0.3.0" +version = "0.4.0" authors = ["Sean Bowe "] description = "Library for building and interfacing with finite fields" documentation = "https://docs.rs/ff/" diff --git a/README.md b/README.md index ae676c162..3efef941b 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ Add the `ff` crate to your `Cargo.toml`: ```toml [dependencies] -ff = "0.3" +ff = "0.4" ``` -The `ff` crate contains `Field`, `PrimeField`, `PrimeFieldRepr` and `SqrtField` traits. See the **[documentation](https://docs.rs/ff/0.3.0/ff/)** for more. +The `ff` crate contains `Field`, `PrimeField`, `PrimeFieldRepr` and `SqrtField` traits. See the **[documentation](https://docs.rs/ff/0.4.0/ff/)** for more. ### #![derive(PrimeField)] @@ -25,7 +25,7 @@ First, enable the `derive` crate feature: ```toml [dependencies] -ff = { version = "0.3", features = ["derive"] } +ff = { version = "0.4", features = ["derive"] } ``` And then use the macro like so: