# BigInt powmod(b, e, m) or equivalent

**URL:** <https://forum.crystal-lang.org/t/bigint-powmod-b-e-m-or-equivalent/486>\
**Category:** Offtopic\
**Created:** [February 28, 2019, 6:36am UTC](https://forum.crystal-lang.org/t/bigint-powmod-b-e-m-or-equivalent/486 "2019-02-28T06:36:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [February 28, 2019, 6:36am UTC](https://forum.crystal-lang.org/t/bigint-powmod-b-e-m-or-equivalent/486/1 "2019-02-28T06:36:08Z")

</div>

i asked this question on the google list last year (July 2018) and got no response, so I’ll ask here again on the new forum.

I’m converting Ruby -\> Crystal.

In the Ruby **OPENSSL** library, when I **‘require openssl’** I can then use a bunch of fast modular math methods like.

`y = b.to_bn.mod_exp(d, self)`

In Crystal I have to do

```auto
def powmod(b, x, m)        
  r = 1.to_big_i; b = b.to_big_i        
  while x > 0          
    r = (r * b) % m if x.odd?          
    b = (b * b) % m          
    x >>= 1        
  end       
  r     
 end      

y = powmod(b, d, self) # y = (b**d) mod self`

```

Does Crystal have the equivalent methods as Ruby (docs don’t show them)?

Will it provide in std libs these modular math function that exist in Ruby in the future?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [March 3, 2019, 3:34pm UTC](https://forum.crystal-lang.org/t/bigint-powmod-b-e-m-or-equivalent/486/2 "2019-03-03T15:34:11Z")

</div>

Please open an issue in the GitHub tracker. It seems there’s a function to that in libgmp, which is what we are using for big numbers: [https://gmplib.org/manual/Integer-Exponentiation.html](https://gmplib.org/manual/Integer-Exponentiation.html)

You can request or even try sending PRs for any functions that we didn’t include in the libgmp wrapper.
