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
use crate::util::*;
use bls12_381_plus::Scalar;
use ff::Field;
use rand_core::RngCore;
use serde::{Deserialize, Serialize};
use subtle::CtOption;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct Nonce(pub Scalar);
impl Nonce {
pub const BYTES: usize = 32;
pub fn hash<B: AsRef<[u8]>>(data: B) -> Self {
Self(hash_to_scalar(data))
}
pub fn random(rng: impl RngCore) -> Self {
Self(Scalar::random(rng))
}
pub fn to_bytes(&self) -> [u8; Self::BYTES] {
scalar_to_bytes(self.0)
}
pub fn from_bytes(bytes: &[u8; Self::BYTES]) -> CtOption<Self> {
scalar_from_bytes(bytes).map(|s| Self(s))
}
}