snarkvm_console_program/data/plaintext/
bytes.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<N: Network> FromBytes for Plaintext<N> {
19    /// Reads the plaintext from a buffer.
20    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
21        // Read the index.
22        let index = u8::read_le(&mut reader)?;
23        // Read the plaintext.
24        let plaintext = match index {
25            0 => Self::Literal(Literal::read_le(&mut reader)?, Default::default()),
26            1 => {
27                // Read the number of members in the struct.
28                let num_members = u8::read_le(&mut reader)?;
29                // Read the members.
30                let mut members = IndexMap::with_capacity(num_members as usize);
31                for _ in 0..num_members {
32                    // Read the identifier.
33                    let identifier = Identifier::<N>::read_le(&mut reader)?;
34                    // Read the plaintext value (in 2 steps to prevent infinite recursion).
35                    let num_bytes = u16::read_le(&mut reader)?;
36                    // Read the plaintext bytes.
37                    let mut bytes = Vec::new();
38                    (&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
39                    // Recover the plaintext value.
40                    let plaintext = Plaintext::read_le(&mut bytes.as_slice())?;
41                    // Add the member.
42                    members.insert(identifier, plaintext);
43                }
44                // Return the struct.
45                Self::Struct(members, Default::default())
46            }
47            2 => {
48                // Read the length of the array.
49                let num_elements = u32::read_le(&mut reader)?;
50                if num_elements as usize > N::MAX_ARRAY_ELEMENTS {
51                    return Err(error("Failed to deserialize plaintext: Array exceeds maximum length"));
52                }
53                // Read the elements.
54                let mut elements = Vec::with_capacity(num_elements as usize);
55                for _ in 0..num_elements {
56                    // Read the plaintext value (in 2 steps to prevent infinite recursion).
57                    let num_bytes = u16::read_le(&mut reader)?;
58                    // Read the plaintext bytes.
59                    let mut bytes = Vec::new();
60                    (&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
61                    // Recover the plaintext value.
62                    let plaintext = Plaintext::read_le(&mut bytes.as_slice())?;
63                    // Add the element.
64                    elements.push(plaintext);
65                }
66                // Return the array.
67                Self::Array(elements, Default::default())
68            }
69            3.. => return Err(error(format!("Failed to decode plaintext variant {index}"))),
70        };
71        Ok(plaintext)
72    }
73}
74
75impl<N: Network> ToBytes for Plaintext<N> {
76    /// Writes the plaintext to a buffer.
77    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
78        match self {
79            Self::Literal(literal, ..) => {
80                0u8.write_le(&mut writer)?;
81                literal.write_le(&mut writer)
82            }
83            Self::Struct(struct_, ..) => {
84                1u8.write_le(&mut writer)?;
85
86                // Write the number of members in the struct.
87                u8::try_from(struct_.len()).map_err(error)?.write_le(&mut writer)?;
88
89                // Write each member.
90                for (member_name, member_value) in struct_ {
91                    // Write the member name.
92                    member_name.write_le(&mut writer)?;
93
94                    // Write the member value (performed in 2 steps to prevent infinite recursion).
95                    let bytes = member_value.to_bytes_le().map_err(|e| error(e.to_string()))?;
96                    // Write the number of bytes.
97                    u16::try_from(bytes.len()).map_err(error)?.write_le(&mut writer)?;
98                    // Write the bytes.
99                    bytes.write_le(&mut writer)?;
100                }
101                Ok(())
102            }
103            Self::Array(array, ..) => {
104                2u8.write_le(&mut writer)?;
105
106                // Write the length of the array.
107                u32::try_from(array.len()).map_err(error)?.write_le(&mut writer)?;
108
109                // Write each element.
110                for element in array {
111                    // Write the element (performed in 2 steps to prevent infinite recursion).
112                    let bytes = element.to_bytes_le().map_err(error)?;
113                    // Write the number of bytes.
114                    u16::try_from(bytes.len()).map_err(error)?.write_le(&mut writer)?;
115                    // Write the bytes.
116                    bytes.write_le(&mut writer)?;
117                }
118                Ok(())
119            }
120        }
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use snarkvm_console_network::MainnetV0;
128
129    type CurrentNetwork = MainnetV0;
130
131    const ITERATIONS: u32 = 1000;
132
133    fn check_bytes(expected: Plaintext<CurrentNetwork>) -> Result<()> {
134        // Check the byte representation.
135        let expected_bytes = expected.to_bytes_le()?;
136        assert_eq!(expected, Plaintext::read_le(&expected_bytes[..])?);
137        Ok(())
138    }
139
140    #[test]
141    fn test_bytes() -> Result<()> {
142        let rng = &mut TestRng::default();
143
144        for _ in 0..ITERATIONS {
145            let private_key = snarkvm_console_account::PrivateKey::<CurrentNetwork>::new(rng)?;
146
147            // Address
148            check_bytes(Plaintext::Literal(
149                Literal::<CurrentNetwork>::Address(Address::try_from(private_key)?),
150                Default::default(),
151            ))?;
152            // Boolean
153            check_bytes(Plaintext::Literal(
154                Literal::<CurrentNetwork>::Boolean(Boolean::new(Uniform::rand(rng))),
155                Default::default(),
156            ))?;
157            // Field
158            check_bytes(Plaintext::Literal(Literal::<CurrentNetwork>::Field(Uniform::rand(rng)), Default::default()))?;
159            // Group
160            check_bytes(Plaintext::Literal(Literal::<CurrentNetwork>::Group(Uniform::rand(rng)), Default::default()))?;
161            // I8
162            check_bytes(Plaintext::Literal(
163                Literal::<CurrentNetwork>::I8(I8::new(Uniform::rand(rng))),
164                Default::default(),
165            ))?;
166            // I16
167            check_bytes(Plaintext::Literal(
168                Literal::<CurrentNetwork>::I16(I16::new(Uniform::rand(rng))),
169                Default::default(),
170            ))?;
171            // I32
172            check_bytes(Plaintext::Literal(
173                Literal::<CurrentNetwork>::I32(I32::new(Uniform::rand(rng))),
174                Default::default(),
175            ))?;
176            // I64
177            check_bytes(Plaintext::Literal(
178                Literal::<CurrentNetwork>::I64(I64::new(Uniform::rand(rng))),
179                Default::default(),
180            ))?;
181            // I128
182            check_bytes(Plaintext::Literal(
183                Literal::<CurrentNetwork>::I128(I128::new(Uniform::rand(rng))),
184                Default::default(),
185            ))?;
186            // U8
187            check_bytes(Plaintext::Literal(
188                Literal::<CurrentNetwork>::U8(U8::new(Uniform::rand(rng))),
189                Default::default(),
190            ))?;
191            // U16
192            check_bytes(Plaintext::Literal(
193                Literal::<CurrentNetwork>::U16(U16::new(Uniform::rand(rng))),
194                Default::default(),
195            ))?;
196            // U32
197            check_bytes(Plaintext::Literal(
198                Literal::<CurrentNetwork>::U32(U32::new(Uniform::rand(rng))),
199                Default::default(),
200            ))?;
201            // U64
202            check_bytes(Plaintext::Literal(
203                Literal::<CurrentNetwork>::U64(U64::new(Uniform::rand(rng))),
204                Default::default(),
205            ))?;
206            // U128
207            check_bytes(Plaintext::Literal(
208                Literal::<CurrentNetwork>::U128(U128::new(Uniform::rand(rng))),
209                Default::default(),
210            ))?;
211            // Scalar
212            check_bytes(Plaintext::Literal(Literal::<CurrentNetwork>::Scalar(Uniform::rand(rng)), Default::default()))?;
213            // String
214            check_bytes(Plaintext::Literal(
215                Literal::<CurrentNetwork>::String(StringType::rand(rng)),
216                Default::default(),
217            ))?;
218        }
219
220        // Check the struct manually.
221        let expected = Plaintext::<CurrentNetwork>::from_str(
222            "{ owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah, token_amount: 100u64 }",
223        )?;
224
225        // Check the byte representation.
226        let expected_bytes = expected.to_bytes_le()?;
227        assert_eq!(expected, Plaintext::read_le(&expected_bytes[..])?);
228
229        // Check the array manually.
230        let expected = Plaintext::<CurrentNetwork>::from_str("[ 1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8 ]")?;
231
232        // Check the byte representation.
233        let expected_bytes = expected.to_bytes_le()?;
234        assert_eq!(expected, Plaintext::read_le(&expected_bytes[..])?);
235
236        Ok(())
237    }
238}