Skip to content

Commit

Permalink
Add FK20 single proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminion committed Feb 10, 2021
1 parent 8986f1c commit f476a0f
Show file tree
Hide file tree
Showing 23 changed files with 660 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.a
*_test
*_bench
*_debug
*.prof
*.out
*.log
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Done so far:
- FFTs over the G1 group
- Polynomial single commitment and verification
- Polynomial multi commitment and verification
- [FK20](https://github.com/khovratovich/Kate/blob/master/Kate_amortized.pdf) single proof method (needs a tidy up)

## Installation

Expand Down Expand Up @@ -69,8 +70,20 @@ make fft_fr_bench

Doing `make clean` should resolve any weird build issues.

## Make debug builds of the tests

The default build is designed not to exit on errors, and will (should) return fairly coarse error codes for any issue. This is good for a utility library, but unhelpful for debugging. The `-DDEBUG` compiler flag builds a version such that any assertion failure aborts the run and outputs file and line info. This is much more useful for tracking down deeply buried errors.

Each test suite can be compiled into its debug version. For example, as follows:

```
make fk20_proofs_test_debug
./fk20_proofs_test_debug fk_single_strided
```

## Prerequisites

- Blst library (see above)
- `clang` compiler. I'm using Clang 10.0.0. I'll likely add `gcc` options in future.
- The Makefile is GNU make compatible.
- I'm developing on Ubuntu 20.04. Will check portability later.
25 changes: 14 additions & 11 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
TESTS = blst_util_test c_kzg_util_test fft_common_test fft_fr_test fft_g1_test \
kzg_proofs_test poly_test
fk20_proofs_test kzg_proofs_test poly_test
BENCH = fft_fr_bench fft_g1_bench
LIB_SRC = blst_util.c c_kzg_util.c fft_common.c fft_fr.c fft_g1.c kzg_proofs.c poly.c
LIB_SRC = blst_util.c c_kzg_util.c fft_common.c fft_fr.c fft_g1.c fk20_proofs.c kzg_proofs.c poly.c
LIB_OBJ = $(LIB_SRC:.c=.o)

CFLAGS = -g
CFLAGS =

.PRECIOUS: %.o

Expand All @@ -14,22 +14,24 @@ CFLAGS = -g
libckzg.a: $(LIB_OBJ) Makefile
ar rc libckzg.a $(LIB_OBJ)

%_test: %_test.c debug_util.o libckzg.a Makefile
clang -Wall $(CFLAGS) -o $@ $@.c debug_util.o libckzg.a -L../lib -lblst
%_test: %_test.c debug_util.o test_util.o libckzg.a Makefile
clang -Wall $(CFLAGS) -o $@ $@.c debug_util.o test_util.o libckzg.a -L../lib -lblst
./$@

# This version will abort on error and print the file and line number
%_test_debug: CFLAGS += -g -O0 -DDEBUG
%_test_debug: %_test.c debug_util.o test_util.o libckzg.a Makefile
clang -Wall $(CFLAGS) -o $@ $*_test.c debug_util.o test_util.o libckzg.a -L../lib -lblst

# Benchmarks
%_bench: CFLAGS += -O3
%_bench: %_bench.c bench_util.o $(LIB_OBJ) Makefile
clang -Wall $(CFLAGS) -o $@ $@.c bench_util.o $(LIB_OBJ) -L../lib -lblst
./$@

lib: CFLAGS += -O3
lib: clean libckzg.a

debuglib: CFLAGS += -O1 -DDEBUG
debuglib: clean libckzg.a

optlib: CFLAGS += -O2
optlib: clean libckzg.a

profilelib: CFLAGS += -fprofile-instr-generate -fcoverage-mapping
profilelib: clean libckzg.a

Expand All @@ -42,4 +44,5 @@ clean:
rm -f libckzg.a
rm -f $(TESTS)
rm -f $(BENCH)
rm -f *_debug
rm -f a.out
3 changes: 1 addition & 2 deletions src/blst_util_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

#include "../inc/acutest.h"
#include "debug_util.h"
#include "test_util.h"
#include "blst_util.h"

// This is -1 (the second root of unity)
uint64_t m1[] = {0xffffffff00000000L, 0x53bda402fffe5bfeL, 0x3339d80809a1d805L, 0x73eda753299d7d48L};

void title(void) {}

void fr_is_zero_works(void) {
blst_fr zero;
fr_from_uint64(&zero, 0);
Expand Down
3 changes: 1 addition & 2 deletions src/c_kzg_util_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

#include "../inc/acutest.h"
#include "debug_util.h"
#include "test_util.h"
#include "c_kzg_util.h"

void title(void) {}

void malloc_works(void) {
int *p;
TEST_CHECK(C_KZG_OK == c_kzg_malloc((void **)&p, 4));
Expand Down
15 changes: 8 additions & 7 deletions src/fft_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,20 @@ C_KZG_RET reverse(blst_fr *out, const blst_fr *roots, const uint64_t width) {
}

C_KZG_RET new_fft_settings(FFTSettings *fs, const unsigned int max_scale) {
C_KZG_RET ret;
fs->max_width = (uint64_t)1 << max_scale;
blst_fr_from_uint64(&fs->root_of_unity, scale2_root_of_unity[max_scale]);

ASSERT(c_kzg_malloc((void **)&fs->expanded_roots_of_unity, (fs->max_width + 1) * sizeof(blst_fr)) == C_KZG_OK,
ASSERT(c_kzg_malloc((void **)&fs->expanded_roots_of_unity,
(fs->max_width + 1) * sizeof *fs->expanded_roots_of_unity) == C_KZG_OK,
C_KZG_MALLOC);
ASSERT(c_kzg_malloc((void **)&fs->reverse_roots_of_unity, (fs->max_width + 1) * sizeof(blst_fr)) == C_KZG_OK,
ASSERT(c_kzg_malloc((void **)&fs->reverse_roots_of_unity,
(fs->max_width + 1) * sizeof *fs->reverse_roots_of_unity) == C_KZG_OK,
C_KZG_MALLOC);

ret = expand_root_of_unity(fs->expanded_roots_of_unity, &fs->root_of_unity, fs->max_width);
if (ret != C_KZG_OK) return ret;
ret = reverse(fs->reverse_roots_of_unity, fs->expanded_roots_of_unity, fs->max_width);
return ret;
ASSERT(expand_root_of_unity(fs->expanded_roots_of_unity, &fs->root_of_unity, fs->max_width) == C_KZG_OK,
C_KZG_ERROR);
ASSERT(reverse(fs->reverse_roots_of_unity, fs->expanded_roots_of_unity, fs->max_width) == C_KZG_OK, C_KZG_ERROR);
return C_KZG_OK;
}

void free_fft_settings(FFTSettings *fs) {
Expand Down
5 changes: 5 additions & 0 deletions src/fft_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

#ifndef FFT_COMMON
#define FFT_COMMON

#include "c_kzg.h"

// MODULUS = 52435875175126190479447740508185965837690552500527637822603658699938581184513
Expand Down Expand Up @@ -67,3 +70,5 @@ C_KZG_RET expand_root_of_unity(blst_fr *roots, const blst_fr *root_of_unity, con
C_KZG_RET reverse(blst_fr *out, const blst_fr *roots, const uint64_t width);
C_KZG_RET new_fft_settings(FFTSettings *s, const unsigned int max_scale);
void free_fft_settings(FFTSettings *s);

#endif
3 changes: 1 addition & 2 deletions src/fft_common_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

#include "../inc/acutest.h"
#include "debug_util.h"
#include "test_util.h"
#include "fft_common.h"
#include "blst_util.h"

#define NUM_ROOTS 32

void title(void) {}

void roots_of_unity_is_the_expected_size(void) {
TEST_CHECK(NUM_ROOTS == sizeof(scale2_root_of_unity) / sizeof(scale2_root_of_unity[0]));
}
Expand Down
6 changes: 4 additions & 2 deletions src/fft_fr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../inc/acutest.h"
#include "debug_util.h"
#include "test_util.h"
#include "fft_fr.h"
#include "blst_util.h"

Expand All @@ -37,8 +38,6 @@ const uint64_t inv_fft_expected[][4] = {
{0xe92ffdda2306c7d4L, 0x54dd2afcd2dfb16bL, 0xf6554603677e87beL, 0x5dbc603bc53c7a39L},
{0xd8cda22e753b3117L, 0x880454ec72238f55L, 0xcaf6199fc14a5353L, 0x197df7c2f05866d4L}};

void title(void) {}

void compare_sft_fft(void) {
// Initialise: ascending values of i (could be anything), and arbitrary size
unsigned int size = 12;
Expand Down Expand Up @@ -126,6 +125,9 @@ void stride_fft(void) {
for (int i = 0; i < width; i++) {
TEST_CHECK(fr_equal(coeffs1 + i, coeffs2 + i));
}

free_fft_settings(&fs1);
free_fft_settings(&fs2);
}

TEST_LIST = {
Expand Down
8 changes: 5 additions & 3 deletions src/fft_g1.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include "blst_util.h"

// Slow Fourier Transform (simple, good for small sizes)
void fft_g1_slow(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uint64_t roots_stride, uint64_t l) {
void fft_g1_slow(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l) {
blst_p1 v, last, jv;
blst_fr r;
for (uint64_t i = 0; i < l; i++) {
Expand All @@ -34,7 +35,8 @@ void fft_g1_slow(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uin
}

// Fast Fourier Transform
void fft_g1_fast(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uint64_t roots_stride, uint64_t l) {
void fft_g1_fast(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l) {
uint64_t half = l / 2;
if (half > 0) { // Tunable parameter
fft_g1_fast(out, in, stride * 2, roots, roots_stride * 2, half);
Expand All @@ -51,7 +53,7 @@ void fft_g1_fast(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uin
}

// The main entry point for forward and reverse FFTs
C_KZG_RET fft_g1(blst_p1 *out, blst_p1 *in, FFTSettings *fs, bool inv, uint64_t n) {
C_KZG_RET fft_g1(blst_p1 *out, const blst_p1 *in, const FFTSettings *fs, bool inv, uint64_t n) {
uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS);
ASSERT(is_power_of_two(n), C_KZG_BADARGS);
Expand Down
8 changes: 5 additions & 3 deletions src/fft_g1.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "c_kzg.h"
#include "fft_common.h"

void fft_g1_slow(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uint64_t roots_stride, uint64_t l);
void fft_g1_fast(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uint64_t roots_stride, uint64_t l);
C_KZG_RET fft_g1(blst_p1 *out, blst_p1 *in, FFTSettings *fs, bool inv, uint64_t n);
void fft_g1_slow(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l);
void fft_g1_fast(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l);
C_KZG_RET fft_g1(blst_p1 *out, const blst_p1 *in, const FFTSettings *fs, bool inv, uint64_t n);
6 changes: 4 additions & 2 deletions src/fft_g1_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

#include "../inc/acutest.h"
#include "debug_util.h"
#include "test_util.h"
#include "fft_g1.h"

void title(void) {}

void make_data(blst_p1 *out, uint64_t n) {
// Multiples of g1_gen
if (n == 0) return;
Expand Down Expand Up @@ -85,6 +84,9 @@ void stride_fft(void) {
for (int i = 0; i < width; i++) {
TEST_CHECK(blst_p1_is_equal(coeffs1 + i, coeffs2 + i));
}

free_fft_settings(&fs1);
free_fft_settings(&fs2);
}

TEST_LIST = {
Expand Down
Loading

0 comments on commit f476a0f

Please sign in to comment.