-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
485 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2019 Kodebox, Inc. | ||
// This file is part of CodeChain. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use ckey::{standard_uncompressed_pubkey, Public}; | ||
use primitives::H256; | ||
use vrf::openssl::{Error as VRFError, ECVRF}; | ||
use vrf::VRF; | ||
|
||
use crate::consensus::{Height, View}; | ||
|
||
#[derive(Debug, Eq, PartialEq, Clone, Copy, RlpEncodable, RlpDecodable)] | ||
pub struct VRFSeed(H256); | ||
|
||
impl AsRef<[u8]> for VRFSeed { | ||
#[inline] | ||
fn as_ref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl ::core::ops::Deref for VRFSeed { | ||
type Target = [u8]; | ||
|
||
#[inline] | ||
fn deref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl From<H256> for VRFSeed { | ||
fn from(hash: H256) -> Self { | ||
VRFSeed(hash) | ||
} | ||
} | ||
|
||
impl From<VRFSeed> for H256 { | ||
fn from(seed: VRFSeed) -> Self { | ||
seed.0 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
impl From<u64> for VRFSeed { | ||
fn from(mut value: u64) -> Self { | ||
let hash: H256 = value.into(); | ||
hash.into() | ||
} | ||
} | ||
|
||
impl VRFSeed { | ||
pub fn zero() -> Self { | ||
VRFSeed(H256::zero()) | ||
} | ||
} | ||
|
||
impl VRFSeed { | ||
/// Calculate the common message used in next seed generation. | ||
pub fn generate_next_msg(&self, height: Height, view: View) -> Vec<u8> { | ||
[&self[..], &height.to_be_bytes(), &view.to_be_bytes()].concat() | ||
} | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, Clone, RlpEncodable, RlpDecodable)] | ||
pub struct SeedInfo { | ||
seed: VRFSeed, | ||
proof: Vec<u8>, | ||
} | ||
|
||
impl SeedInfo { | ||
pub fn from_seed_and_proof(seed: Vec<u8>, proof: Vec<u8>) -> Self { | ||
Self { | ||
seed: H256::from_slice(&seed).into(), | ||
proof, | ||
} | ||
} | ||
|
||
pub fn seed(&self) -> &VRFSeed { | ||
&self.seed | ||
} | ||
|
||
pub fn verify( | ||
&self, | ||
height: Height, | ||
view: View, | ||
prev_seed: &VRFSeed, | ||
signer_public: &Public, | ||
vrf_inst: &mut ECVRF, | ||
) -> Result<bool, VRFError> { | ||
let msg = prev_seed.generate_next_msg(height, view); | ||
let standard_pubkey = standard_uncompressed_pubkey(signer_public); | ||
vrf_inst.verify(&standard_pubkey, &self.proof, &msg).map(|expected_seed| expected_seed == self.seed.to_vec()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.