-
Notifications
You must be signed in to change notification settings - Fork 425
/
Copy pathtests.rs
217 lines (187 loc) · 6.31 KB
/
tests.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use candid::{decode_one, encode_args, encode_one, CandidType, Principal};
use pocket_ic::{PocketIc, PocketIcBuilder, WasmResult};
use schnorr_example_rust::{
PublicKeyReply, SchnorrAlgorithm, SignatureReply, SignatureVerificationReply,
};
use serde::Deserialize;
use std::path::Path;
#[test]
fn signing_and_verification_should_work_correctly() {
const ALGORITHMS: [SchnorrAlgorithm; 2] =
[SchnorrAlgorithm::Bip340Secp256k1, SchnorrAlgorithm::Ed25519];
let pic = PocketIcBuilder::new()
.with_application_subnet()
.with_ii_subnet()
.with_fiduciary_subnet()
.build();
for algorithm in ALGORITHMS {
for _trial in 0..5 {
test_impl(&pic, algorithm);
}
}
}
fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
let my_principal = Principal::anonymous();
// Create an empty example canister as the anonymous principal and add cycles.
let example_canister_id = pic.create_canister();
pic.add_cycles(example_canister_id, 2_000_000_000_000);
let example_wasm_bytes = load_schnorr_example_canister_wasm();
pic.install_canister(example_canister_id, example_wasm_bytes, vec![], None);
// Make sure the canister is properly initialized
fast_forward(&pic, 5);
// a message we can reverse to break the signature
// currently pocket IC only supports 32B messages for BIP340
let message: String = std::iter::repeat('a')
.take(16)
.chain(std::iter::repeat('b').take(16))
.collect();
let sig_reply: Result<SignatureReply, String> = update(
&pic,
my_principal,
example_canister_id,
"sign",
encode_args((message.clone(), algorithm)).unwrap(),
);
let signature_hex = sig_reply.expect("failed to sign").signature_hex;
let pk_reply: Result<PublicKeyReply, String> = update(
&pic,
my_principal,
example_canister_id,
"public_key",
encode_one(algorithm).unwrap(),
);
let public_key_hex = pk_reply.unwrap().public_key_hex;
{
let verification_reply: Result<SignatureVerificationReply, String> = update(
&pic,
my_principal,
example_canister_id,
"verify",
encode_args((
signature_hex.clone(),
message.clone(),
public_key_hex.clone(),
algorithm,
))
.unwrap(),
);
assert!(verification_reply.unwrap().is_signature_valid);
}
{
let verification_reply: Result<SignatureVerificationReply, String> = update(
&pic,
my_principal,
example_canister_id,
"verify",
encode_args((
clone_and_reverse_chars(&signature_hex),
message.clone(),
public_key_hex.clone(),
algorithm,
))
.unwrap(),
);
assert!(!verification_reply.unwrap().is_signature_valid);
}
{
let verification_reply: Result<SignatureVerificationReply, String> = update(
&pic,
my_principal,
example_canister_id,
"verify",
encode_args((
signature_hex.clone(),
clone_and_reverse_chars(&message),
public_key_hex.clone(),
algorithm,
))
.unwrap(),
);
assert!(!verification_reply.unwrap().is_signature_valid);
}
{
let verification_reply: Result<SignatureVerificationReply, String> = update(
&pic,
my_principal,
example_canister_id,
"verify",
encode_args((
signature_hex.clone(),
message.clone(),
clone_and_reverse_chars(&public_key_hex),
algorithm,
))
.unwrap(),
);
assert!(
verification_reply.is_err() || !verification_reply.unwrap().is_signature_valid,
"either the public key should fail to deserialize or the verification should fail"
);
}
{
let verification_reply: Result<SignatureVerificationReply, String> = update(
&pic,
my_principal,
example_canister_id,
"verify",
encode_args((
signature_hex.clone(),
message.clone(),
public_key_hex.clone(),
other_algorithm(algorithm),
))
.unwrap(),
);
assert!(
verification_reply.is_err(),
"ed25519 and BIP340 should have different public key sizes"
);
}
}
fn clone_and_reverse_chars(s: &str) -> String {
let mut v: Vec<_> = s.chars().collect();
v.reverse();
v.into_iter().collect()
}
fn load_schnorr_example_canister_wasm() -> Vec<u8> {
use flate2::write::GzEncoder;
use flate2::Compression;
use std::io::prelude::*;
let wasm_path =
Path::new("../../target/wasm32-unknown-unknown/release/schnorr_example_rust.wasm");
let wasm_bytes = std::fs::read(wasm_path).expect(
"wasm does not exist - run `cargo build --release --target wasm32-unknown-unknown`",
);
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(wasm_bytes.as_slice()).unwrap();
let zipped_bytes = e.finish().unwrap();
zipped_bytes
}
pub fn update<T: CandidType + for<'de> Deserialize<'de>>(
ic: &PocketIc,
sender: Principal,
canister_id: Principal,
method: &str,
args: Vec<u8>,
) -> Result<T, String> {
match ic.update_call(canister_id, sender, method, args) {
Ok(WasmResult::Reply(data)) => {
decode_one(&data).map_err(|e| format!("failed to decode reply: {e:?}"))?
}
Ok(WasmResult::Reject(error_message)) => {
Err(format!("canister rejected the message: {error_message}"))
}
Err(user_error) => Err(format!("canister returned a user error: {user_error}")),
}
}
fn fast_forward(ic: &PocketIc, ticks: u64) {
for _ in 0..ticks - 1 {
ic.tick();
}
}
fn other_algorithm(algorithm: SchnorrAlgorithm) -> SchnorrAlgorithm {
match algorithm {
SchnorrAlgorithm::Bip340Secp256k1 => SchnorrAlgorithm::Ed25519,
SchnorrAlgorithm::Ed25519 => SchnorrAlgorithm::Bip340Secp256k1,
}
}