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
use crate::keychain;
use crate::keychain::BlindingFactor;
use crate::util::secp::key::SecretKey;
use crate::util::secp::pedersen::Commitment;
use crate::util::{secp, secp_static, static_secp_instance};
use failure::Fail;
#[derive(Debug, Clone, PartialEq, Eq, Fail, Serialize, Deserialize)]
pub enum Error {
#[fail(display = "Keychain error {}", _0)]
Keychain(keychain::Error),
#[fail(display = "Secp error {}", _0)]
Secp(secp::Error),
#[fail(display = "Kernel sum mismatch")]
KernelSumMismatch,
#[fail(display = "Invalid value")]
InvalidValue,
}
impl From<secp::Error> for Error {
fn from(e: secp::Error) -> Error {
Error::Secp(e)
}
}
impl From<keychain::Error> for Error {
fn from(e: keychain::Error) -> Error {
Error::Keychain(e)
}
}
pub trait Committed {
fn sum_kernel_excesses(
&self,
offset: &BlindingFactor,
) -> Result<(Commitment, Commitment), Error> {
let kernel_commits = self.kernels_committed();
let kernel_sum = sum_commits(kernel_commits, vec![])?;
let kernel_sum_plus_offset = {
let secp = static_secp_instance();
let secp = secp.lock();
let mut commits = vec![kernel_sum];
if *offset != BlindingFactor::zero() {
let key = offset.secret_key(&secp)?;
let offset_commit = secp.commit(0, key)?;
commits.push(offset_commit);
}
secp.commit_sum(commits, vec![])?
};
Ok((kernel_sum, kernel_sum_plus_offset))
}
fn sum_commitments(&self, overage: i64) -> Result<Commitment, Error> {
let mut input_commits = self.inputs_committed();
let mut output_commits = self.outputs_committed();
if overage != 0 {
let over_commit = {
let secp = static_secp_instance();
let secp = secp.lock();
let overage_abs = overage.checked_abs().ok_or_else(|| Error::InvalidValue)? as u64;
secp.commit_value(overage_abs).unwrap()
};
if overage < 0 {
input_commits.push(over_commit);
} else {
output_commits.push(over_commit);
}
}
sum_commits(output_commits, input_commits)
}
fn inputs_committed(&self) -> Vec<Commitment>;
fn outputs_committed(&self) -> Vec<Commitment>;
fn kernels_committed(&self) -> Vec<Commitment>;
fn verify_kernel_sums(
&self,
overage: i64,
kernel_offset: BlindingFactor,
) -> Result<((Commitment, Commitment)), Error> {
let utxo_sum = self.sum_commitments(overage)?;
let (kernel_sum, kernel_sum_plus_offset) = self.sum_kernel_excesses(&kernel_offset)?;
if utxo_sum != kernel_sum_plus_offset {
return Err(Error::KernelSumMismatch);
}
Ok((utxo_sum, kernel_sum))
}
}
pub fn sum_commits(
mut positive: Vec<Commitment>,
mut negative: Vec<Commitment>,
) -> Result<Commitment, Error> {
let zero_commit = secp_static::commit_to_zero_value();
positive.retain(|x| *x != zero_commit);
negative.retain(|x| *x != zero_commit);
let secp = static_secp_instance();
let secp = secp.lock();
Ok(secp.commit_sum(positive, negative)?)
}
pub fn sum_kernel_offsets(
positive: Vec<BlindingFactor>,
negative: Vec<BlindingFactor>,
) -> Result<BlindingFactor, Error> {
let secp = static_secp_instance();
let secp = secp.lock();
let positive = to_secrets(positive, &secp);
let negative = to_secrets(negative, &secp);
if positive.is_empty() {
Ok(BlindingFactor::zero())
} else {
let sum = secp.blind_sum(positive, negative)?;
Ok(BlindingFactor::from_secret_key(sum))
}
}
fn to_secrets(bf: Vec<BlindingFactor>, secp: &secp::Secp256k1) -> Vec<SecretKey> {
bf.into_iter()
.filter(|x| *x != BlindingFactor::zero())
.filter_map(|x| x.secret_key(&secp).ok())
.collect::<Vec<_>>()
}