Skip to content

Commit

Permalink
EuclideanDomain -> SemiEuclidean
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtlePU committed Sep 6, 2024
1 parent c2f395b commit 26396af
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
22 changes: 13 additions & 9 deletions src/ZkFold/Base/Algebra/Basic/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,20 @@ intScale n a | n < 0 = naturalFromInteger (-n) `scale` negate a
-}
class (AdditiveMonoid a, MultiplicativeMonoid a, FromConstant Natural a) => Semiring a

{- | A Euclidean domain @R@ is an integral domain which can be endowed
with at least one function @f : R\{0} -> R+@ s.t.
If @a@ and @b@ are in @R@ and @b@ is nonzero, then there exist @q@ and @r@ in @R@ such that
@a = bq + r@ and either @r = 0@ or @f(r) < f(b)@.
{- | A semi-Euclidean-domain @a@ is a semiring without zero divisors which can
be endowed with at least one function @f : a\{0} -> R+@ s.t. if @x@ and @y@ are
in @a@ and @y@ is nonzero, then there exist @q@ and @r@ in @a@ such that
@x = qy + r@ and either @r = 0@ or @f(r) < f(y)@.
@q@ and @r@ are called respectively a quotient and a remainder of the division (or Euclidean division) of @a@ by @b@.
@q@ and @r@ are called respectively a quotient and a remainder of the division
(or Euclidean division) of @x@ by @y@.
The function @divMod@ associated with this class produces @q@ and @r@ given @a@ and @b@.
The function @divMod@ associated with this class produces @q@ and @r@
given @a@ and @b@.
This is a generalization of a notion of Euclidean domains to semirings.
-}
class Semiring a => EuclideanDomain a where
class Semiring a => SemiEuclidean a where
{-# MINIMAL divMod | (div, mod) #-}

divMod :: a -> a -> (a, a)
Expand Down Expand Up @@ -479,7 +483,7 @@ instance AdditiveMonoid Natural where

instance Semiring Natural

instance EuclideanDomain Natural where
instance SemiEuclidean Natural where
divMod = Haskell.divMod

instance BinaryExpansion Natural where
Expand Down Expand Up @@ -517,7 +521,7 @@ instance FromConstant Natural Integer where

instance Semiring Integer

instance EuclideanDomain Integer where
instance SemiEuclidean Integer where
divMod = Haskell.divMod

instance Ring Integer
Expand Down
2 changes: 1 addition & 1 deletion src/ZkFold/Base/Algebra/Basic/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ instance KnownNat p => FromConstant Natural (Zp p) where

instance KnownNat p => Semiring (Zp p)

instance KnownNat p => EuclideanDomain (Zp p) where
instance KnownNat p => SemiEuclidean (Zp p) where
divMod a b = let (q, r) = Haskell.divMod (fromZp a) (fromZp b)
in (toZp . fromIntegral $ q, toZp . fromIntegral $ r)

Expand Down
2 changes: 1 addition & 1 deletion src/ZkFold/Base/Algebra/Basic/Sources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ instance (Finite a, Ord i) => BinaryExpansion (Sources a i) where
type Bits (Sources a i) = [Sources a i]
binaryExpansion = replicate (numberOfBits @a)

instance Ord i => EuclideanDomain (Sources a i) where
instance Ord i => SemiEuclidean (Sources a i) where
div = (<>)
mod = (<>)
4 changes: 2 additions & 2 deletions src/ZkFold/Symbolic/Algorithms/Hash/Blake2b.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import Prelude hiding (Num (
replicate, splitAt, truncate, (!!), (&&), (^))

import ZkFold.Base.Algebra.Basic.Class (AdditiveGroup (..), AdditiveSemigroup (..),
EuclideanDomain (..), Exponent (..),
FromConstant (..), MultiplicativeSemigroup (..),
Exponent (..), FromConstant (..),
MultiplicativeSemigroup (..), SemiEuclidean (..),
divMod, one, zero, (-!))
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Prelude (length, replicate, splitAt, (!!))
Expand Down
4 changes: 2 additions & 2 deletions src/ZkFold/Symbolic/Data/UInt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ cast n =
eea
:: forall n c r
. Symbolic c
=> EuclideanDomain (UInt n r c)
=> SemiEuclidean (UInt n r c)
=> KnownNat n
=> KnownNat (NumberOfRegisters (BaseField c) n r)
=> AdditiveGroup (UInt n r c)
Expand Down Expand Up @@ -200,7 +200,7 @@ instance
, KnownRegisterSize rs
, r ~ NumberOfRegisters (BaseField c) n rs
, NFData (c (Vector r))
) => EuclideanDomain (UInt n rs c) where
) => SemiEuclidean (UInt n rs c) where
divMod numerator d = bool @(Bool c) (q, r) (zero, zero) (d == zero)
where
(q, r) = Haskell.foldl longDivisionStep (zero, zero) [value @n -! 1, value @n -! 2 .. 0]
Expand Down
2 changes: 1 addition & 1 deletion src/ZkFold/Symbolic/MonadCircuit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ZkFold.Base.Algebra.Basic.Class
-- | A @'WitnessField'@ should support all algebraic operations
-- used inside an arithmetic circuit.
type WitnessField n a = ( FiniteField a, ToConstant a, Const a ~ n
, FromConstant n a, EuclideanDomain n)
, FromConstant n a, SemiEuclidean n)

-- | A type of witness builders. @i@ is a type of variables, @a@ is a base field.
--
Expand Down

0 comments on commit 26396af

Please sign in to comment.