@@ -13,11 +13,12 @@ use core::ops::Add;
13
13
14
14
use digest:: { BlockInput , Digest } ;
15
15
use generic_array:: sequence:: Concat ;
16
- use generic_array:: typenum:: { Sum , Unsigned } ;
16
+ use generic_array:: typenum:: Sum ;
17
17
use generic_array:: { ArrayLength , GenericArray } ;
18
18
19
19
use crate :: errors:: InternalError ;
20
20
use crate :: group:: Group ;
21
+ use crate :: util:: deserialize;
21
22
use crate :: voprf:: {
22
23
BlindedElement , EvaluationElement , NonVerifiableClient , NonVerifiableServer , Proof ,
23
24
VerifiableClient , VerifiableServer ,
@@ -36,12 +37,9 @@ impl<G: Group, H: BlockInput + Digest> NonVerifiableClient<G, H> {
36
37
37
38
/// Deserialization from bytes
38
39
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
39
- let scalar_len = G :: ScalarLen :: USIZE ;
40
- if input. len ( ) != scalar_len {
41
- return Err ( InternalError :: SizeError ) ;
42
- }
40
+ let mut input = input. iter ( ) . copied ( ) ;
43
41
44
- let blind = G :: from_scalar_slice ( & input[ ..scalar_len ] ) ?;
42
+ let blind = G :: from_scalar_slice ( & deserialize ( & mut input) ? ) ?;
45
43
46
44
Ok ( Self {
47
45
blind,
@@ -62,14 +60,10 @@ impl<G: Group, H: BlockInput + Digest> VerifiableClient<G, H> {
62
60
63
61
/// Deserialization from bytes
64
62
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
65
- let scalar_len = G :: ScalarLen :: USIZE ;
66
- let elem_len = G :: ElemLen :: USIZE ;
67
- if input. len ( ) != scalar_len + elem_len {
68
- return Err ( InternalError :: SizeError ) ;
69
- }
63
+ let mut input = input. iter ( ) . copied ( ) ;
70
64
71
- let blind = G :: from_scalar_slice ( & input[ ..scalar_len ] ) ?;
72
- let blinded_element = G :: from_element_slice ( & input [ scalar_len..scalar_len + elem_len ] ) ?;
65
+ let blind = G :: from_scalar_slice ( & deserialize ( & mut input) ? ) ?;
66
+ let blinded_element = G :: from_element_slice ( & deserialize ( & mut input ) ? ) ?;
73
67
74
68
Ok ( Self {
75
69
blind,
@@ -87,12 +81,9 @@ impl<G: Group, H: BlockInput + Digest> NonVerifiableServer<G, H> {
87
81
88
82
/// Deserialization from bytes
89
83
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
90
- let scalar_len = G :: ScalarLen :: USIZE ;
91
- if input. len ( ) != scalar_len {
92
- return Err ( InternalError :: SizeError ) ;
93
- }
84
+ let mut input = input. iter ( ) . copied ( ) ;
94
85
95
- let sk = G :: from_scalar_slice ( input) ?;
86
+ let sk = G :: from_scalar_slice ( & deserialize ( & mut input) ? ) ?;
96
87
97
88
Ok ( Self {
98
89
sk,
@@ -113,14 +104,10 @@ impl<G: Group, H: BlockInput + Digest> VerifiableServer<G, H> {
113
104
114
105
/// Deserialization from bytes
115
106
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
116
- let scalar_len = G :: ScalarLen :: USIZE ;
117
- let elem_len = G :: ElemLen :: USIZE ;
118
- if input. len ( ) != scalar_len + elem_len {
119
- return Err ( InternalError :: SizeError ) ;
120
- }
107
+ let mut input = input. iter ( ) . copied ( ) ;
121
108
122
- let sk = G :: from_scalar_slice ( & input[ ..scalar_len ] ) ?;
123
- let pk = G :: from_element_slice ( & input[ scalar_len.. ] ) ?;
109
+ let sk = G :: from_scalar_slice ( & deserialize ( & mut input) ? ) ?;
110
+ let pk = G :: from_element_slice ( & deserialize ( & mut input) ? ) ?;
124
111
125
112
Ok ( Self {
126
113
sk,
@@ -142,13 +129,14 @@ impl<G: Group, H: BlockInput + Digest> Proof<G, H> {
142
129
143
130
/// Deserialization from bytes
144
131
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
145
- let scalar_len = G :: ScalarLen :: USIZE ;
146
- if input. len ( ) != scalar_len + scalar_len {
147
- return Err ( InternalError :: SizeError ) ;
148
- }
132
+ let mut input = input. iter ( ) . copied ( ) ;
133
+
134
+ let c_scalar = G :: from_scalar_slice ( & deserialize ( & mut input) ?) ?;
135
+ let s_scalar = G :: from_scalar_slice ( & deserialize ( & mut input) ?) ?;
136
+
149
137
Ok ( Proof {
150
- c_scalar : G :: from_scalar_slice ( & input [ ..scalar_len ] ) ? ,
151
- s_scalar : G :: from_scalar_slice ( & input [ scalar_len.. ] ) ? ,
138
+ c_scalar,
139
+ s_scalar,
152
140
hash : PhantomData ,
153
141
} )
154
142
}
@@ -162,12 +150,12 @@ impl<G: Group, H: BlockInput + Digest> BlindedElement<G, H> {
162
150
163
151
/// Deserialization from bytes
164
152
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
165
- let elem_len = G :: ElemLen :: USIZE ;
166
- if input . len ( ) != elem_len {
167
- return Err ( InternalError :: SizeError ) ;
168
- }
153
+ let mut input = input . iter ( ) . copied ( ) ;
154
+
155
+ let value = G :: from_element_slice ( & deserialize ( & mut input ) ? ) ? ;
156
+
169
157
Ok ( Self {
170
- value : G :: from_element_slice ( input ) ? ,
158
+ value,
171
159
hash : PhantomData ,
172
160
} )
173
161
}
@@ -181,12 +169,12 @@ impl<G: Group, H: BlockInput + Digest> EvaluationElement<G, H> {
181
169
182
170
/// Deserialization from bytes
183
171
pub fn deserialize ( input : & [ u8 ] ) -> Result < Self , InternalError > {
184
- let elem_len = G :: ElemLen :: USIZE ;
185
- if input . len ( ) != elem_len {
186
- return Err ( InternalError :: SizeError ) ;
187
- }
172
+ let mut input = input . iter ( ) . copied ( ) ;
173
+
174
+ let value = G :: from_element_slice ( & deserialize ( & mut input ) ? ) ? ;
175
+
188
176
Ok ( Self {
189
- value : G :: from_element_slice ( input ) ? ,
177
+ value,
190
178
hash : PhantomData ,
191
179
} )
192
180
}
0 commit comments