diff --git a/Cargo.toml b/Cargo.toml index daf6018..b95ed58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ byteorder = "1" ff = { version = "0.6", path = "../ff", features = ["derive"] } group = { version = "0.6", path = "../group" } rand_core = "0.5" +subtle = "2.2.1" [dev-dependencies] rand_xorshift = "0.2" diff --git a/src/bls12_381/fq12.rs b/src/bls12_381/fq12.rs index 56395a4..42b922e 100644 --- a/src/bls12_381/fq12.rs +++ b/src/bls12_381/fq12.rs @@ -4,6 +4,7 @@ use super::fq6::Fq6; use ff::Field; use rand_core::RngCore; use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use subtle::{Choice, ConditionallySelectable}; /// An element of Fq12, represented by c0 + c1 * w. #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -40,6 +41,15 @@ impl Fq12 { } } +impl ConditionallySelectable for Fq12 { + fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { + Fq12 { + c0: Fq6::conditional_select(&a.c0, &b.c0, choice), + c1: Fq6::conditional_select(&a.c1, &b.c1, choice), + } + } +} + impl Neg for Fq12 { type Output = Self; diff --git a/src/bls12_381/fq2.rs b/src/bls12_381/fq2.rs index 8a53109..1950b76 100644 --- a/src/bls12_381/fq2.rs +++ b/src/bls12_381/fq2.rs @@ -3,6 +3,7 @@ use ff::{Field, SqrtField}; use rand_core::RngCore; use std::cmp::Ordering; use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use subtle::{Choice, ConditionallySelectable}; /// An element of Fq2, represented by c0 + c1 * u. #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -54,6 +55,15 @@ impl Fq2 { } } +impl ConditionallySelectable for Fq2 { + fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { + Fq2 { + c0: Fq::conditional_select(&a.c0, &b.c0, choice), + c1: Fq::conditional_select(&a.c1, &b.c1, choice), + } + } +} + impl Neg for Fq2 { type Output = Self; diff --git a/src/bls12_381/fq6.rs b/src/bls12_381/fq6.rs index 5f53a50..3e56f59 100644 --- a/src/bls12_381/fq6.rs +++ b/src/bls12_381/fq6.rs @@ -3,6 +3,7 @@ use super::fq2::Fq2; use ff::Field; use rand_core::RngCore; use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use subtle::{Choice, ConditionallySelectable}; /// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2). #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -100,6 +101,16 @@ impl Fq6 { } } +impl ConditionallySelectable for Fq6 { + fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { + Fq6 { + c0: Fq2::conditional_select(&a.c0, &b.c0, choice), + c1: Fq2::conditional_select(&a.c1, &b.c1, choice), + c2: Fq2::conditional_select(&a.c2, &b.c2, choice), + } + } +} + impl Neg for Fq6 { type Output = Self;