impl ConditionallySelectable for Field

This commit is contained in:
Jack Grigg 2019-12-12 23:15:48 +00:00
parent 575ea832f4
commit b118a07633
4 changed files with 32 additions and 0 deletions

View File

@ -21,6 +21,7 @@ byteorder = "1"
ff = { version = "0.6", path = "../ff", features = ["derive"] } ff = { version = "0.6", path = "../ff", features = ["derive"] }
group = { version = "0.6", path = "../group" } group = { version = "0.6", path = "../group" }
rand_core = "0.5" rand_core = "0.5"
subtle = "2.2.1"
[dev-dependencies] [dev-dependencies]
rand_xorshift = "0.2" rand_xorshift = "0.2"

View File

@ -4,6 +4,7 @@ use super::fq6::Fq6;
use ff::Field; use ff::Field;
use rand_core::RngCore; use rand_core::RngCore;
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use subtle::{Choice, ConditionallySelectable};
/// An element of Fq12, represented by c0 + c1 * w. /// An element of Fq12, represented by c0 + c1 * w.
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[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 { impl Neg for Fq12 {
type Output = Self; type Output = Self;

View File

@ -3,6 +3,7 @@ use ff::{Field, SqrtField};
use rand_core::RngCore; use rand_core::RngCore;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use subtle::{Choice, ConditionallySelectable};
/// An element of Fq2, represented by c0 + c1 * u. /// An element of Fq2, represented by c0 + c1 * u.
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[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 { impl Neg for Fq2 {
type Output = Self; type Output = Self;

View File

@ -3,6 +3,7 @@ use super::fq2::Fq2;
use ff::Field; use ff::Field;
use rand_core::RngCore; use rand_core::RngCore;
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; 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). /// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2).
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[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 { impl Neg for Fq6 {
type Output = Self; type Output = Self;