open-standards/sandbox/sandbox-rs/crates/sandbox-tee/tests/attested_efficiency_example.rs
Transaction Science c2b7f8af04 sandbox-tee: attested_efficiency attests what this host's meter read
The example attested a literal 117,000 µJ, described as "the on-device
meter read 117 mJ … a real sensor reading", under CounterClass::Rapl and
sealed it into efficiency/1 as Energy::modeled. No meter ran and no task
ran; on a host with no RAPL counter it printed a RAPL "real sensor
reading" at IntegrityAttested.

It now runs a bounded SHA-512 chain on this thread, meters it with
PowerMeter::detect_in_process, and attests the figure under the class of
the counter that read the CPU work, sealing it HwShunt; where no counter
read it (none live, or one that did not advance), the figure is the
meter's estimate, attested as CounterClass::Estimator and sealed
Estimator. The closing text follows the attested class. No value figure
is invented (value_uusd: None).

The kernel's per-process count (macOS ri_energy_nj) is the counter
detect_in_process reads on macOS, and no class named it, so
CounterClass::ProcessEnergy is added (tag process_energy, binding
discriminant 7; existing discriminants unchanged; not independently
rooted). jcp-efficiency MAPPING.md now describes the example as it is.

Tests: tests/attested_efficiency_example.rs compiles the example in and
asserts the attested class is one of the meter's live probes over a span
with a CPU reading and sealed HwShunt, else Estimator/Estimator, and that
a span with no CPU reading (the machine meter here) is attested as a
non-zero estimate. energy.rs: every class binds a distinct statement.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-10 17:51:59 -04:00

89 lines
3.8 KiB
Rust

//! The `attested_efficiency` example attests and seals what this host's meter
//! read for a task it ran, never a fixed figure under a counter nothing read.
//!
//! The example file is compiled into this test as a module, so what is tested
//! is what `cargo run --example attested_efficiency` runs. The attested counter
//! class must be one of the meter's live probes, over a span that has a
//! hardware reading of the CPU work, and the receipt must seal it `HwShunt`;
//! otherwise the class must be `Estimator` and the receipt `Estimator`. A
//! literal figure attested as RAPL on a host with no RAPL counter, and sealed
//! `ModelBased`, fails both.
#[allow(dead_code)]
#[path = "../examples/attested_efficiency.rs"]
mod example;
use sandbox_tee::energy::{AttestTrust, CounterClass};
/// The probe name a class's reading comes from, for the classes the example
/// can attest a reading under.
fn source_of(class: CounterClass) -> Option<&'static str> {
match class {
CounterClass::ProcessEnergy => Some("process-energy"),
CounterClass::Rapl => Some("rapl-powercap"),
CounterClass::IoReport => Some("ioreport-energy-model"),
_ => None,
}
}
#[test]
fn the_example_attests_and_seals_what_the_meter_read() {
let r = example::run();
let counter = r.attested.statement.counter;
eprintln!(
"attested_efficiency: sources {:?}; span {:?} from `{}`; attested {} µJ ({}); sealed {} µJ [{}]",
r.sources,
r.measurement.devices,
r.measurement.source,
r.attested.statement.energy_uj,
counter.tag(),
r.receipt.energy_uj,
r.receipt.provenance
);
assert_eq!(r.attested.statement.energy_uj, r.receipt.energy_uj);
assert!(r.receipt.energy_uj > 0, "a task that ran was billed nothing");
assert_eq!(r.trust, AttestTrust::IntegrityAttested);
assert!(r.tamper_rejected);
match counter {
CounterClass::Estimator => {
assert_eq!(r.receipt.provenance, "Estimator", "an estimate sealed as something else");
}
reading => {
let source = source_of(reading)
.unwrap_or_else(|| panic!("the example attested a reading under `{}`, a class it cannot read", reading.tag()));
assert!(
r.sources.contains(&source),
"attested `{}` but the meter's live probes are {:?}: a counter nothing read",
reading.tag(),
r.sources
);
assert!(r.measurement.reads_cpu(), "attested a reading of a span with no CPU reading: {:?}", r.measurement);
assert_eq!(r.receipt.provenance, "HwShunt", "a reading sealed at another grade");
}
}
if r.sources.is_empty() {
assert_eq!(counter, CounterClass::Estimator, "no live counter, yet a reading was attested");
}
}
/// A span with no hardware reading of the CPU work is attested as the
/// estimate it is: `Estimator`, no measured figure, and not free. Driven with
/// the machine-wide meter, which on a host with no live machine counter (Apple
/// Silicon whose IOReport CPU rails are not live) has no reading to give.
#[test]
fn a_span_with_no_cpu_reading_is_attested_as_an_estimate() {
let mut meter = sandbox_meter::PowerMeter::detect();
let (_, m) = meter.measure(example::task);
let (class, energy) = example::figure(&meter, &m);
eprintln!("machine meter: sources {:?}; span {:?}; attested as `{}`: {energy:?}", meter.sources(), m.devices, class.tag());
if m.reads_cpu() && example::cpu_counter(&meter.sources()).is_some() {
assert_ne!(class, CounterClass::Estimator);
assert_eq!(energy.provenance, jcp_schema::Provenance::HwShunt);
} else {
assert_eq!(class, CounterClass::Estimator);
assert_eq!((energy.provenance, energy.measured_uj), (jcp_schema::Provenance::Estimator, None));
}
assert!(energy.uj() > 0, "{energy:?}");
}