dusk_cdf/
witness.rs

1use std::io;
2
3use crate::{
4    Config, DecodableElement, DecodedSource, DecoderContext, Element, EncodableElement,
5    EncodableSource, EncoderContext, Preamble, Scalar,
6};
7
8/// Witness that can be encoded into a CDF file
9#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct EncodableWitness {
11    id: usize,
12    constraint: Option<usize>,
13    value: Scalar,
14    source: EncodableSource,
15}
16
17impl EncodableWitness {
18    /// Create a new encodable witness instance
19    pub const fn new(
20        id: usize,
21        constraint: Option<usize>,
22        value: Scalar,
23        source: EncodableSource,
24    ) -> Self {
25        Self {
26            id,
27            constraint,
28            value,
29            source,
30        }
31    }
32
33    /// Id of the witness in the constraint system
34    pub const fn id(&self) -> usize {
35        self.id
36    }
37
38    /// Constraint that originated the witness
39    pub const fn constraint(&self) -> Option<usize> {
40        self.constraint
41    }
42
43    /// Value of the witness in the constraint system
44    pub const fn value(&self) -> &Scalar {
45        &self.value
46    }
47
48    /// Source reference to be encoded
49    pub const fn source(&self) -> &EncodableSource {
50        &self.source
51    }
52}
53
54impl Element for EncodableWitness {
55    fn len(ctx: &Config) -> usize {
56        usize::len(ctx) + <Option<usize>>::len(ctx) + Scalar::len(ctx) + EncodableSource::len(ctx)
57    }
58
59    fn validate(&self, preamble: &Preamble) -> io::Result<()> {
60        self.id.validate(preamble)?;
61        self.constraint.validate(preamble)?;
62        self.value.validate(preamble)?;
63        self.source.validate(preamble)?;
64
65        Ok(())
66    }
67}
68
69impl EncodableElement for EncodableWitness {
70    fn to_buffer(&self, ctx: &mut EncoderContext, buf: &mut [u8]) {
71        let buf = self.id.encode(ctx, buf);
72        let buf = self.constraint.encode(ctx, buf);
73        let buf = self.value.encode(ctx, buf);
74        let _ = self.source.encode(ctx, buf);
75    }
76}
77
78/// Witness decoded from a CDF file
79#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
80pub struct Witness<'a> {
81    id: usize,
82    constraint: Option<usize>,
83    value: Scalar,
84    source: DecodedSource<'a>,
85}
86
87impl<'a> Witness<'a> {
88    /// Constructor private to the crate because witness is suposed to be created from the cdf
89    /// file
90    pub(crate) const fn _new(
91        id: usize,
92        constraint: Option<usize>,
93        value: Scalar,
94        source: DecodedSource<'a>,
95    ) -> Self {
96        Self {
97            id,
98            constraint,
99            value,
100            source,
101        }
102    }
103
104    /// Id of the witness in the constraint system
105    pub const fn id(&self) -> usize {
106        self.id
107    }
108
109    /// Constraint that originated the witness
110    pub const fn constraint(&self) -> Option<usize> {
111        self.constraint
112    }
113
114    /// Value of the witness in the constraint system
115    pub const fn value(&self) -> &Scalar {
116        &self.value
117    }
118
119    /// Line of the source code
120    pub const fn line(&self) -> u64 {
121        self.source.line
122    }
123
124    /// Column of the source code
125    pub const fn col(&self) -> u64 {
126        self.source.col
127    }
128
129    /// Source file name
130    pub const fn name(&self) -> &str {
131        self.source.name
132    }
133
134    /// Source code contents
135    pub const fn contents(&self) -> &str {
136        self.source.contents
137    }
138}
139
140impl<'a> Element for Witness<'a> {
141    fn len(ctx: &Config) -> usize {
142        usize::len(ctx) + <Option<usize>>::len(ctx) + Scalar::len(ctx) + DecodedSource::len(ctx)
143    }
144
145    fn validate(&self, preamble: &Preamble) -> io::Result<()> {
146        self.id.validate(preamble)?;
147        self.constraint.validate(preamble)?;
148        self.value.validate(preamble)?;
149        self.source.validate(preamble)?;
150
151        Ok(())
152    }
153}
154
155impl<'a> DecodableElement for Witness<'a> {
156    fn try_from_buffer_in_place<'x, 'b>(
157        &'x mut self,
158        ctx: &DecoderContext<'x>,
159        buf: &'b [u8],
160    ) -> io::Result<()> {
161        Self::validate_buffer(ctx.config(), buf)?;
162
163        let buf = self.id.try_decode_in_place(ctx, buf)?;
164        let buf = self.constraint.try_decode_in_place(ctx, buf)?;
165        let buf = self.value.try_decode_in_place(ctx, buf)?;
166        let _ = self.source.try_decode_in_place(ctx, buf)?;
167
168        Ok(())
169    }
170}