Skip to content

Commit

Permalink
play with gcc support
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jul 1, 2024
1 parent 4a32588 commit 1e2d554
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- if: matrix.os == 'ubuntu-latest'
run: sudo apt update && sudo apt install -y clang

- run: cc -v
- run: clang -v
- run: make build
- run: ./ecloop -v
- run: ./ecloop add -f data/btc-puzzles-hash -r 8000:ffff -q -o /dev/null
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
internal/
misc/
*.out
ecloop
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CC_FLAGS ?= -O3

ifeq ($(shell uname -m),x86_64)
CC_FLAGS += -msse4.1 -msha
CC_FLAGS += -march=native
endif

default: build
Expand All @@ -10,15 +10,18 @@ clean:
@rm -rf ecloop bench main a.out *.profraw *.profdata

build: clean
@$(CC) $(CC_FLAGS) main.c -o ecloop
@# @$(CC) $(CC_FLAGS) main.c -o ecloop
@clang $(CC_FLAGS) main.c -o ecloop

# -----------------------------------------------------------------------------

add: build
./ecloop add -f data/btc-puzzles-hash -t 8 -r 8000:ffffff
./ecloop add -f data/btc-puzzles-hash -t 4 -r 8000:ffffff

mul: build
cat data.txt | ./ecloop mul -f _check_1.txt -t 8 -a cu
cat data.txt | ./ecloop mul -f _check_1.txt -t 4 -a cu

blf-test: build
blf: build
@rm -rf /tmp/test.blf
cat data/btc-puzzles-hash | ./ecloop blf-gen -n 1024 -o /tmp/test.blf
./ecloop add -f /tmp/test.blf -t 8 -r 8000:ffffff
29 changes: 14 additions & 15 deletions lib/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "util.c"

void print_res(char *label, u64 stime, u64 iters) {
double dt = (tsnow() - stime) / 1000.0;
double dt = MAX((tsnow() - stime), 1) / 1000.0;
printf("%20s: %.2fM it/s ~ %.2fs\n", label, iters / dt / 1000000, dt);
}

Expand All @@ -17,8 +17,7 @@ void fe_rand(fe r) {
void run_bench() {
ec_gtable_init();

u64 stime;
u64 iters;
u64 stime, iters, i;
pe g;
fe f;

Expand All @@ -27,22 +26,22 @@ void run_bench() {

stime = tsnow();
pe_clone(&g, &G2);
for (u64 i = 0; i < iters; ++i) _ec_jacobi_add1(&g, &g, &G1);
for (i = 0; i < iters; ++i) _ec_jacobi_add1(&g, &g, &G1);
print_res("_ec_jacobi_add1", stime, iters);

pe_clone(&g, &G2);
stime = tsnow();
for (u64 i = 0; i < iters; ++i) _ec_jacobi_add2(&g, &g, &G1);
for (i = 0; i < iters; ++i) _ec_jacobi_add2(&g, &g, &G1);
print_res("_ec_jacobi_add2", stime, iters);

pe_clone(&g, &G2);
stime = tsnow();
for (u64 i = 0; i < iters; ++i) _ec_jacobi_dbl1(&g, &g);
for (i = 0; i < iters; ++i) _ec_jacobi_dbl1(&g, &g);
print_res("_ec_jacobi_dbl1", stime, iters);

pe_clone(&g, &G2);
stime = tsnow();
for (u64 i = 0; i < iters; ++i) _ec_jacobi_dbl2(&g, &g);
for (i = 0; i < iters; ++i) _ec_jacobi_dbl2(&g, &g);
print_res("_ec_jacobi_dbl2", stime, iters);

// ec multiplication
Expand All @@ -54,48 +53,48 @@ void run_bench() {

iters = 1000 * 10;
stime = tsnow();
for (u64 i = 0; i < iters; ++i) ec_jacobi_mul(&g, &G1, numbers[i % numSize]);
for (i = 0; i < iters; ++i) ec_jacobi_mul(&g, &G1, numbers[i % numSize]);
print_res("ec_jacobi_mul", stime, iters);

iters = 1000 * 500;
stime = tsnow();
for (u64 i = 0; i < iters; ++i) ec_gtable_mul(&g, numbers[i % numSize]);
for (i = 0; i < iters; ++i) ec_gtable_mul(&g, numbers[i % numSize]);
print_res("ec_gtable_mul", stime, iters);

// affine coordinates
iters = 1000 * 500;

pe_clone(&g, &G2);
stime = tsnow();
for (u64 i = 0; i < iters; ++i) ec_affine_add(&g, &g, &G1);
for (i = 0; i < iters; ++i) ec_affine_add(&g, &g, &G1);
print_res("ec_affine_add", stime, iters);

pe_clone(&g, &G2);
stime = tsnow();
for (u64 i = 0; i < iters; ++i) ec_affine_dbl(&g, &g);
for (i = 0; i < iters; ++i) ec_affine_dbl(&g, &g);
print_res("ec_affine_dbl", stime, iters);

// modular inversion
iters = 1000 * 100;

stime = tsnow();
for (u64 i = 0; i < iters; ++i) _fe_modinv_binpow(f, g.x);
for (i = 0; i < iters; ++i) _fe_modinv_binpow(f, g.x);
print_res("_fe_modinv_binpow", stime, iters);

stime = tsnow();
for (u64 i = 0; i < iters; ++i) _fe_modinv_addchn(f, g.x);
for (i = 0; i < iters; ++i) _fe_modinv_addchn(f, g.x);
print_res("_fe_modinv_addchn", stime, iters);

// hash functions
iters = 1000 * 1000 * 5;
h160_t h160;

stime = tsnow();
for (u64 i = 0; i < iters; ++i) addr33(h160, &g);
for (i = 0; i < iters; ++i) addr33(h160, &g);
print_res("addr33", stime, iters);

stime = tsnow();
for (u64 i = 0; i < iters; ++i) addr65(h160, &g);
for (i = 0; i < iters; ++i) addr65(h160, &g);
print_res("addr65", stime, iters);
}

Expand Down
15 changes: 11 additions & 4 deletions lib/rmd160.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
typedef unsigned int u32;
typedef unsigned char u8;

// #define swap32(x) ((x) << 24) | ((x) << 8 & 0x00ff0000) | ((x) >> 8 & 0x0000ff00) | ((x) >> 24)
// #define rotl32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define swap32(x) __builtin_bswap32(x)
#define rotl32(x, n) __builtin_rotateleft32(x, n)
#if __has_builtin(__builtin_rotateleft32)
#define rotl32(x, n) __builtin_rotateleft32(x, n)
#else
#define rotl32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#endif

#if __has_builtin(__builtin_bswap32)
#define swap32(x) __builtin_bswap32(x)
#else
#define swap32(x) ((x) << 24) | ((x) << 8 & 0x00ff0000) | ((x) >> 8 & 0x0000ff00) | ((x) >> 24)
#endif

static const u8 _n[80] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, //
Expand Down
2 changes: 1 addition & 1 deletion lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string.h>
#include <time.h>

size_t tsnow() {
unsigned long long tsnow() {
struct timespec ts;
// clock_gettime(CLOCK_MONOTONIC, &ts);
clock_gettime(CLOCK_REALTIME, &ts);
Expand Down
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ int cmd_add(ctx_t *ctx) {
}

ctx_print_status(ctx);
printf("\n");
return 0;
}

Expand Down Expand Up @@ -670,6 +671,7 @@ int cmd_mul(ctx_t *ctx) {
}

ctx_print_status(ctx);
printf("\n");
return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ A high-performance, CPU-optimized tool for computing public keys on the secp256k

```sh
git clone https://github.com/vladkens/ecloop.git && cd ecloop
make
make build
```

Note: Build has been tested with `clang`. It may work with `gcc14`, but this has not been thoroughly tested. If anyone knows how to get this to work with gcc14 or earlier – I'd be happy to get a PR.

## Usage

```text
Expand Down

0 comments on commit 1e2d554

Please sign in to comment.