miden_crypto/dsa/rpo_falcon512/
mod.rs1use crate::{
2 hash::rpo::Rpo256,
3 utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
4 Felt, Word, ZERO,
5};
6
7mod hash_to_point;
8mod keys;
9mod math;
10mod signature;
11
12pub use self::{
13 keys::{PubKeyPoly, PublicKey, SecretKey},
14 math::Polynomial,
15 signature::{Signature, SignatureHeader, SignaturePoly},
16};
17
18const MODULUS: i16 = 12289;
23
24const FALCON_ENCODING_BITS: u32 = 14;
26
27const N: usize = 512;
30const LOG_N: u8 = 9;
31
32const SIG_NONCE_LEN: usize = 40;
34
35const NONCE_ELEMENTS: usize = 8;
37
38pub const PK_LEN: usize = 897;
40
41pub const SK_LEN: usize = 1281;
43
44const SIG_POLY_BYTE_LEN: usize = 625;
46
47const SIG_L2_BOUND: u64 = 34034726;
49
50const SIGMA: f64 = 165.7366171829776;
52
53type ShortLatticeBasis = [Polynomial<i16>; 4];
57
58#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct Nonce([u8; SIG_NONCE_LEN]);
64
65impl Nonce {
66 pub fn new(bytes: [u8; SIG_NONCE_LEN]) -> Self {
68 Self(bytes)
69 }
70
71 pub fn as_bytes(&self) -> &[u8; SIG_NONCE_LEN] {
73 &self.0
74 }
75
76 pub fn to_elements(&self) -> [Felt; NONCE_ELEMENTS] {
81 let mut buffer = [0_u8; 8];
82 let mut result = [ZERO; 8];
83 for (i, bytes) in self.0.chunks(5).enumerate() {
84 buffer[..5].copy_from_slice(bytes);
85 result[i] = Felt::new(u64::from_le_bytes(buffer));
88 }
89
90 result
91 }
92}
93
94impl Serializable for &Nonce {
95 fn write_into<W: ByteWriter>(&self, target: &mut W) {
96 target.write_bytes(&self.0)
97 }
98}
99
100impl Deserializable for Nonce {
101 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
102 let bytes = source.read()?;
103 Ok(Self(bytes))
104 }
105}