1 // Copyright (c) Microsoft Corporation. All rights reserved. 2 // Licensed under the MIT license. 3 4 #include "seal/seal.h" 5 #include "seal/util/rlwe.h" 6 #include "bench.h" 7 8 using namespace benchmark; 9 using namespace sealbench; 10 using namespace seal; 11 using namespace std; 12 13 /** 14 This file defines benchmarks for KeyGen-related HE primitives. 15 */ 16 17 namespace seal 18 { 19 struct KeyGenerator::KeyGeneratorPrivateHelper 20 { generate_skseal::KeyGenerator::KeyGeneratorPrivateHelper21 static void generate_sk(KeyGenerator *keygen) 22 { 23 return keygen->generate_sk(); 24 } 25 }; 26 } // namespace seal 27 28 namespace sealbench 29 { bm_keygen_secret(State & state,shared_ptr<BMEnv> bm_env)30 void bm_keygen_secret(State &state, shared_ptr<BMEnv> bm_env) 31 { 32 KeyGenerator keygen(bm_env->context()); 33 for (auto _ : state) 34 { 35 KeyGenerator::KeyGeneratorPrivateHelper::generate_sk(&keygen); 36 } 37 } 38 bm_keygen_public(State & state,shared_ptr<BMEnv> bm_env)39 void bm_keygen_public(State &state, shared_ptr<BMEnv> bm_env) 40 { 41 shared_ptr<KeyGenerator> keygen = bm_env->keygen(); 42 PublicKey pk; 43 for (auto _ : state) 44 { 45 keygen->create_public_key(pk); 46 } 47 } 48 bm_keygen_relin(State & state,shared_ptr<BMEnv> bm_env)49 void bm_keygen_relin(State &state, shared_ptr<BMEnv> bm_env) 50 { 51 shared_ptr<KeyGenerator> keygen = bm_env->keygen(); 52 RelinKeys rlk; 53 for (auto _ : state) 54 { 55 keygen->create_relin_keys(rlk); 56 } 57 } 58 bm_keygen_galois(State & state,shared_ptr<BMEnv> bm_env)59 void bm_keygen_galois(State &state, shared_ptr<BMEnv> bm_env) 60 { 61 shared_ptr<KeyGenerator> keygen = bm_env->keygen(); 62 GaloisKeys glk; 63 size_t slot_count = bm_env->parms().poly_modulus_degree() >> 1; 64 65 auto random_one_step = [&]() { 66 vector<int> res; 67 res.emplace_back(rand() & static_cast<int>(slot_count - 1)); 68 return res; 69 }; 70 71 for (auto _ : state) 72 { 73 keygen->create_galois_keys({ random_one_step() }, glk); 74 } 75 } 76 } // namespace sealbench 77