miden-crypto/src/dsa/rpo_falcon512/ffi.rs
src/dsa/rpo_falcon512/ffi.rsRaw
1use libc::c_int;
2
3// C IMPLEMENTATION INTERFACE
4// ================================================================================================
5
6#[link(name = "rpo_falcon512", kind = "static")]
7extern "C" {
8 /// Generate a new key pair. Public key goes into pk[], private key in sk[].
9 /// Key sizes are exact (in bytes):
10 /// - public (pk): 897
11 /// - private (sk): 1281
12 ///
13 /// Return value: 0 on success, -1 on error.
14 pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_rpo(pk: *mut u8, sk: *mut u8) -> c_int;
15
16 /// Generate a new key pair from seed. Public key goes into pk[], private key in sk[].
17 /// Key sizes are exact (in bytes):
18 /// - public (pk): 897
19 /// - private (sk): 1281
20 ///
21 /// Return value: 0 on success, -1 on error.
22 pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_from_seed_rpo(
23 pk: *mut u8,
24 sk: *mut u8,
25 seed: *const u8,
26 ) -> c_int;
27
28 /// Compute a signature on a provided message (m, mlen), with a given private key (sk).
29 /// Signature is written in sig[], with length written into *siglen. Signature length is
30 /// variable; maximum signature length (in bytes) is 666.
31 ///
32 /// sig[], m[] and sk[] may overlap each other arbitrarily.
33 ///
34 /// Return value: 0 on success, -1 on error.
35 pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_signature_rpo(
36 sig: *mut u8,
37 siglen: *mut usize,
38 m: *const u8,
39 mlen: usize,
40 sk: *const u8,
41 ) -> c_int;
42
43 // TEST HELPERS
44 // --------------------------------------------------------------------------------------------
45
46 /// Verify a signature (sig, siglen) on a message (m, mlen) with a given public key (pk).
47 ///
48 /// sig[], m[] and pk[] may overlap each other arbitrarily.
49 ///
50 /// Return value: 0 on success, -1 on error.
51 #[cfg(test)]
52 pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
53 sig: *const u8,
54 siglen: usize,
55 m: *const u8,
56 mlen: usize,
57 pk: *const u8,
58 ) -> c_int;
59
60 /// Hash-to-Point algorithm.
61 ///
62 /// This function generates a point in Z_q[x]/(phi) from a given message.
63 ///
64 /// It takes a finalized rpo128_context as input and it generates the coefficients of the polynomial
65 /// representing the point. The coefficients are stored in the array 'x'. The number of coefficients
66 /// is given by 'logn', which must in our case is 512.
67 #[cfg(test)]
68 pub fn PQCLEAN_FALCON512_CLEAN_hash_to_point_rpo(
69 rc: *mut Rpo128Context,
70 x: *mut u16,
71 logn: usize,
72 );
73
74 #[cfg(test)]
75 pub fn rpo128_init(sc: *mut Rpo128Context);
76
77 #[cfg(test)]
78 pub fn rpo128_absorb(
79 sc: *mut Rpo128Context,
80 data: *const ::std::os::raw::c_void,
81 len: libc::size_t,
82 );
83
84 #[cfg(test)]
85 pub fn rpo128_finalize(sc: *mut Rpo128Context);
86}
87
88#[repr(C)]
89#[cfg(test)]
90pub struct Rpo128Context {
91 pub content: [u64; 13usize],
92}
93
94// TESTS
95// ================================================================================================
96
97#[cfg(all(test, feature = "std"))]
98mod tests {
99 use rand_utils::{rand_array, rand_value, rand_vector};
100
101 use super::*;
102 use crate::dsa::rpo_falcon512::{NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};
103
104 #[test]
105 fn falcon_ffi() {
106 unsafe {
107 //let mut rng = rand::thread_rng();
108
109 // --- generate a key pair from a seed ----------------------------
110
111 let mut pk = [0u8; PK_LEN];
112 let mut sk = [0u8; SK_LEN];
113 let seed: [u8; NONCE_LEN] = rand_array();
114
115 assert_eq!(
116 0,
117 PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_from_seed_rpo(
118 pk.as_mut_ptr(),
119 sk.as_mut_ptr(),
120 seed.as_ptr()
121 )
122 );
123
124 // --- sign a message and make sure it verifies -------------------
125
126 let mlen: usize = rand_value::<u16>() as usize;
127 let msg: Vec<u8> = rand_vector(mlen);
128 let mut detached_sig = [0u8; NONCE_LEN + SIG_LEN];
129 let mut siglen = 0;
130
131 assert_eq!(
132 0,
133 PQCLEAN_FALCON512_CLEAN_crypto_sign_signature_rpo(
134 detached_sig.as_mut_ptr(),
135 &mut siglen as *mut usize,
136 msg.as_ptr(),
137 msg.len(),
138 sk.as_ptr()
139 )
140 );
141
142 assert_eq!(
143 0,
144 PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
145 detached_sig.as_ptr(),
146 siglen,
147 msg.as_ptr(),
148 msg.len(),
149 pk.as_ptr()
150 )
151 );
152
153 // --- check verification of different signature ------------------
154
155 assert_eq!(
156 -1,
157 PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
158 detached_sig.as_ptr(),
159 siglen,
160 msg.as_ptr(),
161 msg.len() - 1,
162 pk.as_ptr()
163 )
164 );
165
166 // --- check verification against a different pub key -------------
167
168 let mut pk_alt = [0u8; PK_LEN];
169 let mut sk_alt = [0u8; SK_LEN];
170 assert_eq!(
171 0,
172 PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_rpo(
173 pk_alt.as_mut_ptr(),
174 sk_alt.as_mut_ptr()
175 )
176 );
177
178 assert_eq!(
179 -1,
180 PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
181 detached_sig.as_ptr(),
182 siglen,
183 msg.as_ptr(),
184 msg.len(),
185 pk_alt.as_ptr()
186 )
187 );
188 }
189 }
190}