Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

paillier-bigint

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

paillier-bigint - npm Package Compare versions

Comparing version 3.1.2 to 3.2.1

dist/bundles/paillier-bigint.esm.js

99

package.json
{
"name": "paillier-bigint",
"version": "3.1.2",
"version": "3.2.1",
"description": "An implementation of the Paillier cryptosystem using native JS (ECMA 2020) implementation of BigInt",

@@ -22,28 +22,43 @@ "keywords": [

"repository": "github:juanelas/paillier-bigint",
"main": "./lib/index.node.js",
"browser": "./lib/index.browser.mod.js",
"types": "./types/index.d.ts",
"engines": {
"node": ">=10.4.0"
},
"types": "./dist/esm/types/index.d.ts",
"main": "./dist/cjs/index.node.cjs",
"browser": "./dist/esm/index.browser.js",
"module": "./dist/esm/index.node.js",
"exports": {
".": {
"node": {
"require": "./dist/cjs/index.node.cjs",
"import": "./dist/esm/index.node.js"
},
"default": "./dist/esm/index.browser.js"
},
"./bundles/": "./dist/bundles/",
"./types/": "./dist/esm/types/"
},
"directories": {
"build": "./build",
"lib": "./lib",
"dist": "./dist",
"docs": "./docs",
"src": "./src",
"test": "./test",
"types": "./types"
"test": "./test"
},
"engines": {
"node": ">=10.4.0"
},
"scripts": {
"test": "nyc --check-coverage mocha",
"coverage": "nyc report --reporter=lcov",
"build": "run-s lint build:js docs test:browser coverage",
"build:js": "rollup -c build/rollup.config.js",
"build:standard": "standard --fix",
"build:browserTests": "rollup -c build/rollup.tests.config.js",
"build:docs": "node build/build.docs.js",
"build:dts": "node build/build.dts.js",
"build": "run-s build:**",
"preversion": "npm run build && npm run test",
"postversion": "git push"
"clean": "rimraf .nyc_output .mocha-ts coverage dist docs",
"coverage": "nyc --check-coverage --exclude build --exclude '{src/ts/**/*.spec.ts,test/**/*.ts}' --reporter=text --reporter=lcov node ./build/bin/mocha-ts.js --require build/testing/mocha/mocha-init.js '{src/ts/**/*.spec.ts,test/**/*.ts}'",
"docs": "node build/build.docs.js",
"lint": "ts-standard --fix",
"mocha": "node ./build/bin/mocha-ts.js --require build/testing/mocha/mocha-init.js ",
"preversion": "run-s lint build:js test:browser coverage",
"postversion": "npm run docs",
"test": "run-s test:browser test:node",
"test:browser": "node build/testing/browser/index.js",
"test:node": "npm run mocha -- '{src/ts/**/*.spec.ts,test/**/*.ts}'",
"watch": "npm run mocha -- --watch '{src/ts/**/*.spec.ts,test/**/*.ts}'"
},
"standard": {
"ts-standard": {
"env": [

@@ -53,24 +68,38 @@ "mocha"

"globals": [
"BigInt"
"IS_BROWSER",
"browser",
"page",
"_pkg",
"chai"
],
"project": "./tsconfig.json",
"ignore": [
"/test/browser/",
"/lib/index.browser.bundle.iife.js",
"/lib/index.browser.bundle.mod.js"
"dist/**/*"
]
},
"devDependencies": {
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-multi-entry": "^3.0.1",
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.3",
"chai": "^4.2.0",
"jsdoc-to-markdown": "^5.0.3",
"mocha": "^7.2.0",
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-multi-entry": "^4.0.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@rollup/plugin-replace": "^2.4.1",
"@rollup/plugin-typescript": "^8.2.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.1",
"chai": "^4.3.3",
"glob": "^7.1.6",
"json5": "^2.2.0",
"minimatch": "^3.0.4",
"mocha": "^8.3.0",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"rollup": "^2.23.0",
"rollup-plugin-terser": "^5.3.0",
"standard": "^14.3.4",
"typescript": "^3.9.7"
"pirates": "^4.0.1",
"puppeteer": "^8.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.40.0",
"rollup-plugin-terser": "^7.0.2",
"ts-standard": "^10.0.0",
"tslib": "^2.1.0",
"typedoc": "^0.20.29",
"typedoc-plugin-markdown": "^3.6.0",
"typescript": "^4.2.2"
},

@@ -77,0 +106,0 @@ "dependencies": {

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
![Node CI](https://github.com/juanelas/paillier-bigint/workflows/Node%20CI/badge.svg)
[![Node CI](https://github.com/juanelas/paillier-bigint/workflows/Node%20CI/badge.svg)](https://github.com/juanelas/paillier-bigint/actions?query=workflow%3A%22Node+CI%22)
[![Coverage Status](https://coveralls.io/repos/github/juanelas/paillier-bigint/badge.svg?branch=master)](https://coveralls.io/github/juanelas/paillier-bigint?branch=master)
# paillier-bigint
# Skeleton for developing modules for browser and Node.js in Typescript
An implementation of the Paillier cryptosystem relying on the native JS implementation of BigInt.
> This entire section with all its subsections (Installation, Tooling, Scripts) should be removed from your `src/docs/index.md` after installing. The rest of sections may be useful for your package readme, and you may just modified them in `src/docs/index.md` to meet your needs.
It can be used by any [Web Browser or webview supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) and with Node.js (>=10.4.0). In the latter case, for multi-threaded primality tests, you should use Node.js v11 or newer or enable at runtime with `node --experimental-worker` with Node.js version >= 10.5.0 and < 11.
This is a skeleton for developing JS modules in Typescript that work both in Node.js and native Javascript. The idea is that you should just focus on developing your typescript code in the `src/ts` folder, and the necessary JS files and bundles will be created so that it can be used with no effort in every environment.
_The operations supported on BigInts are not constant time. BigInt can be therefore **[unsuitable for use in cryptography](https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html).** Many platforms provide native support for cryptography, such as [Web Cryptography API](https://w3c.github.io/webcrypto/) or [Node.js Crypto](https://nodejs.org/dist/latest/docs/api/crypto.html)._
You can use string variable `IS_BROWSER` to create specific code for native JS or Node. For example:
The Paillier cryptosystem, named after and invented by Pascal Paillier in 1999, is a probabilistic asymmetric algorithm for public key cryptography. A notable feature of the Paillier cryptosystem is its homomorphic properties.
```typescript
if (IS_BROWSER === 'true') {
// browser specific code here
} else {
// node.js specific code here
}
```
## Homomorphic properties
Besides the actual code, you should create unit testing (mocha+chai) files either in the `test` or the `src/ts` directory, although in the latter case only files ending with `.spec.ts` will be considered as test files.
### Homomorphic addition of plaintexts
When creating the tests, you MUST NOT import either `mocha`, `chai` or your package. They have been automatically added to the global scope:
The product of two ciphertexts will decrypt to the sum of their corresponding plaintexts,
- `mocha` global variable points to mocha,
- `chai` points to chai,
- `_pkg` points to your package (all your exports),
- `_pkgTypes` points to your package typings.
**D( E(m<sub>1</sub>) · E(m<sub>2</sub>) ) mod n<sup>2</sup> = m<sub>1</sub> + m<sub>2</sub> mod n**
## Installation
The product of a ciphertext with a plaintext raising g will decrypt to the sum of the corresponding plaintexts,
Clone this repo to your desired project directory (`my-project` in the following example) and reset the git.
**D( E(m<sub>1</sub>) · g<sup>m<sub>2</sub></sup> ) mod n<sup>2</sup> = m<sub>1</sub> + m<sub>2</sub> mod n**
```console
git clone https://github.com/juanelas/node-browser-skel.git my-project
cd my-project
rm -rf .git
git init
git add -A
```
### (pseudo-)homomorphic multiplication of plaintexts
Edit `package.json` to suit your needs and initialize the project with:
An encrypted plaintext raised to the power of another plaintext will decrypt to the product of the two plaintexts,
```console
npm i
npm update
npm run build
```
**D( E(m<sub>1</sub>)<sup>m<sub>2</sub></sup> mod n<sup>2</sup> ) = m<sub>1</sub> · m<sub>2</sub> mod n**,
The `README.md` file is automatically generated from the `src/docs/index.md` file. EDIT `src/docs/index.md` and rewrite it to your heart's content. Recall removing the section "Skeleton for developing modules for browser and Node.js in Typescript" with all its subsections (Installation, Tooling, Scripts).
**D( E(m<sub>2</sub>)<sup>m<sub>1</sub></sup> mod n<sup>2</sup> ) = m<sub>1</sub> · m<sub>2</sub> mod n**.
## Tooling
More generally, an encrypted plaintext raised to a constant k will decrypt to the product of the plaintext and the
constant,
- Build: [Rollup](https://rollupjs.org) is used for generating UMD, IIFE, ESM and CJS modules with the corresponding Typescript declaration files and sourcemaps in the `dist` directory.
- Coverage: [Nyc-Istanbul](https://github.com/istanbuljs/nyc) is used to track how well your unit-tests exercise your codebase.
- Doc: [TsCode](https://tsdoc.org/) is used for automatically generating the [API docs](./docs/API.md). Consider documenting your code with TsCode for it to be useful.
- Lint: [ts-stamdard](https://github.com/standard/ts-standard) is the chosen linter, although you can easily change it by any other linter (update `scripts.lint` in the `package.json`). If developing with [Visual Studio Code](https://code.visualstudio.com/), consider installing the [Standard-JS extension](https://marketplace.visualstudio.com/items?itemName=chenxsan.vscode-standardjs) and select `ts-standard` as the `Standard:engine` in the extension settings.
- Test: [Mocha](https://mochajs.org/) with [Chai](https://www.chaijs.com/) running both in Node.js and browser (using [puppeteer](https://pptr.dev/)). Test files should be created assuming that Mocha methods and Chai are declared global, so there is no need to import them (see the provided test examples). There is also no need to create separate test files for browser and Node.js, since every file will be tested against both. Test files are transpiled using [tsc CLI](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
**D( E(m<sub>1</sub>)<sup>k</sup> mod n<sup>2</sup> ) = k · m<sub>1</sub> mod n**.
## Scripts
However, given the Paillier encryptions of two messages there is no known way to compute an encryption of the product of
these messages without knowing the private key.
- `npm run build`. Runs the linter (`lint`), builds the JS files (`build:js`), builds the `README.md` and the API doc `./docs/API.md` (`docs`), runs the unit tests in browser (`test:browser`), and creates a coverage report of the tests run in Node.js (`coverage`). See the specific scripts for more details.
- `npm run build:js`. Creates your distributable module files (UMD, IIFE, ESM and CJS), along with the sourcemap and typescript declaration files in the `dist` directory.
- `npm run clean`. Cleans all the artifacts created by the rest of the script (most likely not needed).
- `npm run coverage`. Runs all the unit tests (`src/**/*.spec.ts` and `test/**/*.ts`) in Node.js and track how well they exercise your codebase. Besides the on-screen summary, a complete report in HTML will be generated in the `coverage` directory.
- `npm run docs`. Generates the `README.md` and the API doc `./docs/API.md`. Some labels in the `src/README.md` file will be automatically replaced in the generated `README.md`:
## Key generation
- &#123;&#123;PKG_NAME&#125;&#125; is automatically replaced with property `name` in `package.json` file.
- &#123;&#123;PKG_CAMELCASE&#125;&#125; will be replaced by a came case transformation of the package_name.
- &#123;&#123;IIFE_BUNDLE&#125;&#125; will point to the IIFE bundle file if using github or gitlab as repository.
- &#123;&#123;ESM_BUNDLE&#125;&#125; will point to the ESM bundle file if using github or gitlab as repository.
- &#123;&#123;UMD_BUNDLE&#125;&#125; will point to the UMD bundle file if using github or gitlab as repository.
- It has also some automatically added badges (see the top of this file), that you can remove if desired.
1. Define the bit length of the modulus `n`, or `keyLength` in bits.
2. Choose two large prime numbers `p` and `q` randomly and independently of each other such that `gcd( p·q, (p-1)(q-1) )=1` and `n=p·q` has a key length of keyLength. For instance:
1. Generate a random prime `p` with a bit length of `keyLength/2 + 1`.
2. Generate a random prime `q` with a bit length of `keyLength/2`.
3. Repeat until the bitlength of `n=p·q` is `keyLength`.
3. Compute parameters `λ`, `g` and `μ`. Among other ways, it can be done as follows:
1. Standard approach:
1. Compute `λ = lcm(p-1, q-1)` with `lcm(a, b) = a·b / gcd(a, b)`.
2. Generate randoms `α` and `β` in `Z*` of `n`, and select generator `g` in `Z*` of `n**2` as `g = ( α·n + 1 ) β**n mod n**2`.
3. Compute `μ = ( L( g^λ mod n**2 ) )**(-1) mod n` where `L(x)=(x-1)/n`.
2. If using p,q of equivalent length, a simpler variant would be:
1. `λ = (p-1, q-1)`
2. `g = n+1`
3. `μ = λ**(-1) mod n`
The **public** (encryption) **key** is **(n, g)**.
- `npm run lint`. Uses the `ts-standard` linter to fix all the project files. If unconfortable, change the linter for the one of your liking.
- `npm run mocha -- <glob>`. Runs Node.js mocha for the selected tests (use glob pattern). Add `--watch` before the glob to start mocha in watch mode.
- `npm test`. Runs all the unit tests (`src/**/*.spec.ts` and `test/**/*.ts`) in both Node.js and browser (using puppeteer).
- `npm run test:browser`. Runs all the unit tests (`src/**/*.spec.ts` and `test/**/*.ts`) in a browser (using pupppeteer).
- `npm run test:node`. Runs all the unit tests (`src/**/*.spec.ts` and `test/**/*.ts`) in Node.js.
- `npm run watch`. Likely to be the default script during development. Tests are automatically reexecuted whenever a test or source file changes.
The **private** (decryption) **key** is **(λ, μ)**.
## Encryption
Let `m` in `[0, n)` be the clear-text message,
# paillier-bigint
1. Select random integer `r` in `Z*` of `n`.
Your package description
2. Compute ciphertext as: **`c = g**m · r**n mod n**2`**
## Usage
## Decryption
Let `c` be the ciphertext to decrypt, where `c` in `(0, n**2)`.
1. Compute the plaintext message as: **`m = L( c**λ mod n**2 ) · μ mod n`**
## Installation
`paillier-bigint` can be imported to your project with `npm`:
```bash
```console
npm install paillier-bigint
```
NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/master/lib/index.browser.bundle.iife.js) or the [ESM bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/master/lib/index.browser.bundle.mod.js) from the repository.
Then either require (Node.js CJS):
## Usage
```javascript
const paillierBigint = require('paillier-bigint')
```
Import your module as :
or import (JavaScript ES module):
- Node.js
```javascript
const paillierBigint = require('paillier-bigint')
... // your code here
```
- JavaScript native or TypeScript project (including Angular and React)
```javascript
import * as paillierBigint from 'paillier-bigint'
... // your code here
```
Notice that `paillier-bigint` relies on [`bigint-crypto-utils`](https://github.com/juanelas/bigint-crypto-utils) which cannot be polyfilled to suport older browsers. If you are using webpack/babel to create your production bundles, you should target only the most modern browsers. For instance, for React apps created with [`create-react-app`](https://create-react-app.dev/), you should edit your `package.json` and modify the `browserList` so that it only targets the latest browsers (supporting the latest features):
```json
"browserslist": {
"production": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
```
Also, notice that BigInt is [ES-2020](https://tc39.es/ecma262/#sec-bigint-objects). In order to use it with TypeScript you should set `lib` (and probably also `target` and `module`) to `esnext` in `tsconfig.json`.
- JavaScript native browser ES module
```html
<script type="module">
import * as paillierBigint from 'lib/index.browser.bundle.mod.js' // Use you actual path to the broser mod bundle
... // your code here
</script>
```
- JavaScript native browser IIFE
```html
<head>
...
<script src="../../lib/index.browser.bundle.iife.js"></script> <!-- Use you actual path to the browser bundle -->
</head>
<body>
...
<script>
... // your code here
</script>
</body>
```
Then you could use, for instance, the following code:
```javascript
async function paillierTest () {
// (asynchronous) creation of a random private, public key pair for the Paillier cryptosystem
const { publicKey, privateKey } = await paillierBigint.generateRandomKeys(3072)
import * as paillierBigint from 'paillier-bigint'
```
// Optionally, you can create your public/private keys from known parameters
// const publicKey = new paillierBigint.PublicKey(n, g)
// const privateKey = new paillierBigint.PrivateKey(lambda, mu, publicKey)
The appropriate version for browser or node is automatically exported.
const m1 = 12345678901234567890n
const m2 = 5n
You can also download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/master/dist/bundles/paillier-bigint.iife.js), the [ESM bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/master/dist/bundles/paillier-bigint.esm.js) or the [UMD bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/master/dist/bundles/paillier-bigint.umd.js) and manually add it to your project, or, if you have already imported `paillier-bigint` to your project, just get the bundles from `node_modules/paillier-bigint/dist/bundles/`.
// encryption/decryption
const c1 = publicKey.encrypt(m1)
console.log(privateKey.decrypt(c1)) // 12345678901234567890n
An example of usage could be:
// homomorphic addition of two ciphertexts (encrypted numbers)
const c2 = publicKey.encrypt(m2)
const encryptedSum = publicKey.addition(c1, c2)
console.log(privateKey.decrypt(encryptedSum)) // m1 + m2 = 12345678901234567895n
// multiplication by k
const k = 10n
const encryptedMul = publicKey.multiply(c1, k)
console.log(privateKey.decrypt(encryptedMul)) // k · m1 = 123456789012345678900n
}
paillierTest()
```typescript
YOUR TYPESCRIPT EXAMPLE CODE HERE
```
> Consider using [bigint-conversion](https://github.com/juanelas/bigint-conversion) if you need to convert from/to bigint to/from unicode text, hex, buffer.
## API reference documentation
<a name="PublicKey"></a>
### PublicKey
Class for a Paillier public key
**Kind**: global class
* [PublicKey](#PublicKey)
* [new PublicKey(n, g)](#new_PublicKey_new)
* [.bitLength](#PublicKey+bitLength) ⇒ <code>number</code>
* [.encrypt(m, [r])](#PublicKey+encrypt) ⇒ <code>bigint</code>
* [.addition(...ciphertexts)](#PublicKey+addition) ⇒ <code>bigint</code>
* [.multiply(c, k)](#PublicKey+multiply) ⇒ <code>bigint</code>
<a name="new_PublicKey_new"></a>
#### new PublicKey(n, g)
Creates an instance of class PublicKey
| Param | Type | Description |
| --- | --- | --- |
| n | <code>bigint</code> | the public modulo |
| g | <code>bigint</code> | the public generator |
<a name="PublicKey+bitLength"></a>
#### publicKey.bitLength ⇒ <code>number</code>
Get the bit length of the public modulo
**Kind**: instance property of [<code>PublicKey</code>](#PublicKey)
**Returns**: <code>number</code> - - bit length of the public modulo
<a name="PublicKey+encrypt"></a>
#### publicKey.encrypt(m, [r]) ⇒ <code>bigint</code>
Paillier public-key encryption
**Kind**: instance method of [<code>PublicKey</code>](#PublicKey)
**Returns**: <code>bigint</code> - - the encryption of m with this public key
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| m | <code>bigint</code> | | a bigint representation of a cleartext message |
| [r] | <code>bigint</code> | <code></code> | the random integer factor for encryption. By default is a random in (1,n) |
<a name="PublicKey+addition"></a>
#### publicKey.addition(...ciphertexts) ⇒ <code>bigint</code>
Homomorphic addition
**Kind**: instance method of [<code>PublicKey</code>](#PublicKey)
**Returns**: <code>bigint</code> - - the encryption of (m_1 + ... + m_2) with this public key
| Param | Type | Description |
| --- | --- | --- |
| ...ciphertexts | <code>bigint</code> | n >= 2 ciphertexts (c_1,..., c_n) that are the encryption of (m_1, ..., m_n) with this public key |
<a name="PublicKey+multiply"></a>
#### publicKey.multiply(c, k) ⇒ <code>bigint</code>
Pseudo-homomorphic Paillier multiplication
**Kind**: instance method of [<code>PublicKey</code>](#PublicKey)
**Returns**: <code>bigint</code> - - the encryption of k·m with this public key
| Param | Type | Description |
| --- | --- | --- |
| c | <code>bigint</code> | a number m encrypted with this public key |
| k | <code>bigint</code> \| <code>number</code> | either a bigint or a number |
<a name="PrivateKey"></a>
### PrivateKey
Class for Paillier private keys.
**Kind**: global class
* [PrivateKey](#PrivateKey)
* [new PrivateKey(lambda, mu, publicKey, [p], [q])](#new_PrivateKey_new)
* [.bitLength](#PrivateKey+bitLength) ⇒ <code>number</code>
* [.n](#PrivateKey+n) ⇒ <code>bigint</code>
* [.decrypt(c)](#PrivateKey+decrypt) ⇒ <code>bigint</code>
* [.getRandomFactor(c)](#PrivateKey+getRandomFactor) ⇒ <code>bigint</code>
<a name="new_PrivateKey_new"></a>
#### new PrivateKey(lambda, mu, publicKey, [p], [q])
Creates an instance of class PrivateKey
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| lambda | <code>bigint</code> | | |
| mu | <code>bigint</code> | | |
| publicKey | [<code>PublicKey</code>](#PublicKey) | | |
| [p] | <code>bigint</code> | <code></code> | a big prime |
| [q] | <code>bigint</code> | <code></code> | a big prime |
<a name="PrivateKey+bitLength"></a>
#### privateKey.bitLength ⇒ <code>number</code>
Get the bit length of the public modulo
**Kind**: instance property of [<code>PrivateKey</code>](#PrivateKey)
**Returns**: <code>number</code> - - bit length of the public modulo
<a name="PrivateKey+n"></a>
#### privateKey.n ⇒ <code>bigint</code>
Get the public modulo n=p·q
**Kind**: instance property of [<code>PrivateKey</code>](#PrivateKey)
**Returns**: <code>bigint</code> - - the public modulo n=p·q
<a name="PrivateKey+decrypt"></a>
#### privateKey.decrypt(c) ⇒ <code>bigint</code>
Paillier private-key decryption
**Kind**: instance method of [<code>PrivateKey</code>](#PrivateKey)
**Returns**: <code>bigint</code> - - the decryption of c with this private key
| Param | Type | Description |
| --- | --- | --- |
| c | <code>bigint</code> | a bigint encrypted with the public key |
<a name="PrivateKey+getRandomFactor"></a>
#### privateKey.getRandomFactor(c) ⇒ <code>bigint</code>
Recover the random factor used for encrypting a message with the complementary public key.
The recovery function only works if the public key generator g was using the simple variant
g = 1 + n
**Kind**: instance method of [<code>PrivateKey</code>](#PrivateKey)
**Returns**: <code>bigint</code> - - the random factor (mod n)
**Throws**:
- <code>RangeError</code> - Cannot recover the random factor if publicKey.g != publicKey.n + 1. You should generate yout keys using the simple variant, e.g. generateRandomKeys(3072, true) )
| Param | Type | Description |
| --- | --- | --- |
| c | <code>bigint</code> | the encryption using the public of message m with random factor r |
<a name="generateRandomKeys"></a>
### generateRandomKeys([bitlength], [simplevariant]) ⇒ [<code>Promise.&lt;KeyPair&gt;</code>](#KeyPair)
Generates a pair private, public key for the Paillier cryptosystem.
**Kind**: global function
**Returns**: [<code>Promise.&lt;KeyPair&gt;</code>](#KeyPair) - - a promise that resolves to a [KeyPair](#KeyPair) of public, private keys
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [bitlength] | <code>number</code> | <code>3072</code> | the bit length of the public modulo |
| [simplevariant] | <code>boolean</code> | <code>false</code> | use the simple variant to compute the generator (g=n+1). This is REQUIRED if you want to be able to recover the random integer factor used when encrypting with the public key |
<a name="generateRandomKeysSync"></a>
### generateRandomKeysSync([bitlength], [simplevariant]) ⇒ [<code>KeyPair</code>](#KeyPair)
Generates a pair private, public key for the Paillier cryptosystem in synchronous mode.
Synchronous mode is NOT RECOMMENDED since it won't use workers and thus it'll be slower and may freeze thw window in browser's javascript.
**Kind**: global function
**Returns**: [<code>KeyPair</code>](#KeyPair) - - a [KeyPair](#KeyPair) of public, private keys
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [bitlength] | <code>number</code> | <code>3072</code> | the bit length of the public modulo |
| [simplevariant] | <code>boolean</code> | <code>false</code> | use the simple variant to compute the generator (g=n+1) |
<a name="KeyPair"></a>
### KeyPair : <code>Object</code>
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| publicKey | [<code>PublicKey</code>](#PublicKey) | a Paillier's public key |
| privateKey | [<code>PrivateKey</code>](#PrivateKey) | the associated Paillier's private key |
[Check the API](./docs/API.md)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc