forked from meck93/evote-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemSetup.ts
28 lines (23 loc) · 1.09 KB
/
systemSetup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import BN = require('bn.js')
import { ec as EC } from 'elliptic'
import { GlobalHelper } from '../index'
import { Curve, CurvePoint, Helper, KeyPair, SystemParameters } from './index'
import { curveDefinition } from './curve'
export const generateSystemParameters = (): SystemParameters => {
return { p: Curve.p, n: Curve.n, g: Curve.g }
}
export const generateKeyPair = (): KeyPair => {
const keyPair: EC.KeyPair = curveDefinition.genKeyPair()
const sk: BN = keyPair.getPrivate()
const h: CurvePoint = keyPair.getPublic() as CurvePoint
return { h, sk }
}
export const combinePublicKeys = (publicKeyShares: CurvePoint[]): CurvePoint => {
return publicKeyShares.reduce((product, share) => Helper.ECmul(product, share))
}
// combines multiple private key shares to one private key
// NOTE: this should not be used as the distributed secret keys will become "useless"
// it is only used for testing purpose
export const combinePrivateKeys = (params: SystemParameters, privateKeyShares: BN[]): BN => {
return privateKeyShares.reduce((sum, share) => GlobalHelper.addBN(sum, share, params.n))
}