@@ -13,6 +13,7 @@ mod utils;
1313
1414use std:: { io:: Read , ops:: Mul } ;
1515
16+ use anyhow:: anyhow;
1617use challenges:: { calculate_first_challenge_hash, calculate_second_challenge_hash} ;
1718use polynomial:: { calculate_polynomial_val, generate_polynomial, Polynomial } ;
1819use rand_core:: CryptoRngCore ;
@@ -35,34 +36,44 @@ pub struct UnitVectorProof(
3536
3637impl UnitVectorProof {
3738 /// Decode `UnitVectorProof` from bytes.
38- pub fn from_bytes ( mut bytes : & [ u8 ] , size : usize ) -> Option < Self > {
39+ ///
40+ /// # Errors
41+ /// - Cannot decode announcement value.
42+ /// - Cannot decode ciphertext value.
43+ /// - Cannot decode response randomness value.
44+ /// - Cannot decode scalar value.
45+ pub fn from_bytes ( mut bytes : & [ u8 ] , size : usize ) -> anyhow:: Result < Self > {
3946 let mut ann_buf = [ 0u8 ; Announcement :: BYTES_SIZE ] ;
4047 let mut dl_buf = [ 0u8 ; Ciphertext :: BYTES_SIZE ] ;
4148 let mut rr_buf = [ 0u8 ; ResponseRandomness :: BYTES_SIZE ] ;
4249
4350 let ann = ( 0 ..size)
44- . map ( |_ | {
45- bytes. read_exact ( & mut ann_buf) . ok ( ) ?;
51+ . map ( |i | {
52+ bytes. read_exact ( & mut ann_buf) ?;
4653 Announcement :: from_bytes ( & ann_buf)
54+ . map_err ( |e| anyhow ! ( "Cannot decode announcement at {i}, error: {e}." ) )
4755 } )
48- . collect :: < Option < _ > > ( ) ?;
56+ . collect :: < anyhow :: Result < _ > > ( ) ?;
4957 let dl = ( 0 ..size)
50- . map ( |_ | {
51- bytes. read_exact ( & mut dl_buf) . ok ( ) ?;
58+ . map ( |i | {
59+ bytes. read_exact ( & mut dl_buf) ?;
5260 Ciphertext :: from_bytes ( & dl_buf)
61+ . map_err ( |e| anyhow ! ( "Cannot decode ciphertext at {i}, error: {e}." ) )
5362 } )
54- . collect :: < Option < _ > > ( ) ?;
63+ . collect :: < anyhow :: Result < _ > > ( ) ?;
5564 let rr = ( 0 ..size)
56- . map ( |_ | {
57- bytes. read_exact ( & mut rr_buf) . ok ( ) ?;
65+ . map ( |i | {
66+ bytes. read_exact ( & mut rr_buf) ?;
5867 ResponseRandomness :: from_bytes ( & rr_buf)
68+ . map_err ( |e| anyhow ! ( "Cannot decode response randomness at {i}, error: {e}." ) )
5969 } )
60- . collect :: < Option < _ > > ( ) ?;
70+ . collect :: < anyhow :: Result < _ > > ( ) ?;
6171
6272 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) )
73+ bytes. read_exact ( & mut scalar_buf) ?;
74+ let scalar =
75+ Scalar :: from_bytes ( scalar_buf) . map_err ( |_| anyhow ! ( "Cannot decode scalar field." ) ) ?;
76+ Ok ( Self ( ann, dl, rr, scalar) )
6677 }
6778
6879 /// Get a deserialized bytes size
0 commit comments