dusk_cdf/
constraint.rs

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