Skip to content

Commit

Permalink
Merge pull request #13 from itzmeanjan/update-sha3
Browse files Browse the repository at this point in the history
Refactor FrodoKEM Implementation
  • Loading branch information
itzmeanjan committed Aug 20, 2023
2 parents 5fee3a5 + b457f99 commit 1b36a04
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 367 deletions.
23 changes: 17 additions & 6 deletions .github/workflows/test_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ on:
jobs:
build:

runs-on: ubuntu-latest
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- name: Setup Compiler
run: |
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
- name : Fetch Dependency
run: git submodule update --init
- name: Get CMake
run: sudo apt-get install cmake
- name: Setup Google-Test
run: |
pushd ~
git clone https://github.com/google/googletest.git -b v1.13.0
pushd googletest
mkdir build
pushd build
cmake .. -DBUILD_GMOCK=OFF
make
sudo make install
popd
popd
popd
- name: Execute Tests
run: make
run: make -j
- name: Run Examples
run: |
g++ -std=c++20 -O3 -march=native -mtune=native -Wall -I include -I sha3/include -I subtle/include examples/efrodo640_kem.cpp
Expand Down
71 changes: 54 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -pedantic
OPTFLAGS = -O3 -march=native -mtune=native
IFLAGS = -I ./include
DEPFLAGS = -I ./sha3/include -I ./subtle/include
CXX_FLAGS = -std=c++20
WARN_FLAGS = -Wall -Wextra -pedantic
OPT_FLAGS = -O3 -march=native -mtune=native
LINK_FLAGS = -flto
I_FLAGS = -I ./include
DEP_IFLAGS = -I ./sha3/include -I ./subtle/include

all: testing
SRC_DIR = include
FRODO_SOURCES := $(wildcard $(SRC_DIR)/*.hpp)
BUILD_DIR = build

test/a.out: test/main.cpp include/*.hpp include/test/*.hpp sha3/include/*.hpp subtle/include/*.hpp
$(CXX) $(CXXFLAGS) $(OPTFLAGS) $(IFLAGS) $(DEPFLAGS) $< -o $@
TEST_DIR = tests
TEST_SOURCES := $(wildcard $(TEST_DIR)/*.cpp)
TEST_OBJECTS := $(addprefix $(BUILD_DIR)/, $(notdir $(patsubst %.cpp,%.o,$(TEST_SOURCES))))
TEST_LINK_FLAGS = -lgtest -lgtest_main
TEST_BINARY = $(BUILD_DIR)/test.out

testing: test/a.out
BENCHMARK_DIR = benchmarks
BENCHMARK_SOURCES := $(wildcard $(BENCHMARK_DIR)/*.cpp)
BENCHMARK_OBJECTS := $(addprefix $(BUILD_DIR)/, $(notdir $(patsubst %.cpp,%.o,$(BENCHMARK_SOURCES))))
BENCHMARK_LINK_FLAGS = -lbenchmark -lbenchmark_main
BENCHMARK_BINARY = $(BUILD_DIR)/bench.out
PERF_LINK_FLAGS = -lbenchmark -lbenchmark_main -lpfm
PERF_BINARY = $(BUILD_DIR)/perf.out

all: test

$(BUILD_DIR):
mkdir -p $@

$(BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp $(BUILD_DIR)
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) $(DEP_IFLAGS) -c $< -o $@

$(TEST_BINARY): $(TEST_OBJECTS)
$(CXX) $(OPT_FLAGS) $(LINK_FLAGS) $^ $(TEST_LINK_FLAGS) -o $@

test: $(TEST_BINARY)
./$<

bench/a.out: bench/main.cpp include/*.hpp include/bench/*.hpp sha3/include/*.hpp subtle/include/*.hpp
$(CXX) $(CXXFLAGS) $(OPTFLAGS) $(IFLAGS) $(DEPFLAGS) $< -lbenchmark -o $@
$(BUILD_DIR)/%.o: $(BENCHMARK_DIR)/%.cpp $(BUILD_DIR)
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) $(DEP_IFLAGS) -c $< -o $@

$(BENCHMARK_BINARY): $(BENCHMARK_OBJECTS)
$(CXX) $(OPT_FLAGS) $(LINK_FLAGS) $^ $(BENCHMARK_LINK_FLAGS) -o $@

benchmark: $(BENCHMARK_BINARY)
# Must *not* build google-benchmark with libPFM
./$< --benchmark_time_unit=ms --benchmark_min_warmup_time=1. --benchmark_counters_tabular=true

$(PERF_BINARY): $(BENCHMARK_OBJECTS)
$(CXX) $(OPT_FLAGS) $(LINK_FLAGS) $^ $(PERF_LINK_FLAGS) -o $@

perf: $(PERF_BINARY)
# Must build google-benchmark with libPFM, follow https://gist.github.com/itzmeanjan/05dc3e946f635d00c5e0b21aae6203a7
./$< --benchmark_time_unit=ms --benchmark_min_warmup_time=1. --benchmark_perf_counters=CYCLES --benchmark_counters_tabular=true

benchmark: bench/a.out
# Don't forget to put all CPU cores on performance mode before running benchmarks,
# follow https://github.com/google/benchmark/blob/2dd015df/docs/reducing_variance.md
./$< --benchmark_time_unit=ms --benchmark_counters_tabular=true
.PHONY: format clean

clean:
find . -name '*.out' -o -name '*.o' -o -name '*.so' -o -name '*.gch' | xargs rm -rf
rm -rf build

format:
find . -name '*.cpp' -o -name '*.hpp' | xargs clang-format -i --style=Mozilla
format: $(FRODO_SOURCES) $(TEST_SOURCES) $(BENCHMARK_SOURCES)
clang-format -i --style=Mozilla $^
178 changes: 102 additions & 76 deletions README.md

Large diffs are not rendered by default.

45 changes: 0 additions & 45 deletions bench/main.cpp

This file was deleted.

28 changes: 23 additions & 5 deletions include/bench/bench_kem.hpp → benchmarks/bench_kem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#include "kem.hpp"
#include "prng.hpp"
#include <algorithm>
Expand All @@ -7,9 +6,6 @@
#include <span>
#include <vector>

// Benchmark FrodoKEM and its components
namespace bench_frodo {

namespace utils = frodo_utils;

// Benchmark execution of Frodo key generation algorithm, for some specific
Expand Down Expand Up @@ -201,4 +197,26 @@ decaps(benchmark::State& state)
state.SetItemsProcessed(state.iterations());
}

}
BENCHMARK(keygen<640, 8, 128, 256, 128, 2, 15>)->Name("frodo640-keygen");
BENCHMARK(encaps<640, 8, 128, 256, 128, 256, 2, 15>)->Name("frodo640-encaps");
BENCHMARK(decaps<640, 8, 128, 256, 128, 256, 2, 15>)->Name("frodo640-decaps");

BENCHMARK(keygen<976, 8, 192, 384, 128, 3, 16>)->Name("frodo976-keygen");
BENCHMARK(encaps<976, 8, 192, 384, 128, 384, 3, 16>)->Name("frodo976-encaps");
BENCHMARK(decaps<976, 8, 192, 384, 128, 384, 3, 16>)->Name("frodo976-decaps");

BENCHMARK(keygen<1344, 8, 256, 512, 128, 4, 16>)->Name("frodo1344-keygen");
BENCHMARK(encaps<1344, 8, 256, 512, 128, 512, 4, 16>)->Name("frodo1344-encaps");
BENCHMARK(decaps<1344, 8, 256, 512, 128, 512, 4, 16>)->Name("frodo1344-decaps");

BENCHMARK(keygen<640, 8, 128, 128, 128, 2, 15>)->Name("efrodo640-keygen");
BENCHMARK(encaps<640, 8, 128, 128, 128, 0, 2, 15>)->Name("efrodo640-encaps");
BENCHMARK(decaps<640, 8, 128, 128, 128, 0, 2, 15>)->Name("efrodo640-decaps");

BENCHMARK(keygen<976, 8, 192, 192, 128, 3, 16>)->Name("efrodo976-keygen");
BENCHMARK(encaps<976, 8, 192, 192, 128, 0, 3, 16>)->Name("efrodo976-encaps");
BENCHMARK(decaps<976, 8, 192, 192, 128, 0, 3, 16>)->Name("efrodo976-decaps");

BENCHMARK(keygen<1344, 8, 256, 256, 128, 4, 16>)->Name("efrodo1344-keygen");
BENCHMARK(encaps<1344, 8, 256, 256, 128, 0, 4, 16>)->Name("efrodo1344-encaps");
BENCHMARK(decaps<1344, 8, 256, 256, 128, 0, 4, 16>)->Name("efrodo1344-decaps");
Loading

0 comments on commit 1b36a04

Please sign in to comment.