-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
embedded.rs
199 lines (183 loc) · 7.31 KB
/
embedded.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
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
// LNP/BP Rust Library
// Written in 2020 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
use core::any::Any;
use lnpbp::client_side_validation::CommitConceal;
use super::VirtualMachine;
use crate::{
schema, schema::constants::*, script::StandardProcedure, value,
Assignments, Metadata,
};
macro_rules! push_stack {
($self:ident, $ident:literal) => {
$self.push_stack(Box::new($ident));
};
}
#[derive(Debug)]
pub struct Embedded {
transition_type: Option<schema::TransitionType>,
previous_state: Option<Assignments>,
current_state: Option<Assignments>,
current_meta: Metadata,
stack: Vec<Box<dyn Any>>,
}
impl Embedded {
pub fn with(
transition_type: Option<schema::TransitionType>,
previous_state: Option<Assignments>,
current_state: Option<Assignments>,
current_meta: Metadata,
) -> Self {
Self {
transition_type,
previous_state,
current_state,
current_meta,
stack: vec![],
}
}
pub fn execute(&mut self, proc: StandardProcedure) {
match proc {
StandardProcedure::NoInflationBySum => {
match self.previous_state {
None => {
if self.transition_type == None
|| self.transition_type
== Some(TRANSITION_TYPE_ISSUE)
{
// We are at genesis or issue transition, must check
// issue metadata
// Collect outputs
let outputs =
if let Some(ref state) = self.current_state {
state.all_state_pedersen()
} else {
push_stack!(self, 6u8);
return;
};
// Check their bulletproofs
for c in &outputs {
if c.verify_bullet_proof().is_err() {
push_stack!(self, 2u8);
return;
}
}
// Get issued supply data
let supply = match self
.current_meta
.u64(FIELD_TYPE_ISSUED_SUPPLY)
.first()
{
Some(supply) => *supply,
_ => {
push_stack!(self, 7u8);
return;
}
};
// Check zero knowledge correspondence
if value::Confidential::verify_commit_sum(
outputs
.into_iter()
.map(|c| c.commitment)
.collect(),
vec![
value::Revealed {
value: supply,
blinding: secp256k1zkp::key::ONE_KEY
.into(),
}
.commit_conceal()
.commitment,
],
) {
push_stack!(self, 0u8);
} else {
push_stack!(self, 3u8);
}
} else {
// Other types of transitions are required to have
// a previous state
push_stack!(self, 5u8);
}
}
Some(ref variant) => {
if let Assignments::DiscreteFiniteField(_) = variant {
let prev = variant.all_state_pedersen();
let curr = self
.current_state
.as_ref()
.unwrap()
.all_state_pedersen();
for p in &prev {
if p.verify_bullet_proof().is_err() {
push_stack!(self, 1u8);
return;
}
}
for c in &curr {
if c.verify_bullet_proof().is_err() {
push_stack!(self, 2u8);
return;
}
}
if value::Confidential::verify_commit_sum(
curr.into_iter()
.map(|c| c.commitment)
.collect(),
prev.into_iter()
.map(|c| c.commitment)
.collect(),
) {
push_stack!(self, 0u8);
return;
} else {
push_stack!(self, 3u8);
return;
}
}
push_stack!(self, 4u8);
}
}
}
StandardProcedure::FungibleInflation => {
push_stack!(self, 0u8);
// TODO: Implement secondary fungible issue validation (trivial)
}
StandardProcedure::NonfungibleInflation => {
push_stack!(self, 0u8);
// TODO: Implement secondary NFT issue validation (trivial)
}
StandardProcedure::ProofOfBurn => {
push_stack!(self, 0u8);
// TODO: Implement prunning validation (currently none)
}
StandardProcedure::ProofOfReserve => {
push_stack!(self, 0u8);
// TODO: Implement bitcoin script lock validation (currently
// none)
}
StandardProcedure::IdentityTransfer => {
push_stack!(self, 0u8);
// TODO: Implement
}
StandardProcedure::RightsSplit => {
push_stack!(self, 0u8);
// TODO: Implement
}
}
}
}
impl VirtualMachine for Embedded {
fn stack(&mut self) -> &mut Vec<Box<dyn Any>> {
&mut self.stack
}
}