signer.rs (2307B)
1 use secp256kfun::{ 2 digest::typenum::U32, 3 marker::{Public, Zero}, 4 Scalar, 5 }; 6 7 use schnorr_fun::{ 8 frost::{Frost, XOnlyFrostKey}, 9 musig::{Nonce, NonceKeyPair}, 10 nonce::NonceGen, 11 Message, 12 }; 13 use sha2::Digest; 14 15 pub struct RoastSigner<'a, H, NG> { 16 frost: Frost<H, NG>, 17 frost_key: XOnlyFrostKey, 18 my_index: usize, 19 secret_share: Scalar, 20 message: Message<'a, Public>, 21 my_nonces: Vec<NonceKeyPair>, 22 } 23 24 impl<'a, H: Digest + Clone + Digest<OutputSize = U32>, NG: NonceGen> RoastSigner<'a, H, NG> { 25 pub fn new( 26 frost: Frost<H, NG>, 27 frost_key: XOnlyFrostKey, 28 my_index: usize, 29 secret_share: Scalar, 30 initial_nonce_sid: &[u8], 31 message: Message<'a>, 32 ) -> (RoastSigner<'a, H, NG>, Nonce) { 33 let initial_nonce = frost.gen_nonce( 34 &secret_share, 35 &initial_nonce_sid, 36 Some(frost_key.public_key().normalize()), 37 Some(message), 38 ); 39 let my_nonces = vec![initial_nonce.clone()]; 40 41 ( 42 RoastSigner { 43 frost, 44 frost_key, 45 my_index, 46 secret_share, 47 message, 48 my_nonces, 49 }, 50 initial_nonce.public(), 51 ) 52 } 53 54 pub fn new_nonce(&self, sid: &[u8]) -> NonceKeyPair { 55 let nonce = self.frost.gen_nonce( 56 &self.secret_share, 57 sid, 58 Some(self.frost_key.public_key().normalize()), 59 Some(self.message), 60 ); 61 nonce 62 } 63 64 pub fn sign(&mut self, nonce_set: Vec<(usize, Nonce)>) -> (Scalar<Public, Zero>, Nonce) { 65 let session = self.frost.start_sign_session( 66 &self.frost_key, 67 nonce_set, 68 Message::plain("test", b"test"), 69 ); 70 let my_nonce = self 71 .my_nonces 72 .pop() 73 .expect("some nonce available to use for signing"); 74 let sig = self.frost.sign( 75 &self.frost_key, 76 &session, 77 self.my_index, 78 &self.secret_share, 79 my_nonce, 80 ); 81 // call server with (sig, self.new_nonce()) 82 self.my_nonces.push(self.new_nonce(&[0])); 83 84 (sig, self.my_nonces.last().expect("some nonce").public()) 85 } 86 }