{"name":"paillier-bigint","version":"3.4.1","description":"An implementation of the Paillier cryptosystem using native JS (ECMA 2020) implementation of BigInt","keywords":["paillier","homomorphic","crypto","cryptosystem","bigint","typescript","angular","react"],"license":"MIT","author":{"name":"Juan Hernández Serrano","email":"j.hernandez@upc.edu","url":"https://github.com/juanelas"},"repository":{"type":"git","url":"git+https://github.com/juanelas/paillier-bigint.git"},"engines":{"node":">=12"},"type":"module","main":"./dist/index.node.cjs","browser":"./dist/index.browser.esm.js","types":"./dist/index.d.ts","exports":{".":{"node":{"module":{"types":"./dist/index.d.ts","default":"./dist/index.node.esm.js"},"import":{"types":"./dist/index.d.ts","default":"./dist/index.node.esm.js"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.node.cjs"}},"script":"./dist/bundle.iife.js","default":{"types":"./dist/index.d.ts","default":"./dist/index.browser.esm.js"}},"./esm-browser-bundle":"./dist/bundle.esm.min.js","./esm-browser-bundle-nomin":"./dist/bundle.esm.js","./iife-browser-bundle":"./dist/bundle.iife.js","./umd-browser-bundle":"./dist/bundle.umd.js","./types":"./dist/index.d.ts"},"imports":{"#pkg":{"require":{"types":"./dist/index.d.ts","default":"./dist/index.node.cjs"},"import":{"types":"./dist/index.d.ts","default":"./dist/index.node.esm.js"},"default":{"types":"./dist/index.d.ts","default":"./dist/index.browser.esm.js"}}},"directories":{"build":"./build","dist":"./dist","docs":"./docs","src":"./src","test":"./test","benchmark":"./benchmark","mocha-ts":"./.mocha-ts"},"scripts":{"build":"run-s lint:src build:js lint:test docs","build:js":"rollup -c build/rollup.config.js","postbuild:js":"rimraf .types","clean":"rimraf .mocha-ts coverage dist .types docs","coverage":"c8 --clean --check-coverage --exclude \"{src/ts/**/*.spec.ts,test,test-vectors,build}\" --exclude-after-remap --reporter=text --reporter=lcov node ./build/bin/mocha-ts.cjs --commonjs ","docs":"node build/build.docs.cjs","git:add":"git add -A","lint":"ts-standard --fix","lint:src":"ts-standard --fix \"src/**/!(*.spec).ts\"","lint:test":"ts-standard --fix \"{test/**/*.ts,src/**/*.spec.ts}\"","mocha-ts":"node --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node ./build/bin/mocha-ts.cjs ","mocha-ts:cjs":"node ./build/bin/mocha-ts.cjs --commonjs ","mocha-ts:watch":"npm run mocha-ts:cjs -- --watch ","mocha-ts:browser":"node build/testing/browser/index.cjs ","mocha-ts:browser-headless":"node build/testing/browser/index.cjs headless ","preversion":"run-s clean lint:src build:js lint:test coverage test:browser-headless","version":"run-s docs git:add","postversion":"git push --follow-tags","test":"run-s test:node test:browser-headless","test:browser":"npm run mocha-ts:browser","test:browser-headless":"npm run mocha-ts:browser-headless","test:node":"run-s test:node-cjs test:node-esm","test:node-cjs":"npm run mocha-ts:cjs ","test:node-esm":"npm run mocha-ts ","watch":"npm run mocha-ts:watch "},"ts-standard":{"project":"tsconfig.json","env":["mocha"],"globals":["IS_BROWSER","browser","page","chai"],"ignore":["dist/**/*","examples/**/*","types/**/*","benchmark/**/*"]},"nodeBrowserSkel":{"badges":{"workflow":true,"coveralls":true},"git":{"branch":"main"}},"devDependencies":{"@rollup/plugin-commonjs":"^24.0.1","@rollup/plugin-inject":"^5.0.3","@rollup/plugin-json":"^6.0.0","@rollup/plugin-multi-entry":"^6.0.0","@rollup/plugin-node-resolve":"^15.0.1","@rollup/plugin-replace":"^5.0.1","@rollup/plugin-terser":"^0.4.0","@rollup/plugin-typescript":"^11.1.0","@types/chai":"^4.2.22","@types/mocha":"^10.0.0","c8":"^7.12.0","chai":"^4.3.3","dotenv":"^16.0.3","glob":"^10.0.0","json5":"^2.2.0","minimatch":"^9.0.0","mocha":"^10.0.0","npm-run-all":"^4.1.5","pirates":"^4.0.1","puppeteer":"^19.1.2","rimraf":"^5.0.0","rollup":"^3.20.2","rollup-plugin-dts":"^5.3.0","ts-standard":"^12.0.2","tslib":"^2.3.1","typedoc":"~0.23.0","typedoc-plugin-markdown":"~3.14.0","typescript":"^5.0.3"},"dependencies":{"bigint-crypto-utils":"^3.2.2"},"gitHead":"66fb6da8fcbfcad381728a0ef1fe0a49b79a0c5b","bugs":{"url":"https://github.com/juanelas/paillier-bigint/issues"},"homepage":"https://github.com/juanelas/paillier-bigint#readme","_id":"paillier-bigint@3.4.1","_nodeVersion":"18.15.0","_npmVersion":"9.5.0","dist":{"integrity":"sha512-V6QKtwFm6YjAu9L6SFTG/YpZlBN3vCGn0KfsdP3QkSxu6ingej0MpmTH2ooMMFFiUHL3Rmc6Ao8YCryaiR2iOw==","shasum":"e97828d56ca1154a375878d2eaba4f4c46952cc2","tarball":"http://registry-npm.ywwl.com/paillier-bigint/download/paillier-bigint-3.4.1.tgz","fileCount":29,"unpackedSize":99084,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCuz1Iyd5n6gziVDzyGIH+ZrWHkTvGQVm2ClaC1+DzLZgIhAK22nt2wvgncHiAN8KYt032zd8sVVNa67PhsGcR1jpl1"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkOcLDACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpvEBAAjJT6O8ro66rrXyKxtUgJU2/6gF4XPEd603I7g3FiFuOziz3T\r\nzP1yQLYqe8JUhDO4WGydgnSX25UcnYgMaDgKPMWHzQ0hr5LZczYfafZHCMMI\r\nBp+nvBswB+hXT26S8t2o9RsGgwtu8OhpQR0s1y0ErQTW+le/HVnHT5G7sI/n\r\noxmQ7vqa0DZFpHzfaStQnLrSuMDIPqkJg+JbvScJEtHnhhYiRO1iiCrRp/FQ\r\nxk6qiNKKe+04Kt8LCZJsrdKSI0PoR1AAe7vZipvMdlg6Nn8KvSZIi4tqxGJK\r\nipdI7AUKYudMSxXSMKMuhMJwPueoOAYoRfhUwJfFPwP0Wfz+oJn3tnDrzRWf\r\nus5V8DGuX2Ld3OTRewH5tCATB3GoQS9RfK6UgPzZmvHheJol1riICL+o4u8r\r\n9nTzyGs2lmpecPkLg3ogWjwgA2PyZQpG74OW+YR9DUt/YRQ4kX1zpI3Zj1Ft\r\nnW5LdPOTUGJwZ9v3vgGErQ13ZFxhe7Pp+eJkQAlhMC0FpyIN5AUpJf9vmShd\r\nWhyzNmhg7nm4Hq334niJKqxJDtBdbRtPmma5GhOrY7AgmKOkPNlN+vt1mZsZ\r\nPn2ZQ0yoQlKaKgEBPJF1+6ICOuOOFYqAAF5G6WMUkHJ+WBHdILMAtN6Tgeda\r\nHEZcMJLHctKMPavwZfhZQlbU3OwA1OMnWHs=\r\n=rFiC\r\n-----END PGP SIGNATURE-----\r\n","size":21369,"noattachment":false,"key":"/paillier-bigint/-/paillier-bigint-3.4.1.tgz"},"_npmUser":{"name":"juanelasisme","email":"j.hernandez@upc.edu"},"maintainers":[{"name":"juanelasisme","email":"j.hernandez@upc.edu"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/paillier-bigint_3.4.1_1681507011695_0.09875474061860667"},"_hasShrinkwrap":false,"readme":"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n[![Node.js CI](https://github.com/juanelas/paillier-bigint/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/juanelas/paillier-bigint/actions/workflows/build-and-test.yml)\n[![Coverage Status](https://coveralls.io/repos/github/juanelas/paillier-bigint/badge.svg?branch=main)](https://coveralls.io/github/juanelas/paillier-bigint?branch=main)\n\n# paillier-bigint\n\nAn implementation of the Paillier cryptosystem relying on the native JS implementation of BigInt. \n\nIt 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.\n\n_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)._\n\nThe 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.\n\n## Homomorphic properties\n\n### Homomorphic addition of plaintexts\n\nThe product of two ciphertexts will decrypt to the sum of their corresponding plaintexts,\n\n**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**\n\nThe product of a ciphertext with a plaintext raising g will decrypt to the sum of the corresponding plaintexts,\n\n**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**\n\n### (pseudo-)homomorphic multiplication of plaintexts\n\nAn encrypted plaintext raised to the power of another plaintext will decrypt to the product of the two plaintexts,\n\n**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**,\n\n**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**.\n\nMore generally, an encrypted plaintext raised to a constant k will decrypt to the product of the plaintext and the\nconstant,\n\n**D( E(m<sub>1</sub>)<sup>k</sup> mod n<sup>2</sup> ) = k · m<sub>1</sub> mod n**.\n\nHowever, given the Paillier encryptions of two messages there is no known way to compute an encryption of the product of\nthese messages without knowing the private key.\n\n## Key generation\n\n1. Define the bit length of the modulus `n`, or `keyLength` in bits.\n2. 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:\n   1. Generate a random prime `p` with a bit length of `keyLength/2 + 1`.\n   2. Generate a random prime `q` with a bit length of `keyLength/2`.\n   3. Repeat until the bitlength of `n=p·q` is `keyLength`.\n3. Compute parameters `λ`, `g` and `μ`. Among other ways, it can be done as follows:\n   1. Standard approach:\n      1. Compute `λ = lcm(p-1, q-1)` with `lcm(a, b) = a·b / gcd(a, b)`.\n      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`.\n      3. Compute `μ = ( L( g^λ mod n**2 ) )**(-1) mod n` where `L(x)=(x-1)/n`.\n   2. If using p,q of equivalent length, a simpler variant would be:\n      1. `λ = (p-1, q-1)`\n      2. `g = n+1`\n      3. `μ = λ**(-1) mod n`\n   \nThe **public** (encryption) **key** is **(n, g)**.\n\nThe **private** (decryption) **key** is **(λ, μ)**. \n  \n## Encryption\nLet `m` in `[0, n)` be the clear-text message,\n\n1. Select random integer `r` in `Z*` of `n`.\n\n2. Compute ciphertext as: **`c = g**m · r**n mod n**2`**\n\n## Decryption\nLet `c` be the ciphertext to decrypt, where `c` in `(0, n**2)`.\n\n1. Compute the plaintext message as: **`m = L( c**λ mod n**2 ) · μ mod n`**\n\n## Usage\n\n`paillier-bigint` can be imported to your project with `npm`:\n\n```console\nnpm install paillier-bigint\n```\n\nThen either require (Node.js CJS):\n\n```javascript\nconst paillierBigint = require('paillier-bigint')\n```\n\nor import (JavaScript ES module):\n\n```javascript\nimport * as paillierBigint from 'paillier-bigint'\n```\n\nThe appropriate version for browser or node is automatically exported.\n\nYou can also download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/main/dist/bundle.iife.js), the [ESM bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/main/dist/bundle.esm.min.js) or the [UMD bundle](https://raw.githubusercontent.com/juanelas/paillier-bigint/main/dist/bundle.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/`.\n\nAn example of usage could be:\n\n```javascript\nasync function paillierTest () {\n  // (asynchronous) creation of a random private, public key pair for the Paillier cryptosystem\n  const { publicKey, privateKey } = await paillierBigint.generateRandomKeys(3072)\n\n  // Optionally, you can create your public/private keys from known parameters\n  // const publicKey = new paillierBigint.PublicKey(n, g)\n  // const privateKey = new paillierBigint.PrivateKey(lambda, mu, publicKey)\n\n  const m1 = 12345678901234567890n\n  const m2 = 5n\n\n  // encryption/decryption\n  const c1 = publicKey.encrypt(m1)\n  console.log(privateKey.decrypt(c1)) // 12345678901234567890n\n\n  // homomorphic addition of two ciphertexts (encrypted numbers)\n  const c2 = publicKey.encrypt(m2)\n  const encryptedSum = publicKey.addition(c1, c2)\n  console.log(privateKey.decrypt(encryptedSum)) // m1 + m2 = 12345678901234567895n\n\n  // multiplication by k\n  const k = 10n\n  const encryptedMul = publicKey.multiply(c1, k)\n  console.log(privateKey.decrypt(encryptedMul)) // k · m1 = 123456789012345678900n\n}\npaillierTest()\n```\n\n> Consider using [bigint-conversion](https://github.com/juanelas/bigint-conversion) if you need to convert from/to bigint to/from unicode text, hex, buffer.\n\n## API reference documentation\n\n[Check the API](./docs/API.md)\n","publish_time":1681507011856,"_cnpm_publish_time":1681507011856,"dist-tags":{"latest":"3.4.3"}}