@wavesenterprise/voting-crypto
v1.0.1
Published
### Install
Downloads
26
Keywords
Readme
Voting cryptography lib
Install
Browser:
npm i @wavesenterprise/voting-crypto bn.js
NodeJS:
npm i @wavesenterprise/voting-crypto secp256k1
Usage
Browser:
// For browsers with BigInt support
import { makeEncryptedBulletin } from '@wavesenterprise/voting-crypto/browser'
// For browsers without BigInt support
import { makeEncryptedBulletin } from '@wavesenterprise/voting-crypto/browser-old'
const encrypted = makeEncryptedBulletin({
mainKey: '024bfdeb94ed1ef7011342ff5d2f47d9f3526c147c5e145cb99d440b99f41979f6',
weight: 1,
bulletin: [[0, 1]],
dimension: [[1, 1, 2]],
})
NodeJS:
Generating points:
import { preGeneratePoints } from '@wavesenterprise/voting-crypto/node'
const points = preGeneratePoints({ from: 0, to: 10_000 })
Generating keys:
import { generateKeys } from '@wavesenterprise/voting-crypto/node'
const d1keys = generateKeys()
Unblind keys:
import { unblindPublicKeys } from '@wavesenterprise/voting-crypto/node'
const pairs = [d1keys, d2keys, d3keys].map((keys) => ([keys.publicKeyOfCommit, keys.privateKeyOfCommit]))
const unblindedKeys = unblindPublicKeys(pairs)
Get poly coefficients:
import { getPolynomialCoefficients } from '@wavesenterprise/voting-crypto/node'
const k = 2
const coefficients1 = getPolynomialCoefficients(d1keys.privateKey, k)
Get exponents:
import { getPolynomialCoefficientsExponents } from '@wavesenterprise/voting-crypto/node'
const exponents1 = getPolynomialCoefficientsExponents(coefficients1)
Get encrypted shadows:
import { calculateEncryptedShadows } from '@wavesenterprise/voting-crypto/node'
const shadows1 = calculateEncryptedShadows(unblindedKeys, coefficients1)
Decrypt and check shadows:
import { decryptAndCheckShadows } from '@wavesenterprise/voting-crypto/node'
const idx = 0
const shadowsSum1 = decryptAndCheckShadows({
privateKey: d1keys.privateKey,
idx,
encryptedShadows: [shadows1[idx], shadows2[idx], shadows3[idx]],
exponents: [exponents1, exponents2, exponents3],
unblindedPublicKeys: unblindedKeys,
})
Get mainkey:
import { calculateMainKey } from '@wavesenterprise/voting-crypto/node'
const pairs = [d1keys, d2keys, d3keys].map((keys) => ([keys.publicKeyOfCommit, keys.privateKeyOfCommit]))
const mainKey = calculateMainKey(pairs)
Encrypt bulletin:
pass true
as a second arg to get raw bytes output
import { makeEncryptedBulletin } from '@wavesenterprise/voting-crypto/node'
const dimension: Dimension = [[1, 1, 3]]
const weight = 100
const encrypted = makeEncryptedBulletin({
dimension,
mainKey,
bulletin: [[0, weight, 0]],
weight,
})
Verify bulletins:
weight must be multiplied
encrypted accepts raw bytes or formatted as input
import { verifyBulletin } from '@wavesenterprise/voting-crypto/node'
const result = verifyBulletin(encrypted, mainKey, dimension, weight)
Add bulletins:
import { addBulletins } from '@wavesenterprise/voting-crypto/node'
const prev = null
const added0 = addBulletins(encrypted, prev)
const added = addBulletins(encrypted, added0)
const subtracted = addBulletins(encrypted, added)
Create partial decryption:
import { createPartialDecryption } from '@wavesenterprise/voting-crypto/node'
const decryprion1 = createPartialDecryption(added, shadowsSum1)
Calculate results:
const results = await calculateResult({
indexes: [1, 3],
totalWeight: weight,
exponents: [exponents1, exponents2, exponents3],
partialDecryptions: [decryprion1, decryprion2, decryprion3],
sum: added,
mainKey,
})