miden-crypto/src/dsa/rpo_falcon512/mod.rs
src/dsa/rpo_falcon512/mod.rsRaw
1use 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::keys::{PubKeyPoly, PublicKey, SecretKey};
13pub use self::math::Polynomial;
14pub use self::signature::{Signature, SignatureHeader, SignaturePoly};
15
16// CONSTANTS
17// ================================================================================================
18
19// The Falcon modulus p.
20const MODULUS: i16 = 12289;
21
22// Number of bits needed to encode an element in the Falcon field.
23const FALCON_ENCODING_BITS: u32 = 14;
24
25// The Falcon parameters for Falcon-512. This is the degree of the polynomial `phi := x^N + 1`
26// defining the ring Z_p[x]/(phi).
27const N: usize = 512;
28const LOG_N: u8 = 9;
29
30/// Length of nonce used for key-pair generation.
31const SIG_NONCE_LEN: usize = 40;
32
33/// Number of filed elements used to encode a nonce.
34const NONCE_ELEMENTS: usize = 8;
35
36/// Public key length as a u8 vector.
37pub const PK_LEN: usize = 897;
38
39/// Secret key length as a u8 vector.
40pub const SK_LEN: usize = 1281;
41
42/// Signature length as a u8 vector.
43const SIG_POLY_BYTE_LEN: usize = 625;
44
45/// Bound on the squared-norm of the signature.
46const SIG_L2_BOUND: u64 = 34034726;
47
48/// Standard deviation of the Gaussian over the lattice.
49const SIGMA: f64 = 165.7366171829776;
50
51// TYPE ALIASES
52// ================================================================================================
53
54type ShortLatticeBasis = [Polynomial<i16>; 4];
55
56// NONCE
57// ================================================================================================
58
59/// Nonce of the Falcon signature.
60#[derive(Debug, Clone, PartialEq, Eq)]
61pub struct Nonce([u8; SIG_NONCE_LEN]);
62
63impl Nonce {
64 /// Returns a new [Nonce] instantiated from the provided bytes.
65 pub fn new(bytes: [u8; SIG_NONCE_LEN]) -> Self {
66 Self(bytes)
67 }
68
69 /// Returns the underlying bytes of this nonce.
70 pub fn as_bytes(&self) -> &[u8; SIG_NONCE_LEN] {
71 &self.0
72 }
73
74 /// Converts byte representation of the nonce into field element representation.
75 ///
76 /// Nonce bytes are converted to field elements by taking consecutive 5 byte chunks
77 /// of the nonce and interpreting them as field elements.
78 pub fn to_elements(&self) -> [Felt; NONCE_ELEMENTS] {
79 let mut buffer = [0_u8; 8];
80 let mut result = [ZERO; 8];
81 for (i, bytes) in self.0.chunks(5).enumerate() {
82 buffer[..5].copy_from_slice(bytes);
83 // we can safely (without overflow) create a new Felt from u64 value here since this
84 // value contains at most 5 bytes
85 result[i] = Felt::new(u64::from_le_bytes(buffer));
86 }
87
88 result
89 }
90}
91
92impl Serializable for &Nonce {
93 fn write_into<W: ByteWriter>(&self, target: &mut W) {
94 target.write_bytes(&self.0)
95 }
96}
97
98impl Deserializable for Nonce {
99 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
100 let bytes = source.read()?;
101 Ok(Self(bytes))
102 }
103}