diff --git a/packages/wasm-mps/src/lib.rs b/packages/wasm-mps/src/lib.rs index 01de3e5ed9a..4fd84e0a5e5 100644 --- a/packages/wasm-mps/src/lib.rs +++ b/packages/wasm-mps/src/lib.rs @@ -26,14 +26,14 @@ mod mps { /// Internal state used for round 1. #[derive(serde::Serialize, serde::Deserialize)] - struct StateR1 { + struct DkgStateR1 { pub msg: KeygenMsg1, pub party: KeygenParty, EdwardsPoint>, } /// Internal state used for round 2. #[derive(serde::Serialize, serde::Deserialize)] - struct StateR2 { + struct DkgStateR2 { pub msg: KeygenMsg2, pub party: KeygenParty, } @@ -56,7 +56,7 @@ mod mps { /// decryption_key: Private Curve25519 key. /// encryption_keys: Public Curve25519 keys of other parties. /// seed: PRNG seed for entropy. - pub fn round0_process( + pub fn dkg_round0_process( party_id: u8, decryption_key: &[u8; 32], encryption_keys: &[Vec; 2], @@ -107,7 +107,7 @@ mod mps { let (p1, msg1) = p0.process(()).map_err(|_| DkgError::ProtocolError)?; // Create the state for storage between rounds - let state = StateR1 { + let state = DkgStateR1 { msg: msg1, party: p1, }; @@ -121,12 +121,12 @@ mod mps { /// Process round 1 of protocol. /// round1_messages: Public messages from other parties. /// state: Private state result from from round 0. - pub fn round1_process( + pub fn dkg_round1_process( round1_messages: &[Vec; 2], state: &[u8], ) -> Result { // Parse state - let state: StateR1 = + let state: DkgStateR1 = bincode::deserialize(state).map_err(|_| DkgError::DeserializationError)?; // Parse messages @@ -143,7 +143,7 @@ mod mps { .map_err(|_| DkgError::ProtocolError)?; // Create the state for storage between rounds - let state = StateR2 { + let state = DkgStateR2 { msg: msg2.clone(), party: p2, }; @@ -157,7 +157,10 @@ mod mps { /// Process round 2 of protocol. /// round2_messages: Public messages from other parties. /// state: Private state result from round 1. - pub fn round2_process(round2_messages: &[Vec; 2], state: &[u8]) -> Result { + pub fn dkg_round2_process( + round2_messages: &[Vec; 2], + state: &[u8], + ) -> Result { // Deserialize round2 messages from other parties let i0_msg2: KeygenMsg2 = bincode::deserialize(round2_messages[0].as_slice()) .map_err(|_| DkgError::DeserializationError)?; @@ -165,7 +168,7 @@ mod mps { .map_err(|_| DkgError::DeserializationError)?; // Deserialize state - let state: StateR2 = + let state: DkgStateR2 = bincode::deserialize(state).map_err(|_| DkgError::DeserializationError)?; // Generate share @@ -204,7 +207,7 @@ mod tests { } // Parties generate their round 0 messages - let p0_0 = mps::round0_process( + let p0_0 = mps::dkg_round0_process( 0, &prv_keys[0].to_bytes(), &[ @@ -214,7 +217,7 @@ mod tests { &seeds[0], ) .unwrap(); - let p1_0 = mps::round0_process( + let p1_0 = mps::dkg_round0_process( 1, &prv_keys[1].to_bytes(), &[ @@ -224,7 +227,7 @@ mod tests { &seeds[1], ) .unwrap(); - let p2_0 = mps::round0_process( + let p2_0 = mps::dkg_round0_process( 2, &prv_keys[2].to_bytes(), &[ @@ -237,24 +240,24 @@ mod tests { // Parties generate their round 1 messages let p0_1 = - mps::round1_process(&[p1_0.msg.clone(), p2_0.msg.clone()], p0_0.state.as_slice()) + mps::dkg_round1_process(&[p1_0.msg.clone(), p2_0.msg.clone()], p0_0.state.as_slice()) .unwrap(); let p1_1 = - mps::round1_process(&[p0_0.msg.clone(), p2_0.msg.clone()], p1_0.state.as_slice()) + mps::dkg_round1_process(&[p0_0.msg.clone(), p2_0.msg.clone()], p1_0.state.as_slice()) .unwrap(); let p2_1 = - mps::round1_process(&[p0_0.msg.clone(), p1_0.msg.clone()], p2_0.state.as_slice()) + mps::dkg_round1_process(&[p0_0.msg.clone(), p1_0.msg.clone()], p2_0.state.as_slice()) .unwrap(); // Parties generate their key shares let p0_share = - mps::round2_process(&[p1_1.msg.clone(), p2_1.msg.clone()], p0_1.state.as_slice()) + mps::dkg_round2_process(&[p1_1.msg.clone(), p2_1.msg.clone()], p0_1.state.as_slice()) .unwrap(); let p1_share = - mps::round2_process(&[p0_1.msg.clone(), p2_1.msg.clone()], p1_1.state.as_slice()) + mps::dkg_round2_process(&[p0_1.msg.clone(), p2_1.msg.clone()], p1_1.state.as_slice()) .unwrap(); let p2_share = - mps::round2_process(&[p0_1.msg.clone(), p1_1.msg.clone()], p2_1.state.as_slice()) + mps::dkg_round2_process(&[p0_1.msg.clone(), p1_1.msg.clone()], p2_1.state.as_slice()) .unwrap(); // Assert generated public keys are equal @@ -333,7 +336,7 @@ impl MsgShare { } #[wasm_bindgen] -pub fn round0_process( +pub fn dkg_round0_process( party_id: u8, decryption_key: &[u8], encryption_keys: Array, @@ -343,7 +346,7 @@ pub fn round0_process( .try_into() .map_err(|_| "Deserialization Error")?; let seed_32: [u8; 32] = seed[..32].try_into().map_err(|_| "Deserialization Error")?; - let result = mps::round0_process( + let result = mps::dkg_round0_process( party_id, &decryption_key_32, &[ @@ -361,8 +364,8 @@ pub fn round0_process( } #[wasm_bindgen] -pub fn round1_process(round1_messages: Array, state: &[u8]) -> Result { - let result = mps::round1_process( +pub fn dkg_round1_process(round1_messages: Array, state: &[u8]) -> Result { + let result = mps::dkg_round1_process( &[ js_sys::Uint8Array::from(round1_messages.get(0)).to_vec(), js_sys::Uint8Array::from(round1_messages.get(1)).to_vec(), @@ -378,8 +381,8 @@ pub fn round1_process(round1_messages: Array, state: &[u8]) -> Result Result { - let result = mps::round2_process( +pub fn dkg_round2_process(round2_messages: Array, state: &[u8]) -> Result { + let result = mps::dkg_round2_process( &[ js_sys::Uint8Array::from(round2_messages.get(0)).to_vec(), js_sys::Uint8Array::from(round2_messages.get(1)).to_vec(), diff --git a/packages/wasm-mps/test/mps.ts b/packages/wasm-mps/test/mps.ts index b327f2fb54e..036bd5cb858 100644 --- a/packages/wasm-mps/test/mps.ts +++ b/packages/wasm-mps/test/mps.ts @@ -21,7 +21,7 @@ describe("mps", function () { it("performs round 0", function () { for (let i = 0; i < 3; i++) { - mps.round0_process( + mps.dkg_round0_process( i, keypairs[i].privateKey, otherIndices[i].map((i) => keypairs[i].publicKey), @@ -34,7 +34,7 @@ describe("mps", function () { before("performs round 0", function () { results1 = [0, 1, 2].map((i) => - mps.round0_process( + mps.dkg_round0_process( i, keypairs[i].privateKey, otherIndices[i].map((i) => keypairs[i].publicKey), @@ -45,7 +45,7 @@ describe("mps", function () { it("performs round 1", function () { for (let i = 0; i < 3; i++) { - mps.round1_process( + mps.dkg_round1_process( otherIndices[i].map((i) => results1[i].msg), results1[i].state, ); @@ -56,7 +56,7 @@ describe("mps", function () { before("performs round 1", function () { results2 = [0, 1, 2].map((i) => - mps.round1_process( + mps.dkg_round1_process( otherIndices[i].map((i) => results1[i].msg), results1[i].state, ), @@ -65,7 +65,7 @@ describe("mps", function () { it("performs round 2", function () { const results3 = [0, 1, 2].map((i) => - mps.round2_process( + mps.dkg_round2_process( otherIndices[i].map((i) => results2[i].msg), results2[i].state, ), @@ -88,7 +88,7 @@ describe("mps", function () { describe("round0_process", function () { it("does not panic on bad party size", function () { shouldThrow(() => - mps.round0_process( + mps.dkg_round0_process( "255", Buffer.alloc(32), [Buffer.alloc(32), Buffer.alloc(32)], @@ -99,7 +99,7 @@ describe("mps", function () { it("does not panic on bad encryption key", function () { shouldThrow(() => - mps.round0_process( + mps.dkg_round0_process( 0, "encryption key", [Buffer.alloc(32), Buffer.alloc(32)], @@ -107,7 +107,7 @@ describe("mps", function () { ), ); shouldThrow(() => - mps.round0_process( + mps.dkg_round0_process( 0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(32)], @@ -118,17 +118,17 @@ describe("mps", function () { it("does not panic on bad decryption keys", function () { shouldThrow(() => - mps.round0_process(0, Buffer.alloc(0), "decryption keys", crypto.randomBytes(32)), + mps.dkg_round0_process(0, Buffer.alloc(0), "decryption keys", crypto.randomBytes(32)), ); - shouldThrow(() => mps.round0_process(0, Buffer.alloc(0), [], crypto.randomBytes(32))); + shouldThrow(() => mps.dkg_round0_process(0, Buffer.alloc(0), [], crypto.randomBytes(32))); shouldThrow(() => - mps.round0_process(0, Buffer.alloc(0), ["decryption key"], crypto.randomBytes(32)), + mps.dkg_round0_process(0, Buffer.alloc(0), ["decryption key"], crypto.randomBytes(32)), ); shouldThrow(() => - mps.round0_process(0, Buffer.alloc(0), [Buffer.alloc(0)], crypto.randomBytes(32)), + mps.dkg_round0_process(0, Buffer.alloc(0), [Buffer.alloc(0)], crypto.randomBytes(32)), ); shouldThrow(() => - mps.round0_process( + mps.dkg_round0_process( 0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(0)], @@ -139,10 +139,10 @@ describe("mps", function () { it("does not panic on bad seed", function () { shouldThrow(() => - mps.round0_process(0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(32)], "seed"), + mps.dkg_round0_process(0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(32)], "seed"), ); shouldThrow(() => - mps.round0_process( + mps.dkg_round0_process( 0, Buffer.alloc(0), [Buffer.alloc(32), Buffer.alloc(32)], @@ -154,32 +154,32 @@ describe("mps", function () { describe("round1_process", function () { it("does not panic on bad messages", function () { - shouldThrow(() => mps.round1_process("messages", Buffer.alloc(1224))); - shouldThrow(() => mps.round1_process([], Buffer.alloc(1224))); - shouldThrow(() => mps.round1_process(["message"], Buffer.alloc(1224))); - shouldThrow(() => mps.round1_process([Buffer.alloc(0), Buffer.alloc(1224)])); + shouldThrow(() => mps.dkg_round1_process("messages", Buffer.alloc(1224))); + shouldThrow(() => mps.dkg_round1_process([], Buffer.alloc(1224))); + shouldThrow(() => mps.dkg_round1_process(["message"], Buffer.alloc(1224))); + shouldThrow(() => mps.dkg_round1_process([Buffer.alloc(0), Buffer.alloc(1224)])); }); it("does not panic on bad state", function () { - shouldThrow(() => mps.round1_process([Buffer.alloc(65), Buffer.alloc(65)], "state")); + shouldThrow(() => mps.dkg_round1_process([Buffer.alloc(65), Buffer.alloc(65)], "state")); shouldThrow(() => - mps.round1_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)), + mps.dkg_round1_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)), ); }); }); describe("round2_process", function () { it("does not panic on bad messages", function () { - shouldThrow(() => mps.round2_process("messages", Buffer.alloc(1224))); - shouldThrow(() => mps.round2_process([], Buffer.alloc(1224))); - shouldThrow(() => mps.round2_process(["message"], Buffer.alloc(1224))); - shouldThrow(() => mps.round2_process([Buffer.alloc(0), Buffer.alloc(1224)])); + shouldThrow(() => mps.dkg_round2_process("messages", Buffer.alloc(1224))); + shouldThrow(() => mps.dkg_round2_process([], Buffer.alloc(1224))); + shouldThrow(() => mps.dkg_round2_process(["message"], Buffer.alloc(1224))); + shouldThrow(() => mps.dkg_round2_process([Buffer.alloc(0), Buffer.alloc(1224)])); }); it("does not panic on bad state", function () { - shouldThrow(() => mps.round2_process([Buffer.alloc(65), Buffer.alloc(65)], "state")); + shouldThrow(() => mps.dkg_round2_process([Buffer.alloc(65), Buffer.alloc(65)], "state")); shouldThrow(() => - mps.round2_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)), + mps.dkg_round2_process([Buffer.alloc(65), Buffer.alloc(65)], Buffer.alloc(0)), ); }); });