ERC-5850 bytes32 complex number storage standard (Complex Numbers stored in `bytes32` types)

Stagnant · Other · created 2022-10-29

A standard that splits a complex number's real and imaginary parts in half within a single bytes32, allowing complex numbers to be stored and exchanged without a separate struct.

Why it is needed

To perform computations requiring complex numbers in smart contracts, such as Fourier transforms, AC circuits, or the Navier-Stokes equations, a way to store the real and imaginary parts is needed. However, Solidity has no complex number type, so developers have had to improvise by using a struct with two separate variables. This doubles storage usage and raises gas costs, and because storage order and method differ across libraries, they are incompatible with each other. It also meant that widely used code patterns, such as mappings keyed by bytes32, could not be reused for complex numbers. This standard solves this by simply fixing where and in what order the real and imaginary parts go.

What to implement

In depth

Rule for dividing the storage slot

A bytes32 is 32 bytes (256 bits) in size. This standard specifies splitting it exactly in half, placing the real part in the least significant 16 bytes and the imaginary part in the most significant 16 bytes. This standard only defines the split and placement, operations such as addition or multiplication of complex numbers, decimal precision, and the like are out of scope and left to other discussions.

How cnNew works

cnNew takes a real part (_Real) and an imaginary part (_Imag), both of type int128, as input. When each is converted to bytes16 and assigned to a bytes32, Solidity's type conversion rules place that 16 byte value in the most significant position and fill the lower 16 bytes with zeros. The Real32 value made from the real part is right shifted by 128 bits (>> 128) to move it into the lower 16 byte slot, then combined with Imag32, the imaginary part, via an OR operation to produce the final bytes32 complex number value.

How RealIm works

RealIm takes a complex number value (_cn) combined into bytes32 as input and splits it back into real and imaginary parts. Using assembly, it stores the _cn value into two positions of the memory array tmp, then converts each 16 byte chunk to int128 and returns them as Real and Imag respectively.

Applicable representation schemes

This standard uses the cartesian representation as an example, but explains that it can also be applied to complex number representations in polar or exponential form. The only requirements are that the real and imaginary parts (or the two corresponding components) are each given an equal 16 byte space, and that their order is clearly defined.

Why decimal representation is not mandated

Solidity does not yet fully support fixed point or floating point numbers. For this reason, this standard does not specify a particular decimal representation for complex numbers, and instead leaves it open to use any numeric representation that fits within the 16 byte space. The original text mentions, as reference, fixed point libraries like prb-math, which use 18 decimal places, and floating point libraries like abdk.

Relationship to existing bytes32 usage

Since this standard does not change the meaning of the bytes32 type itself, it has no effect on existing uses of bytes32 that are not complex numbers. A library handling complex number operations can be implemented using the `using Complex for bytes32` syntax, where Complex is the name of that library.

Progress status

This proposal was written in October 2022 and is currently in Stagnant status, no longer being actively progressed.

Things to watch out for

Original ERC · ← ERCs