open-standards/sandbox/sandbox-rs/crates/sandbox-tee/examples/attested_efficiency.rs
dcharlot65-transactionsci 6c77c66e28
sandbox-tee: correct the counter framing — provenance, not accuracy (#35)
The docs overstated the case: they called RAPL/NVML/IOReport 'a model, not a
meter' / 'estimates' / 'not physically accurate'. That's wrong. Modern vendor
counters are real on-die/on-board sensor readings, and all measurement is
model-mediated anyway. The distinction the trust ladder actually rests on is
provenance and tamper-evidence, not accuracy: vendor counters are opaque
(undisclosed derivation/error) and reach a verifier through the platform's own
untrusted software with nothing binding the number to the sensor.

Reword energy.rs (module doc, CounterClass, AttestTrust), the example, and
MAPPING §4 accordingly, and rename CounterClass::is_physical() ->
is_independently_rooted() so the code encodes the real property. IntegrityAttested
= an honest relay of the platform's self-report; PhysicallyRooted = a reading
rooted in a dedicated sensor signed at the source. No behavior change; 39 tests.

Co-authored-by: Transaction Science <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
2026-07-04 04:54:18 -04:00

118 lines
5 KiB
Rust

//! End-to-end: a completed coding task's measured energy is attested inside a TEE,
//! committed into an `efficiency/1` receipt, and then independently re-verified
//! from scratch — deriving the honest trust level, never asserting it. This is the
//! whole frontier composed in one runnable flow.
//!
//! cargo run -p sandbox-tee --example attested_efficiency
use ed25519_dalek::SigningKey;
use jcp_efficiency::{verify as verify_efficiency, Efficiency};
use jcp_schema::{ContentId, Energy};
use sandbox_tee::energy::{
attest_energy, verify_attested_energy, AttestTrust, CounterClass, EnergyStatement,
};
use sandbox_tee::provider::LocalSnpProvider;
use sandbox_tee::Grounding;
fn main() {
// ── 1. A coding task completed: 900 accepted tokens over a 2 s window, and the
// on-device meter read 117 mJ. (A RAPL figure — a real sensor reading,
// but platform-self-reported.)
let energy_uj: u64 = 117_000;
let window_ns: u64 = 2_000_000_000;
let tokens_out: u64 = 900;
// ── 2. A verifier issues a fresh nonce; we attest the reading inside a TEE. The
// LocalSnpProvider mints a real ECDSA-P384 report — software-grounded.
let nonce = [0x5au8; 32];
let measurement = [0x11u8; 48];
let provider = LocalSnpProvider::new(measurement, 7);
let statement = EnergyStatement {
counter: CounterClass::Rapl,
energy_uj,
window_ns,
nonce,
};
let attested = attest_energy(statement, &provider).expect("attest the reading");
// ── 3. Seal the efficiency/1 receipt, committing to the attestation by content id.
let sk = SigningKey::from_bytes(&[0xefu8; 32]);
let steward = "did:jcp:attested-efficiency-demo";
let eff = Efficiency {
task: "fix-auth-bug".into(),
completed: true,
verdict: "tests-pass".into(),
tokens_in: tokens_out * 8,
tokens_out,
energy: Energy::modeled(energy_uj),
wall_ns: window_ns,
value_uusd: Some(5_000_000),
energy_attestation: None,
}
.with_energy_attestation(&attested.report);
let envelope = eff.seal(0, &ContentId([0u8; 32]), &sk, steward);
println!(
"sealed efficiency/1 for '{}' — {} µJ over {} accepted tokens\n",
eff.task, energy_uj, tokens_out
);
// ── 4. A relying party runs the COMPOSED verification, from scratch:
// (a) the receipt verifies under the steward key.
let rec = verify_efficiency(&envelope, steward, &sk.verifying_key().to_bytes())
.expect("efficiency/1 verifies");
// (b) its commitment resolves to the attestation we hold.
let commitment = rec
.energy_attestation
.as_deref()
.expect("receipt carries an attestation commitment");
let resolved = ContentId::of_bytes(&attested.report).to_hex();
assert_eq!(commitment, resolved, "commitment must match the attestation");
// (c) the attestation verifies — deriving the honest trust level from evidence.
let trust = verify_attested_energy(
&attested,
measurement,
&nonce,
&provider.vcek(),
Grounding::Software,
)
.expect("attestation verifies");
// (d) the attested joules equal the sealed joules.
assert_eq!(
attested.statement.energy_uj, rec.energy_uj,
"attested energy must match the receipt"
);
println!("composed verification ✓");
println!(" receipt : verified under the steward key");
println!(" commitment : matches the attestation ({}…)", &commitment[..16]);
println!(
" attested energy : {} µJ == receipt energy {} µJ",
attested.statement.energy_uj, rec.energy_uj
);
println!(" nonce : fresh (matches the issued challenge)");
println!(" efficiency : {} µJ / accepted token", rec.uj_per_accepted_token);
println!(" TRUST LEVEL : {trust:?}");
println!();
// ── 5. The honesty the whole exercise turns on.
assert_eq!(trust, AttestTrust::IntegrityAttested);
println!(
"This is IntegrityAttested, not PhysicallyRooted: a software-grounded report\n\
over a platform-self-reported counter (RAPL) attests an honest *relay* inside\n\
a measured environment — not a reading rooted independently of the platform.\n\
(Not a claim that RAPL is inaccurate: it is a real sensor reading; nothing\n\
binds it to the sensor.) An independent root needs a dedicated sensor signed\n\
at the source (a shunt) on confidential hardware — derived here, never asserted."
);
// Prove tampering is caught: rewrite the sealed energy figure downstream.
let mut forged = attested.clone();
forged.statement.energy_uj = 1;
assert!(
verify_attested_energy(&forged, measurement, &nonce, &provider.vcek(), Grounding::Software)
.is_err(),
"a tampered energy figure must fail attestation verification"
);
println!("\n(a tampered joule figure fails attestation verification — checked.)");
}