41 lines
1.4 KiB
Rust
41 lines
1.4 KiB
Rust
//! The group-of-unknown-order interface the VDF is generic over.
|
|
|
|
use rug::Integer;
|
|
use std::fmt::Debug;
|
|
|
|
pub trait Group: Sync {
|
|
type Elem: Clone + PartialEq + Debug + Send + Sync;
|
|
|
|
fn name(&self) -> String;
|
|
fn identity(&self) -> Self::Elem;
|
|
/// x <- x^2, in place. This is the sequential step the delay is made of.
|
|
fn square(&self, x: &mut Self::Elem);
|
|
fn mul(&self, a: &Self::Elem, b: &Self::Elem) -> Self::Elem;
|
|
/// Canonical fixed-width encoding, used for hashing and for the proof wire format.
|
|
fn serialize(&self, x: &Self::Elem) -> Vec<u8>;
|
|
fn deserialize(&self, b: &[u8]) -> Option<Self::Elem>;
|
|
/// Membership and canonical-form check on an element received from the network.
|
|
fn is_valid(&self, x: &Self::Elem) -> bool;
|
|
|
|
/// Left-to-right square and multiply. Used for the short exponents in verification.
|
|
fn pow(&self, x: &Self::Elem, e: &Integer) -> Self::Elem {
|
|
let mut r = self.identity();
|
|
let bits = e.significant_bits();
|
|
for i in (0..bits).rev() {
|
|
self.square(&mut r);
|
|
if e.get_bit(i) {
|
|
r = self.mul(&r, x);
|
|
}
|
|
}
|
|
r
|
|
}
|
|
|
|
/// x^(2^k) by k squarings.
|
|
fn pow2k(&self, x: &Self::Elem, k: u64) -> Self::Elem {
|
|
let mut r = x.clone();
|
|
for _ in 0..k {
|
|
self.square(&mut r);
|
|
}
|
|
r
|
|
}
|
|
}
|