arnaucube
arnaucube/testudoPublic Self-hosted repository
testudo/src/scalar/mod.rs
src/scalar/mod.rsRaw
 1mod ristretto255;
 2
 3pub type Scalar = ristretto255::Scalar;
 4pub type ScalarBytes = curve25519_dalek::scalar::Scalar;
 5
 6pub trait ScalarFromPrimitives {
 7  fn to_scalar(self) -> Scalar;
 8}
 9
10impl ScalarFromPrimitives for usize {
11  #[inline]
12  fn to_scalar(self) -> Scalar {
13    (0..self).map(|_i| Scalar::one()).sum()
14  }
15}
16
17impl ScalarFromPrimitives for bool {
18  #[inline]
19  fn to_scalar(self) -> Scalar {
20    if self {
21      Scalar::one()
22    } else {
23      Scalar::zero()
24    }
25  }
26}
27
28pub trait ScalarBytesFromScalar {
29  fn decompress_scalar(s: &Scalar) -> ScalarBytes;
30  fn decompress_vector(s: &[Scalar]) -> Vec<ScalarBytes>;
31}
32
33impl ScalarBytesFromScalar for Scalar {
34  fn decompress_scalar(s: &Scalar) -> ScalarBytes {
35    ScalarBytes::from_bytes_mod_order(s.to_bytes())
36  }
37
38  fn decompress_vector(s: &[Scalar]) -> Vec<ScalarBytes> {
39    (0..s.len())
40      .map(|i| Scalar::decompress_scalar(&s[i]))
41      .collect::<Vec<ScalarBytes>>()
42  }
43}