Skip to content

Commit

Permalink
Merge pull request #24 from mrdcvlsc/use-epi-and-cxx20
Browse files Browse the repository at this point in the history
Use epi and cxx20
  • Loading branch information
mrdcvlsc authored Jun 6, 2024
2 parents 39253d1 + 2c832b6 commit b37c698
Show file tree
Hide file tree
Showing 50 changed files with 281 additions and 1,833 deletions.
2 changes: 1 addition & 1 deletion .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ InlayHints:
DeducedTypes: Yes

CompileFlags:
Add: [-std=c++11, -I"include/epi", -I"benchmark/boost/include", -Wall, -Wextra, -Wpedantic, -Werror]
Add: [-std=c++20, -I"include/epi", -I"benchmark/boost/include", -Wall, -Wextra, -Wpedantic, -Werror]
Compiler: clang++

Diagnostics:
Expand Down
23 changes: 13 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ jobs:
runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v3

- name: install clang
run: sudo apt install clang

- uses: actions/checkout@v2
- name: get updated submodules
run: git submodule update --init --recursive

- name: build and compile the static library.
run: make -f staticlib CC=clang++
run: make -f staticlib CXX=clang++

- name: run the tests for the static library.
run: make -f staticlib static_test CC=clang++
run: make -f staticlib static_test CXX=clang++

- name: clean test executables
run: make clean

- name: install the static library.
run: sudo make -f staticlib install CC=clang++
run: sudo make -f staticlib install CXX=clang++

- name: compile the static sample program for the library.
run: clang++ static-build.cpp -o static-build.out -lchacha20 -fsanitize=address
run: clang++ -std=c++20 static-build.cpp -o static-build.out -lchacha20 -fsanitize=address

- name: run the sample program for the library.
run: ./static-build.out
Expand All @@ -48,7 +48,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: get updated submodules
run: git submodule update --init --recursive

- name: build and compile the static library.
run: make -f staticlib
Expand All @@ -63,7 +66,7 @@ jobs:
run: sudo make -f staticlib install

- name: compile the static sample program for the library.
run: g++ static-build.cpp -o static-build.out -lchacha20 -fsanitize=address
run: g++ -std=c++20 static-build.cpp -o static-build.out -lchacha20 -fsanitize=address

- name: run the sample program for the library.
run: ./static-build.out
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ jobs:
- name: install clang
run: sudo apt install clang

- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: get updated submodules
run: git submodule update --init --recursive

- name: compile and run tests in header mode
run: make header_test CC=clang++
run: make header_test CXX=clang++
6 changes: 5 additions & 1 deletion .github/workflows/gcc-gnu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: get updated submodules
run: git submodule update --init --recursive

- name: compile and run tests in header mode
run: make header_test
7 changes: 6 additions & 1 deletion .github/workflows/mingw64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: get updated submodules
run: git submodule update --init --recursive

- name: compiler architecture
run: gcc -dumpmachine

- name: compile and run tests in header mode
run: make header_test
5 changes: 4 additions & 1 deletion .github/workflows/msvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: get updated submodules
run: git submodule update --init --recursive

- name: Configure test executables
run: cmake -S tests -B tests ${{matrix.platform.flags}}
Expand All @@ -31,4 +34,4 @@ jobs:
run: cmake --build tests --config Debug

- name: Run ${{matrix.platform.bin}} test executables
run: ctest --test-dir tests
run: ctest --test-dir tests --build-config Debug --output-on-failure
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "extended-precision-integers"]
path = extended-precision-integers
url = https://github.com/mrdcvlsc/extended-precision-integers
46 changes: 38 additions & 8 deletions ChaCha20-Poly1305.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef CHACHA20_CPP_mrdcvlsc
#define CHACHA20_CPP_mrdcvlsc
#include <cstring>
#include <iostream>

#include "extended-precision-integers/include/epi/epi.hpp"

#ifdef _MAKE_LIB
#include "ChaCha20-Poly1305.hpp"
#endif
Expand Down Expand Up @@ -206,18 +209,40 @@ namespace poly1305 {
memcpy(unclamped_r,key,HALF_KEY_BYTES);
clamp(unclamped_r);

uint320 r(unclamped_r,HALF_KEY_BYTES),
s(key+HALF_KEY_BYTES,HALF_KEY_BYTES),
a(0),
p(0,0,0x3, 0xffffffffffffffff, 0xfffffffffffffffb);
epi::uint320_t r, s, a = 0;
constexpr epi::uint320_t p("0x3fffffffffffffffffffffffffffffffb");

memcpy(&r, unclamped_r, HALF_KEY_BYTES);
memcpy(&s, key+HALF_KEY_BYTES, HALF_KEY_BYTES);
memcpy(&r, unclamped_r, HALF_KEY_BYTES);

std::cout << std::dec << "r = " << std::hex << r << '\n';
std::cout << std::dec << "s = " << std::hex << s << '\n';
std::cout << std::dec << "a = " << std::hex << a << '\n';
std::cout << std::dec << "p = " << std::hex << p << '\n';

// uint320 r(unclamped_r,HALF_KEY_BYTES),
// s(key+HALF_KEY_BYTES,HALF_KEY_BYTES),
// a(0),
// p(0,0,0x3, 0xffffffffffffffff, 0xfffffffffffffffb);

size_t blocks = msg_len/HALF_KEY_BYTES;
size_t remain = msg_len%HALF_KEY_BYTES;

// 16 byte blocks
for(size_t i=0; i<blocks; ++i) {
uint320 n(msg+(i*HALF_KEY_BYTES),HALF_KEY_BYTES);
n.limbs[2] |= 0x01;

epi::uint320_t n;
memcpy(&n, msg+(i*HALF_KEY_BYTES), HALF_KEY_BYTES);
// uint320 n(msg+(i*HALF_KEY_BYTES),HALF_KEY_BYTES);

std::cout << std::dec << "n = " << std::hex << n << '\n';

constexpr epi::uint320_t mask_n("0x100000000000000000000000000000000");
n |= mask_n;
// n.limbs[2] |= 0x01;

std::cout << std::dec << "n | " << std::hex << n << '\n';

a += n;
a = a * r;
Expand All @@ -231,7 +256,12 @@ namespace poly1305 {
memset(last_block+remain+1,0x00,(HALF_KEY_BYTES-remain)-1);
last_block[remain] = 0x01;

uint320 n(last_block,HALF_KEY_BYTES);
epi::uint320_t n;
memcpy(&n, last_block, HALF_KEY_BYTES);

// uint320 n(last_block,HALF_KEY_BYTES);

std::cout << std::dec << "n L " << std::hex << n << '\n';

a += n;
a = a * r;
Expand All @@ -240,7 +270,7 @@ namespace poly1305 {

a += s;

memcpy(output,(unsigned char*)a.limbs,UINT128BYTES);
memcpy(output,(unsigned char*) &a, 16);
}

int verify(const unsigned char *tag1, const unsigned char *tag2) {
Expand Down
116 changes: 1 addition & 115 deletions ChaCha20-Poly1305.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,121 +7,7 @@
#include <bitset>
#include <utility>

//============================== uint320 ==============================

#define UINT128BITS 128
#define UINT128BITS_2x 256
#define UINT127BITS 127
#define UINT128BYTES 16

#if (__x86_64__ || __ia64__ ||__amd__64__)
#define ULONGBITS 64
#define ULONGBITS_2x 128
#else
#define ULONGBITS 32
#define ULONGBITS_2x 64
#endif

// -----------------------------

#define UINT64BYTES 8
#define UINT64BITS 64

#define UINT320LIMBS 5
#define UINT320LIMBS_MINUS_1 4
#define UINT320BYTES 40
#define UINT319BITS 319
#define UINT320BITS 320
#define UINT320BITS_x2 640

/// uint320 least significant limb index.
#define UINT320_LS_LIMB 0

/// uint320 most significant limb index.
#define UINT320_MS_LIMB 4

#define LESS -1
#define EQUAL 0
#define GREAT 1

#if (__MINGW64__ || __MINGW64)
typedef unsigned long long ulongint;
#define PRINT_LIMBHEX "%016llx "
#elif (__clang__ || __GNUC__ || __GNUG__)
typedef unsigned long ulongint;
#define PRINT_LIMBHEX "%016lx "
#else
#define PRINT_LIMBHEX "%016lx "
#define _PURE_CPP
#endif

/// for intel & amd x86_64 & x64 architectures only.
class uint320 {
public:

/**least significant quadword starting from index 0
* up to index 4 the most significant quadword.*/
ulongint limbs[UINT320LIMBS];

uint320(ulongint num);
uint320(const unsigned char *input_bytes, size_t bytes);
uint320(
ulongint n4, ulongint n3, ulongint n2, ulongint n1, ulongint n0
);

/// copy constructor.
uint320(const uint320& src);

/// move constructor.
// uint320(uint320&& src) noexcept;

/// copy assignment.
uint320& operator=(const uint320& src);

/// move assignment
// uint320& uint320::operator=(uint320&& src) noexcept;

~uint320();

/// @return returns; 0 : if uint320 == 0, 1 : if uint320 == 1, and -1 : if uint320 != to 0 or 1.
int one_or_zero() const;

/// @return returns; -1 : if less than, 0 : if equal, 1 : if greater than.
int compare(const uint320& with) const;

bool operator==(const uint320& with) const;
bool operator!=(const uint320& with) const;
bool operator<(const uint320& with) const;
bool operator>=(const uint320& with) const;

uint320 operator+(const uint320& add) const;
uint320& operator+=(const uint320& add);

uint320 operator-(const uint320& sub) const;
uint320& operator-=(const uint320& sub);

/// This is the ugly part.
uint320 operator*(const uint320& mr) const;

/** long division using bits, shifts and subtract. */
uint320 ss_mod(const uint320& divisor) const;

uint320 operator%(const uint320& divisor) const;

// LEFT SHIFT
uint320 operator<<(size_t lshift) const;

// RIGHT SHIFT
uint320 operator>>(size_t rshift) const;

/// the limb[7] will be printed first then 6,5, ..., the limb[0] will be printed last.
void printHex() const;

/// the limb[7] will be printed first then 6,5, ..., the limb[0] will be printed last.
void printBits() const;
};

//=====================================================================
#include "extended-precision-integers/include/epi/epi.hpp"

/// Number of bytes(unsigned char) inside a ChaCha20 State.
#define CHACHA20_STATE_BYTES 64
Expand Down
2 changes: 0 additions & 2 deletions Header-Mode-ChaCha20-Poly1305.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <cstdint>

#include "ChaCha20-Poly1305.hpp"

#include "uint320.cpp"
#include "ChaCha20-Poly1305.cpp"

#endif
26 changes: 9 additions & 17 deletions aarch64test
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CC := g++
TESTFLAGS := -g -Og -D_HIDE_WARNING
CXXFLAGS := -std=c++11 -Wall -Wextra
CXX:=g++
TESTFLAGS:=-g -Og -D_HIDE_WARNING
CXXFLAGS:=-std=c++20 -Wall -Wextra

OS := $(shell uname)
OS:=$(shell uname)

SRC := tests
SRC_FILES := $(wildcard $(SRC)/*.cpp)
OBJ := $(patsubst $(SRC)/%.cpp,$(SRC)/%.out,$(SRC_FILES))
SRC:=tests
SRC_FILES:=$(wildcard $(SRC)/*.cpp)
OBJ:=$(patsubst $(SRC)/%.cpp,$(SRC)/%.out,$(SRC_FILES))

# -------------------------- run test programs ---------------------------

Expand All @@ -17,14 +17,6 @@ header_test: $(OBJ)
@./$(SRC)/QuarterRound_test.out
@./$(SRC)/BlockFunction_test.out
@./$(SRC)/Encryption_test.out
@./$(SRC)/constructor.out
@./$(SRC)/comparison.out
@./$(SRC)/leftshifts.out
@./$(SRC)/rightshifts.out
@./$(SRC)/addition.out
@./$(SRC)/subtraction.out
@./$(SRC)/multiplication.out
@./$(SRC)/division.out
@./$(SRC)/poly1305_mac_test.out
@./$(SRC)/poly1305_keygen.out
@./$(SRC)/chacha20_aead_enc_dec.out
Expand Down Expand Up @@ -58,8 +50,8 @@ header_test: $(OBJ)
# -------------------------- test program compilation ---------------------------

$(SRC)/%.out: $(SRC)/%.cpp
@echo "compiling test program - compiler : $(CC)"
@$(CC) $(TESTFLAGS) $(CXXFLAGS) -o $@ $<
@echo "compiling test program - compiler : $(CXX)"
@$(CXX) $(TESTFLAGS) $(CXXFLAGS) -o $@ $<

clean:
ifeq ($(OS), Linux)
Expand Down
1 change: 1 addition & 0 deletions extended-precision-integers
Loading

0 comments on commit b37c698

Please sign in to comment.