Skip to content

A real yolo-y vibez pod viewer built by claude code that gubsheep and… #138

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

Draft
wants to merge 8 commits into
base: robknight/serialization
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ edition = "2021"
name = "pod2"
path = "src/lib.rs"

[[bin]]
name = "pod2-server"
path = "src/bin/server.rs"

[dependencies]
hex = "0.4.3"
itertools = "0.14.0"
Expand All @@ -16,6 +20,18 @@ anyhow = "1.0.56"
dyn-clone = "1.0.18"
# enabled by features:
plonky2 = { git = "https://github.com/0xPolygonZero/plonky2", optional = true }
serde = "1.0.219"
serde_json = "1.0.140"
base64 = "0.22.1"
# Web server dependencies
actix-web = "4.4.1"
actix-cors = "0.6.5"
actix-files = "0.6.2"
env_logger = "0.10.1"
log = "0.4.20"
uuid = { version = "1.5.0", features = ["v4", "serde"] }
chrono = { version = "0.4.31", features = ["serde"] }
futures = "0.3.28"

[features]
default = ["backend_plonky2"]
Expand Down
27 changes: 21 additions & 6 deletions src/backends/plonky2/basetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//! `backend_plonky2` feature is enabled.
//! See src/middleware/basetypes.rs for more details.

use crate::frontend::serialization::{
deserialize_hash_tuple, deserialize_value_tuple, serialize_hash_tuple, serialize_value_tuple,
};
use crate::middleware::{Params, ToFields};
use anyhow::{anyhow, Error, Result};
use hex::{FromHex, FromHexError};
use plonky2::field::goldilocks_field::GoldilocksField;
Expand All @@ -10,11 +14,10 @@ use plonky2::hash::poseidon::PoseidonHash;
use plonky2::plonk::config::Hasher;
use plonky2::plonk::config::PoseidonGoldilocksConfig;
use plonky2::plonk::proof::Proof as Plonky2Proof;
use serde::{Deserialize, Serialize};
use std::cmp::{Ord, Ordering};
use std::fmt;

use crate::middleware::{Params, ToFields};

use crate::backends::counter;

/// F is the native field we use everywhere. Currently it's Goldilocks from plonky2
Expand All @@ -34,8 +37,14 @@ pub const EMPTY: Value = Value([F::ZERO, F::ZERO, F::ZERO, F::ZERO]);
pub const SELF_ID_HASH: Hash = Hash([F::ONE, F::ZERO, F::ZERO, F::ZERO]);
pub const NULL: Hash = Hash([F::ZERO, F::ZERO, F::ZERO, F::ZERO]);

#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub struct Value(pub [F; VALUE_SIZE]);
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Value(
#[serde(
serialize_with = "serialize_value_tuple",
deserialize_with = "deserialize_value_tuple"
)]
pub [F; VALUE_SIZE],
);

impl ToFields for Value {
fn to_fields(&self, _params: &Params) -> (Vec<F>, usize) {
Expand Down Expand Up @@ -117,8 +126,14 @@ impl fmt::Display for Value {
}
}

#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq)]
pub struct Hash(pub [F; HASH_SIZE]);
#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Hash(
#[serde(
serialize_with = "serialize_hash_tuple",
deserialize_with = "deserialize_hash_tuple"
)]
pub [F; HASH_SIZE],
);

pub fn hash_value(input: &Value) -> Hash {
hash_fields(&input.0)
Expand Down
20 changes: 13 additions & 7 deletions src/backends/plonky2/mock_main/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::{anyhow, Result};
use base64::prelude::*;
use itertools::Itertools;
use plonky2::hash::poseidon::PoseidonHash;
use plonky2::plonk::config::Hasher;
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::fmt;

Expand All @@ -26,14 +28,14 @@ impl PodProver for MockProver {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MockMainPod {
params: Params,
id: PodId,
input_signed_pods: Vec<Box<dyn Pod>>,
input_main_pods: Vec<Box<dyn Pod>>,
// input_signed_pods: Vec<Box<dyn Pod>>,
// input_main_pods: Vec<Box<dyn Pod>>,
// New statements introduced by this pod
input_statements: Vec<Statement>,
// input_statements: Vec<Statement>,
public_statements: Vec<Statement>,
operations: Vec<Operation>,
// All statements (inherited + new)
Expand Down Expand Up @@ -331,9 +333,9 @@ impl MockMainPod {
Ok(Self {
params: params.clone(),
id,
input_signed_pods,
input_main_pods,
input_statements,
// input_signed_pods,
// input_main_pods,
// input_statements,
public_statements,
statements,
operations,
Expand Down Expand Up @@ -468,6 +470,10 @@ impl Pod for MockMainPod {
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}

fn serialized_proof(&self) -> String {
BASE64_STANDARD.encode(serde_json::to_string(self).unwrap())
}
}

#[cfg(test)]
Expand Down
10 changes: 5 additions & 5 deletions src/backends/plonky2/mock_main/operation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use std::fmt;

use super::Statement;
use crate::middleware::{self, OperationType};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum OperationArg {
None,
Index(usize),
Expand All @@ -16,7 +16,7 @@ impl OperationArg {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Operation(pub OperationType, pub Vec<OperationArg>);

impl Operation {
Expand Down
3 changes: 2 additions & 1 deletion src/backends/plonky2/mock_main/statement.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::fmt;

use crate::middleware::{
self, AnchoredKey, NativePredicate, Params, Predicate, StatementArg, ToFields,
};

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Statement(pub Predicate, pub Vec<StatementArg>);

impl Statement {
Expand Down
14 changes: 14 additions & 0 deletions src/backends/plonky2/mock_signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ pub struct MockSignedPod {
dict: Dictionary,
}

impl MockSignedPod {
pub fn new(id: PodId, signature: String, dict: Dictionary) -> Self {
Self {
id,
signature,
dict,
}
}
}

impl Pod for MockSignedPod {
fn verify(&self) -> bool {
// Verify type
Expand Down Expand Up @@ -100,6 +110,10 @@ impl Pod for MockSignedPod {
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}

fn serialized_proof(&self) -> String {
self.signature.to_string()
}
}

#[cfg(test)]
Expand Down
49 changes: 49 additions & 0 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use actix_cors::Cors;
use actix_files::Files;
use actix_web::{App, HttpServer, middleware::Logger};
use actix_web::web;
use log::info;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use pod2::web::{
PodStorage,
get_pods, get_pod_by_id, create_signed_pod, import_pod, export_pod
};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

// Create pod storage
let storage: web::Data<PodStorage> = web::Data::new(Arc::new(RwLock::new(HashMap::new())));

info!("Starting POD2 web server at http://127.0.0.1:8080");

HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
.allow_any_method()
.allow_any_header()
.max_age(3600);

App::new()
.wrap(Logger::default())
.wrap(cors)
.app_data(storage.clone())
// API routes
.service(
web::scope("/api")
.route("/pods", web::get().to(get_pods))
.route("/pods/{id}", web::get().to(get_pod_by_id))
.route("/pods", web::post().to(create_signed_pod))
.route("/pods/import", web::post().to(import_pod))
.route("/pods/{id}/export", web::get().to(export_pod))
)
// Serve static files from 'static' directory
.service(Files::new("/", "static").index_file("index.html"))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
15 changes: 8 additions & 7 deletions src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::collections::HashMap;

use crate::backends::plonky2::mock_signed::MockSigner;
use crate::frontend::{
containers::{Dictionary, Set},
MainPodBuilder, Operation, OperationArg, SignedPod, SignedPodBuilder, Statement, Value,
};
use crate::middleware::containers::Set;
use crate::middleware::{containers::Dictionary, Params, PodType, KEY_SIGNER, KEY_TYPE};
use crate::middleware::{hash_str, CustomPredicateRef, NativeOperation, OperationType};
use crate::middleware::{CustomPredicateRef, NativeOperation, OperationType};
use crate::middleware::{Params, PodType, KEY_SIGNER, KEY_TYPE};
use crate::op;

// ZuKYC
Expand All @@ -28,10 +28,11 @@ pub fn zu_kyc_sign_pod_builders(
pay_stub.insert("startDate", 1706367566);

let mut sanction_list = SignedPodBuilder::new(params);
let sanctions_values = ["A343434340"].map(|s| crate::middleware::Value::from(hash_str(s)));
let sanctions_values = ["A343434340"].map(Value::from);

sanction_list.insert(
"sanctionList",
Value::Set(Set::new(&sanctions_values.to_vec()).unwrap()),
Value::Set(Set::new(sanctions_values.into())),
);

(gov_id, pay_stub, sanction_list)
Expand Down Expand Up @@ -337,7 +338,7 @@ pub fn great_boy_pod_full_flow() -> Result<MainPodBuilder> {
alice_friend_pods.push(friend.sign(&mut bob_signer).unwrap());
alice_friend_pods.push(friend.sign(&mut charlie_signer).unwrap());

let good_boy_issuers_dict = Value::Dictionary(Dictionary::new(&HashMap::new())?); // empty
let good_boy_issuers_dict = Value::Dictionary(Dictionary::new(HashMap::new())); // empty
great_boy_pod_builder(
&params,
[
Expand Down Expand Up @@ -396,6 +397,6 @@ pub fn tickets_pod_full_flow() -> Result<MainPodBuilder> {
&signed_pod,
123,
true,
&Value::Dictionary(Dictionary::new(&HashMap::new())?),
&Value::Dictionary(Dictionary::new(HashMap::new())),
)
}
Loading