implement the Reducing and ReducingExtension gates

This commit is contained in:
Balazs Komuves 2025-01-23 20:01:16 +01:00
parent 8bfe0c6c10
commit 0b87acea64
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
4 changed files with 74 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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