Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added binary instances #3

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ormolu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Ormolu CI

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

jobs:
ormolu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: haskell-actions/run-ormolu@v15
with:
version: "0.7.2.0"
1 change: 1 addition & 0 deletions galois-field.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ common warnings
common deps
build-depends:
, base >=4.18 && <5
, binary >=0.8.9 && <0.9
, bitvec ^>=1.1.3
, groups >=0.4.1 && <0.6
, integer-gmp ^>=1.1
Expand Down
4 changes: 3 additions & 1 deletion src/Data/Field/Galois/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Data.Field.Galois.Base
where

import Control.Monad.Random (Random)
import Data.Binary (Binary)
import Data.Field (Field)
import Data.Group qualified as G (Group (..))
import Data.Propagator (PropagatedNum)
Expand All @@ -28,7 +29,8 @@ class
Random k,
Show k,
NFData k,
PropagatedNum k
PropagatedNum k,
Binary k
) =>
GaloisField k
where
Expand Down
5 changes: 5 additions & 0 deletions src/Data/Field/Galois/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Data.Field.Galois.Binary
where

import Control.Monad.Random (Random (..))
import Data.Binary qualified as B
import Data.Bit (Bit, F2Poly, gcdExt, toF2Poly, unF2Poly)
import Data.Euclidean as S (Euclidean (..), GcdDomain)
import Data.Field (Field)
Expand Down Expand Up @@ -48,6 +49,10 @@ instance Propagated (Binary p)

instance (KnownNat p) => PropagatedNum (Binary p)

instance B.Binary (Binary p) where
put (B x) = B.put (toInteger <$> V.toList (unF2Poly x))
get = B . toF2Poly . V.fromList . map (fromInteger @Bit) <$> B.get

-- Binary fields are Galois fields.
instance (KnownNat p) => GaloisField (Binary p) where
char = const 2
Expand Down
5 changes: 5 additions & 0 deletions src/Data/Field/Galois/Extension.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Data.Field.Galois.Extension
where

import Control.Monad.Random (Random (..))
import Data.Binary qualified as B
import Data.Euclidean (Euclidean (..), GcdDomain, gcdExt)
import Data.Field (Field)
import Data.Field.Galois.Base (GaloisField (..))
Expand Down Expand Up @@ -54,6 +55,10 @@ class (GaloisField k) => ExtensionField k where
newtype Extension p k = E (VPoly k)
deriving (Eq, Generic, NFData, Ord, Show)

instance (GaloisField k) => B.Binary (Extension p k) where
put (E x) = B.put (toList x)
get = E . fromList <$> B.get

-- Extension fields are convertible.
instance (IrreducibleMonic p k) => ExtensionField (Extension p k) where
fromE = toList
Expand Down
6 changes: 5 additions & 1 deletion src/Data/Field/Galois/Prime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Data.Field.Galois.Prime
where

import Control.Monad.Random (Random (..))
import Data.Binary qualified as B
import Data.Euclidean as S (Euclidean (..), GcdDomain)
import Data.Field (Field)
import Data.Field.Galois.Base (GaloisField (..))
Expand Down Expand Up @@ -42,6 +43,10 @@ instance (KnownNat p) => PropagatedNum (Prime p)
instance Hashable (Prime p) where
hashWithSalt s (P x) = hashWithSalt s (unMod x)

instance (KnownNat p) => B.Binary (Prime p) where
put = B.put . fromP
get = P . fromInteger <$> B.get

-- Prime fields are convertible.
instance (KnownNat p) => PrimeField (Prime p) where
fromP (P x) = naturalToInteger (unMod x)
Expand Down Expand Up @@ -111,7 +116,6 @@ instance (KnownNat p) => Pretty (Prime p) where
then pretty x
else text "-" <> pretty (abs (x - p))


-- Prime fields are random.
instance (KnownNat p) => Random (Prime p) where
random = randomR (minBound, maxBound)
Expand Down
Loading