From 0b87acea64dd0de274e2653fb988c4fa14ef62b0 Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Thu, 23 Jan 2025 20:01:16 +0100 Subject: [PATCH] implement the Reducing and ReducingExtension gates --- README.md | 7 ++--- src/Gate/Constraints.hs | 14 ++++----- src/Gate/Custom/Reducing.hs | 62 +++++++++++++++++++++++++++++++++++++ src/Misc/Aux.hs | 4 +-- 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 src/Gate/Custom/Reducing.hs diff --git a/README.md b/README.md index 24a7f94..3d29713 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,12 @@ Supported gates: - [x] PoseidonGate - [x] PoseidonMdsGate - [x] RandomAccessGate -- [ ] ReducingGate -- [ ] ReducingExtensionGate +- [x] ReducingGate +- [x] ReducingExtensionGate Optional features: +- [ ] Supporting different hash functions - [ ] Field extensions with degree higher than 2 - [ ] Being parametric over the field choice -- [ ] Supporting different hash functions - diff --git a/src/Gate/Constraints.hs b/src/Gate/Constraints.hs index 5fb96b5..06db7c8 100644 --- a/src/Gate/Constraints.hs +++ b/src/Gate/Constraints.hs @@ -22,10 +22,12 @@ import Algebra.Expr import Gate.Base import Gate.Vars import Gate.Computation +import Misc.Aux + import Gate.Custom.Poseidon import Gate.Custom.RandomAccess import Gate.Custom.CosetInterp -import Misc.Aux +import Gate.Custom.Reducing -------------------------------------------------------------------------------- -- * Gate constraints @@ -98,17 +100,13 @@ gateComputation gate = -> randomAccessGateConstraints (MkRACfg (Log2 num_bits) num_copies num_extra_constants) ReducingGate num_coeffs - -> todo + -> reducingGateConstraints num_coeffs ReducingExtensionGate num_coeffs - -> todo + -> reducingExtensionGateConstraints num_coeffs UnknownGate name -> error ("gateConstraints: unknown gate `" ++ name ++ "`") - where - - todo = error $ "gateConstraints: gate `" ++ takeWhile isAlpha (show gate) ++ "` not yet implemented" - -------------------------------------------------------------------------------- -- computes `out = base ^ (sum 2^i e_i)` @@ -146,5 +144,7 @@ testCosetGate5 = testCompute $ cosetInterpolationGateConstraints $ cosetInt testRandAccGate = testCompute $ randomAccessGateConstraints $ randomAccessGateConfig (Log2 4) testPoseidonGate = testCompute $ gateComputation (PoseidonGate 12) testPoseidonMdsGate = testCompute $ gateComputation (PoseidonMdsGate 12) +testReducingGate = testCompute $ gateComputation (ReducingGate 13) +testReducingExtGate = testCompute $ gateComputation (ReducingExtensionGate 13) -------------------------------------------------------------------------------- diff --git a/src/Gate/Custom/Reducing.hs b/src/Gate/Custom/Reducing.hs new file mode 100644 index 0000000..1260946 --- /dev/null +++ b/src/Gate/Custom/Reducing.hs @@ -0,0 +1,62 @@ + +-- | The @Reducing@ and @ReducingExtension@ gates +-- +-- These compute +-- +-- > initial*alpha^n + sum_{i=0}^{n-1} c[i]*alpha^(n-i) +-- + +{-# LANGUAGE StrictData, RecordWildCards #-} +module Gate.Custom.Reducing where + +-------------------------------------------------------------------------------- + +import Data.Foldable +import Control.Monad + +import Algebra.Goldilocks +import Algebra.GoldilocksExt +import Algebra.Expr + +import Gate.Vars +import Gate.Computation + +import Misc.Aux + +-------------------------------------------------------------------------------- + +reducingGateConstraints :: Int -> Compute () +reducingGateConstraints num_coeffs = do + + forM_ [0..num_coeffs-1] $ \i -> do + commitExt $ prev i * alpha + fromBase (coeff i) - accum i + + where + + -- witness variables + output = wireExt 0 + alpha = wireExt 2 + initial = wireExt 4 + coeff i = wire (6+i) + accum i = if i < num_coeffs - 1 then wireExt (6 + num_coeffs + 2*i) else output + prev i = if i == 0 then initial else accum (i-1) + +-------------------------------------------------------------------------------- + +reducingExtensionGateConstraints :: Int -> Compute () +reducingExtensionGateConstraints num_coeffs = do + + forM_ [0..num_coeffs-1] $ \i -> do + commitExt $ prev i * alpha + coeff i - accum i + + where + + -- witness variables + output = wireExt 0 + alpha = wireExt 2 + initial = wireExt 4 + coeff i = wireExt (6+2*i) + accum i = if i < num_coeffs - 1 then wireExt (6 + 2*num_coeffs + 2*i) else output + prev i = if i == 0 then initial else accum (i-1) + +-------------------------------------------------------------------------------- diff --git a/src/Misc/Aux.hs b/src/Misc/Aux.hs index 446f072..c73bf0a 100644 --- a/src/Misc/Aux.hs +++ b/src/Misc/Aux.hs @@ -1,7 +1,7 @@ -- | Misc helper functions -{-# LANGUAGE StrictData, DeriveGeneric, DeriveAnyClass #-} +{-# LANGUAGE StrictData, DeriveGeneric, DeriveAnyClass, GeneralizedNewtypeDeriving, DerivingStrategies #-} module Misc.Aux where -------------------------------------------------------------------------------- @@ -17,7 +17,7 @@ import GHC.Generics newtype Log2 = Log2 Int - deriving (Eq,Ord,Show,Num) + deriving newtype (Eq,Ord,Show,Num) fromLog2 :: Log2 -> Int fromLog2 (Log2 k) = k