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

testing

parent c6c9d2f5

Too many changes to show.

To preserve performance only 269 of 269+ files are displayed.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// STD
#include <string>
// SEALNet
#include "seal/c/biguint.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/biguint.h"
using namespace std;
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC BigUInt_Create1(void **bui)
{
IfNullRet(bui, E_POINTER);
BigUInt *biguint = new BigUInt();
*bui = biguint;
return S_OK;
}
SEAL_C_FUNC BigUInt_Create2(int bitCount, void **bui)
{
IfNullRet(bui, E_POINTER);
try
{
BigUInt *biguint = new BigUInt(bitCount, /* value */ static_cast<uint64_t>(0));
*bui = biguint;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC BigUInt_Create3(int bitCount, char *hex_string, void **bui)
{
IfNullRet(hex_string, E_POINTER);
IfNullRet(bui, E_POINTER);
string hexstring(hex_string);
BigUInt *biguint = nullptr;
try
{
biguint = new BigUInt(bitCount, hexstring);
*bui = biguint;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC BigUInt_Create4(int bitCount, uint64_t value, void **bui)
{
IfNullRet(bui, E_POINTER);
try
{
BigUInt *biguint = new BigUInt(bitCount, value);
*bui = biguint;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC BigUInt_Create5(char *hex_string, void **bui)
{
IfNullRet(hex_string, E_POINTER);
IfNullRet(bui, E_POINTER);
string hexstring(hex_string);
BigUInt *biguint = nullptr;
try
{
biguint = new BigUInt(hexstring);
*bui = biguint;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC BigUInt_Create6(void *copy, void **bui)
{
BigUInt *other = FromVoid<BigUInt>(copy);
IfNullRet(other, E_POINTER);
IfNullRet(bui, E_POINTER);
BigUInt *biguint = new BigUInt(*other);
*bui = biguint;
return S_OK;
}
SEAL_C_FUNC BigUInt_Destroy(void *thisptr)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(thisptr, E_POINTER);
delete biguint;
return S_OK;
}
SEAL_C_FUNC BigUInt_IsAlias(void *thisptr, bool *is_alias)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(is_alias, E_POINTER);
*is_alias = biguint->is_alias();
return S_OK;
}
SEAL_C_FUNC BigUInt_BitCount(void *thisptr, int *bit_count)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(bit_count, E_POINTER);
*bit_count = biguint->bit_count();
return S_OK;
}
SEAL_C_FUNC BigUInt_ByteCount(void *thisptr, uint64_t *byte_count)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(byte_count, E_POINTER);
*byte_count = biguint->byte_count();
return S_OK;
}
SEAL_C_FUNC BigUInt_UInt64Count(void *thisptr, uint64_t *uint64_count)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(uint64_count, E_POINTER);
*uint64_count = biguint->uint64_count();
return S_OK;
}
SEAL_C_FUNC BigUInt_IsZero(void *thisptr, bool *is_zero)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(is_zero, E_POINTER);
*is_zero = biguint->is_zero();
return S_OK;
}
SEAL_C_FUNC BigUInt_Get(void *thisptr, uint64_t index, uint8_t *value)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(value, E_POINTER)
if (index >= biguint->byte_count())
{
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
}
auto result = (*biguint)[index];
*value = static_cast<uint8_t>(result);
return S_OK;
}
SEAL_C_FUNC BigUInt_GetU64(void *thisptr, uint64_t index, uint64_t *value)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(value, E_POINTER);
if (index >= biguint->uint64_count())
{
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
}
*value = biguint->data()[index];
return S_OK;
}
SEAL_C_FUNC BigUInt_Set1(void *thisptr, uint64_t index, uint8_t value)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
if (index >= biguint->byte_count())
{
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
}
(*biguint)[index] = static_cast<SEAL_BYTE>(value);
return S_OK;
}
SEAL_C_FUNC BigUInt_GetSignificantBitCount(void *thisptr, int *significant_bit_count)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(significant_bit_count, E_POINTER);
*significant_bit_count = biguint->significant_bit_count();
return S_OK;
}
SEAL_C_FUNC BigUInt_Set2(void *thisptr, void *assign)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *other = FromVoid<BigUInt>(assign);
IfNullRet(other, E_POINTER);
*biguint = *other;
return S_OK;
}
SEAL_C_FUNC BigUInt_Set3(void *thisptr, uint64_t value)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
try
{
*biguint = value;
return S_OK;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC BigUInt_Set4(void *thisptr, char *assign)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(assign, E_POINTER);
string assign_str(assign);
try
{
*biguint = assign_str;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC BigUInt_SetZero(void *thisptr)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
biguint->set_zero();
return S_OK;
}
SEAL_C_FUNC BigUInt_Resize(void *thisptr, int bitCount)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
biguint->resize(bitCount);
return S_OK;
}
SEAL_C_FUNC BigUInt_Equals(void *thisptr, void *compare, bool *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *other = FromVoid<BigUInt>(compare);
IfNullRet(other, E_POINTER);
IfNullRet(result, E_POINTER);
*result = (*biguint) == (*other);
return S_OK;
}
SEAL_C_FUNC BigUInt_CompareTo1(void *thisptr, void *compare, int *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *other = FromVoid<BigUInt>(compare);
IfNullRet(other, E_POINTER);
IfNullRet(result, E_POINTER);
*result = biguint->compareto(*other);
return S_OK;
}
SEAL_C_FUNC BigUInt_CompareTo2(void *thisptr, uint64_t compare, int *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
*result = biguint->compareto(compare);
return S_OK;
}
SEAL_C_FUNC BigUInt_DivideRemainder1(void *thisptr, void *operand2, void *remainder, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operand2bui = FromVoid<BigUInt>(operand2);
IfNullRet(operand2bui, E_POINTER);
BigUInt *remainderbui = FromVoid<BigUInt>(remainder);
IfNullRet(remainderbui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(biguint->divrem(*operand2bui, *remainderbui));
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_DivideRemainder2(void *thisptr, uint64_t operand2, void *remainder, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *remainderbui = FromVoid<BigUInt>(remainder);
IfNullRet(remainderbui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(biguint->divrem(operand2, *remainderbui));
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_ToString(void *thisptr, char *outstr, uint64_t *length)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(length, E_POINTER);
return ToStringHelper(biguint->to_string(), outstr, length);
}
SEAL_C_FUNC BigUInt_ToDecimalString(void *thisptr, char *outstr, uint64_t *length)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(length, E_POINTER);
return ToStringHelper(biguint->to_dec_string(), outstr, length);
}
SEAL_C_FUNC BigUInt_DuplicateTo(void *thisptr, void *destination)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *destbui = FromVoid<BigUInt>(destination);
IfNullRet(destbui, E_POINTER);
biguint->duplicate_to(*destbui);
return S_OK;
}
SEAL_C_FUNC BigUInt_DuplicateFrom(void *thisptr, void *value)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *valuebui = FromVoid<BigUInt>(value);
IfNullRet(valuebui, E_POINTER);
biguint->duplicate_from(*valuebui);
return S_OK;
}
SEAL_C_FUNC BigUInt_ModuloInvert1(void *thisptr, void *modulus, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *modulusui = FromVoid<BigUInt>(modulus);
IfNullRet(modulusui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = nullptr;
try
{
resultbui = new BigUInt(biguint->modinv(*modulusui));
*result = resultbui;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC BigUInt_ModuloInvert2(void *thisptr, uint64_t modulus, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *resultbui = nullptr;
try
{
resultbui = new BigUInt(biguint->modinv(modulus));
*result = resultbui;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC BigUInt_TryModuloInvert1(void *thisptr, void *modulus, void *inverse, bool *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *modulusui = FromVoid<BigUInt>(modulus);
IfNullRet(modulusui, E_POINTER);
BigUInt *inverseui = FromVoid<BigUInt>(inverse);
IfNullRet(inverseui, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = biguint->trymodinv(*modulusui, *inverseui);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC BigUInt_TryModuloInvert2(void *thisptr, uint64_t modulus, void *inverse, bool *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *inverseui = FromVoid<BigUInt>(inverse);
IfNullRet(inverseui, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = biguint->trymodinv(modulus, *inverseui);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC BigUInt_OperatorNeg(void *thisptr, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(biguint->operator-());
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorTilde(void *thisptr, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(biguint->operator~());
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorPlus1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint + *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorPlus2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint + operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorMinus1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint - *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorMinus2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint - operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorMult1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint * *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorMult2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint * operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorDiv1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint / *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorDiv2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint / operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorXor1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint ^ *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorXor2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint ^ operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorAnd1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint & *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorAnd2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint & operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorOr1(void *thisptr, void *operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
BigUInt *operandui = FromVoid<BigUInt>(operand);
IfNullRet(operandui, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint | *operandui);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorOr2(void *thisptr, uint64_t operand, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint | operand);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorShiftLeft(void *thisptr, int shift, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint << shift);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_OperatorShiftRight(void *thisptr, int shift, void **result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
BigUInt *resultbui = new BigUInt(*biguint >> shift);
*result = resultbui;
return S_OK;
}
SEAL_C_FUNC BigUInt_ToDouble(void *thisptr, double *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
*result = biguint->to_double();
return S_OK;
}
SEAL_C_FUNC BigUInt_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = static_cast<int64_t>(biguint->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 BigUInt_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(outptr, E_POINTER);
IfNullRet(out_bytes, E_POINTER);
try
{
*out_bytes = util::safe_cast<int64_t>(biguint->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 BigUInt_Load(void *thisptr, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
BigUInt *biguint = FromVoid<BigUInt>(thisptr);
IfNullRet(biguint, E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes = util::safe_cast<int64_t>(
biguint->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 BigUInt_Create1(void **bui);
SEAL_C_FUNC BigUInt_Create2(int bitCount, void **bui);
SEAL_C_FUNC BigUInt_Create3(int bitCount, char *hex_string, void **bui);
SEAL_C_FUNC BigUInt_Create4(int bitCount, uint64_t value, void **bui);
SEAL_C_FUNC BigUInt_Create5(char *hex_string, void **bui);
SEAL_C_FUNC BigUInt_Create6(void *copy, void **bui);
SEAL_C_FUNC BigUInt_Destroy(void *thispt);
SEAL_C_FUNC BigUInt_IsAlias(void *thisptr, bool *is_alias);
SEAL_C_FUNC BigUInt_BitCount(void *thisptr, int *bit_count);
SEAL_C_FUNC BigUInt_ByteCount(void *thisptr, uint64_t *byte_count);
SEAL_C_FUNC BigUInt_UInt64Count(void *thisptr, uint64_t *uint64_count);
SEAL_C_FUNC BigUInt_IsZero(void *thisptr, bool *is_zero);
SEAL_C_FUNC BigUInt_Get(void *thisptr, uint64_t index, uint8_t *value);
SEAL_C_FUNC BigUInt_GetU64(void *thisptr, uint64_t index, uint64_t *value);
SEAL_C_FUNC BigUInt_Set1(void *thisptr, uint64_t index, uint8_t value);
SEAL_C_FUNC BigUInt_GetSignificantBitCount(void *thisptr, int *significant_bit_count);
SEAL_C_FUNC BigUInt_Set2(void *thisptr, void *assign);
SEAL_C_FUNC BigUInt_Set3(void *thisptr, uint64_t value);
SEAL_C_FUNC BigUInt_Set4(void *thisptr, char *assign);
SEAL_C_FUNC BigUInt_SetZero(void *thispt);
SEAL_C_FUNC BigUInt_Resize(void *thisptr, int bitCount);
SEAL_C_FUNC BigUInt_Equals(void *thisptr, void *compare, bool *result);
SEAL_C_FUNC BigUInt_CompareTo1(void *thisptr, void *compare, int *result);
SEAL_C_FUNC BigUInt_CompareTo2(void *thisptr, uint64_t compare, int *result);
SEAL_C_FUNC BigUInt_DivideRemainder1(void *thisptr, void *operand2, void *remainder, void **result);
SEAL_C_FUNC BigUInt_DivideRemainder2(void *thisptr, uint64_t operand2, void *remainder, void **result);
SEAL_C_FUNC BigUInt_ToString(void *thisptr, char *outstr, uint64_t *length);
SEAL_C_FUNC BigUInt_ToDecimalString(void *thisptr, char *outstr, uint64_t *length);
SEAL_C_FUNC BigUInt_DuplicateTo(void *thisptr, void *destination);
SEAL_C_FUNC BigUInt_DuplicateFrom(void *thisptr, void *value);
SEAL_C_FUNC BigUInt_ModuloInvert1(void *thisptr, void *modulus, void **result);
SEAL_C_FUNC BigUInt_ModuloInvert2(void *thisptr, uint64_t modulus, void **result);
SEAL_C_FUNC BigUInt_TryModuloInvert1(void *thisptr, void *modulus, void *inverse, bool *result);
SEAL_C_FUNC BigUInt_TryModuloInvert2(void *thisptr, uint64_t modulus, void *inverse, bool *result);
SEAL_C_FUNC BigUInt_OperatorNeg(void *thisptr, void **result);
SEAL_C_FUNC BigUInt_OperatorTilde(void *thisptr, void **result);
SEAL_C_FUNC BigUInt_OperatorPlus1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorPlus2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorMinus1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorMinus2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorMult1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorMult2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorDiv1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorDiv2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorXor1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorXor2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorAnd1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorAnd2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorOr1(void *thisptr, void *operand, void **result);
SEAL_C_FUNC BigUInt_OperatorOr2(void *thisptr, uint64_t operand, void **result);
SEAL_C_FUNC BigUInt_OperatorShiftLeft(void *thisptr, int shift, void **result);
SEAL_C_FUNC BigUInt_OperatorShiftRight(void *thisptr, int shift, void **result);
SEAL_C_FUNC BigUInt_ToDouble(void *thisptr, double *result);
SEAL_C_FUNC BigUInt_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result);
SEAL_C_FUNC BigUInt_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes);
SEAL_C_FUNC BigUInt_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/ciphertext.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/ciphertext.h"
using namespace std;
using namespace seal;
using namespace seal::c;
namespace seal
{
/**
Enables access to private members of seal::Ciphertext.
*/
struct Ciphertext::CiphertextPrivateHelper
{
static void resize(Ciphertext *ciphertext, size_t size, size_t poly_modulus_degree, size_t coeff_modulus_size)
{
ciphertext->resize_internal(size, poly_modulus_degree, coeff_modulus_size);
}
static void set_ntt_form(Ciphertext *ciphertext, bool is_ntt_form)
{
ciphertext->is_ntt_form_ = is_ntt_form;
}
};
} // namespace seal
SEAL_C_FUNC Ciphertext_Create1(void *memoryPoolHandle, void **ciphertext)
{
IfNullRet(ciphertext, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(memoryPoolHandle);
try
{
Ciphertext *cipher = new Ciphertext(*handle);
*ciphertext = cipher;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Create2(void *copy, void **ciphertext)
{
Ciphertext *copyptr = FromVoid<Ciphertext>(copy);
IfNullRet(copyptr, E_POINTER);
IfNullRet(ciphertext, E_POINTER);
Ciphertext *cipher = new Ciphertext(*copyptr);
*ciphertext = cipher;
return S_OK;
}
SEAL_C_FUNC Ciphertext_Create3(void *context, void *pool, void **ciphertext)
{
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(ciphertext, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
Ciphertext *cipher = new Ciphertext(sharedctx, *pool_ptr);
*ciphertext = cipher;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Create4(void *context, uint64_t *parms_id, void *pool, void **ciphertext)
{
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(parms_id, E_POINTER);
IfNullRet(ciphertext, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
parms_id_type parmsid;
CopyParmsId(parms_id, parmsid);
Ciphertext *cipher = new Ciphertext(sharedctx, parmsid, *pool_ptr);
*ciphertext = cipher;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Create5(void *context, uint64_t *parms_id, uint64_t capacity, void *pool, void **ciphertext)
{
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(parms_id, E_POINTER);
IfNullRet(ciphertext, E_POINTER);
unique_ptr<MemoryPoolHandle> pool_ptr = MemHandleFromVoid(pool);
try
{
parms_id_type parmsid;
CopyParmsId(parms_id, parmsid);
Ciphertext *cipher = new Ciphertext(sharedctx, parmsid, capacity, *pool_ptr);
*ciphertext = cipher;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Reserve1(void *thisptr, void *context, uint64_t *parms_id, uint64_t size_capacity)
{
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(parms_id, E_POINTER);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
cipher->reserve(sharedctx, parms, size_capacity);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Reserve2(void *thisptr, void *context, uint64_t size_capacity)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
try
{
cipher->reserve(sharedctx, size_capacity);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Reserve3(void *thisptr, uint64_t size_capacity)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
try
{
cipher->reserve(size_capacity);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Set(void *thisptr, void *assign)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
Ciphertext *assignptr = FromVoid<Ciphertext>(assign);
IfNullRet(assignptr, E_POINTER);
*cipher = *assignptr;
return S_OK;
}
SEAL_C_FUNC Ciphertext_Destroy(void *thisptr)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
delete cipher;
return S_OK;
}
SEAL_C_FUNC Ciphertext_Size(void *thisptr, uint64_t *size)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(size, E_POINTER);
*size = cipher->size();
return S_OK;
}
SEAL_C_FUNC Ciphertext_SizeCapacity(void *thisptr, uint64_t *size_capacity)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(size_capacity, E_POINTER);
*size_capacity = cipher->size_capacity();
return S_OK;
}
SEAL_C_FUNC Ciphertext_PolyModulusDegree(void *thisptr, uint64_t *poly_modulus_degree)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(poly_modulus_degree, E_POINTER);
*poly_modulus_degree = cipher->poly_modulus_degree();
return S_OK;
}
SEAL_C_FUNC Ciphertext_CoeffModulusSize(void *thisptr, uint64_t *coeff_modulus_size)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(coeff_modulus_size, E_POINTER);
*coeff_modulus_size = cipher->coeff_modulus_size();
return S_OK;
}
SEAL_C_FUNC Ciphertext_ParmsId(void *thisptr, uint64_t *parms_id)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(parms_id, E_POINTER);
CopyParmsId(cipher->parms_id(), parms_id);
return S_OK;
}
SEAL_C_FUNC Ciphertext_SetParmsId(void *thisptr, uint64_t *parms_id)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(parms_id, E_POINTER);
CopyParmsId(parms_id, cipher->parms_id());
return S_OK;
}
SEAL_C_FUNC Ciphertext_Resize1(void *thisptr, void *context, uint64_t *parms_id, uint64_t size)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
cipher->resize(sharedctx, parms, size);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Resize2(void *thisptr, void *context, uint64_t size)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
try
{
cipher->resize(sharedctx, size);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Resize3(void *thisptr, uint64_t size)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
try
{
cipher->resize(size);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_Resize4(void *thisptr, uint64_t size, uint64_t polyModulusDegree, uint64_t coeffModCount)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
try
{
Ciphertext::CiphertextPrivateHelper::resize(cipher, size, polyModulusDegree, coeffModCount);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Ciphertext_GetDataAt1(void *thisptr, uint64_t index, uint64_t *data)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(data, E_POINTER);
try
{
*data = (*cipher)[index];
return S_OK;
}
catch (const out_of_range &)
{
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
}
}
SEAL_C_FUNC Ciphertext_GetDataAt2(void *thisptr, uint64_t poly_index, uint64_t coeff_index, uint64_t *data)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(data, E_POINTER);
auto poly_uint64_count = util::mul_safe(cipher->poly_modulus_degree(), cipher->coeff_modulus_size());
// poly_index is verified by the data method, we need to verify coeff_index ourselves.
if (coeff_index >= poly_uint64_count)
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
try
{
*data = cipher->data(poly_index)[coeff_index];
return S_OK;
}
catch (const out_of_range &)
{
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
}
}
SEAL_C_FUNC Ciphertext_SetDataAt(void *thisptr, uint64_t index, uint64_t value)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
try
{
(*cipher)[index] = value;
return S_OK;
}
catch (const out_of_range &)
{
return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
}
}
SEAL_C_FUNC Ciphertext_IsNTTForm(void *thisptr, bool *is_ntt_form)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(is_ntt_form, E_POINTER);
*is_ntt_form = cipher->is_ntt_form();
return S_OK;
}
SEAL_C_FUNC Ciphertext_SetIsNTTForm(void *thisptr, bool is_ntt_form)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
Ciphertext::CiphertextPrivateHelper::set_ntt_form(cipher, is_ntt_form);
return S_OK;
}
SEAL_C_FUNC Ciphertext_Scale(void *thisptr, double *scale)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(scale, E_POINTER);
*scale = cipher->scale();
return S_OK;
}
SEAL_C_FUNC Ciphertext_SetScale(void *thisptr, double scale)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
cipher->scale() = scale;
return S_OK;
}
SEAL_C_FUNC Ciphertext_Release(void *thisptr)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
cipher->release();
return S_OK;
}
SEAL_C_FUNC Ciphertext_IsTransparent(void *thisptr, bool *result)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(result, E_POINTER);
*result = cipher->is_transparent();
return S_OK;
}
SEAL_C_FUNC Ciphertext_Pool(void *thisptr, void **pool)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(pool, E_POINTER);
MemoryPoolHandle *handleptr = new MemoryPoolHandle(cipher->pool());
*pool = handleptr;
return S_OK;
}
SEAL_C_FUNC Ciphertext_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = static_cast<int64_t>(cipher->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 Ciphertext_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, E_POINTER);
IfNullRet(outptr, E_POINTER);
IfNullRet(out_bytes, E_POINTER);
try
{
*out_bytes = util::safe_cast<int64_t>(cipher->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 Ciphertext_UnsafeLoad(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, 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>(
cipher->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 Ciphertext_Load(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
Ciphertext *cipher = FromVoid<Ciphertext>(thisptr);
IfNullRet(cipher, 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>(
cipher->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 Ciphertext_Create1(void *pool, void **cipher);
SEAL_C_FUNC Ciphertext_Create2(void *copy, void **cipher);
SEAL_C_FUNC Ciphertext_Create3(void *context, void *pool, void **cipher);
SEAL_C_FUNC Ciphertext_Create4(void *context, uint64_t *parms_id, void *pool, void **cipher);
SEAL_C_FUNC Ciphertext_Create5(void *context, uint64_t *parms_id, uint64_t capacity, void *pool, void **ciphertext);
SEAL_C_FUNC Ciphertext_Reserve1(void *thisptr, void *context, uint64_t *parms_id, uint64_t size_capacity);
SEAL_C_FUNC Ciphertext_Reserve2(void *thisptr, void *context, uint64_t size_capacity);
SEAL_C_FUNC Ciphertext_Reserve3(void *thisptr, uint64_t size_capacity);
SEAL_C_FUNC Ciphertext_Set(void *thisptr, void *assign);
SEAL_C_FUNC Ciphertext_Destroy(void *thisptr);
SEAL_C_FUNC Ciphertext_Size(void *thisptr, uint64_t *size);
SEAL_C_FUNC Ciphertext_SizeCapacity(void *thisptr, uint64_t *size_capacity);
SEAL_C_FUNC Ciphertext_PolyModulusDegree(void *thisptr, uint64_t *poly_modulus_degree);
SEAL_C_FUNC Ciphertext_CoeffModulusSize(void *thisptr, uint64_t *coeff_modulus_size);
SEAL_C_FUNC Ciphertext_ParmsId(void *thisptr, uint64_t *parms_id);
SEAL_C_FUNC Ciphertext_SetParmsId(void *thisptr, uint64_t *parms_id);
SEAL_C_FUNC Ciphertext_Resize1(void *thisptr, void *context, uint64_t *parms_id, uint64_t size);
SEAL_C_FUNC Ciphertext_Resize2(void *thisptr, void *context, uint64_t size);
SEAL_C_FUNC Ciphertext_Resize3(void *thisptr, uint64_t size);
SEAL_C_FUNC Ciphertext_Resize4(void *thisptr, uint64_t size, uint64_t polyModulusDegree, uint64_t coeffModCount);
SEAL_C_FUNC Ciphertext_GetDataAt1(void *thisptr, uint64_t index, uint64_t *data);
SEAL_C_FUNC Ciphertext_GetDataAt2(void *thisptr, uint64_t poly_index, uint64_t coeff_index, uint64_t *data);
SEAL_C_FUNC Ciphertext_SetDataAt(void *thisptr, uint64_t index, uint64_t value);
SEAL_C_FUNC Ciphertext_IsNTTForm(void *thisptr, bool *is_ntt_form);
SEAL_C_FUNC Ciphertext_SetIsNTTForm(void *thisptr, bool is_ntt_form);
SEAL_C_FUNC Ciphertext_Scale(void *thisptr, double *scale);
SEAL_C_FUNC Ciphertext_SetScale(void *thisptr, double scale);
SEAL_C_FUNC Ciphertext_Release(void *thisptr);
SEAL_C_FUNC Ciphertext_IsTransparent(void *thisptr, bool *result);
SEAL_C_FUNC Ciphertext_Pool(void *thisptr, void **pool);
SEAL_C_FUNC Ciphertext_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result);
SEAL_C_FUNC Ciphertext_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes);
SEAL_C_FUNC Ciphertext_UnsafeLoad(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes);
SEAL_C_FUNC Ciphertext_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.
// STD
#include <vector>
// SEALNet
#include "seal/c/ckksencoder.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/ckks.h"
using namespace std;
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC CKKSEncoder_Create(void *context, void **ckks_encoder)
{
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(ckks_encoder, E_POINTER);
try
{
CKKSEncoder *encoder = new CKKSEncoder(sharedctx);
*ckks_encoder = encoder;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC CKKSEncoder_Destroy(void *thisptr)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
delete encoder;
return S_OK;
}
// Array of doubles
SEAL_C_FUNC CKKSEncoder_Encode1(
void *thisptr, uint64_t value_count, double *values, uint64_t *parms_id, double scale, void *destination,
void *pool)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
Plaintext *destinationptr = FromVoid<Plaintext>(destination);
IfNullRet(destinationptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
vector<double> input(value_count);
for (uint64_t i = 0; i < value_count; i++)
{
input[i] = values[i];
}
try
{
encoder->encode(input, parms, scale, *destinationptr, *handle);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
// Array of complex numbers (two doubles per value)
SEAL_C_FUNC CKKSEncoder_Encode2(
void *thisptr, uint64_t value_count, double *complex_values, uint64_t *parms_id, double scale, void *destination,
void *pool)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
Plaintext *destinationptr = FromVoid<Plaintext>(destination);
IfNullRet(destinationptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
vector<complex<double>> input(value_count);
for (uint64_t i = 0; i < value_count; i++)
{
input[i] = complex<double>(complex_values[i * 2], complex_values[i * 2 + 1]);
}
try
{
encoder->encode(input, parms, scale, *destinationptr, *handle);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
// Single double value
SEAL_C_FUNC CKKSEncoder_Encode3(
void *thisptr, double value, uint64_t *parms_id, double scale, void *destination, void *pool)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
Plaintext *destinationptr = FromVoid<Plaintext>(destination);
IfNullRet(destinationptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
encoder->encode(value, parms, scale, *destinationptr, *handle);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
// Single complex value
SEAL_C_FUNC CKKSEncoder_Encode4(
void *thisptr, double value_re, double value_im, uint64_t *parms_id, double scale, void *destination, void *pool)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
Plaintext *destinationptr = FromVoid<Plaintext>(destination);
IfNullRet(destinationptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(pool);
parms_id_type parms;
CopyParmsId(parms_id, parms);
complex<double> input(value_re, value_im);
try
{
encoder->encode(input, parms, scale, *destinationptr, *handle);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
// Single Int64 value
SEAL_C_FUNC CKKSEncoder_Encode5(void *thisptr, int64_t value, uint64_t *parms_id, void *destination)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
Plaintext *destinationptr = FromVoid<Plaintext>(destination);
IfNullRet(destinationptr, E_POINTER);
IfNullRet(parms_id, E_POINTER);
parms_id_type parms;
CopyParmsId(parms_id, parms);
try
{
encoder->encode(value, parms, *destinationptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
// Array of doubles
SEAL_C_FUNC CKKSEncoder_Decode1(void *thisptr, void *plain, uint64_t *value_count, double *values, void *pool)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
IfNullRet(value_count, E_POINTER);
IfNullRet(values, E_POINTER);
Plaintext *plainptr = FromVoid<Plaintext>(plain);
IfNullRet(plainptr, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(pool);
vector<double> destination;
try
{
encoder->decode(*plainptr, destination, *handle);
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
*value_count = destination.size();
// Copy to actual destination
for (uint64_t i = 0; i < destination.size(); i++)
{
values[i] = destination[i];
}
return S_OK;
}
// Array of complex numbers
SEAL_C_FUNC CKKSEncoder_Decode2(void *thisptr, void *plain, uint64_t *value_count, double *values, void *pool)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
IfNullRet(value_count, E_POINTER);
IfNullRet(values, E_POINTER);
Plaintext *plainptr = FromVoid<Plaintext>(plain);
IfNullRet(plainptr, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(pool);
vector<complex<double>> destination;
try
{
encoder->decode(*plainptr, destination, *handle);
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
*value_count = destination.size();
// Copy to actual destination
for (uint64_t i = 0; i < destination.size(); i++)
{
values[i * 2] = destination[i].real();
values[i * 2 + 1] = destination[i].imag();
}
return S_OK;
}
SEAL_C_FUNC CKKSEncoder_SlotCount(void *thisptr, uint64_t *slot_count)
{
CKKSEncoder *encoder = FromVoid<CKKSEncoder>(thisptr);
IfNullRet(encoder, E_POINTER);
IfNullRet(slot_count, E_POINTER);
*slot_count = encoder->slot_count();
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 CKKSEncoder_Create(void *context, void **ckks_encoder);
SEAL_C_FUNC CKKSEncoder_Destroy(void *thisptr);
// Array of doubles
SEAL_C_FUNC CKKSEncoder_Encode1(
void *thisptr, uint64_t value_count, double *values, uint64_t *parms_id, double scale, void *destination,
void *pool);
// Array of complex numbers (two doubles per value)
SEAL_C_FUNC CKKSEncoder_Encode2(
void *thisptr, uint64_t value_count, double *complex_values, uint64_t *parms_id, double scale, void *destination,
void *pool);
// Single double value
SEAL_C_FUNC CKKSEncoder_Encode3(
void *thisptr, double value, uint64_t *parms_id, double scale, void *destination, void *pool);
// Single complex value
SEAL_C_FUNC CKKSEncoder_Encode4(
void *thisptr, double value_re, double value_im, uint64_t *parms_id, double scale, void *destination, void *pool);
// Single Int64 value
SEAL_C_FUNC CKKSEncoder_Encode5(void *thisptr, int64_t value, uint64_t *parms_id, void *destination);
// Array of doubles
SEAL_C_FUNC CKKSEncoder_Decode1(void *thisptr, void *plain, uint64_t *value_count, double *values, void *pool);
// Array of complex numbers
SEAL_C_FUNC CKKSEncoder_Decode2(void *thisptr, void *plain, uint64_t *value_count, double *values, void *pool);
SEAL_C_FUNC CKKSEncoder_SlotCount(void *thisptr, uint64_t *slot_count);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/contextdata.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 ContextData_Destroy(void *thisptr)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
delete cont_data;
return S_OK;
}
SEAL_C_FUNC ContextData_TotalCoeffModulus(void *thisptr, uint64_t *count, uint64_t *total_coeff_modulus)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(count, E_POINTER);
*count = cont_data->parms().coeff_modulus().size();
if (nullptr == total_coeff_modulus)
{
// We only wanted the count.
return S_OK;
}
for (uint64_t i = 0; i < *count; i++)
{
total_coeff_modulus[i] = cont_data->total_coeff_modulus()[i];
}
return S_OK;
}
SEAL_C_FUNC ContextData_TotalCoeffModulusBitCount(void *thisptr, int *bit_count)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(bit_count, E_POINTER);
*bit_count = cont_data->total_coeff_modulus_bit_count();
return S_OK;
}
SEAL_C_FUNC ContextData_Parms(void *thisptr, void **parms)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
EncryptionParameters *enc_params = new EncryptionParameters(cont_data->parms());
*parms = enc_params;
return S_OK;
}
SEAL_C_FUNC ContextData_Qualifiers(void *thisptr, void **epq)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(epq, E_POINTER);
EncryptionParameterQualifiers *qualifiers = new EncryptionParameterQualifiers(cont_data->qualifiers());
*epq = qualifiers;
return S_OK;
}
SEAL_C_FUNC ContextData_CoeffDivPlainModulus(void *thisptr, uint64_t *count, uint64_t *coeff_div)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(count, E_POINTER);
*count = cont_data->parms().coeff_modulus().size();
if (nullptr == coeff_div)
{
// We only wanted the size
return S_OK;
}
for (uint64_t i = 0; i < *count; i++)
{
coeff_div[i] = cont_data->coeff_div_plain_modulus()[i];
}
return S_OK;
}
SEAL_C_FUNC ContextData_PlainUpperHalfThreshold(void *thisptr, uint64_t *puht)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(puht, E_POINTER);
*puht = cont_data->plain_upper_half_threshold();
return S_OK;
}
SEAL_C_FUNC ContextData_PlainUpperHalfIncrement(void *thisptr, uint64_t *count, uint64_t *puhi)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(count, E_POINTER);
*count = cont_data->parms().coeff_modulus().size();
if (nullptr == puhi)
{
// We only wanted the size
return S_OK;
}
for (uint64_t i = 0; i < *count; i++)
{
puhi[i] = cont_data->plain_upper_half_increment()[i];
}
return S_OK;
}
SEAL_C_FUNC ContextData_UpperHalfThreshold(void *thisptr, uint64_t *count, uint64_t *uht)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(count, E_POINTER);
// This is only available for CKKS
if (cont_data->upper_half_threshold() == nullptr)
{
*count = 0;
return S_OK;
}
*count = cont_data->parms().coeff_modulus().size();
if (nullptr == uht)
{
// We only wanted the count
return S_OK;
}
for (uint64_t i = 0; i < *count; i++)
{
uht[i] = cont_data->upper_half_threshold()[i];
}
return S_OK;
}
SEAL_C_FUNC ContextData_UpperHalfIncrement(void *thisptr, uint64_t *count, uint64_t *uhi)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(count, E_POINTER);
if (cont_data->upper_half_increment() == nullptr)
{
*count = 0;
return S_OK;
}
*count = cont_data->parms().coeff_modulus().size();
if (nullptr == uhi)
{
// We only wanted the size
return S_OK;
}
for (uint64_t i = 0; i < *count; i++)
{
uhi[i] = cont_data->upper_half_increment()[i];
}
return S_OK;
}
SEAL_C_FUNC ContextData_PrevContextData(void *thisptr, void **prev_data)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(prev_data, E_POINTER);
// The caller should not try to delete the returned pointer
*prev_data = const_cast<SEALContext::ContextData *>(cont_data->prev_context_data().get());
return S_OK;
}
SEAL_C_FUNC ContextData_NextContextData(void *thisptr, void **next_data)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(next_data, E_POINTER);
// The caller should not try to delete the returned pointer
*next_data = const_cast<SEALContext::ContextData *>(cont_data->next_context_data().get());
return S_OK;
}
SEAL_C_FUNC ContextData_ChainIndex(void *thisptr, uint64_t *index)
{
SEALContext::ContextData *cont_data = FromVoid<SEALContext::ContextData>(thisptr);
IfNullRet(cont_data, E_POINTER);
IfNullRet(index, E_POINTER);
*index = cont_data->chain_index();
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 ContextData_Destroy(void *thisptr);
SEAL_C_FUNC ContextData_TotalCoeffModulus(void *thisptr, uint64_t *count, uint64_t *total_coeff_modulus);
SEAL_C_FUNC ContextData_TotalCoeffModulusBitCount(void *thisptr, int *bit_count);
SEAL_C_FUNC ContextData_Parms(void *thisptr, void **parms);
SEAL_C_FUNC ContextData_Qualifiers(void *thisptr, void **epq);
SEAL_C_FUNC ContextData_CoeffDivPlainModulus(void *thisptr, uint64_t *count, uint64_t *coeff_div);
SEAL_C_FUNC ContextData_PlainUpperHalfThreshold(void *thisptr, uint64_t *puht);
SEAL_C_FUNC ContextData_PlainUpperHalfIncrement(void *thisptr, uint64_t *count, uint64_t *puhi);
SEAL_C_FUNC ContextData_UpperHalfThreshold(void *thisptr, uint64_t *count, uint64_t *uht);
SEAL_C_FUNC ContextData_UpperHalfIncrement(void *thisptr, uint64_t *count, uint64_t *uhi);
SEAL_C_FUNC ContextData_PrevContextData(void *thisptr, void **prev_data);
SEAL_C_FUNC ContextData_NextContextData(void *thisptr, void **next_data);
SEAL_C_FUNC ContextData_ChainIndex(void *thisptr, uint64_t *index);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/decryptor.h"
#include "seal/c/stdafx.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/decryptor.h"
using namespace std;
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC Decryptor_Create(void *context, void *secret_key, void **decryptor)
{
SecretKey *secretKey = FromVoid<SecretKey>(secret_key);
IfNullRet(secretKey, E_POINTER);
const auto &sharedctx = SharedContextFromVoid(context);
IfNullRet(sharedctx.get(), E_POINTER);
IfNullRet(decryptor, E_POINTER);
try
{
Decryptor *decr = new Decryptor(sharedctx, *secretKey);
*decryptor = decr;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Decryptor_Destroy(void *thisptr)
{
Decryptor *decryptor = FromVoid<Decryptor>(thisptr);
IfNullRet(decryptor, E_POINTER);
delete decryptor;
return S_OK;
}
SEAL_C_FUNC Decryptor_Decrypt(void *thisptr, void *encrypted, void *destination)
{
Decryptor *decryptor = FromVoid<Decryptor>(thisptr);
IfNullRet(decryptor, E_POINTER);
Ciphertext *encryptedptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encryptedptr, E_POINTER);
Plaintext *destinationptr = FromVoid<Plaintext>(destination);
IfNullRet(destinationptr, E_POINTER);
try
{
decryptor->decrypt(*encryptedptr, *destinationptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Decryptor_InvariantNoiseBudget(void *thisptr, void *encrypted, int *invariant_noise_budget)
{
Decryptor *decryptor = FromVoid<Decryptor>(thisptr);
IfNullRet(decryptor, E_POINTER);
Ciphertext *encryptedptr = FromVoid<Ciphertext>(encrypted);
IfNullRet(encryptedptr, E_POINTER);
IfNullRet(invariant_noise_budget, E_POINTER);
try
{
*invariant_noise_budget = decryptor->invariant_noise_budget(*encryptedptr);
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
// 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 Decryptor_Create(void *context, void *secret_key, void **decryptor);
SEAL_C_FUNC Decryptor_Destroy(void *thisptr);
SEAL_C_FUNC Decryptor_Decrypt(void *thisptr, void *encrypted, void *destination);
SEAL_C_FUNC Decryptor_InvariantNoiseBudget(void *thisptr, void *encrypted, int *invariant_noise_budget);
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
// STD
#include <cstddef>
// Check that std::size_t is 64 bits
static_assert(sizeof(std::size_t) == 8, "Require sizeof(std::size_t) == 8");
#ifdef _MSC_VER
// Check that architecture (platform) is x64
#ifndef _WIN64
static_assert(false, "Require architecture == x64");
#endif
#ifdef SEAL_C_EXPORTS
#define SEAL_C_DECOR extern "C" __declspec(dllexport)
#else
#define SEAL_C_DECOR extern "C" __declspec(dllimport)
#endif
#define SEAL_C_CALL __cdecl
#else // _MSC_VER
#define SEAL_C_DECOR extern "C"
#define SEAL_C_CALL
#define HRESULT long
#define _HRESULT_TYPEDEF_(hr) ((HRESULT)hr)
#define E_POINTER _HRESULT_TYPEDEF_(0x80004003L)
#define E_INVALIDARG _HRESULT_TYPEDEF_(0x80070057L)
#define E_OUTOFMEMORY _HRESULT_TYPEDEF_(0x8007000EL)
#define E_UNEXPECTED _HRESULT_TYPEDEF_(0x8000FFFFL)
#define COR_E_IO _HRESULT_TYPEDEF_(0x80131620L)
#define COR_E_INVALIDOPERATION _HRESULT_TYPEDEF_(0x80131509L)
#define S_OK _HRESULT_TYPEDEF_(0L)
#define S_FALSE _HRESULT_TYPEDEF_(1L)
#define FACILITY_WIN32 7
#define HRESULT_FROM_WIN32(x) \
((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT)(((x)&0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000)))
#define ERROR_INSUFFICIENT_BUFFER 122L
#define ERROR_INVALID_INDEX 1413L
#define ERROR_INVALID_OPERATION 4317L
#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
#define FAILED(hr) (((HRESULT)(hr)) < 0)
#endif // _MSC_VER
#define SEAL_C_FUNC SEAL_C_DECOR HRESULT SEAL_C_CALL
\ No newline at end of file
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// dllmain.cpp : Defines the entry point for the DLL application.
#include "seal/c/stdafx.h"
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
\ No newline at end of file
// 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);
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