Skip to content

Commit

Permalink
Minor edits to README.md, CHANGES.md, and .gitignore to decorate PR #306
Browse files Browse the repository at this point in the history
. Re-formatted header/source files.
  • Loading branch information
Wei Dai committed Mar 23, 2021
1 parent a549ea0 commit 3be8872
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 34 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ dotnet/examples/SEALNetExamples.csproj
CMakeSettings.json
.vscode

# WebAssembly Output
seal_wasm.js
seal_wasm.wasm

# Build
bin/
lib/
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Improved the error message when attempting to configure with `BUILD_SHARED_LIBS=ON` and `SEAL_BUILD_SEAL_C=ON` [(Issue 284)](https://github.com/microsoft/SEAL/issues/284).
- Added `seal::random_bytes` function in [randomgen.h](native/src/seal/randomgen.h).
- Added support to build SEAL out of the box with [emscripten](https://emscripten.org/) [(PR 306)](https://github.com/microsoft/SEAL/pull/306).

## Version 3.6.2

Expand Down
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,29 +315,32 @@ Specifically, they do not contain any wrappers that can be used from Java (for A

#### Building for WebAssembly

Microsoft SEAL can be compiled for JavaScript and WebAssembly using [emscripten](https://emscripten.org). Building for the Web means SEAL can be run in any client/server environment such as all the major browsers (e.g. Edge, Chrome, Firefox, Safari) and NodeJS.
Microsoft SEAL can be compiled for JavaScript and WebAssembly using [emscripten](https://emscripten.org) on Windows, Linux, and macOS.
Building for the Web means SEAL can be run in any client/server environment such as all the major browsers (e.g. Edge, Chrome, Firefox, Safari) and NodeJS.

Building for WebAssembly requires the emscripten toolchain to be installed. The easiest way to configure the toolchain is to clone [emsdk](https://github.com/emscripten-core/emsdk) somewhere on your system and follow the instructions in the README.
Building for WebAssembly requires the emscripten toolchain to be installed.
The easiest way to configure the toolchain is to clone [emsdk](https://github.com/emscripten-core/emsdk) and follow the [instructions](https://emscripten.org/docs/getting_started/downloads.html#installation-instructions-using-the-emsdk-recommended) (with system-specific notes). For examples, on Linux and macOS, inside the `emsdk` repo, run the following:

Inside the `emsdk` repo, run the following:
```PowerShell
# Install the latest toolchain
./emsdk install latest
./emsdk activate latest
# Source the environment
source ./emsdk_env.sh
```
**On Windows, better run from a developer command prompt for Visual Studio; and replace `./emsdk` and `source ./emsdk_env.sh` with `emsdk` and `emsdk_env.bat`, respectively.**
In other environments, `cmake` must be added to the path, and either "Ninja" or "MinGW Makefiles" should be specified as generator in the following configuration step.
`emcmake` does not work with Visual Studio 16 2019 generator.

With the same shell, navigate to the root directory of Microsoft SEAL, run the following commands to build for WebAssembly:
Within the same shell, navigate to the root directory of Microsoft SEAL, run the following commands to build for WebAssembly:

```PowerShell
# Configure CMake. Example flags for a release build
emcmake cmake \
-DSEAL_USE_CXX17=ON \
-DCMAKE_CXX_FLAGS_RELEASE="-DNDEBUG -flto -O3" \
-DCMAKE_C_FLAGS_RELEASE="-DNDEBUG -flto -O3" \
-DSEAL_USE_INTRIN=OFF \
-DSEAL_USE_INTRIN=ON \
-DSEAL_USE_ZLIB=ON \
-DSEAL_USE_MSGSL=OFF \
-DSEAL_BUILD_EXAMPLES=OFF \
Expand All @@ -352,23 +355,23 @@ emmake make -j
# Build the WebAssembly module
emcc \
-Wall \
-flto \
-O3 \
lib/libseal-3.6.a \
--bind \
-o "bin/seal_wasm.js" \
-s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1
-Wall \
-flto \
-O3 \
lib/libseal-3.6.a \
--bind \
-o "bin/seal_wasm.js" \
-s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1
```
**Note**: There are many flags to consider when building a WebAssembly module. Please refer to the [settings.js](https://github.com/emscripten-core/emscripten/blob/main/src/settings.js) file for advanced build flags.

Building will generate two output files in the top-level `bin/` directory:
- seal_wasm.js
- seal_wasm.wasm
**Note**: There are many flags to consider when building a WebAssembly module. Please refer to the [settings.js](https://github.com/emscripten-core/emscripten/blob/main/src/settings.js) file for advanced build flags.

Notice the file sizes for the artifacts are very small. This is because the optimization flags performed DCE as there are no JavaScript <-> WebAssembly "bindings". Defining these bindings is **necessary** in order to call into WebAssembly from the JavaScript domain; however, Microsoft SEAL does not include any definitions at this time. The build flag `--bind` expects the bindings to be specified using the [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html) syntax.
Building will generate two output files, `seal_wasm.js` and `seal_wasm.wasm`, in the top-level `bin/` directory.
The file sizes for the artifacts are very small.
This is because that the optimization flags perform dead code elimination (DCE) as there are no bindings generated to JavaScript.
Defining these bindings is **necessary** in order to call into WebAssembly from the JavaScript domain; however, Microsoft SEAL does not include any definitions at this time.
The build flag `--bind` expects the bindings to be specified using the [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html) syntax.

#### Basic CMake Options

Expand Down
10 changes: 5 additions & 5 deletions native/src/seal/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,8 @@ namespace seal
RNSIter temp_iter(temp.get(), coeff_count);
SEAL_ITERATE(iter(temp_iter, plain_upper_half_increment), coeff_modulus_size, [&](auto I) {
SEAL_ITERATE(iter(get<0>(I), plain.data()), plain_coeff_count, [&](auto J) {
get<0>(J) = SEAL_COND_SELECT(get<1>(J) >= plain_upper_half_threshold, get<1>(J) + get<1>(I), get<1>(J));
get<0>(J) =
SEAL_COND_SELECT(get<1>(J) >= plain_upper_half_threshold, get<1>(J) + get<1>(I), get<1>(J));
});
});
}
Expand Down Expand Up @@ -1748,7 +1749,8 @@ namespace seal

SEAL_ITERATE(helper_iter, coeff_modulus_size, [&](auto I) {
SEAL_ITERATE(iter(*plain_iter, get<0>(I)), plain_coeff_count, [&](auto J) {
get<1>(J) = SEAL_COND_SELECT(get<0>(J) >= plain_upper_half_threshold, get<0>(J) + get<1>(I), get<0>(J));
get<1>(J) =
SEAL_COND_SELECT(get<0>(J) >= plain_upper_half_threshold, get<0>(J) + get<1>(I), get<0>(J));
});
});
}
Expand Down Expand Up @@ -2240,9 +2242,7 @@ namespace seal
ntt_negacyclic_harvey_lazy(t_ntt, get<2>(J));
#if SEAL_USER_MOD_BIT_COUNT_MAX > 60
// Reduce from [0, 4qi) to [0, 2qi)
SEAL_ITERATE(t_ntt, coeff_count, [&](auto &K) {
K -= SEAL_COND_SELECT(K >= qi_lazy, qi_lazy, 0);
});
SEAL_ITERATE(t_ntt, coeff_count, [&](auto &K) { K -= SEAL_COND_SELECT(K >= qi_lazy, qi_lazy, 0); });
#else
// Since SEAL uses at most 60bit moduli, 8*qi < 2^63.
qi_lazy = qi << 2;
Expand Down
4 changes: 2 additions & 2 deletions native/src/seal/randomgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace seal
memcpy(buf, &last, count);
}
#elif SEAL_SYSTEM == SEAL_SYSTEM_WINDOWS
NTSTATUS status = BCryptGenRandom(
NULL, reinterpret_cast<unsigned char *>(buf), count, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
NTSTATUS status =
BCryptGenRandom(NULL, reinterpret_cast<unsigned char *>(buf), count, BCRYPT_USE_SYSTEM_PREFERRED_RNG);

if (BCRYPT_SUCCESS(status))
{
Expand Down
8 changes: 4 additions & 4 deletions native/src/seal/util/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ namespace seal
// Still need to consider integer-float conversions and all
// unsigned to signed conversions
}

SEAL_IF_CONSTEXPR(std::is_integral<T>::value && std::is_integral<S>::value)
{
// Both integer types
Expand All @@ -292,7 +292,7 @@ namespace seal
// Non-negative number; compare as std::uint64_t
// Cannot use unsigned_leq with C++14 for lack of `if constexpr'
result = static_cast<std::uint64_t>(value) <=
static_cast<std::uint64_t>((std::numeric_limits<T>::max)());
static_cast<std::uint64_t>((std::numeric_limits<T>::max)());
}
else
{
Expand All @@ -305,13 +305,13 @@ namespace seal
{
// Converting to floating-point
result = (static_cast<double>(value) <= static_cast<double>((std::numeric_limits<T>::max)())) &&
(static_cast<double>(value) >= -static_cast<double>((std::numeric_limits<T>::max)()));
(static_cast<double>(value) >= -static_cast<double>((std::numeric_limits<T>::max)()));
}
else
{
// Converting from floating-point
result = (static_cast<double>(value) <= static_cast<double>((std::numeric_limits<T>::max)())) &&
(static_cast<double>(value) >= static_cast<double>((std::numeric_limits<T>::min)()));
(static_cast<double>(value) >= static_cast<double>((std::numeric_limits<T>::min)()));
}

return result;
Expand Down
9 changes: 6 additions & 3 deletions native/src/seal/util/msvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@
#else

#ifdef SEAL_USE_IF_CONSTEXPR
#pragma message("Disabling `if constexpr` based on _MSVC_LANG value " SEAL_STRINGIZE(_MSVC_LANG) ": undefining SEAL_USE_IF_CONSTEXPR")
#pragma message("Disabling `if constexpr` based on _MSVC_LANG value \
" SEAL_STRINGIZE(_MSVC_LANG) ": undefining SEAL_USE_IF_CONSTEXPR")
#undef SEAL_USE_IF_CONSTEXPR
#endif
#ifdef SEAL_USE_MAYBE_UNUSED
#pragma message("Disabling `[[maybe_unused]]` based on _MSVC_LANG value " SEAL_STRINGIZE(_MSVC_LANG) ": undefining SEAL_USE_MAYBE_UNUSED")
#pragma message("Disabling `[[maybe_unused]]` based on _MSVC_LANG value \
" SEAL_STRINGIZE(_MSVC_LANG) ": undefining SEAL_USE_MAYBE_UNUSED")
#undef SEAL_USE_MAYBE_UNUSED
#endif
#ifdef SEAL_USE_NODISCARD
#pragma message("Disabling `[[nodiscard]]` based on _MSVC_LANG value " SEAL_STRINGIZE(_MSVC_LANG) ": undefining SEAL_USE_NODISCARD")
#pragma message("Disabling `[[nodiscard]]` based on _MSVC_LANG value " \
SEAL_STRINGIZE(_MSVC_LANG) ": undefining SEAL_USE_NODISCARD")
#undef SEAL_USE_NODISCARD
#endif
Expand Down

0 comments on commit 3be8872

Please sign in to comment.