Skip to content

Stack-based arbitrary-precision integers - Fast and portable with natural syntax for resource-restricted devices.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHEv2
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

status-im/nim-stint

Repository files navigation

Stint (Stack-based arbitrary precision integers)

License: Apache License: MIT Stability: experimental Github action

stint provides efficient and convenient N-bit integers for Nim, for arbitrary sizes of N decided at compile time with an interface similar to to int64/uint64.

In addition to basic integer operations, stint also contains primtives for modular arithmetic, endian conversion, basic I/O, bit twiddling etc.

stint integers, like their intXX/uintXX counterpart in Nim are stack-based values, meaning that they are naturally allocation-free and have value-based semantics.

import stint

func addmul(a, b, c: UInt256): UInt256 =
  a * b + c

echo addmul(u256"100000000000000000000000000000", u256"1", u256"2")

Priorities

  • Portability
    • 32 and 64 bit
    • ARM/x86/x86_64 extensively tested
    • Additionally RISC-V and MIPS for open hardware and low power IoT devices.
  • Speed, library is carefully tuned to produce the best assembly given the current compilers. However, the library itself does not require assembly for portability.
  • No heap/dynamic allocation
  • Ease of use:
    • Use traditional +, -, +=, etc operators like on native types
    • converting to and from raw byte BigInts (also called octet string in IETF specs)
    • converting to and from Hex
    • converting to and from decimal strings

Non-priorities include:

  • constant-time operation (not suitable for certain kinds of cryptography out of the box)
  • runtime precision

See also

  • constantine - modular arithmetic and elliptic curve operations focusing on cryptography and constant-time implementation
  • N2472 - _ExtInt(N) - native arbitrary precision integers for C
  • stew - helpers and utilities for ordinary Nim integers (endians2, bitops2 etc)

License

Licensed and distributed under either of

or

at your option. This file may not be copied, modified, or distributed except according to those terms.