2 *****************************************************************************
4 AES-Based PRF for ADSNARK.
6 *****************************************************************************
7 * @author This file is part of libsnark, developed by SCIPR Lab
8 * and contributors (see AUTHORS).
9 * @copyright MIT license (see LICENSE file)
10 *****************************************************************************/
12 #include "depends/libsnark-supercop/include/crypto_core_aes128encrypt.h"
13 #include "depends/libsnark-supercop/include/randombytes.h"
16 #include <libsnark/common/default_types/r1cs_ppzkadsnark_pp.hpp>
21 template<> aesPrfKeyT prfGen<default_r1cs_ppzkadsnark_pp>()
24 randombytes(key.key_bytes, 32);
29 libff::Fr<snark_pp<default_r1cs_ppzkadsnark_pp>> prfCompute<
30 default_r1cs_ppzkadsnark_pp>(const aesPrfKeyT &key, const labelT &label)
32 unsigned char seed_bytes[16];
34 unsigned char random_bytes[16 * 3];
40 // compute random seed using AES as PRF
41 crypto_core_aes128encrypt_openssl(
42 seed_bytes, label.label_bytes, key.key_bytes, NULL);
44 // use first 128 bits of output to seed AES-CTR
45 // PRG to expand to 3*128 bits
46 crypto_core_aes128encrypt_openssl(
47 random_bytes, seed_bytes, key.key_bytes + 16, NULL);
49 mpz_import(aux, 16, 0, 1, 0, 0, seed_bytes);
50 mpz_add_ui(aux, aux, 1);
51 mpz_export(seed_bytes, &exp_len, 0, 1, 0, 0, aux);
53 seed_bytes[exp_len++] = 0;
55 crypto_core_aes128encrypt_openssl(
56 random_bytes + 16, seed_bytes, key.key_bytes + 16, NULL);
58 mpz_add_ui(aux, aux, 1);
59 mpz_export(seed_bytes, &exp_len, 0, 1, 0, 0, aux);
61 seed_bytes[exp_len++] = 0;
63 crypto_core_aes128encrypt_openssl(
64 random_bytes + 32, seed_bytes, key.key_bytes + 16, NULL);
66 // see output as integer and reduce modulo r
67 mpz_import(aux, 16 * 3, 0, 1, 0, 0, random_bytes);
68 libff::Fr<snark_pp<default_r1cs_ppzkadsnark_pp>>::mod.to_mpz(Fr_mod);
69 mpz_mod(aux, aux, Fr_mod);
71 return libff::Fr<snark_pp<default_r1cs_ppzkadsnark_pp>>(
73 libff::Fr<snark_pp<default_r1cs_ppzkadsnark_pp>>::num_limbs>(aux));
76 } // namespace libsnark