-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuniversal_tcp.rs
More file actions
460 lines (417 loc) · 16.1 KB
/
Copy pathuniversal_tcp.rs
File metadata and controls
460 lines (417 loc) · 16.1 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use std::marker::PhantomData;
use ark_ff::PrimeField;
use ark_pcd::PCDPredicate;
use bitcoin_r1cs::constraints::{
hash256::Hash256Gadget,
outpoint::OutPointVar,
tx::{TxVar, TxVarConfig},
};
use ark_relations::r1cs::{ConstraintSystemRef, SynthesisError};
use ark_r1cs_std::{ToBytesGadget, boolean::Boolean, uint32::UInt32};
use ark_r1cs_std::eq::EqGadget;
use crate::data_structures::{
messages::{MessageUniversalTCP, MessageUniversalTCPVar},
witnesses::LocalWitnessOutPoint,
};
/// The PCD predicate for a Universal Transaction Chain Proof.
///
/// The situation is the following:
///
/// **Parties:** Alice, Bob
///
/// **Common knowledge:** `genesis_txid`, `input_index`, `output_index`
///
/// **Goal:**
/// Alice holds a transaction `Tx` and she wants to prove that `Tx.outputs[output_index]` belongs to a transaction chain
/// with indices `(input_index, output_index)` starting at `genesis_txid`. Namely, there exist a chain `(Tx0, Tx1, Tx2, .., Txn)` such that:
/// 1. `Tx0.txid() = genesis_txid`
/// 2. `Tx(i+1).inputs[input_index] = (Txi.txid(), output_index)`, `0 <= i <= n-1`
/// 3. `Txn = Tx`
///
/// The circuit is the following:
/// - msg (a [MessageUniversalTCPVar])
/// - witness (a [LocalWitnessOutPoint])
/// - prior_msgs (a couple [MessageUniversalTCPVar])
///
///
/// ```"no_rust"
/// match base_case {
/// true => (msg.outpoint = (msg.genesis_txid, output_index))
/// false => {
/// (msg.outpoint = (witness.txid(), output_index))
/// AND (msg.genesis_txid = prior_msgs.genesis_txid)
/// AND (witness.inputs[input_index].prev_output = prior_msg.outpoint)
/// }
/// }
/// ```
///
/// **Note**: This predicate should only be used in a protocol in which Bob verifies that the public input contains the `genesis_txid`
/// he is interested in.
pub struct UniversalTransactionChainProofPredicate<P: TxVarConfig + Clone> {
/// The indices of the chain
pub input_index: u32,
pub output_index: u32,
/// The structure of the transactions in the chain
_config: PhantomData<P>,
}
impl<P: TxVarConfig + Clone> UniversalTransactionChainProofPredicate<P> {
pub fn new(input_index: u32, output_index: u32) -> Self {
Self {
input_index,
output_index,
_config: PhantomData,
}
}
}
impl<P: TxVarConfig + Clone> Clone for UniversalTransactionChainProofPredicate<P> {
fn clone(&self) -> Self {
Self {
input_index: self.input_index,
output_index: self.output_index,
_config: PhantomData,
}
}
}
impl<F: PrimeField, P: TxVarConfig + Clone> PCDPredicate<F>
for UniversalTransactionChainProofPredicate<P>
{
type Message = MessageUniversalTCP;
type MessageVar = MessageUniversalTCPVar<F>;
type LocalWitness = LocalWitnessOutPoint<P>;
type LocalWitnessVar = TxVar<F, P>;
const PRIOR_MSG_LEN: usize = 1;
fn generate_constraints(
&self,
_cs: ConstraintSystemRef<F>,
msg: &Self::MessageVar,
witness: &Self::LocalWitnessVar,
prior_msgs: &[Self::MessageVar],
base_case: &Boolean<F>,
) -> Result<(), SynthesisError> {
let txid_witness = Hash256Gadget::<F>::evaluate(&witness.to_bytes()?)?;
let base_case_expected_input = {
OutPointVar::<F> {
prev_tx: msg.genesis_txid.clone(),
prev_index: UInt32::<F>::constant(self.output_index),
}
};
let recursive_case_expected_input = {
OutPointVar::<F> {
prev_tx: txid_witness.clone(),
prev_index: UInt32::<F>::constant(self.output_index),
}
};
let is_base_case_verified = Boolean::<F>::kary_and(&[
msg.outpoint.is_eq(&base_case_expected_input)?,
base_case.clone(),
])?;
let is_recursive_case_verified = Boolean::<F>::kary_and(&[
msg.outpoint.is_eq(&recursive_case_expected_input)?,
prior_msgs[0]
.outpoint
.is_eq(&witness.inputs[self.input_index as usize].prev_output)?,
msg.genesis_txid.is_eq(&prior_msgs[0].genesis_txid)?,
])?;
Boolean::<F>::kary_or(&[is_base_case_verified, is_recursive_case_verified])?
.enforce_equal(&Boolean::<F>::TRUE)
}
}
#[cfg(test)]
mod tests {
use ark_pcd::PCDPredicate;
use ark_r1cs_std::{alloc::AllocVar, prelude::Boolean};
use ark_relations::r1cs::ConstraintSystem;
use bitcoin_r1cs::constraints::tx::{TxVar, TxVarConfig};
use ark_mnt4_298::Fq as ScalarFieldMNT6;
use chain_gang::{
messages::{OutPoint, Tx, TxIn},
util::Serializable,
};
use crate::data_structures::messages::{MessageUniversalTCP, MessageUniversalTCPVar};
use super::UniversalTransactionChainProofPredicate;
use std::io::Cursor;
#[derive(Clone)]
struct Config;
impl TxVarConfig for Config {
const N_INPUTS: usize = 2;
const N_OUTPUTS: usize = 2;
const LEN_UNLOCK_SCRIPTS: &[usize] = &[0x49, 0x49];
const LEN_LOCK_SCRIPTS: &[usize] = &[0x23, 0x23];
}
type TestTCP = UniversalTransactionChainProofPredicate<Config>;
// Test transactions, they form a primary chain (chain_index = 0)
fn transactions() -> [Tx; 3] {
[
Tx::read(
&mut Cursor::new(hex::decode("010000000279730d2a2a09636ba3263c7bf4558e8fac852d4fcd7819a5b1cac2f2639b686e00000000494830450221009eda6ae95d014b177f923e7880ca9b95dbe95f7a190eee2c4a223752e1c84fb102200c727fdcd19cbb1f1fad49607b28971925c029f894003cffaf4024f6e13817d8010000000079730d2a2a09636ba3263c7bf4558e8fac852d4fcd7819a5b1cac2f2639b686e00000000494830450221009eda6ae95d014b177f923e7880ca9b95dbe95f7a190eee2c4a223752e1c84fb102200c727fdcd19cbb1f1fad49607b28971925c029f894003cffaf4024f6e13817d801000000000200000000000000002321028fede8b103cfece5c45d721c3db8fb238394e4094c0bb219cce296fcf99f51f6ac00000000000000002321028fede8b103cfece5c45d721c3db8fb238394e4094c0bb219cce296fcf99f51f6ac00000000").unwrap())
).unwrap(),
Tx::read(
&mut Cursor::new(hex::decode("01000000029ac1fdd730cc4697c9236358ef966d67e282747a32ae3244ed8b74d439b3a02200000000494830450221009eda6ae95d014b177f923e7880ca9b95dbe95f7a190eee2c4a223752e1c84fb102200c727fdcd19cbb1f1fad49607b28971925c029f894003cffaf4024f6e13817d8010000000079730d2a2a09636ba3263c7bf4558e8fac852d4fcd7819a5b1cac2f2639b686e01000000494830450221009eda6ae95d014b177f923e7880ca9b95dbe95f7a190eee2c4a223752e1c84fb102200c727fdcd19cbb1f1fad49607b28971925c029f894003cffaf4024f6e13817d801000000000200000000000000002321028fede8b103cfece5c45d721c3db8fb238394e4094c0bb219cce296fcf99f51f6ac00000000000000002321028fede8b103cfece5c45d721c3db8fb238394e4094c0bb219cce296fcf99f51f6ac00000000").unwrap())
).unwrap(),
Tx::read(
&mut Cursor::new(hex::decode("0100000002068c6e5d11052fec4abd5f1fb2775b6da8908197f87cb98e638ea8126899780400000000494830450221009eda6ae95d014b177f923e7880ca9b95dbe95f7a190eee2c4a223752e1c84fb102200c727fdcd19cbb1f1fad49607b28971925c029f894003cffaf4024f6e13817d8010000000079730d2a2a09636ba3263c7bf4558e8fac852d4fcd7819a5b1cac2f2639b686e01000000494830450221009eda6ae95d014b177f923e7880ca9b95dbe95f7a190eee2c4a223752e1c84fb102200c727fdcd19cbb1f1fad49607b28971925c029f894003cffaf4024f6e13817d801000000000200000000000000002321028fede8b103cfece5c45d721c3db8fb238394e4094c0bb219cce296fcf99f51f6ac00000000000000002321028fede8b103cfece5c45d721c3db8fb238394e4094c0bb219cce296fcf99f51f6ac00000000").unwrap())
).unwrap()
]
}
fn test_predicate(
input_index: u32,
output_index: u32,
msg: <TestTCP as PCDPredicate<ScalarFieldMNT6>>::Message,
prior_msg: <TestTCP as PCDPredicate<ScalarFieldMNT6>>::Message,
witness: <TestTCP as PCDPredicate<ScalarFieldMNT6>>::LocalWitness,
base_case: bool,
expected_result: bool,
) -> () {
let tcp = TestTCP::new(input_index, output_index);
let cs = ConstraintSystem::<ScalarFieldMNT6>::new_ref();
let msg_var =
MessageUniversalTCPVar::<ScalarFieldMNT6>::new_input(cs.clone(), || Ok(msg)).unwrap();
let prior_msg_var =
MessageUniversalTCPVar::<ScalarFieldMNT6>::new_witness(cs.clone(), || Ok(prior_msg))
.unwrap();
let witness_var =
TxVar::<ScalarFieldMNT6, Config>::new_witness(cs.clone(), || Ok(witness)).unwrap();
let base_case_var =
Boolean::<ScalarFieldMNT6>::new_witness(cs.clone(), || Ok(base_case)).unwrap();
tcp.generate_constraints(
cs.clone(),
&msg_var,
&witness_var,
&[prior_msg_var],
&base_case_var,
)
.unwrap();
assert_eq!(cs.is_satisfied().unwrap(), expected_result);
}
#[test]
fn base_case_is_ok() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(
&transactions()[1].clone(),
input_index,
&genesis_txid,
);
let prior_msg = msg.clone(); // Can be anything
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(), // Can be anything
true,
true,
);
}
#[test]
fn base_case_with_bad_tx_fails() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(
&transactions()[2].clone(),
input_index,
&genesis_txid,
); // Wrong transaction: its parent is not the genesis
let prior_msg = msg.clone(); // Can be anything
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(), // Can be anything
true,
false,
);
}
#[test]
fn base_case_with_bad_index_fails() {
let mut bad_tx = transactions()[1].clone();
bad_tx.inputs = [TxIn {
prev_output: OutPoint {
hash: bad_tx.inputs[0].prev_output.hash,
index: 1, // Not primary chain
},
unlock_script: bad_tx.inputs[0].unlock_script.clone(),
sequence: bad_tx.inputs[0].sequence,
}]
.to_vec();
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(&bad_tx, input_index, &genesis_txid); // Wrong outpoint: its not part of a primary chain
let prior_msg = msg.clone(); // Can be anything
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(), // Can be anything
true,
false,
);
}
#[test]
fn base_case_with_bad_genesis_and_correct_chain_index_fails() {
let input_index = 0u32;
let output_index = 0u32;
let msg =
MessageUniversalTCP::new_from_tx(&transactions()[1].clone(), input_index, &[0; 32]); // Bad genesis_txid
let prior_msg = msg.clone(); // Can be anything
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(),
true,
false,
);
}
#[test]
fn base_case_with_bad_chain_index_and_correct_genesis_fails() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 1u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(
&transactions()[1].clone(),
input_index,
&genesis_txid,
); // Bad input_index
let prior_msg = msg.clone(); // Can be anything
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(),
true,
false,
);
}
#[test]
fn recursive_case_is_ok() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(
&transactions()[2].clone(),
input_index,
&genesis_txid,
);
let prior_msg: MessageUniversalTCP = MessageUniversalTCP::new_from_tx(
&transactions()[1].clone(),
input_index,
&genesis_txid,
);
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(),
false,
true,
);
}
#[test]
fn recursive_case_with_bad_tx_fails() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(
&transactions()[2].clone(),
input_index,
&genesis_txid,
);
let prior_msg = msg.clone(); // Wrong transaction, is not the previous one
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(), // **NOTE**: The witness is correct
false,
false,
);
}
#[test]
fn recursive_case_with_bad_index_fails() {
let mut bad_tx = transactions()[2].clone();
bad_tx.inputs = [TxIn {
prev_output: OutPoint {
hash: bad_tx.inputs[0].prev_output.hash,
index: 1, // Not primary chain
},
unlock_script: bad_tx.inputs[0].unlock_script.clone(),
sequence: bad_tx.inputs[0].sequence,
}]
.to_vec();
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(&bad_tx, input_index, &genesis_txid);
let prior_msg = MessageUniversalTCP::new_from_tx(
&transactions()[1].clone(),
input_index,
&genesis_txid,
); // The prior message is correct
test_predicate(
input_index,
output_index,
msg, // Wrong input: its not part of a primary chain
prior_msg,
transactions()[1].clone().into(), // Witness is correct
false,
false,
);
}
#[test]
fn recursive_case_with_bad_genesis_and_correct_chain_index_fails() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 0u32;
let output_index = 0u32;
let msg =
MessageUniversalTCP::new_from_tx(&transactions()[2].clone(), input_index, &[0; 32]);
let prior_msg = MessageUniversalTCP::new_from_tx(
&transactions()[1].clone(),
input_index,
&genesis_txid,
); // The prior message is correct
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(), // The witness is correct
false,
false,
);
}
#[test]
fn recursive_case_with_bad_chain_index_and_correct_genesis_fails() {
let genesis_txid = transactions()[0].hash().0;
let input_index = 1u32;
let output_index = 0u32;
let msg = MessageUniversalTCP::new_from_tx(
&transactions()[2].clone(),
input_index,
&genesis_txid,
); // Bad input_index
let prior_msg = MessageUniversalTCP::new_from_tx(
&transactions()[1].clone(),
input_index,
&genesis_txid,
); // The prior message is correct
test_predicate(
input_index,
output_index,
msg,
prior_msg,
transactions()[1].clone().into(),
false,
false,
);
}
}