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
94 changes: 94 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: CI/CD Pipeline

on:
pull_request:
branches: [ "main" ]
push:
branches: [ "main" ]

jobs:
# JOB 1: Build and test matrix
# Runs on PRs and pushes to main across OS's
build-and-test:
name: Build & Test (${{ matrix.config.name }} - ${{ matrix.build-configuration }})
runs-on: ${{ matrix.config.os }}
strategy:
fail-fast: false
matrix:
config:
- { name: "Windows gcc", os: windows-latest, cc: "gcc", cxx: "g++" }
- { name: "Ubuntu gcc", os: ubuntu-latest, cc: "gcc", cxx: "g++" }
- { name: "MacOS clang", os: macos-latest, cc: "clang", cxx: "clang++" }
build-configuration: [ Debug, Release ]

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true

- name: Setup Ninja
uses: ashutoshvarma/setup-ninja@master

- name: Configure CMake
run: |
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.build-configuration }} -DCMAKE_C_COMPILER=${{ matrix.config.cc }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cxx }}

- name: Build Project
run: cmake --build build --config ${{ matrix.build-configuration }}

- name: Run Catch2 Unit Tests
# Runs the unit tests you specified
run: ctest --test-dir build -C ${{ matrix.build-configuration }} --output-on-failure

# We only package and upload 'Release' builds to save storage and time
- name: Compress Release Build Directory
if: matrix.build-configuration == 'Release'
uses: thedoctor0/zip-release@0.7.5
with:
type: 'zip'
path: 'build'
filename: '${{ matrix.config.os }}-build.zip'

- name: Upload Artifacts for Release
if: matrix.build-configuration == 'Release'
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.config.os }}-build
path: ${{ matrix.config.os }}-build.zip
retention-days: 1


# JOB 2: Create Pre-Release
# Runs only on pushes to main, after tests pass
release:
name: Create Pre-Release
needs: build-and-test
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Download all OS Artifacts
uses: actions/download-artifact@v4
with:
path: ./release-artifacts
merge-multiple: true # Puts the Mac, Win, and Linux zip files in the same folder

- name: Generate Tag & Publish Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create naming convention
DATE=$(date +'%Y%m%d')
SHORT_SHA=${GITHUB_SHA::8}
TAG_NAME="dev-${DATE}-${SHORT_SHA}"

# Create Pre-Release and attach all downloaded .zip files using modern GitHub CLI
gh release create "$TAG_NAME" ./release-artifacts/*.zip \
--title "Development Build $TAG_NAME" \
--prerelease \
--generate-notes
34 changes: 34 additions & 0 deletions .github/workflows/clang-format-dispatch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow should only be run by administrators

name: ClangFormat correction

on:
workflow_dispatch:

jobs:
ClangFormat:
name: ClangFormat correction
runs-on: ubuntu-22.04

# Give the default GITHUB_TOKEN write permission to commit and push the changed files back to the repository.
permissions:
contents: write

steps:
# Clone Repo
- name: Checkout
uses: actions/checkout@v4

- name: Install clang-format
run: sudo apt-get update && sudo apt-get install -y clang-format

- name: Format code
#run: clang-format -i **/*.cpp
run: find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) -exec clang-format -i {} \;

# Commit all changed files back to the repository
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 'Apply Clang formatting'
create_branch: false
34 changes: 34 additions & 0 deletions .github/workflows/clang-format-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow is from https://github.com/official-stockfish/Stockfish/blob/master/.github/workflows/clang-format.yml#L23

# This workflow will run clang-format and comment on the PR.
# Because of security reasons, it is crucial that this workflow
# executes no shell script nor runs make.
# Read this before editing: https://securitylab.github.com/research/github-actions-preventing-pwn-requests/

name: ClangFormat check
on:
pull_request_target:
branches:
- "main"
paths:
- "**.cpp"
- "**.h"

# needed for automatic comment in pr
permissions:
pull-requests: write

jobs:
Clang-Format:
name: Clang-Format
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Run clang-format style check
uses: jidicula/clang-format-action@v4.15.0
id: clang-format
with:
clang-format-version: "20"
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.28.3)
project(Base256)
project(BigInt)

# Enforce C++20 and disable compiler-specific extensions
set(CMAKE_CXX_STANDARD 20)
Expand Down
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Base256
# BigInt

Base256 is a small C++20 library for unsigned big-integer arithmetic using
Base-256 byte storage. It was developed for use in
BigInt is a small C++20 library for unsigned big-integer arithmetic using
64-bit limb storage. It was developed for use in
[RSA-Encryptor](https://github.com/ParallelEngineering/RSA-Encryptor) and is
intended to be integrated as a Git submodule.

Expand All @@ -11,61 +11,63 @@ The library provides arithmetic for numbers that can grow beyond native integer
limits such as `uint64_t`. This makes it useful for cryptographic-style
calculations where values often need more than the CPU's built-in integer width.

## How Base256 works
## How BigInt works

`operations::Base256` stores a number as a dynamically sized `ByteArray`
(`std::vector<uint8_t>`). Each byte is one Base-256 digit:
`operations::BigInt` stores a number as a dynamically sized `ByteArray`
(`std::vector<uint64_t>`). Each limb is one base-2^64 digit:

- index `0` contains the least-significant byte
- index `1` contains the next `256^1` byte
- index `0` contains the least-significant limb
- index `1` contains the next `(2^64)^1` limb
- higher indexes continue the same little-endian layout

After arithmetic operations, the internal byte array is normalized by removing
unused high zero-bytes while keeping zero represented as a single byte.
After arithmetic operations, the internal limb array is normalized by removing
unused high zero-limbs while keeping zero represented as a single limb.

For a deeper explanation of the internal representation, see
[`docs/base256.md`](docs/base256.md).
[`docs/bigint.md`](docs/bigint.md).

## Usage

```cpp
#include "base256.h"
#include "bigint.h"
#include "math_utils.h"

using operations::Base256;
using operations::BigInt;
using operations::math::pow;

int main() {
Base256 a(4294967295ULL);
Base256 b(2);
BigInt a(4294967295ULL);
BigInt b(2);

Base256 sum = a + b;
Base256 product = a * b;
Base256 remainder = product % b;
Base256 power = Base256::pow(b, 16);
BigInt sum = a + b;
BigInt product = a * b;
BigInt remainder = product % b;
BigInt power = pow(b, 16);

power.print();
}
```

The class supports construction from `uint64_t`, arithmetic operators
`+`, `-`, `*`, `/`, `%`, compound assignments, comparisons, `Base256::pow`, and
`+`, `-`, `*`, `/`, `%`, compound assignments, comparisons, `operations::math::pow`, and
decimal output with `print()`.

## Integration

Add this repository to your parent project and include it with CMake:

```cmake
add_subdirectory(lib/Base256)
target_link_libraries(YourTarget PRIVATE Base256)
add_subdirectory(lib/BigInt)
target_link_libraries(YourTarget PRIVATE BigInt)
```

If your parent project does not already expose the library headers, add the
submodule's `src` directory to your include path.

> [!NOTE]
> Base256 is a library, not a standalone executable. The library code itself is
> BigInt is a library, not a standalone executable. The library code itself is
> meant to be used from another program. The tests, however, can be built and run
> directly from this repository without integrating Base256 into another project.
> directly from this repository without integrating BigInt into another project.

## Tests

Expand All @@ -76,5 +78,3 @@ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build
ctest --test-dir build --output-on-failure
```


74 changes: 0 additions & 74 deletions docs/base256.md

This file was deleted.

Loading
Loading