Apply formatting rules

This commit is contained in:
Ben Edgington 2021-02-07 14:19:25 +00:00
parent 9085893ef7
commit 80c513f653
19 changed files with 186 additions and 178 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@ tmp/
inc/blst.h inc/blst.h
inc/blst_aux.h inc/blst_aux.h
.vscode/ .vscode/
*.json *.json
.clang-format

View File

@ -19,11 +19,10 @@
static const blst_fr fr_zero = {0L, 0L, 0L, 0L}; static const blst_fr fr_zero = {0L, 0L, 0L, 0L};
// This is 1 in Blst's `blst_fr` limb representation. Crazy but true. // This is 1 in Blst's `blst_fr` limb representation. Crazy but true.
static const blst_fr fr_one = static const blst_fr fr_one = {0x00000001fffffffeL, 0x5884b7fa00034802L, 0x998c4fefecbc4ff5L, 0x1824b159acc5056fL};
{0x00000001fffffffeL, 0x5884b7fa00034802L, 0x998c4fefecbc4ff5L, 0x1824b159acc5056fL};
// The G1 identity/infinity in affine representation // The G1 identity/infinity in affine representation
static const blst_p1_affine identity_g1_affine = {{0L,0L,0L,0L,0L,0L},{0L,0L,0L,0L,0L,0L}}; static const blst_p1_affine identity_g1_affine = {{0L, 0L, 0L, 0L, 0L, 0L}, {0L, 0L, 0L, 0L, 0L, 0L}};
bool fr_is_zero(const blst_fr *p); bool fr_is_zero(const blst_fr *p);
bool fr_is_one(const blst_fr *p); bool fr_is_one(const blst_fr *p);

View File

@ -21,7 +21,9 @@
// This is -1 (the second root of unity) // This is -1 (the second root of unity)
uint64_t m1[] = {0xffffffff00000000L, 0x53bda402fffe5bfeL, 0x3339d80809a1d805L, 0x73eda753299d7d48L}; uint64_t m1[] = {0xffffffff00000000L, 0x53bda402fffe5bfeL, 0x3339d80809a1d805L, 0x73eda753299d7d48L};
void title(void) {;} void title(void) {
;
}
void fr_is_zero_works(void) { void fr_is_zero_works(void) {
blst_fr zero; blst_fr zero;
@ -177,21 +179,20 @@ void pairings_work(void) {
TEST_CHECK(false == pairings_verify(&g1_3, &g2_3, &g1_5, &g2_5)); TEST_CHECK(false == pairings_verify(&g1_3, &g2_3, &g1_5, &g2_5));
} }
TEST_LIST = TEST_LIST = {
{ {"BLST_UTIL_TEST", title},
{"BLST_UTIL_TEST", title}, {"fr_is_zero_works", fr_is_zero_works},
{"fr_is_zero_works", fr_is_zero_works }, {"fr_is_one_works", fr_is_one_works},
{"fr_is_one_works", fr_is_one_works }, {"fr_from_uint64_works", fr_from_uint64_works},
{"fr_from_uint64_works", fr_from_uint64_works}, {"fr_equal_works", fr_equal_works},
{"fr_equal_works", fr_equal_works}, {"fr_negate_works", fr_negate_works},
{"fr_negate_works", fr_negate_works}, {"fr_pow_works", fr_pow_works},
{"fr_pow_works", fr_pow_works}, {"p1_mul_works", p1_mul_works},
{"p1_mul_works", p1_mul_works}, {"p1_sub_works", p1_sub_works},
{"p1_sub_works", p1_sub_works}, {"p2_mul_works", p2_mul_works},
{"p2_mul_works", p2_mul_works}, {"p2_sub_works", p2_sub_works},
{"p2_sub_works", p2_sub_works}, {"identity_g1_is_infinity", identity_g1_is_infinity},
{"identity_g1_is_infinity", identity_g1_is_infinity}, {"g1_linear_combination", g1_linear_combination},
{"g1_linear_combination", g1_linear_combination}, {"pairings_work", pairings_work},
{"pairings_work", pairings_work}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};

View File

@ -30,13 +30,14 @@ typedef enum {
#ifdef DEBUG #ifdef DEBUG
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#define ASSERT(cond, ret) if (!(cond)) \ #define ASSERT(cond, ret) \
{ \ if (!(cond)) { \
printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \ printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \
abort(); \ abort(); \
} }
#else #else
#define ASSERT(cond, ret) if (!(cond)) return (ret) #define ASSERT(cond, ret) \
if (!(cond)) return (ret)
#endif #endif
#endif #endif

View File

@ -18,7 +18,9 @@
#include "debug_util.h" #include "debug_util.h"
#include "c_kzg_util.h" #include "c_kzg_util.h"
void title(void) {;} void title(void) {
;
}
void malloc_works(void) { void malloc_works(void) {
int *p; int *p;
@ -30,10 +32,9 @@ void malloc_huge_fails(void) {
TEST_CHECK(C_KZG_MALLOC == c_kzg_malloc((void **)&p, -1)); TEST_CHECK(C_KZG_MALLOC == c_kzg_malloc((void **)&p, -1));
} }
TEST_LIST = TEST_LIST = {
{ {"C_KZG_UTIL_TEST", title},
{"C_KZG_UTIL_TEST", title}, {"malloc_works", malloc_works},
{"malloc_works", malloc_works}, {"malloc_huge_fails", malloc_huge_fails},
{"malloc_huge_fails", malloc_huge_fails}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};

View File

@ -50,8 +50,7 @@ void print_fr(const blst_fr *a) {
// //
void print_limbs(const blst_fp *fp) { void print_limbs(const blst_fp *fp) {
printf("(%08lx, %08lx, %08lx, %08lx, %08lx, %08lx)", printf("(%08lx, %08lx, %08lx, %08lx, %08lx, %08lx)", fp->l[0], fp->l[1], fp->l[2], fp->l[3], fp->l[4], fp->l[5]);
fp->l[0], fp->l[1], fp->l[2], fp->l[3], fp->l[4], fp->l[5]);
} }
// //

View File

@ -41,11 +41,11 @@ C_KZG_RET expand_root_of_unity(blst_fr *roots, const blst_fr *root_of_unity, con
// Create a reversed list of Fr provided // Create a reversed list of Fr provided
// `width` is one less than the length of `roots` // `width` is one less than the length of `roots`
C_KZG_RET reverse(blst_fr *out, const blst_fr *roots, const uint64_t width) { C_KZG_RET reverse(blst_fr *out, const blst_fr *roots, const uint64_t width) {
for (uint64_t i = 0; i <= width; i++) { for (uint64_t i = 0; i <= width; i++) {
out[i] = roots[width - i]; out[i] = roots[width - i];
} }
return C_KZG_OK; return C_KZG_OK;
} }
C_KZG_RET new_fft_settings(FFTSettings *fs, const unsigned int max_scale) { C_KZG_RET new_fft_settings(FFTSettings *fs, const unsigned int max_scale) {
@ -53,8 +53,10 @@ C_KZG_RET new_fft_settings(FFTSettings *fs, const unsigned int max_scale) {
fs->max_width = (uint64_t)1 << max_scale; fs->max_width = (uint64_t)1 << max_scale;
blst_fr_from_uint64(&fs->root_of_unity, scale2_root_of_unity[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, C_KZG_MALLOC); 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->reverse_roots_of_unity, (fs->max_width + 1) * sizeof(blst_fr)) == C_KZG_OK, C_KZG_MALLOC); C_KZG_MALLOC);
ASSERT(c_kzg_malloc((void **)&fs->reverse_roots_of_unity, (fs->max_width + 1) * sizeof(blst_fr)) == C_KZG_OK,
C_KZG_MALLOC);
ret = expand_root_of_unity(fs->expanded_roots_of_unity, &fs->root_of_unity, fs->max_width); ret = expand_root_of_unity(fs->expanded_roots_of_unity, &fs->root_of_unity, fs->max_width);
if (ret != C_KZG_OK) return ret; if (ret != C_KZG_OK) return ret;

View File

@ -21,41 +21,39 @@
// [pow(PRIMITIVE_ROOT, (MODULUS - 1) // (2**i), MODULUS) for i in range(32)] // [pow(PRIMITIVE_ROOT, (MODULUS - 1) // (2**i), MODULUS) for i in range(32)]
// //
// These are not in `blst_fr` limb format and must be converted via `blst_fr_from_uint64()` // These are not in `blst_fr` limb format and must be converted via `blst_fr_from_uint64()`
static const uint64_t scale2_root_of_unity[][4] = static const uint64_t scale2_root_of_unity[][4] = {
{ {0x0000000000000001L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L},
{0x0000000000000001L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L}, {0xffffffff00000000L, 0x53bda402fffe5bfeL, 0x3339d80809a1d805L, 0x73eda753299d7d48L},
{0xffffffff00000000L, 0x53bda402fffe5bfeL, 0x3339d80809a1d805L, 0x73eda753299d7d48L}, {0x0001000000000000L, 0xec03000276030000L, 0x8d51ccce760304d0L, 0x0000000000000000L},
{0x0001000000000000L, 0xec03000276030000L, 0x8d51ccce760304d0L, 0x0000000000000000L}, {0x8dd702cb688bc087L, 0xa032824078eaa4feL, 0xa733b23a98ca5b22L, 0x3f96405d25a31660L},
{0x8dd702cb688bc087L, 0xa032824078eaa4feL, 0xa733b23a98ca5b22L, 0x3f96405d25a31660L}, {0x95df4b360411fe73L, 0x1ef4e672ebc1e1bbL, 0x6e92a9c460afca4aL, 0x4f2c596e753e4fccL},
{0x95df4b360411fe73L, 0x1ef4e672ebc1e1bbL, 0x6e92a9c460afca4aL, 0x4f2c596e753e4fccL}, {0x9733f3a6ba60eaa6L, 0xbd7fdf9c77487ae7L, 0xd84f8612c8b6cc00L, 0x476fa2fb6162ffabL},
{0x9733f3a6ba60eaa6L, 0xbd7fdf9c77487ae7L, 0xd84f8612c8b6cc00L, 0x476fa2fb6162ffabL}, {0xd2fc5e69ac5db47fL, 0xa12a70a615d0b8e4L, 0x293b1d67bc8de5d9L, 0x0e4840ac57f86f5eL},
{0xd2fc5e69ac5db47fL, 0xa12a70a615d0b8e4L, 0x293b1d67bc8de5d9L, 0x0e4840ac57f86f5eL}, {0xb750da4cab28e208L, 0x501dff643be95635L, 0x8cbe2437f0b4b276L, 0x07d0c802a94a946eL},
{0xb750da4cab28e208L, 0x501dff643be95635L, 0x8cbe2437f0b4b276L, 0x07d0c802a94a946eL}, {0x2cabadec2fe322b8L, 0x752c84f315412560L, 0x32a732ae1a3b0aefL, 0x2e95da59a33dcbf2L},
{0x2cabadec2fe322b8L, 0x752c84f315412560L, 0x32a732ae1a3b0aefL, 0x2e95da59a33dcbf2L}, {0x33811ea1fe0c65f4L, 0x15c1ad4c687f28a2L, 0xecfbede342dee7f4L, 0x1bb466679a5d88b1L},
{0x33811ea1fe0c65f4L, 0x15c1ad4c687f28a2L, 0xecfbede342dee7f4L, 0x1bb466679a5d88b1L}, {0xd58a5af42d010ff9L, 0x79efd6b0570bf109L, 0x3ed6d55a6350721dL, 0x2f27b09858f43cefL},
{0xd58a5af42d010ff9L, 0x79efd6b0570bf109L, 0x3ed6d55a6350721dL, 0x2f27b09858f43cefL}, {0x74a1f6718c130477L, 0xa534af14b61e0abeL, 0xeb674a1a620890d7L, 0x43527a8bca252472L},
{0x74a1f6718c130477L, 0xa534af14b61e0abeL, 0xeb674a1a620890d7L, 0x43527a8bca252472L}, {0x450d9f977ea8ee05L, 0x565af17137d56fc0L, 0xe155cb4893f9e9acL, 0x110cebd0c8e9101bL},
{0x450d9f977ea8ee05L, 0x565af17137d56fc0L, 0xe155cb4893f9e9acL, 0x110cebd0c8e9101bL}, {0x23c9159959a0be92L, 0x87d188ce7a027759L, 0x70491431cab3c3ccL, 0x0ac00eb8b3f7f8daL},
{0x23c9159959a0be92L, 0x87d188ce7a027759L, 0x70491431cab3c3ccL, 0x0ac00eb8b3f7f8daL}, {0x13e96ade69583404L, 0x82c057275306243dL, 0x77e48bf529ca9f2aL, 0x50646ac81fe19595L},
{0x13e96ade69583404L, 0x82c057275306243dL, 0x77e48bf529ca9f2aL, 0x50646ac81fe19595L}, {0xe6a354dda97eccd4L, 0x39929d2e88fbbc57L, 0xa22ba63dd6e7b1c8L, 0x42c22911f5f07f43L},
{0xe6a354dda97eccd4L, 0x39929d2e88fbbc57L, 0xa22ba63dd6e7b1c8L, 0x42c22911f5f07f43L}, {0x137b458acfc35f7aL, 0x0caba63a29c01b06L, 0x0409ee987a02402cL, 0x6709c6cd56aa725bL},
{0x137b458acfc35f7aL, 0x0caba63a29c01b06L, 0x0409ee987a02402cL, 0x6709c6cd56aa725bL}, {0x10251f7d8831e03eL, 0x77d85a937ff858ecL, 0xebe905bd4fb9ac5cL, 0x05deb333f8727901L},
{0x10251f7d8831e03eL, 0x77d85a937ff858ecL, 0xebe905bd4fb9ac5cL, 0x05deb333f8727901L}, {0xbf87b689b9009408L, 0x4f730e7ddd3ccc96L, 0xfd7f05ba4610300cL, 0x5ef5e8db0b8ac903L},
{0xbf87b689b9009408L, 0x4f730e7ddd3ccc96L, 0xfd7f05ba4610300cL, 0x5ef5e8db0b8ac903L}, {0x6499688417cd0c14L, 0xa672867368812f7fL, 0x2e1d9a1922cc3253L, 0x3a689e83aa0a1d80L},
{0x6499688417cd0c14L, 0xa672867368812f7fL, 0x2e1d9a1922cc3253L, 0x3a689e83aa0a1d80L}, {0x20b53cbe41144deaL, 0x870c46fac2f0fcbdL, 0x556c35f6537d6971L, 0x3436287f5f686d91L},
{0x20b53cbe41144deaL, 0x870c46fac2f0fcbdL, 0x556c35f6537d6971L, 0x3436287f5f686d91L}, {0x007e082a436ba2e7L, 0x67c6630f9116e877L, 0x36f8f165fb4460f7L, 0x6eee34d57e7046e0L},
{0x007e082a436ba2e7L, 0x67c6630f9116e877L, 0x36f8f165fb4460f7L, 0x6eee34d57e7046e0L}, {0xc5b670eea53a56d1L, 0x127d1f4253037d7bL, 0x57d4257ea722c2e2L, 0x03ae26a333cbd838L},
{0xc5b670eea53a56d1L, 0x127d1f4253037d7bL, 0x57d4257ea722c2e2L, 0x03ae26a333cbd838L}, {0x1e91484876504cf8L, 0x55bbbf1eb63edd02L, 0xbcdafec84e55aa02L, 0x5145c4cd2dc0beb0L},
{0x1e91484876504cf8L, 0x55bbbf1eb63edd02L, 0xbcdafec84e55aa02L, 0x5145c4cd2dc0beb0L}, {0x5b90153a1ab70e2cL, 0x8deffa3175fb0ab8L, 0xc553ae2346900c95L, 0x1d31dcdc6bd3118cL},
{0x5b90153a1ab70e2cL, 0x8deffa3175fb0ab8L, 0xc553ae2346900c95L, 0x1d31dcdc6bd3118cL}, {0x801c894c59a2e8ebL, 0xbc535c5ce12fc974L, 0x95508d2747d39803L, 0x16d9d3cdac5d094fL},
{0x801c894c59a2e8ebL, 0xbc535c5ce12fc974L, 0x95508d2747d39803L, 0x16d9d3cdac5d094fL}, {0x810fa372cca1d8beL, 0xc67b8c2882e0bfa7L, 0xdbb4edf0e2d35bc2L, 0x712d15805087c995L},
{0x810fa372cca1d8beL, 0xc67b8c2882e0bfa7L, 0xdbb4edf0e2d35bc2L, 0x712d15805087c995L}, {0xeb162203fd88f133L, 0xac96c38ff010ea74L, 0x4307987fe64cfc70L, 0x350fe98d37b7a114L},
{0xeb162203fd88f133L, 0xac96c38ff010ea74L, 0x4307987fe64cfc70L, 0x350fe98d37b7a114L}, {0xaba2f51842f2a254L, 0x4d7f3c3aa71efc0cL, 0x97ae418dd274a80aL, 0x2967385d5e3e7682L},
{0xaba2f51842f2a254L, 0x4d7f3c3aa71efc0cL, 0x97ae418dd274a80aL, 0x2967385d5e3e7682L}, {0x75c55c7b575a0b79L, 0x3ba4a15774a7ded1L, 0xc3974d73a04fccf3L, 0x705aba4f4a939684L},
{0x75c55c7b575a0b79L, 0x3ba4a15774a7ded1L, 0xc3974d73a04fccf3L, 0x705aba4f4a939684L}, {0x8409a9ea14ebb608L, 0xfad0084e66bac611L, 0x04287254811c1dfbL, 0x086d072b23b30c29L},
{0x8409a9ea14ebb608L, 0xfad0084e66bac611L, 0x04287254811c1dfbL, 0x086d072b23b30c29L}, {0xb427c9b367e4756aL, 0xc7537fb902ebc38dL, 0x51de21becd6a205fL, 0x6064ab727923597dL}};
{0xb427c9b367e4756aL, 0xc7537fb902ebc38dL, 0x51de21becd6a205fL, 0x6064ab727923597dL}
};
typedef struct { typedef struct {
uint64_t max_width; uint64_t max_width;
@ -65,7 +63,7 @@ typedef struct {
} FFTSettings; } FFTSettings;
bool is_power_of_two(const uint64_t n); bool is_power_of_two(const uint64_t n);
C_KZG_RET expand_root_of_unity(blst_fr * roots, const blst_fr *root_of_unity, const uint64_t width); C_KZG_RET expand_root_of_unity(blst_fr *roots, const blst_fr *root_of_unity, const uint64_t width);
C_KZG_RET reverse(blst_fr *out, const blst_fr *roots, const uint64_t width); 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); C_KZG_RET new_fft_settings(FFTSettings *s, const unsigned int max_scale);
void free_fft_settings(FFTSettings *s); void free_fft_settings(FFTSettings *s);

View File

@ -21,11 +21,12 @@
#define NUM_ROOTS 32 #define NUM_ROOTS 32
void title(void) {;} void title(void) {
;
}
void roots_of_unity_is_the_expected_size(void) { void roots_of_unity_is_the_expected_size(void) {
TEST_CHECK(NUM_ROOTS == TEST_CHECK(NUM_ROOTS == sizeof(scale2_root_of_unity) / sizeof(scale2_root_of_unity[0]));
sizeof(scale2_root_of_unity) / sizeof(scale2_root_of_unity[0]));
} }
void roots_of_unity_are_plausible(void) { void roots_of_unity_are_plausible(void) {
@ -101,7 +102,7 @@ void new_fft_settings_is_plausible(void) {
void is_power_of_two_works(void) { void is_power_of_two_works(void) {
// All actual powers of two // All actual powers of two
for (int i = 0; i <=63; i++) { for (int i = 0; i <= 63; i++) {
TEST_CHECK(true == is_power_of_two((uint64_t)1 << i)); TEST_CHECK(true == is_power_of_two((uint64_t)1 << i));
TEST_MSG("Case %d", i); TEST_MSG("Case %d", i);
} }
@ -114,14 +115,13 @@ void is_power_of_two_works(void) {
TEST_CHECK(false == is_power_of_two(1234567)); TEST_CHECK(false == is_power_of_two(1234567));
} }
TEST_LIST = TEST_LIST = {
{ {"FFT_COMMON_TEST", title},
{"FFT_COMMON_TEST", title}, {"roots_of_unity_is_the_expected_size", roots_of_unity_is_the_expected_size},
{"roots_of_unity_is_the_expected_size", roots_of_unity_is_the_expected_size}, {"roots_of_unity_are_plausible", roots_of_unity_are_plausible},
{"roots_of_unity_are_plausible", roots_of_unity_are_plausible}, {"reverse_works", reverse_works},
{"reverse_works", reverse_works}, {"expand_roots_is_plausible", expand_roots_is_plausible},
{"expand_roots_is_plausible", expand_roots_is_plausible}, {"new_fft_settings_is_plausible", new_fft_settings_is_plausible},
{"new_fft_settings_is_plausible", new_fft_settings_is_plausible}, {"is_power_of_two_works", is_power_of_two_works},
{"is_power_of_two_works", is_power_of_two_works}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};

View File

@ -18,7 +18,8 @@
#include "blst_util.h" #include "blst_util.h"
// Slow Fourier Transform (simple, good for small sizes) // Slow Fourier Transform (simple, good for small sizes)
void fft_fr_slow(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride, uint64_t l) { void fft_fr_slow(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l) {
blst_fr v, last, jv, r; blst_fr v, last, jv, r;
for (uint64_t i = 0; i < l; i++) { for (uint64_t i = 0; i < l; i++) {
blst_fr_mul(&last, &in[0], &roots[0]); blst_fr_mul(&last, &in[0], &roots[0]);
@ -33,10 +34,11 @@ void fft_fr_slow(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr
} }
// Fast Fourier Transform // Fast Fourier Transform
void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride, uint64_t l) { void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l) {
uint64_t half = l / 2; uint64_t half = l / 2;
if (half > 2) { // TODO: Tunable parameter if (half > 2) { // TODO: Tunable parameter
fft_fr_fast(out, in, stride * 2, roots, roots_stride * 2, half); fft_fr_fast(out, in, stride * 2, roots, roots_stride * 2, half);
fft_fr_fast(out + half, in + stride, stride * 2, roots, roots_stride * 2, half); fft_fr_fast(out + half, in + stride, stride * 2, roots, roots_stride * 2, half);
for (uint64_t i = 0; i < half; i++) { for (uint64_t i = 0; i < half; i++) {
blst_fr y_times_root; blst_fr y_times_root;
@ -50,7 +52,7 @@ void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr
} }
// The main entry point for forward and reverse FFTs // The main entry point for forward and reverse FFTs
C_KZG_RET fft_fr (blst_fr *out, const blst_fr *in, const FFTSettings *fs, bool inv, uint64_t n) { C_KZG_RET fft_fr(blst_fr *out, const blst_fr *in, const FFTSettings *fs, bool inv, uint64_t n) {
uint64_t stride = fs->max_width / n; uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS); ASSERT(n <= fs->max_width, C_KZG_BADARGS);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); ASSERT(is_power_of_two(n), C_KZG_BADARGS);

View File

@ -17,6 +17,8 @@
#include "c_kzg.h" #include "c_kzg.h"
#include "fft_common.h" #include "fft_common.h"
void fft_fr_slow(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride, uint64_t l); void fft_fr_slow(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride, uint64_t l); uint64_t l);
C_KZG_RET fft_fr (blst_fr *out, const blst_fr *in, const FFTSettings *fs, bool inv, uint64_t n); void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l);
C_KZG_RET fft_fr(blst_fr *out, const blst_fr *in, const FFTSettings *fs, bool inv, uint64_t n);

View File

@ -19,27 +19,27 @@
#include "fft_fr.h" #include "fft_fr.h"
#include "blst_util.h" #include "blst_util.h"
const uint64_t inv_fft_expected[][4] = const uint64_t inv_fft_expected[][4] = {
{ {0x7fffffff80000008L, 0xa9ded2017fff2dffL, 0x199cec0404d0ec02L, 0x39f6d3a994cebea4L},
{0x7fffffff80000008L, 0xa9ded2017fff2dffL, 0x199cec0404d0ec02L, 0x39f6d3a994cebea4L}, {0x27325dd08ac4cee9L, 0xcbb94f168ddacca9L, 0x6843be68485784b1L, 0x5a6faf9039451673L},
{0x27325dd08ac4cee9L, 0xcbb94f168ddacca9L, 0x6843be68485784b1L, 0x5a6faf9039451673L}, {0x16d00224dcf9382cL, 0xfee079062d1eaa93L, 0x3ce49204a2235046L, 0x163147176461030eL},
{0x16d00224dcf9382cL, 0xfee079062d1eaa93L, 0x3ce49204a2235046L, 0x163147176461030eL}, {0x10d6917f04735deaL, 0x7e04a13731049a48L, 0x42cbd9ab89d7b1f7L, 0x60546bd624850b42L},
{0x10d6917f04735deaL, 0x7e04a13731049a48L, 0x42cbd9ab89d7b1f7L, 0x60546bd624850b42L}, {0x80007fff80000000L, 0x1fe05202bb00adffL, 0x6045d26b3fd26e6bL, 0x39f6d3a994cebea4L},
{0x80007fff80000000L, 0x1fe05202bb00adffL, 0x6045d26b3fd26e6bL, 0x39f6d3a994cebea4L}, {0xe3388d354a80ed91L, 0x5849af2fc2cd4521L, 0xe3a64f3f31971b0bL, 0x33f1dda75bc30526L},
{0xe3388d354a80ed91L, 0x5849af2fc2cd4521L, 0xe3a64f3f31971b0bL, 0x33f1dda75bc30526L}, {0x16cf0224dcf9382cL, 0x12dd7903b71baa93L, 0xaf92c5362c204b76L, 0x163147176461030dL},
{0x16cf0224dcf9382cL, 0x12dd7903b71baa93L, 0xaf92c5362c204b76L, 0x163147176461030dL}, {0xf9925986d0d25e90L, 0xcdf85d0a339d7782L, 0xee7a9a5f0410e423L, 0x2e0d216170831056L},
{0xf9925986d0d25e90L, 0xcdf85d0a339d7782L, 0xee7a9a5f0410e423L, 0x2e0d216170831056L}, {0x7fffffff80000000L, 0xa9ded2017fff2dffL, 0x199cec0404d0ec02L, 0x39f6d3a994cebea4L},
{0x7fffffff80000000L, 0xa9ded2017fff2dffL, 0x199cec0404d0ec02L, 0x39f6d3a994cebea4L}, {0x066da6782f2da170L, 0x85c546f8cc60e47cL, 0x44bf3da90590f3e1L, 0x45e085f1b91a6cf1L},
{0x066da6782f2da170L, 0x85c546f8cc60e47cL, 0x44bf3da90590f3e1L, 0x45e085f1b91a6cf1L}, {0xe930fdda2306c7d4L, 0x40e02aff48e2b16bL, 0x83a712d1dd818c8fL, 0x5dbc603bc53c7a3aL},
{0xe930fdda2306c7d4L, 0x40e02aff48e2b16bL, 0x83a712d1dd818c8fL, 0x5dbc603bc53c7a3aL}, {0x1cc772c9b57f126fL, 0xfb73f4d33d3116ddL, 0x4f9388c8d80abcf9L, 0x3ffbc9abcdda7821L},
{0x1cc772c9b57f126fL, 0xfb73f4d33d3116ddL, 0x4f9388c8d80abcf9L, 0x3ffbc9abcdda7821L}, {0x7fff7fff80000000L, 0x33dd520044fdadffL, 0xd2f4059cc9cf699aL, 0x39f6d3a994cebea3L},
{0x7fff7fff80000000L, 0x33dd520044fdadffL, 0xd2f4059cc9cf699aL, 0x39f6d3a994cebea3L}, {0xef296e7ffb8ca216L, 0xd5b902cbcef9c1b6L, 0xf06dfe5c7fca260dL, 0x13993b7d05187205L},
{0xef296e7ffb8ca216L, 0xd5b902cbcef9c1b6L, 0xf06dfe5c7fca260dL, 0x13993b7d05187205L}, {0xe92ffdda2306c7d4L, 0x54dd2afcd2dfb16bL, 0xf6554603677e87beL, 0x5dbc603bc53c7a39L},
{0xe92ffdda2306c7d4L, 0x54dd2afcd2dfb16bL, 0xf6554603677e87beL, 0x5dbc603bc53c7a39L}, {0xd8cda22e753b3117L, 0x880454ec72238f55L, 0xcaf6199fc14a5353L, 0x197df7c2f05866d4L}};
{0xd8cda22e753b3117L, 0x880454ec72238f55L, 0xcaf6199fc14a5353L, 0x197df7c2f05866d4L}
};
void title(void) {;} void title(void) {
;
}
void compare_sft_fft(void) { void compare_sft_fft(void) {
// Initialise: ascending values of i (could be anything), and arbitrary size // Initialise: ascending values of i (could be anything), and arbitrary size
@ -100,7 +100,7 @@ void inverse_fft(void) {
TEST_CHECK(fft_fr(out, data, &fs, true, fs.max_width) == C_KZG_OK); TEST_CHECK(fft_fr(out, data, &fs, true, fs.max_width) == C_KZG_OK);
// Verify against the known result, `inv_fft_expected` // Verify against the known result, `inv_fft_expected`
int n = sizeof inv_fft_expected / sizeof inv_fft_expected[0]; int n = sizeof inv_fft_expected / sizeof inv_fft_expected[0];
TEST_CHECK(n == fs.max_width); TEST_CHECK(n == fs.max_width);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
blst_fr expected; blst_fr expected;
@ -111,11 +111,10 @@ void inverse_fft(void) {
free_fft_settings(&fs); free_fft_settings(&fs);
} }
TEST_LIST = TEST_LIST = {
{ {"FFT_FR_TEST", title},
{"FFT_FR_TEST", title}, {"compare_sft_fft", compare_sft_fft},
{"compare_sft_fft", compare_sft_fft}, {"roundtrip_fft", roundtrip_fft},
{"roundtrip_fft", roundtrip_fft}, {"inverse_fft", inverse_fft},
{"inverse_fft", inverse_fft}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};

View File

@ -37,7 +37,7 @@ void fft_g1_slow(blst_p1 *out, blst_p1 *in, uint64_t stride, blst_fr *roots, uin
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, blst_p1 *in, uint64_t stride, blst_fr *roots, uint64_t roots_stride, uint64_t l) {
uint64_t half = l / 2; uint64_t half = l / 2;
if (half > 2) { // TODO: Tunable parameter if (half > 2) { // TODO: Tunable parameter
fft_g1_fast(out, in, stride * 2, roots, roots_stride * 2, half); fft_g1_fast(out, in, stride * 2, roots, roots_stride * 2, half);
fft_g1_fast(out + half, in + stride, stride * 2, roots, roots_stride * 2, half); fft_g1_fast(out + half, in + stride, stride * 2, roots, roots_stride * 2, half);
for (uint64_t i = 0; i < half; i++) { for (uint64_t i = 0; i < half; i++) {
blst_p1 y_times_root; blst_p1 y_times_root;
@ -51,7 +51,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 // 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, blst_p1 *in, FFTSettings *fs, bool inv, uint64_t n) {
uint64_t stride = fs->max_width / n; uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS); ASSERT(n <= fs->max_width, C_KZG_BADARGS);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); ASSERT(is_power_of_two(n), C_KZG_BADARGS);

View File

@ -19,4 +19,4 @@
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, 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); 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); C_KZG_RET fft_g1(blst_p1 *out, blst_p1 *in, FFTSettings *fs, bool inv, uint64_t n);

View File

@ -18,7 +18,9 @@
#include "debug_util.h" #include "debug_util.h"
#include "fft_g1.h" #include "fft_g1.h"
void title(void) {;} void title(void) {
;
}
void make_data(blst_p1 *out, uint64_t n) { void make_data(blst_p1 *out, uint64_t n) {
// Multiples of g1_gen // Multiples of g1_gen
@ -70,10 +72,9 @@ void roundtrip_fft(void) {
free_fft_settings(&fs); free_fft_settings(&fs);
} }
TEST_LIST = TEST_LIST = {
{ {"FFT_G1_TEST", title},
{"FFT_G1_TEST", title}, {"compare_sft_fft", compare_sft_fft},
{"compare_sft_fft", compare_sft_fft}, {"roundtrip_fft", roundtrip_fft},
{"roundtrip_fft", roundtrip_fft}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};

View File

@ -36,7 +36,8 @@ C_KZG_RET compute_proof_single(blst_p1 *out, const KZGSettings *ks, poly *p, con
return compute_proof_multi(out, ks, p, x0, 1); return compute_proof_multi(out, ks, p, x0, 1);
} }
C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x, blst_fr *y) { C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, blst_fr *y) {
blst_p2 x_g2, s_minus_x; blst_p2 x_g2, s_minus_x;
blst_p1 y_g1, commitment_minus_y; blst_p1 y_g1, commitment_minus_y;
p2_mul(&x_g2, blst_p2_generator(), x); p2_mul(&x_g2, blst_p2_generator(), x);
@ -84,7 +85,8 @@ C_KZG_RET compute_proof_multi(blst_p1 *out, const KZGSettings *ks, poly *p, cons
// Check a proof for a KZG commitment for an evaluation f(x w^i) = y_i // Check a proof for a KZG commitment for an evaluation f(x w^i) = y_i
// The ys must have a power of 2 length // The ys must have a power of 2 length
C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x, const blst_fr *ys, uint64_t n) { C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, const blst_fr *ys, uint64_t n) {
poly interp; poly interp;
blst_fr inv_x, inv_x_pow, x_pow; blst_fr inv_x, inv_x_pow, x_pow;
blst_p2 xn2, xn_minus_yn; blst_p2 xn2, xn_minus_yn;
@ -112,7 +114,7 @@ C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *com
// [interpolation_polynomial(s)]_1 // [interpolation_polynomial(s)]_1
commit_to_poly(&is1, ks, &interp); commit_to_poly(&is1, ks, &interp);
// [commitment - interpolation_polynomial(s)]_1 = [commit]_1 - [interpolation_polynomial(s)]_1 // [commitment - interpolation_polynomial(s)]_1 = [commit]_1 - [interpolation_polynomial(s)]_1
p1_sub(&commit_minus_interp, commitment, &is1); p1_sub(&commit_minus_interp, commitment, &is1);
*out = pairings_verify(&commit_minus_interp, blst_p2_generator(), proof, &xn_minus_yn); *out = pairings_verify(&commit_minus_interp, blst_p2_generator(), proof, &xn_minus_yn);

View File

@ -29,6 +29,8 @@ typedef struct {
C_KZG_RET new_kzg_settings(KZGSettings *ks, FFTSettings *fs, blst_p1 *secret_g1, blst_p2 *secret_g2, uint64_t length); C_KZG_RET new_kzg_settings(KZGSettings *ks, FFTSettings *fs, blst_p1 *secret_g1, blst_p2 *secret_g2, uint64_t length);
void commit_to_poly(blst_p1 *out, const KZGSettings *ks, const poly *p); void commit_to_poly(blst_p1 *out, const KZGSettings *ks, const poly *p);
C_KZG_RET compute_proof_single(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0); C_KZG_RET compute_proof_single(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0);
C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x, blst_fr *y); C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, blst_fr *y);
C_KZG_RET compute_proof_multi(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0, uint64_t n); C_KZG_RET compute_proof_multi(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0, uint64_t n);
C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x, const blst_fr *ys, uint64_t n); C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, const blst_fr *ys, uint64_t n);

View File

@ -19,13 +19,9 @@
#include "kzg_proofs.h" #include "kzg_proofs.h"
// The generator for our "trusted" setup // The generator for our "trusted" setup
blst_scalar secret = blst_scalar secret = {0xa4, 0x73, 0x31, 0x95, 0x28, 0xc8, 0xb6, 0xea, 0x4d, 0x08, 0xcc,
{ 0x53, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xa4, 0x73, 0x31, 0x95, 0x28, 0xc8, 0xb6, 0xea, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Little-endian?
0x4d, 0x08, 0xcc, 0x53, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; // Little-endian?
void generate_setup(blst_p1 *s1, blst_p2 *s2, const blst_scalar *secret, const uint64_t n) { void generate_setup(blst_p1 *s1, blst_p2 *s2, const blst_scalar *secret, const uint64_t n) {
blst_fr s_pow, s; blst_fr s_pow, s;
@ -38,7 +34,9 @@ void generate_setup(blst_p1 *s1, blst_p2 *s2, const blst_scalar *secret, const u
} }
} }
void title(void) {;} void title(void) {
;
}
void proof_single(void) { void proof_single(void) {
// Our polynomial: degree 15, 16 coefficients // Our polynomial: degree 15, 16 coefficients
@ -172,11 +170,10 @@ void commit_to_nil_poly(void) {
TEST_CHECK(blst_p1_affine_is_equal(&identity_g1_affine, &result_affine)); TEST_CHECK(blst_p1_affine_is_equal(&identity_g1_affine, &result_affine));
} }
TEST_LIST = TEST_LIST = {
{ {"KZG_PROOFS_TEST", title},
{"KZG_PROOFS_TEST", title}, {"proof_single", proof_single},
{"proof_single", proof_single}, {"proof_multi", proof_multi},
{"proof_multi", proof_multi}, {"commit_to_nil_poly", commit_to_nil_poly},
{"commit_to_nil_poly", commit_to_nil_poly}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};

View File

@ -18,7 +18,9 @@
#include "debug_util.h" #include "debug_util.h"
#include "poly.h" #include "poly.h"
void title(void) {;} void title(void) {
;
}
void poly_div_length(void) { void poly_div_length(void) {
poly a, b; poly a, b;
@ -190,16 +192,15 @@ void poly_eval_nil_check(void) {
free_poly(&p); free_poly(&p);
} }
TEST_LIST = TEST_LIST = {
{ {"POLY_TEST", title},
{"POLY_TEST", title}, {"poly_div_length", poly_div_length},
{"poly_div_length", poly_div_length}, {"poly_div_0", poly_div_0},
{"poly_div_0", poly_div_0}, {"poly_div_1", poly_div_1},
{"poly_div_1", poly_div_1}, {"poly_div_2", poly_div_2},
{"poly_div_2", poly_div_2}, {"poly_div_by_zero", poly_div_by_zero},
{"poly_div_by_zero", poly_div_by_zero}, {"poly_eval_check", poly_eval_check},
{"poly_eval_check", poly_eval_check}, {"poly_eval_0_check", poly_eval_0_check},
{"poly_eval_0_check", poly_eval_0_check}, {"poly_eval_nil_check", poly_eval_nil_check},
{"poly_eval_nil_check", poly_eval_nil_check}, {NULL, NULL} /* zero record marks the end of the list */
{ NULL, NULL } /* zero record marks the end of the list */ };
};