meck93/evote-crypto

View on GitHub
src/ff-elgamal/proofs/decryption.ts

Summary

Maintainability
A
2 hrs
Test Coverage
/**
* Decryption Proof
*
* ElGamal Finite Field Non-Interactive Zero-Knowledge Proof for Decryption
* Using the Chaum-Pedersen Proof
*
* Proving that the decryption is done using the corresponding private key to the
* public key used for the encryption.
*
* - generate and verify proofs
*/
 
Missing semicolon
import BN = require('bn.js')
Missing semicolon
import { GlobalHelper } from '../../index'
Missing semicolon
import { Cipher, SystemParameters, isCipher, isSystemParameters } from '../index'
Missing semicolon
import { DecryptionProof } from './models'
 
Missing semicolon
const web3 = require('web3')
Missing semicolon
const printConsole = false
 
// modulo operations
Identical blocks of code found in 2 locations. Consider refactoring.
Missing semicolon
const add = (a: BN, b: BN, sp: SystemParameters): BN => GlobalHelper.addBN(a, b, sp.q)
Similar blocks of code found in 4 locations. Consider refactoring.
Missing semicolon
const mul = (a: BN, b: BN, sp: SystemParameters): BN => GlobalHelper.mulBN(a, b, sp.p)
Identical blocks of code found in 2 locations. Consider refactoring.
Missing semicolon
const pow = (a: BN, b: BN, sp: SystemParameters): BN => GlobalHelper.powBN(a, b, sp.p)
 
TODO found
// TODO: check paper https://eprint.iacr.org/2016/771.pdf why we should not hash a and b
const generateChallenge = (q: BN, uniqueID: string, a: BN, b: BN, a1: BN, b1: BN): BN => {
Missing semicolon
let c = web3.utils.soliditySha3(uniqueID, a, b, a1, b1)
Missing semicolon
c = web3.utils.toBN(c)
Missing semicolon
c = c.mod(q)
Missing semicolon
return c
Missing semicolon
}
 
// generate a proof for the decryption
// given:
// - a,b: cipher (a = g^r, b = h^r*g^m)
// steps:
// 1. generate random value x
// 2. compute (a1, b1) = (a^x, g^x)
// 3. generate the challenge
// 3. compute f = x + c * sk (NOTE: mod q!)
// 4. compute the decryption factor d = a^r
export const generate = (
cipher: Cipher,
sp: SystemParameters,
sk: BN,
uniqueID: string
): DecryptionProof => {
Missing semicolon
isCipher(cipher)
Missing semicolon
isSystemParameters(sp)
 
Missing semicolon
const { a, b }: Cipher = cipher
 
Missing semicolon
const x: BN = GlobalHelper.getSecureRandomValue(sp.q)
 
Missing semicolon
const a1: BN = pow(a, x, sp)
Missing semicolon
const b1: BN = pow(sp.g, x, sp)
 
Missing semicolon
const c: BN = generateChallenge(sp.q, uniqueID, a, b, a1, b1)
Missing semicolon
const f: BN = add(x, c.mul(sk).mod(sp.q), sp)
Missing semicolon
const d: BN = pow(a, sk, sp)
 
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('x\t\t\t', x.toNumber())
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('a1\t\t\t', a1.toNumber())
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('b1\t\t\t', b1.toNumber())
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('c\t\t\t', c.toNumber())
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('f = x + c*r\t\t', f.toNumber())
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log()
 
Type assertion on object literals is forbidden, use a type annotation instead.
Missing semicolon
return { a1, b1, f, d } as DecryptionProof
Missing semicolon
}
 
// verify a proof for the decryption
// 1. recompute the challenge
// 2. verification a^f == a1 * d^c
// 3. verification g^f == b1 * h^c
export const verify = (
cipher: Cipher,
proof: DecryptionProof,
sp: SystemParameters,
pk: BN,
uniqueID: string
): boolean => {
Missing semicolon
isCipher(cipher)
Missing semicolon
isSystemParameters(sp)
 
Missing semicolon
const { a, b }: Cipher = cipher
Missing semicolon
const { a1, b1, f, d }: DecryptionProof = proof
 
Missing semicolon
const c: BN = generateChallenge(sp.q, uniqueID, a, b, a1, b1)
 
Missing semicolon
const l1: BN = pow(a, f, sp)
Missing semicolon
const r1: BN = mul(a1, pow(d, c, sp), sp)
Missing semicolon
const v1: boolean = GlobalHelper.timingSafeEqualBN(l1, r1)
 
Missing semicolon
const l2: BN = pow(sp.g, f, sp)
Missing semicolon
const r2: BN = mul(b1, pow(pk, c, sp), sp)
Missing semicolon
const v2: boolean = GlobalHelper.timingSafeEqualBN(l2, r2)
 
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('a^f == a1*d^c:\t\t', v1, l1.toNumber(), r1.toNumber())
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('g^f == b1*h^c\t\t', v2, l2.toNumber(), r2.toNumber())
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log()
 
Missing semicolon
return v1 && v2
Missing semicolon
}