use crate::error::{EmbedError, Result};
use std::num::{NonZeroU64, NonZeroUsize};
pub const MIN_SUPPLEMENTARY_ATTESTATION_EVIDENCE_BYTES: usize = 1;
pub const MAX_SUPPLEMENTARY_ATTESTATION_EVIDENCE_BYTES: usize = 4096;
#[derive(Debug)]
pub struct NativeResourceBudget {
max_concurrent_preparations: NonZeroUsize,
max_concurrent_encodes: NonZeroUsize,
max_retained_bytes: NonZeroU64,
max_transient_work_bytes: NonZeroU64,
total_accounted_bytes: u64,
}
impl NativeResourceBudget {
pub fn try_new(
max_concurrent_preparations: NonZeroUsize,
max_concurrent_encodes: NonZeroUsize,
max_retained_bytes: NonZeroU64,
max_transient_work_bytes: NonZeroU64,
) -> Result<Self> {
let retained_bytes = max_retained_bytes.get();
let transient_work_bytes = max_transient_work_bytes.get();
let total_accounted_bytes = retained_bytes.checked_add(transient_work_bytes).ok_or(
EmbedError::ResourceBudgetOverflow {
retained_bytes,
transient_work_bytes,
},
)?;
Ok(Self {
max_concurrent_preparations,
max_concurrent_encodes,
max_retained_bytes,
max_transient_work_bytes,
total_accounted_bytes,
})
}
pub fn max_concurrent_preparations(&self) -> NonZeroUsize {
self.max_concurrent_preparations
}
pub fn max_concurrent_encodes(&self) -> NonZeroUsize {
self.max_concurrent_encodes
}
pub fn max_retained_bytes(&self) -> NonZeroU64 {
self.max_retained_bytes
}
pub fn max_transient_work_bytes(&self) -> NonZeroU64 {
self.max_transient_work_bytes
}
pub fn total_accounted_bytes(&self) -> u64 {
self.total_accounted_bytes
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AttestationAlgorithm {
Sha256V1,
}
#[derive(Debug, PartialEq, Eq)]
pub struct SupplementaryAttestationEvidence {
#[allow(dead_code)] algorithm: AttestationAlgorithm,
#[allow(dead_code)] digest: [u8; 32],
payload: Box<[u8]>,
}
impl SupplementaryAttestationEvidence {
pub fn try_new(
algorithm: AttestationAlgorithm,
digest: [u8; 32],
payload: Vec<u8>,
) -> Result<Self> {
let length = payload.len();
if !(MIN_SUPPLEMENTARY_ATTESTATION_EVIDENCE_BYTES
..=MAX_SUPPLEMENTARY_ATTESTATION_EVIDENCE_BYTES)
.contains(&length)
{
return Err(EmbedError::AttestationReportSize {
length,
min: MIN_SUPPLEMENTARY_ATTESTATION_EVIDENCE_BYTES,
max: MAX_SUPPLEMENTARY_ATTESTATION_EVIDENCE_BYTES,
});
}
Ok(Self {
algorithm,
digest,
payload: payload.into_boxed_slice(),
})
}
pub fn as_bytes(&self) -> &[u8] {
&self.payload
}
}
pub trait CheckpointAttestor: Send + 'static {
fn begin(&mut self, file_count: u64) -> Result<()>;
fn begin_file(&mut self, logical_path: &[u8], declared_len: u64) -> Result<()>;
fn chunk(&mut self, bytes: &[u8]) -> Result<()>;
fn end_file(&mut self) -> Result<()>;
fn finish(self) -> Result<SupplementaryAttestationEvidence>;
}
#[cfg_attr(not(test), allow(dead_code))]
mod resource;
#[cfg_attr(not(test), allow(dead_code))]
mod limits;
#[cfg_attr(not(test), allow(dead_code))]
mod tensor_inventory;