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 super::*;
100 use crate::dsa::rpo_falcon512::{NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};
101 use rand_utils::{rand_array, rand_value, rand_vector};
102
103 #[test]
104 fn falcon_ffi() {
105 unsafe {
106 //let mut rng = rand::thread_rng();
107
108 // --- generate a key pair from a seed ----------------------------
109
110 let mut pk = [0u8; PK_LEN];
111 let mut sk = [0u8; SK_LEN];
112 let seed: [u8; NONCE_LEN] = rand_array();
113
114 assert_eq!(
115 0,
116 PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_from_seed_rpo(
117 pk.as_mut_ptr(),
118 sk.as_mut_ptr(),
119 seed.as_ptr()
120 )
121 );
122
123 // --- sign a message and make sure it verifies -------------------
124
125 let mlen: usize = rand_value::<u16>() as usize;
126 let msg: Vec<u8> = rand_vector(mlen);
127 let mut detached_sig = [0u8; NONCE_LEN + SIG_LEN];
128 let mut siglen = 0;
129
130 assert_eq!(
131 0,
132 PQCLEAN_FALCON512_CLEAN_crypto_sign_signature_rpo(
133 detached_sig.as_mut_ptr(),
134 &mut siglen as *mut usize,
135 msg.as_ptr(),
136 msg.len(),
137 sk.as_ptr()
138 )
139 );
140
141 assert_eq!(
142 0,
143 PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
144 detached_sig.as_ptr(),
145 siglen,
146 msg.as_ptr(),
147 msg.len(),
148 pk.as_ptr()
149 )
150 );
151
152 // --- check verification of different signature ------------------
153
154 assert_eq!(
155 -1,
156 PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
157 detached_sig.as_ptr(),
158 siglen,
159 msg.as_ptr(),
160 msg.len() - 1,
161 pk.as_ptr()
162 )
163 );
164
165 // --- check verification against a different pub key -------------
166
167 let mut pk_alt = [0u8; PK_LEN];
168 let mut sk_alt = [0u8; SK_LEN];
169 assert_eq!(
170 0,
171 PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_rpo(
172 pk_alt.as_mut_ptr(),
173 sk_alt.as_mut_ptr()
174 )
175 );
176
177 assert_eq!(
178 -1,
179 PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
180 detached_sig.as_ptr(),
181 siglen,
182 msg.as_ptr(),
183 msg.len(),
184 pk_alt.as_ptr()
185 )
186 );
187 }
188 }
189}