|
| 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 | +} |
0 commit comments