/
signing.rs
148 lines (136 loc) · 4.93 KB
/
signing.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
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
//! Transaction signing
use crate::{
chain::{
ergo_box::ErgoBox,
ergo_state_context::ErgoStateContext,
input::Input,
transaction::{unsigned::UnsignedTransaction, Transaction},
},
eval::Env,
sigma_protocol::prover::{Prover, ProverError},
};
use thiserror::Error;
/// Errors on transaction signing
#[derive(Error, PartialEq, Eq, Debug, Clone)]
pub enum TxSigningError {
/// error on proving an input
#[error("Prover error (tx input index {1}): {0}")]
ProverError(ProverError, usize),
/// failed to find an input in boxes_to_spend
#[error("Input box not found (index {0})")]
InputBoxNotFound(usize),
}
/// Signs a transaction (generating proofs for inputs)
pub fn sign_transaction(
prover: &dyn Prover,
tx: UnsignedTransaction,
boxes_to_spend: &[ErgoBox],
_data_boxes: &[ErgoBox],
_state_context: &ErgoStateContext,
) -> Result<Transaction, TxSigningError> {
let message_to_sign = tx.bytes_to_sign();
let mut signed_inputs: Vec<Input> = vec![];
boxes_to_spend
.iter()
.enumerate()
.try_for_each(|(idx, input_box)| {
if let Some(unsigned_input) = tx.inputs.get(idx) {
prover
.prove(
&input_box.ergo_tree,
&Env::empty(),
message_to_sign.as_slice(),
)
.map(|proof| {
let input = Input {
box_id: unsigned_input.box_id.clone(),
spending_proof: proof,
};
signed_inputs.push(input);
})
.map_err(|e| TxSigningError::ProverError(e, idx))
} else {
Err(TxSigningError::InputBoxNotFound(idx))
}
})?;
Ok(Transaction::new(
signed_inputs,
tx.data_inputs,
tx.output_candidates,
))
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::collection::vec;
use proptest::prelude::*;
use crate::chain::ergo_box::box_builder::ErgoBoxCandidateBuilder;
use crate::chain::ergo_box::BoxValue;
use crate::{
ast::{Constant, Expr},
chain::{ergo_box::NonMandatoryRegisters, input::UnsignedInput, transaction::TxId},
sigma_protocol::{
prover::TestProver,
verifier::{TestVerifier, Verifier, VerifierError},
DlogProverInput, PrivateInput,
},
types::SType,
ErgoTree,
};
use std::rc::Rc;
fn verify_tx_proofs(
tx: &Transaction,
boxes_to_spend: &[ErgoBox],
) -> Result<bool, VerifierError> {
let verifier = TestVerifier;
let message = tx.bytes_to_sign();
boxes_to_spend
.iter()
.zip(tx.inputs.clone())
.try_fold(true, |acc, (b, input)| {
let res = verifier.verify(
&b.ergo_tree,
&Env::empty(),
&input.spending_proof.proof,
&message,
)?;
Ok(res.result && acc)
})
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(16))]
#[test]
fn test_tx_signing(secrets in vec(any::<DlogProverInput>(), 1..10)) {
let boxes_to_spend: Vec<ErgoBox> = secrets.iter().map(|secret|{
let pk = secret.public_image();
let tree = ErgoTree::from(Rc::new(Expr::Const(Constant {
tpe: SType::SSigmaProp,
v: pk.into(),
})));
ErgoBox::new(BoxValue::SAFE_USER_MIN,
tree,
vec![],
NonMandatoryRegisters::empty(),
0,
TxId::zero(),
0)
}).collect();
let prover = TestProver {
secrets: secrets.clone().into_iter().map(PrivateInput::DlogProverInput).collect(),
};
let inputs = boxes_to_spend.clone().into_iter().map(UnsignedInput::from).collect();
let ergo_tree = ErgoTree::from(Rc::new(Expr::Const(Constant {
tpe: SType::SSigmaProp,
v: secrets.get(0).unwrap().public_image().into(),
})));
let candidate = ErgoBoxCandidateBuilder::new(BoxValue::SAFE_USER_MIN, ergo_tree, 0)
.build().unwrap();
let output_candidates = vec![candidate];
let tx = UnsignedTransaction::new(inputs, vec![], output_candidates);
let res = sign_transaction(Box::new(prover).as_ref(), tx, boxes_to_spend.as_slice(), vec![].as_slice(),
&ErgoStateContext::dummy());
let signed_tx = res.unwrap();
prop_assert!(verify_tx_proofs(&signed_tx, &boxes_to_spend).unwrap());
}
}
}