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
use std::collections::BTreeSet;
use bitcoin::hashes::{sha256, Hmac};
use bitcoin::secp256k1;
use client_side_validation::commit_verify::EmbedCommitVerify;
use miniscript::Segwitv0;
use super::{Container, Error, Proof, ScriptEncodeData};
use crate::lnpbp1;
#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[display(Debug)]
pub struct KeysetContainer {
pub pubkey: secp256k1::PublicKey,
pub keyset: BTreeSet<secp256k1::PublicKey>,
pub tag: sha256::Hash,
pub tweaking_factor: Option<Hmac<sha256::Hash>>,
}
impl Container for KeysetContainer {
type Supplement = sha256::Hash;
type Host = Option<()>;
fn reconstruct(
proof: &Proof,
supplement: &Self::Supplement,
_: &Self::Host,
) -> Result<Self, Error> {
if let ScriptEncodeData::LockScript(ref script) = proof.source {
Ok(Self {
pubkey: proof.pubkey,
keyset: script
.extract_pubkeyset::<Segwitv0>()?
.into_iter()
.map(|pk| pk.key)
.collect(),
tag: supplement.clone(),
tweaking_factor: None,
})
} else {
Err(Error::InvalidProofStructure)
}
}
#[inline]
fn deconstruct(self) -> (Proof, Self::Supplement) {
(Proof::from(self.pubkey), self.tag)
}
#[inline]
fn to_proof(&self) -> Proof {
panic!("KeysetContainer does not support proof generation")
}
#[inline]
fn into_proof(self) -> Proof {
panic!("KeysetContainer does not support proof generation")
}
}
#[derive(Wrapper, Clone, Copy, PartialEq, Eq, Hash, Debug, Display, From)]
#[display("{0}", alt = "{_0:#}*")]
#[wrapper(FromStr, LowerHex)]
pub struct KeysetCommitment(secp256k1::PublicKey);
impl<MSG> EmbedCommitVerify<MSG> for KeysetCommitment
where
MSG: AsRef<[u8]>,
{
type Container = KeysetContainer;
type Error = lnpbp1::Error;
fn embed_commit(
keyset_container: &mut Self::Container,
msg: &MSG,
) -> Result<Self, Self::Error> {
let mut keyset = keyset_container.keyset.clone();
let mut pubkey = keyset_container.pubkey.clone();
keyset.insert(pubkey);
let tweaking_factor = lnpbp1::commit(
&mut keyset,
&mut pubkey,
&keyset_container.tag,
msg,
)?;
keyset_container.tweaking_factor = Some(tweaking_factor);
Ok(KeysetCommitment(pubkey))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::dbc::pubkey::*;
use crate::test::gen_secp_pubkeys;
use amplify::Wrapper;
use bitcoin::hashes::{hex::ToHex, sha256, Hash};
use bitcoin::secp256k1;
use client_side_validation::commit_verify::test_helpers::{
embed_commit_verify_suite, gen_messages,
};
use std::iter::FromIterator;
use std::str::FromStr;
#[test]
fn test_lnpbp1_vs_lnpbp2() {
let tag = sha256::Hash::hash(b"TEST_TAG2");
let msg = "test message";
gen_secp_pubkeys(9).into_iter().for_each(|pubkey| {
let lnpbp1_commitment = PubkeyCommitment::embed_commit(
&mut PubkeyContainer {
pubkey,
tag,
tweaking_factor: None,
},
&msg,
)
.unwrap();
let lnpbp2_commitment = KeysetCommitment::embed_commit(
&mut KeysetContainer {
pubkey,
keyset: BTreeSet::new(),
tag,
tweaking_factor: None,
},
&msg,
)
.unwrap();
assert_eq!(
lnpbp1_commitment.into_inner(),
lnpbp2_commitment.into_inner()
);
});
}
#[test]
fn test_keyset_commitment() {
let tag = sha256::Hash::hash(b"TEST_TAG2");
let pubkey = secp256k1::PublicKey::from_str(
"0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166",
)
.unwrap();
(1..9).into_iter().for_each(|n_keys| {
embed_commit_verify_suite::<Vec<u8>, KeysetCommitment>(
gen_messages(),
&mut KeysetContainer {
pubkey,
keyset: BTreeSet::from_iter(gen_secp_pubkeys(n_keys)),
tag,
tweaking_factor: None,
},
);
});
}
#[test]
fn test_keyset_tweaking_results() {
let tag = sha256::Hash::hash(b"TEST_TAG2");
let msg = "test message";
let pubkey = secp256k1::PublicKey::from_str(
"0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166",
)
.unwrap();
let keyset = BTreeSet::from_iter(vec![secp256k1::PublicKey::from_str(
"03cfb81a7609a4d40914dfd41860f501209c30468d91834c8af1af34ce73f4f3fd",
)
.unwrap()]);
let commitment = KeysetCommitment::embed_commit(
&mut KeysetContainer {
pubkey,
keyset,
tag,
tweaking_factor: None,
},
&msg,
)
.unwrap();
assert_eq!(
commitment.as_inner().to_hex(),
"02e47bb42c041f158ecfcf1099018f08650ef569a9a51bbb317e8787cdf3e06890"
);
}
}