Skip to content

Commit f260d8c

Browse files
Full session
1 parent 8986b77 commit f260d8c

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/libMPC/SCL_atomic_swaps.mjs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export class SCL_Atomic_Initiator{
194194

195195

196196
this.t=int_from_bytes(this.signer.curve.Get_Random_privateKey());
197+
197198
let G= this.signer.curve.GetBase();
198199
this.tG=G.multiply(this.t);
199200

@@ -241,7 +242,7 @@ export class SCL_Atomic_Initiator{
241242

242243
console.log("final check:", check1, check2);
243244
this.ResetSession();
244-
return [SIG_ABTX1, SIG_ABTX2];
245+
return [SIG_ABTX1, SIG_ABTX2];//this message is broadcast onchain to unlock initiator exit liquidity
245246

246247
}
247248

@@ -259,7 +260,12 @@ export class SCL_Atomic_Responder{
259260

260261
this.sk=sk;
261262
this.pubkey=this.signer.IndividualPubKey_array(sk);
263+
this.ResetSession();
264+
}
265+
262266

267+
ResetSession(){
268+
263269
this.state="idle";
264270

265271
this.pubKeyDist=0;//the initiator distant public key
@@ -273,19 +279,13 @@ export class SCL_Atomic_Responder{
273279
this.aggnonce2=0;
274280
this.tG=0;
275281

282+
this.SIG_ABTX1p=0;
283+
this.SIG_ABTX2p=0;
284+
this.psigI2p=0;
285+
this.psigR2=0;
286+
276287
this.tx1=0;
277288
this.tx2=0;
278-
}
279-
280-
281-
ResetSession(){
282-
this.state="idle";
283-
284-
this.nonceA1=0;
285-
this.nonceA2=0;
286-
287-
this.nonceB1=0;
288-
this.nonceB2=0;
289289

290290
}
291291

@@ -310,8 +310,6 @@ export class SCL_Atomic_Responder{
310310
this.aggnonce2 = this.signer.Nonce_agg([this.nonceA2.toString('hex'), this.nonceB2[1].toString('hex')]);
311311

312312

313-
console.log("aggnonce1 for R:", this.aggnonce1);
314-
315313
let Message_R1=[this.aggnonce1, this.aggnonce2, this.nonceB1[1], this.nonceB2[1]];
316314

317315
this.state="waitI2";
@@ -339,15 +337,21 @@ export class SCL_Atomic_Responder{
339337
if(checkpoint2==false){
340338
return false;
341339
}
340+
this.psigI2p=psigI2p;
342341

343342
//Compute partial signatures
344-
let psigI1=this.signer.Psign(this.nonceB1[0], this.sk, session_ctx1);
345-
let psigI2=this.signer.Psign(this.nonceB2[0], this.sk, session_ctx2);
346-
347-
console.log("Partial verify:", this.signer.Psig_verify(psigI1, this.nonceB1[1], this.pubkey, session_ctx1));
348-
console.log("Partial verify:", this.signer.Psig_verify(psigI2, this.nonceB2[1], this.pubkey, session_ctx2));
343+
let psigR1=this.signer.Psign(this.nonceB1[0], this.sk, session_ctx1);
344+
let psigR2=this.signer.Psign(this.nonceB2[0], this.sk, session_ctx2);
345+
this.psigR2=psigR2;
346+
347+
console.log("Partial verify:", this.signer.Psig_verify(psigR1, this.nonceB1[1], this.pubkey, session_ctx1));
348+
console.log("Partial verify:", this.signer.Psig_verify(psigR2, this.nonceB2[1], this.pubkey, session_ctx2));
349349

350-
Message_R2=[psigI1, psigI2];
350+
this.SIG_ABTX1p=this.signer.Partial_sig_agg([int_to_bytes(psigI1p), psigR1], session_ctx1);
351+
this.SIG_ABTX2p=this.signer.Partial_sig_agg([int_to_bytes(psigI2p), psigR2], session_ctx2);
352+
353+
354+
Message_R2=[psigR1, psigR2];
351355
this.state="waitI3";
352356
return Message_R2;//this message is broadcast onchain to unlock responder exit liquidity
353357

@@ -356,11 +360,26 @@ export class SCL_Atomic_Responder{
356360
//looking at Alice's unlocking, Bob can recompute the original signature
357361
FinalUnlock(UnlockSigAlice){
358362
let Message_R3=[];
363+
let SIG_ABTX1=UnlockSigAlice[0];
364+
UnlockSigAlice[0];
365+
366+
let Recomputed_t=((this.signer.order + int_from_bytes(this.SIG_ABTX1p)) - int_from_bytes(SIG_ABTX1)) %this.signer.order;
367+
368+
const session_ctx2=[this.aggnonce2, [ this.pubKeyDist, this.pubkey], [], [], this.tx2];//session_ctx=[aggnonce, pubkeys, [], [], msg];
369+
let psigI2=((this.signer.order+(this.psigI2p)-(Recomputed_t)))%this.signer.order;
370+
psigI2=int_to_bytes(psigI2);
371+
Message_R3=this.signer.Partial_sig_agg([psigI2, this.psigR2], session_ctx2);
372+
let x_aggpk=this.signer.curve.ForceXonly(this.aggpk);//x-only version for noncegen, allways 32
373+
374+
let check=this.signer.Schnorr_verify(this.tx2, x_aggpk, Message_R3);
375+
376+
console.log("final check:", check);
359377

360378
this.state="idle";
361379
return Message_R3;//this message is broadcast onchain to unlock responder exit liquidity
362380

363381
}
364382

383+
365384
}
366385

src/libMPC/test_atomic_swap.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import { SCL_Atomic_Initiator, SCL_Atomic_Responder } from './SCL_atomic_swaps.m
1919
//example of full session with automata
2020
//note that worst case is assumed (Bob read tweak from Alice's signature)
2121
function test_full_atomic_session_automatas(curve){
22+
console.log("/*************************** ");
23+
console.log("Full Atomic Swap session using curve:", curve);
24+
2225
let signer=new SCL_Musig2(curve);
2326

2427
console.log("signer:", signer);
@@ -49,9 +52,8 @@ function test_full_atomic_session_automatas(curve){
4952
console.log("Initiator Signature Aggregation and Unlock");
5053
let UnlockSigAlice=Initiator.FinalUnlock(Message_R2);//final signature to Unlock chain1 token by Initiator
5154

52-
return 1;
5355
console.log("Responder Signature Aggregation and Unlock");
54-
let UnlockSigBob=Initiator.FinalUnlock(UnlockSigAlice);//final signature to Unlock chain2 token by Responder
56+
let UnlockSigBob=Responder.FinalUnlock(UnlockSigAlice);//final signature to Unlock chain2 token by Responder
5557

5658
//todo: result is ok if UnlockSigBob is equal to classic multisig
5759

@@ -60,5 +62,6 @@ function test_full_atomic_session_automatas(curve){
6062

6163
(async () => {
6264
test_full_atomic_session_automatas('secp256k1');
65+
test_full_atomic_session_automatas('ed25519');
6366

6467
})();

0 commit comments

Comments
 (0)