meck93/evote-crypto

View on GitHub
src/ec-elgamal/proofs/membership.ts

Summary

Maintainability
A
3 hrs
Test Coverage
Missing semicolon
import BN = require('bn.js')
 
Missing semicolon
import { GlobalHelper } from '../../index'
import {
Cipher,
Curve,
CurvePoint,
Helper,
SystemParameters,
SystemParametersSerialized,
Missing semicolon
} from '../index'
Missing semicolon
import { MembershipProof } from './models'
Missing semicolon
import { curveDefinition } from '../curve'
 
Missing semicolon
const printConsole = false
 
Similar blocks of code found in 2 locations. Consider refactoring.
export const generateChallenge = (
n: BN,
id: string,
c1: CurvePoint,
c2: CurvePoint,
a1: CurvePoint,
a2: CurvePoint,
b1: CurvePoint,
b2: CurvePoint
): BN => {
Missing semicolon
const pointsAsString = Helper.curvePointsToString([c1, c2, a1, a2, b1, b2])
Missing semicolon
const input = id + pointsAsString
 
let c = curveDefinition
.hash()
.update(input)
Missing semicolon
.digest('hex')
 
Missing semicolon
c = new BN(c, 'hex')
Missing semicolon
c = c.mod(n)
 
Missing semicolon
return c
Missing semicolon
}
 
// generate a proof for an encrypted yes vote
// steps:
// 1. generate fake values c0,f0 for m=0 (random values in Z_q = Z_n)
// 2. compute fake (a0,b0) = (g^f0 / a^c0, h^f0 / b^c0)
// 3. generate proof for m=1
// 3.1 generate a random value x in Z_q = Z_n
// 3.2 compute (a1,b1) = (g^x, h^x)
// 4. generate the challenge c
// 5. compute c1 = c - c0
// 6. compute f1 = x + c1 * r (NOTE: mod q! = mod n!)
Function `generateYesProof` has 28 lines of code (exceeds 25 allowed). Consider refactoring.
export const generateYesProof = (
encryptedVote: Cipher,
params: SystemParameters | SystemParametersSerialized,
publicKey: CurvePoint | string,
id: string
): MembershipProof => {
Missing semicolon
const { a, b, r } = encryptedVote
Missing semicolon
const { g, n } = Helper.deserializeParams(params)
Missing semicolon
const h = Helper.deserializeCurvePoint(publicKey)
 
if (r === undefined || r === null) {
Missing semicolon
throw new Error('value r is undefined')
}
 
Missing semicolon
const c0: BN = GlobalHelper.getSecureRandomValue(n)
Missing semicolon
const f0: BN = GlobalHelper.getSecureRandomValue(n)
 
Missing semicolon
const a0 = Helper.ECdiv(Helper.ECpow(g, f0), Helper.ECpow(a, c0))
Missing semicolon
const b0 = Helper.ECdiv(Helper.ECpow(h, f0), Helper.ECpow(b, c0))
 
Missing semicolon
const x: BN = GlobalHelper.getSecureRandomValue(n)
Missing semicolon
const a1 = Helper.ECpow(g, x)
Missing semicolon
const b1 = Helper.ECpow(h, x)
 
Missing semicolon
const c = generateChallenge(n, id, a, b, a0, b0, a1, b1)
Missing semicolon
const c1 = GlobalHelper.addBN(n, GlobalHelper.subBN(c, c0, n), n)
 
Missing semicolon
const f1 = GlobalHelper.addBN(x, GlobalHelper.mulBN(c1, r, n), n)
 
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('a0 is on the curve?\t', Curve.validate(a0))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('b0 is on the curve?\t', Curve.validate(b0))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('a1 is on the curve?\t', Curve.validate(a1))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('b1 is on the curve?\t', Curve.validate(b1))
 
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('c0\t\t\t\t', c0.toString('hex'))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('f0\t\t\t\t', f0.toString('hex'))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('x\t\t\t\t', x.toString('hex'))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('c\t\t\t\t', c.toString('hex'))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('c1 = (q + (c - c0) % q) % q\t', c1.toString('hex'))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('f1 = x + c1*r\t\t\t', f1.toString('hex'))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log()
 
Missing semicolon
return { a0, a1, b0, b1, c0, c1, f0, f1 }
Missing semicolon
}
 
// generate a proof for an encrypted no vote
// steps:
// 1. generate fake values c1,f1 for m=1 (random values in Z_q = Z_n)
// 2. compute fake b_ = b/g
// 3. compute fake (a1,b1) = (g^f1 / a^c1, h^f1 / (b/g)^c1)
// 4. generate proof for m=0
// 4.1 generate a random value x in Z_q = Z_n
// 4.2 compute (a0,b0) = (g^x, h^x)
// 5. generate the challenge c
// 6. compute c0 = c - c1
// 7. compute f0 = x + c0 * r (NOTE: mod q!)
Function `generateNoProof` has 29 lines of code (exceeds 25 allowed). Consider refactoring.
export const generateNoProof = (
encryptedVote: Cipher,
params: SystemParameters | SystemParametersSerialized,
publicKey: CurvePoint | string,
id: string
): MembershipProof => {
Missing semicolon
const { a, b, r } = encryptedVote
Missing semicolon
const { g, n } = Helper.deserializeParams(params)
Missing semicolon
const h = Helper.deserializeCurvePoint(publicKey)
 
if (r === undefined || r === null) {
Missing semicolon
throw new Error('value r is undefined')
}
 
Missing semicolon
const c1: BN = GlobalHelper.getSecureRandomValue(n)
Missing semicolon
const f1: BN = GlobalHelper.getSecureRandomValue(n)
 
Missing semicolon
const b_ = Helper.ECdiv(b, g)
Missing semicolon
const a1 = Helper.ECdiv(Helper.ECpow(g, f1), Helper.ECpow(a, c1))
Missing semicolon
const b1 = Helper.ECdiv(Helper.ECpow(h, f1), Helper.ECpow(b_, c1))
 
Missing semicolon
const x: BN = GlobalHelper.getSecureRandomValue(n)
Missing semicolon
const a0 = Helper.ECpow(g, x)
Missing semicolon
const b0 = Helper.ECpow(h, x)
 
Missing semicolon
const c = generateChallenge(n, id, a, b, a0, b0, a1, b1)
Missing semicolon
const c0 = GlobalHelper.addBN(n, GlobalHelper.subBN(c, c1, n), n)
 
Missing semicolon
const f0 = GlobalHelper.addBN(x, GlobalHelper.mulBN(c0, r, n), n)
 
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('a1 is on the curve?\t', Curve.validate(a1))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('b1 is on the curve?\t', Curve.validate(b1))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('a0 is on the curve?\t', Curve.validate(a0))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('b0 is on the curve?\t', Curve.validate(b0))
 
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('c1\t\t\t\t', c1.toString('hex'))
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('f1\t\t\t\t', f1.toString('hex'))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('x\t\t\t\t', x.toString('hex'))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('c\t\t\t\t', c.toString('hex'))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('c0 = (q + (c - c1) % q) % q\t', c0.toString('hex'))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('f0 = x + c0*r\t\t\t', f0.toString('hex'))
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log()
 
Missing semicolon
return { a0, a1, b0, b1, c0, c1, f0, f1 }
Missing semicolon
}
 
// verification g^f0 == a0*a^c0
// verification g^f1 == a1*a^c1
// verification h^f0 == b0 * b^c0
// verification h^f1 == b1 * (b/g)^c1
// recompute the hash and verify
export const verify = (
encryptedVote: Cipher,
proof: MembershipProof,
params: SystemParameters | SystemParametersSerialized,
publicKey: CurvePoint | string,
id: string
): boolean => {
Identical blocks of code found in 2 locations. Consider refactoring.
Missing semicolon
const { a0, a1, b0, b1, c0, c1, f0, f1 } = proof
Missing semicolon
const { g, n } = Helper.deserializeParams(params)
Missing semicolon
const h = Helper.deserializeCurvePoint(publicKey)
Missing semicolon
const { a, b } = encryptedVote
 
Missing semicolon
const v1 = Helper.ECpow(g, f0).eq(Helper.ECmul(a0, Helper.ECpow(a, c0)))
Missing semicolon
const v2 = Helper.ECpow(g, f1).eq(Helper.ECmul(a1, Helper.ECpow(a, c1)))
Missing semicolon
const v3 = Helper.ECpow(h, f0).eq(Helper.ECmul(b0, Helper.ECpow(b, c0)))
Missing semicolon
const v4 = Helper.ECpow(h, f1).eq(Helper.ECmul(b1, Helper.ECpow(Helper.ECdiv(b, g), c1)))
Missing semicolon
const v5 = GlobalHelper.addBN(c0, c1, n).eq(generateChallenge(n, id, a, b, a0, b0, a1, b1))
 
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('g^f0 == a0*a^c0:\t', v1)
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log('g^f1 == a1*a^c1\t\t', v2)
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('h^f0 == b0*b^c0\t\t', v3)
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('h^f1 == b1*(b/g)^c1\t', v4)
unused expression, expected an assignment or function call
Missing semicolon
printConsole && console.log('c == c1 + c0\t\t', v5)
Missing semicolon
unused expression, expected an assignment or function call
printConsole && console.log()
 
Missing semicolon
return v1 && v2 && v3 && v4 && v5
Missing semicolon
}