Skip to content

Commit

Permalink
implemented verify post (#416)
Browse files Browse the repository at this point in the history
* implemented verify post

* Changed variable name

* Update vm/runtime/src/lib.rs

Co-authored-by: Austin Abell <austinabell8@gmail.com>

* made fixes and changes

* vm fixes

* Update lib.rs

add spaces in comments

Co-authored-by: Austin Abell <austinabell8@gmail.com>
  • Loading branch information
StaticallyTypedAnxiety and austinabell authored May 16, 2020
1 parent b7f6f92 commit 932dae3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion types/src/sector/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct WinningPoStVerifyInfo {
pub struct WindowPoStVerifyInfo {
pub randomness: PoStRandomness,
pub proofs: Vec<PoStProof>,
pub private_proof: Vec<SectorInfo>,
pub challenged_sectors: Vec<SectorInfo>,
pub prover: ActorID,
}

Expand Down
27 changes: 26 additions & 1 deletion types/src/sector/registered_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use super::SectorSize;
use filecoin_proofs_api::RegisteredSealProof;
use filecoin_proofs_api::{RegisteredPoStProof, RegisteredSealProof};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -168,6 +168,31 @@ impl From<RegisteredProof> for RegisteredSealProof {
}
}

impl From<RegisteredProof> for RegisteredPoStProof {
fn from(p: RegisteredProof) -> Self {
use RegisteredProof::*;

match p {
StackedDRG32GiBSeal
| StackedDRG32GiBPoSt
| StackedDRG32GiBWindowPoSt
| StackedDRG32GiBWinningPoSt => RegisteredPoStProof::StackedDrgWindow32GiBV1,
StackedDRG2KiBSeal
| StackedDRG2KiBPoSt
| StackedDRG2KiBWindowPoSt
| StackedDRG2KiBWinningPoSt => RegisteredPoStProof::StackedDrgWindow2KiBV1,
StackedDRG8MiBSeal
| StackedDRG8MiBPoSt
| StackedDRG8MiBWindowPoSt
| StackedDRG8MiBWinningPoSt => RegisteredPoStProof::StackedDrgWindow8MiBV1,
StackedDRG512MiBSeal
| StackedDRG512MiBPoSt
| StackedDRG512MiBWindowPoSt
| StackedDRG512MiBWinningPoSt => RegisteredPoStProof::StackedDrgWindow512MiBV1,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 3 additions & 3 deletions types/src/sector/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Serialize for WindowPoStVerifyInfo {
(
BytesSer(&self.randomness),
&self.proofs,
&self.private_proof,
&self.challenged_sectors,
&self.prover,
)
.serialize(serializer)
Expand All @@ -155,13 +155,13 @@ impl<'de> Deserialize<'de> for WindowPoStVerifyInfo {
where
D: Deserializer<'de>,
{
let (Byte32De(randomness), proofs, private_proof, prover) =
let (Byte32De(randomness), proofs, challenged_sectors, prover) =
Deserialize::deserialize(deserializer)?;

Ok(Self {
randomness,
proofs,
private_proof,
challenged_sectors,
prover,
})
}
Expand Down
46 changes: 41 additions & 5 deletions vm/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ use clock::ChainEpoch;
use commcid::{cid_to_data_commitment_v1, cid_to_replica_commitment_v1, data_commitment_v1_to_cid};
use crypto::{DomainSeparationTag, Signature};
use fil_types::{
zero_piece_commitment, PaddedPieceSize, PieceInfo, RegisteredProof, SealVerifyInfo,
zero_piece_commitment, PaddedPieceSize, PieceInfo, RegisteredProof, SealVerifyInfo, SectorInfo,
WindowPoStVerifyInfo,
};
use filecoin_proofs_api::seal::{compute_comm_d, verify_seal as proofs_verify_seal};
use filecoin_proofs_api::{
post::verify_window_post,
seal::{compute_comm_d, verify_seal as proofs_verify_seal},
PublicReplicaInfo,
};
use filecoin_proofs_api::{ProverId, SectorId};
use forest_encoding::{blake2b_256, Cbor};
use ipld_blockstore::BlockStore;
use message::UnsignedMessage;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::error::Error as StdError;
use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount};
Expand Down Expand Up @@ -212,9 +217,40 @@ pub trait Syscalls {
Ok(())
}
/// Verifies a proof of spacetime.
fn verify_post(&self, _vi: &WindowPoStVerifyInfo) -> Result<(), Box<dyn StdError>> {
// TODO
todo!()
fn verify_post(&self, verify_info: &WindowPoStVerifyInfo) -> Result<(), Box<dyn StdError>> {
type ReplicaMapResult = Result<(SectorId, PublicReplicaInfo), String>;

// collect proof bytes
let proofs = &verify_info.proofs.iter().fold(Vec::new(), |mut proof, p| {
proof.extend_from_slice(&p.proof_bytes);
proof
});

// collect replicas
let replicas = verify_info
.challenged_sectors
.iter()
.map::<ReplicaMapResult, _>(|sector_info: &SectorInfo| {
let commr = cid_to_replica_commitment_v1(&sector_info.sealed_cid)?;
let replica = PublicReplicaInfo::new(
sector_info.proof.registered_window_post_proof()?.into(),
commr,
);
Ok((SectorId::from(sector_info.sector_number), replica))
})
.collect::<Result<BTreeMap<SectorId, PublicReplicaInfo>, _>>()?;

// construct prover id
let mut prover_id = ProverId::default();
let prover_bytes = verify_info.prover.to_be_bytes();
prover_id[..prover_bytes.len()].copy_from_slice(&prover_bytes);

// verify
if !verify_window_post(&verify_info.randomness, &proofs, &replicas, prover_id)? {
return Err("Proof was invalid".to_string().into());
}

Ok(())
}
/// Verifies that two block headers provide proof of a consensus fault:
/// - both headers mined by the same actor
Expand Down

0 comments on commit 932dae3

Please sign in to comment.