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
use crate::{
cli::*,
cli_types::*,
errors::CLIError,
synthesizer::{CircuitSynthesizer, SerializedCircuit},
};
use leo_compiler::{compiler::Compiler, group::targets::edwards_bls12::EdwardsGroupType};
use leo_package::{
inputs::*,
outputs::{ChecksumFile, CircuitFile, OutputsDirectory, OUTPUTS_DIRECTORY_NAME},
root::Manifest,
source::{LibraryFile, MainFile, LIBRARY_FILENAME, MAIN_FILENAME, SOURCE_DIRECTORY_NAME},
};
use snarkvm_curves::{bls12_377::Bls12_377, edwards_bls12::Fq};
use snarkvm_models::gadgets::r1cs::ConstraintSystem;
use clap::ArgMatches;
use std::{convert::TryFrom, env::current_dir, time::Instant};
#[derive(Debug)]
pub struct BuildCommand;
impl CLI for BuildCommand {
type Options = ();
type Output = Option<(Compiler<Fq, EdwardsGroupType>, bool)>;
const ABOUT: AboutType = "Compile the current package as a program";
const ARGUMENTS: &'static [ArgumentType] = &[];
const FLAGS: &'static [FlagType] = &[];
const NAME: NameType = "build";
const OPTIONS: &'static [OptionType] = &[];
const SUBCOMMANDS: &'static [SubCommandType] = &[];
#[cfg_attr(tarpaulin, skip)]
fn parse(_arguments: &ArgMatches) -> Result<Self::Options, CLIError> {
Ok(())
}
#[cfg_attr(tarpaulin, skip)]
fn output(_options: Self::Options) -> Result<Self::Output, CLIError> {
let span = tracing::span!(tracing::Level::INFO, "Compiling");
let enter = span.enter();
let path = current_dir()?;
let manifest = Manifest::try_from(path.as_path())?;
let package_name = manifest.get_package_name();
let mut package_path = path.clone();
if package_path.is_file() {
package_path.pop();
}
let mut output_directory = package_path.clone();
output_directory.push(OUTPUTS_DIRECTORY_NAME);
tracing::info!("Starting...");
let start = Instant::now();
if LibraryFile::exists_at(&package_path) {
let mut lib_file_path = package_path.clone();
lib_file_path.push(SOURCE_DIRECTORY_NAME);
lib_file_path.push(LIBRARY_FILENAME);
tracing::info!("Compiling library... ({:?})", lib_file_path);
let _program = Compiler::<Fq, EdwardsGroupType>::parse_program_without_input(
package_name.clone(),
lib_file_path,
output_directory.clone(),
)?;
tracing::info!("Complete");
};
if MainFile::exists_at(&package_path) {
OutputsDirectory::create(&package_path)?;
let mut main_file_path = package_path.clone();
main_file_path.push(SOURCE_DIRECTORY_NAME);
main_file_path.push(MAIN_FILENAME);
let (input_string, input_path) = InputFile::new(&package_name).read_from(&path)?;
let (state_string, state_path) = StateFile::new(&package_name).read_from(&path)?;
tracing::info!("Compiling main program... ({:?})", main_file_path);
let program = Compiler::<Fq, EdwardsGroupType>::parse_program_with_input(
package_name.clone(),
main_file_path,
output_directory,
&input_string,
&input_path,
&state_string,
&state_path,
)?;
let program_checksum = program.checksum()?;
{
let mut cs = CircuitSynthesizer::<Bls12_377> {
at: vec![],
bt: vec![],
ct: vec![],
input_assignment: vec![],
aux_assignment: vec![],
};
let temporary_program = program.clone();
let output = temporary_program.compile_constraints(&mut cs)?;
tracing::debug!("Compiled constraints - {:#?}", output);
tracing::debug!("Number of constraints - {:#?}", cs.num_constraints());
let circuit_object = SerializedCircuit::from(cs);
let json = circuit_object.to_json_string().unwrap();
let circuit_file = CircuitFile::new(&package_name);
circuit_file.write_to(&path, json)?;
let serialized = circuit_file.read_from(&package_path)?;
let deserialized = SerializedCircuit::from_json_string(&serialized).unwrap();
let _circuit_synthesizer = CircuitSynthesizer::<Bls12_377>::try_from(deserialized).unwrap();
}
let checksum_file = ChecksumFile::new(&package_name);
let checksum_differs = if checksum_file.exists_at(&package_path) {
let previous_checksum = checksum_file.read_from(&package_path)?;
program_checksum != previous_checksum
} else {
true
};
if checksum_differs {
checksum_file.write_to(&path, program_checksum)?;
tracing::debug!("Checksum saved ({:?})", path);
}
tracing::info!("Complete");
drop(enter);
tracing::span!(tracing::Level::INFO, "Done").in_scope(|| {
tracing::info!("Finished in {} milliseconds\n", start.elapsed().as_millis());
});
return Ok(Some((program, checksum_differs)));
}
drop(enter);
Ok(None)
}
}