1.0.1 • Published 4 years ago
@wavesenterprise/voting-crypto v1.0.1
Voting cryptography lib
Install
Browser:
npm i @wavesenterprise/voting-crypto bn.jsNodeJS:
npm i @wavesenterprise/voting-crypto secp256k1Usage
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,
})1.0.1
4 years ago
1.0.0
4 years ago
0.2.1
4 years ago
0.2.0
4 years ago
0.1.14
4 years ago
0.1.15
4 years ago
0.1.16
4 years ago
0.1.17
4 years ago
0.1.18
4 years ago
0.1.10
4 years ago
0.1.11
4 years ago
0.1.12
4 years ago
0.1.13
4 years ago
0.1.8
4 years ago
0.1.7
4 years ago
0.1.9
4 years ago
0.1.4
5 years ago
0.1.3
5 years ago
0.1.6
4 years ago
0.1.5
5 years ago
0.1.2
5 years ago
0.1.1
5 years ago
0.1.0
5 years ago