1+ //! Low-level operations on BLS votes. 
2+ //!  
3+ //!  
4+ //!  
5+ //!  
6+ 
17use  blst:: min_sig:: * ; 
28use  blst:: * ; 
39use  rand:: RngCore ; 
410
511use  crate :: bls_util:: * ; 
612
13+ /// An empty bytestring. 
714const  EMPTY :  [ u8 ;  0 ]  = [ ] ; 
815
16+ /// The domain separator for Leios. 
917const  DST :  & [ u8 ;  5 ]  = b"Leios" ; 
1018
19+ /// Generate a secret BLS scalar. 
1120pub  fn  gen_key ( )  -> SecretKey  { 
1221    let  mut  rng:  rand:: prelude:: ThreadRng  = rand:: thread_rng ( ) ; 
1322    let  mut  ikm:  [ u8 ;  32 ]  = [ 0u8 ;  32 ] ; 
@@ -16,12 +25,14 @@ pub fn gen_key() -> SecretKey {
1625    SecretKey :: key_gen ( & ikm,  info) . unwrap ( ) 
1726} 
1827
28+ /// Create a proof of possession from a secret key `sk`, namely $\mu_1$ and $\mu_2$ of [Figure 8 (voting)](index.html). 
1929pub  fn  make_pop ( sk :  & SecretKey )  -> ( Signature ,  Signature )  { 
2030    let  m1:  [ u8 ;  192 ]  = sk. sk_to_pk ( ) . serialize ( ) ; 
2131    let  m2 = EMPTY ; 
2232    ( sk. sign ( & m1,  DST ,  b"PoP" ) ,  sk. sign ( & m2,  DST ,  & EMPTY ) ) 
2333} 
2434
35+ /// Verify the the proof of possession, namely $\mu_1$ and $\mu_2$ of [Figure 8 (voting)](index.html), for a public key `pk`. 
2536pub  fn  check_pop ( pk :  & PublicKey ,  mu1 :  & Signature ,  mu2 :  & Signature )  -> bool  { 
2637    let  m1:  [ u8 ;  192 ]  = pk. serialize ( ) ; 
2738    let  m2 = EMPTY ; 
@@ -30,29 +41,35 @@ pub fn check_pop(pk: &PublicKey, mu1: &Signature, mu2: &Signature) -> bool {
3041    result1 == BLST_ERROR :: BLST_SUCCESS  && result2 == BLST_ERROR :: BLST_SUCCESS 
3142} 
3243
44+ /// Sign the message `m` in the election `eid` using the secret key `sk`. 
3345pub  fn  gen_sig ( sk :  & SecretKey ,  eid :  & [ u8 ] ,  m :  & [ u8 ] )  -> Signature  { 
3446    sk. sign ( m,  DST ,  eid) 
3547} 
3648
49+ /// Verify a signature `vs` on the message `m` in the election `eid` for the public key `pk`. 
3750pub  fn  verify_sig ( pk :  & PublicKey ,  eid :  & [ u8 ] ,  m :  & [ u8 ] ,  vs :  & Signature )  -> bool  { 
3851    let  result_m = vs. verify ( true ,  m,  DST ,  eid,  pk,  false ) ; 
3952    result_m == BLST_ERROR :: BLST_SUCCESS 
4053} 
4154
55+ /// Sign the election `eid` with the secret key `sk`. 
4256pub  fn  gen_sigma_eid ( sk :  & SecretKey ,  eid :  & [ u8 ] )  -> Signature  { 
4357    sk. sign ( & EMPTY ,  DST ,  eid) 
4458} 
4559
60+ /// Create a vote for the message `m` in the election `eid` using the secret key `sk`. 
4661pub  fn  gen_vote ( sk :  & SecretKey ,  eid :  & [ u8 ] ,  m :  & [ u8 ] )  -> ( Signature ,  Signature )  { 
4762    ( sk. sign ( & EMPTY ,  DST ,  eid) ,  sk. sign ( m,  DST ,  eid) ) 
4863} 
4964
65+ /// Verify the vote `vs` for the message `m` in the election `eid` for the public key `pk`. 
5066pub  fn  verify_vote ( pk :  & PublicKey ,  eid :  & [ u8 ] ,  m :  & [ u8 ] ,  vs :  & ( Signature ,  Signature ) )  -> bool  { 
5167    let  result_eid = vs. 0 . verify ( true ,  & EMPTY ,  DST ,  eid,  pk,  true ) ; 
5268    let  result_m = vs. 1 . verify ( true ,  m,  DST ,  eid,  pk,  false ) ; 
5369    result_eid == BLST_ERROR :: BLST_SUCCESS  && result_m == BLST_ERROR :: BLST_SUCCESS 
5470} 
5571
72+ /// Hash an array of signatures `sigma_eids` and `sigma_ms`. 
5673fn  hash_sigs ( sigma_eids :  & [ & Signature ] ,  sigma_ms :  & [ & Signature ] )  -> [ u8 ;  32 ]  { 
5774    let  mut  sigmas:  Vec < & Signature >  = Vec :: new ( ) ; 
5875    sigmas. extend ( sigma_eids) ; 
@@ -65,6 +82,7 @@ fn hash_sigs(sigma_eids: &[&Signature], sigma_ms: &[&Signature]) -> [u8; 32] {
6582    } 
6683} 
6784
85+ /// Hash an integer `i` with a previous hash `h`. 
6886fn  hash_index ( i :  i32 ,  h :  & [ u8 ;  32 ] )  -> [ u8 ;  32 ]  { 
6987    let  mut  msg:  [ u8 ;  36 ]  = [ 0 ;  36 ] ; 
7088    let  ii:  [ u8 ;  4 ]  = i. to_ne_bytes ( ) ; 
@@ -80,12 +98,14 @@ fn hash_index(i: i32, h: &[u8; 32]) -> [u8; 32] {
8098    } 
8199} 
82100
101+ /// Create the signatures for a certificate from the individual vote signatures `vss`. 
83102pub  fn  gen_cert ( vss :  & [ & ( Signature ,  Signature ) ] )  -> Result < ( Signature ,  Signature ) ,  BLST_ERROR >  { 
84103    let  sigma_eids:  Vec < & Signature >  = vss. iter ( ) . map ( |vs| & vs. 1 ) . collect ( ) ; 
85104    let  sigma_ms:  Vec < & Signature >  = vss. iter ( ) . map ( |vs| & vs. 1 ) . collect ( ) ; 
86105    gen_cert_fa ( & sigma_eids,  & sigma_ms) 
87106} 
88107
108+ /// Verify the contents of certificate signatures `cs` for the message `m` in election `eid`, given the vote signatures `vss`. 
89109pub  fn  verify_cert ( 
90110    pks :  & [ & PublicKey ] , 
91111    eid :  & [ u8 ] , 
@@ -129,6 +149,7 @@ pub fn verify_cert(
129149    } 
130150} 
131151
152+ /// Create certificate signatures including both sortition and message signing signatures. 
132153pub  fn  gen_cert_fa ( 
133154    sigma_eids :  & [ & Signature ] , 
134155    sigma_ms :  & [ & Signature ] , 
@@ -161,6 +182,7 @@ pub fn gen_cert_fa(
161182    } 
162183} 
163184
185+ /// Create cerificate signatures including only message signing signatures. 
164186pub  fn  gen_cert_fa_pure ( sigma_ms :  & [ & Signature ] )  -> Result < Signature ,  BLST_ERROR >  { 
165187    let  result_m = AggregateSignature :: aggregate ( sigma_ms,  true ) ; 
166188    match  result_m { 
@@ -169,6 +191,7 @@ pub fn gen_cert_fa_pure(sigma_ms: &[&Signature]) -> Result<Signature, BLST_ERROR
169191    } 
170192} 
171193
194+ /// Verify cerificate sigantures according to [Figure 8 (voting)](index.html). 
172195pub  fn  verify_cert_fa ( 
173196    pks :  & [ & PublicKey ] , 
174197    pks_nonpersistent :  & [ & PublicKey ] , 
@@ -212,6 +235,7 @@ pub fn verify_cert_fa(
212235    } 
213236} 
214237
238+ /// Verify signatures for a message, without verifying sortition. 
215239pub  fn  verify_cert_fa_pure ( 
216240    pks :  & [ & PublicKey ] , 
217241    eid :  & [ u8 ] , 
0 commit comments