libcrux_mlkem768_portable.c (5422B)
1 /* 2 * SPDX-FileCopyrightText: 2025 Cryspen Sarl <info@cryspen.com> 3 * 4 * SPDX-License-Identifier: MIT or Apache-2.0 5 * 6 * This code was generated with the following revisions: 7 * Charon: 667d2fc98984ff7f3df989c2367e6c1fa4a000e7 8 * Eurydice: 2381cbc416ef2ad0b561c362c500bc84f36b6785 9 * Karamel: 80f5435f2fc505973c469a4afcc8d875cddd0d8b 10 * F*: 71d8221589d4d438af3706d89cb653cf53e18aab 11 * Libcrux: 68dfed5a4a9e40277f62828471c029afed1ecdcc 12 */ 13 14 #include "libcrux_mlkem768_portable.h" 15 16 #include "internal/libcrux_mlkem_portable.h" 17 #include "libcrux_core.h" 18 19 /** 20 Portable decapsulate 21 */ 22 /** 23 A monomorphic instance of 24 libcrux_ml_kem.ind_cca.instantiations.portable.decapsulate with const generics 25 - K= 3 26 - SECRET_KEY_SIZE= 2400 27 - CPA_SECRET_KEY_SIZE= 1152 28 - PUBLIC_KEY_SIZE= 1184 29 - CIPHERTEXT_SIZE= 1088 30 - T_AS_NTT_ENCODED_SIZE= 1152 31 - C1_SIZE= 960 32 - C2_SIZE= 128 33 - VECTOR_U_COMPRESSION_FACTOR= 10 34 - VECTOR_V_COMPRESSION_FACTOR= 4 35 - C1_BLOCK_SIZE= 320 36 - ETA1= 2 37 - ETA1_RANDOMNESS_SIZE= 128 38 - ETA2= 2 39 - ETA2_RANDOMNESS_SIZE= 128 40 - IMPLICIT_REJECTION_HASH_INPUT_SIZE= 1120 41 */ 42 static void 43 decapsulate_35( 44 libcrux_ml_kem_types_MlKemPrivateKey_d9 *private_key, 45 libcrux_ml_kem_mlkem768_MlKem768Ciphertext *ciphertext, uint8_t ret[32U]) 46 { 47 libcrux_ml_kem_ind_cca_decapsulate_62(private_key, ciphertext, ret); 48 } 49 50 /** 51 Decapsulate ML-KEM 768 52 53 Generates an [`MlKemSharedSecret`]. 54 The input is a reference to an [`MlKem768PrivateKey`] and an 55 [`MlKem768Ciphertext`]. 56 */ 57 void 58 libcrux_ml_kem_mlkem768_portable_decapsulate( 59 libcrux_ml_kem_types_MlKemPrivateKey_d9 *private_key, 60 libcrux_ml_kem_mlkem768_MlKem768Ciphertext *ciphertext, uint8_t ret[32U]) 61 { 62 decapsulate_35(private_key, ciphertext, ret); 63 } 64 65 /** 66 A monomorphic instance of 67 libcrux_ml_kem.ind_cca.instantiations.portable.encapsulate with const generics 68 - K= 3 69 - CIPHERTEXT_SIZE= 1088 70 - PUBLIC_KEY_SIZE= 1184 71 - T_AS_NTT_ENCODED_SIZE= 1152 72 - C1_SIZE= 960 73 - C2_SIZE= 128 74 - VECTOR_U_COMPRESSION_FACTOR= 10 75 - VECTOR_V_COMPRESSION_FACTOR= 4 76 - C1_BLOCK_SIZE= 320 77 - ETA1= 2 78 - ETA1_RANDOMNESS_SIZE= 128 79 - ETA2= 2 80 - ETA2_RANDOMNESS_SIZE= 128 81 */ 82 static tuple_c2 83 encapsulate_cd( 84 libcrux_ml_kem_types_MlKemPublicKey_30 *public_key, uint8_t *randomness) 85 { 86 return libcrux_ml_kem_ind_cca_encapsulate_ca(public_key, randomness); 87 } 88 89 /** 90 Encapsulate ML-KEM 768 91 92 Generates an ([`MlKem768Ciphertext`], [`MlKemSharedSecret`]) tuple. 93 The input is a reference to an [`MlKem768PublicKey`] and [`SHARED_SECRET_SIZE`] 94 bytes of `randomness`. 95 */ 96 tuple_c2 97 libcrux_ml_kem_mlkem768_portable_encapsulate( 98 libcrux_ml_kem_types_MlKemPublicKey_30 *public_key, 99 uint8_t randomness[32U]) 100 { 101 return encapsulate_cd(public_key, randomness); 102 } 103 104 /** 105 Portable generate key pair. 106 */ 107 /** 108 A monomorphic instance of 109 libcrux_ml_kem.ind_cca.instantiations.portable.generate_keypair with const 110 generics 111 - K= 3 112 - CPA_PRIVATE_KEY_SIZE= 1152 113 - PRIVATE_KEY_SIZE= 2400 114 - PUBLIC_KEY_SIZE= 1184 115 - ETA1= 2 116 - ETA1_RANDOMNESS_SIZE= 128 117 */ 118 static libcrux_ml_kem_mlkem768_MlKem768KeyPair 119 generate_keypair_ce( 120 uint8_t *randomness) 121 { 122 return libcrux_ml_kem_ind_cca_generate_keypair_15(randomness); 123 } 124 125 /** 126 Generate ML-KEM 768 Key Pair 127 */ 128 libcrux_ml_kem_mlkem768_MlKem768KeyPair 129 libcrux_ml_kem_mlkem768_portable_generate_key_pair(uint8_t randomness[64U]) 130 { 131 return generate_keypair_ce(randomness); 132 } 133 134 /** 135 Private key validation 136 */ 137 /** 138 A monomorphic instance of 139 libcrux_ml_kem.ind_cca.instantiations.portable.validate_private_key with const 140 generics 141 - K= 3 142 - SECRET_KEY_SIZE= 2400 143 - CIPHERTEXT_SIZE= 1088 144 */ 145 static KRML_MUSTINLINE bool 146 validate_private_key_31( 147 libcrux_ml_kem_types_MlKemPrivateKey_d9 *private_key, 148 libcrux_ml_kem_mlkem768_MlKem768Ciphertext *ciphertext) 149 { 150 return libcrux_ml_kem_ind_cca_validate_private_key_37(private_key, 151 ciphertext); 152 } 153 154 /** 155 Validate a private key. 156 157 Returns `true` if valid, and `false` otherwise. 158 */ 159 bool 160 libcrux_ml_kem_mlkem768_portable_validate_private_key( 161 libcrux_ml_kem_types_MlKemPrivateKey_d9 *private_key, 162 libcrux_ml_kem_mlkem768_MlKem768Ciphertext *ciphertext) 163 { 164 return validate_private_key_31(private_key, ciphertext); 165 } 166 167 /** 168 Private key validation 169 */ 170 /** 171 A monomorphic instance of 172 libcrux_ml_kem.ind_cca.instantiations.portable.validate_private_key_only with 173 const generics 174 - K= 3 175 - SECRET_KEY_SIZE= 2400 176 */ 177 static KRML_MUSTINLINE bool 178 validate_private_key_only_41( 179 libcrux_ml_kem_types_MlKemPrivateKey_d9 *private_key) 180 { 181 return libcrux_ml_kem_ind_cca_validate_private_key_only_d6(private_key); 182 } 183 184 /** 185 Validate the private key only. 186 187 Returns `true` if valid, and `false` otherwise. 188 */ 189 bool 190 libcrux_ml_kem_mlkem768_portable_validate_private_key_only( 191 libcrux_ml_kem_types_MlKemPrivateKey_d9 *private_key) 192 { 193 return validate_private_key_only_41(private_key); 194 } 195 196 /** 197 Public key validation 198 */ 199 /** 200 A monomorphic instance of 201 libcrux_ml_kem.ind_cca.instantiations.portable.validate_public_key with const 202 generics 203 - K= 3 204 - PUBLIC_KEY_SIZE= 1184 205 */ 206 static KRML_MUSTINLINE bool 207 validate_public_key_41(uint8_t *public_key) 208 { 209 return libcrux_ml_kem_ind_cca_validate_public_key_89(public_key); 210 } 211 212 /** 213 Validate a public key. 214 215 Returns `true` if valid, and `false` otherwise. 216 */ 217 bool 218 libcrux_ml_kem_mlkem768_portable_validate_public_key( 219 libcrux_ml_kem_types_MlKemPublicKey_30 *public_key) 220 { 221 return validate_public_key_41(public_key->value); 222 }