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 52
/*
Copyright Michael Lodder. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
use crate::inner_types::*;
use crate::util::*;
#[cfg(feature = "wasm")]
use core::convert::TryFrom;
use serde::{Deserialize, Serialize};
use subtle::CtOption;
/// A blinding factor is applied to a token to enable
/// multi factor authentication
///
/// ```
/// use oberon::Blinding;
///
/// let blinding = Blinding::new(b"1234");
///
/// assert_ne!(blinding.to_bytes(), [0u8; Blinding::BYTES]);
/// ```
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub struct Blinding(pub(crate) G1Projective);
impl Default for Blinding {
fn default() -> Self {
Self(G1Projective::IDENTITY)
}
}
#[cfg(feature = "wasm")]
wasm_slice_impl!(Blinding);
impl Blinding {
/// The number of bytes in a blinding factor
pub const BYTES: usize = 48;
/// Create a new blinding factor
pub fn new(data: &[u8]) -> Self {
Self(hash_to_curve(data))
}
/// Convert this blinding factor into a byte sequence
pub fn to_bytes(&self) -> [u8; Self::BYTES] {
self.0.to_affine().to_compressed()
}
/// Convert a byte sequence to a blinding factor
pub fn from_bytes(data: &[u8; 48]) -> CtOption<Self> {
G1Affine::from_compressed(data).map(|p| Self(G1Projective::from(p)))
}
}