From 0edb80b6b6faaa2d19040a1fa084c4a28d7e0b8c Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Wed, 5 Nov 2025 17:06:24 +0100 Subject: [PATCH] create a "bundle" from all the C files (better inlining opportunities for the C compiler); also add a simple benchmark for the C FFT implementation --- reference/src/FRI/Types.hs | 3 ++ reference/src/NTT/FFT.hs | 14 +++---- reference/src/NTT/FFT/Fast.hs | 2 +- reference/src/NTT/Poly.hs | 14 +++---- reference/src/NTT/Poly/Flat.hs | 8 +++- reference/src/cbits/.gitignore | 1 + reference/src/cbits/bench_fft.c | 59 ++++++++++++++++++++++++++++ reference/src/cbits/bundle.c | 9 +++++ reference/src/cbits/bundle.h | 6 +++ reference/src/cbits/compile.sh | 2 + reference/src/cbits/goldilocks.h | 4 ++ reference/src/cbits/goldilocks_ext.h | 3 ++ reference/src/cbits/monolith.c | 3 ++ reference/src/cbits/monolith.h | 5 +++ reference/src/cbits/ntt.c | 1 + reference/src/cbits/ntt.h | 4 ++ reference/src/cbits/short_dft.h | 4 ++ reference/src/runi.sh | 2 +- 18 files changed, 127 insertions(+), 17 deletions(-) create mode 100644 reference/src/cbits/.gitignore create mode 100644 reference/src/cbits/bench_fft.c create mode 100644 reference/src/cbits/bundle.c create mode 100644 reference/src/cbits/bundle.h diff --git a/reference/src/FRI/Types.hs b/reference/src/FRI/Types.hs index eaed620..61a43a4 100644 --- a/reference/src/FRI/Types.hs +++ b/reference/src/FRI/Types.hs @@ -268,6 +268,7 @@ instance Binary FriProof where -------------------------------------------------------------------------------- +{- estimateFriProofSize :: FriConfig -> Int estimateFriProofSize friConfig@(MkFriConfig{..}) = total where @@ -308,3 +309,5 @@ data FriProof = MkFriProof , proofQueryRounds :: [FriQueryRound] -- ^ query rounds , proofPowWitness :: F -- ^ witness showing that the prover did PoW } + +-} diff --git a/reference/src/NTT/FFT.hs b/reference/src/NTT/FFT.hs index fb25fd0..22f75d0 100644 --- a/reference/src/NTT/FFT.hs +++ b/reference/src/NTT/FFT.hs @@ -1,14 +1,14 @@ {-# LANGUAGE CPP #-} -#ifdef USE_NAIVE_HASKELL +-- #ifdef USE_NAIVE_HASKELL module NTT.FFT ( module NTT.FFT.Slow ) where import NTT.FFT.Slow -#else - -module NTT.FFT ( module NTT.FFT.Fast ) where -import NTT.FFT.Fast - -#endif +-- #else +-- +-- module NTT.FFT ( module NTT.FFT.Fast ) where +-- import NTT.FFT.Fast +-- +-- #endif diff --git a/reference/src/NTT/FFT/Fast.hs b/reference/src/NTT/FFT/Fast.hs index 07aee54..4abc21e 100644 --- a/reference/src/NTT/FFT/Fast.hs +++ b/reference/src/NTT/FFT/Fast.hs @@ -16,7 +16,7 @@ import System.IO.Unsafe import Data.Flat -import NTT.Poly +import NTT.Poly.Flat import NTT.Subgroup import Field.Goldilocks import Misc diff --git a/reference/src/NTT/Poly.hs b/reference/src/NTT/Poly.hs index e2f1e51..4877bcb 100644 --- a/reference/src/NTT/Poly.hs +++ b/reference/src/NTT/Poly.hs @@ -1,13 +1,13 @@ {-# LANGUAGE CPP #-} -#ifdef USE_NAIVE_HASKELL +-- #ifdef USE_NAIVE_HASKELL module NTT.Poly ( module NTT.Poly.Naive ) where import NTT.Poly.Naive -#else - -module NTT.Poly ( module NTT.Poly.Flat ) where -import NTT.Poly.Flat - -#endif +-- #else +-- +-- module NTT.Poly ( module NTT.Poly.Flat ) where +-- import NTT.Poly.Flat +-- +-- #endif diff --git a/reference/src/NTT/Poly/Flat.hs b/reference/src/NTT/Poly/Flat.hs index 82e3c4c..c909dad 100644 --- a/reference/src/NTT/Poly/Flat.hs +++ b/reference/src/NTT/Poly/Flat.hs @@ -26,7 +26,13 @@ import Data.Flat as L -------------------------------------------------------------------------------- -newtype Poly a = MkPoly (L.FlatArray a) +newtype Poly a + = MkPoly (L.FlatArray a) + deriving Show + +-- TEMPORARY HACK +instance (Eq a, Flat a) => Eq (Poly a) where + p == q = (coeffs p == coeffs q) pattern XPoly n arr = MkPoly (L.MkFlatArray n arr) diff --git a/reference/src/cbits/.gitignore b/reference/src/cbits/.gitignore new file mode 100644 index 0000000..cba7efc --- /dev/null +++ b/reference/src/cbits/.gitignore @@ -0,0 +1 @@ +a.out diff --git a/reference/src/cbits/bench_fft.c b/reference/src/cbits/bench_fft.c new file mode 100644 index 0000000..9a7fc03 --- /dev/null +++ b/reference/src/cbits/bench_fft.c @@ -0,0 +1,59 @@ + +#include +#include +#include + +#include + +//#include "goldilocks.h" +//#include "ntt.h" +#include "bundle.h" + +//------------------------------------------------------------------------------ + +const int LOGN = 20; +const int N = (1< //------------------------------------------------------------------------------ @@ -44,3 +46,5 @@ void goldilocks_convert_31_bytes_to_4_field_elements ( const uint8_t *ptr, void goldilocks_convert_bytes_to_field_elements ( int rate, const uint8_t *ptr, uint64_t *felts ); //------------------------------------------------------------------------------ + +#endif // _GOLDILOCKS_H_INCLUDED_ diff --git a/reference/src/cbits/goldilocks_ext.h b/reference/src/cbits/goldilocks_ext.h index ab02a0d..1598aba 100644 --- a/reference/src/cbits/goldilocks_ext.h +++ b/reference/src/cbits/goldilocks_ext.h @@ -1,6 +1,8 @@ // quadratic field extension F[x] = F(x) / (x^2 - 7) over the Goldilocks field +#ifndef _GOLDILOCKS_EXT_H_INCLUDED_ + #include #include "goldilocks.h" @@ -21,3 +23,4 @@ void goldilocks_ext_pow(const uint64_t *b , int e , uint64_t *out); //------------------------------------------------------------------------------ +#endif // _GOLDILOCKS_EXT_H_INCLUDED_ diff --git a/reference/src/cbits/monolith.c b/reference/src/cbits/monolith.c index ba61173..20c7355 100644 --- a/reference/src/cbits/monolith.c +++ b/reference/src/cbits/monolith.c @@ -1,4 +1,6 @@ +// Monolith permutation and hash function + #include #include @@ -242,3 +244,4 @@ void goldilocks_monolith_bytes_digest(int rate, int N, const uint8_t *input, uin } //------------------------------------------------------------------------------ + diff --git a/reference/src/cbits/monolith.h b/reference/src/cbits/monolith.h index 67cfc33..e33f034 100644 --- a/reference/src/cbits/monolith.h +++ b/reference/src/cbits/monolith.h @@ -1,4 +1,7 @@ + +#ifndef _MONOLITH_H_INCLUDED_ + #include //------------------------------------------------------------------------------ @@ -11,3 +14,5 @@ void goldilocks_monolith_bytes_digest (int rate, int N, const uint8_t *input void goldilocks_monolith_felts_digest (int rate, int N, const uint64_t *input, uint64_t *hash); //------------------------------------------------------------------------------ + +#endif // _MONOLITH_H_INCLUDED_ diff --git a/reference/src/cbits/ntt.c b/reference/src/cbits/ntt.c index 6ef15f7..c65599e 100644 --- a/reference/src/cbits/ntt.c +++ b/reference/src/cbits/ntt.c @@ -1,4 +1,5 @@ + #include #include #include diff --git a/reference/src/cbits/ntt.h b/reference/src/cbits/ntt.h index b3d6a19..f241fc9 100644 --- a/reference/src/cbits/ntt.h +++ b/reference/src/cbits/ntt.h @@ -1,4 +1,6 @@ +#ifndef _NTT_H_INCLUDED_ + #include //------------------------------------------------------------------------------ @@ -12,3 +14,5 @@ void goldilocks_ntt_inverse ( int m, uint64_t gen, const void goldilocks_ntt_inverse_shifted (uint64_t eta, int m, uint64_t gen, const uint64_t *src, uint64_t *tgt); //------------------------------------------------------------------------------ + +#endif // _NTT_H_INCLUDED_ diff --git a/reference/src/cbits/short_dft.h b/reference/src/cbits/short_dft.h index fbc7022..e7707db 100644 --- a/reference/src/cbits/short_dft.h +++ b/reference/src/cbits/short_dft.h @@ -1,4 +1,6 @@ +#ifndef _SHORT_DFT_H_INCLUDED_ + #include //------------------------------------------------------------------------------ @@ -22,3 +24,5 @@ void short_inv_DFT_size_16_rescaled( int src_stride, int tgt_stride, uint64_t *s // void short_inv_DFT_size_4_ext_rescaled( int src_stride, int tgt_stride, uint64_t *src, uint64_t *tgt ); //------------------------------------------------------------------------------ + +#endif // _SHORT_DFT_H_INCLUDED_ diff --git a/reference/src/runi.sh b/reference/src/runi.sh index 1321dfb..5049b2e 100755 --- a/reference/src/runi.sh +++ b/reference/src/runi.sh @@ -1,3 +1,3 @@ #!/bin/bash -ghci testMain.hs cbits/goldilocks.o cbits/goldilocks_ext.o cbits/monolith.o cbits/ntt.o cbits/short_dft.o +ghci testMain.hs cbits/bundle.o