Commit 1698a67c authored by Hoang Gia NGUYEN's avatar Hoang Gia NGUYEN
Browse files

testing

parent d8df7d47

Too many changes to show.

To preserve performance only 268 of 268+ files are displayed.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/intencoder.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/intencoder.h"
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC IntegerEncoder_Create(void *context, void **encoder)
{
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(encoder, E_POINTER);
try
{
IntegerEncoder *intEncoder = new IntegerEncoder(sharedctx);
*encoder = intEncoder;
return S_OK;
}
catch (const std::invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC IntegerEncoder_Destroy(void *thisptr)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
delete intenc;
return S_OK;
}
SEAL_C_FUNC IntegerEncoder_Encode1(void *thisptr, int32_t value, void *plain)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *dest = FromVoid<Plaintext>(plain);
IfNullRet(dest, E_POINTER);
intenc->encode(value, *dest);
return S_OK;
}
SEAL_C_FUNC IntegerEncoder_Encode2(void *thisptr, uint32_t value, void *plain)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *dest = FromVoid<Plaintext>(plain);
IfNullRet(dest, E_POINTER);
intenc->encode(value, *dest);
return S_OK;
}
SEAL_C_FUNC IntegerEncoder_Encode3(void *thisptr, uint64_t value, void *plain)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *dest = FromVoid<Plaintext>(plain);
IfNullRet(dest, E_POINTER);
intenc->encode(value, *dest);
return S_OK;
}
SEAL_C_FUNC IntegerEncoder_Encode4(void *thisptr, int64_t value, void *plain)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *dest = FromVoid<Plaintext>(plain);
IfNullRet(dest, E_POINTER);
intenc->encode(value, *dest);
return S_OK;
}
SEAL_C_FUNC IntegerEncoder_Encode5(void *thisptr, void *biguint, void *plain)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
BigUInt *bui = FromVoid<BigUInt>(biguint);
IfNullRet(bui, E_POINTER);
Plaintext *dest = FromVoid<Plaintext>(plain);
IfNullRet(dest, E_POINTER);
intenc->encode(*bui, *dest);
return S_OK;
}
SEAL_C_FUNC IntegerEncoder_DecodeUInt32(void *thisptr, void *plainptr, uint32_t *result)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plainptr);
IfNullRet(plain, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = intenc->decode_uint32(*plain);
return S_OK;
}
catch (const std::invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC IntegerEncoder_DecodeUInt64(void *thisptr, void *plainptr, uint64_t *result)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plainptr);
IfNullRet(plain, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = intenc->decode_uint64(*plain);
return S_OK;
}
catch (const std::invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC IntegerEncoder_DecodeInt32(void *thisptr, void *plainptr, int32_t *result)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plainptr);
IfNullRet(plain, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = intenc->decode_int32(*plain);
return S_OK;
}
catch (const std::invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC IntegerEncoder_DecodeInt64(void *thisptr, void *plainptr, int64_t *result)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plainptr);
IfNullRet(plain, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = intenc->decode_int64(*plain);
return S_OK;
}
catch (std::invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC IntegerEncoder_DecodeBigUInt(void *thisptr, void *plainptr, void **biguint)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plainptr);
IfNullRet(plain, E_POINTER);
IfNullRet(biguint, E_POINTER);
try
{
BigUInt result = intenc->decode_biguint(*plain);
BigUInt *resultPtr = new BigUInt(result);
*biguint = resultPtr;
return S_OK;
}
catch (const std::invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC IntegerEncoder_PlainModulus(void *thisptr, void **smallModPtr)
{
IntegerEncoder *intenc = FromVoid<IntegerEncoder>(thisptr);
IfNullRet(intenc, E_POINTER);
IfNullRet(smallModPtr, E_POINTER);
Modulus *sm = new Modulus(intenc->plain_modulus());
*smallModPtr = sm;
return S_OK;
}
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
///////////////////////////////////////////////////////////////////////////
//
// This API is provided as a simple interface for Microsoft SEAL library
// that can be PInvoked by .Net code.
//
///////////////////////////////////////////////////////////////////////////
#include "seal/c/defines.h"
#include <stdint.h>
SEAL_C_FUNC IntegerEncoder_Create(void *context, void **encoder);
SEAL_C_FUNC IntegerEncoder_Destroy(void *thisptr);
SEAL_C_FUNC IntegerEncoder_Encode1(void *thisptr, int32_t value, void *plain);
SEAL_C_FUNC IntegerEncoder_Encode2(void *thisptr, uint32_t value, void *plain);
SEAL_C_FUNC IntegerEncoder_Encode3(void *thisptr, uint64_t value, void *plain);
SEAL_C_FUNC IntegerEncoder_Encode4(void *thisptr, int64_t value, void *plain);
SEAL_C_FUNC IntegerEncoder_Encode5(void *thisptr, void *biguint, void *plain);
SEAL_C_FUNC IntegerEncoder_DecodeUInt32(void *thisptr, void *plain, uint32_t *result);
SEAL_C_FUNC IntegerEncoder_DecodeUInt64(void *thisptr, void *plain, uint64_t *result);
SEAL_C_FUNC IntegerEncoder_DecodeInt32(void *thisptr, void *plain, int32_t *result);
SEAL_C_FUNC IntegerEncoder_DecodeInt64(void *thisptr, void *plain, int64_t *result);
SEAL_C_FUNC IntegerEncoder_DecodeBigUInt(void *thisptr, void *plain, void **biguint);
SEAL_C_FUNC IntegerEncoder_PlainModulus(void *thisptr, void **smallModulus);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// STD
#include <algorithm>
#include <iterator>
// SEALNet
#include "seal/c/keygenerator.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/keygenerator.h"
#include "seal/util/common.h"
using namespace std;
using namespace seal;
using namespace seal::c;
using namespace seal::util;
struct seal::KeyGenerator::KeyGeneratorPrivateHelper
{
static RelinKeys relin_keys(KeyGenerator *keygen, bool save_seed)
{
return keygen->relin_keys(size_t(1), save_seed);
}
static GaloisKeys galois_keys(KeyGenerator *keygen, const vector<uint32_t> &galois_elts, bool save_seed)
{
return keygen->galois_keys(galois_elts, save_seed);
}
static const GaloisTool *galois_tool(KeyGenerator *keygen)
{
return keygen->context_->key_context_data()->galois_tool();
}
static bool using_keyswitching(const KeyGenerator &keygen)
{
return keygen.context_->using_keyswitching();
}
};
SEAL_C_FUNC KeyGenerator_Create1(void *sealContext, void **key_generator)
{
const auto &sharedctx = SharedContextFromVoid(sealContext);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(key_generator, E_POINTER);
try
{
KeyGenerator *keygen = new KeyGenerator(sharedctx);
*key_generator = keygen;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC KeyGenerator_Create2(void *sealContext, void *secret_key, void **key_generator)
{
const auto &sharedctx = SharedContextFromVoid(sealContext);
IfNullRet(sharedctx.get(), E_POINTER);
SecretKey *secret_key_ptr = FromVoid<SecretKey>(secret_key);
IfNullRet(secret_key_ptr, E_POINTER);
IfNullRet(key_generator, E_POINTER);
try
{
KeyGenerator *keygen = new KeyGenerator(sharedctx, *secret_key_ptr);
*key_generator = keygen;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC KeyGenerator_Destroy(void *thisptr)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
delete keygen;
return S_OK;
}
SEAL_C_FUNC KeyGenerator_RelinKeys(void *thisptr, bool save_seed, void **relin_keys)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(relin_keys, E_POINTER);
try
{
RelinKeys *relinKeys = new RelinKeys(KeyGenerator::KeyGeneratorPrivateHelper::relin_keys(keygen, save_seed));
*relin_keys = relinKeys;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC KeyGenerator_GaloisKeysFromElts(
void *thisptr, uint64_t count, uint32_t *galois_elts, bool save_seed, void **galois_keys)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(galois_elts, E_POINTER);
IfNullRet(galois_keys, E_POINTER);
vector<uint32_t> galois_elts_vec;
copy_n(galois_elts, count, back_inserter(galois_elts_vec));
try
{
GaloisKeys *keys =
new GaloisKeys(KeyGenerator::KeyGeneratorPrivateHelper::galois_keys(keygen, galois_elts_vec, save_seed));
*galois_keys = keys;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC KeyGenerator_GaloisKeysFromSteps(
void *thisptr, uint64_t count, int *steps, bool save_seed, void **galois_keys)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(steps, E_POINTER);
IfNullRet(galois_keys, E_POINTER);
vector<int> steps_vec;
copy_n(steps, count, back_inserter(steps_vec));
vector<uint32_t> galois_elts_vec;
try
{
galois_elts_vec = KeyGenerator::KeyGeneratorPrivateHelper::galois_tool(keygen)->get_elts_from_steps(steps_vec);
GaloisKeys *keys =
new GaloisKeys(KeyGenerator::KeyGeneratorPrivateHelper::galois_keys(keygen, galois_elts_vec, save_seed));
*galois_keys = keys;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC KeyGenerator_GaloisKeysAll(void *thisptr, bool save_seed, void **galois_keys)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(galois_keys, E_POINTER);
vector<uint32_t> galois_elts_vec = KeyGenerator::KeyGeneratorPrivateHelper::galois_tool(keygen)->get_elts_all();
try
{
GaloisKeys *keys =
new GaloisKeys(KeyGenerator::KeyGeneratorPrivateHelper::galois_keys(keygen, galois_elts_vec, save_seed));
*galois_keys = keys;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC KeyGenerator_PublicKey(void *thisptr, void **public_key)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(public_key, E_POINTER);
PublicKey *key = new PublicKey(keygen->public_key());
*public_key = key;
return S_OK;
}
SEAL_C_FUNC KeyGenerator_SecretKey(void *thisptr, void **secret_key)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(secret_key, E_POINTER);
SecretKey *key = new SecretKey(keygen->secret_key());
*secret_key = key;
return S_OK;
}
SEAL_C_FUNC KeyGenerator_ContextUsingKeyswitching(void *thisptr, bool *using_keyswitching)
{
KeyGenerator *keygen = FromVoid<KeyGenerator>(thisptr);
IfNullRet(keygen, E_POINTER);
IfNullRet(using_keyswitching, E_POINTER);
*using_keyswitching = KeyGenerator::KeyGeneratorPrivateHelper::using_keyswitching(*keygen);
return S_OK;
}
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
///////////////////////////////////////////////////////////////////////////
//
// This API is provided as a simple interface for Microsoft SEAL library
// that can be PInvoked by .Net code.
//
///////////////////////////////////////////////////////////////////////////
#include "seal/c/defines.h"
#include <stdint.h>
SEAL_C_FUNC KeyGenerator_Create1(void *sealContext, void **key_generator);
SEAL_C_FUNC KeyGenerator_Create2(void *sealContext, void *secret_key, void **key_generator);
SEAL_C_FUNC KeyGenerator_Destroy(void *thisptr);
SEAL_C_FUNC KeyGenerator_RelinKeys(void *thisptr, bool save_seed, void **relin_keys);
SEAL_C_FUNC KeyGenerator_GaloisKeysFromElts(
void *thisptr, uint64_t count, uint32_t *galois_elts, bool save_seed, void **galois_keys);
SEAL_C_FUNC KeyGenerator_GaloisKeysFromSteps(
void *thisptr, uint64_t count, int *steps, bool save_seed, void **galois_keys);
SEAL_C_FUNC KeyGenerator_GaloisKeysAll(void *thisptr, bool save_seed, void **galois_keys);
SEAL_C_FUNC KeyGenerator_PublicKey(void *thisptr, void **public_key);
SEAL_C_FUNC KeyGenerator_SecretKey(void *thisptr, void **secret_key);
SEAL_C_FUNC KeyGenerator_ContextUsingKeyswitching(void *thisptr, bool *using_keyswitching);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/kswitchkeys.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/kswitchkeys.h"
using namespace std;
using namespace seal;
using namespace seal::c;
namespace
{
HRESULT GetKeyFromVector(const vector<PublicKey> &key, uint64_t *count, void **key_list)
{
*count = key.size();
if (nullptr == key_list)
{
// We only wanted the count
return S_OK;
}
auto pkeys = reinterpret_cast<PublicKey **>(key_list);
for (size_t i = 0; i < key.size(); i++)
{
pkeys[i] = new PublicKey(key[i]);
}
return S_OK;
}
} // namespace
namespace seal
{
struct PublicKey::PublicKeyPrivateHelper
{
inline static PublicKey Create(MemoryPoolHandle pool)
{
return PublicKey(pool);
}
};
} // namespace seal
SEAL_C_FUNC KSwitchKeys_Create1(void **kswitch_keys)
{
IfNullRet(kswitch_keys, E_POINTER);
KSwitchKeys *keys = new KSwitchKeys();
*kswitch_keys = keys;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Create2(void *copy, void **kswitch_keys)
{
KSwitchKeys *copyptr = FromVoid<KSwitchKeys>(copy);
IfNullRet(copyptr, E_POINTER);
IfNullRet(kswitch_keys, E_POINTER);
KSwitchKeys *keys = new KSwitchKeys(*copyptr);
*kswitch_keys = keys;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Destroy(void *thisptr)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
delete keys;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Set(void *thisptr, void *assign)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
KSwitchKeys *assignptr = FromVoid<KSwitchKeys>(assign);
IfNullRet(assignptr, E_POINTER);
*keys = *assignptr;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Size(void *thisptr, uint64_t *size)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(size, E_POINTER);
*size = keys->size();
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_RawSize(void *thisptr, uint64_t *size)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(size, E_POINTER);
*size = keys->data().size();
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_GetKeyList(void *thisptr, uint64_t index, uint64_t *count, void **key_list)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(count, E_POINTER);
auto key = keys->data()[index];
return GetKeyFromVector(key, count, key_list);
}
SEAL_C_FUNC KSwitchKeys_ClearDataAndReserve(void *thisptr, uint64_t size)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
keys->data().clear();
keys->data().reserve(size);
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_AddKeyList(void *thisptr, uint64_t count, void **key_list)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(key_list, E_POINTER);
PublicKey **key = reinterpret_cast<PublicKey **>(key_list);
// Don't resize, only reserve
keys->data().emplace_back();
keys->data().back().reserve(count);
for (uint64_t i = 0; i < count; i++)
{
PublicKey *pkey = key[i];
PublicKey new_pkey(PublicKey::PublicKeyPrivateHelper::Create(keys->pool()));
new_pkey = *pkey;
keys->data().back().emplace_back(move(new_pkey));
}
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_GetParmsId(void *thisptr, uint64_t *parms_id)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(parms_id, E_POINTER);
for (size_t i = 0; i < keys->parms_id().size(); i++)
{
parms_id[i] = keys->parms_id()[i];
}
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_SetParmsId(void *thisptr, uint64_t *parms_id)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(parms_id, E_POINTER);
CopyParmsId(parms_id, keys->parms_id());
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Pool(void *thisptr, void **pool)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(pool, E_POINTER);
MemoryPoolHandle *handleptr = new MemoryPoolHandle(keys->pool());
*pool = handleptr;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = static_cast<int64_t>(keys->save_size(static_cast<compr_mode_type>(compr_mode)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC KSwitchKeys_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(outptr, E_POINTER);
IfNullRet(out_bytes, E_POINTER);
try
{
*out_bytes = util::safe_cast<int64_t>(keys->save(
reinterpret_cast<SEAL_BYTE *>(outptr), util::safe_cast<size_t>(size),
static_cast<compr_mode_type>(compr_mode)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
catch (const runtime_error &)
{
return COR_E_IO;
}
}
SEAL_C_FUNC KSwitchKeys_UnsafeLoad(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes = util::safe_cast<int64_t>(
keys->unsafe_load(sharedctx, reinterpret_cast<SEAL_BYTE *>(inptr), util::safe_cast<size_t>(size)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
catch (const runtime_error &)
{
return COR_E_IO;
}
}
SEAL_C_FUNC KSwitchKeys_Load(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes = util::safe_cast<int64_t>(
keys->load(sharedctx, reinterpret_cast<SEAL_BYTE *>(inptr), util::safe_cast<size_t>(size)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
catch (const runtime_error &)
{
return COR_E_IO;
}
}
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
///////////////////////////////////////////////////////////////////////////
//
// This API is provided as a simple interface for Microsoft SEAL library
// that can be PInvoked by .Net code.
//
///////////////////////////////////////////////////////////////////////////
#include "seal/c/defines.h"
#include <stdint.h>
SEAL_C_FUNC KSwitchKeys_Create1(void **kswitch_keys);
SEAL_C_FUNC KSwitchKeys_Create2(void *copy, void **kswitch_keys);
SEAL_C_FUNC KSwitchKeys_Destroy(void *thisptr);
SEAL_C_FUNC KSwitchKeys_Set(void *thisptr, void *assign);
SEAL_C_FUNC KSwitchKeys_Size(void *thisptr, uint64_t *size);
SEAL_C_FUNC KSwitchKeys_RawSize(void *thisptr, uint64_t *key_count);
SEAL_C_FUNC KSwitchKeys_GetKeyList(void *thisptr, uint64_t index, uint64_t *count, void **key_list);
SEAL_C_FUNC KSwitchKeys_ClearDataAndReserve(void *thisptr, uint64_t size);
SEAL_C_FUNC KSwitchKeys_AddKeyList(void *thisptr, uint64_t count, void **key_list);
SEAL_C_FUNC KSwitchKeys_GetParmsId(void *thisptr, uint64_t *parms_id);
SEAL_C_FUNC KSwitchKeys_SetParmsId(void *thisptr, uint64_t *parms_id);
SEAL_C_FUNC KSwitchKeys_Pool(void *thisptr, void **pool);
SEAL_C_FUNC KSwitchKeys_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result);
SEAL_C_FUNC KSwitchKeys_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes);
SEAL_C_FUNC KSwitchKeys_UnsafeLoad(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes);
SEAL_C_FUNC KSwitchKeys_Load(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/memorymanager.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/memorymanager.h"
using namespace std;
using namespace seal;
using namespace seal::c;
namespace
{
template <class T>
HRESULT GenericCreateProfileCopy(T *original, MMProf **copyptr)
{
T *copy = new T(*original);
*copyptr = copy;
return S_OK;
}
HRESULT CreateProfileCopy(MMProf *profile, MMProf **copyptr)
{
IfNullRet(profile, E_POINTER);
IfNullRet(copyptr, E_POINTER);
MMProfGlobal *global = dynamic_cast<MMProfGlobal *>(profile);
if (nullptr != global)
{
return GenericCreateProfileCopy(global, copyptr);
}
MMProfFixed *fixed = dynamic_cast<MMProfFixed *>(profile);
if (nullptr != fixed)
{
return GenericCreateProfileCopy(fixed, copyptr);
}
MMProfNew *newprof = dynamic_cast<MMProfNew *>(profile);
if (nullptr != newprof)
{
return GenericCreateProfileCopy(newprof, copyptr);
}
MMProfThreadLocal *threadlocal = dynamic_cast<MMProfThreadLocal *>(profile);
if (nullptr != threadlocal)
{
return GenericCreateProfileCopy(threadlocal, copyptr);
}
// No matching profile.
return E_UNEXPECTED;
}
} // namespace
SEAL_C_FUNC MemoryManager_GetPool1(int prof_opt, bool clear_on_destruction, void **pool_handle)
{
IfNullRet(pool_handle, E_POINTER);
mm_prof_opt profile_opt = static_cast<mm_prof_opt>(prof_opt);
MemoryPoolHandle handle;
// clear_on_destruction is only used when using FORCE_NEW
if (profile_opt == mm_prof_opt::FORCE_NEW)
{
handle = MemoryManager::GetPool(profile_opt, clear_on_destruction);
}
else
{
handle = MemoryManager::GetPool(profile_opt);
}
MemoryPoolHandle *handle_ptr = new MemoryPoolHandle(move(handle));
*pool_handle = handle_ptr;
return S_OK;
}
SEAL_C_FUNC MemoryManager_GetPool2(void **pool_handle)
{
IfNullRet(pool_handle, E_POINTER);
MemoryPoolHandle handle = MemoryManager::GetPool();
MemoryPoolHandle *handle_ptr = new MemoryPoolHandle(move(handle));
*pool_handle = handle_ptr;
return S_OK;
}
SEAL_C_FUNC MemoryManager_SwitchProfile(void *new_profile)
{
MMProf *profile = FromVoid<MMProf>(new_profile);
IfNullRet(profile, E_POINTER);
// SwitchProfile takes ownership of the profile pointer that is passed.
// The managed side will keep ownership of the new_profile parameter, so we
// need to make a copy that will be owned by the Memory Manager.
MMProf *new_mm_profile = nullptr;
IfFailRet(CreateProfileCopy(profile, &new_mm_profile));
MemoryManager::SwitchProfile(static_cast<MMProf *>(new_mm_profile));
return S_OK;
}
SEAL_C_FUNC MMProf_CreateGlobal(void **profile)
{
IfNullRet(profile, E_POINTER);
MMProfGlobal *global = new MMProfGlobal();
*profile = global;
return S_OK;
}
SEAL_C_FUNC MMProf_CreateFixed(void *pool, void **profile)
{
MemoryPoolHandle *poolptr = FromVoid<MemoryPoolHandle>(pool);
IfNullRet(poolptr, E_POINTER);
IfNullRet(profile, E_POINTER);
MemoryPoolHandle myhandle(*poolptr);
MMProfFixed *fixed = new MMProfFixed(myhandle);
*profile = fixed;
return S_OK;
}
SEAL_C_FUNC MMProf_CreateNew(void **profile)
{
IfNullRet(profile, E_POINTER);
MMProfNew *newprof = new MMProfNew();
*profile = newprof;
return S_OK;
}
SEAL_C_FUNC MMProf_CreateThreadLocal(void **profile)
{
IfNullRet(profile, E_POINTER);
MMProfThreadLocal *threadlocal = new MMProfThreadLocal();
*profile = threadlocal;
return S_OK;
}
SEAL_C_FUNC MMProf_GetPool(void *thisptr, void **pool_handle)
{
MMProf *profile = FromVoid<MMProf>(thisptr);
IfNullRet(profile, E_POINTER);
IfNullRet(pool_handle, E_POINTER);
// The parameter to get_pool is always ignored, so just pass 0
MemoryPoolHandle *handle_ptr = new MemoryPoolHandle(profile->get_pool(0));
*pool_handle = handle_ptr;
return S_OK;
}
SEAL_C_FUNC MMProf_Destroy(void *thisptr)
{
MMProf *profile = FromVoid<MMProf>(thisptr);
IfNullRet(profile, E_POINTER);
delete profile;
return S_OK;
}
\ No newline at end of file
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
///////////////////////////////////////////////////////////////////////////
//
// This API is provided as a simple interface for Microsoft SEAL library
// that can be PInvoked by .Net code.
//
///////////////////////////////////////////////////////////////////////////
#include "seal/c/defines.h"
#include <stdint.h>
SEAL_C_FUNC MemoryManager_GetPool1(int prof_opt, bool clear_on_destruction, void **pool_handle);
SEAL_C_FUNC MemoryManager_GetPool2(void **pool_handle);
SEAL_C_FUNC MemoryManager_SwitchProfile(void *new_profile);
SEAL_C_FUNC MMProf_CreateGlobal(void **profile);
SEAL_C_FUNC MMProf_CreateFixed(void *pool, void **profile);
SEAL_C_FUNC MMProf_CreateNew(void **profile);
SEAL_C_FUNC MMProf_CreateThreadLocal(void **profile);
SEAL_C_FUNC MMProf_GetPool(void *thisptr, void **pool_handle);
SEAL_C_FUNC MMProf_Destroy(void *thisptr);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment