Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#120 from subspace/local-challenge-from-…
Browse files Browse the repository at this point in the history
…signature

Derive local challenge from signature instead of public key hash
  • Loading branch information
nazar-pc committed Nov 8, 2021
2 parents 0a7c7d7 + 480c639 commit fac541c
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 180 deletions.
82 changes: 13 additions & 69 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/pallet-offences-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sp-std = { version = "4.0.0-dev", default-features = false, git = "https://githu
[dev-dependencies]
sp-io = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
sp-core = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
schnorrkel = "0.10.1"
schnorrkel = "0.9.1"

[features]
default = ["std"]
Expand Down
20 changes: 11 additions & 9 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sp_runtime::{
Perbill,
};
use subspace_core_primitives::{
ArchivedBlockProgress, LastArchivedBlock, Piece, RootBlock, Sha256Hash, Tag,
ArchivedBlockProgress, LastArchivedBlock, Piece, RootBlock, Sha256Hash, Signature, Tag,
};
use subspace_solving::{SubspaceCodec, SOLUTION_SIGNING_CONTEXT};

Expand Down Expand Up @@ -194,17 +194,18 @@ pub fn go_to_block(keypair: &Keypair, block: u64, slot: u64) {
let subspace_solving = SubspaceCodec::new(&keypair.public);
let ctx = schnorrkel::context::signing_context(SOLUTION_SIGNING_CONTEXT);
let piece_index = 0;
let mut piece = Piece::default();
subspace_solving.encode(piece_index, &mut piece).unwrap();
let tag: Tag = subspace_solving::create_tag(&piece, Subspace::salt().to_le_bytes());
let mut encoding = Piece::default();
subspace_solving.encode(piece_index, &mut encoding).unwrap();
let tag: Tag = subspace_solving::create_tag(&encoding, Subspace::salt().to_le_bytes());

let pre_digest = make_pre_digest(
slot.into(),
Solution {
public_key: FarmerPublicKey::from_slice(&keypair.public.to_bytes()),
piece_index: 0,
encoding: piece.to_vec(),
signature: keypair.sign(ctx.bytes(&tag)).to_bytes().to_vec(),
encoding,
signature: keypair.sign(ctx.bytes(&tag)).to_bytes().into(),
local_challenge: Signature::default(),
tag,
},
);
Expand Down Expand Up @@ -254,7 +255,7 @@ pub fn generate_equivocation_proof(
let tag: Tag = [(current_block % 8) as u8; 8];

let public_key = FarmerPublicKey::from_slice(&keypair.public.to_bytes());
let signature = keypair.sign(ctx.bytes(&tag)).to_bytes().to_vec();
let signature = keypair.sign(ctx.bytes(&tag)).to_bytes();

let make_header = |piece_index| {
let parent_hash = System::parent_hash();
Expand All @@ -263,8 +264,9 @@ pub fn generate_equivocation_proof(
Solution {
public_key: public_key.clone(),
piece_index,
encoding: encoding.to_vec(),
signature: signature.clone(),
encoding,
signature: signature.into(),
local_challenge: Signature::default(),
tag,
},
);
Expand Down
3 changes: 2 additions & 1 deletion crates/sc-consensus-subspace-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ where
piece_index: solution.piece_index,
encoding: solution.encoding,
signature: solution.signature,
local_challenge: solution.local_challenge,
tag: solution.tag,
};

Expand All @@ -326,7 +327,7 @@ where
// This will be sent to the farmer
Ok(Ok(SlotInfo {
slot_number: new_slot_info.slot.into(),
challenge: new_slot_info.challenge,
global_challenge: new_slot_info.global_challenge,
salt: new_slot_info.salt,
next_salt: new_slot_info.next_salt,
solution_range: new_slot_info.solution_range,
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace/src/authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ where

let new_slot_info = NewSlotInfo {
slot,
challenge: subspace_solving::derive_global_challenge(&epoch_randomness, slot),
global_challenge: subspace_solving::derive_global_challenge(&epoch_randomness, slot),
salt: salt.to_le_bytes(),
// TODO: This will not be the correct way in the future once salt is no longer
// just an incremented number
Expand Down
12 changes: 6 additions & 6 deletions crates/sc-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ use std::{borrow::Cow, collections::HashMap, pin::Pin, sync::Arc, time::Duration
pub use subspace_archiving::archiver::ArchivedSegment;
use subspace_archiving::archiver::{BlockArchiver, ObjectArchiver};
use subspace_archiving::pre_genesis_data;
use subspace_core_primitives::{Randomness, RootBlock, Salt};
use subspace_core_primitives::{Randomness, RootBlock, Salt, Tag};
use subspace_solving::SOLUTION_SIGNING_CONTEXT;

mod archiver;
Expand All @@ -130,8 +130,8 @@ mod verification;
pub struct NewSlotInfo {
/// Slot
pub slot: Slot,
/// Slot challenge
pub challenge: [u8; 8],
/// Global slot challenge
pub global_challenge: Tag,
/// Salt
pub salt: Salt,
/// Salt for the next eon
Expand Down Expand Up @@ -264,12 +264,12 @@ pub enum Error<B: BlockT> {
/// Bad solution signature
#[display(fmt = "Bad solution signature on slot {:?}: {:?}", _0, _1)]
BadSolutionSignature(Slot, schnorrkel::SignatureError),
/// Bad local challenge
#[display(fmt = "Local challenge is invalid for slot {}: {}", _0, _1)]
BadLocalChallenge(Slot, schnorrkel::SignatureError),
/// Solution is outside of solution range
#[display(fmt = "Solution is outside of solution range for slot {}", _0)]
OutsideOfSolutionRange(Slot),
/// Encoding is of wrong size
#[display(fmt = "Encoding is of the wrong size")]
EncodingOfWrongSize,
/// Invalid encoding of a piece
#[display(fmt = "Invalid encoding for slot {}", _0)]
InvalidEncoding(Slot),
Expand Down
Loading

0 comments on commit fac541c

Please sign in to comment.