-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparams-and-keys.ts
143 lines (127 loc) · 4.86 KB
/
params-and-keys.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { BytearrayWrapper } from '../bytearray-wrapper';
import {
accumulatorDeriveMembershipProvingKeyFromNonMembershipKey,
generateAccumulatorKeyPair,
generateAccumulatorParams,
generateAccumulatorPublicKey,
generateAccumulatorSecretKey,
generateMembershipProvingKey,
generateNonMembershipProvingKey,
isAccumulatorParamsValid,
isAccumulatorPublicKeyValid,
generateAccumulatorParamsForKeyedVerification,
generateAccumulatorPublicKeyForKeyedVerification
} from 'crypto-wasm-new';
export class AccumulatorParams extends BytearrayWrapper {
/**
* Generate accumulator parameters. They are needed to generate public key and initialize the accumulator.
* @param label - Pass to generate parameters deterministically.
* @returns
*/
static generate(label?: Uint8Array): AccumulatorParams {
return new AccumulatorParams(generateAccumulatorParams(label));
}
/**
* Check if parameters are valid. Before verifying witness or using for proof verification,
* make sure the params are valid.
* @returns true if key is valid, false otherwise
*/
isValid(): boolean {
return isAccumulatorParamsValid(this.value);
}
}
export class AccumulatorSecretKey extends BytearrayWrapper {
/**
* Generate secret key for the accumulator manager who updates the accumulator and creates witnesses.
* @param seed - Pass to generate key deterministically.
* @returns
*/
static generate(seed?: Uint8Array): AccumulatorSecretKey {
return new AccumulatorSecretKey(generateAccumulatorSecretKey(seed));
}
/**
* Generate public key from given params and secret key.
* @param params
* @returns
*/
generatePublicKey(params: AccumulatorParams): AccumulatorPublicKey {
return new AccumulatorPublicKey(generateAccumulatorPublicKey(this.value, params.value));
}
}
export class AccumulatorPublicKey extends BytearrayWrapper {
static generate(secretKey: AccumulatorSecretKey, params: AccumulatorParams): AccumulatorPublicKey {
return secretKey.generatePublicKey(params);
}
/**
* Check if public key is valid. Before verifying witness or using for proof verification,
* make sure the public key is valid.
* @returns true if key is valid, false otherwise
*/
isValid(): boolean {
return isAccumulatorPublicKeyValid(this.value);
}
}
export class AccumulatorKeypair {
sk: AccumulatorSecretKey;
pk: AccumulatorPublicKey;
static generate(params: AccumulatorParams, seed?: Uint8Array): AccumulatorKeypair {
const keypair = generateAccumulatorKeyPair(params.value, seed);
return new AccumulatorKeypair(
new AccumulatorSecretKey(keypair.secret_key),
new AccumulatorPublicKey(keypair.public_key)
);
}
constructor(sk: AccumulatorSecretKey, pk: AccumulatorPublicKey) {
this.sk = sk;
this.pk = pk;
}
get secretKey(): AccumulatorSecretKey {
return this.sk;
}
get publicKey(): AccumulatorPublicKey {
return this.pk;
}
}
/**
* Generate proving key for proving membership in an accumulator in zero knowledge. Proving key is
* public data that must be known to both the prover and verifier. Any prover and verifier pair can mutually agree
* on a proving key and the manager does not need to be aware of any proving key.
* @param label - The bytearray that is hashed to deterministically generate the proving key.
*/
export class MembershipProvingKey extends BytearrayWrapper {
static generate(label?: Uint8Array): MembershipProvingKey {
return new MembershipProvingKey(generateMembershipProvingKey(label));
}
}
/**
* Generate proving key for proving non-membership in a universal accumulator in zero knowledge.
* @param label - The bytearray that is hashed to deterministically generate the proving key.
*/
export class NonMembershipProvingKey extends BytearrayWrapper {
static generate(label?: Uint8Array): NonMembershipProvingKey {
return new NonMembershipProvingKey(generateNonMembershipProvingKey(label));
}
deriveMembershipProvingKey(): MembershipProvingKey {
return new MembershipProvingKey(accumulatorDeriveMembershipProvingKeyFromNonMembershipKey(this.value));
}
}
export class AccumulatorParamsForKeyedVerification extends BytearrayWrapper {
/**
* Generate accumulator parameters for keyed-verification.
* @param label - Pass to generate parameters deterministically.
* @returns
*/
static generate(label?: Uint8Array): AccumulatorParamsForKeyedVerification {
return new AccumulatorParamsForKeyedVerification(generateAccumulatorParamsForKeyedVerification(label));
}
}
export class AccumulatorPublicKeyForKeyedVerification extends BytearrayWrapper {
static generate(
secretKey: AccumulatorSecretKey,
params: AccumulatorParamsForKeyedVerification
): AccumulatorPublicKeyForKeyedVerification {
return new AccumulatorPublicKeyForKeyedVerification(
generateAccumulatorPublicKeyForKeyedVerification(secretKey.value, params.value)
);
}
}