Skip to content

scalajs-io/bignum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bignum API for Scala.js

bignum - An arbitrary-precision integer arithmetic using OpenSSL.

Description

This library is based on node-bigint by substack, but instead of using libgmp, it uses the builtin bignum functionality provided by OpenSSL. The advantage is that OpenSSL is already part of Node.js, so this library does not add any external dependency whatsoever.

Build Requirements

Build/publish the SDK locally

 $ sbt clean publish-local

Running the tests

Before running the tests the first time, you must ensure the npm packages are installed:

$ npm install

Then you can run the tests:

$ sbt test

Examples

You can perform math functions via built-in methods:

import io.scalajs.npm.bignum._

val v1 = "782910138827292261791972728324982"
val v2 = "182373273283402171237474774728373"

val b = new BigNum(v1).add(500).sub(v2).div(8)
// b.toString == "75067108192986261319312244199638"

Alternatively, you can perform math functions via overloaded operators:

import io.scalajs.npm.bignum._

val v1 = "782910138827292261791972728324982"
val v2 = "182373273283402171237474774728373"

val b = (new BigNum(v1) + 500 - v2) / 8
// b.toString == "75067108192986261319312244199638"

Prime number detection:

import io.scalajs.npm.bignum._

val v1 = "782910138827292261791972728324982"
val v2 = "182373273283402171237474774728373"

for {
    n <- 0 to 100
    p = BigNum.pow(2, n) - 1 if p.probPrime(50)
} {
    val perfect = p.mul(BigNum.pow(2, n - 1))
    println(perfect.toString)
}

Artifacts and Resolvers

To add the BigNum binding to your project, add the following to your build.sbt:

libraryDependencies += "io.scalajs.npm" %%% "bignum" % "0.5.0"

Optionally, you may add the Sonatype Repository resolver:

resolvers += Resolver.sonatypeRepo("releases") 

About

Arbitrary-precision integer arithmetic using OpenSSL

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages