Skip to content

Commit

Permalink
Extensions to key generation utility
Browse files Browse the repository at this point in the history
New APIs to save/load separated SK, PK, and Switching Key matrices.

Co-authored-by: TabOg <tabitha.ogilvie.2019@live.rhul.ac.uk>
Co-authored-by: Hamish Hunt <hamish.hunt@intel.com>
Co-authored-by: Flavio Bergamaschi <anadyomeni@gmx.com>
  • Loading branch information
4 people authored Dec 22, 2022
1 parent d7be6f0 commit 56264fa
Show file tree
Hide file tree
Showing 25 changed files with 2,604 additions and 87 deletions.
42 changes: 34 additions & 8 deletions include/helib/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
* limitations under the License. See accompanying LICENSE file.
*/

/* Copyright (C) 2022 Intel Corporation
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef HELIB_KEYS_H
#define HELIB_KEYS_H
/**
Expand Down Expand Up @@ -423,61 +427,83 @@ class SecKey : public PubKey
/**
* @brief Write out the `SecKey` object in binary format.
* @param str Output `std::ostream`.
* @param sk_only Whether to write only the secret key polynomial and context,
*and not the public key.
**/
void writeTo(std::ostream& str) const;
void writeTo(std::ostream& str, bool sk_only = false) const;

/**
* @brief Read from the stream the serialized `SecKey` object in binary
* format.
* @param str Input `std::istream`.
* @param sk_only Whether the stream contains only the secret key polynomial
*and context, and not the public key.
* @return The deserialized `SecKey` object.
**/
static SecKey readFrom(std::istream& str, const Context& context);
static SecKey readFrom(std::istream& str,
const Context& context,
bool sk_only = false);

/**
* @brief Write out the secret key (`SecKey`) object to the output
* stream using JSON format.
* @param str Output `std::ostream`.
* @param sk_only whether to write only secret key polynomial and context, and
*not the public key.
**/
void writeToJSON(std::ostream& str) const;
void writeToJSON(std::ostream& str, bool sk_only = false) const;

/**
* @brief Write out the secret key (`SecKey`) object to a `JsonWrapper`.
* @return The `JsonWrapper`.
* @param sk_only whether to write only secret key polynomial and context, and
*not the public key.
**/
JsonWrapper writeToJSON() const;
JsonWrapper writeToJSON(bool sk_only = false) const;

/**
* @brief Read from the stream the serialized secret key (`SecKey`) object
* using JSON format.
* @param str Input `std::istream`.
* @param context The `Context` to be used.
* @param sk_only whether the stream contains only the secret key polynomial
*and context, and not the public key.
* @return The deserialized `SecKey` object.
**/
static SecKey readFromJSON(std::istream& str, const Context& context);
static SecKey readFromJSON(std::istream& str,
const Context& context,
bool sk_only = false);

/**
* @brief Read from the `JsonWrapper` the serialized secret key (`SecKey`)
* object.
* @param j The `JsonWrapper` containing the serialized `SecKey` object.
* @param context The `Context` to be used.
* @param sk_only whether the stream contains only the secret key polynomial
*and context, and not the public key.
* @return The deserialized `SecKey` object.
**/
static SecKey readFromJSON(const JsonWrapper& j, const Context& context);
static SecKey readFromJSON(const JsonWrapper& j,
const Context& context,
bool sk_only = false);

/**
* @brief Read from the stream the serialized secret key (`SecKey`) object
* using JSON format.
* @param str Input `std::istream`.
* @param sk_only whether the stream contains only the secret key polynomial
*and context, and not the public key.
**/
void readJSON(std::istream& str);
void readJSON(std::istream& str, bool sk_only = false);

/**
* @brief Read from the `JsonWrapper` the serialized secret key (`SecKey`)
* object.
* @param j The `JsonWrapper` containing the serialized `SecKey` object.
* @param sk_only whether the stream contains only the secret key polynomial
*and context, and not the public key.
**/
void readJSON(const JsonWrapper& j);
void readJSON(const JsonWrapper& j, bool sk_only = false);

// TODO: Add a similar method for binary serialization
// This just writes the derived part, not including the public key
Expand Down
60 changes: 60 additions & 0 deletions misc/psi/tests/gen-params.batx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
# See the License for the specific language governing permissions and
# limitations under the License. See accompanying LICENSE file.

# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

utils_dir="../../../../utils"
load "../../../utils/tests/std"

nslots=3
#nslots=13860
#nslots=79872
datadir="data_and_params"
keygen_datadir="keygen_data_and_params"
sk_keygen_datadir="sk_keygen_data_and_params"
data_prefix="../$datadir"
db_encoded="../db${nslots}.enc"
query_encoded="../query${nslots}.enc"
keygen_prefix="${prefix_bgv}_keygen"
sk_keygen_prefix="${prefix_bgv}_keygen"

function create-toy-lookup-params {
rm -f "${prefix_bgv}".params
Expand Down Expand Up @@ -73,6 +80,17 @@ function create-serious-lookup-params {
echo "Qbits=1800" >> "${prefix_bgv}".params
}

function create-test-params {
rm -f "${prefix_bgv}".params
touch "${prefix_bgv}".params
echo "# Generated by bash test" >> "${prefix_bgv}".params
echo "p=65537" >> "${prefix_bgv}".params
echo "m=131072" >> "${prefix_bgv}".params
echo "r=1" >> "${prefix_bgv}".params
echo "c=3" >> "${prefix_bgv}".params
echo "Qbits=1130" >> "${prefix_bgv}".params
}

@test "only generate the context" {
create-toy-lookup-params
# create-serious-lookup-params
Expand All @@ -85,6 +103,7 @@ function create-serious-lookup-params {
rm -rf ../$datadir
create-toy-lookup-params
# create-serious-lookup-params
# create-test-params
createContext BGV "${prefix_bgv}.params" "$prefix_bgv" "--frob-skm"
# Encrypt the database and query
$encrypt ${prefix_bgv}.pk ${db_encoded} -o "db.ctxt"
Expand All @@ -93,3 +112,44 @@ function create-serious-lookup-params {
mv $tmp_folder/ $datadir/
}

@test "keygen only generate the context" {
create-toy-lookup-params
# create-serious-lookup-params
keyGeneration BGV "${prefix_bgv}.params" "${prefix_bgv}" "--frob-skm"
techo "$(cat ${prefix_bgv}.info)"
}

@test "keygen generate params and encrypt query and database" {
# Delete the existing data and params directory.
rm -rf ../$keygen_datadir
create-toy-lookup-params
# create-serious-lookup-params
# create-test-params
keyGeneration BGV "${prefix_bgv}.params" "${prefix_bgv}" "--frob-skm"
# Encrypt the database and query
$encrypt ${prefix_bgv}Enc.pk ${db_encoded} -o "db.ctxt"
$encrypt ${prefix_bgv}Enc.pk ${query_encoded} -o "query.ctxt"
cd -
mv $tmp_folder/ $keygen_datadir/
}

@test "keygen, sk only only generate the context" {
create-toy-lookup-params
# create-serious-lookup-params
keyGeneration BGV "${prefix_bgv}.params" "${prefix_bgv}" "--frob-skm" -s
techo "$(cat ${prefix_bgv}.info)"
}

@test "keygen, sk only generate params and encrypt query and database" {
# Delete the existing data and params directory.
rm -rf ../$sk_keygen_datadir
create-toy-lookup-params
# create-serious-lookup-params
# create-test-params
keyGeneration BGV "${prefix_bgv}.params" "${prefix_bgv}" "--frob-skm" -s
# Encrypt the database and query
$encrypt ${prefix_bgv}Enc.pk ${db_encoded} -o "db.ctxt"
$encrypt ${prefix_bgv}Enc.pk ${query_encoded} -o "query.ctxt"
cd -
mv $tmp_folder/ $sk_keygen_datadir/
}
Loading

0 comments on commit 56264fa

Please sign in to comment.