|
| 1 | +use core::{fmt::Debug, hash::Hash}; |
| 2 | + |
| 3 | +/// Label identifying which protocol a witness item belongs to; appears in |
| 4 | +/// every [`crate::WitnessError`]. |
| 5 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 6 | +pub struct NamespaceId { |
| 7 | + pub name: &'static str, |
| 8 | +} |
| 9 | + |
| 10 | +impl NamespaceId { |
| 11 | + pub const fn new(name: &'static str) -> Self { |
| 12 | + Self { name } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/// Type-level identity of one protocol's witness taxonomy. |
| 17 | +/// |
| 18 | +/// Implemented by uninhabited enums (no values exist; all dispatch is |
| 19 | +/// compile-time). The associated types bind the protocol's polynomial, |
| 20 | +/// opening, public-input, and challenge identifier spaces, so oracles from |
| 21 | +/// different protocols cannot be mixed up. |
| 22 | +pub trait WitnessNamespace { |
| 23 | + type CommittedId: Copy + Debug + Eq + Hash; |
| 24 | + type VirtualId: Copy + Debug + Eq + Hash; |
| 25 | + type OpeningId: Copy + Debug + Eq + Hash; |
| 26 | + type PublicId: Copy + Debug + Eq + Hash; |
| 27 | + type ChallengeId: Copy + Debug + Eq + Hash; |
| 28 | + |
| 29 | + const ID: NamespaceId; |
| 30 | +} |
| 31 | + |
| 32 | +/// Committed polynomials go through the commitment scheme; virtual |
| 33 | +/// polynomials are derived during proving and never committed. |
| 34 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 35 | +pub enum OracleKind<C, V> { |
| 36 | + Committed(C), |
| 37 | + Virtual(V), |
| 38 | +} |
| 39 | + |
| 40 | +/// Typed reference to a single polynomial within namespace `N`. |
| 41 | +#[derive(Debug, PartialEq, Eq, Hash)] |
| 42 | +pub struct OracleRef<N: WitnessNamespace> { |
| 43 | + pub kind: OracleKind<N::CommittedId, N::VirtualId>, |
| 44 | +} |
| 45 | + |
| 46 | +// Manual impls: deriving would incorrectly require `N: Clone + Copy` even |
| 47 | +// though `N` only appears through its associated types. |
| 48 | +impl<N: WitnessNamespace> Clone for OracleRef<N> { |
| 49 | + fn clone(&self) -> Self { |
| 50 | + *self |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<N: WitnessNamespace> Copy for OracleRef<N> {} |
| 55 | + |
| 56 | +impl<N: WitnessNamespace> OracleRef<N> { |
| 57 | + pub const fn committed(id: N::CommittedId) -> Self { |
| 58 | + Self { |
| 59 | + kind: OracleKind::Committed(id), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + pub const fn virtual_polynomial(id: N::VirtualId) -> Self { |
| 64 | + Self { |
| 65 | + kind: OracleKind::Virtual(id), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments