use alloy::primitives::{Address, Bytes, FixedBytes};
use newton_aggregator::{rpc_server::PublicShare, PartialDecryptionData};
use newton_core::{
common::ResolvedPolicyInputs, crypto::SecureEnvelope, newton_prover_task_manager::NewtonMessage, TaskId,
};
use serde::{Deserialize, Serialize};
pub const ENCLAVE_PROTOCOL_VERSION: u16 = 3;
pub const VSOCK_PORT_COMPUTE: u32 = 5005;
pub const VSOCK_PORT_EGRESS: u32 = 5006;
pub const VSOCK_CID_HOST: u32 = 3;
pub const MAX_FRAME_LEN: usize = 16 * 1024 * 1024;
pub const MAX_EGRESS_RESPONSE_BYTES: usize = 1024 * 1024;
pub const MAX_EGRESS_REQUESTS_PER_EXECUTION: u32 = 10;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveEnvelope {
pub ref_id: Option<String>,
pub domain: Option<FixedBytes<32>>,
pub envelope: SecureEnvelope,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveEvalRequest {
pub task_id: TaskId,
pub policy_client: Address,
pub intent: NewtonMessage::Intent,
pub intent_signature: Bytes,
pub policy_task_data: NewtonMessage::PolicyTaskData,
pub resolved_policy: ResolvedPolicyInputs,
pub initialization_timestamp: u64,
pub proof_data: Option<serde_json::Value>,
pub ephemeral_envelopes: Vec<SecureEnvelope>,
pub identity_envelopes: Vec<EnclaveEnvelope>,
pub confidential_envelopes: Vec<EnclaveEnvelope>,
pub threshold: Option<ThresholdEvalInput>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThresholdEvalInput {
pub task_id: TaskId,
pub encrypted_peer_partials: Vec<EncryptedPartialDH>,
pub ephemeral_count: usize,
pub identity_count: usize,
pub confidential_count: usize,
pub public_shares: Vec<PublicShare>,
pub config: EnclaveThresholdConfig,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct EnclaveThresholdConfig {
pub threshold: u32,
pub total: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveEvalResponse {
pub verified: bool,
}
pub use newton_core::crypto::encrypted_partial::{EnclaveOperatorId, EncryptedPartialDH};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclavePartialDhRequest {
pub task_id: TaskId,
pub enc_points: Vec<Vec<u8>>,
pub peer_enclave_pubkeys: Vec<(EnclaveOperatorId, [u8; 32])>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclavePartialDhResponse {
pub encrypted_partials: Vec<EncryptedPartialDH>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetAttestationRequest {
pub nonce: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetAttestationResponse {
pub attestation_doc: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveWireRequest {
pub version: u16,
pub request_id: u64,
pub body: EnclaveWireRequestBody,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveWireResponse {
pub version: u16,
pub request_id: u64,
pub body: EnclaveWireResponseBody,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EnclaveWireRequestBody {
Health,
Init(EnclaveInitRequest),
GetPublicKey,
Evaluate(Box<EnclaveEvalRequest>),
PartialDh(EnclavePartialDhRequest),
GetAttestation(GetAttestationRequest),
PrepareEval(Box<PrepareEvalRequest>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EnclaveWireResponseBody {
Health,
Init,
PublicKey(Vec<u8>),
Evaluate(EnclaveEvalResponse),
PartialDh(EnclavePartialDhResponse),
Attestation(GetAttestationResponse),
PrepareEval(PrepareEvalResponse),
Error(EnclaveWireError),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveInitRequest {
pub hpke_private_key: Option<Vec<u8>>,
pub threshold_keystore: Option<Vec<u8>>,
pub threshold_keystore_password: Option<Vec<u8>>,
pub kms_seed_ciphertext: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrepareEvalRequest {
pub task_id: TaskId,
pub wasm_plugins: Vec<WasmPluginInput>,
pub enc_points: Vec<Vec<u8>>,
pub peer_enclave_pubkeys: Vec<(EnclaveOperatorId, [u8; 32])>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmPluginInput {
pub wasm_bytes: Vec<u8>,
pub wasm_code_hash: FixedBytes<32>,
pub wasm_args: String,
pub encrypted_secrets: Option<SecureEnvelope>,
pub max_http_calls: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrepareEvalResponse {
pub plugin_results: Vec<WasmPluginOutput>,
pub encrypted_partials: Vec<EncryptedPartialDH>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmPluginOutput {
pub result: Result<String, WasmPluginError>,
}
#[derive(Debug, Clone, thiserror::Error, Serialize, Deserialize)]
pub enum WasmPluginError {
#[error("secret decrypt failed: {0}")]
SecretDecryptFailed(String),
#[error("compilation failed: {0}")]
CompilationFailed(String),
#[error("execution failed: {0}")]
ExecutionFailed(String),
#[error("timeout: {0}")]
Timeout(String),
#[error("oversized binary: {0}")]
OversizedBinary(String),
#[error("resource exhausted: {0}")]
ResourceExhausted(String),
#[error("cancelled: {0}")]
Cancelled(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EgressRequest {
pub url: String,
pub method: String,
pub headers: Vec<(String, String)>,
pub body: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EgressResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EgressError {
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EgressWireMessage {
Request(EgressRequest),
Response(EgressResponse),
Error(EgressError),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnclaveWireError {
pub code: EnclaveErrorCode,
pub message: String,
pub retryable: bool,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum EnclaveErrorCode {
InvalidRequest,
UnsupportedVersion,
MissingInput,
DecryptFailed,
ThresholdFailed,
PolicyEvalFailed,
Uninitialized,
Internal,
KeyDerivation,
AttestationFailed,
}