-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
1b64376
commit 92ccf4c
Showing
12 changed files
with
275 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ Cargo.lock | |
# Added by cargo | ||
|
||
/target | ||
|
||
# Ignore macos specific files | ||
.DS_Store |
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 was deleted.
Oops, something went wrong.
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,69 @@ | ||
use askeladd_core::prover_service::ProverService; | ||
use askeladd_core::types::FibonnacciProvingRequest; | ||
use nostr_sdk::prelude::*; | ||
|
||
const SUBSCRIBED_RELAYS: &[&str] = &[ | ||
"wss://nostr.oxtr.dev", | ||
"wss://relay.damus.io", | ||
"wss://nostr.openchain.fr", | ||
]; | ||
const PROVING_REQ_SUB_ID: &str = "askeladd_proving_request"; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let opts = Options::new().wait_for_send(false); | ||
let client = Client::builder().opts(opts).build(); | ||
|
||
for relay in SUBSCRIBED_RELAYS { | ||
client.add_relay(Url::parse(relay).unwrap()).await?; | ||
} | ||
|
||
client.connect().await; | ||
|
||
let proving_req_sub_id = SubscriptionId::new(PROVING_REQ_SUB_ID); | ||
let filter = Filter::new().kind(Kind::TextNote); | ||
|
||
client | ||
.subscribe_with_id(proving_req_sub_id.clone(), vec![filter], None) | ||
.await; | ||
|
||
let proving_service: ProverService = Default::default(); | ||
|
||
client | ||
.handle_notifications(|notification| async { | ||
if let RelayPoolNotification::Event { | ||
subscription_id, | ||
event, | ||
.. | ||
} = notification | ||
{ | ||
if subscription_id == proving_req_sub_id { | ||
println!("Proving request received: {:?}", event); | ||
|
||
// Deserialize the request | ||
if let Ok(request) = | ||
serde_json::from_str::<FibonnacciProvingRequest>(&event.content) | ||
{ | ||
// Generate the proof | ||
match proving_service.generate_proof(request) { | ||
Ok(response) => { | ||
// Serialize the response to JSON | ||
let response_json = serde_json::to_string(&response)?; | ||
|
||
// Publish the proving response | ||
let tags = vec![]; | ||
let event_id = | ||
client.publish_text_note(response_json, tags).await?; | ||
println!("Proving response published with event ID: {}", event_id); | ||
} | ||
Err(e) => println!("Proof generation failed: {}", e), | ||
} | ||
} | ||
} | ||
} | ||
Ok(false) | ||
}) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
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,82 @@ | ||
use askeladd_core::types::{FibonnacciProvingRequest, FibonnacciProvingResponse}; | ||
use askeladd_core::verifier_service::VerifierService; | ||
use nostr_sdk::prelude::*; | ||
use uuid::Uuid; | ||
|
||
const SUBSCRIBED_RELAYS: &[&str] = &[ | ||
"wss://nostr.oxtr.dev", | ||
"wss://relay.damus.io", | ||
"wss://nostr.openchain.fr", | ||
]; | ||
const PROVING_RESP_SUB_ID: &str = "askeladd_proving_response"; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let opts = Options::new().wait_for_send(false); | ||
let client = Client::builder().opts(opts).build(); | ||
|
||
for relay in SUBSCRIBED_RELAYS { | ||
client.add_relay(Url::parse(relay).unwrap()).await?; | ||
} | ||
|
||
client.connect().await; | ||
|
||
// Generate a unique request ID | ||
let request_id = Uuid::new_v4().to_string(); | ||
|
||
// Create a proving request | ||
let proving_request = FibonnacciProvingRequest { | ||
request_id: request_id.clone(), | ||
log_size: 5, | ||
claim: 443693538, | ||
}; | ||
|
||
// Serialize the request to JSON | ||
let request_json = serde_json::to_string(&proving_request)?; | ||
|
||
// Publish the proving request | ||
let event_id = client.publish_text_note(request_json, []).await?; | ||
|
||
println!("Proving request published with event ID: {}", event_id); | ||
|
||
// Subscribe to proving responses | ||
let proving_resp_sub_id = SubscriptionId::new(PROVING_RESP_SUB_ID); | ||
let filter = Filter::new().kind(Kind::TextNote).since(Timestamp::now()); | ||
|
||
client | ||
.subscribe_with_id(proving_resp_sub_id.clone(), vec![filter], None) | ||
.await; | ||
|
||
// Handle subscription notifications | ||
client | ||
.handle_notifications(|notification| async { | ||
if let RelayPoolNotification::Event { | ||
subscription_id, | ||
event, | ||
.. | ||
} = notification | ||
{ | ||
if subscription_id == proving_resp_sub_id { | ||
println!("Proving response received: {:?}", event); | ||
|
||
// Deserialize the response | ||
if let Ok(response) = | ||
serde_json::from_str::<FibonnacciProvingResponse>(&event.content) | ||
{ | ||
// Verify the proof | ||
let verifier_service: VerifierService = Default::default(); | ||
match verifier_service.verify_proof(response) { | ||
Ok(_) => println!("Proof successfully verified"), | ||
Err(e) => println!("Proof verification failed: {}", e), | ||
} | ||
return Ok(true); // Stop listening after receiving and verifying the | ||
// response | ||
} | ||
} | ||
} | ||
Ok(false) | ||
}) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod prover_service; | ||
pub mod types; | ||
pub mod verifier_service; | ||
pub mod wrappers; |
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 |
---|---|---|
@@ -1,18 +1,35 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use stwo_prover::core::prover::StarkProof; | ||
|
||
/// A request to generate a proof for a Fibonnacci sequence. | ||
use crate::wrappers::{ConversionError, StarkProofWrapper}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct FibonnacciProvingRequest { | ||
/// The size of the log to generate. | ||
pub request_id: String, | ||
pub log_size: u32, | ||
/// The claim to be proved. | ||
pub claim: u32, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct FibonnacciProvingResponse { | ||
/// The size of the log to generate. | ||
pub request_id: String, | ||
pub log_size: u32, | ||
/// The claim to be proved. | ||
pub claim: u32, | ||
/// The proof generated for the request. | ||
pub proof: StarkProof, | ||
pub proof: StarkProofWrapper, | ||
} | ||
|
||
impl FibonnacciProvingResponse { | ||
pub fn new(request_id: String, log_size: u32, claim: u32, proof: StarkProof) -> Self { | ||
Self { | ||
request_id, | ||
log_size, | ||
claim, | ||
proof: proof.into(), | ||
} | ||
} | ||
|
||
pub fn into_stark_proof(self) -> Result<(String, u32, u32, StarkProof), ConversionError> { | ||
let proof = self.proof.try_into().unwrap(); | ||
Ok((self.request_id, self.log_size, self.claim, proof)) | ||
} | ||
} |
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,50 @@ | ||
use std::mem; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use stwo_prover::core::prover::StarkProof; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct StarkProofWrapper(Vec<u8>); | ||
|
||
/// Warning: This is a hack to get around the fact that StarkProof is not serializable. | ||
/// TODO: Remove this when StarkProof is serializable. | ||
impl From<StarkProof> for StarkProofWrapper { | ||
fn from(proof: StarkProof) -> Self { | ||
let bytes = unsafe { | ||
let ptr = &proof as *const StarkProof as *const u8; | ||
std::slice::from_raw_parts(ptr, mem::size_of::<StarkProof>()) | ||
}; | ||
StarkProofWrapper(bytes.to_vec()) | ||
} | ||
} | ||
|
||
/// Warning: This is a hack to get around the fact that StarkProof is not serializable. | ||
/// TODO: Remove this when StarkProof is serializable. | ||
impl TryFrom<StarkProofWrapper> for StarkProof { | ||
type Error = &'static str; | ||
|
||
fn try_from(wrapper: StarkProofWrapper) -> Result<Self, Self::Error> { | ||
if wrapper.0.len() != mem::size_of::<StarkProof>() { | ||
return Err("Invalid byte length for StarkProof"); | ||
} | ||
|
||
let proof = unsafe { | ||
let ptr = wrapper.0.as_ptr() as *const StarkProof; | ||
ptr.read() | ||
}; | ||
|
||
Ok(proof) | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum ConversionError { | ||
#[error("Invalid hash")] | ||
InvalidHash, | ||
#[error("Invalid lookup value")] | ||
InvalidLookupValue, | ||
#[error("Invalid sampled value")] | ||
InvalidSampledValue, | ||
#[error("Invalid Merkle proof")] | ||
InvalidMerkleProof, | ||
} |