Giter Site home page Giter Site logo

nim-tommath's Introduction

nim-tommath

An High-Level wrapper of libtommath written in Nim.

import pkg/tommath
    
let a = initBigInt(10) # Load from regular int
let b = initBigInt("A", 16) # Load from string (Hex)
    
echo a + b # add
echo a * b # multiply
    
echo initBigInt("-99946744073709551615").toString(2) # Load from string (Decimal) and print as binary

Features

  • Portable: This library compiles libtommath directly, so the end-user doesn't need to install it on the system.
  • Open License: Unlike GMP, libtommath is under the public domain, which makes the distribution process less complicated.
  • Performant: While there are native implementations of Big Integers in Nim, they are not fully optimized. This becomes important when we want to deal with very large numbers (2048+bits). This library instead uses fast algorithms that reduce the complexity of operations.
  • Easy to use: An high-level interface is provided by default, which makes libtommath much easier to use.

Installing

Use nimble to install the library:

nimble install https://github.com/dadadani/nim-tommath

Accesing the low-level API

If you want to access the low-level API, you can use the raw module:

import pkg/tommath/raw

var mp: MpInt
discard mp_init_i64(addr mp, 2342543)

var mp2: MpInt
discard mp_init_i64(addr mp2, 1000000)

var mp3: MpInt
discard mp_init(addr mp3)

discard mp_add(addr mp, addr mp2, addr mp3)

var outp: string

var size: csize_t
discard mp_radix_size(addr mp3, 10, addr size)

outp.setLen(size-1)
discard mp_to_radix(addr mp3, cstring(outp), cint(size), nil, 10)

echo outp

mp_clear(addr mp)
mp_clear(addr mp2)
mp_clear(addr mp3)

Dynamic Linking

If you want to use the system's libtommath, compile your program with -d:useExternalTommath:

nim c -d:useExternalTommath myprogram.nim

Contributing

This wrapper is rather incomplete, so feel free to open pull requests if you are interested!

nim-tommath's People

Contributors

dadadani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.