Repository navigation
chore(beacon): drand protobuf and drand fake signer #7747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| assets/actor_bundles.car.zst filter=lfs diff=lfs merge=lfs -text | ||
| src/beacon/drand_pb.rs linguist-generated=true |
This file contains hidden or 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,13 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package drand_pb; | ||
|
|
||
| // https://github.com/drand/drand/blob/v2.1.7/protobuf/drand/api.proto#L42-L53 | ||
| // Only the fields an unchained beacon entry needs. The rest are skipped on decode, as | ||
| // protobuf skips unknown fields: `previous_signature` is empty on unchained networks, | ||
| // `randomness` is deprecated and derived from the signature, and `metadata` names the beacon, | ||
| // which the gossip topic already identifies. | ||
| message PublicRandResponse { | ||
| uint64 round = 1; | ||
| bytes signature = 2; | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use quick_protobuf::{BytesReader, MessageRead as _, MessageWrite as _, Writer}; | ||
|
|
||
| use super::drand::{FIL_BLOCK_DELAY, FIL_GENESIS_TIME}; | ||
| use super::fake_drand_server::FakeDrandChain; | ||
| use crate::beacon::{ | ||
| Beacon, BeaconEntry, ChainInfo, DrandBeacon, DrandConfig, DrandNetwork, | ||
| drand_pb::PublicRandResponse, signatures::sign_unchained, | ||
| }; | ||
| use crate::utils::encoding::hex; | ||
| use blstrs::{G2Projective, Scalar}; | ||
| use group::{Curve, Group}; | ||
|
|
||
| /// Signs any round of a `quicknet`-like chain with a made-up key, and encodes it as `drand` | ||
| /// gossips it. | ||
| pub struct FakeDrandSigner { | ||
| secret: Scalar, | ||
| config: DrandConfig<'static>, | ||
| } | ||
|
|
||
| impl FakeDrandSigner { | ||
| pub fn new() -> Self { | ||
| // Arbitrary: any non-zero scalar makes a valid key pair. | ||
| let secret = Scalar::from(0xC0FFEEu64); | ||
|
EclesioMeloJunior marked this conversation as resolved.
|
||
| let public = G2Projective::generator() * secret; | ||
| let public_key = hex::encode(public.to_affine().to_compressed()); | ||
| // Real `quicknet` timing, so rounds map to epochs as on mainnet; the key and hashes | ||
| // are made up. | ||
| let quicknet = FakeDrandChain::quicknet().info; | ||
| Self { | ||
| secret, | ||
| config: DrandConfig { | ||
| servers: vec![], | ||
| chain_info: ChainInfo { | ||
| public_key: public_key.into(), | ||
| period: quicknet.period, | ||
| genesis_time: quicknet.genesis_time, | ||
| hash: "0011".repeat(16).into(), | ||
| group_hash: "00".repeat(32).into(), | ||
| }, | ||
| network_type: DrandNetwork::Quicknet, // unchained | ||
| // Would clash with the real quicknet config's collector. | ||
| register_metrics: false, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| pub fn entry(&self, round: u64) -> BeaconEntry { | ||
| let signature = sign_unchained(self.secret, BeaconEntry::message_unchained(round).as_ref()); | ||
| BeaconEntry::new(round, signature.0.to_compressed().to_vec()) | ||
| } | ||
|
|
||
| /// `round`, signed, as a gossiped `PublicRandResponse`. | ||
| pub fn encode(&self, round: u64) -> Vec<u8> { | ||
| self.encode_raw(round, self.entry(round).signature()) | ||
| } | ||
|
|
||
| /// A gossiped `PublicRandResponse` with any signature. | ||
| pub fn encode_raw(&self, round: u64, signature: &[u8]) -> Vec<u8> { | ||
| let mut out = Vec::new(); | ||
| PublicRandResponse { | ||
| round, | ||
| signature: signature.to_vec(), | ||
| } | ||
| .write_message(&mut Writer::new(&mut out)) | ||
| .unwrap(); | ||
| out | ||
| } | ||
|
|
||
| pub fn decode(payload: &[u8]) -> BeaconEntry { | ||
| let response = | ||
| PublicRandResponse::from_reader(&mut BytesReader::from_bytes(payload), payload) | ||
| .unwrap(); | ||
| BeaconEntry::new(response.round, response.signature) | ||
| } | ||
|
|
||
| pub fn beacon(&self) -> DrandBeacon { | ||
| DrandBeacon::new(FIL_GENESIS_TIME, FIL_BLOCK_DELAY, &self.config) | ||
| } | ||
| } | ||
|
|
||
| // Entries decoded from the gossip encoding verify against the fake chain's key. | ||
| #[test] | ||
| fn fake_drand_entries_verify() { | ||
| let d = FakeDrandSigner::new(); | ||
| let entries: Vec<_> = (1..=5) | ||
| .map(|round| FakeDrandSigner::decode(&d.encode(round))) | ||
| .collect(); | ||
|
EclesioMeloJunior marked this conversation as resolved.
|
||
| assert!( | ||
| d.beacon() | ||
| .verify_entries(&entries, &BeaconEntry::default()) | ||
| .unwrap() | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.