1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use hex;
use error::ErrorKind;
use result::Result;
use traits::Validate;
use traits::{BinarySerialize, HexSerialize};
use scalar::Scalar;
use point::Point;
use std::io::Write;
use std::fmt;
#[derive(Copy, Clone, Eq, PartialEq, Default, Debug, Serialize, Deserialize)]
pub struct ZKPWitness(pub Point);
impl ZKPWitness {
pub fn new(instance: Scalar) -> ZKPWitness {
ZKPWitness(&Point::default() * &instance)
}
pub fn from_point(point: Point) -> Result<ZKPWitness> {
point.validate()?;
Ok(ZKPWitness(point))
}
pub fn to_point(&self) -> Point {
self.0
}
}
impl Validate for ZKPWitness {
fn validate(&self) -> Result<()> {
self.0.validate()
}
}
impl BinarySerialize for ZKPWitness {
fn to_bytes(&self) -> Result<Vec<u8>> {
self.0.to_bytes()
}
fn from_bytes(b: &[u8]) -> Result<ZKPWitness> {
Ok(ZKPWitness(Point::from_bytes(b)?))
}
}
impl HexSerialize for ZKPWitness {
fn to_hex(&self) -> Result<String> {
self.0.to_hex()
}
fn from_hex(s: &str) -> Result<ZKPWitness> {
Ok(ZKPWitness(Point::from_hex(s)?))
}
}
impl fmt::Display for ZKPWitness {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.to_hex().unwrap())
}
}
#[derive(Copy, Clone, Eq, PartialEq, Default, Debug, Serialize, Deserialize)]
pub struct ZKPProof {
pub public_coin: Point,
pub challenge: Scalar,
pub response: Scalar,
}
impl ZKPProof {
pub fn new(instance: Scalar, message: &[u8]) -> Result<ZKPProof> {
let g = Point::default();
let witness = &g * &instance;
let public_coin_scalar = Scalar::from_hash(message);
let public_coin = &g * &public_coin_scalar;
let mut buf = Vec::new();
buf.write_all(&g.to_bytes()?)?;
buf.write_all(&witness.to_bytes()?)?;
buf.write_all(&public_coin.to_bytes()?)?;
let challenge = Scalar::from_hash(&buf);
let response = &public_coin_scalar - &(&challenge*&instance);
Ok(ZKPProof {
public_coin: public_coin,
challenge: challenge,
response: response,
})
}
pub fn verify(&self, witness: ZKPWitness) -> Result<bool> {
witness.validate()?;
let g = Point::default();
let gr = &g * &self.response;
let wc = &witness.to_point() * &self.challenge;
Ok(self.public_coin == &gr + &wc)
}
}
impl Validate for ZKPProof {
fn validate(&self) -> Result<()> {
self.public_coin.validate()?;
self.challenge.validate()?;
self.response.validate()?;
Ok(())
}
}
impl BinarySerialize for ZKPProof {
fn to_bytes(&self) -> Result<Vec<u8>> {
let mut buf = Vec::new();
buf.write_all(&self.public_coin.to_bytes()?)?;
buf.write_all(&self.challenge.to_bytes()?)?;
buf.write_all(&self.response.to_bytes()?)?;
Ok(buf)
}
fn from_bytes(b: &[u8]) -> Result<ZKPProof> {
if b.len() != 96 {
return Err(ErrorKind::InvalidLength.into());
}
let public_coin = Point::from_bytes(&b[0..32])?;
let challenge = Scalar::from_bytes(&b[32..64])?;
let response = Scalar::from_bytes(&b[64..])?;
Ok(ZKPProof {
public_coin: public_coin,
challenge: challenge,
response: response,
})
}
}
impl HexSerialize for ZKPProof {
fn to_hex(&self) -> Result<String> {
Ok(hex::encode(&self.to_bytes()?))
}
fn from_hex(s: &str) -> Result<ZKPProof> {
Self::from_bytes(&hex::decode(s)?)
}
}