Skip to content

Commit 092a38d

Browse files
Merge pull request #184 from BitGo/HSM-1425-namespace-prefix
chore(wasm-mps): namespace prefix mps dkg functions
2 parents 822f3f1 + e1c12c1 commit 092a38d

2 files changed

Lines changed: 54 additions & 51 deletions

File tree

packages/wasm-mps/src/lib.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ mod mps {
2626

2727
/// Internal state used for round 1.
2828
#[derive(serde::Serialize, serde::Deserialize)]
29-
struct StateR1 {
29+
struct DkgStateR1 {
3030
pub msg: KeygenMsg1,
3131
pub party: KeygenParty<R1<EdwardsPoint>, EdwardsPoint>,
3232
}
3333

3434
/// Internal state used for round 2.
3535
#[derive(serde::Serialize, serde::Deserialize)]
36-
struct StateR2 {
36+
struct DkgStateR2 {
3737
pub msg: KeygenMsg2<EdwardsPoint>,
3838
pub party: KeygenParty<R2, EdwardsPoint>,
3939
}
@@ -56,7 +56,7 @@ mod mps {
5656
/// decryption_key: Private Curve25519 key.
5757
/// encryption_keys: Public Curve25519 keys of other parties.
5858
/// seed: PRNG seed for entropy.
59-
pub fn round0_process(
59+
pub fn dkg_round0_process(
6060
party_id: u8,
6161
decryption_key: &[u8; 32],
6262
encryption_keys: &[Vec<u8>; 2],
@@ -107,7 +107,7 @@ mod mps {
107107
let (p1, msg1) = p0.process(()).map_err(|_| DkgError::ProtocolError)?;
108108

109109
// Create the state for storage between rounds
110-
let state = StateR1 {
110+
let state = DkgStateR1 {
111111
msg: msg1,
112112
party: p1,
113113
};
@@ -121,12 +121,12 @@ mod mps {
121121
/// Process round 1 of protocol.
122122
/// round1_messages: Public messages from other parties.
123123
/// state: Private state result from from round 0.
124-
pub fn round1_process(
124+
pub fn dkg_round1_process(
125125
round1_messages: &[Vec<u8>; 2],
126126
state: &[u8],
127127
) -> Result<MsgState, DkgError> {
128128
// Parse state
129-
let state: StateR1 =
129+
let state: DkgStateR1 =
130130
bincode::deserialize(state).map_err(|_| DkgError::DeserializationError)?;
131131

132132
// Parse messages
@@ -143,7 +143,7 @@ mod mps {
143143
.map_err(|_| DkgError::ProtocolError)?;
144144

145145
// Create the state for storage between rounds
146-
let state = StateR2 {
146+
let state = DkgStateR2 {
147147
msg: msg2.clone(),
148148
party: p2,
149149
};
@@ -157,15 +157,18 @@ mod mps {
157157
/// Process round 2 of protocol.
158158
/// round2_messages: Public messages from other parties.
159159
/// state: Private state result from round 1.
160-
pub fn round2_process(round2_messages: &[Vec<u8>; 2], state: &[u8]) -> Result<Share, DkgError> {
160+
pub fn dkg_round2_process(
161+
round2_messages: &[Vec<u8>; 2],
162+
state: &[u8],
163+
) -> Result<Share, DkgError> {
161164
// Deserialize round2 messages from other parties
162165
let i0_msg2: KeygenMsg2<EdwardsPoint> = bincode::deserialize(round2_messages[0].as_slice())
163166
.map_err(|_| DkgError::DeserializationError)?;
164167
let i1_msg2: KeygenMsg2<EdwardsPoint> = bincode::deserialize(round2_messages[1].as_slice())
165168
.map_err(|_| DkgError::DeserializationError)?;
166169

167170
// Deserialize state
168-
let state: StateR2 =
171+
let state: DkgStateR2 =
169172
bincode::deserialize(state).map_err(|_| DkgError::DeserializationError)?;
170173

171174
// Generate share
@@ -204,7 +207,7 @@ mod tests {
204207
}
205208

206209
// Parties generate their round 0 messages
207-
let p0_0 = mps::round0_process(
210+
let p0_0 = mps::dkg_round0_process(
208211
0,
209212
&prv_keys[0].to_bytes(),
210213
&[
@@ -214,7 +217,7 @@ mod tests {
214217
&seeds[0],
215218
)
216219
.unwrap();
217-
let p1_0 = mps::round0_process(
220+
let p1_0 = mps::dkg_round0_process(
218221
1,
219222
&prv_keys[1].to_bytes(),
220223
&[
@@ -224,7 +227,7 @@ mod tests {
224227
&seeds[1],
225228
)
226229
.unwrap();
227-
let p2_0 = mps::round0_process(
230+
let p2_0 = mps::dkg_round0_process(
228231
2,
229232
&prv_keys[2].to_bytes(),
230233
&[
@@ -237,24 +240,24 @@ mod tests {
237240

238241
// Parties generate their round 1 messages
239242
let p0_1 =
240-
mps::round1_process(&[p1_0.msg.clone(), p2_0.msg.clone()], p0_0.state.as_slice())
243+
mps::dkg_round1_process(&[p1_0.msg.clone(), p2_0.msg.clone()], p0_0.state.as_slice())
241244
.unwrap();
242245
let p1_1 =
243-
mps::round1_process(&[p0_0.msg.clone(), p2_0.msg.clone()], p1_0.state.as_slice())
246+
mps::dkg_round1_process(&[p0_0.msg.clone(), p2_0.msg.clone()], p1_0.state.as_slice())
244247
.unwrap();
245248
let p2_1 =
246-
mps::round1_process(&[p0_0.msg.clone(), p1_0.msg.clone()], p2_0.state.as_slice())
249+
mps::dkg_round1_process(&[p0_0.msg.clone(), p1_0.msg.clone()], p2_0.state.as_slice())
247250
.unwrap();
248251

249252
// Parties generate their key shares
250253
let p0_share =
251-
mps::round2_process(&[p1_1.msg.clone(), p2_1.msg.clone()], p0_1.state.as_slice())
254+
mps::dkg_round2_process(&[p1_1.msg.clone(), p2_1.msg.clone()], p0_1.state.as_slice())
252255
.unwrap();
253256
let p1_share =
254-
mps::round2_process(&[p0_1.msg.clone(), p2_1.msg.clone()], p1_1.state.as_slice())
257+
mps::dkg_round2_process(&[p0_1.msg.clone(), p2_1.msg.clone()], p1_1.state.as_slice())
255258
.unwrap();
256259
let p2_share =
257-
mps::round2_process(&[p0_1.msg.clone(), p1_1.msg.clone()], p2_1.state.as_slice())
260+
mps::dkg_round2_process(&[p0_1.msg.clone(), p1_1.msg.clone()], p2_1.state.as_slice())
258261
.unwrap();
259262

260263
// Assert generated public keys are equal
@@ -333,7 +336,7 @@ impl MsgShare {
333336
}
334337

335338
#[wasm_bindgen]
336-
pub fn round0_process(
339+
pub fn dkg_round0_process(
337340
party_id: u8,
338341
decryption_key: &[u8],
339342
encryption_keys: Array,
@@ -343,7 +346,7 @@ pub fn round0_process(
343346
.try_into()
344347
.map_err(|_| "Deserialization Error")?;
345348
let seed_32: [u8; 32] = seed[..32].try_into().map_err(|_| "Deserialization Error")?;
346-
let result = mps::round0_process(
349+
let result = mps::dkg_round0_process(
347350
party_id,
348351
&decryption_key_32,
349352
&[
@@ -361,8 +364,8 @@ pub fn round0_process(
361364
}
362365

363366
#[wasm_bindgen]
364-
pub fn round1_process(round1_messages: Array, state: &[u8]) -> Result<MsgState, String> {
365-
let result = mps::round1_process(
367+
pub fn dkg_round1_process(round1_messages: Array, state: &[u8]) -> Result<MsgState, String> {
368+
let result = mps::dkg_round1_process(
366369
&[
367370
js_sys::Uint8Array::from(round1_messages.get(0)).to_vec(),
368371
js_sys::Uint8Array::from(round1_messages.get(1)).to_vec(),
@@ -378,8 +381,8 @@ pub fn round1_process(round1_messages: Array, state: &[u8]) -> Result<MsgState,
378381
}
379382

380383
#[wasm_bindgen]
381-
pub fn round2_process(round2_messages: Array, state: &[u8]) -> Result<Share, String> {
382-
let result = mps::round2_process(
384+
pub fn dkg_round2_process(round2_messages: Array, state: &[u8]) -> Result<Share, String> {
385+
let result = mps::dkg_round2_process(
383386
&[
384387
js_sys::Uint8Array::from(round2_messages.get(0)).to_vec(),
385388
js_sys::Uint8Array::from(round2_messages.get(1)).to_vec(),

packages/wasm-mps/test/mps.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("mps", function () {
2121

2222
it("performs round 0", function () {
2323
for (let i = 0; i < 3; i++) {
24-
mps.round0_process(
24+
mps.dkg_round0_process(
2525
i,
2626
keypairs[i].privateKey,
2727
otherIndices[i].map((i) => keypairs[i].publicKey),
@@ -34,7 +34,7 @@ describe("mps", function () {
3434

3535
before("performs round 0", function () {
3636
results1 = [0, 1, 2].map((i) =>
37-
mps.round0_process(
37+
mps.dkg_round0_process(
3838
i,
3939
keypairs[i].privateKey,
4040
otherIndices[i].map((i) => keypairs[i].publicKey),
@@ -45,7 +45,7 @@ describe("mps", function () {
4545

4646
it("performs round 1", function () {
4747
for (let i = 0; i < 3; i++) {
48-
mps.round1_process(
48+
mps.dkg_round1_process(
4949
otherIndices[i].map((i) => results1[i].msg),
5050
results1[i].state,
5151
);
@@ -56,7 +56,7 @@ describe("mps", function () {
5656

5757
before("performs round 1", function () {
5858
results2 = [0, 1, 2].map((i) =>
59-
mps.round1_process(
59+
mps.dkg_round1_process(
6060
otherIndices[i].map((i) => results1[i].msg),
6161
results1[i].state,
6262
),
@@ -65,7 +65,7 @@ describe("mps", function () {
6565

6666
it("performs round 2", function () {
6767
const results3 = [0, 1, 2].map((i) =>
68-
mps.round2_process(
68+
mps.dkg_round2_process(
6969
otherIndices[i].map((i) => results2[i].msg),
7070
results2[i].state,
7171
),
@@ -88,7 +88,7 @@ describe("mps", function () {
8888
describe("round0_process", function () {
8989
it("does not panic on bad party size", function () {
9090
shouldThrow(() =>
91-
mps.round0_process(
91+
mps.dkg_round0_process(
9292
"255",
9393
Buffer.alloc(32),
9494
[Buffer.alloc(32), Buffer.alloc(32)],
@@ -99,15 +99,15 @@ describe("mps", function () {
9999

100100
it("does not panic on bad encryption key", function () {
101101
shouldThrow(() =>
102-
mps.round0_process(
102+
mps.dkg_round0_process(
103103
0,
104104
"encryption key",
105105
[Buffer.alloc(32), Buffer.alloc(32)],
106106
crypto.randomBytes(32),
107107
),
108108
);
109109
shouldThrow(() =>
110-
mps.round0_process(
110+
mps.dkg_round0_process(
111111
0,
112112
Buffer.alloc(0),
113113
[Buffer.alloc(32), Buffer.alloc(32)],
@@ -118,17 +118,17 @@ describe("mps", function () {
118118

119119
it("does not panic on bad decryption keys", function () {
120120
shouldThrow(() =>
121-
mps.round0_process(0, Buffer.alloc(0), "decryption keys", crypto.randomBytes(32)),
121+
mps.dkg_round0_process(0, Buffer.alloc(0), "decryption keys", crypto.randomBytes(32)),
122122
);
123-
shouldThrow(() => mps.round0_process(0, Buffer.alloc(0), [], crypto.randomBytes(32)));
123+
shouldThrow(() => mps.dkg_round0_process(0, Buffer.alloc(0), [], crypto.randomBytes(32)));
124124
shouldThrow(() =>
125-
mps.round0_process(0, Buffer.alloc(0), ["decryption key"], crypto.randomBytes(32)),
125+
mps.dkg_round0_process(0, Buffer.alloc(0), ["decryption key"], crypto.randomBytes(32)),
126126
);
127127
shouldThrow(() =>
128-
mps.round0_process(0, Buffer.alloc(0), [Buffer.alloc(0)], crypto.randomBytes(32)),
128+
mps.dkg_round0_process(0, Buffer.alloc(0), [Buffer.alloc(0)], crypto.randomBytes(32)),
129129
);
130130
shouldThrow(() =>
131-
mps.round0_process(
131+
mps.dkg_round0_process(
132132
0,
133133
Buffer.alloc(0),
134134
[Buffer.alloc(32), Buffer.alloc(0)],
@@ -139,10 +139,10 @@ describe("mps", function () {
139139

140140
it("does not panic on bad seed", function () {
141141
shouldThrow(() =>
142-
mps.round0_process(0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(32)], "seed"),
142+
mps.dkg_round0_process(0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(32)], "seed"),
143143
);
144144
shouldThrow(() =>
145-
mps.round0_process(
145+
mps.dkg_round0_process(
146146
0,
147147
Buffer.alloc(0),
148148
[Buffer.alloc(32), Buffer.alloc(32)],
@@ -154,32 +154,32 @@ describe("mps", function () {
154154

155155
describe("round1_process", function () {
156156
it("does not panic on bad messages", function () {
157-
shouldThrow(() => mps.round1_process("messages", Buffer.alloc(1224)));
158-
shouldThrow(() => mps.round1_process([], Buffer.alloc(1224)));
159-
shouldThrow(() => mps.round1_process(["message"], Buffer.alloc(1224)));
160-
shouldThrow(() => mps.round1_process([Buffer.alloc(0), Buffer.alloc(1224)]));
157+
shouldThrow(() => mps.dkg_round1_process("messages", Buffer.alloc(1224)));
158+
shouldThrow(() => mps.dkg_round1_process([], Buffer.alloc(1224)));
159+
shouldThrow(() => mps.dkg_round1_process(["message"], Buffer.alloc(1224)));
160+
shouldThrow(() => mps.dkg_round1_process([Buffer.alloc(0), Buffer.alloc(1224)]));
161161
});
162162

163163
it("does not panic on bad state", function () {
164-
shouldThrow(() => mps.round1_process([Buffer.alloc(65), Buffer.alloc(65)], "state"));
164+
shouldThrow(() => mps.dkg_round1_process([Buffer.alloc(65), Buffer.alloc(65)], "state"));
165165
shouldThrow(() =>
166-
mps.round1_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)),
166+
mps.dkg_round1_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)),
167167
);
168168
});
169169
});
170170

171171
describe("round2_process", function () {
172172
it("does not panic on bad messages", function () {
173-
shouldThrow(() => mps.round2_process("messages", Buffer.alloc(1224)));
174-
shouldThrow(() => mps.round2_process([], Buffer.alloc(1224)));
175-
shouldThrow(() => mps.round2_process(["message"], Buffer.alloc(1224)));
176-
shouldThrow(() => mps.round2_process([Buffer.alloc(0), Buffer.alloc(1224)]));
173+
shouldThrow(() => mps.dkg_round2_process("messages", Buffer.alloc(1224)));
174+
shouldThrow(() => mps.dkg_round2_process([], Buffer.alloc(1224)));
175+
shouldThrow(() => mps.dkg_round2_process(["message"], Buffer.alloc(1224)));
176+
shouldThrow(() => mps.dkg_round2_process([Buffer.alloc(0), Buffer.alloc(1224)]));
177177
});
178178

179179
it("does not panic on bad state", function () {
180-
shouldThrow(() => mps.round2_process([Buffer.alloc(65), Buffer.alloc(65)], "state"));
180+
shouldThrow(() => mps.dkg_round2_process([Buffer.alloc(65), Buffer.alloc(65)], "state"));
181181
shouldThrow(() =>
182-
mps.round2_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)),
182+
mps.dkg_round2_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)),
183183
);
184184
});
185185
});

0 commit comments

Comments
 (0)