From 33ee3d68dc9c118e88e8842d9f423a9f4f9851cf Mon Sep 17 00:00:00 2001 From: martyall Date: Mon, 20 May 2024 13:19:53 -0700 Subject: [PATCH] added binary instances --- .github/workflows/ormolu.yml | 16 ++++++++++++++++ galois-field.cabal | 1 + src/Data/Field/Galois/Base.hs | 4 +++- src/Data/Field/Galois/Binary.hs | 5 +++++ src/Data/Field/Galois/Extension.hs | 5 +++++ src/Data/Field/Galois/Prime.hs | 6 +++++- 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/ormolu.yml diff --git a/.github/workflows/ormolu.yml b/.github/workflows/ormolu.yml new file mode 100644 index 0000000..79a171a --- /dev/null +++ b/.github/workflows/ormolu.yml @@ -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" \ No newline at end of file diff --git a/galois-field.cabal b/galois-field.cabal index cdc2ca7..b4f97d8 100644 --- a/galois-field.cabal +++ b/galois-field.cabal @@ -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 diff --git a/src/Data/Field/Galois/Base.hs b/src/Data/Field/Galois/Base.hs index def966f..24885a2 100644 --- a/src/Data/Field/Galois/Base.hs +++ b/src/Data/Field/Galois/Base.hs @@ -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) @@ -28,7 +29,8 @@ class Random k, Show k, NFData k, - PropagatedNum k + PropagatedNum k, + Binary k ) => GaloisField k where diff --git a/src/Data/Field/Galois/Binary.hs b/src/Data/Field/Galois/Binary.hs index 428797d..705c27e 100644 --- a/src/Data/Field/Galois/Binary.hs +++ b/src/Data/Field/Galois/Binary.hs @@ -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) @@ -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 diff --git a/src/Data/Field/Galois/Extension.hs b/src/Data/Field/Galois/Extension.hs index 928e8dd..291f6d4 100644 --- a/src/Data/Field/Galois/Extension.hs +++ b/src/Data/Field/Galois/Extension.hs @@ -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 (..)) @@ -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 diff --git a/src/Data/Field/Galois/Prime.hs b/src/Data/Field/Galois/Prime.hs index 88cdd46..1a15813 100644 --- a/src/Data/Field/Galois/Prime.hs +++ b/src/Data/Field/Galois/Prime.hs @@ -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 (..)) @@ -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) @@ -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)