EIPs Explained

ERC-5850 · Stagnant · ERC · 2022-10-29

ERC-5850: Complex Numbers stored in `bytes32` types

Proposes packing a complex number into one bytes32, with the real part in the low 16 bytes and the imaginary part in the high 16 bytes, so libraries can share a common layout.

What problem does it solve?

Complex numbers show up in Fourier transforms, AC circuit models, signal processing and other scientific calculations. Solidity has no native complex type, so any contract or library that needs one invents its own representation: a struct, two separate variables, or some packing scheme. Different libraries then cannot pass values to each other.

ERC-5850 proposes one simple, shared layout: a complex number is a bytes32, split exactly in half.

How it works

  • The real part occupies the least significant 16 bytes.
  • The imaginary part occupies the most significant 16 bytes.
  • Each half holds a 128-bit value. The examples use int128 in Cartesian form, but the ERC says any number representation (fixed point, floating point, polar or exponential forms) is acceptable as long as each component fits in 16 bytes.

Because a complex value is a plain bytes32, it fits in a single storage slot, can be a mapping key, and can be passed around anywhere bytes32 is accepted. A positive real number below 2^127 maps directly onto the same bytes32 you would get from casting the integer.

The ERC deliberately defines only the layout. Arithmetic (addition, multiplication, etc.) and the choice of decimal precision are left to libraries, which could be attached with using Complex for bytes32.

Sample code

Packing and unpacking with plain bit operations (equivalent in effect to the spec's cnNew / RealIm helpers):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

library Complex {
    function pack(int128 re, int128 im) internal pure returns (bytes32) {
        return bytes32(
            (uint256(uint128(im)) << 128) | uint256(uint128(re))
        );
    }

    function unpack(bytes32 c) internal pure returns (int128 re, int128 im) {
        re = int128(uint128(uint256(c)));        // low 16 bytes
        im = int128(uint128(uint256(c) >> 128)); // high 16 bytes
    }

    // Example operation a library could provide (overflow-checked by 0.8 casts)
    function add(bytes32 a, bytes32 b) internal pure returns (bytes32) {
        (int128 ar, int128 ai) = unpack(a);
        (int128 br, int128 bi) = unpack(b);
        return pack(ar + br, ai + bi);
    }
}

contract Demo {
    using Complex for bytes32;

    function example() external pure returns (int128 re, int128 im) {
        bytes32 z = Complex.pack(3, -4);      // 3 - 4i
        bytes32 w = Complex.pack(1, 2);       // 1 + 2i
        return z.add(w).unpack();             // (4, -2)
    }
}

Things to know

  • Status: Stagnant. It has not progressed toward Final, and there is no sign of a widely used library built on it.
  • Do not convert the bytes32 straight to an integer to read the real part; a non-zero imaginary part or a negative real part will give a wrong result. Always unpack.
  • If you manipulate the raw bytes32 with assembly or unchecked math, you must handle overflow yourself, as the spec notes.
  • Precision and scaling (for example 18-decimal fixed point) are not specified, so two libraries using this layout could still disagree on what the numbers mean.

#math #solidity #encoding #library

Details

Authors
Paul Edge (@genkifs)
Status
Stagnant
Type
Standards Track / ERC
Created
2022-10-29

Machine-readable: Markdown · JSON