-
Notifications
You must be signed in to change notification settings - Fork 30
/
commitment.rs
94 lines (81 loc) · 2.15 KB
/
commitment.rs
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
use crate::gang::{GroupElement, Scalar};
use rand_core::{CryptoRng, RngCore};
use std::ops::{Add, Mul};
/// Pedersen commitment
#[derive(Clone, PartialEq, Eq)]
pub struct Commitment {
c: GroupElement,
}
#[derive(Clone)]
pub struct CommitmentKey {
pub h: GroupElement,
}
impl CommitmentKey {
pub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let h = GroupElement::random(rng);
CommitmentKey { h }
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Validity {
Valid,
Invalid,
}
#[derive(Clone)]
pub struct Open {
m: Scalar,
r: Scalar,
}
impl Commitment {
pub fn new_open(ck: &CommitmentKey, o: &Open) -> Self {
let c = GroupElement::generator() * &o.m + &ck.h * &o.r;
Commitment { c }
}
pub fn new(ck: &CommitmentKey, m: &Scalar, r: &Scalar) -> Self {
let c = GroupElement::generator() * m + &ck.h * r;
Commitment { c }
}
pub fn verify(&self, ck: &CommitmentKey, o: &Open) -> Validity {
let other = GroupElement::generator() * &o.m + &ck.h * &o.r;
if self.c == other {
Validity::Valid
} else {
Validity::Invalid
}
}
pub fn to_bytes(&self) -> Vec<u8> {
self.c.to_bytes().to_vec()
}
}
impl<'a, 'b> Add<&'b Commitment> for &'a Commitment {
type Output = Commitment;
fn add(self, rhs: &'b Commitment) -> Self::Output {
let c = &self.c + &rhs.c;
Commitment { c }
}
}
impl<'b> Add<&'b Commitment> for Commitment {
type Output = Commitment;
fn add(self, rhs: &'b Commitment) -> Self::Output {
let c = &self.c + &rhs.c;
Commitment { c }
}
}
impl<'a, 'b> Mul<&'b Scalar> for &'a Commitment {
type Output = Commitment;
fn mul(self, rhs: &'b Scalar) -> Self::Output {
Commitment { c: &self.c * rhs }
}
}
impl<'a> Mul<Scalar> for &'a Commitment {
type Output = Commitment;
fn mul(self, rhs: Scalar) -> Self::Output {
Commitment { c: &self.c * rhs }
}
}
impl Mul<Scalar> for Commitment {
type Output = Commitment;
fn mul(self, rhs: Scalar) -> Self::Output {
Commitment { c: &self.c * &rhs }
}
}