Skip to content

Commit

Permalink
Updates libgost15 to 0.4.0, see details
Browse files Browse the repository at this point in the history
Major changes
    + Renames methods, manages src subdirectories, renames main header
    + Integrates CTest
    + Tweaks Travis CI script

Minor changes
    + Modifies method of implementation selection
    + Removes compiler-specific stuff
    + Adds platform header
    + Disables coverage in selftests
  • Loading branch information
Arseny Aprelev committed Jun 5, 2016
1 parent 4253724 commit 8ced6f7
Show file tree
Hide file tree
Showing 22 changed files with 258 additions and 266 deletions.
32 changes: 14 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,19 @@ compiler:
- clang

env:
global:
- ENABLE_COVERAGE=ON
matrix:
- ENABLE_PRECALCULATIONS=OFF ENABLE_SIMD=OFF BUILD_TYPE=Release
- ENABLE_PRECALCULATIONS=OFF ENABLE_SIMD=OFF BUILD_TYPE=Debug
- ENABLE_PRECALCULATIONS=ON ENABLE_SIMD=OFF BUILD_TYPE=Release
- ENABLE_PRECALCULATIONS=ON ENABLE_SIMD=OFF BUILD_TYPE=Debug
# allow_failures:
# - ENABLE_PRECALCULATIONS=ON ENABLE_SIMD=ON BUILD_TYPE=Debug
# - ENABLE_PRECALCULATIONS=ON ENABLE_SIMD=ON BUILD_TYPE=Release
- Coverage=ON BuildType=Release Implementation=Compact
- Coverage=ON BuildType=Release Implementation=Optimised
matrix:
allow_failures:
- env: Coverage=ON BuildType=Release Implementation=SIMD

install:
# Installing CMake 3.2.1
- wget --no-check-certificate http://www.cmake.org/files/v3.2/cmake-3.2.1.tar.gz
- tar -xzf cmake-3.2.1.tar.gz
- cd cmake-3.2.1
- cmake .
- make
- cmake . > /dev/null
- make > /dev/null
- cd ..
- rm -rf cmake-3.2.1.tar.gz

Expand All @@ -37,15 +32,16 @@ before_script:
- cd build

- ../cmake-3.2.1/bin/cmake
-DCMAKE_BUILD_TYPE=$BUILD_TYPE
-DENABLE_PRECALCULATIONS=$ENABLE_PRECALCULATIONS
-DENABLE_SIMD=$ENABLE_SIMD
-DENABLE_COVERAGE=$ENABLE_COVERAGE
-DCMAKE_BUILD_TYPE=$BuildType
-DIMPLEMENTATION=$Implementation
-DENABLE_COVERAGE=$Coverage
-DCMAKE_VERBOSE_MAKEFILE=ON
-DCMAKE_RULE_MESSAGES=OFF
../src/

script:
- make selftests_gost
- ./libgost15/tests/selftests_gost
- make --no-print-directory all
- cd libgost15/tests/ && ctest

after_success:
- bash <(curl -s https://codecov.io/bash)
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ All functions provided by `libgost15` are thread-safe thus measuring takes place
| Block encryption | 1.2840 MB/s | 62.6575 MB/s | 112.2875 MB/s |
| Block decryption | 1.2676 MB/s | 64.4036 MB/s | 114.6625 MB/s |

Performance measuring is enabled when `benchmark` target is built with CMake:

```
cmake -DIMPLEMENTATION=[implementation] [target]
make benchmark
./benchmark/benchmark
```

### Implementations

Expand All @@ -54,26 +61,30 @@ Why use this and not [official TC26 implementation](http://tc26.ru/standard/gost
* All sixteen R transformations are merged into single L transformation thus cutting out rotations.
* Better grammar and code organisation.

This implementation is build by default and it does not require any special predefined variables.
This implementation is built with `-DIMPLEMENTATION=Compact` option:

```
cmake -DIMPLEMENTATION=Compact [target]
```

#### Optimised implementation

Optimised implementation employs vector-by-matrix multiplication precomutation technique described in [no link yet], similar to one in 64KB versions of AES. This implementation is much faster that the compact one, but requires 128KB os additional memory in data segment for storing precomputed tables. Does not require SSE instructions.

To use optimised implementation, define `ENABLE_PRECALCULATIONS` environment variable before building:
This implementation is built with `-DIMPLEMENTATION=Optimised` option:

```
cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_PRECALCULATIONS=ON ...
cmake -DIMPLEMENTATION=Optimised [target]
```

#### SIMD implementation

SIMD implementation utilises SSE instruction set, a set of extended processor instructions which enable one to operate over 128-bit XMM registers, thus further speeding up optimised implementation. Requires SSE2 or higher.

To use optimised implementation, define both `ENABLE_PRECALCULATIONS` and `ENABLE_SIMD` environment variables before building:
This implementation is built with `-DIMPLEMENTATION=SIMD` option:

```
cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_PRECALCULATIONS=ON -DENABLE_SIMD=ON ...
cmake -DIMPLEMENTATION=SIMD [target]
```

Future versions of `libgost15` might enable this implementation version by default when optimised version is selected and SSE instruction set (SSE2+) is available.
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.2)

## libgost15-lib project declaration
project(aprelev-libgost15 VERSION 0.3.6)
project(libgost15-wrap VERSION 0.4.0)

## libgost15 library and selftests
add_subdirectory(libgost15)
Expand Down
12 changes: 11 additions & 1 deletion src/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
cmake_minimum_required(VERSION 3.2)

## libgost15 project declaration
project(libgost15-benchmark VERSION 0.3.5 LANGUAGES CXX)
project(benchmark VERSION 0.3.6 LANGUAGES CXX)

## benchmark definition
add_executable(benchmark src/benchmark.cpp)

## Falling back to strict C++11 standard
set_target_properties(benchmark PROPERTIES CXX_STANDARD 11)
set_target_properties(benchmark PROPERTIES CXX_STANDARD_REQUIRED ON)
set_target_properties(benchmark PROPERTIES CXX_EXTENSIONS OFF)

## Excluding benchmark from all target
set_target_properties(benchmark PROPERTIES EXCLUDE_FROM_ALL ON)

## Linking libgost15
target_link_libraries(benchmark libgost15)
6 changes: 3 additions & 3 deletions src/benchmark/src/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iomanip>
#include <sstream>
#include <random>
#include <libgost15/gost15.h>
#include <libgost15/libgost15.h>

const auto defaultDuration = std::chrono::duration<double, std::milli>(2000.);

Expand Down Expand Up @@ -90,7 +90,7 @@ void benchmarkEncryption(std::chrono::duration<double, std::milli> minimumDurati
auto startedAt_ = std::chrono::high_resolution_clock::now();

for (size_t iterationIndex_ = 0; iterationIndex_ < iterations_; ++iterationIndex_) {
encryptBlock(roundKeys_, block_);
encryptBlockWithGost15(roundKeys_, block_);
}

auto finishedAt_ = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -128,7 +128,7 @@ void benchmarkDecryption(std::chrono::duration<double, std::milli> minimumDurati
auto startedAt_ = std::chrono::high_resolution_clock::now();

for (size_t iterationIndex_ = 0; iterationIndex_ < iterations_; ++iterationIndex_) {
encryptBlock(roundKeys_, block_);
encryptBlockWithGost15(roundKeys_, block_);
}

auto finishedAt_ = std::chrono::high_resolution_clock::now();
Expand Down
62 changes: 19 additions & 43 deletions src/libgost15/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@
cmake_minimum_required(VERSION 3.2)

## libgost15 project declaration
project(libgost15 VERSION 0.3.5 LANGUAGES C)
project(libgost15 VERSION 0.4.0 LANGUAGES C)

## libgost15 global options
option(ENABLE_PRECALCULATIONS "Use optimised implementation with precalculated tables" OFF)
option(ENABLE_SIMD "Enable SIMD code optimisations" OFF)
set(IMPLEMENTATION "Optimised" CACHE STRING "Implementation version: Compact, Optimised or SIMD.")

## libgost15 definition
add_library(libgost15)

## libgost15 source files
if (ENABLE_PRECALCULATIONS AND ENABLE_SIMD)
message(WARNING "Building SIMD implementation version.")
target_sources(libgost15 PRIVATE src/SIMD.c src/tables.c src/SIMD_tables.c)
elseif(ENABLE_PRECALCULATIONS)
message(WARNING "Building optimised implementation version.")
target_sources(libgost15 PRIVATE src/optimised.c src/tables.c src/optimised_tables.c)
if (IMPLEMENTATION STREQUAL SIMD)
message(AUTHOR_WARNING "Building SIMD implementation version.")
target_sources(libgost15 PRIVATE src/SIMD/SIMD.c src/SIMD/SIMD_tables.c src/shared/tables.c)
elseif(IMPLEMENTATION STREQUAL Optimised)
message(AUTHOR_WARNING "Building optimised implementation version.")
target_sources(libgost15 PRIVATE src/optimised/optimised.c src/optimised/optimised_tables.c src/shared/tables.c)
elseif(IMPLEMENTATION STREQUAL Compact)
message(AUTHOR_WARNING "Building compact implementation version.")
target_sources(libgost15 PRIVATE src/compact/compact.c src/shared/tables.c)
else()
message(WARNING "Building compact implementation version.")
target_sources(libgost15 PRIVATE src/compact.c src/tables.c)
message(FATAL_ERROR "No implementation specified.")
endif()

## libgost15 include directories and compiler features
## libgost15 include directories
target_include_directories(libgost15 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_include_directories(libgost15 PUBLIC $<INSTALL_INTERFACE:include>)
target_include_directories(libgost15 PRIVATE src)
Expand All @@ -40,38 +41,13 @@ endif()

## Falling back to strict C standard
set_target_properties(libgost15 PROPERTIES C_EXTENSIONS OFF)
set_target_properties(libgost15 PROPERTIES C_STANDARD 99)
set_target_properties(libgost15 PROPERTIES C_STANDARD_REQUIRED ON)

## libgost15 GCC compiler-specific flags
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
target_compile_options(libgost15 PRIVATE $<$<CONFIG:Debug>:-pedantic -Wall -Wextra -Werror -Wno-unused-function -Wno-cast-align>)
target_compile_options(libgost15 PRIVATE $<$<CONFIG:Release>:-march=native>)

## Disabling SIMD if required
if(NOT ENABLE_SIMD)
target_compile_options(libgost15 PRIVATE -mno-sse)
endif()

## Enabling coverage
if(ENABLE_COVERAGE)
target_link_libraries(libgost15 PRIVATE --coverage)
target_compile_options(libgost15 PRIVATE --coverage)
endif()

## libgost15 Clang compiler-specific flags
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
target_compile_options(libgost15 PRIVATE $<$<CONFIG:DEBUG>:-Weverything -Werror -Wno-unused-function -Wno-cast-align>)
target_compile_options(libgost15 PRIVATE $<$<CONFIG:RELEASE>:-march=native>)

## Disabling SIMD if required
if(NOT ENABLE_SIMD)
target_compile_options(libgost15 PRIVATE -mno-sse)
endif()

## Enabling coverage
if(ENABLE_COVERAGE)
target_link_libraries(libgost15 PRIVATE --coverage)
target_compile_options(libgost15 PRIVATE --coverage)
endif()
## Enabling coverage
if(ENABLE_COVERAGE)
target_link_libraries(libgost15 PRIVATE --coverage)
target_compile_options(libgost15 PRIVATE --coverage)
endif()

## libgost15 selftests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#if !defined LIBGOST15_HEADER_INCLUDED_
#define LIBGOST15_HEADER_INCLUDED_

#include <stdint.h>
#include <stddef.h>

#include <libgost15/platform.h>

enum {
NumberOfRounds = 10,
Expand All @@ -19,22 +17,23 @@ extern const size_t WorkspaceOfScheduleRoundKeys;
extern "C" {
#endif

void encryptBlock(
void encryptBlockWithGost15(
const void *roundKeys,
void *block
);
void decryptBlock(

void decryptBlockWithGost15(
const void *roundKeys,
void *block
);

void scheduleEncryptionRoundKeys(
void scheduleEncryptionRoundKeysForGost15(
void *roundKeys,
const void *key,
void *memory
);

void scheduleDecryptionRoundKeys(
void scheduleDecryptionRoundKeysForGost15(
void *roundKeys,
const void *key,
void *memory
Expand Down
14 changes: 14 additions & 0 deletions src/libgost15/include/libgost15/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if !defined LIBGOST15_PLATFORM_HEADER_INCLUDED_
#define LIBGOST15_PLATFORM_HEADER_INCLUDED_

#include <stdint.h>
#include <stddef.h>

/* TODO: Make platform-dependent keywords available within CMake.
*/

/* Restrict keyword macros. */

/* Force inline keyword macros. */

#endif
16 changes: 8 additions & 8 deletions src/libgost15/src/SIMD.c → src/libgost15/src/SIMD/SIMD.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <libgost15/libgost15.h>
#include <SIMD/SIMD_tables.h>
#include <shared/tables.h>
#include <emmintrin.h>
#include <string.h>
#include <libgost15/gost15.h>
#include <tables.h>
#include <SIMD_tables.h>

// TODO: Beat alignment warnings with Clang's and GCC's -Wcast-align.

Expand Down Expand Up @@ -139,7 +139,7 @@ static void applyFTransformation(
}


void scheduleEncryptionRoundKeys(
void scheduleEncryptionRoundKeysForGost15(
void *restrict roundKeys,
const void *restrict key,
void *restrict memory
Expand All @@ -165,13 +165,13 @@ void scheduleEncryptionRoundKeys(
}


void scheduleDecryptionRoundKeys(
void scheduleDecryptionRoundKeysForGost15(
void *restrict roundKeys,
const void *restrict key,
void *restrict memory
) {
uint64_t *roundKeys_ = roundKeys;
scheduleEncryptionRoundKeys(roundKeys, key, memory);
scheduleEncryptionRoundKeysForGost15(roundKeys, key, memory);

for (int keyIndex_ = 1; keyIndex_ <= 8; ++keyIndex_) {
__m128i temporary1_, temporary2_;
Expand All @@ -184,7 +184,7 @@ void scheduleDecryptionRoundKeys(
}


void encryptBlock(
void encryptBlockWithGost15(
const void *restrict roundKeys,
void *restrict data
) {
Expand All @@ -202,7 +202,7 @@ void encryptBlock(
}


void decryptBlock(
void decryptBlockWithGost15(
const void *restrict roundKeys,
void *restrict data
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <SIMD_tables.h>
#include <SIMD/SIMD_tables.h>

const uint8_t ALIGNED(bitmask[16]) = {
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !defined LIBGOST15_SIMD_TABLES_HEADER_INCLUDED_
#define LIBGOST15_SIMD_TABLES_HEADER_INCLUDED_

#include <stdint.h>
#include <libgost15/platform.h>

/* Crude alignment macros. */
#if defined _MSC_VER
Expand Down
Loading

0 comments on commit 8ced6f7

Please sign in to comment.