Skip to content

Commit

Permalink
Add secret key -> polynomial array conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
samtay committed Feb 9, 2024
1 parent 03b8561 commit 2118cfa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
27 changes: 26 additions & 1 deletion native/src/seal/c/polyarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,32 @@ SEAL_C_FUNC PolynomialArray_CreateFromPublicKey(void *memoryPoolHandle, void *co
}
}

SEAL_C_FUNC PolynomialArray_Copy(void *copy, void **poly_array) {
SEAL_C_FUNC PolynomialArray_CreateFromSecretKey(
void *memoryPoolHandle, void *context, void *secret_key, void **poly_array)
{
const SEALContext *ctx = FromVoid<SEALContext>(context);
IfNullRet(ctx, E_POINTER);

const SecretKey *sk = FromVoid<SecretKey>(secret_key);
IfNullRet(sk, E_POINTER);

IfNullRet(poly_array, E_POINTER);
unique_ptr<MemoryPoolHandle> handle = MemHandleFromVoid(memoryPoolHandle);

try
{
PolynomialArray *array = new PolynomialArray(*ctx, *sk, *handle);
*poly_array = array;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}

SEAL_C_FUNC PolynomialArray_Copy(void *copy, void **poly_array)
{
PolynomialArray *copyptr = FromVoid<PolynomialArray>(copy);
IfNullRet(copyptr, E_POINTER);
IfNullRet(poly_array, E_POINTER);
Expand Down
2 changes: 2 additions & 0 deletions native/src/seal/c/polyarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ SEAL_C_FUNC PolynomialArray_CreateFromCiphertext(void *memoryPoolHandle, void *c

SEAL_C_FUNC PolynomialArray_CreateFromPublicKey(void *memoryPoolHandle, void *context, void *public_key, void **poly_array);

SEAL_C_FUNC PolynomialArray_CreateFromSecretKey(void *memoryPoolHandle, void *context, void *secret_key, void **poly_array);

SEAL_C_FUNC PolynomialArray_Copy(void *copy, void **poly_array);

SEAL_C_FUNC PolynomialArray_Destroy(void *thisptr);
Expand Down
32 changes: 31 additions & 1 deletion native/src/seal/polyarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,41 @@ namespace seal
}
}

PolynomialArray::PolynomialArray(
const SEALContext &context,
const SecretKey &secret_key,
MemoryPoolHandle pool
) : PolynomialArray(pool) {

auto &pt = secret_key.data();
auto &parms = context.first_context_data()->parms();
auto &plain_modulus = parms.plain_modulus();
auto &coeff_modulus = parms.coeff_modulus();
auto coeff_modulus_size = coeff_modulus.size();
auto poly_modulus_degree = pt.coeff_count();

auto is_ntt_form = pt.is_ntt_form();
size_t coeff_count = parms.poly_modulus_degree();
auto &context_data = *context.get_context_data(parms.parms_id());
auto ntt_tables = context_data.small_ntt_tables();

reserve(1, poly_modulus_degree, {plain_modulus});

const auto data_ptr = pt.data();
insert_polynomial(0, data_ptr);

// Convert out of NTT form for each polynomial.
if (is_ntt_form) {
for (size_t i = 0; i < coeff_modulus_size; i++) {
inverse_ntt_negacyclic_harvey(get_polynomial(0) + i * coeff_count, ntt_tables[i]);
}
}
}

PolynomialArray::PolynomialArray(const PolynomialArray &copy): PolynomialArray(copy.pool_) {
// These parameters in the result object are internally stored once
// reserve is called.
auto poly_size = copy.poly_size();
auto coeff_modulus_size = copy.coeff_modulus_size();
auto coeff_modulus = copy.coeff_modulus_;
auto poly_modulus_degree = copy.poly_modulus_degree();

Expand Down
8 changes: 8 additions & 0 deletions native/src/seal/polyarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "seal/ciphertext.h"
#include "seal/memorymanager.h"
#include "seal/publickey.h"
#include "seal/secretkey.h"
#include <stdexcept>

using namespace seal::util;
Expand Down Expand Up @@ -45,6 +46,13 @@ namespace seal
const SEALContext &context, const PublicKey &public_key, MemoryPoolHandle pool
);

/**
Create a PolynomialArray from a secret key.
*/
PolynomialArray(
const SEALContext &context, const SecretKey &secret_key, MemoryPoolHandle pool
);

/**
Creates a new ciphertext by copying a given one.
Expand Down

0 comments on commit 2118cfa

Please sign in to comment.