# Addmod / mulmod as built-in functions

**URL:** <https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973>\
**Category:** 🏄🏻‍♀️ Cadence\
**Tags:** cadence\
**Created:** [December 17, 2022, 3:24pm UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973 "2022-12-17T15:24:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![avcd](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/avcd/32/790_2.png) [@avcd](https://forum.flow.com/u/avcd)\
**Post date:** [December 17, 2022, 3:24pm UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/1 "2022-12-17T15:24:08Z")

</div>

I recently attempted a public key derivation of an elliptic curve using Cadence. This is part of an experimental art creation using smart contracts.  
Take a look at the GitHub repository below.

> **[GitHub - avcdsld/cadence-secp256k1](https://github.com/avcdsld/cadence-secp256k1)**
>
> Contribute to avcdsld/cadence-secp256k1 development by creating an account on GitHub.

In the process, I suffered from overflow constraints. I would prefer not to allow overflow/underflow in ordinary codes, but I thought there might be a certain demand for `addmod` / `mulmod` as built-in functions.

I realize that this will affect the security of the smart contract code, but at the same time, I think it will be an element of creativity in a great environment for resource oriented programming.

What do you think about this? Have you considered this in the past?

---

<div class="post-metadata">

**Author:** ![bastian](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bastian/32/183_2.png) [@bastian](https://forum.flow.com/u/bastian)\
**Post date:** [January 4, 2023, 11:47pm UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/2 "2023-01-04T23:47:07Z")

</div>

@avcd Awesome work!

If you want to perform over/underflowing arithmetic, you can use the `Word*` types instead of the `UInt*` types, see the bottom of the “Integers” section: [Values and Types | Flow Blockchain](https://developers.flow.com/cadence/language/values-and-types#integers)

---

<div class="post-metadata">

**Author:** ![tarak.by](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@tarak.by](https://forum.flow.com/u/tarak.by)\
**Post date:** [January 5, 2023, 7:50pm UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/3 "2023-01-05T19:50:55Z")

</div>

Nice work!

I believe modular arithmetic for 256 bits numbers could be implemented on Cadence as a little “library” (modAdd, modSub, modMul and modInv). That’s what Math libraries do based on the low level “limited” operations.  
The lib could hardcode some known modulos like the one you used from secp256k1, for a faster computation.

Btw multiplication on your code could nicely leverage the 128-bits or 64-bits operations provided by Cadence, by using Karatsuba algorithm. This should improve performance significantly.

---

<div class="post-metadata">

**Author:** ![avcd](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/avcd/32/790_2.png) [@avcd](https://forum.flow.com/u/avcd)\
**Post date:** [January 6, 2023, 2:08am UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/4 "2023-01-06T02:08:54Z")

</div>

Thank you for the info 😊  
If Word128 and Word256, which are even larger than Word64, were available…  
Is there a reason why Word256 was never created? (If known)

---

<div class="post-metadata">

**Author:** ![avcd](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/avcd/32/790_2.png) [@avcd](https://forum.flow.com/u/avcd)\
**Post date:** [January 6, 2023, 2:14am UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/5 "2023-01-06T02:14:00Z")

</div>

> [@tarak.by](#):
>
> I believe modular arithmetic for 256 bits numbers could be implemented on Cadence as a little “library” (modAdd, modSub, modMul and modInv). That’s what Math libraries do based on the low level “limited” operations.

This is exactly what I want!

> Karatsuba algorithm

Thanks for letting me know. This will make the calculations a lot smaller (but that’s still not a workable calculation 😂)

---

<div class="post-metadata">

**Author:** ![bastian](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bastian/32/183_2.png) [@bastian](https://forum.flow.com/u/bastian)\
**Post date:** [January 9, 2023, 9:34pm UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/6 "2023-01-09T21:34:51Z")

</div>

@avcd We would highly appreciate a PR adding such functionality directly to Cadence!

Adding the `Word128` and `Word256` types is mainly copy and pasting existing code (`UInt128` and `UInt256`). Adding the operations is little work, most of the effort is likely in testing it thoroughly.

We’re more than happy to assist you or anyone else who would like to add this!

---

<div class="post-metadata">

**Author:** ![bastian](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bastian/32/183_2.png) [@bastian](https://forum.flow.com/u/bastian)\
**Post date:** [March 23, 2023, 10:17pm UTC](https://forum.flow.com/t/addmod-mulmod-as-built-in-functions/3973/7 "2023-03-23T22:17:50Z")

</div>

Related Cadence issue: [https://github.com/onflow/cadence/issues/2389](https://github.com/onflow/cadence/issues/2389)

cc @avcd
