Skip to content

Commit

Permalink
Merge pull request #237 from zkFold/turtlepu-fix-binary-zp
Browse files Browse the repository at this point in the history
Fixed $\mathbb{Z}_p$'s binary instance to take _at most_ wordCount words
  • Loading branch information
vlasin authored Sep 2, 2024
2 parents 8074d7f + 56f9293 commit 9397074
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/ZkFold/Base/Algebra/Basic/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module ZkFold.Base.Algebra.Basic.Field (
Ext3(..)
) where

import Control.Applicative ((<|>))
import Control.DeepSeq (NFData (..))
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Bifunctor (first)
Expand All @@ -32,7 +33,7 @@ import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Base.Algebra.Polynomials.Univariate
import ZkFold.Base.Data.ByteString
import ZkFold.Prelude (log2ceiling, replicateA)
import ZkFold.Prelude (log2ceiling)

------------------------------ Prime Fields -----------------------------------

Expand Down Expand Up @@ -166,10 +167,11 @@ instance KnownNat p => Binary (Zp p) where
go n count =
let (n', r) = n `Haskell.divMod` 256
in putWord8 (fromIntegral r) <> go n' (count -! 1)
get = toZp . go <$> replicateA (wordCount @p) getWord8
get = toZp <$> go (wordCount @p)
where
go [] = 0
go (x:xs) = fromIntegral x + 256 * go xs
go 0 = pure zero
go n = liftA2 combine getWord8 (go $ n -! 1) <|> pure zero
combine r d = fromIntegral r + 256 * d

wordCount :: forall p. KnownNat p => Natural
wordCount = ceiling $ log2ceiling (value @p -! 1) % (8 :: Natural)
Expand Down

0 comments on commit 9397074

Please sign in to comment.