{"name":"paillier-bigint","version":"1.2.0","description":"An implementation of the Paillier cryptosystem using native JS (stage 3) implementation of BigInt","keywords":["paillier","homomorphic","crypto","cryptosystem"],"license":"MIT","author":{"name":"Juan Hernández Serrano","email":"jserrano@entel.upc.edu","url":"https://github.com/juanelas"},"repository":{"type":"git","url":"git+https://github.com/juanelas/paillier-bigint.git"},"main":"./dist/paillier-bigint-latest.node.js","browser":"./dist/paillier-bigint-latest.browser.mod.js","directories":{"build":"./build","dist":"./dist","src":"./src","test":"./test"},"scripts":{"test":"node --experimental-worker node_modules/mocha/bin/_mocha --timeout 600000","build":"node build/build.rollup.js","build:browserTests":"node build/build.browser.tests.js","build:docs":"jsdoc2md --template=README.hbs --files ./src/main.js > README.md","build:all":"npm run build && npm run build:browserTests && npm run build:docs","prepublishOnly":"npm run build && npm run build:docs"},"devDependencies":{"chai":"^4.2.0","eslint":"^6.0.0","jsdoc-to-markdown":"^5.0.3","mocha":"^6.2.2","rollup":"^1.27.13","rollup-plugin-babel-minify":"^9.0.0","rollup-plugin-commonjs":"^10.1.0","rollup-plugin-multi-entry":"^2.1.0","rollup-plugin-node-resolve":"^5.2.0","rollup-plugin-replace":"^2.2.0"},"dependencies":{"bigint-crypto-utils":"^2.3.1"},"gitHead":"35862bd45a7761c95a4367b058705fa4b85af4c0","bugs":{"url":"https://github.com/juanelas/paillier-bigint/issues"},"homepage":"https://github.com/juanelas/paillier-bigint#readme","_id":"paillier-bigint@1.2.0","_nodeVersion":"12.14.0","_npmVersion":"6.13.4","dist":{"integrity":"sha512-WW5vn5kZ4MFN1cIvM6So4IDYXJMYgBXQS+3JlVc3owyahf0LR6zq3AjUOlYSvlr2bsjJEUAtkMlZ+wSWNUR7cA==","shasum":"092d2f6591f465b6b4f128ba70bec199ba201eec","tarball":"http://registry-npm.ywwl.com/paillier-bigint/download/paillier-bigint-1.2.0.tgz","fileCount":17,"unpackedSize":144099,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJd/PBuCRA9TVsSAnZWagAAl2MP/2qZ/+ozu02YM3r5WrYz\nkp+tB5UV+ylWpt5bAcMY/DzMNOb+2qg/wn5DNAr/F9gKQsFiu71M97v1AxRB\nQ9LFjeQEP//Vndxn9CCdCTmDpEUQiP5rPapxxiURCEjtFd29wk4ylC7Fc80L\n3ULmVRa2r3fvNyLSpseDuGEaKTl5NLaTrVN9WaOaJeUaVfPaVS9I4qo5ZGNZ\nFaC6XALcWIz7DyAOwQ8ggHaHYFW/+8O637ICjWaocJiHU3TvfCfXwUMi3LUj\nMfC3EjJmdkBNJ/RchD2CpnSRqgOHHtvZaWm5aYfd/wcRgN/sI1HvuyRlRAl0\nMavY4DGVW/r/sUBm4r0UGTNO807zE/XPlQkBMUX1GHlPtsvTWtQZ7jcvQwpH\nulH3YD+Bji/Ij7BgYbnqIfo5r5/B97OBGpWLzqrEaQx6rMZAuY35bK88O+dk\nrPEZi0obsmLBq8XNc6xqE2bfufnGbxzvu39sRjjcqWBXsjnepyY/xyFgs1JP\nt7mXPRR4atvcRIauN3Jx87Ct3v9tTP90mXi35ZRm+1l7iWFGXz3wX1XZep5j\nkYgw1I47DtL5Zdq2QNPh82R7hwyF/ucCsVca2bjQ3N/Z6d2/WHwe0vt69Etg\ntJTNGp3gRVLpjrK9ioBO0VCNqLhWs6jOdtu93rYrBTijymE6HavCA3pBzcq3\nhoPb\r\n=NXpM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDwfKufveuzKYP20sg4fEGmo0RGjdaWvmnc4cpiWrOueAIhAMLJN3mx/ljRbl0CmP2DP5z9hX3gBHLH8nJOf3FOURFM"}],"size":32389,"noattachment":false,"key":"/paillier-bigint/-/paillier-bigint-1.2.0.tgz"},"maintainers":[{"name":"juanelas","email":"jserrano@entel.upc.edu"}],"_npmUser":{"name":"juanelas","email":"jserrano@entel.upc.edu"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/paillier-bigint_1.2.0_1576857710161_0.7797922544842786"},"_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":1576857710293,"_cnpm_publish_time":1576857710293,"dist-tags":{"latest":"3.4.3"}}