<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>

CC BY-SA 4.0
2022-03-22T20:58:10.870Z
Thank you for correcting my bad cases, but note that the use of odd here is not allowed since `PlutusTx.Prelude` does not have this.
Fermat
CC BY-SA 4.0
2022-03-22T21:03:48.797Z
Well in this case should `otherwise` (instead of `odd`) also work, but an `odd` would be also great. 
Andreas Hauser
CC BY-SA 4.0
2022-03-22T20:51:19.703Z
2022-03-22T21:01:39.747Z
2022-03-22T21:01:39.747Z
Andreas Hauser
<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>

<plutus><plutus-pioneer-program><smart-contract>
How to take powers in validator
# How to take powers in validator

(Asked by: username_0 on 2022-03-22)

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.

**Tags:** plutus, plutus-pioneer-program, smart-contract

---

## 1 answers

### Answer by username_1 on 2022-03-22. Score: 1

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.

> **Comments:**
> **username_0**: Thank you for correcting my bad cases, but note that the use of odd here is not allowed since `PlutusTx.Prelude` does not have this.  
> **username_1**: Well in this case should `otherwise` (instead of `odd`) also work, but an `odd` would be also great.   

---

