Skip to content

Commit f016c4b

Browse files
committed
Implement priority message
1 parent b85b8e6 commit f016c4b

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

core/src/consensus/sortition/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
mod binom_cdf;
1818
mod lot;
19+
mod priority_message;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2019 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use ccrypto::sha256;
18+
use ckey::Public;
19+
use primitives::H256;
20+
use vrf::openssl::{Error as VrfError, ECVRF};
21+
use vrf::VRF;
22+
23+
pub type Priority = H256;
24+
25+
pub struct PriorityMessage {
26+
msg_hash: H256,
27+
priority: Priority,
28+
vrf_hash: Vec<u8>,
29+
vrf_proof: Vec<u8>,
30+
sub_user_index: u64,
31+
signer_index: usize,
32+
}
33+
34+
impl PriorityMessage {
35+
pub fn signer_index(&self) -> usize {
36+
self.signer_index
37+
}
38+
39+
fn verify_vrf_hash(&self, signer_public: &Public, vrf_inst: &mut ECVRF) -> Result<bool, VrfError> {
40+
let verified_hash = vrf_inst.verify(signer_public, &self.vrf_proof, &*self.msg_hash)?;
41+
Ok(verified_hash == self.vrf_hash)
42+
}
43+
44+
pub fn verify(&self, signer_public: &Public, vrf_inst: &mut ECVRF) -> Result<bool, VrfError> {
45+
let vrf_verification_result = self.verify_vrf_hash(signer_public, vrf_inst)?;
46+
if vrf_verification_result {
47+
let sub_user_idx_vec = self.sub_user_index.to_be_bytes();
48+
let concatenated = [&self.vrf_hash[..], &sub_user_idx_vec[..]].concat();
49+
50+
let expected_priority = sha256(&concatenated);
51+
Ok(expected_priority == self.priority)
52+
} else {
53+
Ok(false)
54+
}
55+
}
56+
}

core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern crate snap;
5555
extern crate statrs;
5656
extern crate table;
5757
extern crate util_error;
58+
extern crate vrf;
5859
#[macro_use]
5960
extern crate log;
6061
extern crate core;

0 commit comments

Comments
 (0)