1
1
use super :: PublicKey ;
2
2
use crate :: error:: Error ;
3
+ use crate :: knox:: short_group_sig_core:: short_group_traits:: ProofOfSignatureKnowledge ;
3
4
use crate :: CredxResult ;
4
5
use blsful:: inner_types:: { group:: prime:: PrimeCurveAffine , * } ;
5
6
use core:: convert:: TryFrom ;
@@ -17,80 +18,11 @@ pub struct PokSignatureProof {
17
18
pub ( crate ) proof : Vec < Scalar > ,
18
19
}
19
20
20
- impl PokSignatureProof {
21
- /// Store the proof as a sequence of bytes
22
- /// Each point is compressed to big-endian format
23
- /// Needs (N + 2) * 32 + 48 * 2 + 96 space otherwise it will panic
24
- /// where N is the number of hidden messages
25
- pub fn to_bytes ( & self ) -> Vec < u8 > {
26
- let mut buffer = Vec :: new ( ) ;
27
- buffer. extend_from_slice ( & self . sigma_1 . to_affine ( ) . to_compressed ( ) ) ;
28
- buffer. extend_from_slice ( & self . sigma_2 . to_affine ( ) . to_compressed ( ) ) ;
29
- buffer. extend_from_slice ( & self . commitment . to_affine ( ) . to_compressed ( ) ) ;
30
-
31
- for m in & self . proof {
32
- buffer. extend_from_slice ( m. to_be_bytes ( ) . as_ref ( ) ) ;
33
- }
34
- buffer
35
- }
36
-
37
- /// Convert a byte sequence into the blind signature context
38
- /// Expected size is (N + 2) * 32 + 48 * 2 bytes
39
- pub fn from_bytes < B : AsRef < [ u8 ] > > ( bytes : B ) -> Option < Self > {
40
- const SIZE : usize = 32 * 3 + 48 * 4 ;
41
- let buffer = bytes. as_ref ( ) ;
42
- if buffer. len ( ) < SIZE {
43
- return None ;
44
- }
45
- if buffer. len ( ) % 32 != 0 {
46
- return None ;
47
- }
48
-
49
- let hid_msg_cnt = ( buffer. len ( ) - 48 * 4 ) / 32 ;
50
- let mut offset = 48 ;
51
- let mut end = 96 ;
52
- let sigma_1 = G1Affine :: from_compressed ( & <[ u8 ; 48 ] >:: try_from ( & buffer[ ..offset] ) . unwrap ( ) )
53
- . map ( G1Projective :: from) ;
54
- let sigma_2 =
55
- G1Affine :: from_compressed ( & <[ u8 ; 48 ] >:: try_from ( & buffer[ offset..end] ) . unwrap ( ) )
56
- . map ( G1Projective :: from) ;
57
- offset = end;
58
- end += 96 ;
59
- let commitment =
60
- G2Affine :: from_compressed ( & <[ u8 ; 96 ] >:: try_from ( & buffer[ offset..end] ) . unwrap ( ) )
61
- . map ( G2Projective :: from) ;
62
-
63
- if sigma_1. is_none ( ) . unwrap_u8 ( ) == 1
64
- || sigma_2. is_none ( ) . unwrap_u8 ( ) == 1
65
- || commitment. is_none ( ) . unwrap_u8 ( ) == 1
66
- {
67
- return None ;
68
- }
69
-
70
- offset = end;
71
- end += 32 ;
72
-
73
- let mut proof = Vec :: new ( ) ;
74
- for _ in 0 ..hid_msg_cnt {
75
- let c = Scalar :: from_be_bytes ( & <[ u8 ; 32 ] >:: try_from ( & buffer[ offset..end] ) . unwrap ( ) ) ;
76
- offset = end;
77
- end = offset + 32 ;
78
- if c. is_none ( ) . unwrap_u8 ( ) == 1 {
79
- return None ;
80
- }
81
-
82
- proof. push ( c. unwrap ( ) ) ;
83
- }
84
- Some ( Self {
85
- sigma_1 : sigma_1. unwrap ( ) ,
86
- sigma_2 : sigma_2. unwrap ( ) ,
87
- commitment : commitment. unwrap ( ) ,
88
- proof,
89
- } )
90
- }
21
+ impl ProofOfSignatureKnowledge for PokSignatureProof {
22
+ type PublicKey = PublicKey ;
91
23
92
24
/// Convert the committed values to bytes for the fiat-shamir challenge
93
- pub fn add_challenge_contribution (
25
+ fn add_proof_contribution (
94
26
& self ,
95
27
public_key : & PublicKey ,
96
28
rvl_msgs : & [ ( usize , Scalar ) ] ,
@@ -140,7 +72,7 @@ impl PokSignatureProof {
140
72
/// Validate the proof, only checks the signature proof
141
73
/// the selective disclosure proof is checked by verifying
142
74
/// self.challenge == computed_challenge
143
- pub fn verify ( & self , rvl_msgs : & [ ( usize , Scalar ) ] , public_key : & PublicKey ) -> bool {
75
+ fn verify ( & self , rvl_msgs : & [ ( usize , Scalar ) ] , public_key : & PublicKey ) -> CredxResult < ( ) > {
144
76
// check the signature proof
145
77
if self
146
78
. sigma_1
@@ -149,22 +81,24 @@ impl PokSignatureProof {
149
81
. unwrap_u8 ( )
150
82
== 1
151
83
{
152
- return false ;
84
+ return Err ( Error :: General ( "Invalid proof - identity" ) ) ;
153
85
}
154
86
155
87
if public_key. y . len ( ) < rvl_msgs. len ( ) {
156
- return false ;
88
+ return Err ( Error :: General (
89
+ "Invalid key - revealed messages length is bigger than the public key" ,
90
+ ) ) ;
157
91
}
158
92
if public_key. is_invalid ( ) . unwrap_u8 ( ) == 1u8 {
159
- return false ;
93
+ return Err ( Error :: General ( "Invalid public key" ) ) ;
160
94
}
161
95
162
96
let mut points = Vec :: new ( ) ;
163
97
let mut scalars = Vec :: new ( ) ;
164
98
165
99
for ( idx, msg) in rvl_msgs {
166
100
if * idx > public_key. y . len ( ) {
167
- return false ;
101
+ return Err ( Error :: General ( "Invalid proof - revealed message index" ) ) ;
168
102
}
169
103
points. push ( public_key. y [ * idx] ) ;
170
104
scalars. push ( * msg) ;
@@ -176,7 +110,7 @@ impl PokSignatureProof {
176
110
177
111
let j = G2Projective :: sum_of_products ( points. as_ref ( ) , scalars. as_ref ( ) ) ;
178
112
179
- multi_miller_loop ( & [
113
+ let res = multi_miller_loop ( & [
180
114
( & self . sigma_1 . to_affine ( ) , & G2Prepared :: from ( j. to_affine ( ) ) ) ,
181
115
(
182
116
& self . sigma_2 . to_affine ( ) ,
@@ -186,11 +120,16 @@ impl PokSignatureProof {
186
120
. final_exponentiation ( )
187
121
. is_identity ( )
188
122
. unwrap_u8 ( )
189
- == 1
123
+ == 1 ;
124
+ if res {
125
+ Ok ( ( ) )
126
+ } else {
127
+ Err ( Error :: General ( "Invalid proof - signature proof" ) )
128
+ }
190
129
}
191
130
192
131
/// Return the Schnorr proofs for all hidden messages
193
- pub fn get_hidden_message_proofs (
132
+ fn get_hidden_message_proofs (
194
133
& self ,
195
134
public_key : & PublicKey ,
196
135
rvl_msgs : & [ ( usize , Scalar ) ] ,
@@ -218,3 +157,76 @@ impl PokSignatureProof {
218
157
Ok ( hidden)
219
158
}
220
159
}
160
+
161
+ impl PokSignatureProof {
162
+ /// Store the proof as a sequence of bytes
163
+ /// Each point is compressed to big-endian format
164
+ /// Needs (N + 2) * 32 + 48 * 2 + 96 space otherwise it will panic
165
+ /// where N is the number of hidden messages
166
+ pub fn to_bytes ( & self ) -> Vec < u8 > {
167
+ let mut buffer = Vec :: new ( ) ;
168
+ buffer. extend_from_slice ( & self . sigma_1 . to_affine ( ) . to_compressed ( ) ) ;
169
+ buffer. extend_from_slice ( & self . sigma_2 . to_affine ( ) . to_compressed ( ) ) ;
170
+ buffer. extend_from_slice ( & self . commitment . to_affine ( ) . to_compressed ( ) ) ;
171
+
172
+ for m in & self . proof {
173
+ buffer. extend_from_slice ( m. to_be_bytes ( ) . as_ref ( ) ) ;
174
+ }
175
+ buffer
176
+ }
177
+
178
+ /// Convert a byte sequence into the blind signature context
179
+ /// Expected size is (N + 2) * 32 + 48 * 2 bytes
180
+ pub fn from_bytes < B : AsRef < [ u8 ] > > ( bytes : B ) -> Option < Self > {
181
+ const SIZE : usize = 32 * 3 + 48 * 4 ;
182
+ let buffer = bytes. as_ref ( ) ;
183
+ if buffer. len ( ) < SIZE {
184
+ return None ;
185
+ }
186
+ if buffer. len ( ) % 32 != 0 {
187
+ return None ;
188
+ }
189
+
190
+ let hid_msg_cnt = ( buffer. len ( ) - 48 * 4 ) / 32 ;
191
+ let mut offset = 48 ;
192
+ let mut end = 96 ;
193
+ let sigma_1 = G1Affine :: from_compressed ( & <[ u8 ; 48 ] >:: try_from ( & buffer[ ..offset] ) . unwrap ( ) )
194
+ . map ( G1Projective :: from) ;
195
+ let sigma_2 =
196
+ G1Affine :: from_compressed ( & <[ u8 ; 48 ] >:: try_from ( & buffer[ offset..end] ) . unwrap ( ) )
197
+ . map ( G1Projective :: from) ;
198
+ offset = end;
199
+ end += 96 ;
200
+ let commitment =
201
+ G2Affine :: from_compressed ( & <[ u8 ; 96 ] >:: try_from ( & buffer[ offset..end] ) . unwrap ( ) )
202
+ . map ( G2Projective :: from) ;
203
+
204
+ if sigma_1. is_none ( ) . unwrap_u8 ( ) == 1
205
+ || sigma_2. is_none ( ) . unwrap_u8 ( ) == 1
206
+ || commitment. is_none ( ) . unwrap_u8 ( ) == 1
207
+ {
208
+ return None ;
209
+ }
210
+
211
+ offset = end;
212
+ end += 32 ;
213
+
214
+ let mut proof = Vec :: new ( ) ;
215
+ for _ in 0 ..hid_msg_cnt {
216
+ let c = Scalar :: from_be_bytes ( & <[ u8 ; 32 ] >:: try_from ( & buffer[ offset..end] ) . unwrap ( ) ) ;
217
+ offset = end;
218
+ end = offset + 32 ;
219
+ if c. is_none ( ) . unwrap_u8 ( ) == 1 {
220
+ return None ;
221
+ }
222
+
223
+ proof. push ( c. unwrap ( ) ) ;
224
+ }
225
+ Some ( Self {
226
+ sigma_1 : sigma_1. unwrap ( ) ,
227
+ sigma_2 : sigma_2. unwrap ( ) ,
228
+ commitment : commitment. unwrap ( ) ,
229
+ proof,
230
+ } )
231
+ }
232
+ }
0 commit comments