67 lines
1.6 KiB
Python
67 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import hashlib
|
|
from dataclasses import dataclass, asdict, field
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
def _digest(data: str) -> str:
|
|
return hashlib.sha256(data.encode("utf-8")).hexdigest()
|
|
|
|
|
|
@dataclass
|
|
class LocalProblem:
|
|
id: str
|
|
neighborhood: str
|
|
tasks: List[Dict[str, Any]] # each task: {type, constraints, etc}
|
|
capacity: int
|
|
equity_budget: float
|
|
|
|
def to_json(self) -> str:
|
|
return json.dumps(asdict(self), sort_keys=True)
|
|
|
|
@staticmethod
|
|
def from_dict(d: Dict[str, Any]) -> "LocalProblem":
|
|
return LocalProblem(
|
|
id=d["id"],
|
|
neighborhood=d["neighborhood"],
|
|
tasks=d.get("tasks", []),
|
|
capacity=d.get("capacity", 0),
|
|
equity_budget=d.get("equity_budget", 0.0),
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class SharedSignals:
|
|
version: int
|
|
signals: Dict[str, Any]
|
|
|
|
def to_json(self) -> str:
|
|
return json.dumps(asdict(self), sort_keys=True)
|
|
|
|
|
|
@dataclass
|
|
class PlanDelta:
|
|
version: int
|
|
plan: Dict[str, Any]
|
|
provenance: str = "unknown"
|
|
signature: str = ""
|
|
|
|
def to_json(self) -> str:
|
|
return json.dumps(asdict(self), sort_keys=True)
|
|
|
|
def digest(self) -> str:
|
|
return _digest(self.to_json())
|
|
|
|
|
|
class CryptoProof:
|
|
@staticmethod
|
|
def sign(payload: str, key: str = "default") -> str:
|
|
# Very lightweight placeholder signature (not cryptographically secure)
|
|
return _digest(payload + "|" + key)
|
|
|
|
@staticmethod
|
|
def verify(signature: str, payload: str, key: str = "default") -> bool:
|
|
return signature == CryptoProof.sign(payload, key)
|