Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use an array backend Part 1 #104

Merged
merged 26 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
de87739
Implement types and uint128 primitives
mratsim Jun 12, 2020
36cc2b2
Implement comparison
mratsim Jun 12, 2020
cbbffe4
reimplement bitwise
mratsim Jun 12, 2020
206ffa9
Implement multiprecision addition / substraction
mratsim Jun 12, 2020
a0dec54
Implement multiplication
mratsim Jun 12, 2020
7f6c588
Passing addition tests (however simple bitwise ops crash the int128 V…
mratsim Jun 12, 2020
2ac1ee3
Fix compiletime primitives to pass all bitwise tests except large shifts
mratsim Jun 13, 2020
777a84e
Implement toHex/fromHex and fix `shl`
mratsim Jun 13, 2020
195480d
passing compile-time bitwise tests (but not runtime :?)
mratsim Jun 13, 2020
3df7f38
Fix noInit issue at runtime, pass the bitwise tests
mratsim Jun 13, 2020
59bca47
Fix comparison operators
mratsim Jun 13, 2020
254d4da
Pass extended precision bitops2 tests
mratsim Sep 6, 2020
dc9e0a4
Implement exponentiation, test mul, split mul/div tests
mratsim Sep 6, 2020
dd3ab71
For division we need internal add/sub/shift/bitwise so create interna…
mratsim Feb 21, 2021
f952314
dump progress
mratsim Aug 6, 2021
c2ed8a4
stash div refactor
mratsim Jan 12, 2022
53d2fd1
uint division - compile and pass the single limb tests
mratsim Jan 22, 2022
7efa248
Division/modulo implemented - pass property-based testing vs ttmath
mratsim Jan 23, 2022
4660dfe
Use littleEndian for limb-endianness: bigEndian arch are very rare, u…
mratsim Jan 23, 2022
27e9c9e
Add randomized testing, harden against edge cases
mratsim Jan 25, 2022
bff69b3
Add randomized testing vs GMP
mratsim Jan 31, 2022
63a3212
rebase and try to make it works with clients
jangko Jun 9, 2023
0dc6afe
let the tests compileable and run
jangko Jun 12, 2023
e5c352f
fix stuint constructor
jangko Jun 13, 2023
7ce5364
disable dot borrow temporary
jangko Jun 13, 2023
8c5a964
nimvm workaround for primitives
jangko Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Main focus:
- Uint2048 for Ethereum Bloom filters
- Ease of use:
- Use traditional `+`, `-`, `+=`, etc operators like on native types
- Representation of numbers in memory is the exact same as native types and endianness aware.
- In practice that means that interfacing with binary blobs representing numbers from cryptographic libraries can be done with a `cast` if it represents a Uint256, Uint512, Uint1024, Uint2048.
- 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

Expand Down
91 changes: 91 additions & 0 deletions benchmarks/bench.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import ../stint, std/[times, monotimes]

template bench(desc: string, body: untyped) =
let start = getMonotime()
body
let stop = getMonotime()
echo desc,": ", inMilliseconds(stop-start), " ms"

# Warmup on normal int to ensure max CPU freq
# Complex enough that the compiler doesn't optimize it away

proc warmup() =
var foo = 123
bench "Warmup":
for i in 0 ..< 10_000_000:
foo += i*i mod 456
foo = foo mod 789

warmup()
####################################

let a = [123'u64, 123'u64, 123'u64, 123'u64]
let m = [456'u64, 456'u64, 456'u64, 45'u64]

proc add_stint(a, m: array[4, uint64]) =
let aU256 = cast[Stuint[256]](a)
let mU256 = cast[Stuint[256]](m)

bench "Add (stint)":
var foo = aU256
for i in 0 ..< 100_000_000:
foo += mU256
foo += aU256

proc mul_stint(a, m: array[4, uint64]) =
let aU256 = cast[Stuint[256]](a)
let mU256 = cast[Stuint[256]](m)

bench "Mul (stint)":
var foo = aU256
for i in 0 ..< 100_000_000:
foo += (foo * foo)

proc mod_stint(a, m: array[4, uint64]) =
let aU256 = cast[Stuint[256]](a)
let mU256 = cast[Stuint[256]](m)

bench "Mod (stint)":
var foo = aU256
for i in 0 ..< 100_000_000:
foo += (foo * foo) mod mU256

add_stint(a, m)
mul_stint(a, m)
mod_stint(a, m)

when defined(bench_ttmath):
# need C++
import ttmath, ../tests/ttmath_compat

proc add_ttmath(a, m: array[4, uint64]) =
let aU256 = a.astt()
let mU256 = m.astt()

bench "Add (ttmath)":
var foo = aU256
for i in 0 ..< 100_000_000:
foo += mU256
foo += aU256

proc mul_ttmath(a, m: array[4, uint64]) =
let aU256 = a.astt()
let mU256 = m.astt()

bench "Mul (ttmath)":
var foo = aU256
for i in 0 ..< 100_000_000:
foo += (foo * foo)

proc mod_ttmath(a, m: array[4, uint64]) =
let aU256 = a.astt()
let mU256 = m.astt()

bench "Mod (ttmath)":
var foo = aU256
for i in 0 ..< 100_000_000:
foo += (foo * foo) mod mU256

add_ttmath(a, m)
mul_ttmath(a, m)
mod_ttmath(a, m)
55 changes: 0 additions & 55 deletions benchmarks/bench_mod.nim

This file was deleted.

Loading