Split out testing utils to separate file

This commit is contained in:
Ben Edgington 2021-02-02 11:18:43 +00:00
parent 32340d46a1
commit d0af9ce85e
6 changed files with 75 additions and 39 deletions

View File

@ -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

View File

@ -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);

View File

@ -14,10 +14,10 @@
* limitations under the License.
*/
#include "c-kzg.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#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);

View File

@ -15,6 +15,7 @@
*/
#include "inc/acutest.h"
#include "test_util.h"
#include "fft_fr.h"
#define NUM_ROOTS 32

View File

@ -14,17 +14,48 @@
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#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;
}

31
test_util.h Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#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);