1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crypto_bigint::{Choice, CtEq};
/// Multiplicative identity (unity).
pub trait One: CtEq + Sized {
/// Create an instance of the multiplicative identity (i.e. the value `1`)
/// in the underlying algebraic structure.
fn one() -> Self;
/// Determine if this value is the multiplicative identity (i.e. `Self::one`)
/// in the underlying algebraic structure.
/// If so, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
#[inline]
fn is_one(&self) -> Choice {
self.ct_eq(&Self::one())
}
/// Set `self` to the multiplicative identity (i.e. `Self::one`)
/// in the underlying algebraic structure.
#[inline]
fn set_one(&mut self) {
*self = One::one();
}
/// Return the value `1` in the same algebraic structure as `other`.
fn one_like(other: &Self) -> Self where Self: Clone {
let mut ret = other.clone();
ret.set_one();
ret
}
}
pub trait BNField {
/// Convert `self` to byte array representation.
fn to_bytes(&self) -> Vec<u8>;
/// Compute the value of 2×`self`.
fn double(&self) -> Self;
/// Compute the value of `self`/2.
fn half(&self) -> Self;
/// Compute `self`².
fn sq(&self) -> Self;
/// Compute `self`³.
fn cb(&self) -> Self;
/// Compute the inverse of `self` (or 0, if `self` itself is zero).
fn inv(&self) -> Self;
}