# 256-bit and 512-bit integers

**URL:** <https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064>\
**Category:** language design\
**Created:** [July 23, 2021, 8:34am UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064 "2021-07-23T08:34:30Z")\
**Posts on this page:** 12\
**Page:** 3

<div class="post-metadata">

**Author:** ![Jon-Davis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jon-davis/32/5453_2.png) [@Jon-Davis](https://internals.rust-lang.org/u/Jon-Davis)\
**Post date:** [August 2, 2021, 9:55pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/43 "2021-08-02T21:55:16Z")

</div>

Just a quick mention of some prior art:

[zig](https://ziglang.org/documentation/0.8.0/#Primitive-Types) uses arbitrary width integers:

```rust
const byte: u8 = 8;
const ascii: u7 = 7;
const big: u512 = 512;
const max_int: i65535 = 65535;
const PackedStruct = struct {
   packed1: u38,
   packed2: u17,
   packed3: u9,
};

```

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [August 3, 2021, 5:07pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/44 "2021-08-03T17:07:59Z")

</div>

> [@SkiFire13](#):
>
> In practice you'll likely get a compile time error anyway because it's impossible to store the type of that expression. That or the compiler hangs until it runs out of memory.

Not sure about the type, but the compiler running out of memory is a hard upper limit! 😉

---

<div class="post-metadata">

**Author:** ![eko](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@eko](https://internals.rust-lang.org/u/eko)\
**Post date:** [August 5, 2021, 8:46pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/45 "2021-08-05T20:46:45Z")

</div>

> [@anon32976453](#):
>
> and add software emulation which makes it a no longer a zero cost abstraction.

How is it not a zero cost abstraction? We just need a good enough software emulation implemented in the compiler that you can't practically beat by writing the emulation yourself in the Rust code.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [August 6, 2021, 7:38am UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/46 "2021-08-06T07:38:48Z")

</div>

> [@eko](#):
>
> How is it not a zero cost abstraction?

I'm guessing @anon32976453 meant zero-cost in the way eg newtype are zero-cost: they don't exist at runtime and so cost nothing at runtime. Using that definition emulation Indeed isn't zero cost.

---

<div class="post-metadata">

**Author:** ![eko](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@eko](https://internals.rust-lang.org/u/eko)\
**Post date:** [August 6, 2021, 11:23am UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/47 "2021-08-06T11:23:47Z")

</div>

By that definition adding two `i32` is not zero cost either, the computation takes a while at runtime. I can't see how this definition is useful while discussing proper hardware support for integer types. If the hardware doesn't directly supports 256 byte integer operations the only way of performing them is some kind of emulation.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [August 6, 2021, 12:54pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/48 "2021-08-06T12:54:09Z")

</div>

Bignum arithmetic is challenging to optimize, even just on integers, and it often needs to be re-tuned for each processor generation. I invite you to peruse [GMP developers' corner](https://gmplib.org/devel/) to get an idea of how much work it can be. Pay attention to the last-modification dates on each page; GMP is chronically understaffed.

Having bignums built into the language is a tempting idea, but I'm leery of dumping this much additional work on the stdlib team.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [August 6, 2021, 1:01pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/49 "2021-08-06T13:01:35Z")

</div>

The generally accepted definition of a "zero cost abstraction" or a "zero overhead abstraction" is one where you do not pay for what you don't use and it is not possible to write it more efficiently by hand.

---

<div class="post-metadata">

**Author:** ![eko](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@eko](https://internals.rust-lang.org/u/eko)\
**Post date:** [August 6, 2021, 1:17pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/50 "2021-08-06T13:17:48Z")

</div>

> [@zackw](#):
>
> Bignum arithmetic is challenging to optimize, even just on integers, and it often needs to be re-tuned for each processor generation.

No doubt it is. I kind of assumed LLVM would already have some optimizations around arbitrary size integers since I remember reading that LLVM IR supports integer types in the form of `i[0-9]+`. Obviously I don't have much knowledge about how all of that plays out in the generated code.

> [@Nokel81](#):
>
> The generally accepted definition of a "zero cost abstraction" or a "zero overhead abstraction" is one where you do not pay for what you don't use and it is not possible to write it more efficiently by hand.

I know, this is the reason I wrote my first reply.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [August 6, 2021, 10:12pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/51 "2021-08-06T22:12:46Z")

</div>

Split this topic on August 6, 2021, 10:12pm UTC

12 posts were split to a new topic: [Glob imports can shadow built-in types](https://internals.rust-lang.org/t/glob-imports-can-shadow-built-in-types/15151)

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [August 6, 2021, 3:33pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/55 "2021-08-06T15:33:00Z")

</div>

> [@H2CO3](#):
>
> There should instead be a single, `const` generic, preferably non-heap-allocating, arbitrary-width integer type for those who want to do big math.

FWIW RustCrypto has been working on a crate like that:

[https://github.com/RustCrypto/utils/tree/master/crypto-bigint](https://github.com/RustCrypto/utils/tree/master/crypto-bigint)

As a general observation it's tricky. Many algorithms (e.g. for modular arithmetic) need direct access to "limbs" and are optimized for certain limb sizes. Effectively leveraging things like SIMD in the context of such algorithms can be quite nuanced.

I wouldn't necessarily be opposed to there being a general purpose type for this in `core`/`std`, but IMO I probably wouldn't use it and would expect crates can do a better job entirely at the library level.

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [August 9, 2021, 1:38pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/56 "2021-08-09T13:38:30Z")

</div>

@Tom-Phinney suggested to me that I should drop the proposal of reserving `(i|u|f)(8|16|32|64|128|size)` in a PM (that he graciously allowed me to reference here to the group as a whole); I'm inclined to agree with him, as others have shown that there are good reasons to keep the current behavior. However, since @mbrubeck split this topic and created the [Glob imports can shadow built-in types](https://internals.rust-lang.org/t/glob-imports-can-shadow-built-in-types/15151), I'll post my thoughts on shadowing over there, and limit my comments here to be about bit integers.

@bascule reminded me of a serious issue concerning larger numbers that I'd completely forgotten about; the 'best' algorithm to use for a big integer library depends on the values that are being computed, as well as the environment and conditions that they're being computed within. A pure scientific application might only care about speed, but cryptographic applications may need to do a tradeoff between speed and designs that defeat sidechannel attacks that leak the keys, while embedded systems that are operating on battery might choose slower, but less power-hungry implementations to increase their battery life. Selecting which algorithm to use (including the choice of using hardware-provided operations, or emulating them in software) requires domain knowledge that we as rust-internals members lack.

To make things worse, these choices may be mixed within the same code. One part of the code may be handling cryptographic secrets, while another part might be doing pure science, so we can't really select a single algorithm.

Long story short, I'm now convinced that the only practical way forwards would be via crates that the user can select amongst.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 7, 2021, 1:39pm UTC](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064/57 "2021-11-07T13:39:14Z")

</div>

Closed on November 7, 2021, 1:39pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/256-bit-and-512-bit-integers/15064.md?page=2)
