user2335: I was trying to implement some El Gamal cryptography in plutus, basic verification of statements. But while trying to implement an efficient modular exponential function I noticed that the current PlutusTx.Prelude does not even provide a simple power function (^). Digging even deeper I saw that the PlutusTx.Prelude only povides an even function and not even an odd function. How can I contribute to add these function to PlutusTx.Prelude?
Is my implementation correct? I import Plutus.Prelude as Prel. Do I also need to add the template haskell inlinable pragma if I make this a library?
power :: Integer -> Integer -> Integer
power x n |(n==1) = x
        | Prel.even n       = (power x ( Prel.divide n 2))*(pow x ( Prel.divide n 2)) 
        | Prel.even (n-1)   = x * (power x (n-1))

expMod :: Integer -> Integer -> Integer -> Integer
expMod b e m
    | b == 1    = b
    | e == 0    = 1
    | e == 1    = b `Prel.modulo` m
    | even e    = let p = expMod b (e `Prel.divide` 2) m `Prel.modulo` m
                   in (power p (2::Integer)) `Prel.modulo` m
    | otherwise = (b * expMod b (e-1) m) `Prel.modulo` m

which seems to compile without the pragma in a validator.

user5617: Try this function:
power x n
    | n == 0  =  1
    | x == 0  =  0
    | even n = ( power x (n 'div' 2) ) * ( power x (n 'div' 2) )
    | otherwise n = x * ( power x ((n - 1) 'div' 2)) * ( power x ((n - 1) 'div' 2) )

Look at the type signature for even:
even :: (Integral a) => a -> Bool.
odd has a similar signature.
So you can see that even and odd take any type of the Integral typeclass and evaluate it. Then look at the signature for /:
(/) :: (Fractional a) => a -> a -> a.
/ takes only Fractional types, not Integral ones. It's impossible for one type to be evaluated by both those functions. In this case, there's an easy fix: use div instead of /.
Note that if you use div as an infix function (place it between the arguments) you'll need to put backticks around div.

2335: <p>I was trying to implement some El Gamal cryptography in plutus, basic verification of statements. But while trying to implement an efficient modular exponential function I noticed that the current <code>PlutusTx.Prelude</code> does not even provide a simple power function <code>(^)</code>. Digging even deeper I saw that the <code>PlutusTx.Prelude</code> only povides an <code>even</code> function and not even an <code>odd</code> function. How can I contribute to add these function to <code>PlutusTx.Prelude</code>?</p>
<p>Is my implementation correct? I import <code>Plutus.Prelude</code> as <code>Prel</code>. Do I also need to add the template haskell <code>inlinable</code> pragma if I make this a library?</p>
<pre><code>power :: Integer -&gt; Integer -&gt; Integer
power x n |(n==1) = x
        | Prel.even n       = (power x ( Prel.divide n 2))*(pow x ( Prel.divide n 2)) 
        | Prel.even (n-1)   = x * (power x (n-1))

expMod :: Integer -&gt; Integer -&gt; Integer -&gt; Integer
expMod b e m
    | b == 1    = b
    | e == 0    = 1
    | e == 1    = b `Prel.modulo` m
    | even e    = let p = expMod b (e `Prel.divide` 2) m `Prel.modulo` m
                   in (power p (2::Integer)) `Prel.modulo` m
    | otherwise = (b * expMod b (e-1) m) `Prel.modulo` m
</code></pre>
<p>which seems to compile without the pragma in a validator.</p>
 5617: <p>Try this function:</p>
<pre><code>power x n
    | n == 0  =  1
    | x == 0  =  0
    | even n = ( power x (n 'div' 2) ) * ( power x (n 'div' 2) )
    | otherwise n = x * ( power x ((n - 1) 'div' 2)) * ( power x ((n - 1) 'div' 2) )
</code></pre>
<p>Look at the type signature for <code>even</code>:
<code>even :: (Integral a) =&gt; a -&gt; Bool</code>.
<code>odd</code> has a similar signature.</p>
<p>So you can see that <code>even</code> and <code>odd</code> take any type of the <code>Integral</code> typeclass and evaluate it. Then look at the signature for <code>/</code>:
<code>(/) :: (Fractional a) =&gt; a -&gt; a -&gt; a</code>.</p>
<p><code>/</code> takes only <code>Fractional</code> types, not <code>Integral</code> ones. It's impossible for one type to be evaluated by both those functions. In this case, there's an easy fix: use <code>div</code> instead of <code>/</code>.</p>
<p>Note that if you use <code>div</code> as an infix function (place it between the arguments) you'll need to put backticks around <code>div</code>.</p>
