Installations
npm install paillier-bigint
Developer Guide
Typescript
Yes
Module System
ESM, UMD
Min. Node Version
>=12
Node Version
18.16.1
NPM Version
9.5.1
Score
90.9
Supply Chain
99
Quality
76
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (74.45%)
TypeScript (25.55%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
juanelas
Download Statistics
Total Downloads
1,981,595
Last Day
800
Last Week
39,444
Last Month
126,145
Last Year
892,671
GitHub Statistics
MIT License
47 Stars
101 Commits
9 Forks
2 Watchers
3 Branches
2 Contributors
Updated on Feb 11, 2025
Bundle Size
9.43 kB
Minified
3.45 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.4.3
Package Id
paillier-bigint@3.4.3
Unpacked Size
84.35 kB
Size
17.21 kB
File Count
23
NPM Version
9.5.1
Node Version
18.16.1
Published on
Jun 30, 2023
Total Downloads
Cumulative downloads
Total Downloads
1,981,595
Last Day
128.6%
800
Compared to previous day
Last Week
49.9%
39,444
Compared to previous week
Last Month
116.2%
126,145
Compared to previous month
Last Year
2.2%
892,671
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
28
paillier-bigint
An implementation of the Paillier cryptosystem relying on the native JS implementation of BigInt.
It can be used by any Web Browser or webview supporting BigInt 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.
The operations supported on BigInts are not constant time. BigInt can be therefore unsuitable for use in cryptography. Many platforms provide native support for cryptography, such as Web Cryptography API or Node.js Crypto.
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.
Homomorphic properties
Homomorphic addition of plaintexts
The product of two ciphertexts will decrypt to the sum of their corresponding plaintexts,
D( E(m1) · E(m2) ) mod n2 = m1 + m2 mod n
The product of a ciphertext with a plaintext raising g will decrypt to the sum of the corresponding plaintexts,
D( E(m1) · gm2 ) mod n2 = m1 + m2 mod n
(pseudo-)homomorphic multiplication of plaintexts
An encrypted plaintext raised to the power of another plaintext will decrypt to the product of the two plaintexts,
D( E(m1)m2 mod n2 ) = m1 · m2 mod n,
D( E(m2)m1 mod n2 ) = m1 · m2 mod n.
More generally, an encrypted plaintext raised to a constant k will decrypt to the product of the plaintext and the constant,
D( E(m1)k mod n2 ) = k · m1 mod n.
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.
Key generation
- Define the bit length of the modulus
n
, orkeyLength
in bits. - Choose two large prime numbers
p
andq
randomly and independently of each other such thatgcd( p·q, (p-1)(q-1) )=1
andn=p·q
has a key length of keyLength. For instance:- Generate a random prime
p
with a bit length ofkeyLength/2 + 1
. - Generate a random prime
q
with a bit length ofkeyLength/2
. - Repeat until the bitlength of
n=p·q
iskeyLength
.
- Generate a random prime
- Compute parameters
λ
,g
andμ
. Among other ways, it can be done as follows:- Standard approach:
- Compute
λ = lcm(p-1, q-1)
withlcm(a, b) = a·b / gcd(a, b)
. - Generate randoms
α
andβ
inZ*
ofn
, and select generatorg
inZ*
ofn**2
asg = ( α·n + 1 ) β**n mod n**2
. - Compute
μ = ( L( g^λ mod n**2 ) )**(-1) mod n
whereL(x)=(x-1)/n
.
- Compute
- If using p,q of equivalent length, a simpler variant would be:
λ = (p-1, q-1)
g = n+1
μ = λ**(-1) mod n
- Standard approach:
The public (encryption) key is (n, g).
The private (decryption) key is (λ, μ).
Encryption
Let m
in [0, n)
be the clear-text message,
-
Select random integer
r
inZ*
ofn
. -
Compute ciphertext as:
c = g**m · r**n mod n**2
Decryption
Let c
be the ciphertext to decrypt, where c
in (0, n**2)
.
- Compute the plaintext message as:
m = L( c**λ mod n**2 ) · μ mod n
Usage
paillier-bigint
can be imported to your project with npm
:
1npm install paillier-bigint
Then either require (Node.js CJS):
1const paillierBigint = require('paillier-bigint')
or import (JavaScript ES module):
1import * as paillierBigint from 'paillier-bigint'
The appropriate version for browser or node is automatically exported.
You can also download the IIFE bundle, the ESM bundle or the UMD bundle 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/
.
An example of usage could be:
1async function paillierTest () { 2 // (asynchronous) creation of a random private, public key pair for the Paillier cryptosystem 3 const { publicKey, privateKey } = await paillierBigint.generateRandomKeys(3072) 4 5 // Optionally, you can create your public/private keys from known parameters 6 // const publicKey = new paillierBigint.PublicKey(n, g) 7 // const privateKey = new paillierBigint.PrivateKey(lambda, mu, publicKey) 8 9 const m1 = 12345678901234567890n 10 const m2 = 5n 11 12 // encryption/decryption 13 const c1 = publicKey.encrypt(m1) 14 console.log(privateKey.decrypt(c1)) // 12345678901234567890n 15 16 // homomorphic addition of two ciphertexts (encrypted numbers) 17 const c2 = publicKey.encrypt(m2) 18 const encryptedSum = publicKey.addition(c1, c2) 19 console.log(privateKey.decrypt(encryptedSum)) // m1 + m2 = 12345678901234567895n 20 21 // multiplication by k 22 const k = 10n 23 const encryptedMul = publicKey.multiply(c1, k) 24 console.log(privateKey.decrypt(encryptedMul)) // k · m1 = 123456789012345678900n 25} 26paillierTest()
Consider using bigint-conversion if you need to convert from/to bigint to/from unicode text, hex, buffer.
API reference documentation

No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:43: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:46: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:72: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:75: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:94: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:97: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:109: update your workflow using https://app.stepsecurity.io/secureworkflow/juanelas/paillier-bigint/build-and-test.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/build-and-test.yml:53
- Info: 0 out of 8 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 3 out of 4 npmCommand dependencies pinned
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build-and-test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
14 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-76p7-773f-r4q5
- Warn: Project is vulnerable to: GHSA-cchq-frgv-rjh5
- Warn: Project is vulnerable to: GHSA-g644-9gfx-q4q4
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.8
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to paillier-bigint
paillier-pure
No-dependency version of paillier-bigint
paillier-js
A pure Javascript implementation of the Paillier cryptosystem. THIS PROJECT IS NO LONGER MAINTAINED. Consider switching to paillier-bigint that it is a pure JS native implementation and will work with both Node.js and Browsers, and not just Node.js
pallier-big-integer
An implementation of the Paillier cryptosystem relying on the js-biginteger with reference to pailler-bigint
doloressint
An implementation of the Paillier cryptosystem using native JS (ECMA 2020) implementation of BigInt