#ethereum #u256 #eth-types

un-prim

Custom primitive types

3 releases

0.1.2 Sep 6, 2022
0.1.1 Sep 1, 2022
0.1.0 Sep 1, 2022

#3 in #u256


Used in owl-crypto

GPL-3.0-or-later

44KB
1K SLoC

UN-PRIM

build License: GPL v3 crates.io

Unusual primitive types for any rust project.

This crate was unstable and maybe lot's of bugs and I suggest not to use this.

Usage

[dependencies]
un-prim = "0.1"
use un_prim::*;

let x: U256 = 100u128.into();
let y: U256 = 2u128.into();
let z = x * y;

assert_eq!(200u64, z.into());

Macro

To create new type, you can use macro define!.

use un_prim::*;

define!(U512, 64, "512-bits custom type.");

let x: U512 = 100u8.into();

assert_eq!(x * 2u8.into(), 200u8.into());

TODO

  • Add Pow/Exp method for each types.
  • Add SQRT method for each types.

lib.rs:

unprim contains primitive types from 8 into 256 bit. it is unstable and not intended for production use.

use un_prim::*;

let a = U256::from(100);
let b = U256::from(2);

assert_eq!(a * b, 200u64.into());

Or you can use .into() method to init the types.

use un_prim::*;

let a: U24 = 100u64.into();
let b: U24 = 2u64.into();
let c: u32 = (a * b).into();

assert_eq!(c, 200);

You can use macro to define new types. In example if you want to define a type with 512 bit, you can use the macro.


use un_prim::*;

define!(U512, 64, "512 bit");

let a = U512::from(100);
let b = U512::from(2);
let c = a * b;
assert_eq!(c, 200u64.into());

Dependencies

~100–335KB