Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
scutil.*
# files related to build
build/
.cache/

# do not track compiled documentation files
doc/
/TAGS
/GPATH
/GRTAGS
/GTAGS
/.projectile

# build files for examples
examples/build/
examples/libscl.*

# misc IDE stuff
secure-computation-library.*
/.cache/
/compile_commands.json
/lib/
/install/
compile_commands.json
77 changes: 51 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

cmake_minimum_required( VERSION 3.14 )

project( scl VERSION 1.1.1 DESCRIPTION "Secure Computation Library" )
project( scl VERSION 2.0.0 DESCRIPTION "Secure Computation Library" )

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

option(WITH_EC "Include support for elliptic curves (requires GMP)" ON)

message(STATUS "CMAKE_BUILD_TYPE=" ${CMAKE_BUILD_TYPE})
message(STATUS "WITH_EC=" ${WITH_EC})

if(WITH_EC MATCHES ON)
find_library(GMP gmp libgmp REQUIRED)
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -pedantic -Werror -std=gnu++17")
Expand All @@ -44,6 +51,13 @@ set(SCL_SOURCE_FILES
src/scl/net/discovery/server.cc
src/scl/net/discovery/client.cc)

if(WITH_EC MATCHES ON)
set(SCL_SOURCE_FILES ${SCL_SOURCE_FILES}
src/scl/math/secp256k1_field.cc
src/scl/math/secp256k1_curve.cc
src/scl/math/number.cc)
endif()

set(SCL_HEADERS "${CMAKE_SOURCE_DIR}/include")

include_directories(${SCL_HEADERS})
Expand All @@ -65,7 +79,7 @@ if(CMAKE_BUILD_TYPE MATCHES "Release")
FILES_MATCHING PATTERN "*.h")

endif()

if(CMAKE_BUILD_TYPE MATCHES "Debug")

set(SCL_TEST_SOURCE_FILES
Expand Down Expand Up @@ -97,29 +111,40 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")

test/scl/p/test_simple.cc)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
find_package(Catch2 REQUIRED)
include(CTest)
include(Catch)
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)

# Tests that check bounds for reading/writing are sped up considerably by
# lowering said bounds.
add_compile_definitions(MAX_VEC_READ_SIZE=1024)
add_compile_definitions(MAX_MAT_READ_SIZE=1024)
add_compile_definitions(SCL_TEST_DATA_DIR="${CMAKE_SOURCE_DIR}/test/data/")

add_executable(scl_test ${SCL_SOURCE_FILES} ${SCL_TEST_SOURCE_FILES})
target_link_libraries(scl_test Catch2::Catch2 pthread)
catch_discover_tests(scl_test)

append_coverage_compiler_flags()

# Tell lcov to ignore system STL headers in order to make the coverage
# output more precise.
setup_target_for_coverage_lcov(
NAME coverage
EXECUTABLE scl_test
EXCLUDE "/usr/include/*" "test/*" "/usr/lib/*" "/usr/local/*")
if(WITH_EC MATCHES ON)
set(SCL_TEST_SOURCE_FILES ${SCL_TEST_SOURCE_FILES}
test/scl/math/test_secp256k1.cc
test/scl/math/test_number.cc)
add_compile_definitions(SCL_ENABLE_EC_TESTS)
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
find_package(Catch2 REQUIRED)
include(CTest)
include(Catch)
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)

# Lower the max size of Vec/Mat reads to speed up tests
add_compile_definitions(MAX_VEC_READ_SIZE=1024)
add_compile_definitions(MAX_MAT_READ_SIZE=1024)
add_compile_definitions(SCL_TEST_DATA_DIR="${CMAKE_SOURCE_DIR}/test/data/")

add_executable(scl_test ${SCL_SOURCE_FILES} ${SCL_TEST_SOURCE_FILES})
target_link_libraries(scl_test Catch2::Catch2 pthread)

if(WITH_EC MATCHES ON)
target_link_libraries(scl_test ${GMP})
endif()

catch_discover_tests(scl_test)

append_coverage_compiler_flags()

# Tell lcov to ignore system STL headers in order to make the coverage
# output more precise.
setup_target_for_coverage_lcov(
NAME coverage
EXECUTABLE scl_test
EXCLUDE "/usr/include/*" "test/*" "/usr/lib/*" "/usr/local/*")

endif()
6 changes: 3 additions & 3 deletions DoxyConf
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ WARN_LOGFILE =
INPUT = ./include \
./include/scl \
./include/scl/math \
./include/scl/math/z2k \
./include/scl/math/fields \
./include/scl/math/curves \
./include/scl/ss \
./include/scl/net \
./include/scl/net/discovery \
Expand All @@ -111,7 +112,7 @@ RECURSIVE = NO
EXCLUDE =
EXCLUDE_SYMLINKS = NO
EXCLUDE_PATTERNS =
EXCLUDE_SYMBOLS = _SCL_*
EXCLUDE_SYMBOLS = SCL_*
EXAMPLE_PATH =
EXAMPLE_PATTERNS = *
EXAMPLE_RECURSIVE = NO
Expand All @@ -133,7 +134,6 @@ VERBATIM_HEADERS = YES
CLANG_ASSISTED_PARSING = NO
CLANG_OPTIONS =
ALPHABETICAL_INDEX = YES
COLS_IN_ALPHA_INDEX = 5
IGNORE_PREFIX =
GENERATE_HTML = YES
HTML_OUTPUT = html
Expand Down
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# SCL — Secure Computation Library

SCL is a utilities library for prototyping Secure Multiparty Computation (MPC
for short) protocols. The focus of SCL is usability, both in terms of the
interfaces provided, but also the build process (SCL has no external
dependencies, for example). SCL moreover attempts to provide functionality that
abstracts away all the annoying "boilerplate" code that is needed for
implementing a new and exciting MPC protocol, such as implementing a finite
field, getting networking to work, or instantiating a PRG or hash function.

Hopefully, by using SCL, researches (and hobbyists) will find it a lot easier,
and quicker!, to implement MPC protocols.
SCL is a utilities library for prototyping Secure Multiparty Computation (_MPC_
for short) protocols. The focus of SCL is usability, not necessarily speed. What
this means is that SCL strives to provide an intuitive, easy to use and
understand and well documented interface that helps the programmer prototype an
MPC protocol faster (and nicer) than if they had to write everything themselves.

SCL provides high level interfaces and functionality for working with
* Secret sharing, additive and Shamir.
* Finite fields.
* Networking.
* Primitives, such as hash functions and PRGs.

### Disclaimer

Expand All @@ -20,13 +21,11 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.


# Building SCL

SCL has no external dependencies, except if you want to build the unittests. In
that case, [catch2](https://github.com/catchorg/Catch2/tree/v2.x) is required as
that's the framework used for testing, as well as ~lcov~ for generating test
coverage.
SCL uses [gmp](https://gmplib.org/) for working with Elliptic Curves, and
[catch2](https://github.com/catchorg/Catch2/tree/v2.x) for testing and `lcov`
for test coverage.

The CMake file recongnizes two different build types: `Debug` and `Release`, the
latter being the default. In either case, building is straight forward and can
Expand All @@ -47,6 +46,10 @@ after the build command. By default, headers are install in `usr/local/include`
and the shared library in `/usr/local/lib`. This location can be controlled by
setting the `CMAKE_INSTALL_PREFIX` accordingly.

Support for Elliptic Curves can be disabled (and thus remove the need to have
gmp installed) by passing `-DWITH_EC=OFF` to cmake.


# Using SCL

To use SCL, link `libscl.so` when building your program and include the
Expand Down
10 changes: 10 additions & 0 deletions RELEASE.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2.0: Elliptic curves and finite field refactoring
- Make it simpler to define new finite fields
- Include optional (but enabled by default) support for elliptic curves
- Implement secp256k1
- Include optional (but enabled by default) support for multi-precision integers
- Significantly increase test coverage
- Make header guards standard compliant
- Rename FF<Bits> to Fp<Bits>.
- Move class FF into scl namespace.

1.1: Refactoring of finite field internals
- Finite field operations are now defined by individual specializations of
templated functions
Expand Down
18 changes: 9 additions & 9 deletions examples/02_finite_fields.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main() {
* which returns the actual size of a field element (so 61 bits in the below
* case).
*/
using FF = scl::FF<32>;
using Fp = scl::Fp<32>;

/* FF supports constructing an element from an int constant. The value input
* is interpreted "modulo p" where p is the prime. This makes it possible to
Expand All @@ -42,9 +42,9 @@ int main() {
* The default construtor of FF can be used to construct an element equal to
* 0.
*/
auto a = FF(1);
auto b = FF(1234);
auto c = FF(555);
auto a = Fp(1);
auto b = Fp(1234);
auto c = Fp(555);

/* FF supports all operations required for a field, so addition, subtraction,
* multiplication and "division". Division is defined as multiplication by the
Expand All @@ -71,16 +71,16 @@ int main() {

/* Using a PRG (see the PRG example), we can generate random field elements.
*/
std::cout << FF::Random(prg) << "\n";
std::cout << FF::Random(prg) << "\n";
std::cout << FF::Random(prg) << "\n";
std::cout << Fp::Random(prg) << "\n";
std::cout << Fp::Random(prg) << "\n";
std::cout << Fp::Random(prg) << "\n";

/* Serialization is also supported.
*/
unsigned char buffer[FF::ByteSize()];
unsigned char buffer[Fp::ByteSize()];

a.Write(buffer);
auto a_ = FF::Read(buffer);
auto a_ = Fp::Read(buffer);

std::cout << (a_ == a) << "\n";
}
16 changes: 8 additions & 8 deletions examples/03_secret_sharing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#include <stdexcept>

int main() {
using FF = scl::FF<32>;
using Vec = scl::Vec<FF>;
using Fp = scl::Fp<32>;
using Vec = scl::Vec<Fp>;
scl::PRG prg;

/* We can easily create an additive secret sharing of some secret value:
*/
FF secret(12345);
Fp secret(12345);
Vec shares = scl::CreateAdditiveShares(secret, 5, prg);

std::cout << "additive shares:\n" << shares << "\n";
Expand Down Expand Up @@ -59,7 +59,7 @@ int main() {

/* If we introduce an error, then reconstruction fails
*/
shamir_shares[2] = FF(123);
shamir_shares[2] = Fp(123);
try {
std::cout << scl::ReconstructShamir(shamir_shares, 1) << "\n";
} catch (std::logic_error& e) {
Expand All @@ -78,7 +78,7 @@ int main() {
/* first we need the alphas that were used when generating the shares. By
* default these are just the field elements 1 through 4.
*/
Vec alphas = {FF(1), FF(2), FF(3), FF(4)};
Vec alphas = {Fp(1), Fp(2), Fp(3), Fp(4)};
auto pe = scl::ReconstructShamirRobust(shamir_shares, alphas, 1);

/* pe is a pair of polynomials. The first is the original polynomial used for
Expand All @@ -87,16 +87,16 @@ int main() {
*
* The secret is embedded in the constant term.
*/
std::cout << pe[0].Evaluate(FF(0)) << "\n";
std::cout << pe[0].Evaluate(Fp(0)) << "\n";

/* This will be 0, indicating that the share corresponding to party 3 had an
* error.
*/
std::cout << pe[1].Evaluate(FF(3)) << "\n";
std::cout << pe[1].Evaluate(Fp(3)) << "\n";

/* Lastly, if there's too many errors, then correction is not possible
*/
shamir_shares[1] = FF(22);
shamir_shares[1] = Fp(22);
try {
scl::ReconstructShamirRobust(shamir_shares, 1);
} catch (std::logic_error& e) {
Expand Down
4 changes: 3 additions & 1 deletion examples/04_networking.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <iostream>

#include "scl/net/tcp_channel.h"

scl::NetworkConfig RunServer(int n) {
scl::DiscoveryServer server(n);
scl::Party party{0, "127.0.0.1", 5000};
Expand Down Expand Up @@ -63,7 +65,7 @@ int main(int argc, char** argv) {
* around.
*/

auto network = scl::Network::Create(config);
auto network = scl::Network::Create<scl::TcpChannel>(config);

for (std::size_t i = 0; i < 3; ++i) {
// similar to the TCP channel example, send our ID to everyone:
Expand Down
11 changes: 7 additions & 4 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ project( scl_examples VERSION 1.0 DESCRIPTION "SCL example programs" )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -O2 -std=gnu++17" )

find_library(GMP gmp libgmp REQUIRED)
find_library(SCL scl libscl REQUIRED)

## primitives
set( primitives "01_primitives" )
add_executable( "${primitives}" "${primitives}.cc" )
target_link_libraries( "${primitives}" scl pthread )
target_link_libraries( "${primitives}" "${SCL}" "${GMP}" pthread )

## finite fields
set( finite_fields "02_finite_fields" )
add_executable( "${finite_fields}" "${finite_fields}.cc" )
target_link_libraries( "${finite_fields}" scl pthread )
target_link_libraries( "${finite_fields}" "${SCL}" "${GMP}" pthread )

## secret sharing
set( secret_sharing "03_secret_sharing" )
add_executable( "${secret_sharing}" "${secret_sharing}.cc" )
target_link_libraries( "${secret_sharing}" scl pthread )
target_link_libraries( "${secret_sharing}" "${SCL}" "${GMP}" pthread )

## simple networking
set( networking "04_networking" )
add_executable( "${networking}" "${networking}.cc" )
target_link_libraries( "${networking}" scl pthread )
target_link_libraries( "${networking}" "${SCL}" "${GMP}" pthread )
6 changes: 3 additions & 3 deletions include/scl/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef _SCL_HASH_H
#define _SCL_HASH_H
#ifndef SCL_HASH_H
#define SCL_HASH_H

#include <array>
#include <cstdint>
Expand Down Expand Up @@ -214,4 +214,4 @@ std::string DigestToString(const D &digest) {

} // namespace scl

#endif // _SCL_HASH_H
#endif // SCL_HASH_H
Loading