{-# LINE 1 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
module Data.Number.Flint.NMod.FFI (
NMod (..)
, CNMod (..)
, newNMod
, withNMod
, withNewNMod
, nmod_init
, _nmod_add
, nmod_add
, _nmod_sub
, nmod_sub
, nmod_neg
, nmod_mul
, _nmod_mul_fullword
, nmod_inv
, nmod_div
, nmod_pow_ui
, NModDiscreteLogPohligHellman (..)
, CNModDiscreteLogPohligHellman (..)
, nmod_discrete_log_pohlig_hellman_init
, nmod_discrete_log_pohlig_hellman_clear
, nmod_discrete_log_pohlig_hellman_precompute_prime
, nmod_discrete_log_pohlig_hellman_primitive_root
, nmod_discrete_log_pohlig_hellman_run
) where
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Ptr ( Ptr, FunPtr, plusPtr, nullPtr )
import Foreign.Storable
import Data.Number.Flint.Flint
data NMod = NMod {-# UNPACK #-} !(ForeignPtr CNMod)
data CNMod = CNMod CMpLimb CMpLimb CFBitCnt
instance Storable CNMod where
{-# INLINE sizeOf #-}
sizeOf :: CNMod -> Int
sizeOf CNMod
_ = (Int
24)
{-# LINE 56 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
{-# INLINE alignment #-}
alignment :: CNMod -> Int
alignment CNMod
_ = Int
8
{-# LINE 58 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
peek ptr = return CNMod
<*> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 60 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
{-# LINE 61 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
{-# LINE 62 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
poke = error "CNMod.poke: Not defined"
instance Show CNMod where
show :: CNMod -> [Char]
show (CNMod CMpLimb
n CMpLimb
ninv CMpLimb
norm) = forall a. Show a => a -> [Char]
show CMpLimb
n
data NModDiscreteLogPohligHellman =
NModDiscreteLogPohligHellman !(ForeignPtr CNModDiscreteLogPohligHellman)
type CNModDiscreteLogPohligHellman = CFlint NModDiscreteLogPohligHellman
instance Storable CNModDiscreteLogPohligHellman where
{-# INLINE sizeOf #-}
sizeOf :: CNModDiscreteLogPohligHellman -> Int
sizeOf CNModDiscreteLogPohligHellman
_ = (Int
56)
{-# LINE 76 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
{-# INLINE alignment #-}
alignment :: CNModDiscreteLogPohligHellman -> Int
alignment CNModDiscreteLogPohligHellman
_ = Int
8
{-# LINE 78 "src/Data/Number/Flint/NMod/FFI.hsc" #-}
peek ptr = error "CNModDiscreteLogPohligHellman poke: Not defined"
poke :: Ptr CNModDiscreteLogPohligHellman
-> CNModDiscreteLogPohligHellman -> IO ()
poke = forall a. HasCallStack => [Char] -> a
error [Char]
"CNMod.poke: Not defined"
newNMod :: CMpLimb -> IO NMod
newNMod CMpLimb
n = do
ForeignPtr CNMod
x <- forall a. Storable a => IO (ForeignPtr a)
mallocForeignPtr
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CNMod
x forall a b. (a -> b) -> a -> b
$ \Ptr CNMod
x -> Ptr CNMod -> CMpLimb -> IO ()
nmod_init Ptr CNMod
x CMpLimb
n
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ForeignPtr CNMod -> NMod
NMod ForeignPtr CNMod
x
{-# INLINE withNMod #-}
withNMod :: NMod -> (Ptr CNMod -> IO a) -> IO (NMod, a)
withNMod (NMod ForeignPtr CNMod
x) Ptr CNMod -> IO a
f = do
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CNMod
x forall a b. (a -> b) -> a -> b
$ \Ptr CNMod
xp -> Ptr CNMod -> IO a
f Ptr CNMod
xp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ForeignPtr CNMod -> NMod
NMod ForeignPtr CNMod
x,)
withNewNMod :: CMpLimb -> (Ptr CNMod -> IO a) -> IO (NMod, a)
withNewNMod CMpLimb
n Ptr CNMod -> IO a
f = do
NMod
x <- CMpLimb -> IO NMod
newNMod CMpLimb
n
forall {a}. NMod -> (Ptr CNMod -> IO a) -> IO (NMod, a)
withNMod NMod
x forall a b. (a -> b) -> a -> b
$ \Ptr CNMod
x -> Ptr CNMod -> IO a
f Ptr CNMod
x
foreign import ccall "nmod.h nmod_init"
nmod_init :: Ptr CNMod -> CMpLimb -> IO ()
foreign import ccall "nmod.h _nmod_add"
_nmod_add :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_add"
nmod_add :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h _nmod_sub"
_nmod_sub :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_sub"
nmod_sub :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_neg"
nmod_neg :: CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_mul"
nmod_mul :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h _nmod_mul_fullword"
_nmod_mul_fullword :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_inv"
nmod_inv :: CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_div"
nmod_div :: CMpLimb -> CMpLimb -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_pow_ui"
nmod_pow_ui :: CMpLimb -> CULong -> Ptr CNMod -> IO CMpLimb
foreign import ccall "nmod.h nmod_discrete_log_pohlig_hellman_init"
nmod_discrete_log_pohlig_hellman_init :: Ptr CNModDiscreteLogPohligHellman -> IO ()
foreign import ccall "nmod.h nmod_discrete_log_pohlig_hellman_clear"
nmod_discrete_log_pohlig_hellman_clear :: Ptr CNModDiscreteLogPohligHellman -> IO ()
foreign import ccall "nmod.h nmod_discrete_log_pohlig_hellman_precompute_prime"
nmod_discrete_log_pohlig_hellman_precompute_prime :: Ptr CNModDiscreteLogPohligHellman -> CMpLimb -> IO CDouble
foreign import ccall "nmod.h nmod_discrete_log_pohlig_hellman_primitive_root"
nmod_discrete_log_pohlig_hellman_primitive_root :: Ptr CNModDiscreteLogPohligHellman -> IO CMpLimb
foreign import ccall "nmod.h nmod_discrete_log_pohlig_hellman_run"
nmod_discrete_log_pohlig_hellman_run :: Ptr CNModDiscreteLogPohligHellman -> CMpLimb -> IO CULong