From d0af9ce85e53c4ba81896c8c4fab41cf363c4bcd Mon Sep 17 00:00:00 2001 From: Ben Edgington Date: Tue, 2 Feb 2021 11:18:43 +0000 Subject: [PATCH] Split out testing utils to separate file --- Makefile | 14 ++++++++------ fft_fr.c | 19 ------------------- fft_fr.h | 5 +---- fft_fr_test.c | 1 + util.c => test_util.c | 44 +++++++++++++++++++++++++++++++++---------- test_util.h | 31 ++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 39 deletions(-) rename util.c => test_util.c (70%) create mode 100644 test_util.h diff --git a/Makefile b/Makefile index 8920761..790253d 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,16 @@ tests = fft_fr_test -fft_fr.o: fft_fr.c fft_fr.h c-kzg.h - clang -Wall -c fft_fr.c +.PRECIOUS: %.o -%_test: %.o %_test.c - @clang -Wall -o $@ $@.c $*.o -Llib -lblst - @./$@ +%.o: %.c %.h c-kzg.h + clang -Wall -c $*.c + +%_test: %.o %_test.c test_util.o + clang -Wall -o $@ $@.c test_util.o $*.o -Llib -lblst + ./$@ test: $(tests) - @rm -f $(tests) + rm -f $(tests) clean: rm -f *.o diff --git a/fft_fr.c b/fft_fr.c index cad44da..8b1ef0b 100644 --- a/fft_fr.c +++ b/fft_fr.c @@ -16,18 +16,6 @@ #include "fft_fr.h" -void print_bytes_as_hex_le(byte *bytes, int start, int len) { - for (int i = start + len - 1; i >= start; i--) { - printf("%02x", bytes[i]); - } -} - -void print_fr(const blst_fr *a) { - blst_scalar b; - blst_scalar_from_fr(&b, a); - print_bytes_as_hex_le(b.b, 0, 32); -} - bool is_one(const blst_fr *fr_p) { uint64_t a[4]; blst_uint64_from_fr(a, fr_p); @@ -38,13 +26,6 @@ bool is_power_of_two(uint64_t n) { return (n & (n - 1)) == 0; } -bool fr_equal(blst_fr *aa, blst_fr *bb) { - uint64_t a[4], b[4]; - blst_uint64_from_fr(a, aa); - blst_uint64_from_fr(b, bb); - return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]; -} - void fr_from_uint64(blst_fr *a, uint64_t n) { uint64_t vals[] = {n, 0, 0, 0}; blst_fr_from_uint64(a, vals); diff --git a/fft_fr.h b/fft_fr.h index af362f9..4b1a9f2 100644 --- a/fft_fr.h +++ b/fft_fr.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "c-kzg.h" #include #include #include +#include "c-kzg.h" // This is 1 in Blst's `blst_fr` limb representation. Crazy but true. static const blst_fr one = @@ -71,11 +71,8 @@ typedef struct { blst_fr *reverse_roots_of_unity; } FFTSettings; -void print_bytes_as_hex_le(byte *bytes, int start, int len); -void print_fr(const blst_fr *a); bool is_one(const blst_fr *fr_p); bool is_power_of_two(uint64_t n); -bool fr_equal(blst_fr *aa, blst_fr *bb); void fr_from_uint64(blst_fr *a, uint64_t n); blst_fr *expand_root_of_unity(blst_fr *root_of_unity, uint64_t width); blst_fr *reverse(blst_fr *r, uint64_t width); diff --git a/fft_fr_test.c b/fft_fr_test.c index 34b6871..8eea6a5 100644 --- a/fft_fr_test.c +++ b/fft_fr_test.c @@ -15,6 +15,7 @@ */ #include "inc/acutest.h" +#include "test_util.h" #include "fft_fr.h" #define NUM_ROOTS 32 diff --git a/util.c b/test_util.c similarity index 70% rename from util.c rename to test_util.c index 0cbfa9d..1bb6b83 100644 --- a/util.c +++ b/test_util.c @@ -14,17 +14,48 @@ * limitations under the License. */ -#include -#include +#include "test_util.h" -#include "inc/blst.h" +// +// General Utilities +// +// Big-endian void print_bytes_as_hex(byte *bytes, int start, int len) { for (int i = start; i < start + len; i++) { printf("%02x", bytes[i]); } } +// Little-endian +void print_bytes_as_hex_le(byte *bytes, int start, int len) { + for (int i = start + len - 1; i >= start; i--) { + printf("%02x", bytes[i]); + } +} + +// +// Fr utilities +// + +// Print a `blst_fr` +void print_fr(const blst_fr *a) { + blst_scalar b; + blst_scalar_from_fr(&b, a); + print_bytes_as_hex_le(b.b, 0, 32); +} + +bool fr_equal(blst_fr *aa, blst_fr *bb) { + uint64_t a[4], b[4]; + blst_uint64_from_fr(a, aa); + blst_uint64_from_fr(b, bb); + return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]; +} + +// +// G1 and G2 utilities +// + /* "Pretty" print an affine point in G1 */ void print_p1_affine(const blst_p1_affine *p1) { byte *p1_hex = (byte *)malloc(96); @@ -52,10 +83,3 @@ void print_p2_affine(const blst_p2_affine *p2) { printf(")]\n"); free(p2_hex); } - - -int main() { - print_p1_affine(blst_p1_affine_generator()); - print_p2_affine(blst_p2_affine_generator()); - return 0; -} diff --git a/test_util.h b/test_util.h new file mode 100644 index 0000000..6c5d853 --- /dev/null +++ b/test_util.h @@ -0,0 +1,31 @@ +/* + * Copyright 2021 Benjamin Edgington + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "inc/blst.h" + +// General Utilities +void print_bytes_as_hex(byte *bytes, int start, int len); +void print_bytes_as_hex_le(byte *bytes, int start, int len); + +// Fr utilities +void print_fr(const blst_fr *a); +bool fr_equal(blst_fr *aa, blst_fr *bb); + +// G1 and G2 utilities +void print_p1_affine(const blst_p1_affine *p1); +void print_p2_affine(const blst_p2_affine *p2);