@@ -11,7 +11,7 @@ mod polynomial;
1111mod randomness_announcements;
1212mod utils;
1313
14- use std:: ops:: Mul ;
14+ use std:: { io :: Read , ops:: Mul } ;
1515
1616use challenges:: { calculate_first_challenge_hash, calculate_second_challenge_hash} ;
1717use polynomial:: { calculate_polynomial_val, generate_polynomial, Polynomial } ;
@@ -25,13 +25,72 @@ use crate::crypto::{
2525} ;
2626
2727/// Unit vector proof struct
28+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
2829pub struct UnitVectorProof (
2930 Vec < Announcement > ,
3031 Vec < Ciphertext > ,
3132 Vec < ResponseRandomness > ,
3233 Scalar ,
3334) ;
3435
36+ impl UnitVectorProof {
37+ /// Decode `UnitVectorProof` from bytes.
38+ pub fn from_bytes ( mut bytes : & [ u8 ] , size : usize ) -> Option < Self > {
39+ let mut ann_buf = [ 0u8 ; Announcement :: BYTES_SIZE ] ;
40+ let mut dl_buf = [ 0u8 ; Ciphertext :: BYTES_SIZE ] ;
41+ let mut rr_buf = [ 0u8 ; ResponseRandomness :: BYTES_SIZE ] ;
42+
43+ let ann = ( 0 ..size)
44+ . map ( |_| {
45+ bytes. read_exact ( & mut ann_buf) . ok ( ) ?;
46+ Announcement :: from_bytes ( & ann_buf)
47+ } )
48+ . collect :: < Option < _ > > ( ) ?;
49+ let dl = ( 0 ..size)
50+ . map ( |_| {
51+ bytes. read_exact ( & mut dl_buf) . ok ( ) ?;
52+ Ciphertext :: from_bytes ( & dl_buf)
53+ } )
54+ . collect :: < Option < _ > > ( ) ?;
55+ let rr = ( 0 ..size)
56+ . map ( |_| {
57+ bytes. read_exact ( & mut rr_buf) . ok ( ) ?;
58+ ResponseRandomness :: from_bytes ( & rr_buf)
59+ } )
60+ . collect :: < Option < _ > > ( ) ?;
61+
62+ let mut scalar_buf = [ 0u8 ; Scalar :: BYTES_SIZE ] ;
63+ bytes. read_exact ( & mut scalar_buf) . ok ( ) ?;
64+ let scalar = Scalar :: from_bytes ( scalar_buf) ?;
65+ Some ( Self ( ann, dl, rr, scalar) )
66+ }
67+
68+ /// Get a deserialized bytes size
69+ #[ must_use]
70+ pub fn bytes_size ( & self ) -> usize {
71+ self . 0 . len ( ) * Announcement :: BYTES_SIZE
72+ + self . 0 . len ( ) * Ciphertext :: BYTES_SIZE
73+ + self . 0 . len ( ) * ResponseRandomness :: BYTES_SIZE
74+ }
75+
76+ /// Encode `EncryptedVote` tos bytes.
77+ #[ must_use]
78+ pub fn to_bytes ( & self ) -> Vec < u8 > {
79+ let mut res = Vec :: with_capacity ( self . bytes_size ( ) ) ;
80+ self . 0
81+ . iter ( )
82+ . for_each ( |c| res. extend_from_slice ( & c. to_bytes ( ) ) ) ;
83+ self . 1
84+ . iter ( )
85+ . for_each ( |c| res. extend_from_slice ( & c. to_bytes ( ) ) ) ;
86+ self . 2
87+ . iter ( )
88+ . for_each ( |c| res. extend_from_slice ( & c. to_bytes ( ) ) ) ;
89+ res. extend_from_slice ( & self . 3 . to_bytes ( ) ) ;
90+ res
91+ }
92+ }
93+
3594/// Generates a unit vector proof.
3695///
3796/// `unit_vector` must be a collection of `Scalar` where only one element is equal to
@@ -232,12 +291,40 @@ fn check_2(
232291
233292#[ cfg( test) ]
234293mod tests {
235- use proptest:: sample:: size_range;
294+ use proptest:: {
295+ prelude:: { any_with, Arbitrary , BoxedStrategy , Strategy } ,
296+ sample:: size_range,
297+ } ;
236298 use rand_core:: OsRng ;
237299 use test_strategy:: proptest;
238300
239301 use super :: { super :: elgamal:: SecretKey , * } ;
240302
303+ impl Arbitrary for UnitVectorProof {
304+ type Parameters = usize ;
305+ type Strategy = BoxedStrategy < Self > ;
306+
307+ fn arbitrary_with ( size : Self :: Parameters ) -> Self :: Strategy {
308+ any_with :: < (
309+ Vec < ( ( Announcement , Ciphertext ) , ResponseRandomness ) > ,
310+ Scalar ,
311+ ) > ( ( ( size_range ( size) , ( ( ( ) , ( ) ) , ( ) ) ) , ( ) ) )
312+ . prop_map ( |( val, scalar) | {
313+ let ( vec, rr) : ( Vec < _ > , Vec < _ > ) = val. into_iter ( ) . unzip ( ) ;
314+ let ( an, ciph) = vec. into_iter ( ) . unzip ( ) ;
315+ Self ( an, ciph, rr, scalar)
316+ } )
317+ . boxed ( )
318+ }
319+ }
320+
321+ #[ proptest]
322+ fn proof_to_bytes_from_bytes_test ( p1 : UnitVectorProof ) {
323+ let bytes = p1. to_bytes ( ) ;
324+ let p2 = UnitVectorProof :: from_bytes ( & bytes, p1. 0 . len ( ) ) . unwrap ( ) ;
325+ assert_eq ! ( p1, p2) ;
326+ }
327+
241328 fn is_unit_vector ( vector : & [ Scalar ] ) -> bool {
242329 let ones = vector. iter ( ) . filter ( |s| s == & & Scalar :: one ( ) ) . count ( ) ;
243330 let zeros = vector. iter ( ) . filter ( |s| s == & & Scalar :: zero ( ) ) . count ( ) ;
0 commit comments