Skip to content

Commit 288592b

Browse files
hamishunJack L CrawfordEnrico Steffinlongo
authored andcommitted
Utils (homenc#351)
* create-context * encrypt and decrypt * example encode and decode scripts * Added new serialize and deserialize functions * Added utils tests that uses bats framework * bug fixes Co-Authored-By: Jack L Crawford <[email protected]> Co-Authored-By: Enrico Steffinlongo <[email protected]>
1 parent d47b983 commit 288592b

37 files changed

+3872
-137
lines changed

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
gh-pages/
22
build/
33

4+
__pycache__/
5+
6+
**.sk
7+
**.pk
8+
**.out
9+
**.ptxt
10+
**.ctxt
11+
412
src/fhe.a
513
src/*.o
614
src/*_x
@@ -12,8 +20,6 @@ src/*_x.dSYM
1220
src/*.sh
1321
src/*.bin
1422
src/*.txt
15-
src/misc/*.bin
16-
src/misc/*.txt
1723

1824
**.bak
1925
**.swp

OLD_INSTALL.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ library, currently these are:
117117
flag should be passed to gcc
118118

119119
-DHELIB_BOOT_THREADS tells HElib to use a multithreading strategy for
120-
bootstrapping; requires -DHELIB_THREADS (see above)
120+
bootstrapping; requires -DHELIB_THREADS (see above)
121121

122122

123123
-DDEBUG_PRINTOUT

include/helib/Context.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,13 @@ class Context
412412
return ans;
413413
}
414414

415+
//! @brief Size in bits of Q.
416+
long bitSizeOfQ() const
417+
{
418+
IndexSet primes = ctxtPrimes | specialPrimes;
419+
return std::ceil(logOfProduct(primes) / log(2.0));
420+
}
421+
415422
//! @brief An estimate for the security-level
416423
double securityLevel() const
417424
{
@@ -427,6 +434,9 @@ class Context
427434
return (7.2 * phim / bitsize - 110);
428435
}
429436

437+
//! @brief print out algebra and other important info
438+
void printout(std::ostream& out = std::cout) const;
439+
430440
//! @brief Just add the given prime to the chain
431441
void AddSmallPrime(long q);
432442
void AddCtxtPrime(long q);

include/helib/NumbTh.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,53 @@ class RandomState
608608
//! the char cc
609609
void seekPastChar(std::istream& str, int cc);
610610

611+
/**
612+
* @brief Advance the input stream `str` beyond white spaces and a single
613+
* `separator` in the region-of-interest delimited by `begin_char` and
614+
* `end_char`.
615+
* @param str The stream to be advanced.
616+
* @param begin_char The character determining the beginning of the
617+
* region-of-interest (to advance beyond of).
618+
* @param separator The separator character to advance beyond of.
619+
* @param end_char The character determining the end of the region-of-interest
620+
* (to advance beyond of).
621+
* @return `true` if the region-of-interest is not completed (i.e.: `end_char`
622+
* is not reached). `false` otherwise.
623+
* @note Throws `helib::RuntimeError` if after spaces there is a character
624+
* different from `begin_char`, `beyond`, or `end_char`.
625+
*/
626+
bool iterateInterestRegion(std::istream& str,
627+
int begin_char,
628+
int separator,
629+
int end_char);
630+
631+
/**
632+
* @brief Advance the input stream `istr` beyond white spaces. Then split the
633+
* region delimited by `begin_char` and `end_char` at each occurrence of
634+
* `separator` that is not contained in an inner `begin_char` - `end_char`
635+
* section. The function returns a `std::vector<std::stringstream>` with the
636+
* stream of every section of the input region.
637+
* @param istr The stream to be advanced.
638+
* @param begin_char The character determining the beginning of the
639+
* region-of-interest.
640+
* @param end_char The character determining the end of the
641+
* region-of-interest
642+
* @param separator The separator character to split at.
643+
* @param skip_space Boolean value determining whether to skip spaces when
644+
* extracting the sub-streams (default = `true`).
645+
* @return A `std::vector<std::stringstream>` with the stream of every section
646+
* of the input region.
647+
* @throws IOError If the stream is badly formatted (i.e. it does not start with
648+
* `begin_char` or it does not end with `end_char`).
649+
* @note Requires `begin_char`, `end_char` and `separator` to be distinct and
650+
* different from ` ` (space).
651+
*/
652+
std::vector<std::stringstream> extractTokenizeRegion(std::istream& istr,
653+
char begin_char,
654+
char end_char,
655+
char separator,
656+
bool skip_space = true);
657+
611658
//! @brief Reverse a vector in place
612659
template <typename T>
613660
void reverse(NTL::Vec<T>& v, long lo, long hi)

include/helib/PAlgebra.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ class PAlgebra
153153
/* I/O methods */
154154

155155
//! Prints the structure in a readable form
156-
void printout() const;
157-
void printAll() const; // print even more
156+
void printout(std::ostream& out = std::cout) const;
157+
void printAll(std::ostream& out = std::cout) const; // print even more
158158

159159
/* Access methods */
160160

include/helib/PolyMod.h

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ class PolyMod
188188
NTL::ZZX getG() const;
189189

190190
/**
191-
* @brief Getter function that returns the data of `PolyMod` as an `NTL::ZZX`.
191+
* @brief Getter function that returns the data of `PolyMod` as an
192+
* `NTL::ZZX` const reference.
192193
**/
193-
NTL::ZZX getData() const;
194+
const NTL::ZZX& getData() const;
194195

195196
/**
196197
* @brief Equals operator between two `PolyMod` objects.
@@ -373,6 +374,46 @@ class PolyMod
373374
**/
374375
PolyMod& operator-=(const NTL::ZZX& otherPoly);
375376

377+
/**
378+
* @brief Deserialize a `PolyMod` object from the input stream `is`.
379+
* @param is Input `std::istream`.
380+
* @param poly Destination `PolyMod` object.
381+
* @throws IOError if the stream is badly formatted (i.e. it is not delimited
382+
* by '[' and ']').
383+
* @note `poly` must be constructed with an appropriate p2r and G @b BEFORE
384+
* calling this function. For example,
385+
* @code
386+
* PolyMod my_poly(p2r, G);
387+
* deserialize(std::cin, my_poly);
388+
* @endcode
389+
*
390+
* The input stream has to be formatted as a comma-separated list surrounded
391+
* by '[' and ']'.\n
392+
* Each element of the list will be deserialized as a coefficient of the
393+
* polynomial.\n
394+
* For example '['coef0', 'coef1', 'coef2']' will be deserialized as a
395+
* `PolyMod` object `poly` where `poly[0]=coef0`, `poly[1]=coef1`,
396+
* `poly[2]=coef2` and `poly[i]=0` for `i>2`.
397+
**/
398+
friend void deserialize(std::istream& is, PolyMod& poly);
399+
400+
/**
401+
* @brief Serialize a `PolyMod` to the output stream `os`.
402+
* @param os Output `std::ostream`.
403+
* @param poly `PolyMod` object to be written.
404+
* @return Input `std::ostream` post writing.
405+
* @note p2r and G are not serialized, see note of `deserialize`.
406+
*
407+
* The output stream will be formatted as a comma-separated list surrounded by
408+
* '[' and ']'.\n
409+
* Each coefficient of `poly` will be serialized in an element of such list by
410+
* the `>>` operator.\n
411+
* For example if we have a `PolyMod` object `poly` such that `poly[0]=coef0`,
412+
* `poly[1]=coef1`, `poly[2]=coef2`, and `poly[i]=0` for `i>2`, it will be
413+
* serialized as '['coef0', 'coef1', 'coef2']'.
414+
**/
415+
friend void serialize(std::ostream& os, const PolyMod& slot);
416+
376417
/**
377418
* @brief Input shift operator.
378419
* @param is Input `std::istream`.
@@ -384,6 +425,16 @@ class PolyMod
384425
* PolyMod my_poly(p2r, G);
385426
* std::cin >> my_poly;
386427
* @endcode
428+
*
429+
* The input stream has to be formatted as a comma-separated list surrounded
430+
* by '[' and ']'.\n
431+
* Each element of the list will be deserialized as a coefficient of the
432+
* polynomial.\n
433+
* If the number of tokens in the list is less than the number of
434+
* coefficients, the higher-degree coefficients will be padded by 0.\n
435+
* For example '['coef0', 'coef1', 'coef2']' will be deserialized as a
436+
* `PolyMod` object `poly` where `poly[0]=coef0`, `poly[1]=coef1`,
437+
* `poly[2]=coef2` and `poly[i]=0` for `i>2`.
387438
**/
388439
friend std::istream& operator>>(std::istream& is, PolyMod& poly);
389440

@@ -392,7 +443,15 @@ class PolyMod
392443
* @param os Output `std::ostream`.
393444
* @param poly `PolyMod` object to be written.
394445
* @return Input `std::ostream` post writing.
395-
* @note p2r and G are not serialised, see note of `operator>>`.
446+
* @note p2r and G are not serialized, see note of `operator>>`.
447+
*
448+
* The output stream will be formatted as a comma-separated list surrounded by
449+
* '[' and ']'.\n
450+
* Each coefficient of `poly` will be serialized in an element of such list by
451+
* the `>>` operator.\n
452+
* For example if we have a `PolyMod` object `poly` such that `poly[0]=coef0`,
453+
* `poly[1]=coef1`, `poly[2]=coef2`, and `poly[i]=0` for `i>2`, it will be
454+
* serialized as '['coef0', 'coef1', 'coef2']'.
396455
**/
397456
friend std::ostream& operator<<(std::ostream& os, const PolyMod& poly);
398457

0 commit comments

Comments
 (0)