Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: absract signer #10

Open
wants to merge 1 commit into
base: feat--signinging-protocol-refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/mpc/keygen/keygenMessages/abstractKeygenBroadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ import {
} from "./broadcasts";

export class AbstractKeygenBroadcast {
public readonly from: PartyId;
public readonly type: number;

constructor(from: PartyId, type: number) {
this.from = from;
this.type = type;
}

public toJSON():
| KeygenBroadcastForRound2JSON
| KeygenBroadcastForRound3JSON
| KeygenBroadcastForRound4JSON
| KeygenBroadcastForRound5JSON {
throw new Error("toJSON method must be implemented in derived classes");
}

public static fromJSON(
json:
| KeygenBroadcastForRound2JSON
Expand Down
13 changes: 13 additions & 0 deletions src/mpc/keygen/keygenMessages/abstractKeygenDirectMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { KeygenDirectMessageForRound4JSON } from "../types";
import { KeygenDirectMessageForRound4 } from "./directMessages";

export class AbstractKeygenDirectMessage {
public static fromJSON(json: KeygenDirectMessageForRound4JSON): AbstractKeygenDirectMessage {
switch (json.type) {
case 4:
return KeygenDirectMessageForRound4.fromJSON(json as KeygenDirectMessageForRound4JSON);
default:
throw new Error("Invalid round type");
}
}
}
28 changes: 20 additions & 8 deletions src/mpc/keygen/keygenMessages/broadcasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import {
KeygenBroadcastForRound5JSON,
} from "../types";

export class KeygenBroadcastForRound2 extends AbstractKeygenBroadcast {
export class KeygenBroadcastForRound2 {
public readonly commitment: Uint8Array;
public readonly from: PartyId;
public readonly type: 2;

constructor(from: PartyId, commitment: Uint8Array) {
super(from, 2);
this.from = from;
this.commitment = commitment;
this.type = 2;
}

public toJSON(): KeygenBroadcastForRound2JSON {
Expand All @@ -37,7 +40,9 @@ export class KeygenBroadcastForRound2 extends AbstractKeygenBroadcast {
}
}

export class KeygenBroadcastForRound3 extends AbstractKeygenBroadcast {
export class KeygenBroadcastForRound3 {
public readonly from: PartyId;
public readonly type: 3;
public readonly RID: bigint;
public readonly C: bigint;
public readonly vssPolynomial: Exponent;
Expand All @@ -56,7 +61,8 @@ export class KeygenBroadcastForRound3 extends AbstractKeygenBroadcast {
pedersenPublic: PedersenParams,
decommitment: Uint8Array
) {
super(from, 3);
this.from = from;
this.type = 3;
this.RID = RID;
this.C = C;
this.vssPolynomial = vssPolynomial;
Expand Down Expand Up @@ -107,12 +113,15 @@ export class KeygenBroadcastForRound3 extends AbstractKeygenBroadcast {
}
}

export class KeygenBroadcastForRound4 extends AbstractKeygenBroadcast {
export class KeygenBroadcastForRound4 {
public readonly from: PartyId;
public readonly type: 4;
public readonly modProof: ZkModProof;
public readonly prmProof: ZkPrmProof;

constructor(from: PartyId, modProof: ZkModProof, prmProof: ZkPrmProof) {
super(from, 4);
this.from = from;
this.type = 4;
this.modProof = modProof;
this.prmProof = prmProof;
}
Expand All @@ -136,11 +145,14 @@ export class KeygenBroadcastForRound4 extends AbstractKeygenBroadcast {
}
}

export class KeygenBroadcastForRound5 extends AbstractKeygenBroadcast {
export class KeygenBroadcastForRound5 {
public readonly from: PartyId;
public readonly type: 5;
public readonly SchnorrResponse: ZkSchResponse;

constructor(from: PartyId, SchnorrResponse: ZkSchResponse) {
super(from, 5);
this.from = from;
this.type = 5;
this.SchnorrResponse = SchnorrResponse;
}

Expand Down
3 changes: 3 additions & 0 deletions src/mpc/keygen/keygenMessages/directMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { KeygenDirectMessageForRound4JSON } from "../types";
export class KeygenDirectMessageForRound4 {
public readonly from: PartyId;
public readonly to: PartyId;
public readonly type: 4;
public readonly share: bigint;
public readonly facProof: ZkFacProof;

private constructor(from: PartyId, to: PartyId, share: bigint, facProof: ZkFacProof) {
this.from = from;
this.to = to;
this.type = 4;
this.share = share;
this.facProof = facProof;
}
Expand All @@ -37,6 +39,7 @@ export class KeygenDirectMessageForRound4 {
to: this.to,
shareHex: this.share.toString(16),
facProof: this.facProof.toJSON(),
type: 4,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/mpc/keygen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,5 @@ export type KeygenDirectMessageForRound4JSON = {
to: string;
shareHex: string;
facProof: ZkFacProofJSON;
type: 4;
};
16 changes: 1 addition & 15 deletions src/mpc/signing/signMessages/abstractDirectMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@ import { PartyId } from "../../keygen/partyKey";
import { SignMessageForRound2JSON, SignMessageForRound3JSON, SignMessageForRound4JSON } from "../types";
import { SignMessageForRound2, SignMessageForRound3, SignMessageForRound4 } from "./directMessages";

export abstract class AbstractSignDirectMessage {
public readonly from: PartyId;
public readonly type: number;
public readonly to: PartyId;

constructor(from: PartyId, to: PartyId, type: number) {
this.from = from;
this.type = type;
this.to = to;
}

public toJSON(): SignMessageForRound2JSON | SignMessageForRound3JSON | SignMessageForRound4JSON {
throw new Error("toJSON method must be implemented in derived classes");
}

export class AbstractSignDirectMessage {
public static fromJSON(
json: SignMessageForRound2JSON | SignMessageForRound3JSON | SignMessageForRound4JSON
): AbstractSignDirectMessage {
Expand Down
18 changes: 1 addition & 17 deletions src/mpc/signing/signMessages/abstractSignBroadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,7 @@ import {
SignBroadcastForRound5,
} from "./broadcasts";

export abstract class AbstractSignBroadcast {
public readonly from: PartyId;
public readonly type: number;

constructor(from: PartyId, type: number) {
this.from = from;
this.type = type;
}

public toJSON():
| SignBroadcastForRound2JSON
| SignBroadcastForRound3JSON
| SignBroadcastForRound4JSON
| SignBroadcastForRound5JSON {
throw new Error("toJSON method must be implemented in derived classes");
}

export class AbstractSignBroadcast {
public static fromJSON(
json:
| SignBroadcastForRound2JSON
Expand Down
21 changes: 12 additions & 9 deletions src/mpc/signing/signMessages/broadcasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
} from "../types";
import { AbstractSignBroadcast } from "./abstractSignBroadcast";

export class SignBroadcastForRound2 extends AbstractSignBroadcast {
export class SignBroadcastForRound2 {
public readonly from: PartyId;
public readonly K: bigint; // Paillier ciphertext
public readonly G: bigint; // Paillier ciphertext
public readonly t;
public readonly type: 2;

private constructor(from: PartyId, K: bigint, G: bigint) {
super(from, 2);
this.from = from;
this.type = 2;
this.K = K;
this.G = G;
}
Expand Down Expand Up @@ -45,13 +45,14 @@ export class SignBroadcastForRound2 extends AbstractSignBroadcast {
};
}
}
export class SignBroadcastForRound3 extends AbstractSignBroadcast {
export class SignBroadcastForRound3 {
public readonly type: 3;
public readonly from: PartyId;
public readonly BigGammaShare: AffinePoint;

public constructor(from: PartyId, BigGammaShare: AffinePoint) {
super(from, 2);
this.from = from;
this.type = 3;
this.BigGammaShare = BigGammaShare;
}

Expand Down Expand Up @@ -83,14 +84,15 @@ export class SignBroadcastForRound3 extends AbstractSignBroadcast {
}
}

export class SignBroadcastForRound4 extends AbstractSignBroadcast {
export class SignBroadcastForRound4 {
public readonly type: 4;
public readonly from: PartyId;
public readonly DeltaShare: bigint;
public readonly BigDeltaShare: AffinePoint;

private constructor(from: PartyId, DeltaShare: bigint, BigDeltaShare: AffinePoint) {
super(from, 4);
this.from = from;
this.type = 4;
this.DeltaShare = DeltaShare;
this.BigDeltaShare = BigDeltaShare;
}
Expand Down Expand Up @@ -130,13 +132,14 @@ export class SignBroadcastForRound4 extends AbstractSignBroadcast {
}
}

export class SignBroadcastForRound5 extends AbstractSignBroadcast {
export class SignBroadcastForRound5 {
public readonly type: 5;
public readonly from: PartyId;
public readonly SigmaShare: bigint;

private constructor(from: PartyId, SigmaShare: bigint) {
super(from, 2);
this.from = from;
this.type = 5;
this.SigmaShare = SigmaShare;
}

Expand Down
18 changes: 9 additions & 9 deletions src/mpc/signing/signMessages/directMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { ZkLogstarProof } from "../../zk/logstar";
import { SignMessageForRound2JSON, SignMessageForRound3JSON, SignMessageForRound4JSON } from "../types";
import { AbstractSignDirectMessage } from "./abstractDirectMessage";

export class SignMessageForRound4 extends AbstractSignDirectMessage {
export class SignMessageForRound4 {
public readonly from: PartyId;
public readonly to: PartyId;
public readonly ProofLog: ZkLogstarProof;
public readonly type;
public readonly type: 4;

public constructor(from: PartyId, to: PartyId, ProofLog: ZkLogstarProof) {
super(from, to, 4);
this.from = from;
this.to = to;
this.type = 4;
this.ProofLog = ProofLog;
}

Expand Down Expand Up @@ -48,7 +48,7 @@ export class SignMessageForRound4 extends AbstractSignDirectMessage {
}
}

export class SignMessageForRound3 extends AbstractSignDirectMessage {
export class SignMessageForRound3 {
public readonly from: PartyId;
public readonly to: PartyId;
public readonly DeltaD: bigint; // Ciphertext
Expand All @@ -58,7 +58,7 @@ export class SignMessageForRound3 extends AbstractSignDirectMessage {
public readonly ChiF: bigint; // Ciphertext
public readonly ChiProof: ZkAffgProof;
public readonly ProofLog: ZkLogstarProof;
public readonly type;
public readonly type: 3;

private constructor(
from: PartyId,
Expand All @@ -71,9 +71,9 @@ export class SignMessageForRound3 extends AbstractSignDirectMessage {
ChiProof: ZkAffgProof,
ProofLog: ZkLogstarProof
) {
super(from, to, 3);
this.from = from;
this.to = to;
this.type = 3;
this.DeltaD = DeltaD;
this.DeltaF = DeltaF;
this.DeltaProof = DeltaProof;
Expand Down Expand Up @@ -149,16 +149,16 @@ export class SignMessageForRound3 extends AbstractSignDirectMessage {
}
}

export class SignMessageForRound2 extends AbstractSignDirectMessage {
export class SignMessageForRound2 {
public readonly from: PartyId;
public readonly to: PartyId;
public readonly proofEnc: ZkEncProof;
public readonly type;
public readonly type: 2;

private constructor(from: PartyId, to: PartyId, proofEnc: ZkEncProof) {
super(from, to, 2);
this.from = from;
this.to = to;
this.type = 2;
this.proofEnc = proofEnc;
}

Expand Down
28 changes: 16 additions & 12 deletions src/p2p/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ class P2pServer extends AppLogger {

this.updateReplica(Number(this.NODE_ID), "CONNECT");
new ValidatorsGroup(this.validator.toString());
new KeygenSessionManager(this.validator, []);
// new SigningSessionManager(this.validator, [], "");
// this.signSessionProcessor = new SigningSessionManager();
// this.keygenSessionProcessor = new KeygenSessionManager();
// this.keygenSessionProcessor = new KeygenSessionManager(this.validator);
// this.signSessionProcessor = new SigningSessionManager(this.validator);

this.initState();
}
Expand Down Expand Up @@ -306,17 +308,18 @@ class P2pServer extends AppLogger {
//handle keygen & pBFT consensus for broadcasts
// console.log(message);
if (!this.signSessionProcessor && message.type === MESSAGE_TYPE.signSessionInit) {
this.signSessionProcessor = new SigningSessionManager(
this.validator,
this.validators,
"hello"
);
this.signSessionProcessor.init(this.threshold, this.validators);
// this.keygenSessionProcessor = new KeygenSessionManager(this.validator);
this.signSessionProcessor = new SigningSessionManager();
// this.keygenSessionProcessor = new KeygenSessionManager();
this.signSessionProcessor.init(this.threshold, this.validator, this.validators);
await delay(1000);
}
if (!this.keygenSessionProcessor && message.type === MESSAGE_TYPE.keygenInit) {
// this.keygenSessionProcessor = new KeygenSessionManager(this.validator, this.validators);
this.keygenSessionProcessor = new KeygenSessionManager();
this.keygenSessionProcessor.init(this.threshold, this.validator, this.validators);
await delay(1000);
}
// if (!this.keygenSessionProcessor && message.type === MESSAGE_TYPE.keygenInit) {
// this.keygenSessionProcessor = new KeygenSessionManager(this.validator, this.validators);
// this.keygenSessionProcessor.init(this.threshold, this.validators);
// }
await this.signSessionProcessor?.handleSignSessionConsensusMessage(message);
await this.keygenSessionProcessor?.handleKeygenConsensusMessage(message);
await this.chain.handleBlockchainConsensusMessage(message);
Expand All @@ -330,6 +333,7 @@ class P2pServer extends AppLogger {
//handle keygen & pBFT consensus for direcct msgs
await this.signSessionProcessor?.handleSignSessionConsensusMessage(message);
// await KeygenSessionManager.handleKeygenConsensusMessage(message);
await this.keygenSessionProcessor?.handleKeygenConsensusMessage(message);
await this.chain.handleBlockchainConsensusMessage(message);
await callback();
} catch (error) {
Expand Down
Loading