Trait prio::field::FieldElement [−][src]
pub trait FieldElement: Sized + Debug + Copy + PartialEq + Eq + Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign + Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign + Neg<Output = Self> + Display + From<Self::Integer> + for<'a> TryFrom<&'a [u8], Error = FieldError> + Into<Vec<u8>> + Serialize + DeserializeOwned + 'static {
type IntegerTryFromError: Debug;
type Integer: Copy + Debug + PartialOrd + BitAnd<Output = <Self as FieldElement>::Integer> + Div<Output = <Self as FieldElement>::Integer> + Shr<Output = <Self as FieldElement>::Integer> + Sub<Output = <Self as FieldElement>::Integer> + TryFrom<usize, Error = Self::IntegerTryFromError>;
const ENCODED_SIZE: usize;
fn pow(&self, exp: Self::Integer) -> Self;
fn inv(&self) -> Self;
fn modulus() -> Self::Integer;
fn try_from_reader<R: Read>(reader: &mut R) -> Result<Self, FieldError>;
fn generator_order() -> Self::Integer;
fn generator() -> Self;
fn root(l: usize) -> Option<Self>;
fn zero() -> Self;
fn one() -> Self;
fn slice_into_byte_vec(values: &[Self]) -> Vec<u8> { ... }
fn byte_slice_into_vec(bytes: &[u8]) -> Result<Vec<Self>, FieldError> { ... }
}
Expand description
Objects with this trait represent an element of GF(p)
for some prime p
.
Associated Types
The error returned if converting usize
to an Int
fails.
type Integer: Copy + Debug + PartialOrd + BitAnd<Output = <Self as FieldElement>::Integer> + Div<Output = <Self as FieldElement>::Integer> + Shr<Output = <Self as FieldElement>::Integer> + Sub<Output = <Self as FieldElement>::Integer> + TryFrom<usize, Error = Self::IntegerTryFromError>
type Integer: Copy + Debug + PartialOrd + BitAnd<Output = <Self as FieldElement>::Integer> + Div<Output = <Self as FieldElement>::Integer> + Shr<Output = <Self as FieldElement>::Integer> + Sub<Output = <Self as FieldElement>::Integer> + TryFrom<usize, Error = Self::IntegerTryFromError>
The integer representation of the field element.
Associated Constants
const ENCODED_SIZE: usize
const ENCODED_SIZE: usize
Size in bytes of the encoding of a value.
Required methods
Modular inversion, i.e., self^-1 (mod p)
. If self
is 0, then the output is undefined.
fn try_from_reader<R: Read>(reader: &mut R) -> Result<Self, FieldError>
fn try_from_reader<R: Read>(reader: &mut R) -> Result<Self, FieldError>
Interprets the next Self::ENCODED_SIZE
bytes from the input slice as an element of the
field.
Errors
An error is returned if the provided slice is too small to encode a field element or if the result encodes an integer larger than the field modulus.
Notes
Ideally we would implement TryFrom<R: Read> for FieldElement
but the stdlib’s blanket
implementation of TryFrom
forbids this: https://github.com/rust-lang/rust/issues/50133
fn generator_order() -> Self::Integer
fn generator_order() -> Self::Integer
Returns the size of the multiplicative subgroup generated by generator()
.
Returns the generator of the multiplicative subgroup of size generator_order()
.
Returns the 2^l
-th principal root of unity for any l <= 20
. Note that the 2^0
-th
prinicpal root of unity is 1 by definition.
Provided methods
fn slice_into_byte_vec(values: &[Self]) -> Vec<u8>
fn slice_into_byte_vec(values: &[Self]) -> Vec<u8>
Convert a slice of field elements into a vector of bytes.
Notes
Ideally we would implement From<&[F: FieldElement]> for Vec<u8>
or the corresponding
Into
, but the orphan rule and the stdlib’s blanket implementations of Into
make this
impossible.
fn byte_slice_into_vec(bytes: &[u8]) -> Result<Vec<Self>, FieldError>
fn byte_slice_into_vec(bytes: &[u8]) -> Result<Vec<Self>, FieldError>
Convert a slice of bytes into a vector of field elements. The slice is interpreted as a
sequence of Self::ENCODED_SIZE
-byte sequences.
Errors
Returns an error if the length of the provided byte slice is not a multiple of the size of a
field element, or if any of the values in the byte slice are invalid encodings of a field
element as documented in Self::try_from_reader
.
Notes
Ideally we would implement From<&[u8]> for Vec<F: FieldElement>
or the corresponding
Into
, but the orphan rule and the stdlib’s blanket implementations of Into
make this
impossible.