binius_core/oracle/
oracle_id.rs

1// Copyright 2025 Irreducible Inc.
2
3use std::fmt;
4
5use binius_utils::{DeserializeBytes, SerializationError, SerializationMode, SerializeBytes};
6
7/// Identifier for a multilinear oracle in a [`super::MultilinearOracleSet`].
8///
9/// This is essentially an index.
10#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct OracleId(usize);
12
13impl OracleId {
14	/// Create an Oracle ID from the index this oracle stored in the
15	/// [`super::MultilinearOracleSet`].
16	///
17	/// Largely an escape hatch and discouraged to use.
18	pub fn from_index(index: usize) -> Self {
19		Self(index)
20	}
21
22	/// Returns the index in the associated [`super::MultilinearOracleSet`].
23	///
24	/// Largely an escape hatch and discouraged to use.
25	pub fn index(&self) -> usize {
26		self.0
27	}
28
29	/// Returns an invalid OracleId.
30	pub const fn invalid() -> Self {
31		Self(usize::MAX)
32	}
33}
34
35impl SerializeBytes for OracleId {
36	fn serialize(
37		&self,
38		write_buf: impl bytes::BufMut,
39		mode: SerializationMode,
40	) -> Result<(), SerializationError> {
41		let index: u32 = self.0 as u32;
42		u32::serialize(&index, write_buf, mode)
43	}
44}
45impl DeserializeBytes for OracleId {
46	fn deserialize(
47		read_buf: impl bytes::Buf,
48		mode: SerializationMode,
49	) -> Result<Self, SerializationError>
50	where
51		Self: Sized,
52	{
53		let index = u32::deserialize(read_buf, mode)?;
54		Ok(Self(index as usize))
55	}
56}
57
58impl fmt::Display for OracleId {
59	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60		// Delegate to debug.
61		write!(f, "{self:?}")
62	}
63}
64
65// Technically, there must be no notion of a "default" oracle ID. However, there is some code that
66// requires that, so until it's fixed this is going to mean INVALID.
67impl Default for OracleId {
68	fn default() -> Self {
69		Self::invalid()
70	}
71}