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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::{
commitment::HomomorphicCommitment,
ristretto::{constants::RISTRETTO_NUMS_POINTS, RistrettoPublicKey},
};
use curve25519_dalek::{constants::RISTRETTO_BASEPOINT_POINT, ristretto::RistrettoPoint, traits::MultiscalarMul};
use crate::{commitment::HomomorphicCommitmentFactory, ristretto::RistrettoSecretKey};
use curve25519_dalek::scalar::Scalar;
use std::{borrow::Borrow, iter::Sum};
pub const RISTRETTO_PEDERSEN_G: RistrettoPoint = RISTRETTO_BASEPOINT_POINT;
lazy_static! {
pub static ref RISTRETTO_PEDERSEN_H: RistrettoPoint = RISTRETTO_NUMS_POINTS[0];
}
pub type PedersenCommitment = HomomorphicCommitment<RistrettoPublicKey>;
#[derive(Debug, PartialEq, Eq, Clone)]
#[allow(non_snake_case)]
pub struct PedersenCommitmentFactory {
pub(crate) G: RistrettoPoint,
pub(crate) H: RistrettoPoint,
}
impl PedersenCommitmentFactory {
#[allow(non_snake_case)]
pub fn new(G: RistrettoPoint, H: RistrettoPoint) -> PedersenCommitmentFactory {
PedersenCommitmentFactory { G, H }
}
}
impl Default for PedersenCommitmentFactory {
fn default() -> Self {
PedersenCommitmentFactory::new(RISTRETTO_PEDERSEN_G, *RISTRETTO_PEDERSEN_H)
}
}
impl HomomorphicCommitmentFactory for PedersenCommitmentFactory {
type P = RistrettoPublicKey;
fn commit(&self, k: &RistrettoSecretKey, v: &RistrettoSecretKey) -> PedersenCommitment {
let c = RistrettoPoint::multiscalar_mul(&[v.0, k.0], &[self.H, self.G]);
HomomorphicCommitment(RistrettoPublicKey::new_from_pk(c))
}
fn zero(&self) -> PedersenCommitment {
let zero = Scalar::zero();
let c = RistrettoPoint::multiscalar_mul(&[zero, zero], &[self.H, self.G]);
HomomorphicCommitment(RistrettoPublicKey::new_from_pk(c))
}
fn open(&self, k: &RistrettoSecretKey, v: &RistrettoSecretKey, commitment: &PedersenCommitment) -> bool {
let c_test = self.commit(k, v);
commitment.0 == c_test.0
}
fn commit_value(&self, k: &RistrettoSecretKey, value: u64) -> PedersenCommitment {
let v = RistrettoSecretKey::from(value);
self.commit(k, &v)
}
fn open_value(&self, k: &RistrettoSecretKey, v: u64, commitment: &HomomorphicCommitment<Self::P>) -> bool {
let kv = RistrettoSecretKey::from(v);
self.open(k, &kv, commitment)
}
}
impl<T> Sum<T> for PedersenCommitment
where T: Borrow<PedersenCommitment>
{
fn sum<I>(iter: I) -> Self
where I: Iterator<Item = T> {
let mut total = RistrettoPoint::default();
for c in iter {
let commitment = c.borrow();
total += (commitment.0).point
}
let sum = RistrettoPublicKey::new_from_pk(total);
HomomorphicCommitment(sum)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::keys::{PublicKey, SecretKey};
use rand;
use std::convert::From;
use tari_utilities::{message_format::MessageFormat, ByteArray};
#[test]
fn check_default_base() {
let base = PedersenCommitmentFactory::default();
assert_eq!(base.G, RISTRETTO_PEDERSEN_G);
assert_eq!(base.H, *RISTRETTO_PEDERSEN_H)
}
#[test]
fn pubkey_roundtrip() {
let mut rng = rand::thread_rng();
let (_, p) = RistrettoPublicKey::random_keypair(&mut rng);
let c = PedersenCommitment::from_public_key(&p);
assert_eq!(c.as_public_key(), &p);
let c2 = PedersenCommitment::from_bytes(c.as_bytes()).unwrap();
assert_eq!(c, c2);
}
#[test]
fn commitment_sub() {
let mut rng = rand::thread_rng();
let (_, a) = RistrettoPublicKey::random_keypair(&mut rng);
let (_, b) = RistrettoPublicKey::random_keypair(&mut rng);
let c = &a + &b;
let a = PedersenCommitment::from_public_key(&a);
let b = PedersenCommitment::from_public_key(&b);
let c = PedersenCommitment::from_public_key(&c);
assert_eq!(b, &c - &a);
}
#[test]
fn check_g_ne_h() {
assert_ne!(RISTRETTO_PEDERSEN_G, *RISTRETTO_PEDERSEN_H);
}
#[test]
#[allow(non_snake_case)]
fn check_open() {
let factory = PedersenCommitmentFactory::default();
let H = RISTRETTO_PEDERSEN_H.clone();
let mut rng = rand::thread_rng();
for _ in 0..100 {
let v = RistrettoSecretKey::random(&mut rng);
let k = RistrettoSecretKey::random(&mut rng);
let c = factory.commit(&k, &v);
let c_calc: RistrettoPoint = v.0 * H + k.0 * RISTRETTO_PEDERSEN_G;
assert_eq!(RistrettoPoint::from(c.as_public_key()), c_calc);
assert!(factory.open(&k, &v, &c));
assert!(!factory.open(&k, &(&v + &v), &c));
assert!(!factory.open(&(&k + &v), &v, &c));
}
}
#[test]
fn check_homomorphism() {
let mut rng = rand::thread_rng();
for _ in 0..100 {
let v1 = RistrettoSecretKey::random(&mut rng);
let v2 = RistrettoSecretKey::random(&mut rng);
let v_sum = &v1 + &v2;
let k1 = RistrettoSecretKey::random(&mut rng);
let k2 = RistrettoSecretKey::random(&mut rng);
let k_sum = &k1 + &k2;
let factory = PedersenCommitmentFactory::default();
let c1 = factory.commit(&k1, &v1);
let c2 = factory.commit(&k2, &v2);
let c_sum = &c1 + &c2;
let c_sum2 = factory.commit(&k_sum, &v_sum);
assert!(factory.open(&k1, &v1, &c1));
assert!(factory.open(&k2, &v2, &c2));
assert_eq!(c_sum, c_sum2);
assert!(factory.open(&k_sum, &v_sum, &c_sum));
}
}
#[test]
fn sum_commitment_vector() {
let mut rng = rand::thread_rng();
let mut v_sum = RistrettoSecretKey::default();
let mut k_sum = RistrettoSecretKey::default();
let zero = RistrettoSecretKey::default();
let commitment_factory = PedersenCommitmentFactory::default();
let mut c_sum = commitment_factory.commit(&zero, &zero);
let mut commitments = Vec::with_capacity(100);
for _ in 0..100 {
let v = RistrettoSecretKey::random(&mut rng);
v_sum = &v_sum + &v;
let k = RistrettoSecretKey::random(&mut rng);
k_sum = &k_sum + &k;
let c = commitment_factory.commit(&k, &v);
c_sum = &c_sum + &c;
commitments.push(c);
}
assert!(commitment_factory.open(&k_sum, &v_sum, &c_sum));
assert_eq!(c_sum, commitments.iter().sum());
}
#[test]
fn serialize_deserialize() {
let mut rng = rand::thread_rng();
let factory = PedersenCommitmentFactory::default();
let k = RistrettoSecretKey::random(&mut rng);
let c = factory.commit_value(&k, 420);
let ser_c = c.to_base64().unwrap();
let c2 = PedersenCommitment::from_base64(&ser_c).unwrap();
assert!(factory.open_value(&k, 420, &c2));
let ser_c = c.to_binary().unwrap();
let c2 = PedersenCommitment::from_binary(&ser_c).unwrap();
assert!(factory.open_value(&k, 420, &c2));
}
}