Skip to content

Commit 0871a6f

Browse files
committed
Add unit tests to vrf_sortition
1 parent 52d4102 commit 0871a6f

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ vrf = { git = "https://github.com/HoOngEe/vrf-rs.git" }
4646

4747
[dev-dependencies]
4848
rand_xorshift = "0.1.0"
49+
hex = "0.4.0"
4950

5051
[features]
5152
nightly = []

core/src/consensus/sortition/vrf_sortition.rs

+111
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,114 @@ impl PriorityInfo {
105105
expected_priority == self.priority
106106
}
107107
}
108+
109+
#[cfg(test)]
110+
mod vrf_tests {
111+
extern crate hex;
112+
113+
use ccrypto::sha256;
114+
use ckey::KeyPair;
115+
use vrf::openssl::CipherSuite;
116+
117+
use super::*;
118+
#[test]
119+
fn test_create_highest_priority_info() {
120+
let priv_key = sha256("secret_key");
121+
let seed = sha256("seed");
122+
let ec_vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap();
123+
let ec_vrf = Arc::new(RwLock::new(ec_vrf));
124+
let sortition_scheme = VRFSortition {
125+
total_power: 100,
126+
expectation: 50.0,
127+
vrf_inst: ec_vrf,
128+
};
129+
// maximized when sha256(vrf_result || byte expression of 1u64), the testing oracle is generated from python sha256.
130+
let expected_priority =
131+
H256::from_slice(&hex::decode("ddc2ca3bd180e1af8fdec721ea863f79ad33279da2148dd58953b44420a0abca").unwrap());
132+
let expected_sub_user_idx = 1;
133+
let actual_priority_info =
134+
sortition_scheme.create_highest_priority_info(seed, priv_key.into(), 10).unwrap().unwrap();
135+
assert_eq!(expected_priority, actual_priority_info.priority());
136+
assert_eq!(expected_sub_user_idx, actual_priority_info.sub_user_idx());
137+
}
138+
139+
#[test]
140+
fn test_create_highest_priority_info2() {
141+
let priv_key = sha256("secret_key");
142+
let seed = sha256("seed");
143+
let ec_vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap();
144+
let ec_vrf = Arc::new(RwLock::new(ec_vrf));
145+
let sortition_scheme = VRFSortition {
146+
total_power: 100,
147+
expectation: 1.2,
148+
vrf_inst: ec_vrf,
149+
};
150+
let actual_priority_info = sortition_scheme.create_highest_priority_info(seed, priv_key.into(), 10).unwrap();
151+
assert!(actual_priority_info.is_none());
152+
}
153+
154+
#[test]
155+
fn test_verify_vrf_hash() {
156+
let priv_key = sha256("secret_key2");
157+
let pub_key = *KeyPair::from_private(priv_key.into()).expect("Valid private key").public();
158+
let wrong_priv_key = sha256("wrong_secret_key");
159+
let wrong_pub_key = *KeyPair::from_private(wrong_priv_key.into()).expect("Valid private key").public();
160+
161+
// sha256("seed2")
162+
let seed = sha256("seed2");
163+
let ec_vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap();
164+
let ec_vrf = Arc::new(RwLock::new(ec_vrf));
165+
let sortition_scheme = VRFSortition {
166+
total_power: 100,
167+
expectation: 60.7,
168+
vrf_inst: ec_vrf,
169+
};
170+
let voting_power = 100;
171+
let priority_info =
172+
sortition_scheme.create_highest_priority_info(seed, priv_key.into(), voting_power).unwrap().unwrap();
173+
assert!(priority_info.verify_vrf_hash(&pub_key, &seed, Arc::clone(&sortition_scheme.vrf_inst)).unwrap());
174+
match priority_info.verify_vrf_hash(&wrong_pub_key, &seed, Arc::clone(&sortition_scheme.vrf_inst)) {
175+
Err(VrfError::InvalidProof) => assert!(true),
176+
_ => assert!(false),
177+
}
178+
}
179+
180+
#[test]
181+
fn test_verify_sub_user_idx() {
182+
let priv_key = sha256("secret_key3");
183+
let seed = sha256("seed3");
184+
let ec_vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap();
185+
let ec_vrf = Arc::new(RwLock::new(ec_vrf));
186+
let sortition_scheme = VRFSortition {
187+
total_power: 100,
188+
expectation: 60.7,
189+
vrf_inst: ec_vrf,
190+
};
191+
let voting_power = 100;
192+
let priority_info =
193+
sortition_scheme.create_highest_priority_info(seed, priv_key.into(), voting_power).unwrap().unwrap();
194+
assert!(priority_info.verify_sub_user_idx(
195+
voting_power,
196+
sortition_scheme.total_power,
197+
sortition_scheme.expectation
198+
));
199+
}
200+
201+
202+
#[test]
203+
fn test_priority() {
204+
let priv_key = sha256("secret_key4");
205+
let seed = sha256("seed4");
206+
let ec_vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap();
207+
let ec_vrf = Arc::new(RwLock::new(ec_vrf));
208+
let sortition_scheme = VRFSortition {
209+
total_power: 100,
210+
expectation: 41.85,
211+
vrf_inst: ec_vrf,
212+
};
213+
let voting_power = 50;
214+
let priority_info =
215+
sortition_scheme.create_highest_priority_info(seed, priv_key.into(), voting_power).unwrap().unwrap();
216+
assert!(priority_info.verify_priority());
217+
}
218+
}

0 commit comments

Comments
 (0)