Commit c6c9d2f5 authored by Hoang Gia NGUYEN's avatar Hoang Gia NGUYEN
Browse files

first commit

parent fe2d6195
Pipeline #60 failed with stages
in 0 seconds

Too many changes to show.

To preserve performance only 275 of 275+ files are displayed.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// STD
#include <string.h>
// SEALNet
#include "seal/c/encryptionparameterqualifiers.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/context.h"
using namespace std;
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC EPQ_Create(void *copy, void **epq)
{
EncryptionParameterQualifiers *copyptr = FromVoid<EncryptionParameterQualifiers>(copy);
IfNullRet(copyptr, E_POINTER);
IfNullRet(epq, E_POINTER);
EncryptionParameterQualifiers *result = new EncryptionParameterQualifiers(*copyptr);
*epq = result;
return S_OK;
}
SEAL_C_FUNC EPQ_Destroy(void *thisptr)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
delete epq;
return S_OK;
}
SEAL_C_FUNC EPQ_ParametersSet(void *thisptr, bool *parameters_set)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(parameters_set, E_POINTER);
*parameters_set = epq->parameters_set();
return S_OK;
}
SEAL_C_FUNC EPQ_UsingFFT(void *thisptr, bool *using_fft)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(using_fft, E_POINTER);
*using_fft = epq->using_fft;
return S_OK;
}
SEAL_C_FUNC EPQ_UsingNTT(void *thisptr, bool *using_ntt)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(using_ntt, E_POINTER);
*using_ntt = epq->using_ntt;
return S_OK;
}
SEAL_C_FUNC EPQ_UsingBatching(void *thisptr, bool *using_batching)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(using_batching, E_POINTER);
*using_batching = epq->using_batching;
return S_OK;
}
SEAL_C_FUNC EPQ_UsingFastPlainLift(void *thisptr, bool *using_fast_plain_lift)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(using_fast_plain_lift, E_POINTER);
*using_fast_plain_lift = epq->using_fast_plain_lift;
return S_OK;
}
SEAL_C_FUNC EPQ_UsingDescendingModulusChain(void *thisptr, bool *using_descending_modulus_chain)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(using_descending_modulus_chain, E_POINTER);
*using_descending_modulus_chain = epq->using_descending_modulus_chain;
return S_OK;
}
SEAL_C_FUNC EPQ_SecLevel(void *thisptr, int *sec_level)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(sec_level, E_POINTER);
*sec_level = static_cast<int>(epq->sec_level);
return S_OK;
}
SEAL_C_FUNC EPQ_ParameterErrorName(void *thisptr, char *outstr, uint64_t *length)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(length, E_POINTER);
return ToStringHelper2(epq->parameter_error_name(), outstr, length);
}
SEAL_C_FUNC EPQ_ParameterErrorMessage(void *thisptr, char *outstr, uint64_t *length)
{
EncryptionParameterQualifiers *epq = FromVoid<EncryptionParameterQualifiers>(thisptr);
IfNullRet(epq, E_POINTER);
IfNullRet(length, E_POINTER);
return ToStringHelper2(epq->parameter_error_message(), outstr, length);
}
// 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 EPQ_Create(void *copy, void **epq);
SEAL_C_FUNC EPQ_Destroy(void *thisptr);
SEAL_C_FUNC EPQ_ParametersSet(void *thisptr, bool *parameters_set);
SEAL_C_FUNC EPQ_UsingFFT(void *thisptr, bool *using_fft);
SEAL_C_FUNC EPQ_UsingNTT(void *thisptr, bool *using_ntt);
SEAL_C_FUNC EPQ_UsingBatching(void *thisptr, bool *using_batching);
SEAL_C_FUNC EPQ_UsingFastPlainLift(void *thisptr, bool *using_fast_plain_lift);
SEAL_C_FUNC EPQ_UsingDescendingModulusChain(void *thisptr, bool *using_descending_modulus_chain);
SEAL_C_FUNC EPQ_SecLevel(void *thisptr, int *sec_level);
SEAL_C_FUNC EPQ_ParameterErrorName(void *thisptr, char *outstr, uint64_t *length);
SEAL_C_FUNC EPQ_ParameterErrorMessage(void *thisptr, char *outstr, uint64_t *length);
\ No newline at end of file
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/encryptionparameters.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/encryptionparams.h"
#include "seal/modulus.h"
#include "seal/util/common.h"
#include "seal/util/hash.h"
using namespace std;
using namespace seal;
using namespace seal::c;
namespace seal
{
/**
Enables access to private members of seal::EncryptionParameters.
*/
struct EncryptionParameters::EncryptionParametersPrivateHelper
{
static auto parms_id(const EncryptionParameters &parms)
{
return parms.parms_id();
}
};
} // namespace seal
SEAL_C_FUNC EncParams_Create1(uint8_t scheme, void **enc_params)
{
IfNullRet(enc_params, E_POINTER);
try
{
EncryptionParameters *params = new EncryptionParameters(scheme);
*enc_params = params;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC EncParams_Create2(void *copy, void **enc_params)
{
EncryptionParameters *copypt = FromVoid<EncryptionParameters>(copy);
IfNullRet(copypt, E_POINTER);
IfNullRet(enc_params, E_POINTER);
EncryptionParameters *params = new EncryptionParameters(*copypt);
*enc_params = params;
return S_OK;
}
SEAL_C_FUNC EncParams_Destroy(void *thisptr)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
delete params;
return S_OK;
}
SEAL_C_FUNC EncParams_Set(void *thisptr, void *assign)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
EncryptionParameters *assignpt = FromVoid<EncryptionParameters>(assign);
IfNullRet(assignpt, E_POINTER);
*params = *assignpt;
return S_OK;
}
SEAL_C_FUNC EncParams_GetPolyModulusDegree(void *thisptr, uint64_t *degree)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(degree, E_POINTER);
*degree = params->poly_modulus_degree();
return S_OK;
}
SEAL_C_FUNC EncParams_SetPolyModulusDegree(void *thisptr, uint64_t degree)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
try
{
params->set_poly_modulus_degree(degree);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC EncParams_GetCoeffModulus(void *thisptr, uint64_t *length, void **coeffs)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(length, E_POINTER);
BuildModulusPointers(params->coeff_modulus(), length, coeffs);
return S_OK;
}
SEAL_C_FUNC EncParams_SetCoeffModulus(void *thisptr, uint64_t length, void **coeffs)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(coeffs, E_POINTER);
Modulus **coeff_array = reinterpret_cast<Modulus **>(coeffs);
vector<Modulus> coefficients(length);
for (uint64_t i = 0; i < length; i++)
{
coefficients[i] = *coeff_array[i];
}
try
{
params->set_coeff_modulus(coefficients);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC EncParams_GetScheme(void *thisptr, uint8_t *scheme)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(scheme, E_POINTER);
*scheme = static_cast<uint8_t>(params->scheme());
return S_OK;
}
SEAL_C_FUNC EncParams_GetParmsId(void *thisptr, uint64_t *parms_id)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(parms_id, E_POINTER);
// We will assume the array is always size hash_block_uint64_count
auto parmsid = EncryptionParameters::EncryptionParametersPrivateHelper::parms_id(*params);
for (size_t i = 0; i < util::HashFunction::hash_block_uint64_count; i++)
{
parms_id[i] = parmsid[i];
}
return S_OK;
}
SEAL_C_FUNC EncParams_GetPlainModulus(void *thisptr, void **plain_modulus)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(plain_modulus, E_POINTER);
const auto plainmodulus = &params->plain_modulus();
*plain_modulus = const_cast<Modulus *>(plainmodulus);
return S_OK;
}
SEAL_C_FUNC EncParams_SetPlainModulus1(void *thisptr, void *plain_modulus)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
Modulus *modulus = FromVoid<Modulus>(plain_modulus);
IfNullRet(modulus, E_POINTER);
try
{
params->set_plain_modulus(*modulus);
return S_OK;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC EncParams_SetPlainModulus2(void *thisptr, uint64_t plain_modulus)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
try
{
params->set_plain_modulus(plain_modulus);
return S_OK;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC EncParams_Equals(void *thisptr, void *otherptr, bool *result)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
EncryptionParameters *other = FromVoid<EncryptionParameters>(otherptr);
IfNullRet(other, E_POINTER);
IfNullRet(result, E_POINTER);
*result = (*params == *other);
return S_OK;
}
SEAL_C_FUNC EncParams_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = static_cast<int64_t>(params->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 EncParams_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(outptr, E_POINTER);
IfNullRet(out_bytes, E_POINTER);
try
{
*out_bytes = util::safe_cast<int64_t>(params->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 EncParams_Load(void *thisptr, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
EncryptionParameters *params = FromVoid<EncryptionParameters>(thisptr);
IfNullRet(params, E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes =
util::safe_cast<int64_t>(params->load(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 EncParams_Create1(uint8_t scheme, void **enc_params);
SEAL_C_FUNC EncParams_Create2(void *copy, void **enc_params);
SEAL_C_FUNC EncParams_Destroy(void *thisptr);
SEAL_C_FUNC EncParams_Set(void *thisptr, void *assign);
SEAL_C_FUNC EncParams_GetPolyModulusDegree(void *thisptr, uint64_t *degree);
SEAL_C_FUNC EncParams_SetPolyModulusDegree(void *thisptr, uint64_t degree);
SEAL_C_FUNC EncParams_GetCoeffModulus(void *thisptr, uint64_t *length, void **coeffs);
SEAL_C_FUNC EncParams_SetCoeffModulus(void *thisptr, uint64_t length, void **coeffs);
SEAL_C_FUNC EncParams_GetScheme(void *thisptr, uint8_t *scheme);
SEAL_C_FUNC EncParams_GetParmsId(void *thisptr, uint64_t *parms_id);
SEAL_C_FUNC EncParams_GetPlainModulus(void *thisptr, void **plain_modulus);
SEAL_C_FUNC EncParams_SetPlainModulus1(void *thisptr, void *modulus);
SEAL_C_FUNC EncParams_SetPlainModulus2(void *thisptr, uint64_t plain_modulus);
SEAL_C_FUNC EncParams_Equals(void *thisptr, void *otherptr, bool *result);
SEAL_C_FUNC EncParams_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result);
SEAL_C_FUNC EncParams_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes);
SEAL_C_FUNC EncParams_Load(void *thisptr, 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/encryptor.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/encryptor.h"
using namespace std;
using namespace seal;
using namespace seal::c;
struct seal::Encryptor::EncryptorPrivateHelper
{
static void encrypt_symmetric_internal(
Encryptor *encryptor, const Plaintext &plain, bool save_seed, Ciphertext &destination, MemoryPoolHandle pool)
{
encryptor->encrypt_internal(plain, false, save_seed, destination, pool);
}
static void encrypt_zero_symmetric_internal(
Encryptor *encryptor, parms_id_type parms_id, bool save_seed, Ciphertext &destination, MemoryPoolHandle pool)
{
encryptor->encrypt_zero_internal(parms_id, false, save_seed, destination, pool);
}
static void encrypt_zero_symmetric_internal(
Encryptor *encryptor, bool save_seed, Ciphertext &destination, MemoryPoolHandle pool)
{
encryptor->encrypt_zero_internal(encryptor->context_->first_parms_id(), false, save_seed, destination, pool);
}
};
SEAL_C_FUNC Encryptor_Create(void *context, void *public_key, void *secret_key, void **encryptor)
{
PublicKey *pkey = FromVoid<PublicKey>(public_key);
SecretKey *skey = FromVoid<SecretKey>(secret_key);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(encryptor, E_POINTER);
if (nullptr == pkey && nullptr == skey)
{
return E_POINTER;
}
try
{
Encryptor *enc;
if (nullptr != pkey)
{
enc = new Encryptor(sharedctx, *pkey);
if (nullptr != skey)
{
enc->set_secret_key(*skey);
}
}
else
{
enc = new Encryptor(sharedctx, *skey);
}
*encryptor = enc;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Encryptor_SetPublicKey(void *thisptr, void *public_key)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
PublicKey *pkey = FromVoid<PublicKey>(public_key);
IfNullRet(pkey, E_POINTER);
try
{
encryptor->set_public_key(*pkey);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Encryptor_SetSecretKey(void *thisptr, void *secret_key)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
SecretKey *skey = FromVoid<SecretKey>(secret_key);
IfNullRet(skey, E_POINTER);
try
{
encryptor->set_secret_key(*skey);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Encryptor_Encrypt(void *thisptr, void *plaintext, void *destination, void *pool_handle)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plaintext);
IfNullRet(plain, E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(destination);
IfNullRet(cipher, E_POINTER);
unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
try
{
encryptor->encrypt(*plain, *cipher, *pool);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Encryptor_EncryptZero1(void *thisptr, uint64_t *parms_id, void *destination, void *pool_handle)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
IfNullRet(parms_id, E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(destination);
IfNullRet(cipher, E_POINTER);
unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
encryptor->encrypt_zero(parms, *cipher, *pool);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Encryptor_EncryptZero2(void *thisptr, void *destination, void *pool_handle)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(destination);
IfNullRet(cipher, E_POINTER);
unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
try
{
encryptor->encrypt_zero(*cipher, *pool);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Encryptor_EncryptSymmetric(
void *thisptr, void *plaintext, bool save_seed, void *destination, void *pool_handle)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
Plaintext *plain = FromVoid<Plaintext>(plaintext);
IfNullRet(plain, E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(destination);
IfNullRet(cipher, E_POINTER);
unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
try
{
Encryptor::EncryptorPrivateHelper::encrypt_symmetric_internal(encryptor, *plain, save_seed, *cipher, *pool);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Encryptor_EncryptZeroSymmetric1(
void *thisptr, uint64_t *parms_id, bool save_seed, void *destination, void *pool_handle)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
IfNullRet(parms_id, E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(destination);
IfNullRet(cipher, E_POINTER);
unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
Encryptor::EncryptorPrivateHelper::encrypt_zero_symmetric_internal(encryptor, parms, save_seed, *cipher, *pool);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Encryptor_EncryptZeroSymmetric2(void *thisptr, bool save_seed, void *destination, void *pool_handle)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(destination);
IfNullRet(cipher, E_POINTER);
unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
try
{
Encryptor::EncryptorPrivateHelper::encrypt_zero_symmetric_internal(encryptor, save_seed, *cipher, *pool);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Encryptor_Destroy(void *thisptr)
{
Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
IfNullRet(encryptor, E_POINTER);
delete encryptor;
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 Encryptor_Create(void *context, void *public_key, void *secret_key, void **encryptor);
SEAL_C_FUNC Encryptor_SetPublicKey(void *thisptr, void *public_key);
SEAL_C_FUNC Encryptor_SetSecretKey(void *thisptr, void *secret_key);
SEAL_C_FUNC Encryptor_Encrypt(void *thisptr, void *plaintext, void *destination, void *pool_handle);
SEAL_C_FUNC Encryptor_EncryptZero1(void *thisptr, uint64_t *parms_id, void *destination, void *pool_handle);
SEAL_C_FUNC Encryptor_EncryptZero2(void *thisptr, void *destination, void *pool_handle);
SEAL_C_FUNC Encryptor_EncryptSymmetric(
void *thisptr, void *plaintext, bool save_seed, void *destination, void *pool_handle);
SEAL_C_FUNC Encryptor_EncryptZeroSymmetric1(
void *thisptr, uint64_t *parms_id, bool save_seed, void *destination, void *pool_handle);
SEAL_C_FUNC Encryptor_EncryptZeroSymmetric2(void *thisptr, bool save_seed, void *destination, void *pool_handle);
SEAL_C_FUNC Encryptor_Destroy(void *thisptr);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// STD
#include <algorithm>
#include <iterator>
// SEALNet
#include "seal/c/evaluator.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/context.h"
#include "seal/evaluator.h"
#include "seal/util/common.h"
using namespace std;
using namespace seal;
using namespace seal::c;
using namespace seal::util;
struct seal::Evaluator::EvaluatorPrivateHelper
{
static bool using_keyswitching(const Evaluator &ev)
{
return ev.context_->using_keyswitching();
}
};
SEAL_C_FUNC Evaluator_Create(void *sealContext, void **evaluator)
{
SEALContext *context = FromVoid<SEALContext>(sealContext);
IfNullRet(context, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(sealContext);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(evaluator, E_POINTER);
try
{
Evaluator *eval = new Evaluator(sharedctx);
*evaluator = eval;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Evaluator_Destroy(void *thisptr)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
delete eval;
return S_OK;
}
SEAL_C_FUNC Evaluator_Negate(void *thisptr, void *encrypted, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->negate(*encrypted_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_Add(void *thisptr, void *encrypted1, void *encrypted2, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted1_ptr = FromVoid<Ciphertext>(encrypted1);
IfNullRet(encrypted1_ptr, E_POINTER);
Ciphertext *encrypted2_ptr = FromVoid<Ciphertext>(encrypted2);
IfNullRet(encrypted2_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->add(*encrypted1_ptr, *encrypted2_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_AddMany(void *thisptr, uint64_t count, void **encrypteds, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
IfNullRet(encrypteds, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
Ciphertext **encrypteds_pp = reinterpret_cast<Ciphertext **>(encrypteds);
vector<Ciphertext> encrypteds_vec;
encrypteds_vec.reserve(count);
for (uint64_t i = 0; i < count; i++)
{
encrypteds_vec.emplace_back(*encrypteds_pp[i]);
}
try
{
eval->add_many(encrypteds_vec, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_AddPlain(void *thisptr, void *encrypted, void *plain, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Plaintext *plain_ptr = FromVoid<Plaintext>(plain);
IfNullRet(plain_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->add_plain(*encrypted_ptr, *plain_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_Sub(void *thisptr, void *encrypted1, void *encrypted2, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted1_ptr = FromVoid<Ciphertext>(encrypted1);
IfNullRet(encrypted1_ptr, E_POINTER);
Ciphertext *encrypted2_ptr = FromVoid<Ciphertext>(encrypted2);
IfNullRet(encrypted2_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->sub(*encrypted1_ptr, *encrypted2_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_SubPlain(void *thisptr, void *encrypted, void *plain, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Plaintext *plain_ptr = FromVoid<Plaintext>(plain);
IfNullRet(plain_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->sub_plain(*encrypted_ptr, *plain_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_Multiply(void *thisptr, void *encrypted1, void *encrypted2, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted1_ptr = FromVoid<Ciphertext>(encrypted1);
IfNullRet(encrypted1_ptr, E_POINTER);
Ciphertext *encrypted2_ptr = FromVoid<Ciphertext>(encrypted2);
IfNullRet(encrypted2_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->multiply(*encrypted1_ptr, *encrypted2_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_MultiplyMany(
void *thisptr, uint64_t count, void **encrypteds, void *relin_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
IfNullRet(encrypteds, E_POINTER);
RelinKeys *relin_keys_ptr = FromVoid<RelinKeys>(relin_keys);
IfNullRet(relin_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
Ciphertext **encrypteds_pp = reinterpret_cast<Ciphertext **>(encrypteds);
vector<Ciphertext> encrypteds_vec;
encrypteds_vec.reserve(count);
for (uint64_t i = 0; i < count; i++)
{
encrypteds_vec.emplace_back(*encrypteds_pp[i]);
}
try
{
eval->multiply_many(encrypteds_vec, *relin_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_MultiplyPlain(void *thisptr, void *encrypted, void *plain, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Plaintext *plain_ptr = FromVoid<Plaintext>(plain);
IfNullRet(plain_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->multiply_plain(*encrypted_ptr, *plain_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_Square(void *thisptr, void *encrypted, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->square(*encrypted_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_Relinearize(void *thisptr, void *encrypted, void *relin_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
RelinKeys *relin_keys_ptr = FromVoid<RelinKeys>(relin_keys);
IfNullRet(relin_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->relinearize(*encrypted_ptr, *relin_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_ModSwitchToNext1(void *thisptr, void *encrypted, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->mod_switch_to_next(*encrypted_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_ModSwitchToNext2(void *thisptr, void *plain, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Plaintext *plain_ptr = FromVoid<Plaintext>(plain);
IfNullRet(plain_ptr, E_POINTER);
Plaintext *destination_ptr = FromVoid<Plaintext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->mod_switch_to_next(*plain_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Evaluator_ModSwitchTo1(void *thisptr, void *encrypted, uint64_t *parms_id, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
eval->mod_switch_to(*encrypted_ptr, parms, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_ModSwitchTo2(void *thisptr, void *plain, uint64_t *parms_id, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Plaintext *plain_ptr = FromVoid<Plaintext>(plain);
IfNullRet(plain_ptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
Plaintext *destination_ptr = FromVoid<Plaintext>(destination);
IfNullRet(destination_ptr, E_POINTER);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
eval->mod_switch_to(*plain_ptr, parms, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Evaluator_RescaleToNext(void *thisptr, void *encrypted, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->rescale_to_next(*encrypted_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_RescaleTo(void *thisptr, void *encrypted, uint64_t *parms_id, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
eval->rescale_to(*encrypted_ptr, parms, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_Exponentiate(
void *thisptr, void *encrypted, uint64_t exponent, void *relin_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
RelinKeys *relin_keys_ptr = FromVoid<RelinKeys>(relin_keys);
IfNullRet(relin_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->exponentiate(*encrypted_ptr, exponent, *relin_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_TransformToNTT1(void *thisptr, void *plain, uint64_t *parms_id, void *destination_ntt, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Plaintext *plain_ptr = FromVoid<Plaintext>(plain);
IfNullRet(plain_ptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
Plaintext *destination_ptr = FromVoid<Plaintext>(destination_ntt);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
eval->transform_to_ntt(*plain_ptr, parms, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Evaluator_TransformToNTT2(void *thisptr, void *encrypted, void *destination_ntt)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination_ntt);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->transform_to_ntt(*encrypted_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_TransformFromNTT(void *thisptr, void *encrypted_ntt, void *destination)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted_ntt);
IfNullRet(encrypted_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
try
{
eval->transform_from_ntt(*encrypted_ptr, *destination_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_ApplyGalois(
void *thisptr, void *encrypted, uint32_t galois_elt, void *galois_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
GaloisKeys *galois_keys_ptr = FromVoid<GaloisKeys>(galois_keys);
IfNullRet(galois_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->apply_galois(*encrypted_ptr, galois_elt, *galois_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_RotateRows(
void *thisptr, void *encrypted, int steps, void *galoisKeys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
GaloisKeys *galois_keys_ptr = FromVoid<GaloisKeys>(galoisKeys);
IfNullRet(galois_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->rotate_rows(*encrypted_ptr, steps, *galois_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_RotateColumns(void *thisptr, void *encrypted, void *galois_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
GaloisKeys *galois_keys_ptr = FromVoid<GaloisKeys>(galois_keys);
IfNullRet(galois_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->rotate_columns(*encrypted_ptr, *galois_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_RotateVector(
void *thisptr, void *encrypted, int steps, void *galois_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
GaloisKeys *galois_keys_ptr = FromVoid<GaloisKeys>(galois_keys);
IfNullRet(galois_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->rotate_vector(*encrypted_ptr, steps, *galois_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_ComplexConjugate(void *thisptr, void *encrypted, void *galois_keys, void *destination, void *pool)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
Ciphertext *encrypted_ptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encrypted_ptr, E_POINTER);
GaloisKeys *galois_keys_ptr = FromVoid<GaloisKeys>(galois_keys);
IfNullRet(galois_keys_ptr, E_POINTER);
Ciphertext *destination_ptr = FromVoid<Ciphertext>(destination);
IfNullRet(destination_ptr, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
eval->complex_conjugate(*encrypted_ptr, *galois_keys_ptr, *destination_ptr, *pool_ptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Evaluator_ContextUsingKeyswitching(void *thisptr, bool *using_keyswitching)
{
Evaluator *eval = FromVoid<Evaluator>(thisptr);
IfNullRet(eval, E_POINTER);
IfNullRet(using_keyswitching, E_POINTER);
*using_keyswitching = Evaluator::EvaluatorPrivateHelper::using_keyswitching(*eval);
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 Evaluator_Create(void *sealContext, void **evaluator);
SEAL_C_FUNC Evaluator_Destroy(void *thisptr);
SEAL_C_FUNC Evaluator_Negate(void *thisptr, void *encrypted, void *destination);
SEAL_C_FUNC Evaluator_Add(void *thisptr, void *encrypted1, void *encrypted2, void *destination);
SEAL_C_FUNC Evaluator_AddMany(void *thisptr, uint64_t count, void **encrypteds, void *destination);
SEAL_C_FUNC Evaluator_AddPlain(void *thisptr, void *encrypted, void *plain, void *destination);
SEAL_C_FUNC Evaluator_Sub(void *thisptr, void *encrypted1, void *encrypted2, void *destination);
SEAL_C_FUNC Evaluator_SubPlain(void *thisptr, void *encrypted, void *plain, void *destination);
SEAL_C_FUNC Evaluator_Multiply(void *thisptr, void *encrypted1, void *encrypted2, void *destination, void *pool);
SEAL_C_FUNC Evaluator_MultiplyMany(
void *thisptr, uint64_t count, void **encrypteds, void *relin_keys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_MultiplyPlain(void *thisptr, void *encrypted, void *plain, void *destination, void *pool);
SEAL_C_FUNC Evaluator_Square(void *thisptr, void *encrypted, void *destination, void *pool);
SEAL_C_FUNC Evaluator_Relinearize(void *thisptr, void *encrypted, void *relinKeys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_ModSwitchToNext1(void *thisptr, void *encrypted, void *destination, void *pool);
SEAL_C_FUNC Evaluator_ModSwitchToNext2(void *thisptr, void *plain, void *destination);
SEAL_C_FUNC Evaluator_ModSwitchTo1(void *thisptr, void *encrypted, uint64_t *parms_id, void *destination, void *pool);
SEAL_C_FUNC Evaluator_ModSwitchTo2(void *thisptr, void *plain, uint64_t *parms_id, void *destination);
SEAL_C_FUNC Evaluator_RescaleToNext(void *thisptr, void *encrypted, void *destination, void *pool);
SEAL_C_FUNC Evaluator_RescaleTo(void *thisptr, void *encrypted, uint64_t *parms_id, void *destination, void *pool);
SEAL_C_FUNC Evaluator_Exponentiate(
void *thisptr, void *encrypted, uint64_t exponent, void *relin_keys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_TransformToNTT1(
void *thisptr, void *plain, uint64_t *parms_id, void *destination_ntt, void *pool);
SEAL_C_FUNC Evaluator_TransformToNTT2(void *thisptr, void *encrypted, void *destination_ntt);
SEAL_C_FUNC Evaluator_TransformFromNTT(void *thisptr, void *encrypted_ntt, void *destination);
SEAL_C_FUNC Evaluator_ApplyGalois(
void *thisptr, void *encrypted, uint32_t galois_elt, void *galois_keys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_RotateRows(
void *thisptr, void *encrypted, int steps, void *galoisKeys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_RotateColumns(void *thisptr, void *encrypted, void *galois_keys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_RotateVector(
void *thisptr, void *encrypted, int steps, void *galois_keys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_ComplexConjugate(
void *thisptr, void *encrypted, void *galois_keys, void *destination, void *pool);
SEAL_C_FUNC Evaluator_ContextUsingKeyswitching(void *thisptr, bool *using_keyswitching);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/galoiskeys.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/galoiskeys.h"
using namespace std;
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC GaloisKeys_GetIndex(uint32_t galois_elt, uint64_t *index)
{
IfNullRet(index, E_POINTER);
try
{
*index = GaloisKeys::get_index(galois_elt);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
\ 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 GaloisKeys_GetIndex(uint32_t galois_elt, uint64_t *index);
// 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;
}
}
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