Skip to content

Commit

Permalink
add tests and get rid of brorand
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Sep 30, 2021
1 parent c5d7a84 commit d47d5df
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

ko_fi: v1rtl
liberapay: v1rtl
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Run tests
run: deno test --coverage=coverage
- name: Create coverage report
run: deno coverage ./coverage --lcov > coverage.lcov
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./coverage.lcov
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
<div align="center">

# miller_rabin

[![GitHub Workflow Status][gh-actions-img]][github-actions]
[![Coverage][cov-badge]][cov] [![][docs-badge]][docs] [![][code-quality-img]][code-quality]

</div>

Modern rewrite of [miller-rabin](https://github.com/indutny/miller-rabin).

```ts
import { MillerRabin } from 'https://deno.land/x/miller_rabin/mod.ts'

const md = new MillerRabin()
```

[docs-badge]: https://img.shields.io/github/v/release/deno-libs/miller_rabin?label=Docs&logo=deno&style=for-the-badge&color=black
[docs]: https://doc.deno.land/https/deno.land/x/miller_rabin/mod.ts
[gh-actions-img]: https://img.shields.io/github/workflow/status/deno-libs/miller_rabin/CI?style=for-the-badge&logo=github&label=&color=black
[cov]: https://coveralls.io/github/deno-libs/miller_rabin
[github-actions]: https://github.com/deno-libs/miller_rabin/actions
[cov-badge]: https://img.shields.io/coveralls/github/deno-libs/miller_rabin?style=for-the-badge&color=black&
[code-quality-img]: https://img.shields.io/codefactor/grade/github/deno-libs/miller_rabin?style=for-the-badge&color=black&
[code-quality]: https://www.codefactor.io/repository/github/deno-libs/miller_rabin
29 changes: 11 additions & 18 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
import { default as brorand, Rand } from 'https://cdn.skypack.dev/brorand'
import { BN } from 'https://deno.land/x/bn_deno@1.0.0/lib/bn.js'

export class MillerRabin {
rand: any
constructor(rand?: typeof Rand) {
this.rand = rand || new brorand.Rand()
}

static create(rand: typeof Rand) {
return new MillerRabin(rand)
}

_randbelow(n: BN) {
#randbelow(n: BN) {
const len = n.bitLength()
const min_bytes = Math.ceil(len / 8)

// Generage random bytes until a number less than n is found.
// This ensures that 0..n-1 have an equal probability of being selected.
let a: BN
do a = new BN(this.rand.generate(min_bytes))
while (a.cmp(n) >= 0)
do {
const arr = new Uint8Array(min_bytes)
self.crypto.getRandomValues(arr)
a = new BN(arr)
} while (a.cmp(n) >= 0)

return a
}

_randrange(start: BN, stop: BN): BN {
#randrange(start: BN, stop: BN): BN {
// Generate a random number greater than or equal to start and less than stop.
const size = stop.sub(start)
return start.add(this._randbelow(size))
return start.add(this.#randbelow(size))
}

test(n: BN, k: number, cb: (a: any) => void): boolean {
test(n: BN, k?: number, cb?: (a: any) => void): boolean {
const len = n.bitLength()
const red = BN.mont(n)
const rone = new BN(1).toRed(red)
Expand All @@ -46,7 +39,7 @@ export class MillerRabin {

const prime = true
for (; k > 0; k--) {
const a = this._randrange(new BN(2), n1)
const a = this.#randrange(new BN(2), n1)
if (cb) cb(a)

let x = a.toRed(red).redPow(d)
Expand Down Expand Up @@ -80,7 +73,7 @@ export class MillerRabin {
const rn1 = n1.toRed(red)

for (; k > 0; k--) {
const a = this._randrange(new BN(2), n1)
const a = this.#randrange(new BN(2), n1)

const g = n.gcd(a)
if (g.cmpn(1) !== 0) return g
Expand Down
25 changes: 25 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it, expect, run } from 'https://deno.land/x/tincan@0.2.2/mod.ts'
import { BN } from 'https://deno.land/x/bn_deno@1.0.0/lib/bn.js'
import { MillerRabin } from './mod.ts'

describe('MillerRabin', () => {
it('should test number for primality', function () {
const mr = new MillerRabin()

expect(!mr.test(new BN(221))).toBe(true)
expect(mr.test(new BN(257))).toBe(true)

const p = new BN(
'dba8191813fe8f51eaae1de70213aafede8f323f95f32cff' +
'8b64ebada275cfb18a446a0150e5fdaee246244c5f002ce0' +
'aca97584be1745f2dd1eea2849c52aac8c4b5fb78a1c4da7' +
'052774338d3310a6e020c46168cb1f94014e9312511cc4fb' +
'79d695bb732449f0e015745b86bfa371dc6ca7386e9c7309' +
'5549c2e4b8002873',
16
)
expect(mr.test(p)).toBe(true)
})
})

run()

0 comments on commit d47d5df

Please sign in to comment.