├── CMakeLists.txt ├── Kunlun ├── README.md ├── adcp │ └── adcp.hpp ├── config │ ├── config.h │ └── config.h.in ├── crypto │ ├── aes.hpp │ ├── bigint.hpp │ ├── block.hpp │ ├── curve25519.hpp │ ├── ec_group.hpp │ ├── ec_point.hpp │ ├── hash.hpp │ ├── otp.hpp │ ├── prg.hpp │ ├── prp.hpp │ └── setup.hpp ├── docs │ ├── crypto │ │ ├── aes.md │ │ ├── hash.md │ │ └── prg.md │ ├── filter │ │ └── bloom_filter.md │ ├── mpc │ │ ├── oprf │ │ │ └── mp_oprf.md │ │ ├── ot │ │ │ ├── iknp_ote.md │ │ │ └── naor_pinkas_ot.md │ │ ├── pso │ │ │ └── pso_from_mqrpmt.md │ │ └── rpmt │ │ │ └── cwprf_mqrpmt.md │ └── utility │ │ ├── routines.md │ │ └── serialization.md ├── filter │ ├── bloom_filter.hpp │ └── cuckoo_filter.hpp ├── gadget │ └── range_proof.hpp ├── include │ ├── global.hpp │ ├── openssl.inc │ └── std.inc ├── mpc │ ├── okvs │ │ ├── Baxos.hpp │ │ ├── CMakeLists.txt │ │ ├── Paxos.hpp │ │ ├── okvs.md │ │ ├── test_okvs.cpp │ │ └── utils.h │ ├── oprf │ │ ├── ddh_oprf.hpp │ │ └── ote_oprf.hpp │ ├── ot │ │ ├── alsz_ote.hpp │ │ ├── iknp_ote.hpp │ │ └── naor_pinkas_ot.hpp │ ├── peqt │ │ └── peqt_from_ddh.hpp │ ├── psi │ │ ├── cwprf_psi.hpp │ │ └── psi_from_oprf.hpp │ ├── pso │ │ ├── mqrpmt_private_id.hpp │ │ ├── mqrpmt_psi.hpp │ │ ├── mqrpmt_psi_card.hpp │ │ ├── mqrpmt_psi_card_sum.hpp │ │ └── mqrpmt_psu.hpp │ └── rpmt │ │ └── cwprf_mqrpmt.hpp ├── netio │ └── stream_channel.hpp ├── test │ ├── test_accountable_ring_sig.cpp │ ├── test_adcp.cpp │ ├── test_aes.cpp │ ├── test_alsz_ote.cpp │ ├── test_bloom_filter.cpp │ ├── test_bullet_proof.cpp │ ├── test_calculate_dlog.cpp │ ├── test_cuckoo_filter.cpp │ ├── test_cwprf_psi.cpp │ ├── test_elgamal.cpp │ ├── test_iknp_ote.cpp │ ├── test_innerproduct_proof.cpp │ ├── test_mcl.cpp │ ├── test_misc.cpp │ ├── test_mpoprf_based_psi.cpp │ ├── test_mqrpmt.cpp │ ├── test_mqrpmt_private_id.cpp │ ├── test_mqrpmt_psi.cpp │ ├── test_mqrpmt_psi_card.cpp │ ├── test_mqrpmt_psi_card_sum.cpp │ ├── test_mqrpmt_psu.cpp │ ├── test_naor_pinkas_ot.cpp │ ├── test_netio.cpp │ ├── test_nizk_dlog_equality.cpp │ ├── test_nizk_dlog_knowledge.cpp │ ├── test_nizk_enc_relation.cpp │ ├── test_nizk_plaintext_equality.cpp │ ├── test_nizk_plaintext_knowledge.cpp │ ├── test_oteoprf.cpp │ ├── test_peqt.cpp │ ├── test_range_proof.cpp │ ├── test_schnorr_sig.cpp │ ├── test_twisted_elgamal.cpp │ └── testcase │ │ └── bloom_test_case │ │ ├── random-list.txt │ │ ├── word-list-extra-large.txt │ │ ├── word-list-large.txt │ │ └── word-list.txt └── utility │ ├── bit_operation.hpp │ ├── murmurhash2.hpp │ ├── murmurhash3.hpp │ ├── polymul.hpp │ ├── print.hpp │ ├── routines.hpp │ └── serialization.hpp ├── LICENSE ├── README.md ├── cli ├── base_clp.h ├── common_utils.cpp ├── common_utils.h ├── csv_reader.cpp ├── csv_reader.h ├── pd_tool │ ├── CMakeLists.txt │ ├── clp.h │ └── pd_tool.cpp ├── receiver │ ├── CMakeLists.txt │ ├── clp.h │ ├── receiver.cpp │ ├── receiver_utils.cpp │ └── receiver_utils.h └── sender │ ├── CMakeLists.txt │ ├── clp.h │ └── sender.cpp ├── cmake ├── APSUConfig.cmake.in ├── APSUMacros.cmake ├── CompileSchemaCXX.cmake ├── DetectArch.cmake ├── EnableCXXCompilerFlags.cmake └── FindAVX.cmake ├── common └── apsu │ ├── CMakeLists.txt │ ├── apsu.h │ ├── config.h.in │ ├── crypto_context.h │ ├── fourq │ ├── CMakeLists.txt │ ├── FourQ.h │ ├── FourQ_api.h │ ├── FourQ_internal.h │ ├── FourQ_params.h │ ├── FourQ_tables.h │ ├── amd64 │ │ ├── CMakeLists.txt │ │ ├── consts.c │ │ ├── fp2_1271.S │ │ ├── fp2_1271_AVX2.S │ │ └── fp_x64.h │ ├── cgmanifest.json │ ├── crypto_util.c │ ├── eccp2.c │ ├── eccp2_core.c │ ├── eccp2_no_endo.c │ ├── generic │ │ ├── CMakeLists.txt │ │ └── fp.h │ ├── hash_to_curve.c │ ├── random.c │ ├── random.h │ └── table_lookup.h │ ├── item.cpp │ ├── item.h │ ├── log.cpp │ ├── log.h │ ├── network │ ├── CMakeLists.txt │ ├── channel.h │ ├── ciphertext.fbs │ ├── network_channel.h │ ├── plainresult.h │ ├── receiver_operation.cpp │ ├── receiver_operation.h │ ├── receiver_operation_response.cpp │ ├── receiver_operation_response.h │ ├── result_package.cpp │ ├── result_package.fbs │ ├── result_package.h │ ├── rop.fbs │ ├── rop_header.fbs │ ├── rop_response.fbs │ ├── stream_channel.cpp │ ├── stream_channel.h │ └── zmq │ │ ├── CMakeLists.txt │ │ ├── zmq_channel.cpp │ │ └── zmq_channel.h │ ├── oprf │ ├── CMakeLists.txt │ ├── ecpoint.cpp │ ├── ecpoint.h │ ├── oprf_common.h │ ├── oprf_receiver.cpp │ ├── oprf_receiver.h │ ├── oprf_sender.cpp │ └── oprf_sender.h │ ├── ot │ ├── CMakeLists.txt │ ├── kunlun_ot.cpp │ └── kunlun_ot.h │ ├── peqt │ ├── CMakeLists.txt │ ├── DDHPEQT.cpp │ ├── DDHPEQT.h │ ├── OSNPEQT.cpp │ └── OSNPEQT.h │ ├── permute │ ├── CMakeLists.txt │ ├── apsu_OSNReceiver.cpp │ ├── apsu_OSNReceiver.h │ ├── apsu_OSNSender.cpp │ ├── apsu_OSNSender.h │ ├── apsu_benes.cpp │ └── apsu_benes.h │ ├── powers.cpp │ ├── powers.h │ ├── psu_params.cpp │ ├── psu_params.fbs │ ├── psu_params.h │ ├── requests.h │ ├── responses.h │ ├── seal_object.h │ ├── thread_pool_mgr.cpp │ ├── thread_pool_mgr.h │ ├── util │ ├── CMakeLists.txt │ ├── db_encoding.cpp │ ├── db_encoding.h │ ├── interpolate.cpp │ ├── interpolate.h │ ├── label_encryptor.cpp │ ├── label_encryptor.h │ ├── stopwatch.cpp │ ├── stopwatch.h │ ├── thread_pool.h │ ├── utils.cpp │ └── utils.h │ ├── version.cpp │ └── version.h ├── parameters ├── 100K-1-16.json ├── 100K-1.json ├── 16M-1-32.json ├── 16M-1.json ├── 16M-1024.json ├── 16M-11041.json ├── 16M-2048.json ├── 16M-4096-32.json ├── 16M-4096.json ├── 16M-5535.json ├── 1M-1-32.json ├── 1M-1.json ├── 1M-1024-cmp.json ├── 1M-1024-com.json ├── 1M-11041.json ├── 1M-2048-cmp.json ├── 1M-2048-com.json ├── 1M-256-288.json ├── 1M-256.json ├── 1M-4096-32.json ├── 1M-4096-cmp.json ├── 1M-4096-com.json ├── 1M-512-cmp.json ├── 1M-512-com.json ├── 1M-5535.json ├── 256K-1.json ├── 256K-1024.json ├── 256K-2048-cmp.json ├── 256K-2048-com.json ├── 256K-4096-cmp.json ├── 256K-4096-com.json ├── 256K-512.json ├── 256M-1.json ├── 256M-1024.json ├── 256M-2048.json └── 256M-4096.json ├── receiver └── apsu │ ├── CMakeLists.txt │ ├── bin_bundle.cpp │ ├── bin_bundle.fbs │ ├── bin_bundle.h │ ├── query.cpp │ ├── query.h │ ├── receiver_db.cpp │ ├── receiver_db.fbs │ ├── receiver_db.h │ ├── receiver_ddh.cpp │ ├── receiver_ddh.h │ ├── receiver_osn.cpp │ ├── receiver_osn.h │ ├── util │ ├── CMakeLists.txt │ ├── cuckoo_filter.cpp │ ├── cuckoo_filter.h │ ├── cuckoo_filter_table.cpp │ ├── cuckoo_filter_table.h │ └── hash.h │ └── zmq │ ├── CMakeLists.txt │ ├── receiver_dispatcher_ddh.cpp │ ├── receiver_dispatcher_ddh.h │ ├── receiver_dispatcher_osn.cpp │ └── receiver_dispatcher_osn.h ├── sender └── apsu │ ├── CMakeLists.txt │ ├── itt.h │ ├── match_record.h │ ├── plaintext_powers.cpp │ ├── plaintext_powers.h │ ├── sender_ddh.cpp │ ├── sender_ddh.h │ ├── sender_osn.cpp │ ├── sender_osn.h │ ├── utils.cpp │ └── utils.h └── tools └── auto_test.py /Kunlun/config/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define NUMBER_OF_LOGICAL_CORES 16 4 | #define NUMBER_OF_PHYSICAL_CORES 8 5 | #define IS_64BIT 1 6 | #define HAS_SSE2 0 7 | 8 | /* #undef IS_LINUX */ 9 | #define IS_LINUX 1 10 | /* #undef IS_WINDOWS */ 11 | -------------------------------------------------------------------------------- /Kunlun/config/config.h.in: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define NUMBER_OF_LOGICAL_CORES @_NUMBER_OF_LOGICAL_CORES@ 4 | #define NUMBER_OF_PHYSICAL_CORES @_NUMBER_OF_PHYSICAL_CORES@ 5 | #define IS_64BIT @_IS_64BIT@ 6 | #define HAS_SSE2 @_HAS_SSE2@ 7 | 8 | #cmakedefine IS_LINUX @IS_LINUX@ 9 | #cmakedefine IS_MACOS @IS_MACOS@ 10 | #cmakedefine IS_WINDOWS @IS_WINDOWS@ -------------------------------------------------------------------------------- /Kunlun/crypto/ec_group.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CRYPTO_EC_GROUP_HPP_ 2 | #define CRYPTO_EC_GROUP_HPP_ 3 | 4 | #include "../include/global.hpp" 5 | #include "../utility/print.hpp" 6 | #include "bigint.hpp" 7 | 8 | // enable pre-computation for fixed point multiplication 9 | #define PRECOMPUTE_ENABLE 10 | 11 | /* 12 | ** enable point compression 13 | ** will save bandwidth by half at the cost of expensive decompression 14 | */ 15 | 16 | //#define USING_CURVE_25519 17 | static int curve_id = NID_X9_62_prime256v1; 18 | #define ECPOINT_COMPRESSED 19 | 20 | inline EC_GROUP *group; 21 | const inline EC_POINT *generator; 22 | 23 | inline BIGNUM *order; 24 | static BIGNUM *cofactor; // The cofactor of this group 25 | static BIGNUM *curve_params_p; 26 | static BIGNUM *curve_params_a; 27 | static BIGNUM *curve_params_b; 28 | static BIGNUM *curve_params_q; // q = (p-1)/2 29 | 30 | inline size_t POINT_BYTE_LEN; // the byte length of ec point 31 | inline size_t POINT_COMPRESSED_BYTE_LEN; // the byte length of ec point in compressed form 32 | 33 | static BN_CTX *ec_ctx; // define ctx for ecc operations 34 | 35 | 36 | inline void ECGroup_Initialize(){ 37 | group = EC_GROUP_new_by_curve_name(curve_id); 38 | // If this fails, this is usually due to an invalid curve id. 39 | CRYPTO_CHECK(group !=nullptr); 40 | 41 | generator = EC_GROUP_get0_generator(group); 42 | 43 | order = BN_new(); 44 | CRYPTO_CHECK(EC_GROUP_get_order(group, order, bn_ctx[0]) == 1); 45 | 46 | cofactor = BN_new(); 47 | CRYPTO_CHECK(EC_GROUP_get_cofactor(group, cofactor, bn_ctx[0]) == 1); 48 | 49 | curve_params_p = BN_new(); 50 | curve_params_a = BN_new(); 51 | curve_params_b = BN_new(); 52 | 53 | CRYPTO_CHECK(EC_GROUP_get_curve_GFp(group, curve_params_p, curve_params_a, curve_params_b, bn_ctx[0]) == 1); 54 | 55 | size_t rounds = 100; 56 | CRYPTO_CHECK(BN_is_prime_ex(curve_params_p, rounds, bn_ctx[0], nullptr) == 1); 57 | 58 | curve_params_q = BN_new(); 59 | BN_rshift(curve_params_q, curve_params_p, 1); // p_minus_one_over_two = (p-1)/2 60 | 61 | // PrintSplitLine('-'); 62 | // std::cout << "EC group info >>> " << std::endl; 63 | 64 | // std::cout << "a = " << BN_bn2hex(curve_params_a) << std::endl; 65 | // std::cout << "b = " << BN_bn2hex(curve_params_b) << std::endl; 66 | // std::cout << "p = " << BN_bn2hex(curve_params_p) << std::endl; 67 | // std::cout << "q = " << BN_bn2hex(curve_params_q) << std::endl; 68 | // PrintSplitLine('-'); 69 | 70 | BN_BYTE_LEN = BN_num_bits(curve_params_p)/8 + BN_num_bits(curve_params_p)%8; 71 | POINT_BYTE_LEN = BN_BYTE_LEN * 2 + 1; 72 | POINT_COMPRESSED_BYTE_LEN = BN_BYTE_LEN + 1; 73 | 74 | 75 | 76 | #ifdef PRECOMPUTE_ENABLE 77 | EC_GROUP_precompute_mult((EC_GROUP*) group, bn_ctx[0]); // pre-compute the table of g 78 | // check if precomputation have been done properly 79 | if(EC_GROUP_have_precompute_mult((EC_GROUP*) group) == 0){ 80 | std::cerr << "pre-computation is not done properly" << std::endl; 81 | }; 82 | #endif 83 | } 84 | 85 | inline void ECGroup_Finalize(){ 86 | EC_GROUP_free(group); 87 | 88 | BN_free(curve_params_p); 89 | BN_free(curve_params_a); 90 | BN_free(curve_params_b); 91 | BN_free(curve_params_q); 92 | 93 | BN_free(order); 94 | BN_free(cofactor); 95 | } 96 | 97 | #endif //_CRYPTO_EC_GROUP_HPP_ 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /Kunlun/crypto/otp.hpp: -------------------------------------------------------------------------------- 1 | #ifndef KUNLUN_OTP_HPP_ 2 | #define KUNLUN_OTP_HPP_ 3 | 4 | #include "prg.hpp" 5 | 6 | inline void XOR(uint8_t* A, uint8_t* B, uint8_t* C, size_t LEN) 7 | { 8 | #pragma omp parallel for num_threads(NUMBER_OF_THREADS) 9 | for(auto i = 0; i < LEN; i++){ 10 | C[i] = A[i]^B[i]; 11 | } 12 | } 13 | 14 | inline std::string XOR(std::string &str_A, std::string &str_B) 15 | { 16 | if(str_A.size() != str_B.size()){ 17 | std::cerr << "size does not match" << std::endl; 18 | } 19 | size_t LEN = str_A.size(); 20 | std::string str_C(LEN, '0'); 21 | XOR((uint8_t*)&str_A[0], (uint8_t*)&str_B[0], (uint8_t*)&str_C[0], LEN); 22 | return str_C; 23 | } 24 | 25 | inline std::vector XOR(std::vector &vec_A, std::vector &vec_B) 26 | { 27 | if(vec_A.size()!=vec_B.size()){ 28 | std::cerr << "size does not match" << std::endl; 29 | } 30 | size_t LEN = vec_A.size(); 31 | std::vector vec_C(LEN); 32 | XOR((uint8_t*)&vec_A[0], &vec_B[0], &vec_C[0], LEN); 33 | return vec_C; 34 | } 35 | 36 | 37 | namespace OTP{ 38 | 39 | inline std::vector Enc(block &key, std::vector& plaintext) 40 | { 41 | size_t LEN = plaintext.size(); 42 | PRG::Seed seed = PRG::SetSeed(&key, 0); 43 | std::vector one_time_pad = PRG::GenRandomBytes(seed, LEN); 44 | std::vector ciphertext = XOR(one_time_pad, plaintext); 45 | return ciphertext; 46 | } 47 | 48 | inline std::vector Dec(block &key, std::vector& ciphertext) 49 | { 50 | size_t LEN = ciphertext.size(); 51 | PRG::Seed seed = PRG::SetSeed(&key, 0); 52 | std::vector one_time_pad = PRG::GenRandomBytes(seed, LEN); 53 | std::vector plaintext = XOR(one_time_pad, ciphertext); 54 | return plaintext; 55 | } 56 | 57 | inline std::string Enc(block &key, std::string& str_plaintext) 58 | { 59 | size_t LEN = str_plaintext.size(); 60 | std::vector plaintext(LEN); 61 | memcpy(&plaintext[0], str_plaintext.data(), LEN); 62 | std::vector ciphertext = Enc(key, plaintext); 63 | std::string str_ciphertext(LEN, '0'); 64 | memcpy(&str_ciphertext[0], ciphertext.data(), LEN); 65 | return str_ciphertext; 66 | } 67 | 68 | inline std::string Dec(block &key, std::string& str_ciphertext) 69 | { 70 | size_t LEN = str_ciphertext.size(); 71 | std::vector ciphertext(LEN); 72 | memcpy(&ciphertext[0], str_ciphertext.data(), LEN); 73 | std::vector plaintext = Enc(key, ciphertext); 74 | std::string str_plaintext(LEN, '0'); 75 | memcpy(&str_plaintext[0], plaintext.data(), LEN); 76 | return str_plaintext; 77 | } 78 | 79 | } 80 | 81 | 82 | 83 | 84 | #endif// PRP_H__ 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /Kunlun/crypto/prp.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** Modified from the following project 3 | ** 1. https://github.com/emp-toolkit/ 4 | */ 5 | 6 | #ifndef KUNLUN_PRP_HPP_ 7 | #define KUNLUN_PRP_HPP_ 8 | 9 | #include "aes.hpp" 10 | #include "constants.h" 11 | 12 | 13 | /* 14 | * When the key is public, we usually need to model AES with this public key as a random permutation. 15 | * [REF] "Efficient Garbling from a Fixed-Key Blockcipher" https://eprint.iacr.org/2013/426.pdf 16 | */ 17 | 18 | namespace PRP{ 19 | 20 | void AES::Key GenKey(const block& salt) 21 | { 22 | return AES::GenEncKey(salt); 23 | } 24 | 25 | // key plays the role of enc key 26 | block Evaluate(AES::Key &key, const block &data) { 27 | return AES::ECBEnc(enc_key, data); 28 | } 29 | 30 | block Inverse(AES::Key &key, const block &data) { 31 | AES::dec_key = AES::DeriveDecKeyFromEncKey(key); 32 | return AES::ECBDec(dec_key, data); 33 | } 34 | 35 | } 36 | #endif -------------------------------------------------------------------------------- /Kunlun/crypto/setup.hpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this hpp file define and initialize misc global variables 3 | ***************************************************************************** 4 | * @author developed by Yu Chen 5 | * @copyright MIT license (see LICENSE file) 6 | *****************************************************************************/ 7 | #ifndef KUNLUN_SETUP_HPP_ 8 | #define KUNLUN_SETUP_HPP_ 9 | 10 | #include "../include/std.inc" 11 | #include "../include/openssl.inc" 12 | #include "../utility/print.hpp" 13 | 14 | #include "bigint.hpp" 15 | #include "ec_group.hpp" 16 | #include "hash.hpp" 17 | #include "prg.hpp" 18 | #include "block.hpp" 19 | #include "aes.hpp" 20 | 21 | inline void CRYPTO_Initialize() 22 | { 23 | BN_Initialize(); 24 | 25 | AES_Initialize(); 26 | #ifndef USING_CURVE_25519 27 | ECGroup_Initialize(); 28 | #endif 29 | 30 | PrintSplitLine('-'); 31 | std::cout << "GLOBAL ENVIROMENT INFO >>>" << std::endl; 32 | std::cout << "THREAD NUM = " << NUMBER_OF_THREADS << std::endl; 33 | 34 | #ifndef USING_CURVE_25519 35 | std::cout << "EC Curve ID = " << curve_id << std::endl; 36 | std::cout << "ECPoint COMPRESSION = "; 37 | #ifdef ECPOINT_COMPRESSED 38 | std::cout << "ON" << std::endl; 39 | #else 40 | std::cout << "OFF" << std::endl; 41 | #endif 42 | #else 43 | std::cout << "EC Curve ID = 25519" << std::endl; 44 | #endif 45 | PrintSplitLine('-'); 46 | } 47 | 48 | inline void CRYPTO_Finalize() 49 | { 50 | BN_Finalize(); 51 | #ifndef USING_CURVE_25519 52 | ECGroup_Finalize(); 53 | #endif 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /Kunlun/docs/crypto/hash.md: -------------------------------------------------------------------------------- 1 | # Hash 2 | This file implements basic hash based on SM3 and AES CBC encryption, and provides interfaces with different input data type. 3 | 4 | ## Use 5 | ### Basic Hash 6 | Adaptor for SM3: default output length is 256 bit. 7 | ``` 8 | void SM3(const unsigned char *input, size_t HASH_INPUT_LEN, unsigned char *output); 9 | ``` 10 | * `const unsigned char *input`: a pointer of `char` points to the input hashing data. 11 | * `size_t HASH_INPUT_LEN`: the byte length of the input hashing data. 12 | * `unsigned char *output`: a pointer of `char` points to the output hashing data. 13 | 14 | Adaptor for CBC-AES hash: default output length is 128 bit. 15 | ``` 16 | void CBCAES(const unsigned char *input, size_t HASH_INPUT_LEN, unsigned char *output); 17 | ``` 18 | * `const unsigned char *input`: a pointer of `char` points to the input hashing data. 19 | * `size_t HASH_INPUT_LEN`: the byte length of the input hashing data. 20 | * `unsigned char *output`: a pointer of `char` points to the output hashing data. 21 | 22 | ### Hash String to Other 23 | Hash a `string` to a `block`. 24 | ``` 25 | block StringToBlock(const std::string &str_input); 26 | ``` 27 | 28 | Hash a `string` to a `BigInt`. 29 | ``` 30 | BigInt StringToBigInt(const std::string& str_input); 31 | ``` 32 | 33 | Hash a `string` to an `ECPoint`. 34 | ``` 35 | ECPoint StringToECPoint(const std::string& input); 36 | ``` 37 | 38 | ### Hash ECPoint to Other 39 | Hash an `ECPoint` to a `block`. 40 | ``` 41 | block ECPointToBlock(const ECPoint &A); 42 | ``` 43 | 44 | Hash an `ECPoint` to a `string`. 45 | ``` 46 | std::string ECPointToString(const ECPoint &A); 47 | ``` 48 | 49 | ### Hash Block to Other 50 | Hash a vector of `block`s to a `block`. 51 | ``` 52 | block BlocksToBlock(const std::vector &input_block); 53 | ``` 54 | 55 | Hash a `block` to an `ECPoint`. Note that if you enable the multi-thread programing, you should call `ThreadSafeBlockToECPoint` to ensure correctness. 56 | ``` 57 | inline ECPoint BlockToECPoint(const block &var); 58 | 59 | inline ECPoint ThreadSafeBlockToECPoint(const block &var); 60 | ``` 61 | -------------------------------------------------------------------------------- /Kunlun/docs/crypto/prg.md: -------------------------------------------------------------------------------- 1 | # Pseudorandom Generator 2 | This file implements `PRG` using [`AES`](./aes.md). 3 | 4 | 5 | ## Construction 6 | All identifiers are defined in namespace `PRG`. 7 | 8 | ``` 9 | struct Seed{ 10 | size_t counter = 0; 11 | AES::Key aes_key; 12 | }; 13 | ``` 14 | * `size_t counter`: records the state of `PRG`. 15 | * `AES::Key`: a struct at namespace [`AES`](./aes.md), which depics the AES key. 16 | 17 | ### SetSeed 18 | Initialize the PRG seed. 19 | ``` 20 | Seed SetSeed(const void* salt = nullptr, uint64_t id = 0); 21 | ``` 22 | * `const void* salt`: the random data needed to initialize the PRG seed. It can be assigned by the caller or generated in the function by default. 23 | * `uint64_t id`: the identidier of `PRG` in use. Default to 0 if unset. 24 | 25 | ### ReSeed 26 | Reset the PRG seed using the random data assigned by the caller. 27 | ``` 28 | void ReSeed(Seed &seed, const block* salt, uint64_t id = 0); 29 | ``` 30 | * `Seed &seed`: the seed needs to be reseted. 31 | * `const block* salt`: a random block assigned by the caller to set the PRG seed. 32 | * `uint64_t id`: the identidier of `PRG` in use. Default to 0 if unset. 33 | 34 | 35 | ## Use 36 | ### GenRandomBlocks 37 | Generate a random block vector. 38 | ``` 39 | std::vector GenRandomBlocks(Seed &seed, size_t LEN); 40 | ``` 41 | * `Seed &seed`: the PRG seed. 42 | * `size_t LEN`: the length of the generated vector. 43 | 44 | ### GenRandomBytes 45 | Generate a random byte vector. 46 | ``` 47 | std::vector GenRandomBytes(Seed &seed, size_t LEN); 48 | ``` 49 | * `Seed &seed`: the PRG seed. 50 | * `size_t LEN`: the length of the generated vector. 51 | 52 | ### GenRandomBits 53 | Generate a random bool vector: each byte represents a bit in a sparse way. 54 | ``` 55 | std::vector GenRandomBits(Seed &seed, size_t LEN); 56 | ``` 57 | * `Seed &seed`: the PRG seed. 58 | * `size_t LEN`: the length of the generated vector. 59 | 60 | ### GenRandomBitMatrix 61 | Generate a random bit matrix, which is stored in column vector. 62 | ``` 63 | std::vector GenRandomBitMatrix(Seed &seed, size_t ROW_NUM, size_t COLUMN_NUM); 64 | ``` 65 | * `Seed &seed`: the PRG seed. 66 | * `size_t ROW_NUM`: the number of rows in the matrix. 67 | * `size_t COLUMN_NUM`: the number of columns in the matrix. 68 | 69 | ### CompareBits 70 | `CompareBits` checks if two bit vectors are equal, returns true if they are equal, returns false if they are not. 71 | ``` 72 | bool CompareBits(std::vector& vec_A, std::vector& vec_B); 73 | ``` 74 | * `std::vector& vec_A` and `std::vector& vec_B`: the bit vector needs to be compared. 75 | -------------------------------------------------------------------------------- /Kunlun/docs/mpc/ot/naor_pinkas_ot.md: -------------------------------------------------------------------------------- 1 | # Naor-Pinkas Oblivious Transform 2 | `NPOT` is an implementation of [Efficient Oblivious Transfer Protocols](https://dl.acm.org/doi/10.5555/365411.365502). It also plays the role as base OT in [`IKNPOTE`](./iknp_ote.md). 1-out-of-2 OT is a two-party protocol, where sender takes as input two strings $\{m_0, m_1\}$, and receiver takes as input a random bit $b$ and obtains nothing other than $m_b$ while sender learns nothing about $b$. 3 | 4 | 5 | ## Construction 6 | All identifiers are defined in namespace `NPOT`. 7 | 8 | ### Public Parameters 9 | ``` 10 | struct PP 11 | { 12 | ECPoint g; 13 | }; 14 | ``` 15 | The `PP` struct holds the public parameter of NPOT protocol, which is a generator group `g`. It can be initialized by `Setup()`. 16 | ``` 17 | PP Setup(); 18 | ``` 19 | 20 | 21 | ## Use 22 | ### Serialization 23 | ``` 24 | void SerializePP(PP &pp, std::ofstream &fout); 25 | void SavePP(PP &pp, std::string pp_filename); 26 | ``` 27 | The struct `PP` can be serialized and saved to file named `pp_filename`. `SavePP` will call `SerializePP` internally. 28 | ``` 29 | void DeserializePP(PP &pp, std::ifstream &fin); 30 | void FetchPP(PP &pp, std::string pp_filename); 31 | ``` 32 | Similarly, `FetchPP` will call `DeserializePP` to fetch serialized `PP` from file named `pp_filename`. 33 | 34 | ### Instantiate 35 | If you want to instantiate a number of OTs, all you need to do is start two processes, one process act as sender and call `Send`, the other process act as receiver and call `Receive`. 36 | ``` 37 | void Send(NetIO &io, PP &pp, const std::vector& vec_m0, const std::vector &vec_m1, size_t LEN); 38 | ``` 39 | * `NetIO &io`: a class object handling communication through socket. Sender is deemed as "server". 40 | * `PP &pp`: a public parameter struct of `NPOT`. 41 | * `std::vector& vec_m0`: a vector of messages. 42 | * `std::vector &vec_m1`: another vector of messages. 43 | * `size_t LEN`: the number of OT instances, that is, the length of vector `vec_m0` and `vec_m1`. 44 | 45 | ``` 46 | std::vector Receive(NetIO &io, PP &pp, const std::vector &vec_selection_bit, size_t LEN); 47 | ``` 48 | * `NetIO &io`: a class object handling communication through socket. Receiver is deemed as "client". 49 | * `PP &pp`: a public parameter struct of `NPOT`. 50 | * `std::vector &vec_selection_bit`: a vector of choice bits. Receiver will choose from `vec_m0` or `vec_m1` according to its choice bits. 51 | * `size_t LEN`: the number of OT instances, that is, the length of vector `vec_selection_bit`. The caller must guarantee that it is the same as input `size_t LEN` of `Send`. 52 | 53 | The messages receiver chooses is returned from `Receive` in `std::vector` proto. 54 | 55 | 56 | ## Sample Code 57 | An example of how to instantiate 128 OTs. More detailed sample code is provided in test files. 58 | ``` 59 | PRG::Seed seed = PRG::SetSeed(nullptr, 0); // initialize PRG seed 60 | NPOT::PP pp = NPOT::Setup(); 61 | size_t NUM = 128; // set instance size 62 | 63 | std::vector vec_selection_bit = GenRandomBits(seed, NUM); 64 | std::vector vec_K0 = PRG::GenRandomBlocks(seed, NUM); 65 | std::vector vec_K1 = PRG::GenRandomBlocks(seed, NUM); 66 | 67 | if (party == "sender") 68 | { 69 | NetIO sender_io("server", "", 8080); 70 | NPOT::Send(sender_io, pp, vec_K0, vec_K1, NUM); 71 | } 72 | 73 | if (party == "receiver") 74 | { 75 | NetIO receiver_io("client", "127.0.0.1", 8080); 76 | std::vector vec_K = NPOT::Receive(receiver_io, pp, vec_selection_bit, NUM); 77 | } 78 | ``` -------------------------------------------------------------------------------- /Kunlun/docs/utility/routines.md: -------------------------------------------------------------------------------- 1 | # Routine Algorithms 2 | This file implements some routine algorithms. 3 | 4 | 5 | ## Utility Functions 6 | ### FileExist 7 | `FileExist` checks whether a file named `filename` exists, returns true if it exists, returns false if not. 8 | ``` 9 | inline bool FileExist(const std::string& filename); 10 | ``` 11 | 12 | ### FormatToHexString 13 | `FormatToHexString` formats the input `byte_str` which is an octet string to a hexadecimal string. 14 | ``` 15 | std::string FormatToHexString(std::string byte_str); 16 | ``` 17 | 18 | ### IsPowerOfTwo 19 | `IsPowerOfTwo` checks whether there exists an integer $n$ satisfies $x = 2^n$ for $x > 0$, returns true if integer $n$ exists, returns false if not. 20 | ``` 21 | bool IsPowerOfTwo(size_t x); 22 | ``` 23 | 24 | ### GenRandomIntegerVectorLessThan 25 | `GenRandomIntegerVectorLessThan` returns a random integer vector. The vector generated in this way does not require cryptographic security. 26 | ``` 27 | std::vector GenRandomIntegerVectorLessThan(size_t LEN, int64_t MAX); 28 | ``` 29 | * `size_t LEN`: the length of the generated integer vector. 30 | * `int64_t MAX`: the range of each value in the generated vector is limited to [0,...,MAX]. 31 | -------------------------------------------------------------------------------- /Kunlun/docs/utility/serialization.md: -------------------------------------------------------------------------------- 1 | # Serialization 2 | 3 | ## Operator Overloading 4 | We provide serialization in many places, so we need to overload operator `>>` and `<<` to deal with different input/output stream objects, such as `ElementType` including any C++ POD type, and `std::vector`. 5 | ``` 6 | template std::ofstream &operator<<(std::ofstream &fout, const ElementType& element); 7 | template std::ifstream &operator>>(std::ifstream &fin, ElementType& element); 8 | 9 | template 10 | std::ofstream &operator<<(std::ofstream &fout, const std::vector& vec_element); 11 | template 12 | std::ifstream &operator>>(std::ifstream &fin, std::vector& vec_element); 13 | 14 | template < > std::ofstream &operator<<(std::ofstream &fout, const std::string& str); 15 | template < > std::ifstream &operator>>(std::ifstream &fin, std::string& str); 16 | ``` 17 | 18 | Note: if the input/output stream object is string, it will call the specialized template function. -------------------------------------------------------------------------------- /Kunlun/include/global.hpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this hpp file define global variables for the Kunlun lib 3 | NUMBER_OF_THREADS indicates the maximum number of threads that openmp works 4 | ***************************************************************************** 5 | * @author developed by Yu Chen 6 | * @copyright MIT license (see LICENSE file) 7 | *****************************************************************************/ 8 | #ifndef KUNLUN_GLOBAL_HPP_ 9 | #define KUNLUN_GLOBAL_HPP_ 10 | 11 | #include "openssl.inc" 12 | #include "std.inc" 13 | #include "../config/config.h" 14 | 15 | inline std::random_device rd; 16 | inline std::mt19937 global_built_in_prg(rd()); 17 | 18 | /* 19 | * default setting: set the maximum thread num as num of physical cores 20 | * you can switch to **N** thread setting by assign NUMBER_OF_THREADS = N by hand 21 | */ 22 | // inline const size_t NUMBER_OF_THREADS = NUMBER_OF_PHYSICAL_CORES; 23 | inline const size_t NUMBER_OF_THREADS = 4; 24 | inline const size_t CHECK_BUFFER_SIZE = 1024*8; 25 | 26 | // return the error message reported by OpenSSL 27 | inline void CRYPTO_CHECK(bool condition) 28 | { 29 | if (condition == false){ 30 | char buffer[256]; 31 | ERR_error_string_n(ERR_get_error(), buffer, sizeof(buffer)); 32 | std::cerr << std::string(buffer); 33 | } 34 | } 35 | 36 | inline void LoadErrorStrings() 37 | { 38 | ERR_load_BN_strings(); 39 | ERR_load_BUF_strings(); 40 | ERR_load_CRYPTO_strings(); 41 | ERR_load_EC_strings(); 42 | ERR_load_ERR_strings(); 43 | ERR_load_EVP_strings(); 44 | ERR_load_RAND_strings(); 45 | } 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Kunlun/include/openssl.inc: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this file includes all the OpenSSL includes we use 3 | ***************************************************************************** 4 | * @author developed by Yu Chen 5 | * @copyright MIT license (see LICENSE file) 6 | *****************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include -------------------------------------------------------------------------------- /Kunlun/include/std.inc: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this file includes commonly used head files 3 | ***************************************************************************** 4 | * @author developed by Yu Chen 5 | * @copyright MIT license (see LICENSE file) 6 | *****************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | -------------------------------------------------------------------------------- /Kunlun/mpc/okvs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Specify the minimum version for CMake 2 | CMAKE_MINIMUM_REQUIRED(VERSION 3.2) 3 | 4 | # Project's name 5 | PROJECT(Kunlun) 6 | 7 | FIND_PACKAGE(OpenSSL REQUIRED) 8 | FIND_PACKAGE(OpenMP REQUIRED) 9 | 10 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fpic -flto -Wno-deprecated-declarations -Wno-unused-result -Xpreprocessor -O3 -DNDEBUG -Wall") 11 | # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mpclmul -msse2 -std=c++14 -mavx -mavx2 -g -DNDEBUG -fpic -flto -Wno-deprecated-declarations -Wno-unused-result -Xpreprocessor -march=native -Wall -Wfatal-errors") 12 | # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -g ") 13 | 14 | INCLUDE_DIRECTORIES( 15 | ${OPENSSL_INCLUDE_DIR} 16 | "/usr/local/include" 17 | ) 18 | 19 | LINK_DIRECTORIES( 20 | /usr/local/lib 21 | ) 22 | 23 | SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build) 24 | 25 | 26 | 27 | ADD_EXECUTABLE(test_okvs ./test_okvs.cpp) 28 | 29 | if(OpenMP_CXX_FOUND) 30 | target_compile_options(test_okvs PUBLIC ${OpenMP_CXX_FLAGS}) 31 | endif() 32 | target_compile_options(test_okvs PUBLIC $<$:-std=c++14> -lpthread) 33 | 34 | TARGET_LINK_LIBRARIES(test_okvs ${OPENSSL_LIBRARIES} OpenMP::OpenMP_CXX) 35 | 36 | -------------------------------------------------------------------------------- /Kunlun/mpc/okvs/test_okvs.cpp: -------------------------------------------------------------------------------- 1 | #include "./Baxos.hpp" 2 | #include "./Paxos.hpp" 3 | #include "../crypto/setup.hpp" 4 | 5 | struct OKVSTestcase{ 6 | size_t ITEM_NUM; 7 | size_t BIN_SIZE; 8 | size_t thread_num; 9 | PRG::Seed seed; 10 | std::vector vec_value; 11 | std::vector vec_key; 12 | 13 | }; 14 | 15 | OKVSTestcase GenTestCase(size_t ITEM_NUM) 16 | { 17 | OKVSTestcase testcase; 18 | testcase.ITEM_NUM = ITEM_NUM; 19 | testcase.BIN_SIZE = size_t(pow(2, 15)); 20 | testcase.thread_num = 4; 21 | PRG::Seed seed = PRG::SetSeed(fixed_seed, 0); 22 | testcase.vec_value = PRG::GenRandomBlocks(seed, ITEM_NUM); 23 | testcase.vec_key = PRG::GenRandomBlocks(seed, ITEM_NUM); 24 | 25 | return testcase; 26 | } 27 | 28 | void SaveTestCase(OKVSTestcase &testcase, std::string testcase_filename) 29 | { 30 | std::ofstream fout; 31 | fout.open(testcase_filename, std::ios::binary); 32 | if(!fout) 33 | { 34 | std::cerr << testcase_filename << " open error" << std::endl; 35 | exit(1); 36 | } 37 | fout << testcase.ITEM_NUM; 38 | fout << testcase.BIN_SIZE; 39 | fout << testcase.thread_num; 40 | 41 | fout << testcase.vec_value; 42 | fout << testcase.vec_key; 43 | 44 | fout.close(); 45 | } 46 | 47 | void FetchTestCase(OKVSTestcase &testcase, std::string testcase_filename) 48 | { 49 | std::ifstream fin; 50 | fin.open(testcase_filename, std::ios::binary); 51 | if(!fin) 52 | { 53 | std::cerr << testcase_filename << " open error" << std::endl; 54 | exit(1); 55 | } 56 | fin >> testcase.ITEM_NUM; 57 | fin >> testcase.BIN_SIZE; 58 | fin >> testcase.thread_num; 59 | testcase.vec_value.resize(testcase.ITEM_NUM); 60 | testcase.vec_key.resize(testcase.ITEM_NUM); 61 | fin >> testcase.vec_value; 62 | fin >> testcase.vec_key; 63 | 64 | fin.close(); 65 | } 66 | 67 | int main() 68 | { 69 | 70 | CRYPTO_Initialize(); 71 | 72 | PrintSplitLine('-'); 73 | std::cout << "OKVS test begins >>>" << std::endl; 74 | PrintSplitLine('-'); 75 | std::cout << "generate or load public parameters and test case" << std::endl; 76 | 77 | // set instance size 78 | size_t ITEM_NUM = size_t(pow(2, 20)); 79 | std::cout << "ITEM_NUM of OKVS = " << ITEM_NUM << std::endl; 80 | 81 | // generate or fetch test case 82 | std::string testcase_filename = "okvs.testcase"; 83 | OKVSTestcase testcase; 84 | if(!FileExist(testcase_filename)){ 85 | testcase = GenTestCase(ITEM_NUM); 86 | SaveTestCase(testcase, testcase_filename); 87 | } 88 | else{ 89 | FetchTestCase(testcase, testcase_filename); 90 | } 91 | PRG::Seed seed = PRG::SetSeed(fixed_seed, 0); 92 | Baxos baxos(testcase.ITEM_NUM, testcase.BIN_SIZE, 3); 93 | std::vector encode_result(baxos.bin_num * baxos.total_size); 94 | std::vector decode_result(testcase.ITEM_NUM); 95 | baxos.solve(testcase.vec_key, testcase.vec_value, encode_result, &seed, testcase.thread_num); 96 | 97 | baxos.decode(testcase.vec_key, decode_result, encode_result, testcase.thread_num); 98 | 99 | for (auto i = 0; i < testcase.vec_value.size(); i++) 100 | { 101 | if (!Block::Compare(decode_result[i], testcase.vec_value[i])) 102 | { 103 | std::cout << "OKVS test fails" << std::endl; 104 | } 105 | } 106 | 107 | std::cout << "OKVS test succeeds" << std::endl; 108 | 109 | 110 | PrintSplitLine('-'); 111 | std::cout << "OKVS test ends >>>" << std::endl; 112 | PrintSplitLine('-'); 113 | 114 | CRYPTO_Finalize(); 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /Kunlun/test/test_accountable_ring_sig.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../signature/accountable_ring_sig.hpp" 4 | #include "../crypto/setup.hpp" 5 | 6 | 7 | void test_accountable_ring_sig() 8 | { 9 | PrintSplitLine('-'); 10 | std::cout << "begin the test of accoutable ring signature >>>" << std::endl; 11 | 12 | AccountableRingSig::PP pp; 13 | AccountableRingSig::SP sp; 14 | size_t N_max = 32; 15 | std::tie(pp, sp) = AccountableRingSig::Setup(N_max); 16 | 17 | size_t N = 8; // ring size 18 | std::vector vk_ring(N); 19 | std::vector sk_ring(N); 20 | for(auto i = 0; i < N; i++){ 21 | std::tie(vk_ring[i], sk_ring[i]) = AccountableRingSig::KeyGen(pp); 22 | } 23 | srand(time(0)); 24 | size_t index = rand() % N; 25 | 26 | std::string message = "I am a hacker"; 27 | AccountableRingSig::Signature sigma; 28 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 29 | sigma = AccountableRingSig::Sign(pp, sk_ring[index], vk_ring, message); 30 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 31 | auto running_time = end_time - start_time; 32 | std::cout << "size-" << N << " acountable ring signature generation takes time = " 33 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 34 | 35 | start_time = std::chrono::steady_clock::now(); // start to count the time 36 | AccountableRingSig::Verify(pp, vk_ring, message, sigma); 37 | end_time = std::chrono::steady_clock::now(); // end to count the time 38 | running_time = end_time - start_time; 39 | std::cout << "size-" << N << " acountable ring signature verification takes time = " 40 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 41 | 42 | start_time = std::chrono::steady_clock::now(); // start to count the time 43 | DLOGEquality::Proof correct_decryption_proof; 44 | ECPoint vk; 45 | std::tie(vk, correct_decryption_proof) = AccountableRingSig::Open(pp, sp, vk_ring, sigma); 46 | end_time = std::chrono::steady_clock::now(); // end to count the time 47 | running_time = end_time - start_time; 48 | std::cout << "size-" << N << " acountable ring signature open takes time = " 49 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 50 | 51 | start_time = std::chrono::steady_clock::now(); // start to count the time 52 | AccountableRingSig::Justify(pp, vk_ring, sigma, vk, correct_decryption_proof); 53 | end_time = std::chrono::steady_clock::now(); // end to count the time 54 | running_time = end_time - start_time; 55 | std::cout << "size-" << N << " acountable ring signature justify takes time = " 56 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 57 | 58 | } 59 | 60 | int main() 61 | { 62 | CRYPTO_Initialize(); 63 | 64 | test_accountable_ring_sig(); 65 | 66 | CRYPTO_Finalize(); 67 | 68 | return 0; 69 | } 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Kunlun/test/test_aes.cpp: -------------------------------------------------------------------------------- 1 | #include "../crypto/aes.hpp" 2 | #include "../crypto/setup.hpp" 3 | 4 | int main() 5 | { 6 | CRYPTO_Initialize(); 7 | 8 | std::ios::sync_with_stdio(false); 9 | 10 | PrintSplitLine('-'); 11 | std::cout << "AES test begins >>>>>>" << std::endl; 12 | 13 | ECPoint A = ECPoint(generator); 14 | 15 | unsigned char buffer[POINT_BYTE_LEN]; 16 | EC_POINT_point2oct(group, A.point_ptr, POINT_CONVERSION_UNCOMPRESSED, buffer, POINT_BYTE_LEN, nullptr); 17 | block data[4]; 18 | data[0] = _mm_loadu_si128(((block *)(&buffer[0 ]))); 19 | data[1] = _mm_loadu_si128(((block *)(&buffer[16]))); 20 | data[2] = _mm_loadu_si128(((block *)(&buffer[32]))); 21 | data[3] = _mm_loadu_si128(((block *)(&buffer[48]))); 22 | PrintSplitLine('-'); 23 | std::cout << "plaintext ==" << std::endl; 24 | Block::PrintBlocks(data, 4); 25 | PrintSplitLine('-'); 26 | 27 | std::cout << "after encryption ==" << std::endl; 28 | AES::CBCEnc(AES::fixed_enc_key, data, 4); 29 | Block::PrintBlocks(data, 4); 30 | PrintSplitLine('-'); 31 | 32 | std::cout << "after decryption ==" << std::endl; 33 | AES::CBCDec(AES::fixed_dec_key, data, 4); 34 | Block::PrintBlocks(data, 4); 35 | PrintSplitLine('-'); 36 | 37 | CRYPTO_Finalize(); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Kunlun/test/test_bloom_filter.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../filter/bloom_filter.hpp" 4 | 5 | 6 | template class Container> 7 | bool ReadFileToContainer(std::string file_name, Container& container) 8 | { 9 | std::ifstream fin; 10 | fin.open(file_name); 11 | if(!fin) 12 | { 13 | std::cerr << file_name << " open error" << std::endl; 14 | exit(EXIT_FAILURE); 15 | } 16 | std::string str; 17 | 18 | while (std::getline(fin, str)) 19 | { 20 | container.emplace_back(str); 21 | } 22 | fin.close(); 23 | return true; 24 | } 25 | 26 | 27 | void test_bloom_filter() 28 | { 29 | PrintSplitLine('-'); 30 | std::cout << "begin the test of bloom filter >>>" << std::endl; 31 | PrintSplitLine('-'); 32 | 33 | 34 | std::vector word_list; 35 | ReadFileToContainer("word-list-extra-large.txt", word_list); 36 | size_t max_element_num = word_list.size(); 37 | double statistical_security_parameter = 40; 38 | BloomFilter filter(max_element_num, statistical_security_parameter); 39 | 40 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 41 | //filter.insert(word_list.begin(), word_list.end()); 42 | filter.Insert(word_list); 43 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 44 | auto running_time = end_time - start_time; 45 | std::cout << "insert #" << word_list.size() << " elements take " 46 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 47 | 48 | std::cout << "statistical security parameter = " << statistical_security_parameter << std::endl; 49 | std::cout << double(filter.table_size)/filter.inserted_element_num << " bit per element" << std::endl; 50 | 51 | 52 | std::cout << "please type the string you want to check, and q to exit >>>" << std::endl; 53 | std::string new_str; 54 | while(true){ 55 | std::getline(std::cin, new_str); 56 | if(new_str == "q") break; 57 | if (filter.Contain(new_str) == true) { 58 | std::cout << new_str <<" is in the set" << std::endl; 59 | } 60 | else{ 61 | std::cout << new_str <<" is not in the set" << std::endl; 62 | } 63 | } 64 | 65 | std::string filter_file_name = "filter-1.bloom"; 66 | filter.WriteObject(filter_file_name); 67 | 68 | BloomFilter new_filter; 69 | new_filter.ReadObject(filter_file_name); 70 | 71 | std::cout << "please type the string you want to check, and q to exit >>>" << std::endl; 72 | while(true){ 73 | std::getline(std::cin, new_str); 74 | if(new_str == "q") break; 75 | if (filter.Contain(new_str) == true) { 76 | std::cout << new_str <<" is in the set" << std::endl; 77 | } 78 | else{ 79 | std::cout << new_str <<" is not in the set" << std::endl; 80 | } 81 | } 82 | 83 | PrintSplitLine('-'); 84 | std::cout << "finish the test of bloom filter >>>" << std::endl; 85 | PrintSplitLine('-'); 86 | 87 | } 88 | 89 | 90 | int main() 91 | { 92 | test_bloom_filter(); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /Kunlun/test/test_calculate_dlog.cpp: -------------------------------------------------------------------------------- 1 | #include "../pke/calculate_dlog.hpp" 2 | #include "../crypto/setup.hpp" 3 | 4 | void benchmark_dlog(size_t RANGE_LEN, size_t TRADEOFF_NUM, size_t TEST_NUM) 5 | { 6 | PrintSplitLine('-'); 7 | std::cout << "dlog benchmark test begins >>>"<< std::endl; 8 | PrintSplitLine('-'); 9 | std::cout << "RANGE_LEN = " << RANGE_LEN << std::endl; 10 | std::cout << "TRADEOFF_NUM = " << TRADEOFF_NUM << std::endl; 11 | std::cout << "TEST_NUM = " << TEST_NUM << std::endl; 12 | PrintSplitLine('-'); 13 | 14 | CheckDlogParameters(RANGE_LEN, TRADEOFF_NUM); 15 | 16 | ECPoint g = ECPoint(generator); 17 | std::string table_filename = GetTableFileName(g, RANGE_LEN, TRADEOFF_NUM); 18 | 19 | /* generate and save table */ 20 | if(FileExist(table_filename) == false){ 21 | std::cout << table_filename << " does not exist" << std::endl; 22 | BuildSaveTable(g, RANGE_LEN, TRADEOFF_NUM, table_filename); 23 | } 24 | 25 | // load the table from file 26 | std::cout << table_filename << " already exists" << std::endl; 27 | LoadTable(table_filename, RANGE_LEN, TRADEOFF_NUM); 28 | 29 | BigInt x[TEST_NUM]; // scalars 30 | BigInt x_prime[TEST_NUM]; // dlog scalars 31 | ECPoint Y[TEST_NUM]; 32 | BigInt MAX = BigInt(bn_2).Exp(RANGE_LEN); 33 | for(auto i = 0; i < TEST_NUM; i++) 34 | { 35 | x[i] = GenRandomBigIntLessThan(MAX); 36 | Y[i] = g * x[i]; 37 | } 38 | 39 | /* test dlog efficiency */ 40 | auto start_time = std::chrono::steady_clock::now(); 41 | for(auto i = 0; i < TEST_NUM; i++) 42 | { 43 | ShanksDLOG(g, Y[i], RANGE_LEN, TRADEOFF_NUM, x_prime[i]); 44 | } 45 | auto end_time = std::chrono::steady_clock::now(); 46 | auto running_time = end_time - start_time; 47 | std::cout << "average dlog takes time = " 48 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 49 | 50 | for(auto i = 0; i < TEST_NUM; i++) 51 | { 52 | if(x[i] != x_prime[i]){ 53 | std::cout << "dlog fails at test case " << i << std::endl; 54 | } 55 | } 56 | 57 | PrintSplitLine('-'); 58 | std::cout << "dlog benchmark test finishes <<<" << std::endl; 59 | PrintSplitLine('-'); 60 | } 61 | 62 | 63 | 64 | int main() 65 | { 66 | CRYPTO_Initialize(); 67 | 68 | std::ios::sync_with_stdio(false); 69 | 70 | size_t RANGE_LEN = 32; 71 | size_t TRADEOFF_NUM = 7; 72 | size_t TEST_NUM = 10000; 73 | 74 | benchmark_dlog(RANGE_LEN, TRADEOFF_NUM, TEST_NUM); 75 | 76 | CRYPTO_Finalize(); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Kunlun/test/test_innerproduct_proof.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../zkp/bulletproofs/innerproduct_proof.hpp" 4 | #include "../crypto/setup.hpp" 5 | 6 | // generate a random instance-witness pair 7 | void GenRandomInnerProductInstanceWitness(InnerProduct::PP &pp, InnerProduct::Instance &instance, InnerProduct::Witness &witness) 8 | { 9 | 10 | std::cout << "generate random (instance, witness) pair >>>" << std::endl; 11 | 12 | // InnerProduct_Instance_new(instance); 13 | witness.vec_a = GenRandomBigIntVectorLessThan(pp.VECTOR_LEN, BigInt(order)); 14 | witness.vec_b = GenRandomBigIntVectorLessThan(pp.VECTOR_LEN, BigInt(order)); 15 | 16 | //instance.u = GenRandomGenerator(); 17 | BigInt c = BigIntVectorModInnerProduct(witness.vec_a, witness.vec_b, BigInt(order)); 18 | 19 | instance.P = pp.u * c; // P = u^c 20 | 21 | instance.P = instance.P + ECPointVectorMul(pp.vec_g, witness.vec_a) + ECPointVectorMul(pp.vec_h, witness.vec_b); 22 | } 23 | 24 | void test_innerproduct_proof() 25 | { 26 | PrintSplitLine('-'); 27 | std::cout << "begin the test of innerproduct proof >>>" << std::endl; 28 | 29 | size_t VECTOR_LEN = 32; 30 | 31 | InnerProduct::PP pp = InnerProduct::Setup(VECTOR_LEN, true); 32 | 33 | InnerProduct::Instance instance; 34 | InnerProduct::Witness witness; 35 | 36 | GenRandomInnerProductInstanceWitness(pp, instance, witness); 37 | 38 | 39 | InnerProduct::Proof proof; 40 | 41 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 42 | std::string transcript_str = ""; 43 | transcript_str += instance.P.ToByteString(); 44 | 45 | InnerProduct::Prove(pp, instance, witness, transcript_str, proof); 46 | 47 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 48 | auto running_time = end_time - start_time; 49 | std::cout << "inner-product proof generation takes time = " 50 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 51 | 52 | start_time = std::chrono::steady_clock::now(); // start to count the time 53 | transcript_str = ""; 54 | transcript_str += instance.P.ToByteString(); 55 | InnerProduct::Verify(pp, instance, transcript_str, proof); 56 | end_time = std::chrono::steady_clock::now(); // end to count the time 57 | running_time = end_time - start_time; 58 | std::cout << "inner-product proof verification takes time = " 59 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 60 | 61 | start_time = std::chrono::steady_clock::now(); // start to count the time 62 | transcript_str = ""; 63 | transcript_str += instance.P.ToByteString(); 64 | InnerProduct::FastVerify(pp, instance, transcript_str, proof); 65 | end_time = std::chrono::steady_clock::now(); // end to count the time 66 | running_time = end_time - start_time; 67 | std::cout << "fast inner-product proof verification takes time = " 68 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 69 | 70 | std::cout << "finish the test of innerproduct proof >>>" << std::endl; 71 | } 72 | 73 | int main() 74 | { 75 | CRYPTO_Initialize(); 76 | 77 | test_innerproduct_proof(); 78 | 79 | CRYPTO_Finalize(); 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /Kunlun/test/test_mcl.cpp: -------------------------------------------------------------------------------- 1 | //#define DEBUG 2 | #include "../utility/print.hpp" 3 | 4 | #include 5 | 6 | 7 | struct TagZn; 8 | typedef mcl::FpT<> Fp; 9 | typedef mcl::FpT Zn; 10 | typedef mcl::EcT Ec; 11 | 12 | 13 | void benchmark_mcl_ecc(size_t TEST_NUM) 14 | { 15 | PrintSplitLine('-'); 16 | std::cout << "MCL ECC benchmark test begins >>>" << std::endl; 17 | PrintSplitLine('-'); 18 | 19 | Ec g; 20 | mcl::initCurve(MCL_SECP256K1, &g); 21 | //mcl::initCurve(MCL_NIST_P256, &g); 22 | 23 | Ec A[TEST_NUM]; 24 | Ec B[TEST_NUM]; 25 | Ec C[TEST_NUM]; 26 | Zn r[TEST_NUM]; 27 | 28 | Zn sk; 29 | Ec pk; 30 | 31 | sk.setRand(); 32 | Ec::mul(pk, g, sk); 33 | 34 | size_t winSize = 7; 35 | size_t bitSize = Zn::getBitSize(); 36 | mcl::fp::WindowMethod wm_g; 37 | wm_g.init(g, bitSize, winSize); 38 | 39 | 40 | for(auto i = 0; i < TEST_NUM; i++){ 41 | r[i].setRand(); 42 | } 43 | 44 | auto start_time = std::chrono::steady_clock::now(); 45 | 46 | for(auto i = 0; i < TEST_NUM; i++){ 47 | wm_g.mul(A[i], r[i]); 48 | } 49 | auto end_time = std::chrono::steady_clock::now(); 50 | auto running_time = end_time - start_time; 51 | std::cout << "fixed point exp with precomputation takes = " 52 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 53 | 54 | 55 | start_time = std::chrono::steady_clock::now(); 56 | 57 | for(auto i = 0; i < TEST_NUM; i++){ 58 | Ec::mul(B[i], pk, r[i]); 59 | } 60 | end_time = std::chrono::steady_clock::now(); 61 | running_time = end_time - start_time; 62 | std::cout << "fixed point exp without precomputation takes = " 63 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 64 | 65 | 66 | 67 | start_time = std::chrono::steady_clock::now(); 68 | for(auto i = 0; i < TEST_NUM; i++) 69 | { 70 | Ec::add(C[i], A[i], B[i]); 71 | } 72 | end_time = std::chrono::steady_clock::now(); 73 | running_time = end_time - start_time; 74 | std::cout << "point add takes time = " 75 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 76 | 77 | 78 | PrintSplitLine('-'); 79 | std::cout << "ECC benchmark test finishes <<<<<<" << std::endl; 80 | PrintSplitLine('-'); 81 | } 82 | 83 | int main() 84 | { 85 | size_t TEST_NUM = 10000; 86 | benchmark_mcl_ecc(TEST_NUM); 87 | 88 | return 0; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /Kunlun/test/test_netio.cpp: -------------------------------------------------------------------------------- 1 | #include "../netio/stream_channel.hpp" 2 | 3 | void test_client() 4 | { 5 | NetIO client("client", "127.0.0.1", 8080); 6 | 7 | std::string message; 8 | 9 | //std::getline(std::cin, message); 10 | message = "hello"; 11 | client.SendString(message); 12 | 13 | message = "world"; 14 | client.SendString(message); 15 | } 16 | 17 | void test_server() 18 | { 19 | NetIO server("server", "", 8080); 20 | 21 | std::string message(5, '0'); 22 | 23 | server.ReceiveString(message); 24 | std::cout << "message from client: " << message << std::endl; 25 | 26 | server.ReceiveString(message); 27 | std::cout << "message from client: " << message << std::endl; 28 | } 29 | 30 | void test_netio(std::string party) 31 | { 32 | if (party == "server") 33 | { 34 | test_server(); 35 | } 36 | 37 | if (party == "client") 38 | { 39 | test_client(); 40 | } 41 | 42 | } 43 | 44 | int main() 45 | { 46 | std::string party; 47 | 48 | std::cout << "please select your role (hint: first start server, then start the client) >>> "; 49 | std::getline(std::cin, party); // first receiver (acts as server), then sender (acts as client) 50 | test_netio(party); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Kunlun/test/test_nizk_dlog_equality.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../zkp/nizk/nizk_dlog_equality.hpp" 4 | #include "../crypto/setup.hpp" 5 | 6 | void GenRandomDDHInstanceWitness(DLOGEquality::PP &pp, DLOGEquality::Instance &instance, 7 | DLOGEquality::Witness &witness, bool flag) 8 | { 9 | // generate a true statement (false with overwhelming probability) 10 | PrintSplitLine('-'); 11 | if (flag == true){ 12 | std::cout << "generate a DDH tuple >>>" << std::endl; 13 | } 14 | else{ 15 | std::cout << "generate a random tuple >>>" << std::endl; 16 | } 17 | witness.w = GenRandomBigIntLessThan(order); 18 | 19 | instance.g1 = GenRandomGenerator(); 20 | instance.g2 = GenRandomGenerator(); 21 | 22 | instance.h1 = instance.g1 * witness.w; 23 | instance.h2 = instance.g2 * witness.w; 24 | 25 | if(flag == false){ 26 | ECPoint noisy = GenRandomGenerator(); 27 | instance.h2 = instance.h2 + noisy; 28 | } 29 | } 30 | 31 | void test_nizk_dlog_equality(bool flag) 32 | { 33 | PrintSplitLine('-'); 34 | std::cout << "begin the test of dlog equality proof (standard version) >>>" << std::endl; 35 | 36 | DLOGEquality::PP pp = DLOGEquality::Setup(); 37 | DLOGEquality::Instance instance; 38 | DLOGEquality::Witness witness; 39 | 40 | 41 | std::string transcript_str; 42 | 43 | // test the standard version 44 | 45 | GenRandomDDHInstanceWitness(pp, instance, witness, flag); 46 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 47 | transcript_str = ""; 48 | DLOGEquality::Proof proof = DLOGEquality::Prove(pp, instance, witness, transcript_str); 49 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 50 | auto running_time = end_time - start_time; 51 | std::cout << "DDH proof generation takes time = " 52 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 53 | 54 | 55 | start_time = std::chrono::steady_clock::now(); // start to count the time 56 | transcript_str = ""; 57 | DLOGEquality::Verify(pp, instance, transcript_str, proof); 58 | end_time = std::chrono::steady_clock::now(); // end to count the time 59 | running_time = end_time - start_time; 60 | std::cout << "DDH proof verification takes time = " 61 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 62 | 63 | std::cout << "finish the test of dlog equality proof (standard version) >>>" << std::endl; 64 | 65 | } 66 | 67 | int main() 68 | { 69 | CRYPTO_Initialize(); 70 | 71 | test_nizk_dlog_equality(true); 72 | test_nizk_dlog_equality(false); 73 | 74 | CRYPTO_Finalize(); 75 | 76 | return 0; 77 | } 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Kunlun/test/test_nizk_dlog_knowledge.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../zkp/nizk/nizk_dlog_knowledge.hpp" 4 | #include "../crypto/setup.hpp" 5 | 6 | void GenRandomDLOGInstanceWitness(DLOGKnowledge::PP &pp, DLOGKnowledge::Instance &instance, DLOGKnowledge::Witness &witness) 7 | { 8 | // generate a true statement (false with overwhelming probability) 9 | PrintSplitLine('-'); 10 | std::cout << "generate a random DLOG tuple >>>" << std::endl; 11 | 12 | witness.w = GenRandomBigIntLessThan(order); 13 | instance.g = GenRandomGenerator(); 14 | instance.h = instance.g * witness.w; 15 | } 16 | 17 | void test_nizk_dlog_knowledge() 18 | { 19 | PrintSplitLine('-'); 20 | std::cout << "begin the test of dlog knowledge proof (standard version) >>>" << std::endl; 21 | 22 | DLOGKnowledge::PP pp = DLOGKnowledge::Setup(); 23 | DLOGKnowledge::Instance instance; 24 | DLOGKnowledge::Witness witness; 25 | 26 | std::string transcript_str; 27 | 28 | // test the standard version 29 | 30 | GenRandomDLOGInstanceWitness(pp, instance, witness); 31 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 32 | transcript_str = ""; 33 | DLOGKnowledge::Proof proof = DLOGKnowledge::Prove(pp, instance, witness, transcript_str); 34 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 35 | auto running_time = end_time - start_time; 36 | std::cout << "DLOG proof generation takes time = " 37 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 38 | 39 | 40 | start_time = std::chrono::steady_clock::now(); // start to count the time 41 | transcript_str = ""; 42 | DLOGKnowledge::Verify(pp, instance, transcript_str, proof); 43 | end_time = std::chrono::steady_clock::now(); // end to count the time 44 | running_time = end_time - start_time; 45 | std::cout << "DLOG proof verification takes time = " 46 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 47 | 48 | std::cout << "finish the test of dlog knowledge proof (standard version) >>>" << std::endl; 49 | 50 | } 51 | 52 | int main() 53 | { 54 | CRYPTO_Initialize(); 55 | 56 | test_nizk_dlog_knowledge(); 57 | 58 | CRYPTO_Finalize(); 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Kunlun/test/test_nizk_enc_relation.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../zkp/nizk/nizk_enc_relation.hpp" 4 | #include "../crypto/setup.hpp" 5 | 6 | 7 | void GenRandomEncInstanceWitness(EncRelation::PP &pp, EncRelation::Instance &instance, 8 | EncRelation::Witness &witness, bool flag) 9 | { 10 | PrintSplitLine('-'); 11 | 12 | size_t N = 8; 13 | 14 | srand(time(0)); 15 | witness.l = rand() % N; 16 | 17 | std::vector vec_m = GenRandomECPointVector(N); 18 | std::vector vec_r = GenRandomBigIntVectorLessThan(N, order); 19 | instance.vec_CT.resize(N); 20 | instance.ek = GenRandomECPoint(); 21 | 22 | for(auto i = 0; i < N; i++){ 23 | instance.vec_CT[i] = TwistedElGamal::Enc(pp.enc_part, instance.ek, vec_m[i], vec_r[i]); 24 | } 25 | 26 | if (flag == true) 27 | { 28 | std::cout << "generate " << N <<" well-formed ciphertexts >>>" << std::endl; 29 | ECPoint m; 30 | m.SetInfinity(); 31 | instance.vec_CT[witness.l] = TwistedElGamal::Enc(pp.enc_part, instance.ek, m, vec_r[witness.l]); 32 | } 33 | else 34 | { 35 | std::cout << "generate " << N <<" ill-formed ciphertexts >>>" << std::endl; 36 | } 37 | 38 | witness.r = vec_r[witness.l]; 39 | } 40 | 41 | void test_nizk_enc_relation(bool flag) 42 | { 43 | PrintSplitLine('-'); 44 | std::cout << "begin the test of NIZKPoK for enc relation >>>" << std::endl; 45 | 46 | size_t N_max = 32; 47 | Pedersen::PP com_pp = Pedersen::Setup(N_max); 48 | 49 | 50 | size_t MSG_LEN = 32; 51 | size_t TRADEOFF_NUM = 7; 52 | size_t DEC_THREAD_NUM = 8; 53 | TwistedElGamal::PP enc_pp = TwistedElGamal::Setup(MSG_LEN, TRADEOFF_NUM); 54 | 55 | 56 | size_t n = 2; 57 | EncRelation::PP pp = EncRelation::Setup(com_pp, enc_pp, n); 58 | 59 | EncRelation::Instance instance; 60 | EncRelation::Witness witness; 61 | 62 | std::string transcript_str; 63 | 64 | GenRandomEncInstanceWitness(pp, instance, witness, flag); 65 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 66 | transcript_str = ""; 67 | 68 | EncRelation::Proof proof = EncRelation::Prove(pp, instance, witness, transcript_str); 69 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 70 | auto running_time = end_time - start_time; 71 | std::cout << "proof generation takes time = " 72 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 73 | 74 | start_time = std::chrono::steady_clock::now(); // start to count the time 75 | transcript_str = ""; 76 | EncRelation::Verify(pp, instance, transcript_str, proof); 77 | end_time = std::chrono::steady_clock::now(); // end to count the time 78 | running_time = end_time - start_time; 79 | std::cout << "proof verification takes time = " 80 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 81 | } 82 | 83 | int main() 84 | { 85 | CRYPTO_Initialize(); 86 | 87 | test_nizk_enc_relation(true); 88 | test_nizk_enc_relation(false); 89 | 90 | CRYPTO_Finalize(); 91 | 92 | return 0; 93 | } 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /Kunlun/test/test_nizk_plaintext_equality.cpp: -------------------------------------------------------------------------------- 1 | #define DEBUG 2 | 3 | #include "../pke/twisted_elgamal.hpp" 4 | #include "../zkp/nizk/nizk_plaintext_equality.hpp" 5 | #include "../crypto/setup.hpp" 6 | 7 | void GenRandomTripleEncInstanceWitness(PlaintextEquality::PP &pp, PlaintextEquality::Instance &instance, 8 | PlaintextEquality::Witness &witness, bool flag) 9 | { 10 | PrintSplitLine('-'); 11 | if (flag == true){ 12 | std::cout << "generate a well-formed 1-message 3-recipient twisted ElGamal ciphertext >>>" << std::endl; 13 | } else{ 14 | std::cout << ">>> generate an ill-formed 1-message 3-recipient twisted ElGamal ciphertext" << std::endl; 15 | } 16 | 17 | witness.r = GenRandomBigIntLessThan(order); 18 | witness.v = GenRandomBigIntLessThan(order); 19 | 20 | instance.vec_pk.resize(3); 21 | for(auto i = 0; i < instance.vec_pk.size(); i++){ 22 | instance.vec_pk[i] = GenRandomGenerator(); 23 | } 24 | 25 | 26 | TwistedElGamal::PP enc_pp; 27 | enc_pp.g = pp.g; 28 | enc_pp.h = pp.h; 29 | 30 | instance.ct = TwistedElGamal::Enc(enc_pp, instance.vec_pk, witness.v, witness.r); 31 | 32 | 33 | if(flag == false){ 34 | ECPoint noisy = GenRandomGenerator(); 35 | instance.ct.Y = instance.ct.Y + noisy; 36 | } 37 | } 38 | 39 | void test_nizk_plaintext_equality(bool flag) 40 | { 41 | PrintSplitLine('-'); 42 | std::cout << "begin the test of NIZKPoK for plaintext equality >>>" << std::endl; 43 | 44 | TwistedElGamal::PP pp_enc = TwistedElGamal::Setup(32, 7); 45 | PlaintextEquality::PP pp = PlaintextEquality::Setup(pp_enc); 46 | PlaintextEquality::Instance instance; 47 | PlaintextEquality::Witness witness; 48 | 49 | std::string transcript_str; 50 | 51 | GenRandomTripleEncInstanceWitness(pp, instance, witness, flag); 52 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 53 | transcript_str = ""; 54 | PlaintextEquality::Proof proof = PlaintextEquality::Prove(pp, instance, witness, transcript_str); 55 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 56 | auto running_time = end_time - start_time; 57 | std::cout << "proof generation takes time = " 58 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 59 | 60 | start_time = std::chrono::steady_clock::now(); // start to count the time 61 | transcript_str = ""; 62 | PlaintextEquality::Verify(pp, instance, transcript_str, proof); 63 | end_time = std::chrono::steady_clock::now(); // end to count the time 64 | running_time = end_time - start_time; 65 | std::cout << "proof verification takes time = " 66 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 67 | } 68 | 69 | int main() 70 | { 71 | CRYPTO_Initialize(); 72 | 73 | test_nizk_plaintext_equality(true); 74 | test_nizk_plaintext_equality(false); 75 | 76 | CRYPTO_Finalize(); 77 | 78 | return 0; 79 | } 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Kunlun/test/test_nizk_plaintext_knowledge.cpp: -------------------------------------------------------------------------------- 1 | //#define DEBUG 2 | 3 | #include "../pke/twisted_elgamal.hpp" 4 | #include "../zkp/nizk/nizk_plaintext_knowledge.hpp" 5 | #include "../crypto/setup.hpp" 6 | 7 | 8 | void GenRandomEncInstanceWitness(PlaintextKnowledge::PP &pp, PlaintextKnowledge::Instance &instance, 9 | PlaintextKnowledge::Witness &witness) 10 | { 11 | PrintSplitLine('-'); 12 | std::cout << "generate a valid twisted elgamal ciphertext >>>" << std::endl; 13 | 14 | witness.r = GenRandomBigIntLessThan(order); 15 | witness.v = GenRandomBigIntLessThan(order); 16 | 17 | instance.pk = GenRandomGenerator(); 18 | TwistedElGamal::PP pp_enc; 19 | pp_enc.g = pp.g; 20 | pp_enc.h = pp.h; 21 | 22 | instance.ct = TwistedElGamal::Enc(pp_enc, instance.pk, witness.v, witness.r); 23 | } 24 | 25 | void test_nizk_plaintext_knowledge() 26 | { 27 | std::cout << "begin the test of NIZKPoK for plaintext knowledge >>>" << std::endl; 28 | 29 | TwistedElGamal::PP pp_enc = TwistedElGamal::Setup(32, 7); 30 | PlaintextKnowledge::PP pp = PlaintextKnowledge::Setup(pp_enc); 31 | PlaintextKnowledge::Instance instance; 32 | PlaintextKnowledge::Witness witness; 33 | 34 | GenRandomEncInstanceWitness(pp, instance, witness); 35 | 36 | std::string transcript_str; 37 | 38 | auto start_time = std::chrono::steady_clock::now(); // start to count the time 39 | transcript_str = ""; 40 | PlaintextKnowledge::Proof proof = PlaintextKnowledge::Prove(pp, instance, witness, transcript_str); 41 | auto end_time = std::chrono::steady_clock::now(); // end to count the time 42 | auto running_time = end_time - start_time; 43 | std::cout << "proof generation takes time = " 44 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 45 | 46 | start_time = std::chrono::steady_clock::now(); // start to count the time 47 | transcript_str = ""; 48 | PlaintextKnowledge::Verify(pp, instance, transcript_str, proof); 49 | end_time = std::chrono::steady_clock::now(); // end to count the time 50 | running_time = end_time - start_time; 51 | std::cout << "proof verification takes time = " 52 | << std::chrono::duration (running_time).count() << " ms" << std::endl; 53 | } 54 | 55 | int main() 56 | { 57 | CRYPTO_Initialize(); 58 | 59 | test_nizk_plaintext_knowledge(); 60 | 61 | CRYPTO_Finalize(); 62 | 63 | return 0; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Kunlun/test/test_oteoprf.cpp: -------------------------------------------------------------------------------- 1 | #include "../mpc/oprf/ote_oprf.hpp" 2 | #include "../crypto/setup.hpp" 3 | 4 | struct OTEOPRFTestCase{ 5 | std::vector vec_X; // server set 6 | std::vector vec_Y; // client set 7 | size_t LEN; // size of set 8 | }; 9 | 10 | OTEOPRFTestCase GenTestCase(size_t LOG_LEN) 11 | { 12 | OTEOPRFTestCase testcase; 13 | testcase.LEN = 1 << LOG_LEN; 14 | 15 | PRG::Seed seed = PRG::SetSeed(fixed_seed, 0); // initialize PRG 16 | testcase.vec_X = PRG::GenRandomBlocks(seed, testcase.LEN); 17 | 18 | return testcase; 19 | } 20 | 21 | void SaveTestCase(OTEOPRFTestCase &testcase, std::string testcase_filename) 22 | { 23 | std::ofstream fout; 24 | fout.open(testcase_filename, std::ios::binary); 25 | if(!fout) 26 | { 27 | std::cerr << testcase_filename << " open error" << std::endl; 28 | exit(1); 29 | } 30 | fout << testcase.LEN; 31 | 32 | fout << testcase.vec_X; 33 | 34 | fout.close(); 35 | } 36 | 37 | void FetchTestCase(OTEOPRFTestCase &testcase, std::string testcase_filename) 38 | { 39 | std::ifstream fin; 40 | fin.open(testcase_filename, std::ios::binary); 41 | if(!fin) 42 | { 43 | std::cerr << testcase_filename << " open error" << std::endl; 44 | exit(1); 45 | } 46 | fin >> testcase.LEN; 47 | 48 | testcase.vec_X.resize(testcase.LEN); 49 | 50 | fin >> testcase.vec_X; 51 | 52 | fin.close(); 53 | } 54 | 55 | int main() 56 | { 57 | CRYPTO_Initialize(); 58 | 59 | std::cout << "OTE-based OPRF test begins >>>" << std::endl; 60 | 61 | PrintSplitLine('-'); 62 | std::cout << "generate or load public parameters and test case" << std::endl; 63 | 64 | size_t LOG_LEN = 20; 65 | 66 | // generate pp (must be same for both server and client) 67 | std::string pp_filename = "OTEOPRF.pp"; 68 | OTEOPRF::PP pp; 69 | if(!FileExist(pp_filename)){ 70 | pp = OTEOPRF::Setup(LOG_LEN); // 40 is the statistical parameter 71 | OTEOPRF::SavePP(pp, pp_filename); 72 | } 73 | else{ 74 | OTEOPRF::FetchPP(pp, pp_filename); 75 | } 76 | 77 | std::cout << "number of elements = " << (1 << LOG_LEN) << std::endl; 78 | 79 | std::string testcase_filename = "OTEOPRF.testcase"; 80 | 81 | OTEOPRFTestCase testcase; 82 | if(!FileExist(testcase_filename)){ 83 | testcase = GenTestCase(LOG_LEN); 84 | SaveTestCase(testcase, testcase_filename); 85 | } 86 | else{ 87 | FetchTestCase(testcase, testcase_filename); 88 | } 89 | PrintSplitLine('-'); 90 | 91 | std::string party; 92 | std::cout << "please select your role between server and receiver (hint: first start server, then start client) ==> "; 93 | std::getline(std::cin, party); 94 | 95 | 96 | if (party == "server") 97 | { 98 | NetIO server_io("server", "", 8080); 99 | 100 | std::vector> oprf_key = OTEOPRF::Server(server_io, pp); 101 | std::vector> vec_Fk_X = OTEOPRF::Evaluate(pp, oprf_key, testcase.vec_X, pp.LEN); 102 | } 103 | 104 | if (party == "client") 105 | { 106 | NetIO client_io("client", "127.0.0.1", 8080); 107 | std::vector> vec_Fk_X = OTEOPRF::Client(client_io, pp, testcase.vec_X, pp.LEN); 108 | } 109 | 110 | CRYPTO_Finalize(); 111 | 112 | return 0; 113 | } -------------------------------------------------------------------------------- /Kunlun/test/test_schnorr_sig.cpp: -------------------------------------------------------------------------------- 1 | //#define DEBUG 2 | #include "../signature/schnorr.hpp" 3 | #include "../utility/print.hpp" 4 | #include "../crypto/setup.hpp" 5 | 6 | void test_schnorr(size_t TEST_NUM) 7 | { 8 | std::cout << "begin the basic correctness test >>>" << std::endl; 9 | 10 | Schnorr::PP pp = Schnorr::Setup(); 11 | std::vector pk(TEST_NUM); 12 | std::vector sk(TEST_NUM); 13 | 14 | auto start_time = std::chrono::steady_clock::now(); 15 | for(auto i = 0; i < TEST_NUM; i++){ 16 | std::tie(pk[i], sk[i]) = Schnorr::KeyGen(pp); 17 | } 18 | auto end_time = std::chrono::steady_clock::now(); 19 | 20 | auto running_time = end_time - start_time; 21 | std::cout << "key generation takes time = " 22 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 23 | 24 | std::vector sigma(TEST_NUM); 25 | 26 | std::string message = "crypto is hard"; 27 | 28 | start_time = std::chrono::steady_clock::now(); 29 | 30 | for(auto i = 0; i < TEST_NUM; i++){ 31 | sigma[i] = Schnorr::Sign(pp, sk[i], message); 32 | } 33 | end_time = std::chrono::steady_clock::now(); 34 | running_time = end_time - start_time; 35 | std::cout << "sign message takes time = " 36 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 37 | 38 | 39 | start_time = std::chrono::steady_clock::now(); 40 | for(auto i = 0; i < TEST_NUM; i++){ 41 | if(Schnorr::Verify(pp, pk[i], message, sigma[i]) == false){ 42 | std::cout << "the " << i << "th verification fails" << std::endl; 43 | } 44 | } 45 | end_time = std::chrono::steady_clock::now(); 46 | running_time = end_time - start_time; 47 | std::cout << "verify signature takes time = " 48 | << std::chrono::duration (running_time).count()/TEST_NUM << " ms" << std::endl; 49 | } 50 | 51 | 52 | int main() 53 | { 54 | CRYPTO_Initialize(); 55 | 56 | 57 | std::ios::sync_with_stdio(false); 58 | 59 | PrintSplitLine('-'); 60 | std::cout << "Schnorr SIG test begins >>>" << std::endl; 61 | PrintSplitLine('-'); 62 | 63 | size_t TEST_NUM = 10000; 64 | test_schnorr(TEST_NUM); 65 | 66 | PrintSplitLine('-'); 67 | std::cout << "Schnorr SIG test finishes >>>" << std::endl; 68 | PrintSplitLine('-'); 69 | 70 | 71 | CRYPTO_Finalize(); 72 | 73 | return 0; 74 | } 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /Kunlun/utility/bit_operation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BIT_OPERATION_HPP_ 2 | #define BIT_OPERATION_HPP_ 3 | 4 | 5 | // inspired from http://www-graphics.stanford.edu/~seander/bithacks.html#ZeroInWord 6 | 7 | #define haszero4(x) (((x)-0x1111ULL) & (~(x)) & 0x8888ULL) 8 | #define hasvalue4(x, n) (haszero4((x) ^ (0x1111ULL * (n)))) 9 | 10 | #define haszero8(x) (((x)-0x01010101ULL) & (~(x)) & 0x80808080ULL) 11 | #define hasvalue8(x, n) (haszero8((x) ^ (0x01010101ULL * (n)))) 12 | 13 | #define haszero12(x) (((x)-0x001001001001ULL) & (~(x)) & 0x800800800800ULL) 14 | #define hasvalue12(x, n) (haszero12((x) ^ (0x001001001001ULL * (n)))) 15 | 16 | #define haszero16(x) \ 17 | (((x)-0x0001000100010001ULL) & (~(x)) & 0x8000800080008000ULL) 18 | #define hasvalue16(x, n) (haszero16((x) ^ (0x0001000100010001ULL * (n)))) 19 | 20 | inline uint32_t upperpower2(uint32_t x) { 21 | x--; 22 | x |= x >> 1; 23 | x |= x >> 2; 24 | x |= x >> 4; 25 | x |= x >> 8; 26 | x |= x >> 16; 27 | x++; 28 | return x; 29 | } 30 | 31 | #endif -------------------------------------------------------------------------------- /Kunlun/utility/polymul.hpp: -------------------------------------------------------------------------------- 1 | #ifndef POLY_MUL_HPP_ 2 | #define POLY_MUL_HPP_ 3 | 4 | // A[] represents coefficients of first polynomial 5 | // B[] represents coefficients of second polynomial 6 | std::vector PolyMul(std::vector A, std::vector B) 7 | { 8 | // Initialize the product polynomial 9 | std::vector C(A.size() + B.size()-1, bn_0); 10 | 11 | // Multiply two polynomials term by term 12 | // Take ever term of first polynomial 13 | for (auto i = 0; i < A.size(); i++){ 14 | // Multiply the current term of first polynomial with every term of second polynomial. 15 | for (auto j = 0; j < B.size(); j++) 16 | C[i+j] = (C[i+j] + A[i] * B[j]) % order; 17 | } 18 | 19 | return C; 20 | } 21 | 22 | // result = P[0]*...P[n-1] 23 | std::vector PolyMul(std::vector> P) 24 | { 25 | std::vector result(1, bn_1); 26 | for(auto i = 0; i < P.size(); i++){ 27 | result = PolyMul(result, P[i]); 28 | } 29 | return result; 30 | } 31 | 32 | // A utility function to print a polynomial 33 | void PrintPoly(std::vector P) 34 | { 35 | for (auto i = 0; i < P.size(); i++){ 36 | P[i].PrintInDec(); 37 | if (i != 0) std::cout << "*x^" << i; 38 | if (i != P.size()-1) std::cout << " + "; 39 | } 40 | std::cout << std::endl; 41 | } 42 | 43 | // A utility function to print a polynomial 44 | BigInt EvalPoly(std::vector P, BigInt x) 45 | { 46 | BigInt result = bn_0; 47 | BigInt carry = bn_1; 48 | for (auto i = 0; i < P.size(); i++){ 49 | result += (P[i] * carry) % order; 50 | carry *= x; 51 | } 52 | return result % order; 53 | } 54 | 55 | #endif -------------------------------------------------------------------------------- /Kunlun/utility/print.hpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this hpp implements print functionality 3 | ***************************************************************************** 4 | * @author This file is part of Kunlun, developed by Yu Chen 5 | * @copyright MIT license (see LICENSE file) 6 | *****************************************************************************/ 7 | #ifndef KUNLUN_UTILITY_PRINT_HPP_ 8 | #define KUNLUN_UTILITY_PRINT_HPP_ 9 | 10 | #include 11 | 12 | const static size_t LINE_LEN = 120; // the length of split line 13 | 14 | 15 | /* print split line */ 16 | inline void PrintSplitLine(char ch) 17 | { 18 | for (auto i = 0; i < LINE_LEN; i++) std::cout << ch; 19 | std::cout << std::endl; 20 | } 21 | 22 | // print uint_8 in hex 23 | inline void PrintBytes(uint8_t* A, size_t LEN) 24 | { 25 | for(auto i = 0; i < LEN; i++) 26 | std::cout << std::hex << +A[i] << " "; 27 | std::cout << std::endl; 28 | } 29 | 30 | 31 | inline void PrintBitMatrix(uint8_t *M, size_t ROW_NUM, size_t COLUMN_NUM) 32 | { 33 | uint8_t mask = 0X80; 34 | //std::cout << int(mask) << std::endl; 35 | uint8_t *temp = new uint8_t [ROW_NUM/8 * COLUMN_NUM]; 36 | memcpy(temp, M, ROW_NUM/8 * COLUMN_NUM); 37 | uint8_t T[ROW_NUM*COLUMN_NUM]; 38 | for(auto i = 0; i < ROW_NUM*COLUMN_NUM; i++){ 39 | if((temp[i/8]&mask) == 0X80) T[i] = 1; 40 | else T[i] = 0; 41 | temp[i/8] = temp[i/8] << 1; 42 | } 43 | 44 | for(auto i = 0; i < ROW_NUM; i++){ 45 | for(auto j = 0; j < COLUMN_NUM; j++){ 46 | if(T[j*ROW_NUM+i] == 0) std::cout << 0 << " "; 47 | else std::cout << 1 << " "; 48 | } 49 | std::cout << std::endl; 50 | } 51 | std::cout << std::endl; 52 | delete[] temp; 53 | } 54 | 55 | 56 | #endif // COMMON_PRINT_HPP_ -------------------------------------------------------------------------------- /Kunlun/utility/routines.hpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this hpp implements some routine algorithms 3 | ***************************************************************************** 4 | * @author This file is part of Kunlun, developed by Yu Chen 5 | * @copyright MIT license (see LICENSE file) 6 | *****************************************************************************/ 7 | #ifndef KUNLUN_UTILITY_ROUTINES_HPP_ 8 | #define KUNLUN_UTILITY_ROUTINES_HPP_ 9 | 10 | #include "../include/std.inc" 11 | 12 | // check the existence of a given file 13 | inline bool FileExist(const std::string& filename) 14 | { 15 | bool existing_flag; 16 | std::ifstream fin; 17 | fin.open(filename); 18 | if(!fin) existing_flag = false; 19 | else existing_flag = true; 20 | return existing_flag; 21 | } 22 | 23 | 24 | inline std::string ToHexString(std::string byte_str) 25 | { 26 | std::string hex_str; 27 | std::stringstream ss; 28 | 29 | for (const auto &item : byte_str) { 30 | ss << std::hex << int(item); 31 | } 32 | hex_str = ss.str(); 33 | 34 | // format to uppercase 35 | for (auto & c: hex_str) c = toupper(c); 36 | return hex_str; 37 | } 38 | 39 | // A simple trick to decide if x = 2^n for n > 0 and x > 0 40 | inline bool IsPowerOfTwo(size_t x) 41 | { 42 | return (x != 0) && ((x & (x - 1)) == 0); 43 | } 44 | 45 | // 0 <= r_i < MAX 46 | inline std::vector GenRandomNaturalIntegerVectorLessThan(size_t LEN, int64_t MAX) 47 | { 48 | std::vector vec_result(LEN); 49 | srand(time(0)); 50 | for(auto i = 0; i < LEN; i++) 51 | { 52 | vec_result[i] = rand() % MAX; 53 | } 54 | return vec_result; 55 | } 56 | 57 | // -MAX < r_i < MAX 58 | inline std::vector GenRandomIntegerVectorAbsoluteLessThan(size_t LEN, int64_t MAX) 59 | { 60 | std::vector vec_result = GenRandomNaturalIntegerVectorLessThan(LEN, 2*MAX); 61 | for(auto i = 0; i < LEN; i++) 62 | { 63 | vec_result[i] = vec_result[i] - MAX; 64 | } 65 | return vec_result; 66 | } 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /Kunlun/utility/serialization.hpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | this hpp implements some routine algorithms 3 | ***************************************************************************** 4 | * @author This file is part of Kunlun, developed by Yu Chen 5 | * @copyright MIT license (see LICENSE file) 6 | *****************************************************************************/ 7 | #ifndef KUNLUN_SERIALIZATION_HPP_ 8 | #define KUNLUN_SERIALIZATION_HPP_ 9 | 10 | #include "../include/std.inc" 11 | #include "../config/config.h" 12 | 13 | namespace Serialization{ 14 | 15 | #ifdef IS_LINUX 16 | inline std::ofstream &operator<<(std::ofstream &fout, const size_t& element) 17 | { 18 | fout.write(reinterpret_cast(&element), 8); 19 | return fout; 20 | } 21 | 22 | inline std::ifstream &operator>>(std::ifstream &fin, size_t& element) 23 | { 24 | fin.read(reinterpret_cast(&element), 8); 25 | return fin; 26 | } 27 | #endif 28 | 29 | template // Note: T must be a C++ POD type. 30 | inline std::ofstream &operator<<(std::ofstream &fout, const ElementType& element) 31 | { 32 | fout.write(reinterpret_cast(&element), sizeof(ElementType)); 33 | return fout; 34 | } 35 | 36 | template // Note: T must be a C++ POD type. 37 | inline std::ifstream &operator>>(std::ifstream &fin, ElementType& element) 38 | { 39 | fin.read(reinterpret_cast(&element), sizeof(ElementType)); 40 | return fin; 41 | } 42 | 43 | 44 | template // Note: T must be a C++ POD type. 45 | inline std::ofstream &operator<<(std::ofstream &fout, const std::vector& vec_element) 46 | { 47 | fout.write(reinterpret_cast(vec_element.data()), vec_element.size() * sizeof(ElementType)); 48 | return fout; 49 | } 50 | 51 | template // Note: T must be a C++ POD type. 52 | inline std::ifstream &operator>>(std::ifstream &fin, std::vector& vec_element) 53 | { 54 | fin.read(reinterpret_cast(vec_element.data()), vec_element.size() * sizeof(ElementType)); 55 | return fin; 56 | } 57 | 58 | template <> // specialize for string 59 | inline std::ofstream &operator<<(std::ofstream &fout, const std::string& str) 60 | { 61 | size_t LEN = str.size(); 62 | fout << LEN; 63 | fout.write(reinterpret_cast(&str[0]), str.size()); 64 | return fout; 65 | } 66 | 67 | template <> // specialize for string 68 | inline std::ifstream &operator>>(std::ifstream &fin, std::string& str) 69 | { 70 | size_t LEN; 71 | fin >> LEN; 72 | str.resize(LEN); 73 | fin.read(reinterpret_cast(&str[0]), str.size()); 74 | return fin; 75 | } 76 | 77 | } 78 | 79 | using namespace Serialization; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /cli/common_utils.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | 10 | // APSU 11 | #include "apsu/util/stopwatch.h" 12 | 13 | /** 14 | Prepare console for color output. 15 | */ 16 | void prepare_console(); 17 | 18 | /** 19 | Generate timing report for timespans. 20 | */ 21 | std::vector generate_timespan_report( 22 | const std::vector ×pans, int max_name_length); 23 | 24 | /** 25 | Generate timing report for single events. 26 | */ 27 | std::vector generate_event_report( 28 | const std::vector &timepoints, int max_name_length); 29 | 30 | /** 31 | Print timings. 32 | */ 33 | void print_timing_report(const apsu::util::Stopwatch &stopwatch); 34 | 35 | /** 36 | Throw an exception if the given file is invalid. 37 | */ 38 | void throw_if_file_invalid(const std::string &file_name); 39 | -------------------------------------------------------------------------------- /cli/csv_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // APSU 15 | #include "apsu/item.h" 16 | #include "apsu/psu_params.h" 17 | #include "apsu/util/db_encoding.h" 18 | 19 | /** 20 | Simple CSV file parser 21 | */ 22 | class CSVReader { 23 | public: 24 | using UnlabeledData = std::vector; 25 | 26 | using LabeledData = std::vector>; 27 | 28 | using DBData = std::variant; 29 | 30 | CSVReader(); 31 | 32 | CSVReader(const std::string &file_name); 33 | 34 | std::pair> read(std::istream &stream) const; 35 | 36 | std::pair> read() const; 37 | 38 | private: 39 | std::string file_name_; 40 | 41 | std::pair process_line( 42 | const std::string &line, 43 | std::string &orig_item, 44 | apsu::Item &item, 45 | apsu::Label &label) const; 46 | }; // class CSVReader 47 | -------------------------------------------------------------------------------- /cli/pd_tool/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | target_sources(pd_tool 5 | PRIVATE 6 | ${CMAKE_CURRENT_LIST_DIR}/pd_tool.cpp 7 | ) 8 | -------------------------------------------------------------------------------- /cli/pd_tool/clp.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | 11 | // TCLAP 12 | #ifdef _MSC_VER 13 | #pragma warning(push, 0) 14 | #endif 15 | #include "tclap/CmdLine.h" 16 | #ifdef _MSC_VER 17 | #pragma warning(pop) 18 | #endif 19 | 20 | /** 21 | Command Line Processor for pd_tool. 22 | */ 23 | class CLP : public TCLAP::CmdLine { 24 | public: 25 | CLP(const std::string &description, const std::string &version) 26 | : TCLAP::CmdLine(description, /* delim */ ' ', version) 27 | {} 28 | 29 | bool parse_args(int argc, char **argv) 30 | { 31 | TCLAP::ValueArg bound_arg( 32 | "b", 33 | "bound", 34 | "Up to what power we want to compute (max_items_per_bin)", 35 | /* req */ true, 36 | /* value */ 1, 37 | /* type desc */ "unsigned integer"); 38 | add(bound_arg); 39 | 40 | TCLAP::ValueArg ps_low_degree_arg( 41 | "p", 42 | "ps_low_degree", 43 | "Low power when using Paterson-Stockmeyer for polynomial evaluation", 44 | /* req */ false, 45 | /* value */ 0, 46 | /* type desc */ "unsigned integer"); 47 | add(ps_low_degree_arg); 48 | 49 | TCLAP::ValueArg dot_file_arg( 50 | "o", 51 | "out", 52 | "Write the PowersDag in DOT format to given file", 53 | /* req */ false, 54 | /* value */ "", 55 | /* type desc */ "string"); 56 | add(dot_file_arg); 57 | 58 | TCLAP::UnlabeledMultiArg sources_arg( 59 | "sources", 60 | "The source powers", 61 | /* req */ true, 62 | "list of unsigned integers"); 63 | add(sources_arg); 64 | 65 | try { 66 | parse(argc, argv); 67 | 68 | bound_ = bound_arg.getValue(); 69 | ps_low_degree_ = ps_low_degree_arg.getValue(); 70 | if (dot_file_arg.isSet()) { 71 | dot_file_ = dot_file_arg.getValue(); 72 | } 73 | sources_ = sources_arg.getValue(); 74 | } catch (...) { 75 | std::cout << "Error parsing parameters."; 76 | return false; 77 | } 78 | 79 | return true; 80 | } 81 | 82 | std::uint32_t bound() const 83 | { 84 | return bound_; 85 | } 86 | 87 | std::uint32_t ps_low_degree() const 88 | { 89 | return ps_low_degree_; 90 | } 91 | 92 | std::string dot_file() const 93 | { 94 | return dot_file_; 95 | } 96 | 97 | const std::vector &sources() const 98 | { 99 | return sources_; 100 | } 101 | 102 | private: 103 | std::uint32_t bound_; 104 | 105 | std::uint32_t ps_low_degree_ = 0; 106 | 107 | std::string dot_file_; 108 | 109 | std::vector sources_; 110 | }; 111 | -------------------------------------------------------------------------------- /cli/pd_tool/pd_tool.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // STD 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // APSU 12 | #include "apsu/powers.h" 13 | #include "apsu/version.h" 14 | #include "apsu/util/utils.h" 15 | #include "pd_tool/clp.h" 16 | 17 | using namespace std; 18 | using namespace apsu; 19 | 20 | void write_dot(const PowersDag &pd, string dot_file) 21 | { 22 | try { 23 | ofstream fs(dot_file); 24 | fs.exceptions(ios_base::badbit | ios_base::failbit); 25 | fs << pd.to_dot(); 26 | } catch (const ios_base::failure &ex) { 27 | cout << "Failed to write to file: " << ex.what() << endl; 28 | } catch (...) { 29 | cout << "Unknown error writing to file" << endl; 30 | throw; 31 | } 32 | 33 | cout << "DOT was written to file: " << dot_file << endl; 34 | } 35 | 36 | int main(int argc, char **argv) 37 | { 38 | CLP clp( 39 | "pd_tool is a command-line tool for computing the depths of source power configurations.", 40 | to_string(apsu_version)); 41 | clp.parse_args(argc, argv); 42 | 43 | PowersDag pd; 44 | set sources_set(clp.sources().begin(), clp.sources().end()); 45 | set targets_set = util::create_powers_set(clp.ps_low_degree(), clp.bound()); 46 | pd.configure(sources_set, targets_set); 47 | if (pd.is_configured()) { 48 | cout << "Found configuration with depth " << pd.depth() << endl; 49 | } else { 50 | cout << "Failed to configure PowersDag" << endl; 51 | } 52 | if (pd.is_configured() && !clp.dot_file().empty()) { 53 | write_dot(pd, clp.dot_file()); 54 | } 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /cli/receiver/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | target_sources(receiver_cli_osn 5 | PRIVATE 6 | ${CMAKE_CURRENT_LIST_DIR}/receiver.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/receiver_utils.cpp 8 | ) 9 | target_sources(receiver_cli_ddh 10 | PRIVATE 11 | ${CMAKE_CURRENT_LIST_DIR}/receiver.cpp 12 | ${CMAKE_CURRENT_LIST_DIR}/receiver_utils.cpp 13 | ) 14 | -------------------------------------------------------------------------------- /cli/receiver/receiver_utils.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // STD 5 | #include 6 | #include 7 | #include 8 | 9 | // APSU 10 | #include "apsu/log.h" 11 | #include "common_utils.h" 12 | #include "receiver/receiver_utils.h" 13 | 14 | // SEAL 15 | #include "seal/modulus.h" 16 | 17 | using namespace std; 18 | using namespace seal; 19 | using namespace apsu; 20 | 21 | unique_ptr build_psu_params(const CLP &cmd) 22 | { 23 | string params_json; 24 | 25 | try { 26 | throw_if_file_invalid(cmd.params_file()); 27 | fstream input_file(cmd.params_file(), ios_base::in); 28 | 29 | if (!input_file.is_open()) { 30 | APSU_LOG_ERROR("File " << cmd.params_file() << " could not be open for reading."); 31 | throw runtime_error("Could not open params file"); 32 | } 33 | 34 | string line; 35 | while (getline(input_file, line)) { 36 | params_json.append(line); 37 | params_json.append("\n"); 38 | } 39 | 40 | input_file.close(); 41 | } catch (const exception &ex) { 42 | APSU_LOG_ERROR( 43 | "Error trying to read input file " << cmd.params_file() << ": " << ex.what()); 44 | return nullptr; 45 | } 46 | 47 | unique_ptr params; 48 | try { 49 | params = make_unique(PSUParams::Load(params_json)); 50 | } catch (const exception &ex) { 51 | APSU_LOG_ERROR("APSU threw an exception creating PSUParams: " << ex.what()); 52 | return nullptr; 53 | } 54 | 55 | APSU_LOG_INFO( 56 | "PSUParams have false-positive probability 2^(" << params->log2_fpp() 57 | << ") per receiver item"); 58 | 59 | return params; 60 | } 61 | -------------------------------------------------------------------------------- /cli/receiver/receiver_utils.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | 9 | // APSU 10 | #include "apsu/psu_params.h" 11 | #include "receiver/clp.h" 12 | 13 | std::unique_ptr build_psu_params(const CLP &cmd); 14 | -------------------------------------------------------------------------------- /cli/sender/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | target_sources(sender_cli_osn 5 | PRIVATE 6 | ${CMAKE_CURRENT_LIST_DIR}/sender.cpp 7 | ) 8 | target_sources(sender_cli_ddh 9 | PRIVATE 10 | ${CMAKE_CURRENT_LIST_DIR}/sender.cpp 11 | ) 12 | -------------------------------------------------------------------------------- /cli/sender/clp.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | 9 | // Base 10 | #include "base_clp.h" 11 | 12 | /** 13 | Command Line Processor for Receiver. 14 | */ 15 | class CLP : public BaseCLP { 16 | public: 17 | CLP(const std::string &desc, const std::string &version) : BaseCLP(desc, version) 18 | {} 19 | 20 | virtual void add_args() 21 | { 22 | add(net_addr_arg_); 23 | add(net_port_arg_); 24 | add(query_file_arg_); 25 | add(params_file_arg_); 26 | add(out_file_arg_); 27 | } 28 | 29 | virtual void get_args() 30 | { 31 | net_addr_ = net_addr_arg_.getValue(); 32 | net_port_ = net_port_arg_.getValue(); 33 | query_file_ = query_file_arg_.getValue(); 34 | params_file_ = params_file_arg_.getValue(); 35 | output_file_ = out_file_arg_.getValue(); 36 | } 37 | 38 | const std::string &net_addr() const 39 | { 40 | return net_addr_; 41 | } 42 | 43 | int net_port() const 44 | { 45 | return net_port_; 46 | } 47 | 48 | const std::string &query_file() const 49 | { 50 | return query_file_; 51 | } 52 | 53 | const std::string &output_file() const 54 | { 55 | return output_file_; 56 | } 57 | const std::string ¶ms_file() const 58 | { 59 | return params_file_; 60 | } 61 | private: 62 | TCLAP::ValueArg net_addr_arg_ = TCLAP::ValueArg( 63 | "a", "ipAddr", "IP address for a sender endpoint", false, "localhost", "string"); 64 | 65 | TCLAP::ValueArg net_port_arg_ = TCLAP::ValueArg( 66 | "", "port", "TCP port to connect to (default is 1212)", false, 1212, "TCP port"); 67 | 68 | TCLAP::ValueArg query_file_arg_ = TCLAP::ValueArg( 69 | "q", 70 | "queryFile", 71 | "Path to a text file containing query data (one per line)", 72 | false, 73 | "query.csv", 74 | "string"); 75 | 76 | TCLAP::ValueArg out_file_arg_ = TCLAP::ValueArg( 77 | "o", 78 | "outFile", 79 | "Path to a file where intersection result will be written", 80 | false, 81 | "", 82 | "string"); 83 | TCLAP::ValueArg params_file_arg_ = TCLAP::ValueArg( 84 | "p", 85 | "paramsFile", 86 | "Path to a JSON file that specifies APSU parameters; this must be given if --dbFile is " 87 | "specified with a path " 88 | "to a CSV file", 89 | false, 90 | "16M-1024.json", 91 | "string"); 92 | std::string params_file_; 93 | 94 | std::string net_addr_; 95 | 96 | int net_port_; 97 | 98 | std::string query_file_; 99 | 100 | std::string output_file_; 101 | }; 102 | -------------------------------------------------------------------------------- /cmake/APSUConfig.cmake.in: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Exports target APSU::apsu 5 | # 6 | # Creates variables: 7 | # APSU_FOUND : If APSU was found 8 | # APSU_VERSION : the full version number 9 | # APSU_VERSION_MAJOR : the major version number 10 | # APSU_VERSION_MINOR : the minor version number 11 | # APSU_VERSION_PATCH : the patch version number 12 | # APSU_BUILD_TYPE : The build type (e.g., "Release" or "Debug") 13 | # APSU_DEBUG : Set to non-zero value if library is compiled with extra debugging code (very slow!) 14 | # APSU_USE_CXX17 : Set to non-zero value if library is compiled as C++17 instead of C++14 15 | # APSU_USE_LOG4CPLUS : Set to non-zero value if library is compiled with log4cplus for logging 16 | # APSU_USE_ZMQ : Set to non-zero value if library is compiled with ZeroMQ and cppzmq for networking 17 | 18 | @PACKAGE_INIT@ 19 | 20 | include(CMakeFindDependencyMacro) 21 | 22 | macro(apsu_find_dependency dep) 23 | find_dependency(${dep}) 24 | if(NOT ${dep}_FOUND) 25 | if(NOT APSU_FIND_QUIETLY) 26 | message(WARNING "Could not find dependency `${dep}` required by this configuration") 27 | endif() 28 | set(APSU_FOUND FALSE) 29 | return() 30 | endif() 31 | endmacro() 32 | 33 | set(APSU_FOUND FALSE) 34 | 35 | set(APSU_VERSION @APSU_VERSION@) 36 | set(APSU_VERSION_MAJOR @APSU_VERSION_MAJOR@) 37 | set(APSU_VERSION_MINOR @APSU_VERSION_MINOR@) 38 | set(APSU_VERSION_PATCH @APSU_VERSION_PATCH@) 39 | 40 | set(APSU_BUILD_TYPE @CMAKE_BUILD_TYPE@) 41 | set(APSU_DEBUG @APSU_DEBUG@) 42 | set(APSU_USE_CXX17 @APSU_USE_CXX17@) 43 | 44 | apsu_find_dependency(SEAL 3.7 REQUIRED) 45 | apsu_find_dependency(Kuku 2.1 REQUIRED) 46 | apsu_find_dependency(Flatbuffers REQUIRED) 47 | apsu_find_dependency(jsoncpp REQUIRED) 48 | 49 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 50 | set(THREADS_PREFER_PTHREAD_FLAG TRUE) 51 | apsu_find_dependency(Threads REQUIRED) 52 | 53 | set(APSU_USE_LOG4CPLUS @APSU_USE_LOG4CPLUS@) 54 | if(APSU_USE_LOG4CPLUS) 55 | apsu_find_dependency(log4cplus REQUIRED) 56 | endif() 57 | 58 | set(APSU_USE_ZMQ @APSU_USE_ZMQ@) 59 | if(APSU_USE_ZMQ) 60 | apsu_find_dependency(ZeroMQ REQUIRED) 61 | apsu_find_dependency(cppzmq REQUIRED) 62 | endif() 63 | 64 | # Add the current directory to the module search path 65 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) 66 | 67 | include(${CMAKE_CURRENT_LIST_DIR}/APSUTargets.cmake) 68 | 69 | if(TARGET APSU::apsu) 70 | set(APSU_FOUND TRUE) 71 | endif() 72 | 73 | if(APSU_FOUND) 74 | if(NOT APSU_FIND_QUIETLY) 75 | message(STATUS "APSU -> Version ${APSU_VERSION} detected") 76 | endif() 77 | if(APSU_DEBUG AND NOT APSU_FIND_QUIETLY) 78 | message(STATUS "Performance warning: APSU compiled in debug mode") 79 | endif() 80 | if(NOT APSU_FIND_QUIETLY) 81 | message(STATUS "APSU -> Targets available: APSU::apsu") 82 | endif() 83 | else() 84 | if(NOT APSU_FIND_QUIETLY) 85 | message(STATUS "APSU -> NOT FOUND") 86 | endif() 87 | endif() 88 | -------------------------------------------------------------------------------- /cmake/APSUMacros.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Set the C++ language version 5 | macro(apsu_set_language target) 6 | if(APSU_USE_CXX17) 7 | target_compile_features(${target} PUBLIC cxx_std_17) 8 | else() 9 | target_compile_features(${target} PUBLIC cxx_std_14) 10 | endif() 11 | endmacro() 12 | 13 | # Set the VERSION property 14 | macro(apsu_set_version target) 15 | set_target_properties(${target} PROPERTIES VERSION ${APSU_VERSION}) 16 | endmacro() 17 | 18 | # Set the library filename to reflect version 19 | macro(apsu_set_version_filename target) 20 | set_target_properties(${target} PROPERTIES OUTPUT_NAME ${target}-${APSU_VERSION_MAJOR}.${APSU_VERSION_MINOR}) 21 | endmacro() 22 | 23 | # Set the SOVERSION property 24 | macro(apsu_set_soversion target) 25 | set_target_properties(${target} PROPERTIES SOVERSION ${APSU_VERSION_MAJOR}.${APSU_VERSION_MINOR}) 26 | endmacro() 27 | 28 | # Link a thread library 29 | macro(apsu_link_threads target) 30 | # Require thread library 31 | if(NOT TARGET Threads::Threads) 32 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 33 | set(THREADS_PREFER_PTHREAD_FLAG TRUE) 34 | find_package(Threads REQUIRED) 35 | endif() 36 | 37 | # Link Threads 38 | target_link_libraries(${target} PUBLIC Threads::Threads) 39 | endmacro() 40 | 41 | # Include target to given export 42 | macro(apsu_install_target target export) 43 | install(TARGETS ${target} EXPORT ${export} 44 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 45 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 46 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 47 | endmacro() 48 | 49 | # Add secure compile options 50 | macro(apsu_set_secure_compile_options target scope) 51 | if(MSVC) 52 | # Build debug symbols for static analysis tools 53 | target_link_options(${target} ${scope} /DEBUG) 54 | 55 | # Control Flow Guard / Spectre 56 | target_compile_options(${target} ${scope} /guard:cf) 57 | target_compile_options(${target} ${scope} /Qspectre) 58 | target_link_options(${target} ${scope} /guard:cf) 59 | target_link_options(${target} ${scope} /DYNAMICBASE) 60 | endif() 61 | endmacro() 62 | -------------------------------------------------------------------------------- /cmake/CompileSchemaCXX.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | execute_process( 5 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp -o "${APSU_BUILD_DIR}/common/apsu" "${APSU_SOURCE_DIR}/common/apsu/psu_params.fbs" 6 | OUTPUT_QUIET 7 | RESULT_VARIABLE result) 8 | if(result) 9 | message(FATAL_ERROR "flatc failed (${result})") 10 | endif() 11 | 12 | execute_process( 13 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp -o "${APSU_BUILD_DIR}/common/apsu/network" "${APSU_SOURCE_DIR}/common/apsu/network/ciphertext.fbs" 14 | OUTPUT_QUIET 15 | RESULT_VARIABLE result) 16 | if(result) 17 | message(FATAL_ERROR "flatc failed (${result})") 18 | endif() 19 | 20 | execute_process( 21 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp --include-prefix "apsu/network/" -o "${APSU_BUILD_DIR}/common/apsu/network" "${APSU_SOURCE_DIR}/common/apsu/network/rop.fbs" 22 | OUTPUT_QUIET 23 | RESULT_VARIABLE result) 24 | if(result) 25 | message(FATAL_ERROR "flatc failed (${result})") 26 | endif() 27 | 28 | execute_process( 29 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp -o "${APSU_BUILD_DIR}/common/apsu/network" "${APSU_SOURCE_DIR}/common/apsu/network/rop_header.fbs" 30 | OUTPUT_QUIET 31 | RESULT_VARIABLE result) 32 | if(result) 33 | message(FATAL_ERROR "flatc failed (${result})") 34 | endif() 35 | 36 | execute_process( 37 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp --include-prefix "apsu/network/" -o "${APSU_BUILD_DIR}/common/apsu/network" "${APSU_SOURCE_DIR}/common/apsu/network/rop_response.fbs" 38 | OUTPUT_QUIET 39 | RESULT_VARIABLE result) 40 | if(result) 41 | message(FATAL_ERROR "flatc failed (${result})") 42 | endif() 43 | 44 | execute_process( 45 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp --include-prefix "apsu/network/" -o "${APSU_BUILD_DIR}/common/apsu/network" "${APSU_SOURCE_DIR}/common/apsu/network/result_package.fbs" 46 | OUTPUT_QUIET 47 | RESULT_VARIABLE result) 48 | if(result) 49 | message(FATAL_ERROR "flatc failed (${result})") 50 | endif() 51 | 52 | execute_process( 53 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp --include-prefix "apsu/" -o "${APSU_BUILD_DIR}/receiver/apsu" "${APSU_SOURCE_DIR}/receiver/apsu/bin_bundle.fbs" 54 | OUTPUT_QUIET 55 | RESULT_VARIABLE result) 56 | if(result) 57 | message(FATAL_ERROR "flatc failed (${result})") 58 | endif() 59 | 60 | execute_process( 61 | COMMAND ${FLATBUFFERS_FLATC_PATH} --cpp --include-prefix "apsu/" -I "${APSU_SOURCE_DIR}/common/apsu" -o "${APSU_BUILD_DIR}/receiver/apsu" "${APSU_SOURCE_DIR}/receiver/apsu/receiver_db.fbs" 62 | OUTPUT_QUIET 63 | RESULT_VARIABLE result) 64 | if(result) 65 | message(FATAL_ERROR "flatc failed (${result})") 66 | endif() 67 | -------------------------------------------------------------------------------- /cmake/DetectArch.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Copyright (c) Microsoft Corporation. All rights reserved. 3 | # Licensed under the MIT license. 4 | 5 | set(CMAKE_REQUIRED_QUIET_OLD ${CMAKE_REQUIRED_QUIET}) 6 | set(CMAKE_REQUIRED_QUIET ON) 7 | if(MSVC) 8 | check_cxx_source_runs(" 9 | #if defined(_M_ARM64) 10 | int main() { 11 | return 0; 12 | } 13 | #else 14 | #error 15 | #endif 16 | " 17 | APSU_FOURQ_ARM64 18 | ) 19 | check_cxx_source_runs(" 20 | #if defined(_M_AMD64) 21 | int main() { 22 | return 0; 23 | } 24 | #else 25 | #error 26 | #endif 27 | " 28 | APSU_FOURQ_AMD64 29 | ) 30 | else() 31 | check_cxx_source_runs(" 32 | #if defined(__aarch64__) 33 | int main() { 34 | return 0; 35 | } 36 | #else 37 | #error 38 | #endif 39 | " 40 | APSU_FOURQ_ARM64 41 | ) 42 | check_cxx_source_runs(" 43 | #if defined(__amd64) 44 | int main() { 45 | return 0; 46 | } 47 | #else 48 | #error 49 | #endif 50 | " 51 | APSU_FOURQ_AMD64 52 | ) 53 | endif() 54 | set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_OLD}) 55 | -------------------------------------------------------------------------------- /cmake/EnableCXXCompilerFlags.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | include(CheckCXXCompilerFlag) 5 | 6 | # For easier adding of CXX compiler flags 7 | function(seal_enable_cxx_compiler_flag_if_supported flag) 8 | string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_already_set) 9 | if(flag_already_set EQUAL -1) 10 | message(STATUS "Adding CXX compiler flag: ${flag} ...") 11 | check_cxx_compiler_flag("${flag}" flag_supported) 12 | if(flag_supported) 13 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) 14 | endif() 15 | unset(flag_supported CACHE) 16 | endif() 17 | endfunction() 18 | 19 | if(NOT MSVC AND SEAL_DEBUG) 20 | seal_enable_cxx_compiler_flag_if_supported("-Wall") 21 | seal_enable_cxx_compiler_flag_if_supported("-Wextra") 22 | seal_enable_cxx_compiler_flag_if_supported("-Wconversion") 23 | seal_enable_cxx_compiler_flag_if_supported("-Wshadow") 24 | seal_enable_cxx_compiler_flag_if_supported("-pedantic") 25 | endif() 26 | -------------------------------------------------------------------------------- /cmake/FindAVX.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Check for the presence of AVX and figure out the flags to use for it. 5 | macro(check_for_avx target) 6 | set(CMAKE_REQUIRED_QUIET_OLD ${CMAKE_REQUIRED_QUIET}) 7 | set(CMAKE_REQUIRED_QUIET ON) 8 | include(CheckCXXSourceRuns) 9 | set(CMAKE_REQUIRED_FLAGS) 10 | 11 | # Check AVX 12 | if(MSVC AND MSVC_VERSION GREATER_EQUAL 1600) 13 | set(CMAKE_REQUIRED_FLAGS "/arch:AVX") 14 | elseif(NOT MSVC) 15 | set(CMAKE_REQUIRED_FLAGS "-mavx") 16 | endif() 17 | 18 | check_cxx_source_runs(" 19 | #include 20 | int main() 21 | { 22 | __m256 a, b, c; 23 | const float src[8] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; 24 | float dst[8]; 25 | a = _mm256_loadu_ps( src ); 26 | b = _mm256_loadu_ps( src ); 27 | c = _mm256_add_ps( a, b ); 28 | _mm256_storeu_ps( dst, c ); 29 | for( int i = 0; i < 8; i++ ){ 30 | if( ( src[i] + src[i] ) != dst[i] ){ 31 | return -1; 32 | } 33 | } 34 | return 0; 35 | }" 36 | HAVE_AVX_EXTENSIONS) 37 | 38 | # Check AVX2 39 | if(MSVC AND MSVC_VERSION GREATER_EQUAL 1800) 40 | set(CMAKE_REQUIRED_FLAGS "/arch:AVX2") 41 | elseif(NOT MSVC) 42 | set(CMAKE_REQUIRED_FLAGS "-mavx2") 43 | endif() 44 | 45 | check_cxx_source_runs(" 46 | #include 47 | int main() 48 | { 49 | __m256i a, b, c; 50 | const int src[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 51 | int dst[8]; 52 | a = _mm256_loadu_si256( (__m256i*)src ); 53 | b = _mm256_loadu_si256( (__m256i*)src ); 54 | c = _mm256_add_epi32( a, b ); 55 | _mm256_storeu_si256( (__m256i*)dst, c ); 56 | for( int i = 0; i < 8; i++ ){ 57 | if( ( src[i] + src[i] ) != dst[i] ){ 58 | return -1; 59 | } 60 | } 61 | return 0; 62 | }" 63 | HAVE_AVX2_EXTENSIONS) 64 | 65 | # Set Flags 66 | if(MSVC) 67 | if(HAVE_AVX2_EXTENSIONS AND MSVC_VERSION GREATER_EQUAL 1800) 68 | target_compile_options(${target} PUBLIC /arch:AVX2) 69 | elseif(HAVE_AVX_EXTENSIONS AND MSVC_VERSION GREATER_EQUAL 1600) 70 | target_compile_options(${target} PUBLIC /arch:AVX) 71 | endif() 72 | else() 73 | if(HAVE_AVX2_EXTENSIONS) 74 | target_compile_options(${target} PUBLIC -mavx2) 75 | elseif(HAVE_AVX_EXTENSIONS) 76 | target_compile_options(${target} PUBLIC -mavx) 77 | endif() 78 | endif() 79 | set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_OLD}) 80 | endmacro() 81 | -------------------------------------------------------------------------------- /common/apsu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/item.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/log.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/powers.cpp 9 | ${CMAKE_CURRENT_LIST_DIR}/psu_params.cpp 10 | ${CMAKE_CURRENT_LIST_DIR}/thread_pool_mgr.cpp 11 | ${CMAKE_CURRENT_LIST_DIR}/version.cpp 12 | ) 13 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 14 | ${CMAKE_CURRENT_LIST_DIR}/item.cpp 15 | ${CMAKE_CURRENT_LIST_DIR}/log.cpp 16 | ${CMAKE_CURRENT_LIST_DIR}/powers.cpp 17 | ${CMAKE_CURRENT_LIST_DIR}/psu_params.cpp 18 | ${CMAKE_CURRENT_LIST_DIR}/thread_pool_mgr.cpp 19 | ${CMAKE_CURRENT_LIST_DIR}/version.cpp 20 | ) 21 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 22 | ${CMAKE_CURRENT_LIST_DIR}/item.cpp 23 | ${CMAKE_CURRENT_LIST_DIR}/log.cpp 24 | ${CMAKE_CURRENT_LIST_DIR}/powers.cpp 25 | ${CMAKE_CURRENT_LIST_DIR}/psu_params.cpp 26 | ${CMAKE_CURRENT_LIST_DIR}/thread_pool_mgr.cpp 27 | ${CMAKE_CURRENT_LIST_DIR}/version.cpp 28 | ) 29 | # Add header files for installation 30 | install( 31 | FILES 32 | ${CMAKE_CURRENT_LIST_DIR}/apsu.h 33 | ${CMAKE_CURRENT_LIST_DIR}/crypto_context.h 34 | ${CMAKE_CURRENT_LIST_DIR}/item.h 35 | ${CMAKE_CURRENT_LIST_DIR}/log.h 36 | ${CMAKE_CURRENT_LIST_DIR}/powers.h 37 | ${CMAKE_CURRENT_LIST_DIR}/psu_params.h 38 | ${CMAKE_CURRENT_LIST_DIR}/requests.h 39 | ${CMAKE_CURRENT_LIST_DIR}/responses.h 40 | ${CMAKE_CURRENT_LIST_DIR}/seal_object.h 41 | ${CMAKE_CURRENT_LIST_DIR}/thread_pool_mgr.h 42 | ${CMAKE_CURRENT_LIST_DIR}/version.h 43 | DESTINATION 44 | ${APSU_INCLUDES_INSTALL_DIR}/apsu 45 | ) 46 | 47 | add_subdirectory(fourq) 48 | add_subdirectory(network) 49 | add_subdirectory(oprf) 50 | add_subdirectory(util) 51 | add_subdirectory(permute) 52 | add_subdirectory(peqt) 53 | add_subdirectory(ot) 54 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 55 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 56 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 57 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} PARENT_SCOPE) 58 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} PARENT_SCOPE) 59 | set(APSU_SOURCE_FILES_SENDER_DDH ${APSU_SOURCE_FILES_SENDER_DDH} PARENT_SCOPE) 60 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} PARENT_SCOPE) 61 | -------------------------------------------------------------------------------- /common/apsu/apsu.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | -------------------------------------------------------------------------------- /common/apsu/config.h.in: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #define APSU_VERSION "@APSU_VERSION@" 7 | #define APSU_VERSION_MAJOR @APSU_VERSION_MAJOR@ 8 | #define APSU_VERSION_MINOR @APSU_VERSION_MINOR@ 9 | #define APSU_VERSION_PATCH @APSU_VERSION_PATCH@ 10 | 11 | #cmakedefine APSU_DEBUG 12 | #cmakedefine APSU_USE_CXX17 13 | #cmakedefine APSU_USE_LOG4CPLUS 14 | #cmakedefine APSU_USE_ZMQ 15 | -------------------------------------------------------------------------------- /common/apsu/fourq/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/crypto_util.c 7 | ${CMAKE_CURRENT_LIST_DIR}/eccp2_core.c 8 | ${CMAKE_CURRENT_LIST_DIR}/eccp2_no_endo.c 9 | ${CMAKE_CURRENT_LIST_DIR}/eccp2.c 10 | ${CMAKE_CURRENT_LIST_DIR}/hash_to_curve.c 11 | ${CMAKE_CURRENT_LIST_DIR}/random.c 12 | ) 13 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 14 | ${CMAKE_CURRENT_LIST_DIR}/crypto_util.c 15 | ${CMAKE_CURRENT_LIST_DIR}/eccp2_core.c 16 | ${CMAKE_CURRENT_LIST_DIR}/eccp2_no_endo.c 17 | ${CMAKE_CURRENT_LIST_DIR}/eccp2.c 18 | ${CMAKE_CURRENT_LIST_DIR}/hash_to_curve.c 19 | ${CMAKE_CURRENT_LIST_DIR}/random.c 20 | ) 21 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 22 | ${CMAKE_CURRENT_LIST_DIR}/crypto_util.c 23 | ${CMAKE_CURRENT_LIST_DIR}/eccp2_core.c 24 | ${CMAKE_CURRENT_LIST_DIR}/eccp2_no_endo.c 25 | ${CMAKE_CURRENT_LIST_DIR}/eccp2.c 26 | ${CMAKE_CURRENT_LIST_DIR}/hash_to_curve.c 27 | ${CMAKE_CURRENT_LIST_DIR}/random.c 28 | ) 29 | 30 | # Add header files for installation 31 | install( 32 | FILES 33 | ${CMAKE_CURRENT_LIST_DIR}/FourQ_api.h 34 | ${CMAKE_CURRENT_LIST_DIR}/FourQ_internal.h 35 | ${CMAKE_CURRENT_LIST_DIR}/FourQ_params.h 36 | ${CMAKE_CURRENT_LIST_DIR}/FourQ_tables.h 37 | ${CMAKE_CURRENT_LIST_DIR}/FourQ.h 38 | ${CMAKE_CURRENT_LIST_DIR}/table_lookup.h 39 | DESTINATION 40 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/fourq 41 | ) 42 | 43 | if(APSU_FOURQ_AMD64) 44 | add_subdirectory(amd64) 45 | elseif(APSU_FOURQ_ARM64) 46 | add_subdirectory(arm64) 47 | else() 48 | add_subdirectory(generic) 49 | endif() 50 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 51 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 52 | 53 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 54 | 55 | -------------------------------------------------------------------------------- /common/apsu/fourq/FourQ_params.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************************** 2 | * FourQlib: a high-performance crypto library based on the elliptic curve FourQ 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | * Abstract: FourQ's curve parameters 7 | * 8 | * This code is based on the paper "FourQ: four-dimensional decompositions on a 9 | * Q-curve over the Mersenne prime" by Craig Costello and Patrick Longa, in Advances 10 | * in Cryptology - ASIACRYPT, 2015. 11 | * Preprint available at http://eprint.iacr.org/2015/565. 12 | ************************************************************************************/ 13 | 14 | #pragma once 15 | #ifndef __FOURQ_PARAMS_H__ 16 | #define __FOURQ_PARAMS_H__ 17 | 18 | #include "apsu/fourq/FourQ_internal.h" 19 | 20 | // Encoding of field elements, elements over Z_r and elements over GF(p^2): 21 | // ----------------------------------------------------------------------- 22 | // Elements over GF(p) and Z_r are encoded with the least significant digit located in the leftmost 23 | // position (i.e., little endian format). Elements (a+b*i) over GF(p^2), where a and b are defined 24 | // over GF(p), are encoded as a||b, with a in the least significant position. 25 | 26 | static const uint64_t PARAMETER_d[4] = { 27 | 0x0000000000000142, 0x00000000000000E4, 0xB3821488F1FC0C8D, 0x5E472F846657E0FC 28 | }; 29 | static const uint64_t GENERATOR_x[4] = { 30 | 0x286592AD7B3833AA, 0x1A3472237C2FB305, 0x96869FB360AC77F6, 0x1E1F553F2878AA9C 31 | }; 32 | static const uint64_t GENERATOR_y[4] = { 33 | 0xB924A2462BCBB287, 0x0E3FEE9BA120785A, 0x49A7C344844C8B5C, 0x6E1C4AF8630E0242 34 | }; 35 | static const uint64_t curve_order[4] = { 36 | 0x2FB2540EC7768CE7, 0xDFBD004DFE0F7999, 0xF05397829CBC14E5, 0x0029CBC14E5E0A72 37 | }; 38 | static const uint64_t Montgomery_Rprime[4] = { 39 | 0xC81DB8795FF3D621, 0x173EA5AAEA6B387D, 0x3D01B7C72136F61C, 0x0006A5F16AC8F9D3 40 | }; 41 | static const uint64_t Montgomery_rprime[4] = { 42 | 0xE12FE5F079BC3929, 0xD75E78B8D1FCDCF3, 0xBCE409ED76B5DB21, 0xF32702FDAFC1C074 43 | }; 44 | 45 | // Constants for hash to FourQ function 46 | 47 | #if (RADIX == 32) 48 | static felm_t con1 = { 6651107, 0, 4290264256, 2147483647 }; 49 | static felm_t con2 = { 1725590130, 1719979744, 2225079900, 707200452 }; 50 | static felm_t b0 = { 3738038324, 2664081113, 587564626, 1252475115 }; 51 | static felm_t b1 = { 17, 0, 4294967284, 2147483647 }; 52 | static felm_t A0 = { 1289, 0, 4294966384, 2147483647 }; 53 | static felm_t A1 = { 1007904792, 2866591091, 4136083791, 1668973403 }; 54 | #elif (RADIX == 64) 55 | static felm_t con1 = { 6651107ULL, 9223372036850072768ULL }; 56 | static felm_t con2 = { 7387256751988042354ULL, 3037402815281497692ULL }; 57 | static felm_t b0 = { 11442141257964318772ULL, 5379339658566403666ULL }; 58 | static felm_t b1 = { 17ULL, 9223372036854775796ULL }; 59 | static felm_t A0 = { 1289ULL, 9223372036854774896ULL }; 60 | static felm_t A1 = { 12311914987857864728ULL, 7168186187914912079ULL }; 61 | #endif 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /common/apsu/fourq/amd64/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | if(APSU_FOURQ_USE_ASM) 6 | if(HAVE_AVX2_EXTENSIONS) 7 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 8 | ${CMAKE_CURRENT_LIST_DIR}/consts.c 9 | ${CMAKE_CURRENT_LIST_DIR}/fp2_1271_AVX2.S 10 | ) 11 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 12 | ${CMAKE_CURRENT_LIST_DIR}/consts.c 13 | ${CMAKE_CURRENT_LIST_DIR}/fp2_1271_AVX2.S 14 | ) 15 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 16 | ${CMAKE_CURRENT_LIST_DIR}/consts.c 17 | ${CMAKE_CURRENT_LIST_DIR}/fp2_1271_AVX2.S 18 | ) 19 | else() 20 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 21 | ${CMAKE_CURRENT_LIST_DIR}/fp2_1271.S 22 | ) 23 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 24 | ${CMAKE_CURRENT_LIST_DIR}/fp2_1271.S 25 | ) 26 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 27 | ${CMAKE_CURRENT_LIST_DIR}/fp2_1271.S 28 | ) 29 | endif() 30 | endif() 31 | 32 | # Add header files for installation 33 | install( 34 | FILES 35 | ${CMAKE_CURRENT_LIST_DIR}/fp_x64.h 36 | DESTINATION 37 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/fourq/amd64 38 | ) 39 | 40 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 41 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 42 | 43 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 44 | 45 | -------------------------------------------------------------------------------- /common/apsu/fourq/amd64/consts.c: -------------------------------------------------------------------------------- 1 | /*********************************************************************************** 2 | * FourQlib: a high-performance crypto library based on the elliptic curve FourQ 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | * Abstract: constants for assembly implementation 7 | ************************************************************************************/ 8 | 9 | #include 10 | 11 | uint32_t ONEx8[8] = { 1, 1, 1, 1, 1, 1, 1, 1 }; 12 | uint32_t TWOx8[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; 13 | uint64_t PRIME1271[4] = { 14 | 0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF 15 | }; 16 | -------------------------------------------------------------------------------- /common/apsu/fourq/cgmanifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "Registrations": [ 3 | { 4 | "component": { 5 | "type": "git", 6 | "git": { 7 | "repositoryUrl": "https://github.com/microsoft/FourQlib", 8 | "commitHash": "ff61f680505c98c98e33387962223ce0b5e620bc" 9 | } 10 | } 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /common/apsu/fourq/generic/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Add header files for installation 5 | install( 6 | FILES 7 | ${CMAKE_CURRENT_LIST_DIR}/fp.h 8 | DESTINATION 9 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/fourq/generic 10 | ) 11 | 12 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 13 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 14 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 15 | -------------------------------------------------------------------------------- /common/apsu/fourq/random.c: -------------------------------------------------------------------------------- 1 | /*********************************************************************************** 2 | * FourQlib: a high-performance crypto library based on the elliptic curve FourQ 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | * Abstract: pseudo-random function 7 | ************************************************************************************/ 8 | 9 | #include "apsu/fourq/random.h" 10 | #include 11 | #include 12 | #if defined(__WINDOWS__) 13 | #include 14 | #include 15 | #define RTL_GENRANDOM "SystemFunction036" 16 | NTSTATUS last_bcrypt_error = 0; 17 | #elif defined(__LINUX__) 18 | #include 19 | #include 20 | static int lock = -1; 21 | #endif 22 | 23 | #ifdef __LINUX__ 24 | static __inline void delay(unsigned int count) 25 | { 26 | while (count--) { 27 | } 28 | } 29 | #endif 30 | 31 | int random_bytes(unsigned char *random_array, unsigned int nbytes) 32 | { // Generation of "nbytes" of random values 33 | 34 | #if defined(__WINDOWS__) 35 | if (BCRYPT_SUCCESS(last_bcrypt_error)) { 36 | NTSTATUS status = 37 | BCryptGenRandom(NULL, random_array, nbytes, BCRYPT_USE_SYSTEM_PREFERRED_RNG); 38 | 39 | if (BCRYPT_SUCCESS(status)) { 40 | return true; 41 | } 42 | last_bcrypt_error = status; 43 | } 44 | 45 | HMODULE hAdvApi = LoadLibraryA("ADVAPI32.DLL"); 46 | if (!hAdvApi) { 47 | return false; 48 | } 49 | 50 | BOOLEAN(APIENTRY * RtlGenRandom) 51 | (void *, ULONG) = (BOOLEAN(APIENTRY *)(void *, ULONG))GetProcAddress(hAdvApi, RTL_GENRANDOM); 52 | 53 | BOOLEAN genrand_result = FALSE; 54 | if (RtlGenRandom) { 55 | genrand_result = RtlGenRandom(random_array, nbytes); 56 | } 57 | 58 | FreeLibrary(hAdvApi); 59 | 60 | if (!genrand_result) { 61 | return false; 62 | } 63 | 64 | #elif defined(__LINUX__) 65 | int r, n = nbytes, count = 0; 66 | 67 | if (lock == -1) { 68 | do { 69 | lock = open("/dev/urandom", O_RDONLY); 70 | if (lock == -1) { 71 | delay(0xFFFFF); 72 | } 73 | } while (lock == -1); 74 | } 75 | 76 | while (n > 0) { 77 | do { 78 | r = read(lock, random_array + count, n); 79 | if (r == -1) { 80 | delay(0xFFFF); 81 | } 82 | } while (r == -1); 83 | count += r; 84 | n -= r; 85 | } 86 | #endif 87 | 88 | return true; 89 | } 90 | -------------------------------------------------------------------------------- /common/apsu/fourq/random.h: -------------------------------------------------------------------------------- 1 | #ifndef __RANDOM_H__ 2 | #define __RANDOM_H__ 3 | 4 | // For C++ 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | // Generate random bytes and output the result to random_array 10 | int random_bytes(unsigned char *random_array, unsigned int nbytes); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /common/apsu/item.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // STD 5 | #include 6 | #include 7 | 8 | // APSU 9 | #include "apsu/item.h" 10 | #include "apsu/util/utils.h" 11 | 12 | // SEAL 13 | #include "seal/util/blake2.h" 14 | 15 | using namespace std; 16 | 17 | namespace apsu { 18 | void Item::hash_to_value(const void *in, size_t size) 19 | { 20 | APSU_blake2b(value_.data(), sizeof(value_), in, size, nullptr, 0); 21 | } 22 | 23 | Bitstring Item::to_bitstring(uint32_t item_bit_count) const 24 | { 25 | vector bytes; 26 | bytes.reserve(sizeof(value_type)); 27 | copy(value_.cbegin(), value_.cend(), back_inserter(bytes)); 28 | return { move(bytes), item_bit_count }; 29 | } 30 | 31 | string Item::to_string() const 32 | { 33 | return util::to_string(get_as()); 34 | } 35 | } // namespace apsu 36 | -------------------------------------------------------------------------------- /common/apsu/log.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | 11 | #define APSU 1 12 | #define ARBITARY 0 13 | #define CARD 0 14 | #define CARDSUM 0 15 | namespace apsu { 16 | /** 17 | Class that provides the logging interface. 18 | */ 19 | class Log { 20 | public: 21 | /** 22 | Supported log levels 23 | */ 24 | enum class Level : int { all, debug, info, warning, error, off }; 25 | 26 | /** 27 | This class is only to be used through its static methods. 28 | */ 29 | Log() = delete; 30 | 31 | static void SetLogLevel(Level level); 32 | 33 | static Level GetLogLevel(); 34 | 35 | static void SetLogLevel(const std::string &level); 36 | 37 | static void SetLogFile(const std::string &file); 38 | 39 | static void SetConsoleDisabled(bool console_disabled); 40 | 41 | static void ConfigureIfNeeded(); 42 | 43 | static void Terminate(); 44 | 45 | static void DoLog(std::string msg, Level msg_level); 46 | 47 | private: 48 | static void Configure(); 49 | 50 | static Level log_level_; 51 | }; // class Log 52 | } // namespace apsu 53 | 54 | #define APSU_INTERNAL_CHECK_LOG_LEVEL(log_level) \ 55 | apsu::Log::ConfigureIfNeeded(); \ 56 | if (apsu::Log::GetLogLevel() > log_level) { \ 57 | break; \ 58 | } 59 | 60 | #define APSU_INTERNAL_DO_LOG(msg, msg_level) \ 61 | std::stringstream log_ss; \ 62 | log_ss << msg; \ 63 | std::string log_str = log_ss.str(); \ 64 | apsu::Log::DoLog(log_str, msg_level); 65 | 66 | #define APSU_LOG_DEBUG(msg) \ 67 | do { \ 68 | APSU_INTERNAL_CHECK_LOG_LEVEL(apsu::Log::Level::debug); \ 69 | APSU_INTERNAL_DO_LOG(msg, apsu::Log::Level::debug); \ 70 | } while (0); 71 | 72 | #define APSU_LOG_INFO(msg) \ 73 | do { \ 74 | APSU_INTERNAL_CHECK_LOG_LEVEL(apsu::Log::Level::info); \ 75 | APSU_INTERNAL_DO_LOG(msg, apsu::Log::Level::info); \ 76 | } while (0); 77 | 78 | #define APSU_LOG_WARNING(msg) \ 79 | do { \ 80 | APSU_INTERNAL_CHECK_LOG_LEVEL(apsu::Log::Level::warning); \ 81 | APSU_INTERNAL_DO_LOG(msg, apsu::Log::Level::warning); \ 82 | } while (0); 83 | 84 | #define APSU_LOG_ERROR(msg) \ 85 | do { \ 86 | APSU_INTERNAL_CHECK_LOG_LEVEL(apsu::Log::Level::error); \ 87 | APSU_INTERNAL_DO_LOG(msg, apsu::Log::Level::error); \ 88 | } while (0); 89 | -------------------------------------------------------------------------------- /common/apsu/network/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/result_package.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation_response.cpp 9 | ${CMAKE_CURRENT_LIST_DIR}/stream_channel.cpp 10 | ) 11 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 12 | ${CMAKE_CURRENT_LIST_DIR}/result_package.cpp 13 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation.cpp 14 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation_response.cpp 15 | ${CMAKE_CURRENT_LIST_DIR}/stream_channel.cpp 16 | ) 17 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 18 | ${CMAKE_CURRENT_LIST_DIR}/result_package.cpp 19 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation.cpp 20 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation_response.cpp 21 | ${CMAKE_CURRENT_LIST_DIR}/stream_channel.cpp 22 | ) 23 | # Add header files for installation 24 | install( 25 | FILES 26 | ${CMAKE_CURRENT_LIST_DIR}/channel.h 27 | ${CMAKE_CURRENT_LIST_DIR}/network_channel.h 28 | ${CMAKE_CURRENT_LIST_DIR}/result_package.h 29 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation.h 30 | ${CMAKE_CURRENT_LIST_DIR}/receiver_operation_response.h 31 | ${CMAKE_CURRENT_LIST_DIR}/stream_channel.h 32 | DESTINATION 33 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/network 34 | ) 35 | 36 | if(APSU_USE_ZMQ) 37 | add_subdirectory(zmq) 38 | endif() 39 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 40 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 41 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 42 | -------------------------------------------------------------------------------- /common/apsu/network/ciphertext.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | namespace apsu.network.fbs; 5 | 6 | table Ciphertext { 7 | data:[ubyte] (required); 8 | } 9 | 10 | root_type Ciphertext; 11 | -------------------------------------------------------------------------------- /common/apsu/network/network_channel.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // APSU 7 | #include "apsu/network/channel.h" 8 | 9 | namespace apsu { 10 | namespace network { 11 | class NetworkChannel : public Channel {}; 12 | } // namespace network 13 | } // namespace apsu 14 | -------------------------------------------------------------------------------- /common/apsu/network/plainresult.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/real-world-cryptography/APSU/b2f090fa48ce6121ad174a0291e538a3f0c493db/common/apsu/network/plainresult.h -------------------------------------------------------------------------------- /common/apsu/network/result_package.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | include "ciphertext.fbs"; 5 | 6 | namespace apsu.network.fbs; 7 | 8 | table ResultPackage { 9 | bundle_idx:uint32; 10 | cache_idx:uint32; 11 | psu_result:Ciphertext (required); 12 | label_byte_count:uint32; 13 | nonce_byte_count:uint32; 14 | label_result:[Ciphertext]; 15 | } 16 | 17 | root_type ResultPackage; 18 | -------------------------------------------------------------------------------- /common/apsu/network/result_package.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // SEAL 14 | #include "seal/ciphertext.h" 15 | #include "seal/context.h" 16 | #include "seal/util/defines.h" 17 | 18 | // APSU 19 | #include "apsu/crypto_context.h" 20 | #include "apsu/seal_object.h" 21 | 22 | namespace apsu { 23 | namespace network { 24 | /** 25 | Stores a decrypted and decoded PSU response and optionally a labeled PSU response. 26 | */ 27 | struct PlainResultPackage { 28 | std::uint32_t bundle_idx; 29 | 30 | std::vector psu_result; 31 | 32 | std::uint32_t label_byte_count; 33 | 34 | std::uint32_t nonce_byte_count; 35 | 36 | std::vector> label_result; 37 | }; 38 | 39 | /** 40 | Stores a PSU response and optionally labeled PSU response ciphertexts. 41 | */ 42 | class ResultPackage { 43 | public: 44 | /** 45 | Writes the ResultPackage to a stream. 46 | */ 47 | std::size_t save(std::ostream &out) const; 48 | 49 | /** 50 | Reads the ResultPackage from a stream. 51 | */ 52 | std::size_t load(std::istream &in, std::shared_ptr context); 53 | 54 | PlainResultPackage extract(const CryptoContext &crypto_context); 55 | 56 | std::uint32_t bundle_idx; 57 | 58 | std::uint32_t cache_idx; 59 | 60 | seal::compr_mode_type compr_mode = seal::Serialization::compr_mode_default; 61 | 62 | SEALObject psu_result; 63 | 64 | std::uint32_t label_byte_count; 65 | 66 | std::uint32_t nonce_byte_count; 67 | 68 | std::vector> label_result; 69 | }; // struct ResultPackage 70 | } // namespace network 71 | } // namespace apsu 72 | -------------------------------------------------------------------------------- /common/apsu/network/rop.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | include "ciphertext.fbs"; 5 | 6 | namespace apsu.network.fbs; 7 | 8 | table ParmsRequest { 9 | } 10 | 11 | table OPRFRequest { 12 | data:[ubyte] (required); 13 | } 14 | 15 | table QueryRequestPart { 16 | exponent:uint32; 17 | cts:[Ciphertext] (required); 18 | } 19 | 20 | table QueryRequest { 21 | compression_type:ubyte; 22 | relin_keys:[ubyte]; 23 | query:[QueryRequestPart] (required); 24 | } 25 | 26 | table plainResponse{ 27 | bundle_idx: uint32; 28 | psu_result: [uint64] (required); 29 | cache_idx:uint32; 30 | 31 | } 32 | 33 | union Request { ParmsRequest, OPRFRequest, QueryRequest, plainResponse } 34 | 35 | table ReceiverOperation { 36 | request:Request (required); 37 | } 38 | 39 | root_type ReceiverOperation; 40 | -------------------------------------------------------------------------------- /common/apsu/network/rop_header.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | namespace apsu.network.fbs; 5 | 6 | enum ReceiverOperationType:uint32 { rop_unknown = 0, rop_parms = 1, rop_oprf = 2, rop_query = 3 ,rop_response = 4} 7 | 8 | table ReceiverOperationHeader { 9 | version:uint32; 10 | type:ReceiverOperationType = rop_unknown; 11 | } 12 | 13 | root_type ReceiverOperationHeader; 14 | -------------------------------------------------------------------------------- /common/apsu/network/rop_response.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | namespace apsu.network.fbs; 5 | 6 | table ParmsResponse { 7 | data:[ubyte]; 8 | } 9 | 10 | table OPRFResponse { 11 | data:[ubyte] (required); 12 | } 13 | 14 | table QueryResponse { 15 | package_count:uint32; 16 | alpha_max_cache_count:uint32; 17 | } 18 | 19 | union Response { ParmsResponse, OPRFResponse, QueryResponse } 20 | 21 | table ReceiverOperationResponse { 22 | response:Response (required); 23 | } 24 | 25 | root_type ReceiverOperationResponse; 26 | -------------------------------------------------------------------------------- /common/apsu/network/zmq/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/zmq_channel.cpp 7 | ) 8 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 9 | ${CMAKE_CURRENT_LIST_DIR}/zmq_channel.cpp 10 | ) 11 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 12 | ${CMAKE_CURRENT_LIST_DIR}/zmq_channel.cpp 13 | ) 14 | 15 | # Add header files for installation 16 | install( 17 | FILES 18 | ${CMAKE_CURRENT_LIST_DIR}/zmq_channel.h 19 | DESTINATION 20 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/network/zmq 21 | ) 22 | 23 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 24 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 25 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 26 | -------------------------------------------------------------------------------- /common/apsu/oprf/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/ecpoint.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.cpp 9 | ) 10 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 11 | ${CMAKE_CURRENT_LIST_DIR}/ecpoint.cpp 12 | ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.cpp 13 | ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.cpp 14 | ) 15 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 16 | ${CMAKE_CURRENT_LIST_DIR}/ecpoint.cpp 17 | ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.cpp 18 | ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.cpp 19 | ) 20 | 21 | # Add header files for installation 22 | install( 23 | FILES 24 | ${CMAKE_CURRENT_LIST_DIR}/ecpoint.h 25 | ${CMAKE_CURRENT_LIST_DIR}/oprf_common.h 26 | ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.h 27 | ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.h 28 | DESTINATION 29 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/oprf 30 | ) 31 | 32 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 33 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 34 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 35 | 36 | 37 | -------------------------------------------------------------------------------- /common/apsu/oprf/ecpoint.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // GSL 14 | #include "gsl/span" 15 | 16 | // FourQ 17 | #include "apsu/fourq/FourQ.h" 18 | 19 | namespace apsu { 20 | namespace oprf { 21 | class ECPoint { 22 | public: 23 | static constexpr std::size_t save_size = sizeof(f2elm_t); 24 | static constexpr std::size_t point_size = sizeof(point_t); 25 | static constexpr std::size_t order_size = sizeof(digit_t) * NWORDS_ORDER; 26 | 27 | using scalar_type = std::array; 28 | using scalar_const_type = const scalar_type; 29 | 30 | using scalar_span_type = gsl::span; 31 | using scalar_span_const_type = gsl::span; 32 | 33 | using input_span_const_type = gsl::span; 34 | 35 | using point_save_span_type = gsl::span; 36 | using point_save_span_const_type = gsl::span; 37 | 38 | // Output hash size is 32 bytes: 16 for item hash and 16 for label encryption key 39 | static constexpr std::size_t hash_size = 32; 40 | 41 | using hash_span_type = gsl::span; 42 | 43 | // Initializes the ECPoint with the neutral element 44 | ECPoint() = default; 45 | 46 | ECPoint &operator=(const ECPoint &assign); 47 | 48 | ECPoint(const ECPoint ©) 49 | { 50 | operator=(copy); 51 | } 52 | 53 | // This function applies Blake2b on value and hashes the output to 54 | // a uniformly random elliptic curve point. 55 | ECPoint(input_span_const_type value); 56 | 57 | // Creates a random non-zero number modulo the prime order subgroup 58 | // order and computes its inverse. 59 | static void MakeRandomNonzeroScalar(scalar_span_type out); 60 | 61 | static void InvertScalar(scalar_span_const_type in, scalar_span_type out); 62 | 63 | bool scalar_multiply(scalar_span_const_type scalar, bool clear_cofactor); 64 | 65 | void save(std::ostream &stream) const; 66 | 67 | void load(std::istream &stream); 68 | 69 | void save(point_save_span_type out) const; 70 | 71 | void load(point_save_span_const_type in); 72 | 73 | void extract_hash(hash_span_type out) const; 74 | 75 | private: 76 | // Initialize to neutral element 77 | point_t pt_ = { { { { 0 } }, { { 1 } } } }; // { {.x = { 0 }, .y = { 1 } }}; 78 | }; // class ECPoint 79 | } // namespace oprf 80 | } // namespace apsu 81 | -------------------------------------------------------------------------------- /common/apsu/oprf/oprf_common.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // APSU 7 | #include "apsu/item.h" 8 | #include "apsu/oprf/ecpoint.h" 9 | 10 | namespace apsu { 11 | namespace oprf { 12 | using oprf_key_type = ECPoint::scalar_type; 13 | using oprf_key_const_type = const ECPoint::scalar_type; 14 | using oprf_key_span_type = ECPoint::scalar_span_type; 15 | using oprf_key_span_const_type = ECPoint::scalar_span_const_type; 16 | 17 | constexpr auto oprf_item_size = sizeof(Item); 18 | constexpr auto oprf_hash_size = sizeof(HashedItem); 19 | constexpr auto oprf_query_size = ECPoint::save_size; 20 | constexpr auto oprf_response_size = ECPoint::save_size; 21 | constexpr auto oprf_key_size = ECPoint::order_size; 22 | } // namespace oprf 23 | } // namespace apsu 24 | -------------------------------------------------------------------------------- /common/apsu/oprf/oprf_sender.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // SEAL 17 | #include "seal/dynarray.h" 18 | #include "seal/memorymanager.h" 19 | 20 | // APSU 21 | #include "apsu/item.h" 22 | #include "apsu/oprf/oprf_common.h" 23 | 24 | // GSL 25 | #include "gsl/span" 26 | 27 | namespace apsu { 28 | namespace oprf { 29 | class OPRFKey { 30 | public: 31 | OPRFKey() 32 | { 33 | create(); 34 | } 35 | 36 | OPRFKey &operator=(const OPRFKey ©) 37 | { 38 | oprf_key_ = copy.oprf_key_; 39 | return *this; 40 | } 41 | 42 | OPRFKey &operator=(OPRFKey &&source) = default; 43 | 44 | OPRFKey(const OPRFKey ©) 45 | { 46 | operator=(copy); 47 | } 48 | 49 | OPRFKey(OPRFKey &&source) = default; 50 | 51 | bool operator==(const OPRFKey &compare) const; 52 | 53 | bool operator!=(const OPRFKey &compare) const 54 | { 55 | return !operator==(compare); 56 | } 57 | 58 | void create() 59 | { 60 | // Create a random key 61 | ECPoint::MakeRandomNonzeroScalar( 62 | oprf_key_span_type{ oprf_key_.begin(), oprf_key_size }); 63 | } 64 | 65 | void save(std::ostream &stream) const; 66 | 67 | void load(std::istream &stream); 68 | 69 | void save(oprf_key_span_type oprf_key) const; 70 | 71 | void load(oprf_key_span_const_type oprf_key); 72 | 73 | void clear() 74 | { 75 | oprf_key_ = seal::DynArray( 76 | oprf_key_size, 77 | seal::MemoryManager::GetPool(seal::mm_prof_opt::mm_force_new, true)); 78 | } 79 | 80 | oprf_key_span_const_type key_span() const noexcept 81 | { 82 | return oprf_key_span_const_type{ oprf_key_.cbegin(), oprf_key_size }; 83 | } 84 | 85 | private: 86 | seal::DynArray oprf_key_{ 87 | oprf_key_size, seal::MemoryManager::GetPool(seal::mm_prof_opt::mm_force_new, true) 88 | }; 89 | }; // class OPRFKey 90 | 91 | class OPRFSender { 92 | public: 93 | OPRFSender() = delete; 94 | 95 | static std::vector ProcessQueries( 96 | gsl::span oprf_queries, const OPRFKey &oprf_key); 97 | 98 | static std::pair GetItemHash( 99 | const Item &item, const OPRFKey &oprf_key); 100 | 101 | static std::vector ComputeHashes( 102 | const gsl::span &oprf_items, const OPRFKey &oprf_key); 103 | 104 | static std::vector> ComputeHashes( 105 | const gsl::span> &oprf_item_labels, 106 | const OPRFKey &oprf_key, 107 | std::size_t label_byte_count, 108 | std::size_t nonce_byte_count); 109 | }; // class OPRFSender 110 | } // namespace oprf 111 | } // namespace apsu 112 | -------------------------------------------------------------------------------- /common/apsu/ot/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 2 | ${CMAKE_CURRENT_LIST_DIR}/kunlun_ot.cpp 3 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.cpp 4 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.cpp 5 | ) 6 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 7 | ${CMAKE_CURRENT_LIST_DIR}/kunlun_ot.cpp 8 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.cpp 9 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.cpp 10 | ) 11 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 12 | ${CMAKE_CURRENT_LIST_DIR}/kunlun_ot.cpp 13 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.cpp 14 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.cpp 15 | ) 16 | 17 | # Add header files for installation 18 | install( 19 | FILES 20 | ${CMAKE_CURRENT_LIST_DIR}/kunlun_ot.h 21 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_common.h 22 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_sender.h 23 | # ${CMAKE_CURRENT_LIST_DIR}/oprf_receiver.h 24 | DESTINATION 25 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/ot 26 | ) 27 | 28 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 29 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 30 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) -------------------------------------------------------------------------------- /common/apsu/ot/kunlun_ot.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Kunlun/mpc/ot/iknp_ote.hpp" 3 | #include "Kunlun/mpc/ot/alsz_ote.hpp" 4 | #include 5 | #include 6 | #include "cryptoTools/Common/block.h" 7 | #include "cryptoTools/Common/BitVector.h" 8 | namespace apsu{ 9 | #define OUTPUTTOFILE 10 | 11 | namespace KunlunOT{ 12 | void IKNP_KL_RandomSender(NetIO& chl,std::vector>& sendMsg,size_t thread_num = 1); 13 | void IKNP_KL_RandomReceiver(NetIO& chl,osuCrypto::BitVector& choices, 14 | std::vector& recvMsg,size_t thread_num = 1); 15 | void IKNP_KL_ChosenSender(NetIO& chl,std::vector> Messages); 16 | void IKNP_KL_ChosenReceiver(NetIO& chl,std::vector& DiffSet,size_t BlockNum, std::vector choicesVec); 17 | 18 | void ALSZ_KL_RandomSender(NetIO& chl,std::vector>& sendMsg,size_t thread_num = 1); 19 | void ALSZ_KL_RandomReceiver(NetIO& chl,osuCrypto::BitVector& choices, 20 | std::vector& recvMsg,size_t thread_num = 1); 21 | 22 | void ALSZ_KL_ChosenSender(NetIO& chl,std::vector> Messages); 23 | void ALSZ_KL_ChosenReceiver(NetIO& chl,std::vector& DiffSet,size_t BlockNum, std::vector choicesVec); 24 | 25 | 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /common/apsu/peqt/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} 2 | ${CMAKE_CURRENT_LIST_DIR}/OSNPEQT.cpp 3 | ) 4 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} 5 | ${CMAKE_CURRENT_LIST_DIR}/OSNPEQT.cpp 6 | ) 7 | set(APSU_SOURCE_FILES_SENDER_DDH ${APSU_SOURCE_FILES_SENDER_DDH} 8 | ${CMAKE_CURRENT_LIST_DIR}/DDHPEQT.cpp 9 | ) 10 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} 11 | ${CMAKE_CURRENT_LIST_DIR}/DDHPEQT.cpp 12 | ) 13 | 14 | # Add header files for installation 15 | install( 16 | FILES 17 | ${CMAKE_CURRENT_LIST_DIR}/DDHPEQT.h 18 | ${CMAKE_CURRENT_LIST_DIR}/OSNPEQT.h 19 | DESTINATION 20 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/peqt 21 | ) 22 | 23 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 24 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} PARENT_SCOPE) 25 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} PARENT_SCOPE) 26 | set(APSU_SOURCE_FILES_SENDER_DDH ${APSU_SOURCE_FILES_SENDER_DDH} PARENT_SCOPE) 27 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} PARENT_SCOPE) -------------------------------------------------------------------------------- /common/apsu/peqt/DDHPEQT.cpp: -------------------------------------------------------------------------------- 1 | #include "DDHPEQT.h" 2 | namespace apsu{ 3 | namespace peqt{ 4 | std::vector ddh_peqt_sender(NetIO& net,std::vector& decrypt_randoms_matrix,size_t alpha_max_cache_count,size_t item_cnt){ 5 | 6 | CRYPTO_Initialize(); 7 | 8 | 9 | // Block::PrintBlocks(decrypt_randoms_matrix); 10 | 11 | auto permutation = DDHPEQT::Send(net, decrypt_randoms_matrix, alpha_max_cache_count,item_cnt); 12 | decrypt_randoms_matrix.clear(); 13 | decrypt_randoms_matrix.shrink_to_fit(); 14 | CRYPTO_Finalize(); 15 | return permutation; 16 | 17 | } 18 | 19 | std::vector ddh_peqt_receiver(NetIO& net,std::vector& random_matrix,size_t max_bin_bundle_conut_alpha,size_t item_cnt){ 20 | 21 | CRYPTO_Initialize(); 22 | // Block::PrintBlocks(random_matrix); 23 | auto vec_result = DDHPEQT::Receive(net,random_matrix,max_bin_bundle_conut_alpha,item_cnt); 24 | 25 | std::vector ans; 26 | for(size_t cache_idx = 0;cache_idx ddh_peqt_sender 7 | (NetIO& net, 8 | std::vector& decrypt_randoms_matrix, 9 | size_t alpha_max_cache_count, 10 | size_t item_cnt); 11 | 12 | std::vector ddh_peqt_receiver( 13 | NetIO& net, 14 | std::vector& random_matrix, 15 | size_t max_bin_bundle_conut_alpha, 16 | size_t item_cnt); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /common/apsu/peqt/OSNPEQT.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "apsu/ot/kunlun_ot.h" 3 | #include "apsu/permute/apsu_OSNReceiver.h" 4 | #include "apsu/permute/apsu_OSNSender.h" 5 | 6 | #include "Kunlun/mpc/oprf/ote_oprf.hpp" 7 | #include "Kunlun/crypto/setup.hpp" 8 | namespace apsu{ 9 | namespace peqt{ 10 | std::vector osn_peqt_sender( 11 | oc::Socket &SenderChl, 12 | NetIO& net, 13 | std::vector decrypt_randoms_matrix, 14 | size_t alpha_max_cache_count, 15 | size_t item_cnt); 16 | std::vector osn_peqt_receiver( 17 | oc::Socket &ReceiverChl, 18 | NetIO& net, 19 | std::vector random_matrix, 20 | size_t alpha_max_cache_count, 21 | size_t item_cnt 22 | 23 | ); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /common/apsu/permute/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/apsu_benes.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNReceiver.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNSender.cpp 9 | ) 10 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} 11 | ${CMAKE_CURRENT_LIST_DIR}/apsu_benes.cpp 12 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNReceiver.cpp 13 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNSender.cpp 14 | ) 15 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} 16 | ${CMAKE_CURRENT_LIST_DIR}/apsu_benes.cpp 17 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNReceiver.cpp 18 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNSender.cpp 19 | ) 20 | # Add header files for installation 21 | install( 22 | FILES 23 | ${CMAKE_CURRENT_LIST_DIR}/apsu_benes.h 24 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNReceiver.h 25 | ${CMAKE_CURRENT_LIST_DIR}/apsu_OSNSender.h 26 | DESTINATION 27 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/permute 28 | ) 29 | 30 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 31 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} PARENT_SCOPE) 32 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} PARENT_SCOPE) 33 | 34 | -------------------------------------------------------------------------------- /common/apsu/permute/apsu_OSNReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "cryptoTools/Common/Defines.h" 5 | #include "cryptoTools/Common/Timer.h" 6 | #include "cryptoTools/Network/Channel.h" 7 | #include "libOTe/Tools/Coproto.h" 8 | #include 9 | #include "apsu/ot/kunlun_ot.h" 10 | 11 | class OSNReceiver 12 | { 13 | public: 14 | enum OT_type{ 15 | Silent_OT, 16 | RandOT, 17 | KunlunOT 18 | }; 19 | private: 20 | size_t size; 21 | size_t thread_num; 22 | OT_type ot_type; 23 | oc::Timer* timer; 24 | std::atomic cpus; 25 | 26 | void rand_ot_send(std::vector>& sendMsg, oc::Socket chl); 27 | void silent_ot_send(std::vector>& sendMsg, oc::Socket chl); 28 | void Kunlun_ot_send(std::vector>& sendMsg); 29 | std::vector> gen_benes_client_osn(int values, oc::Socket chls, NetIO& RecvChl); 30 | void prepare_correction(int n, int Val, int lvl_p, int perm_idx, std::vector& src, 31 | std::vector, 2>>& ot_output, 32 | std::vector>& correction_blocks); 33 | public: 34 | 35 | 36 | OSNReceiver(size_t size = 0, OT_type ot_type = Silent_OT); 37 | void init(size_t size, OT_type ot_type = Silent_OT, size_t threads = 1); 38 | std::vector run_osn(std::vector inputs,oc::Socket chl, NetIO& RecvChl); 39 | void setTimer(oc::Timer& timer); 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /common/apsu/permute/apsu_OSNSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "cryptoTools/Common/BitVector.h" 7 | #include "cryptoTools/Common/Timer.h" 8 | #include "cryptoTools/Network/Channel.h" 9 | #include "libOTe/Tools/Coproto.h" 10 | #include "apsu_benes.h" 11 | #include "apsu/ot/kunlun_ot.h" 12 | class OSNSender 13 | { 14 | public: 15 | enum OT_type{ 16 | Silent_OT, 17 | RandOT, 18 | KunlunOT 19 | }; 20 | private: 21 | size_t size; 22 | size_t thread_num; 23 | OT_type ot_type; 24 | oc::Timer* timer; 25 | 26 | Benes benes; 27 | void silent_ot_recv(osuCrypto::BitVector& choices, 28 | std::vector& recvMsg, 29 | oc::Socket chl); 30 | void rand_ot_recv(osuCrypto::BitVector& choices, 31 | std::vector& recvMsg, 32 | oc::Socket chl); 33 | std::vector> gen_benes_server_osn(int values,oc::Socket chl,NetIO& SendChl); 34 | public: 35 | std::vector dest; 36 | std::vector rows_permutation; 37 | std::vector cols_premutation; 38 | OSNSender(size_t size = 0, OT_type ot_type = Silent_OT); 39 | void init(size_t Row_num,size_t Col_num, OT_type ot_type = Silent_OT, size_t threads=1, const std::string& osn_cache = ""); 40 | std::vector run_osn(oc::Socket chl,NetIO& SendChl); 41 | void setTimer(oc::Timer& timer); 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /common/apsu/permute/apsu_benes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | class Benes 8 | { 9 | std::vector perm; 10 | std::vector inv_perm; 11 | std::vector> switched; 12 | std::vector path; 13 | 14 | void DFS(int idx, int route); 15 | 16 | void gen_benes_eval(int n, int lvl_p, int perm_idx, std::vector& src); 17 | public: 18 | 19 | bool dump(const std::string& filename); 20 | 21 | bool load(const std::string& filename); 22 | 23 | void initialize(int values, int levels); 24 | 25 | osuCrypto::BitVector return_switches(int N); 26 | 27 | void gen_benes_route(int n, int lvl_p, int perm_idx, const std::vector& src, 28 | const std::vector& dest); 29 | 30 | void gen_benes_eval(int n, int lvl_p, int perm_idx, std::vector& src); 31 | 32 | void gen_benes_masked_evaluate(int n, int lvl_p, int perm_idx, std::vector& src, 33 | std::vector>>& ot_output); 34 | 35 | osuCrypto::BitVector return_gen_benes_switches(int values); 36 | }; -------------------------------------------------------------------------------- /common/apsu/psu_params.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | namespace apsu.fbs; 5 | 6 | struct ItemParams { 7 | felts_per_item:uint32; 8 | } 9 | 10 | struct TableParams { 11 | table_size:uint32; 12 | max_items_per_bin:uint32; 13 | hash_func_count:uint32; 14 | } 15 | 16 | table QueryParams { 17 | ps_low_degree:uint32; 18 | query_powers:[uint32]; 19 | } 20 | 21 | table SEALParams { 22 | data:[ubyte] (required); 23 | } 24 | 25 | table PSUParams { 26 | version:uint32; 27 | item_params:ItemParams; 28 | table_params:TableParams; 29 | query_params:QueryParams; 30 | seal_params:SEALParams (required); 31 | } 32 | 33 | root_type PSUParams; 34 | -------------------------------------------------------------------------------- /common/apsu/thread_pool_mgr.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // STD 5 | #include 6 | #include 7 | 8 | // APSU 9 | #include "apsu/thread_pool_mgr.h" 10 | 11 | using namespace std; 12 | using namespace apsu; 13 | using namespace apsu::util; 14 | 15 | /** 16 | Static reference count that will manage the lifetime of the single ThreadPool 17 | object that all users of this class will share. 18 | */ 19 | size_t ThreadPoolMgr::ref_count_ = 0; 20 | 21 | namespace { 22 | mutex tp_mutex; 23 | size_t thread_count = thread::hardware_concurrency(); 24 | size_t phys_thread_count = thread::hardware_concurrency(); 25 | unique_ptr thread_pool_; 26 | } // namespace 27 | 28 | ThreadPoolMgr::ThreadPoolMgr() 29 | { 30 | unique_lock lock(tp_mutex); 31 | 32 | if (ref_count_ == 0) { 33 | thread_pool_ = make_unique(phys_thread_count); 34 | } 35 | 36 | ref_count_++; 37 | } 38 | 39 | ThreadPoolMgr::~ThreadPoolMgr() 40 | { 41 | unique_lock lock(tp_mutex); 42 | 43 | ref_count_--; 44 | if (ref_count_ == 0) { 45 | thread_pool_ = nullptr; 46 | } 47 | } 48 | 49 | ThreadPool &ThreadPoolMgr::thread_pool() const 50 | { 51 | if (!thread_pool_) 52 | throw runtime_error("Thread pool is not available"); 53 | 54 | return *thread_pool_; 55 | } 56 | 57 | void ThreadPoolMgr::SetThreadCount(size_t threads) 58 | { 59 | unique_lock lock(tp_mutex); 60 | 61 | thread_count = threads != 0 ? threads : thread::hardware_concurrency(); 62 | phys_thread_count = thread_count; 63 | 64 | if (thread_pool_) { 65 | thread_pool_->set_pool_size(phys_thread_count); 66 | } 67 | } 68 | 69 | void ThreadPoolMgr::SetPhysThreadCount(size_t threads) 70 | { 71 | unique_lock lock(tp_mutex); 72 | 73 | phys_thread_count = threads != 0 ? threads : thread::hardware_concurrency(); 74 | 75 | if (thread_pool_) { 76 | thread_pool_->set_pool_size(phys_thread_count); 77 | } 78 | } 79 | 80 | size_t ThreadPoolMgr::GetThreadCount() 81 | { 82 | return thread_count; 83 | } 84 | -------------------------------------------------------------------------------- /common/apsu/thread_pool_mgr.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | 9 | // APSU 10 | #include "apsu/util/thread_pool.h" 11 | 12 | namespace apsu { 13 | /** 14 | Manages lifetime of a static thread pool. While an instance of this class exists, 15 | a static thread pool will be shared among all instances. 16 | */ 17 | class ThreadPoolMgr { 18 | public: 19 | /** 20 | Build an instance of ThreadPoolMgr 21 | */ 22 | ThreadPoolMgr(); 23 | 24 | /** 25 | Destructor for ThreadPoolMgr 26 | */ 27 | ~ThreadPoolMgr(); 28 | 29 | /** 30 | Get the thread pool managed by the thread pool manager 31 | */ 32 | util::ThreadPool &thread_pool() const; 33 | 34 | /** 35 | Set the number of threads to be used by the thread pool 36 | */ 37 | static void SetThreadCount(std::size_t threads); 38 | 39 | /** 40 | This method is to be used explicitly by tests. 41 | */ 42 | static void SetPhysThreadCount(std::size_t threads); 43 | 44 | /** 45 | Get the number of threads used by the thread pool 46 | */ 47 | static std::size_t GetThreadCount(); 48 | 49 | private: 50 | /** 51 | Reference count to manage lifetime of the static thread pool 52 | */ 53 | static std::size_t ref_count_; 54 | }; 55 | } // namespace apsu 56 | -------------------------------------------------------------------------------- /common/apsu/util/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/interpolate.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/label_encryptor.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/db_encoding.cpp 9 | ${CMAKE_CURRENT_LIST_DIR}/stopwatch.cpp 10 | ${CMAKE_CURRENT_LIST_DIR}/utils.cpp 11 | ) 12 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 13 | ${CMAKE_CURRENT_LIST_DIR}/interpolate.cpp 14 | ${CMAKE_CURRENT_LIST_DIR}/label_encryptor.cpp 15 | ${CMAKE_CURRENT_LIST_DIR}/db_encoding.cpp 16 | ${CMAKE_CURRENT_LIST_DIR}/stopwatch.cpp 17 | ${CMAKE_CURRENT_LIST_DIR}/utils.cpp 18 | ) 19 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 20 | ${CMAKE_CURRENT_LIST_DIR}/interpolate.cpp 21 | ${CMAKE_CURRENT_LIST_DIR}/label_encryptor.cpp 22 | ${CMAKE_CURRENT_LIST_DIR}/db_encoding.cpp 23 | ${CMAKE_CURRENT_LIST_DIR}/stopwatch.cpp 24 | ${CMAKE_CURRENT_LIST_DIR}/utils.cpp 25 | ) 26 | # Add header files for installation 27 | install( 28 | FILES 29 | ${CMAKE_CURRENT_LIST_DIR}/interpolate.h 30 | ${CMAKE_CURRENT_LIST_DIR}/label_encryptor.h 31 | ${CMAKE_CURRENT_LIST_DIR}/db_encoding.h 32 | ${CMAKE_CURRENT_LIST_DIR}/stopwatch.h 33 | ${CMAKE_CURRENT_LIST_DIR}/thread_pool.h 34 | ${CMAKE_CURRENT_LIST_DIR}/utils.h 35 | DESTINATION 36 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/util 37 | ) 38 | 39 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 40 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 41 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 42 | -------------------------------------------------------------------------------- /common/apsu/util/db_encoding.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // GSL 17 | #include "gsl/span" 18 | 19 | // SEAL 20 | #include "seal/modulus.h" 21 | 22 | // APSU 23 | #include "apsu/item.h" 24 | 25 | namespace apsu { 26 | namespace util { 27 | // An element of a field with prime modulus < 2⁶⁴ 28 | using felt_t = std::uint64_t; 29 | 30 | // A representation of item as a sequence felt_t 31 | using AlgItem = std::vector; 32 | 33 | // A representation of label as a sequence of felt_t 34 | using AlgLabel = std::vector; 35 | 36 | // A representation of item-label as a sequence of pairs of felt_t and std::vector 37 | using AlgItemLabel = std::vector>>; 38 | 39 | /** 40 | Converts the given bitstring to a sequence of field elements (modulo `mod`). 41 | */ 42 | std::vector bits_to_field_elts( 43 | BitstringView bits, const seal::Modulus &mod); 44 | 45 | /** 46 | Converts the given bitstring to a sequence of field elements (modulo `mod`). 47 | */ 48 | std::vector bits_to_field_elts( 49 | BitstringView bits, const seal::Modulus &mod); 50 | 51 | /** 52 | Converts the given field elements (modulo `mod`) to a bitstring. 53 | */ 54 | Bitstring field_elts_to_bits( 55 | gsl::span felts, std::uint32_t bit_count, const seal::Modulus &mod); 56 | 57 | /** 58 | Converts an item and label into a sequence of (felt_t, felt_t) pairs, where the the first 59 | pair value is a chunk of the item, and the second is a chunk of the label. item_bit_count 60 | denotes the bit length of the items and labels (they are the same length). mod denotes the 61 | modulus of the prime field. 62 | */ 63 | AlgItemLabel algebraize_item_label( 64 | const HashedItem &item, 65 | const EncryptedLabel &label, 66 | std::size_t item_bit_count, 67 | const seal::Modulus &mod); 68 | 69 | /** 70 | Converts an item into a sequence of (felt_t, monostate) pairs, where the the first pair 71 | value is a chunk of the item, and the second is the unit type. item_bit_count denotes the 72 | bit length of the items and labels (they are the same length). mod denotes the modulus of 73 | the prime field. mod denotes the modulus of the prime field. 74 | */ 75 | AlgItem algebraize_item( 76 | const HashedItem &item, std::size_t item_bit_count, const seal::Modulus &mod); 77 | 78 | /** 79 | Converts a sequence of field elements into a HashedItem. This will throw an invalid_argument 80 | if too many field elements are given, i.e., if modulus_bitlen * num_elements > 128. 81 | */ 82 | HashedItem dealgebraize_item( 83 | const AlgItem &item, std::size_t item_bit_count, const seal::Modulus &mod); 84 | 85 | /** 86 | Converts a sequence of field elements into an EncryptedLabel. This will throw an 87 | invalid_argument if too many field elements are given, i.e., if modulus_bitlen * 88 | num_elements > 128. 89 | */ 90 | EncryptedLabel dealgebraize_label( 91 | const AlgLabel &label, std::size_t label_bit_count, const seal::Modulus &mod); 92 | } // namespace util 93 | } // namespace apsu 94 | -------------------------------------------------------------------------------- /common/apsu/util/interpolate.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // APSU 7 | #include 8 | #include 9 | #include 10 | 11 | // SEAL 12 | #include "seal/modulus.h" 13 | 14 | // GSL 15 | #include "gsl/span" 16 | 17 | namespace apsu { 18 | namespace util { 19 | /** 20 | Multiplies the given polynomial P with the monomial x - a, where a is given. Polynomial 21 | coefficients are expected to be in degree-ascending order, i.e., polyn[0] is the constant 22 | term. 23 | */ 24 | void polyn_mul_monic_monomial_inplace( 25 | std::vector &polyn, std::uint64_t a, const seal::Modulus &mod); 26 | 27 | /** 28 | Given a set of distinct field elements a₁, ..., aₛ, returns the coefficients of the unique 29 | monic polynomial P with roots a₁, ..., aₛ. Concretely, P = (x-a₁)*...*(x-aₛ). The returned 30 | coefficients are in degree-ascending order. That is, polyn[0] is the constant term. 31 | */ 32 | std::vector polyn_with_roots( 33 | const std::vector &roots, const seal::Modulus &mod); 34 | 35 | /** 36 | Returns the Newton interpolation of the given points and values. Specifically, this function 37 | returns the coefficients of a polynomial P in degree-ascending order, where P(pointᵢ) == 38 | valueᵢ for all i. 39 | */ 40 | std::vector newton_interpolate_polyn( 41 | const std::vector &points, 42 | const std::vector &values, 43 | const seal::Modulus &mod); 44 | } // namespace util 45 | } // namespace apsu 46 | -------------------------------------------------------------------------------- /common/apsu/util/label_encryptor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // STD 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // APSU 12 | #include "apsu/fourq/random.h" 13 | #include "apsu/util/label_encryptor.h" 14 | #include "apsu/util/utils.h" 15 | 16 | // SEAL 17 | #include "seal/randomgen.h" 18 | #include "seal/util/blake2.h" 19 | 20 | using namespace std; 21 | using namespace seal; 22 | 23 | namespace apsu { 24 | namespace util { 25 | EncryptedLabel encrypt_label( 26 | const Label &label, 27 | const LabelKey &key, 28 | size_t label_byte_count, 29 | size_t nonce_byte_count) 30 | { 31 | if (nonce_byte_count > max_nonce_byte_count) { 32 | throw invalid_argument("nonce_byte_count is too large"); 33 | } 34 | 35 | // We use up to max_nonce_byte_count nonce bytes. This is enough for securely using 36 | // "random nonces". In most cases the number of label changes is likely to be so small 37 | // that a much smaller nonce should provide an adequate level of security. We append the 38 | // key to the nonce and use the combined buffer as input to Blake2xb to create the 39 | // pseudo-random byte stream for encryption. 40 | 41 | // Set up the result and create the nonce 42 | size_t encrypted_label_byte_count = nonce_byte_count + label_byte_count; 43 | EncryptedLabel result(encrypted_label_byte_count); 44 | random_bytes(result.data(), static_cast(nonce_byte_count)); 45 | 46 | // Fill result with mask from Blake2xb 47 | APSU_blake2xb( 48 | reinterpret_cast(result.data() + nonce_byte_count), 49 | label_byte_count, 50 | reinterpret_cast(result.data()), 51 | nonce_byte_count, 52 | reinterpret_cast(key.data()), 53 | label_key_byte_count); 54 | 55 | // XOR in the label 56 | xor_buffers( 57 | result.data() + nonce_byte_count, 58 | label.data(), 59 | min(label.size(), label_byte_count)); 60 | 61 | return result; 62 | } 63 | 64 | Label decrypt_label( 65 | const EncryptedLabel &encrypted_label, const LabelKey &key, size_t nonce_byte_count) 66 | { 67 | if (nonce_byte_count > max_nonce_byte_count) { 68 | throw invalid_argument("nonce_byte_count is too large"); 69 | } 70 | if (encrypted_label.size() < nonce_byte_count) { 71 | throw invalid_argument("encrypted_label cannot be smaller than nonce_byte_count"); 72 | } 73 | 74 | // Set up the result 75 | size_t label_byte_count = encrypted_label.size() - nonce_byte_count; 76 | Label result(label_byte_count); 77 | 78 | // Fill result with mask from Blake2xb 79 | APSU_blake2xb( 80 | reinterpret_cast(result.data()), 81 | label_byte_count, 82 | reinterpret_cast(encrypted_label.data()), 83 | nonce_byte_count, 84 | reinterpret_cast(key.data()), 85 | label_key_byte_count); 86 | 87 | // XOR in the encrypted label 88 | xor_buffers(result.data(), encrypted_label.data() + nonce_byte_count, label_byte_count); 89 | 90 | return result; 91 | } 92 | } // namespace util 93 | } // namespace apsu 94 | -------------------------------------------------------------------------------- /common/apsu/util/label_encryptor.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | 9 | // APSU 10 | #include "apsu/item.h" 11 | 12 | namespace apsu { 13 | namespace util { 14 | EncryptedLabel encrypt_label( 15 | const Label &label, 16 | const LabelKey &key, 17 | std::size_t label_byte_count, 18 | std::size_t nonce_byte_count); 19 | 20 | Label decrypt_label( 21 | const EncryptedLabel &encrypted_label, 22 | const LabelKey &key, 23 | std::size_t nonce_byte_count); 24 | } // namespace util 25 | } // namespace apsu 26 | -------------------------------------------------------------------------------- /common/apsu/version.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // APSU 5 | #include "apsu/config.h" 6 | #include "apsu/version.h" 7 | 8 | namespace apsu { 9 | const uint32_t apsu_version = 10 | (APSU_VERSION_PATCH << 20) + (APSU_VERSION_MINOR << 10) + APSU_VERSION_MAJOR; 11 | 12 | const uint32_t apsu_serialization_version = 1; 13 | 14 | bool same_serialization_version(uint32_t sv) 15 | { 16 | return sv == apsu_serialization_version; 17 | } 18 | } // namespace apsu 19 | -------------------------------------------------------------------------------- /common/apsu/version.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | 9 | namespace apsu { 10 | extern const std::uint32_t apsu_version; 11 | 12 | extern const std::uint32_t apsu_serialization_version; 13 | 14 | bool same_serialization_version(std::uint32_t sv); 15 | } // namespace apsu 16 | -------------------------------------------------------------------------------- /parameters/100K-1-16.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 409, 5 | "max_items_per_bin": 42 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 2048, 17 | "coeff_modulus_bits": [ 48 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/100K-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 409, 5 | "max_items_per_bin": 20 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 2048, 17 | "coeff_modulus_bits": [ 48 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-1-32.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 2048, 5 | "max_items_per_bin": 782 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 26, 12 | "query_powers": [ 1, 5, 8, 27, 135 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 21, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 2048, 5 | "max_items_per_bin": 782 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 26, 12 | "query_powers": [ 1, 5, 8, 27, 135 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 21, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-1024.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 1638, 5 | "max_items_per_bin": 1304 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 44, 12 | "query_powers": [ 1, 3, 11, 18, 45, 225 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-11041.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 16380, 5 | "max_items_per_bin": 1304 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 44, 12 | "query_powers": [ 1, 3, 11, 18, 45, 225 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-2048.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 3276, 5 | "max_items_per_bin": 1304 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 44, 12 | "query_powers": [ 1, 3, 11, 18, 45, 225 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-4096-32.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6552, 5 | "max_items_per_bin": 1304 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 44, 12 | "query_powers": [ 1, 3, 11, 18, 45, 225 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-4096.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6552, 5 | "max_items_per_bin": 1304 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 44, 12 | "query_powers": [ 1, 3, 11, 18, 45, 225 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/16M-5535.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 8190, 5 | "max_items_per_bin": 1304 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 44, 12 | "query_powers": [ 1, 3, 11, 18, 45, 225 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 50 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-1-32.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 1638, 5 | "max_items_per_bin": 228 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 8, 19, 33, 39, 92, 102 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 48, 48 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 409, 5 | "max_items_per_bin": 55 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 2048, 17 | "coeff_modulus_bits": [ 48 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-1024-cmp.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 2046, 5 | "max_items_per_bin": 101 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 5, 8, 14, 20, 26, 32, 38, 44, 47, 48, 49, 51, 52 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 40961, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 40, 32, 32 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-1024-com.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 1638, 5 | "max_items_per_bin": 125 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 5, 12 | "query_powers": [ 1, 2, 3, 4, 5, 6, 18, 30, 42, 54, 60 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 18, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 36, 25 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-11041.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 16384, 5 | "max_items_per_bin": 98 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 8, 12 | "query_powers": [ 1, 3, 4, 9, 27 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 21, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 24, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-2048-cmp.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 3410, 5 | "max_items_per_bin": 72 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 9, 11, 16, 20, 25, 27, 32, 33, 35, 36 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 40961, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 40, 32, 32 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-2048-com.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 3410, 5 | "max_items_per_bin": 125 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 5, 12 | "query_powers": [ 1, 2, 3, 4, 5, 6, 18, 30, 42, 54, 60 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 30, 30 ] 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /parameters/1M-256-288.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 682, 5 | "max_items_per_bin": 512 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 6, 10, 13, 15, 21, 29, 37, 45, 53, 61, 69, 75, 77, 80, 84, 86, 87, 89, 90, 181, 183, 188, 190, 195, 197, 206, 213, 214, 222, 230, 238, 246, 254, 261, 337, 338, 345, 353, 361, 370, 372, 377, 379, 384, 386, 477, 479, 486, 487, 495, 503, 511 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 18, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 32, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-256.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 585, 5 | "max_items_per_bin": 180 6 | }, 7 | "item_params": { 8 | "felts_per_item": 7 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 6, 10, 13, 15, 21, 29, 37, 45, 53, 61, 69, 77, 81, 83, 86, 87, 90, 92, 96 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 40961, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 40, 32, 32 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-4096-32.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6552, 5 | "max_items_per_bin": 770 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 26, 12 | "query_powers": [ 1, 5, 8, 27, 135 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 21, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 48, 56, 56, 48 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-4096-cmp.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6552, 5 | "max_items_per_bin": 40 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 9, 11, 16, 17, 19, 20 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 30, 30 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-4096-com.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6825, 5 | "max_items_per_bin": 98 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 8, 12 | "query_powers": [ 1, 3, 4, 9, 27 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 30 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-512-cmp.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 1364, 5 | "max_items_per_bin": 128 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 5, 8, 14, 20, 26, 32, 38, 44, 50, 56, 59, 60, 61, 63, 64 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 40, 34, 30 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-512-com.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 1364, 5 | "max_items_per_bin": 228 6 | }, 7 | "item_params": { 8 | "felts_per_item": 6 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 4, 12 | "query_powers": [ 1, 2, 3, 4, 5, 10, 15, 35, 55, 75, 95, 115, 125, 130, 140 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 34, 27 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/1M-5535.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 8192, 5 | "max_items_per_bin": 98 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 8, 12 | "query_powers": [ 1, 3, 4, 9, 27 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 21, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 24, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 409, 5 | "max_items_per_bin": 35 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 2048, 17 | "coeff_modulus_bits": [ 48 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-1024.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 1638, 5 | "max_items_per_bin": 46 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 2, 3, 7, 11, 15, 19, 21, 22, 24 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 32, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-2048-cmp.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 3276, 5 | "max_items_per_bin": 32 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 5, 7, 9, 10, 21, 22 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 32, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-2048-com.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 3276, 5 | "max_items_per_bin": 51 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 3, 12 | "query_powers": [ 1, 2, 3, 4, 12, 20, 24 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 32, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-4096-cmp.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6552, 5 | "max_items_per_bin": 20 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 2, 5, 8, 9, 10 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 18, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 32, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-4096-com.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6552, 5 | "max_items_per_bin": 54 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 4, 12 | "query_powers": [ 1, 3, 5, 15 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 32 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256K-512.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 819, 5 | "max_items_per_bin": 64 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 0, 12 | "query_powers": [ 1, 3, 4, 9, 11, 16, 21, 23, 28, 29, 31, 32 ] 13 | }, 14 | "seal_params": { 15 | "plain_modulus": 65537, 16 | "poly_modulus_degree": 4096, 17 | "coeff_modulus_bits": [ 48, 32, 24 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256M-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 1, 4 | "table_size": 1638, 5 | "max_items_per_bin": 8100 6 | }, 7 | "item_params": { 8 | "felts_per_item": 5 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 310, 12 | "query_powers": [ 1, 4, 10, 11, 28, 33, 78, 118, 143, 311, 1555] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 22, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 56, 56, 56, 32 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256M-1024.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 2048, 5 | "max_items_per_bin": 4000 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 310, 12 | "query_powers": [ 1, 4, 10, 11, 28, 33, 78, 118, 143, 311, 1555] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 26, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 50, 50, 50, 38, 30 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256M-2048.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 4096, 5 | "max_items_per_bin": 4000 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 310, 12 | "query_powers": [ 1, 4, 10, 11, 28, 33, 78, 118, 143, 311, 1555] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 26, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 50, 50, 50, 38, 30 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parameters/256M-4096.json: -------------------------------------------------------------------------------- 1 | { 2 | "table_params": { 3 | "hash_func_count": 3, 4 | "table_size": 6144, 5 | "max_items_per_bin": 4000 6 | }, 7 | "item_params": { 8 | "felts_per_item": 4 9 | }, 10 | "query_params": { 11 | "ps_low_degree": 310, 12 | "query_powers": [ 1, 4, 10, 11, 28, 33, 78, 118, 143, 311, 1555] 13 | }, 14 | "seal_params": { 15 | "plain_modulus_bits": 26, 16 | "poly_modulus_degree": 8192, 17 | "coeff_modulus_bits": [ 50, 50, 50, 38, 30 ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /receiver/apsu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/bin_bundle.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/query.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/receiver_osn.cpp 9 | ${CMAKE_CURRENT_LIST_DIR}/receiver_db.cpp 10 | ) 11 | 12 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 13 | ${CMAKE_CURRENT_LIST_DIR}/bin_bundle.cpp 14 | ${CMAKE_CURRENT_LIST_DIR}/query.cpp 15 | ${CMAKE_CURRENT_LIST_DIR}/receiver_db.cpp 16 | ) 17 | 18 | 19 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} 20 | ${CMAKE_CURRENT_LIST_DIR}/receiver_osn.cpp 21 | ) 22 | 23 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} 24 | ${CMAKE_CURRENT_LIST_DIR}/receiver_ddh.cpp 25 | ) 26 | # Add header files for installation 27 | install( 28 | FILES 29 | ${CMAKE_CURRENT_LIST_DIR}/bin_bundle.h 30 | ${CMAKE_CURRENT_LIST_DIR}/query.h 31 | ${CMAKE_CURRENT_LIST_DIR}/receiver_osn.h 32 | ${CMAKE_CURRENT_LIST_DIR}/receiver_ddh.h 33 | ${CMAKE_CURRENT_LIST_DIR}/receiver_db.h 34 | DESTINATION 35 | ${APSU_INCLUDES_INSTALL_DIR}/apsu 36 | ) 37 | 38 | add_subdirectory(util) 39 | 40 | if(APSU_USE_ZMQ) 41 | add_subdirectory(zmq) 42 | endif() 43 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 44 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 45 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} PARENT_SCOPE) 46 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} PARENT_SCOPE) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /receiver/apsu/bin_bundle.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | namespace apsu.fbs; 5 | 6 | table FEltArray { 7 | felts:[uint64] (required); 8 | } 9 | 10 | table FEltMatrix { 11 | rows:[FEltArray] (required); 12 | } 13 | 14 | table Plaintext { 15 | data:[ubyte] (required); 16 | } 17 | 18 | table BatchedPlaintextPolyn { 19 | coeffs:[Plaintext] (required); 20 | } 21 | 22 | table BinBundleCache { 23 | felt_matching_polyns:FEltMatrix (required); 24 | batched_matching_polyn:BatchedPlaintextPolyn (required); 25 | felt_interp_polyns:[FEltMatrix]; 26 | batched_interp_polyns:[BatchedPlaintextPolyn]; 27 | } 28 | 29 | table BinBundle { 30 | bundle_idx:uint32; 31 | mod:uint64; 32 | item_bins:FEltMatrix (required); 33 | label_bins:[FEltMatrix]; 34 | cache:BinBundleCache; 35 | stripped:bool; 36 | } 37 | 38 | root_type BinBundle; 39 | -------------------------------------------------------------------------------- /receiver/apsu/query.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // APSU 15 | #include "apsu/network/receiver_operation.h" 16 | #include "apsu/powers.h" 17 | #include "apsu/requests.h" 18 | #include "apsu/receiver_db.h" 19 | 20 | // SEAL 21 | #include "seal/ciphertext.h" 22 | #include "seal/relinkeys.h" 23 | 24 | namespace apsu { 25 | namespace receiver { 26 | class Query { 27 | public: 28 | Query() = default; 29 | 30 | Query(QueryRequest query_request, std::shared_ptr receiver_db); 31 | 32 | Query deep_copy() const; 33 | 34 | Query(const Query &source) = delete; 35 | 36 | Query(Query &&source) = default; 37 | 38 | Query &operator=(const Query &source) = delete; 39 | 40 | Query &operator=(Query &&source) = default; 41 | 42 | bool is_valid() const noexcept 43 | { 44 | return valid_; 45 | } 46 | 47 | explicit operator bool() const noexcept 48 | { 49 | return is_valid(); 50 | } 51 | 52 | const seal::RelinKeys &relin_keys() const noexcept 53 | { 54 | return relin_keys_; 55 | } 56 | 57 | auto &data() const noexcept 58 | { 59 | return data_; 60 | } 61 | 62 | const PowersDag &pd() const noexcept 63 | { 64 | return pd_; 65 | } 66 | 67 | std::shared_ptr receiver_db() const noexcept 68 | { 69 | return receiver_db_; 70 | } 71 | 72 | seal::compr_mode_type compr_mode() const noexcept 73 | { 74 | return compr_mode_; 75 | } 76 | 77 | private: 78 | seal::RelinKeys relin_keys_; 79 | 80 | std::unordered_map> data_; 81 | 82 | PowersDag pd_; 83 | 84 | std::shared_ptr receiver_db_; 85 | 86 | bool valid_ = false; 87 | 88 | seal::compr_mode_type compr_mode_; 89 | }; 90 | } // namespace receiver 91 | } // namespace apsu 92 | -------------------------------------------------------------------------------- /receiver/apsu/receiver_db.fbs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | include "psu_params.fbs"; 5 | 6 | namespace apsu.fbs; 7 | 8 | struct HashedItem { 9 | low_word:uint64; 10 | high_word:uint64; 11 | } 12 | 13 | struct ReceiverDBInfo { 14 | label_byte_count:uint32; 15 | nonce_byte_count:uint32; 16 | item_count:uint64; 17 | compressed:bool; 18 | stripped:bool; 19 | } 20 | 21 | table ReceiverDB { 22 | params:[ubyte] (required); 23 | info:ReceiverDBInfo; 24 | oprf_key:[ubyte] (required); 25 | hashed_items:[HashedItem] (required); 26 | bin_bundle_count:uint32; 27 | } 28 | 29 | root_type ReceiverDB; 30 | -------------------------------------------------------------------------------- /receiver/apsu/util/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/cuckoo_filter.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/cuckoo_filter_table.cpp 8 | ) 9 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} 10 | ${CMAKE_CURRENT_LIST_DIR}/cuckoo_filter.cpp 11 | ${CMAKE_CURRENT_LIST_DIR}/cuckoo_filter_table.cpp 12 | ) 13 | 14 | # Add header files for installation 15 | install( 16 | FILES 17 | ${CMAKE_CURRENT_LIST_DIR}/cuckoo_filter.h 18 | ${CMAKE_CURRENT_LIST_DIR}/cuckoo_filter_table.h 19 | ${CMAKE_CURRENT_LIST_DIR}/hash.h 20 | DESTINATION 21 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/util 22 | ) 23 | 24 | set(APSU_SOURCE_FILES_RECEIVER ${APSU_SOURCE_FILES_RECEIVER} PARENT_SCOPE) 25 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 26 | -------------------------------------------------------------------------------- /receiver/apsu/util/cuckoo_filter_table.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | 11 | namespace apsu { 12 | namespace receiver { 13 | namespace util { 14 | /** 15 | Implementation of a Cuckoo Filter table. 16 | Logically the table is divided in buckets. Each bucket is capable of storing up to 17 | tags_per_bucket_ tags. Each tags uses bits_per_tag_ bits of storage. 18 | */ 19 | class CuckooFilterTable { 20 | public: 21 | /** 22 | Build an instance of a Cuckoo Filter Table 23 | */ 24 | CuckooFilterTable(std::size_t num_items, std::size_t bits_per_tag); 25 | 26 | /** 27 | Read the tag at the given bucket and tag index within the bucket 28 | */ 29 | std::uint32_t read_tag(std::size_t bucket, std::size_t tag_idx) const; 30 | 31 | /** 32 | Write a tag at the given bucket and tag index within the bucket 33 | */ 34 | void write_tag(std::size_t bucket, std::size_t tag_idx, std::uint32_t tag); 35 | 36 | /** 37 | Insert a tag in the given bucket 38 | */ 39 | bool insert_tag( 40 | std::size_t bucket, std::uint32_t tag, bool kickout, std::uint32_t &old_tag); 41 | 42 | /** 43 | Delete a tag fromthe given bucket 44 | */ 45 | bool delete_tag(std::size_t bucket, std::uint32_t tag); 46 | 47 | /** 48 | Get the number of buckets 49 | */ 50 | std::size_t get_num_buckets() const 51 | { 52 | return num_buckets_; 53 | } 54 | 55 | /** 56 | Get the number of bits to use per tag 57 | */ 58 | std::size_t get_bits_per_tag() const 59 | { 60 | return bits_per_tag_; 61 | } 62 | 63 | /** 64 | Find a tag in the given bucket 65 | */ 66 | bool find_tag_in_bucket(std::size_t bucket, std::uint32_t tag) const; 67 | 68 | /** 69 | Find a tag in the given buckets 70 | */ 71 | bool find_tag_in_buckets( 72 | std::size_t bucket1, std::size_t bucket2, std::uint32_t tag) const; 73 | 74 | private: 75 | /** 76 | Indicates how many tags each bucket will contain 77 | */ 78 | constexpr static std::size_t tags_per_bucket_ = 4; 79 | 80 | /** 81 | Number of bits per tag 82 | */ 83 | std::size_t bits_per_tag_; 84 | 85 | /** 86 | Input elements will be limited with this mask 87 | */ 88 | std::uint32_t tag_input_mask_; 89 | 90 | /** 91 | The bucket table is encoded as an array of uint64_t 92 | */ 93 | std::vector table_; 94 | 95 | /** 96 | Number of buckets in the current table 97 | */ 98 | std::size_t num_buckets_; 99 | }; 100 | } // namespace util 101 | } // namespace receiver 102 | } // namespace apsu 103 | -------------------------------------------------------------------------------- /receiver/apsu/zmq/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/receiver_dispatcher_osn.cpp 7 | ) 8 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} 9 | ${CMAKE_CURRENT_LIST_DIR}/receiver_dispatcher_osn.cpp 10 | ) 11 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} 12 | ${CMAKE_CURRENT_LIST_DIR}/receiver_dispatcher_ddh.cpp 13 | ) 14 | # Add header files for installation 15 | install( 16 | FILES 17 | ${CMAKE_CURRENT_LIST_DIR}/receiver_dispatcher_osn.h 18 | ${CMAKE_CURRENT_LIST_DIR}/receiver_dispatcher_ddh.h 19 | 20 | DESTINATION 21 | ${APSU_INCLUDES_INSTALL_DIR}/apsu/zmq 22 | ) 23 | 24 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 25 | set(APSU_SOURCE_FILES_RECEIVER_OSN ${APSU_SOURCE_FILES_RECEIVER_OSN} PARENT_SCOPE) 26 | set(APSU_SOURCE_FILES_RECEIVER_DDH ${APSU_SOURCE_FILES_RECEIVER_DDH} PARENT_SCOPE) 27 | 28 | -------------------------------------------------------------------------------- /receiver/apsu/zmq/receiver_dispatcher_ddh.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | 11 | // APSU 12 | #include "apsu/network/receiver_operation.h" 13 | #include "apsu/network/zmq/zmq_channel.h" 14 | #include "apsu/oprf/oprf_receiver.h" 15 | #include "apsu/receiver_ddh.h" 16 | #include "apsu/receiver_db.h" 17 | 18 | namespace apsu { 19 | namespace receiver { 20 | /** 21 | The ZMQReceiverDispatcher is in charge of handling incoming requests through the network. 22 | */ 23 | class ZMQReceiverDispatcher { 24 | public: 25 | ZMQReceiverDispatcher() = delete; 26 | 27 | /** 28 | Creates a new ZMQReceiverDispatcher object. This constructor accepts both a ReceiverDB 29 | object, as well as a separately provided OPRF key. It uses the provided OPRF key to 30 | respond to OPRF requests, instead of attempting to retrieve a key from the ReceiverDB. 31 | This is necessary, for example, when the ReceiverDB is stripped, in which case it no 32 | longer carries a valid OPRF key. 33 | */ 34 | ZMQReceiverDispatcher(std::shared_ptr receiver_db, oprf::OPRFKey oprf_key,Receiver receiver); 35 | ZMQReceiverDispatcher(std::shared_ptr receiver_db,Receiver receiver); 36 | /** 37 | Creates a new ZMQReceiverDispatcher object. This constructor accepts a ReceiverDB object. It 38 | attempts to retrieve an OPRF key from the ReceiverDB and uses it to serve OPRF requests. 39 | This constructor cannot be used if the ReceiverDB is stripped, because the OPRF key is no 40 | longer available through the ReceiverDB. 41 | */ 42 | ZMQReceiverDispatcher(std::shared_ptr receiver_db); 43 | 44 | /** 45 | Run the dispatcher on the given port. 46 | */ 47 | void run(const std::atomic &stop, int port); 48 | 49 | private: 50 | std::shared_ptr receiver_db_; 51 | 52 | oprf::OPRFKey oprf_key_; 53 | 54 | Receiver receiver_; 55 | 56 | /** 57 | Dispatch a Get Parameters request to the Receiver. 58 | */ 59 | void dispatch_parms( 60 | std::unique_ptr rop, 61 | network::ZMQReceiverChannel &channel); 62 | 63 | 64 | 65 | /** 66 | Dispatch a Query request to the Receiver. 67 | */ 68 | void dispatch_query( 69 | std::unique_ptr rop, 70 | network::ZMQReceiverChannel &channel); 71 | 72 | void dispatch_re( 73 | std::unique_ptr rop, 74 | network::ZMQReceiverChannel &channel); 75 | 76 | }; // class ZMQReceiverDispatcher 77 | } // namespace receiver 78 | } // namespace apsu 79 | -------------------------------------------------------------------------------- /receiver/apsu/zmq/receiver_dispatcher_osn.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | 11 | // APSU 12 | #include "apsu/network/receiver_operation.h" 13 | #include "apsu/network/zmq/zmq_channel.h" 14 | #include "apsu/oprf/oprf_receiver.h" 15 | #include "apsu/receiver_osn.h" 16 | #include "apsu/receiver_db.h" 17 | 18 | namespace apsu { 19 | namespace receiver { 20 | /** 21 | The ZMQReceiverDispatcher is in charge of handling incoming requests through the network. 22 | */ 23 | class ZMQReceiverDispatcher { 24 | public: 25 | ZMQReceiverDispatcher() = delete; 26 | 27 | /** 28 | Creates a new ZMQReceiverDispatcher object. This constructor accepts both a ReceiverDB 29 | object, as well as a separately provided OPRF key. It uses the provided OPRF key to 30 | respond to OPRF requests, instead of attempting to retrieve a key from the ReceiverDB. 31 | This is necessary, for example, when the ReceiverDB is stripped, in which case it no 32 | longer carries a valid OPRF key. 33 | */ 34 | ZMQReceiverDispatcher(std::shared_ptr receiver_db, oprf::OPRFKey oprf_key,Receiver receiver); 35 | ZMQReceiverDispatcher(std::shared_ptr receiver_db,Receiver receiver); 36 | /** 37 | Creates a new ZMQReceiverDispatcher object. This constructor accepts a ReceiverDB object. It 38 | attempts to retrieve an OPRF key from the ReceiverDB and uses it to serve OPRF requests. 39 | This constructor cannot be used if the ReceiverDB is stripped, because the OPRF key is no 40 | longer available through the ReceiverDB. 41 | */ 42 | ZMQReceiverDispatcher(std::shared_ptr receiver_db); 43 | 44 | /** 45 | Run the dispatcher on the given port. 46 | */ 47 | void run(const std::atomic &stop, int port); 48 | 49 | private: 50 | std::shared_ptr receiver_db_; 51 | 52 | oprf::OPRFKey oprf_key_; 53 | 54 | Receiver receiver_; 55 | 56 | /** 57 | Dispatch a Get Parameters request to the Receiver. 58 | */ 59 | void dispatch_parms( 60 | std::unique_ptr rop, 61 | network::ZMQReceiverChannel &channel); 62 | 63 | 64 | /** 65 | Dispatch a Query request to the Receiver. 66 | */ 67 | void dispatch_query( 68 | std::unique_ptr rop, 69 | network::ZMQReceiverChannel &channel); 70 | 71 | void dispatch_re( 72 | std::unique_ptr rop, 73 | network::ZMQReceiverChannel &channel); 74 | 75 | }; // class ZMQReceiverDispatcher 76 | } // namespace receiver 77 | } // namespace apsu 78 | -------------------------------------------------------------------------------- /sender/apsu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Source files in this directory 5 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} 6 | ${CMAKE_CURRENT_LIST_DIR}/plaintext_powers.cpp 7 | ) 8 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} 9 | ${CMAKE_CURRENT_LIST_DIR}/plaintext_powers.cpp 10 | ) 11 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} 12 | ${CMAKE_CURRENT_LIST_DIR}/sender_osn.cpp 13 | ${CMAKE_CURRENT_LIST_DIR}/utils.cpp 14 | 15 | ) 16 | set(APSU_SOURCE_FILES_SENDER_DDH ${APSU_SOURCE_FILES_SENDER_DDH} 17 | ${CMAKE_CURRENT_LIST_DIR}/sender_ddh.cpp 18 | ${CMAKE_CURRENT_LIST_DIR}/utils.cpp 19 | ) 20 | # Add header files for installation 21 | install( 22 | FILES 23 | ${CMAKE_CURRENT_LIST_DIR}/itt.h 24 | ${CMAKE_CURRENT_LIST_DIR}/match_record.h 25 | ${CMAKE_CURRENT_LIST_DIR}/sender_osn.h 26 | ${CMAKE_CURRENT_LIST_DIR}/sender_ddh.h 27 | ${CMAKE_CURRENT_LIST_DIR}/utils.h 28 | DESTINATION 29 | ${APSU_INCLUDES_INSTALL_DIR}/apsu 30 | ) 31 | 32 | set(APSU_SOURCE_FILES_SENDER ${APSU_SOURCE_FILES_SENDER} PARENT_SCOPE) 33 | set(APSU_SOURCE_FILES ${APSU_SOURCE_FILES} PARENT_SCOPE) 34 | set(APSU_SOURCE_FILES_SENDER_OSN ${APSU_SOURCE_FILES_SENDER_OSN} PARENT_SCOPE) 35 | set(APSU_SOURCE_FILES_SENDER_DDH ${APSU_SOURCE_FILES_SENDER_DDH} PARENT_SCOPE) 36 | -------------------------------------------------------------------------------- /sender/apsu/itt.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | 10 | namespace apsu { 11 | namespace sender { 12 | class Sender; 13 | 14 | class IndexTranslationTable { 15 | friend class Sender; 16 | 17 | public: 18 | /** 19 | Translates a cuckoo table index to an index of the vector of items that were used to 20 | create this query. If the given table index was not populated, i.e., there is no 21 | translation, then this function returns the number of items encoded by this query. 22 | */ 23 | std::size_t find_item_idx(std::size_t table_idx) const noexcept; 24 | 25 | /** 26 | Returns the number of items encoded by this index translation table. 27 | */ 28 | std::size_t item_count() const noexcept 29 | { 30 | return item_count_; 31 | } 32 | 33 | private: 34 | IndexTranslationTable() = default; 35 | 36 | std::unordered_map table_idx_to_item_idx_; 37 | 38 | std::size_t item_count_; 39 | }; 40 | } // namespace sender 41 | } // namespace apsu 42 | -------------------------------------------------------------------------------- /sender/apsu/plaintext_powers.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | // STD 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // APSU 12 | #include "apsu/log.h" 13 | 14 | #include "apsu/util/utils.h" 15 | 16 | #include "apsu/plaintext_powers.h" 17 | // SEAL 18 | #include "seal/util/uintarithsmallmod.h" 19 | 20 | using namespace std; 21 | using namespace seal; 22 | using namespace seal::util; 23 | 24 | namespace apsu { 25 | namespace sender { 26 | PlaintextPowers::PlaintextPowers( 27 | vector values, const PSUParams ¶ms, const PowersDag &pd) 28 | : mod_(params.seal_params().plain_modulus()) 29 | { 30 | compute_powers(move(values), pd); 31 | } 32 | 33 | unordered_map> PlaintextPowers::encrypt( 34 | const CryptoContext &crypto_context) 35 | { 36 | if (!crypto_context.encryptor()) { 37 | throw invalid_argument("encryptor is not set in crypto_context"); 38 | } 39 | 40 | unordered_map> result; 41 | for (auto &p : powers_) { 42 | Plaintext pt; 43 | crypto_context.encoder()->encode(p.second, pt); 44 | result.emplace( 45 | make_pair(p.first, crypto_context.encryptor()->encrypt_symmetric(pt))); 46 | } 47 | 48 | return result; 49 | } 50 | 51 | void PlaintextPowers::square_array(gsl::span in) const 52 | { 53 | transform(in.begin(), in.end(), in.begin(), [this](auto val) { 54 | return multiply_uint_mod(val, val, mod_); 55 | }); 56 | } 57 | 58 | void PlaintextPowers::multiply_array( 59 | gsl::span in1, gsl::span in2, gsl::span out) const 60 | { 61 | transform( 62 | in1.begin(), in1.end(), in2.begin(), out.begin(), [this](auto val1, auto val2) { 63 | return multiply_uint_mod(val1, val2, mod_); 64 | }); 65 | } 66 | 67 | vector PlaintextPowers::exponentiate_array( 68 | vector values, uint32_t exponent) 69 | { 70 | if (!exponent) { 71 | throw invalid_argument("exponent cannot be zero"); 72 | } 73 | 74 | vector result(values.size(), 1); 75 | while (exponent) { 76 | if (exponent & 1) { 77 | multiply_array(values, result, result); 78 | } 79 | square_array(values); 80 | exponent >>= 1; 81 | } 82 | 83 | return result; 84 | } 85 | 86 | void PlaintextPowers::compute_powers(vector values, const PowersDag &pd) 87 | { 88 | auto source_powers = pd.source_nodes(); 89 | 90 | for (auto &s : source_powers) { 91 | powers_[s.power] = exponentiate_array(values, s.power); 92 | } 93 | 94 | vector powers_vec; 95 | transform(powers_.begin(), powers_.end(), back_inserter(powers_vec), [](auto &p) { 96 | return p.first; 97 | }); 98 | APSU_LOG_DEBUG("Plaintext powers computed: " << util::to_string(powers_vec)); 99 | } 100 | } // namespace sender 101 | } // namespace apsu 102 | -------------------------------------------------------------------------------- /sender/apsu/plaintext_powers.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | // STD 7 | #include 8 | #include 9 | #include 10 | 11 | // APSU 12 | #include "apsu/crypto_context.h" 13 | #include "apsu/powers.h" 14 | #include "apsu/psu_params.h" 15 | #include "apsu/seal_object.h" 16 | 17 | // SEAL 18 | #include "seal/ciphertext.h" 19 | #include "seal/modulus.h" 20 | 21 | // GSL 22 | #include "gsl/span" 23 | 24 | namespace apsu { 25 | namespace sender { 26 | class PlaintextPowers { 27 | public: 28 | PlaintextPowers( 29 | std::vector values, const PSUParams ¶ms, const PowersDag &pd); 30 | 31 | std::unordered_map> encrypt( 32 | const CryptoContext &crypto_context); 33 | 34 | private: 35 | seal::Modulus mod_; 36 | 37 | std::unordered_map> powers_; 38 | 39 | void square_array(gsl::span in) const; 40 | 41 | void multiply_array( 42 | gsl::span in1, 43 | gsl::span in2, 44 | gsl::span out) const; 45 | 46 | std::vector exponentiate_array( 47 | std::vector values, std::uint32_t exponent); 48 | 49 | void compute_powers(std::vector values, const PowersDag &pd); 50 | }; 51 | } // namespace sender 52 | } // namespace apsu 53 | -------------------------------------------------------------------------------- /sender/apsu/utils.h: -------------------------------------------------------------------------------- 1 | #include "coproto/Socket/AsioSocket.h" 2 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtReceiver.h" 3 | #include "libOTe/Base/BaseOT.h" 4 | #include "kuku/kuku.h" 5 | #include "libOTe/Tools/Coproto.h" 6 | namespace apsu{ 7 | namespace sender{ 8 | std::vector oprf_receiver(std::vector in,oc::Socket ); 9 | // void ResponseOT(oc::Socket socket,std::vector>& shuffleMessages); 10 | 11 | 12 | } 13 | 14 | 15 | 16 | } 17 | --------------------------------------------------------------------------------