Skip to content

Commit 174e941

Browse files
committed
Fix review comments
1 parent 5ebe1c1 commit 174e941

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+150
-93
lines changed

crates/dvt_abi/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::fs::File;
88
use std::io::Read;
99

1010
use std::error::Error;
11+
use std::path::Path;
1112

1213
pub const BLS_SIGNATURE_SIZE: usize = 96;
1314
pub const BLS_PUBKEY_SIZE: usize = 48;
@@ -380,14 +381,27 @@ impl DvtBadEncryptedShare {
380381
}
381382
}
382383

383-
pub fn read_data_from_json_file<T>(filename: &str) -> Result<T, Box<dyn Error>>
384-
where
385-
T: DeserializeOwned,
386-
{
384+
pub fn check_if_file_exist(path: &str) {
385+
if !Path::new(&path).exists() {
386+
panic!("Error: File '{}' does not exist.", path);
387+
}
388+
}
389+
390+
pub fn read_text_file(filename: &str) -> Result<String, Box<dyn Error>> {
391+
check_if_file_exist(filename);
387392
let mut file = File::open(filename).map_err(|e| format!("Error opening file: {}", e))?;
388393
let mut contents = String::new();
389394
file.read_to_string(&mut contents)
390395
.map_err(|e| format!("Error reading file: {}", e))?;
396+
Ok(contents)
397+
}
398+
399+
pub fn read_data_from_json_file<T>(filename: &str) -> Result<T, Box<dyn Error>>
400+
where
401+
T: DeserializeOwned,
402+
{
403+
check_if_file_exist(filename);
404+
let contents = read_text_file(filename)?;
391405

392406
let data: T = serde_json::from_str(&contents)?;
393407
Ok(data)

examples/finalization_test.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"params": {
3+
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json",
4+
"expected_exit_code": 0
5+
},
6+
"scenario": {
7+
"settings": {
8+
"n": 5,
9+
"k": 3,
10+
"gen_id": "89f0996b33ca953e0cf9b70a5f84cb50"
11+
},
12+
"generations": [
13+
{
14+
"base_pubkeys": [
15+
"aa772b2a290804da566759b4162de6ffc8f0a47b70908f8db932a3e8e1dff73eb04f59f7549df8a33219a333bb19659c",
16+
"a19e4b16cc1d5de8c6fd87549f9fa1fa7de066c5cb566f610909e1a712780bf8c291e82e98b023121f70d980096e4af5",
17+
"84496615716d84d3c1d69fb295c968ed4563acd51d0fd0cc624ccce384af1d479f7adbf0ee5cce74b80f248ff8224dee"
18+
],
19+
"base_hash": "0bcbd9433dfde67e0dd7c501e255a2bdc31d6968e51b1290e6ef7d264911eb91",
20+
"partial_pubkey": "8da434e68daef9af33e39ab727557a3cd86d7991cd6b545746bf92c8edec37012912cfa2292a21512bce9040a1c0e502",
21+
"message_cleartext": "test for signing",
22+
"message_signature": "8a751bd10706178f9dae64b18ccb758abd0535d9592ca06c3ac44831715c6cb4e66eff0b0248cdae23ec0f921963ae58040feb4862929549b690e2d8073f7b937e971c1a2387feb8d11fdc107390290bb254e941700932e15cc2d63165e09d52"
23+
},
24+
{
25+
"base_pubkeys": [
26+
"92cad77a95432bc1030d81b5465cb69be672c1dd0da752230bf8112f8449b03149e7fa208a6fae460a9f0a1d5bd175e9",
27+
"98876a81fe982573ec5f986956bf9bf0bcb5349d95c3c8da0aefd05a49fea6215f59b0696f906547baed90ab245804e8",
28+
"ad2c4e5b631fbded449ede4dca2d040b9c7eae58d1e73b3050486c1ba22c15a92d9ff13c05c356f974447e4fca84864a"
29+
],
30+
"base_hash": "13602b274436a91ffa3bd3eb16927e011d81fe9e2e9673769a0152acbafe3e33",
31+
"partial_pubkey": "a3cd061aab6013f7561978959482d79e9ca636392bc94d4bcad9cb6f90fe2cdf52100f211052f1570db0ca690b6a9903",
32+
"message_cleartext": "test for signing",
33+
"message_signature": "8268c0494f7228cb71472a9c8d05374336ac65b7af2b4fb52292867595d69138a75cfc9a5ff6f102de68a7e173a57f8e13a8378ef9e103adab85b9413b6455d0bb104fc176b0e4859f5c7719ac1f63955fecefbe9eca69a4fa42eadef5b27df5"
34+
},
35+
{
36+
"base_pubkeys": [
37+
"91283a9d9826347da8ef73ebe88a013989d2abecf0eb6368cb14e079da25fdf8e90fa2f793ad3566ac613882a93a531c",
38+
"b059e3afa1f18c18eabb2be01a491b97b013b493e34bcf4fb288a7975e6a71a0e45d1d6b38d308a89726e99f09a7be85",
39+
"a6d720d55aaae5b3a11758ca2dc5900ede471d508243b86d2adc715095c4a1efac82e96b6949763dfc288af555e8a9df"
40+
],
41+
"base_hash": "1ae7975df76c5df0c301d390d82180ff536f5dbbf78942e53b4ec9e9e1497a1a",
42+
"partial_pubkey": "8cbfb6cb7af927cfe5fb17621df7036de539b7ff4aa0620cdc218d6b7fe7f2e714a96bdeddb2a0dc24867a90594427e1",
43+
"message_cleartext": "test for signing",
44+
"message_signature": "8b796dd8e8a35a6b97bd740388ad2da0399839f7ce83ae85e8651e23a0d58db44ccaca43e747713624344945ee7f25d803fe0919c669518e9bce2b0ba16e88f4ca05f5568b482b1a0068158468bc3ea8c9764b3dae9c7cb56002fcd5ab2ef25c"
45+
},
46+
{
47+
"base_pubkeys": [
48+
"ac70a68387afb0ad4912970b72afc0e1143833091414498ecab27661677f7b2a79aebee35cfbd95731d3b77985b4dc72",
49+
"97bcf8fe7f75a87f5b9e4852f4b76fdad62d8af3dc8895f4db74e74d2096b10976f262e2930c89fccc484bc9edf6c0f3",
50+
"a04c57e48a2ad82468f702b1d444f37af91fe4a05034d003fd1ff422958d37c30d29760d443e9374aa98bc096abf4d2d"
51+
],
52+
"base_hash": "54effb7411607a0c6484cb68cad8a30fd03e24c0fd561c5c8a4c5baa0509f9b3",
53+
"partial_pubkey": "9892b390d9d3000c7bf04763006fbc617b7ba9c261fff35094aec3f43599f2c254ae667d9ba135747309b77cd02f1fbc",
54+
"message_cleartext": "test for signing",
55+
"message_signature": "999e7b24bee2587d687e8f358ed10627ef57ec54935bd7a500bbbb18a57e7aa21b800f8b1f487a980d7c93918fdbd8020b66ce9a9e5788a4826e610ac937d8c2ce0ad9c0ee9a5732cf73052493e9a500cc5100a15bdbf9e5b79104db52dbf07c"
56+
},
57+
{
58+
"base_pubkeys": [
59+
"a6e87e5bbed258d82a13c53e04c1c3fb5baa6649a28a0d00e4a934b6473575721a7f1f1c3787145f43a1bf15368619ee",
60+
"858276460f23124923c94bf968024dd415a39a0f638d67f1d5f27ce8d8de75273d145cd70177a26bd76c07b8e784c98a",
61+
"8174cc034101357048634437473371c7f6b8c39e2a305c2f95fc95c13ed03939a5c3bf2bdbbaab5351e8aa3443949038"
62+
],
63+
"base_hash": "dbd8b546ab29bfa87aa9d568e8315037695e8802fa0e0c9ed9009fe8a0fde402",
64+
"partial_pubkey": "b255c8a66fd1a13373537e8a4ba258f4990c141fc3c06daccda0711f5ebaffc092f0e5b0e4454e6344e2f97957be4017",
65+
"message_cleartext": "test for signing",
66+
"message_signature": "b61e78c430eaea45f8b7974c411dd6d50eb8748fde211089838b1ca93ddefd2d9571548c4ee2eb03eec232997dc3818c1917af21083157f19966c998c35e4acf6c59e8e92bf0988692026580e425742a754eb69255e834dc75d9b729cd7ca83a"
67+
}
68+
],
69+
"aggregate_pubkey": "a31d9a483703cd0da9873e5e76b4de5f7035d0a73d79b3be8667daa4fc7065a1bbb5bf77787fcf2a35bd327eecc4fa6b"
70+
}
71+
}

src/main.rs

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::env;
55

66
use jsonschema::JSONSchema;
77
use std::error::Error;
8-
use std::fs::File;
9-
use std::io::Read;
108

119
pub const SHARE_PROVER_ELF: &[u8] = include_elf!("share_exchange_prove");
1210
pub const FINALE_PROVER_ELF: &[u8] = include_elf!("finalization_prove");
@@ -47,14 +45,6 @@ struct Cli {
4745
mode: Mode,
4846
}
4947

50-
fn read_text_file(filename: &str) -> Result<String, Box<dyn Error>> {
51-
let mut file = File::open(filename).map_err(|e| format!("Error opening file: {}", e))?;
52-
let mut contents = String::new();
53-
file.read_to_string(&mut contents)
54-
.map_err(|e| format!("Error reading file: {}", e))?;
55-
Ok(contents)
56-
}
57-
5848
fn execute<T>(data: &T, elf: &[u8], show_report: bool)
5949
where
6050
T: ProverSerialize,
@@ -63,10 +53,10 @@ where
6353
let mut stdin = SP1Stdin::new();
6454
data.write(&mut stdin);
6555
let client = ProverClient::new();
66-
let (_public_values, report) = client.execute(elf, stdin).run().unwrap_or_else(|e| {
67-
eprintln!("Failed to prove: {}", e);
68-
std::process::exit(1);
69-
});
56+
let (_public_values, report) = client
57+
.execute(elf, stdin)
58+
.run()
59+
.unwrap_or_else(|e| panic!("Failed to prove: {}", e));
7060

7161
print!("executed");
7262
if show_report {
@@ -84,20 +74,16 @@ where
8474

8575
let (pk, _) = client.setup(elf);
8676

87-
print!("proving....");
8877
let proof = client.prove(&pk, stdin).run().unwrap_or_else(|e| {
89-
eprintln!("Failed to prove: {}", e);
90-
std::process::exit(1);
78+
panic!("Failed to prove: {}", e);
9179
});
9280

93-
print!("saving file");
9481
proof.save("proof.bin").unwrap();
95-
print!("file was saved")
9682
}
9783

9884
fn validate_json(schema_path: &str, json_path: &str) -> Result<(), Box<dyn Error>> {
99-
let schema = read_text_file(schema_path)?;
100-
let json = read_text_file(json_path)?;
85+
let schema = dvt_abi::read_text_file(schema_path)?;
86+
let json = dvt_abi::read_text_file(json_path)?;
10187

10288
let schema = serde_json::from_str(&schema).unwrap();
10389
let data = serde_json::from_str(&json).unwrap();
@@ -124,35 +110,27 @@ fn validate_json(schema_path: &str, json_path: &str) -> Result<(), Box<dyn Error
124110
fn main() {
125111
let args = Cli::parse();
126112

127-
if args.input_file.is_empty() {
128-
eprintln!("No file name provided after '--input-file'.");
129-
std::process::exit(1);
130-
}
131-
132-
if args.json_schema.is_some() {
133-
let ok = validate_json(args.json_schema.unwrap().as_str(), args.input_file.as_str());
113+
if let Some(path_to_schema) = args.json_schema {
114+
let ok = validate_json(path_to_schema.as_str(), args.input_file.as_str());
134115
if ok.is_err() {
135-
eprintln!("{}", ok.unwrap_err());
136-
std::process::exit(1);
116+
panic!("{}", ok.unwrap_err());
137117
}
138118
}
139119

140120
utils::setup_logger();
141121

142122
// Depending on the command type, we could do different things here.
143-
// For now, both variants handle the data similarly.
123+
// For now, all variants handle the data similarly.
144124
match args.command_type {
145125
CommandType::Share => {
146126
let data =
147127
dvt_abi::read_data_from_json_file::<dvt_abi::DvtBlsSharedData>(&args.input_file)
148128
.unwrap_or_else(|e| {
149-
eprintln!("Error parsing JSON: {}", e);
150-
std::process::exit(1);
129+
panic!("Error parsing JSON: {}", e);
151130
});
152131

153132
let abi_data = data.to_abi().unwrap_or_else(|e| {
154-
eprintln!("Error converting to ABI data: {}", e);
155-
std::process::exit(1);
133+
panic!("Error converting to ABI data: {}", e);
156134
});
157135
match args.mode {
158136
Mode::Prove => {
@@ -167,13 +145,11 @@ fn main() {
167145
let data =
168146
dvt_abi::read_data_from_json_file::<dvt_abi::DvtFinalizationData>(&args.input_file)
169147
.unwrap_or_else(|e| {
170-
eprintln!("Error parsing JSON: {}", e);
171-
std::process::exit(1);
148+
panic!("Error parsing JSON: {}", e);
172149
});
173150

174151
let abi_data = data.to_abi().unwrap_or_else(|e| {
175-
eprintln!("Error converting to ABI data: {}", e);
176-
std::process::exit(1);
152+
panic!("Error converting to ABI data: {}", e);
177153
});
178154

179155
match args.mode {
@@ -190,13 +166,11 @@ fn main() {
190166
&args.input_file,
191167
)
192168
.unwrap_or_else(|e| {
193-
eprintln!("Error parsing JSON: {}", e);
194-
std::process::exit(1);
169+
panic!("Error parsing JSON: {}", e);
195170
});
196171

197172
let abi_data = data.to_abi().unwrap_or_else(|e| {
198-
eprintln!("Error converting to ABI data: {}", e);
199-
std::process::exit(1);
173+
panic!("Error converting to ABI data: {}", e);
200174
});
201175

202176
match args.mode {
@@ -217,13 +191,11 @@ fn main() {
217191
&args.input_file,
218192
)
219193
.unwrap_or_else(|e| {
220-
eprintln!("Error parsing JSON: {}", e);
221-
std::process::exit(1);
194+
panic!("Error parsing JSON: {}", e);
222195
});
223196

224197
let abi_data = data.to_abi().unwrap_or_else(|e| {
225-
eprintln!("Error converting to ABI data: {}", e);
226-
std::process::exit(1);
198+
panic!("Error converting to ABI data: {}", e);
227199
});
228200

229201
match args.mode {

test_vectors/finalization/bad-base-hash.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

test_vectors/finalization/bad-message-signature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

test_vectors/finalization/bad-partial-pubkey.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

test_vectors/finalization/empty-generation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

test_vectors/finalization/fewer-base-pubkeys.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

test_vectors/finalization/more-base-pubkeys.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

test_vectors/finalization/wrong-aggregate-pubkey.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"params": {
33
"cmd_extra_args": "--type=finalization --json-schema-file=spec/finalization_spec.json --mode=execute",
4-
"expected_exit_code": 1
4+
"expected_exit_code": 101
55
},
66
"scenario": {
77
"settings": {

0 commit comments

Comments
 (0)