Skip to content

Commit 645f253

Browse files
committed
Add unit tests to vrf_sortition
1 parent 52d4102 commit 645f253

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-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

+112
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl VRFSortition {
5353
let vrf_proof = vrf_inst.prove(&*priv_key, &*seed)?;
5454
let vrf_hash = vrf_inst.proof_to_hash(&vrf_proof)?;
5555
let j = lot(voting_power, self.total_power, self.expectation, &vrf_hash);
56+
println!("{:?}, {}", vrf_hash, j);
5657

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

0 commit comments

Comments
 (0)