meck93/evote-crypto

View on GitHub
src/helper.ts

Summary

Maintainability
A
3 hrs
Test Coverage
Missing semicolon
import BN = require('bn.js')
Missing semicolon
import crypto = require('crypto')
 
Missing semicolon
export const newBN = (n: number, base = 10): BN => new BN(n, base)
Missing semicolon
export const invmBN = (a: BN, modulus: BN): BN => a.invm(modulus)
Similar blocks of code found in 3 locations. Consider refactoring.
Missing semicolon
export const addBN = (a: BN, b: BN, modulus: BN): BN => a.add(b).mod(modulus)
Similar blocks of code found in 3 locations. Consider refactoring.
Missing semicolon
export const subBN = (a: BN, b: BN, modulus: BN): BN => a.sub(b).mod(modulus)
Similar blocks of code found in 3 locations. Consider refactoring.
Missing semicolon
export const mulBN = (a: BN, b: BN, modulus: BN): BN => a.mul(b).mod(modulus)
Missing semicolon
export const divBN = (a: BN, b: BN, modulus: BN): BN => mulBN(a, invmBN(b, modulus), modulus)
Missing semicolon
export const powBN = (a: BN, b: BN, modulus: BN): BN => a.pow(b).mod(modulus)
 
// compute the required number of bytes to store a decimal
export const getByteSizeForDecimalNumber = (n: BN): BN => {
Missing semicolon
const modulus: BN = n.mod(new BN(256, 10))
Missing semicolon
const smallerHalf: boolean = modulus.lt(new BN(128, 10))
Missing semicolon
const result: BN = n.divRound(new BN(256, 10))
Missing semicolon
return smallerHalf ? result.add(new BN(1, 10)) : result
Missing semicolon
}
 
// get a secure random value x: 0 < x < n
export const getSecureRandomValue = (n: BN): BN => {
Missing semicolon
const ONE: BN = new BN(1, 10)
Missing semicolon
const UPPER_BOUND_RANDOM: BN = n.sub(ONE)
Missing semicolon
const BYTE_SIZE: BN = getByteSizeForDecimalNumber(n)
 
Missing semicolon
let byteSize: number
try {
Missing semicolon
byteSize = BYTE_SIZE.toNumber()
} catch {
// https://www.ecma-international.org/ecma-262/5.1/#sec-8.5
// used for large numbers from EC
Missing semicolon
byteSize = 32
}
 
Missing semicolon
let randomBytes: Buffer = crypto.randomBytes(byteSize)
Missing semicolon
let randomValue: BN = new BN(randomBytes)
 
// ensure that the random value is in range [1, n-1]
while (!(randomValue.lte(UPPER_BOUND_RANDOM) && randomValue.gte(ONE))) {
Missing semicolon
randomBytes = crypto.randomBytes(byteSize)
Missing semicolon
randomValue = new BN(randomBytes)
}
Missing semicolon
return randomValue
Missing semicolon
}
 
export const timingSafeEqual = (a: Buffer, b: Buffer): boolean => {
if (!Buffer.isBuffer(a)) {
Missing semicolon
throw new TypeError('First argument must be a buffer')
}
if (!Buffer.isBuffer(b)) {
Missing semicolon
throw new TypeError('Second argument must be a buffer')
}
 
// check if both buffers have the same length but don't leak any information about it
// if buffers don't have the same length -> therefore, compare buffer a with itself and always return false
// if buffers have the same length -> XOR every position in the Buffer
Missing semicolon
let mismatch = a.length === b.length ? 0 : 1
 
if (mismatch) {
Missing semicolon
b = a
}
 
for (let i = 0, len = a.length; i < len; i++) {
Forbidden bitwise operation
Missing semicolon
mismatch |= a[i] ^ b[i]
}
Missing semicolon
return mismatch === 0
Missing semicolon
}
 
export const timingSafeEqualBN = (a: BN, b: BN): boolean => {
if (!BN.isBN(a)) {
Missing semicolon
throw new TypeError('First argument must be of type: BN')
}
if (!BN.isBN(b)) {
Missing semicolon
throw new TypeError('Second argument must be of type: BN')
}
Missing semicolon
const a_ = new Buffer(a.toArray())
Missing semicolon
const b_ = new Buffer(b.toArray())
Missing semicolon
return timingSafeEqual(a_, b_)
Missing semicolon
}