# Reject text: IC0503: Canister trapped explicitly: bigint function error

**URL:** <https://forum.dfinity.org/t/reject-text-ic0503-canister-trapped-explicitly-bigint-function-error/26937>\
**Category:** Developers\
**Created:** [January 24, 2024, 7:07am UTC](https://forum.dfinity.org/t/reject-text-ic0503-canister-trapped-explicitly-bigint-function-error/26937 "2024-01-24T07:07:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [January 24, 2024, 7:07am UTC](https://forum.dfinity.org/t/reject-text-ic0503-canister-trapped-explicitly-bigint-function-error/26937/1 "2024-01-24T07:07:06Z")

</div>

I’m attempting (in Motoko) to decompress the compressed public key that is returned from the `ecdsa_public_key` function within the management canister interface. As far as I know, this is a necessary step in order to be able to authenticate an HTTP request with an ECDSA signature.

To achieve this decompression, I’m using the method referenced by @LightningLad91 which can be found [here](https://forum.dfinity.org/t/error-failed-to-authenticate-request-due-to-invalid-signature-invalid-public-key-malformed-placeholder-public-key-error-error-in-der-encoding-invalid-length-of-bit-string-74/26869/8).

This method requires me to perform exponentiation in `modulo p` : (a\*\*b) % p

where `a`’s value is 77 digits long, `b`’s value is 77 digits long and `p`’s value is 78 digits long.  
as far as I know, theres no library available in motoko that would allow me to perform this exponentiation in a manner that is Space efficient. The only way (that I’m aware of) for exponentiating in `modulo p` is to perform the exponentiation and then take the remainder `modulo p` of the result of said exponentiation.

the issue with this approach is that the result of the exponentiation of such large numbers results in a number too large to fit within the canister’s storage and its causing the canister to trap.

My question is, are there any library functions that would allow me to be able to perform this exponentiation in modulo p in a space efficient way that won’t cause the canister to blow up?

also, is there another way to decompress the public the `ecdsa_public_key`?

also, is it even necessary to decompress the `ecdsa_public_key`? I don’t suppose it would be possible to perform the signature authentication using the compressed key, would it?

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [January 24, 2024, 8:24am UTC](https://forum.dfinity.org/t/reject-text-ic0503-canister-trapped-explicitly-bigint-function-error/26937/2 "2024-01-24T08:24:53Z")

</div>

I found some python code that performs exponentiation in `modulo p` efficiently. I’m putting it here for the next person who needs it when the time comes:

```auto
def pow_mod(x, y, z):
    "Calculate (x ** y) % z efficiently."
    number = 1
    while y:
        if y & 1:
            number = number * x % z
        y >>= 1
        x = x * x % z
    return number

```

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [January 24, 2024, 3:02pm UTC](https://forum.dfinity.org/t/reject-text-ic0503-canister-trapped-explicitly-bigint-function-error/26937/3 "2024-01-24T15:02:14Z")

</div>

Here is how one can perform exponentiation in `modulo P` where the base, exponent, and modulus are all ridiculously large natural numbers, without encountering the `bigint function error`, in the motoko langauge:

```auto
public func pow_mod(base: Nat, exponent: Nat, modulus: Nat ) : Nat {
    var result: Nat = 1;
    var base_ = base;
    var exponent_ = exponent;

    base_ := base_ % modulus;
    while (exponent_ > 0){
      if(exponent_ % 2 == 1) result := (result * base_) % modulus;
      exponent_ := exponent_ / 2;
      base_ := (base_ * base_) % modulus
    };
    return result;
  };

```

---

<div class="post-metadata">

**Author:** ![LightningLad91](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lightninglad91/32/5373_2.png) [@LightningLad91](https://forum.dfinity.org/u/LightningLad91)\
**Post date:** [January 24, 2024, 3:32pm UTC](https://forum.dfinity.org/t/reject-text-ic0503-canister-trapped-explicitly-bigint-function-error/26937/4 "2024-01-24T15:32:17Z")

</div>

Awesome, thanks @Jesse!
