1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#![forbid(unsafe_code)]
#![cfg_attr(test, allow(clippy::assertions_on_result_states))]
mod equal;
mod helpers;
#[cfg(test)]
use console::TestRng;
#[cfg(test)]
use snarkvm_circuit_environment::assert_scope;
use snarkvm_circuit_environment::prelude::*;
use snarkvm_circuit_types_boolean::Boolean;
use snarkvm_circuit_types_field::Field;
use snarkvm_circuit_types_integers::U8;
#[derive(Clone)]
pub struct StringType<E: Environment> {
mode: Mode,
bytes: Vec<U8<E>>,
size_in_bytes: Field<E>,
}
impl<E: Environment> StringTrait for StringType<E> {}
#[cfg(console)]
impl<E: Environment> Inject for StringType<E> {
type Primitive = console::StringType<E::Network>;
fn new(mode: Mode, string: Self::Primitive) -> Self {
let num_bytes =
console::Field::from_u32(u32::try_from(string.len()).unwrap_or_else(|error| E::halt(error.to_string())));
let expected_size_in_bytes = Field::constant(num_bytes);
let size_in_bytes = match mode.is_constant() {
true => expected_size_in_bytes.clone(),
false => Field::new(Mode::Private, num_bytes),
};
E::assert_eq(&expected_size_in_bytes, &size_in_bytes);
Self {
mode,
bytes: string.as_bytes().iter().map(|byte| U8::new(mode, console::Integer::new(*byte))).collect(),
size_in_bytes,
}
}
}
#[cfg(console)]
impl<E: Environment> Eject for StringType<E> {
type Primitive = console::StringType<E::Network>;
fn eject_mode(&self) -> Mode {
match self.bytes.is_empty() {
true => self.mode,
false => self.bytes.eject_mode(),
}
}
fn eject_value(&self) -> Self::Primitive {
let num_bytes = self.bytes.len();
match num_bytes <= E::MAX_STRING_BYTES as usize {
true => console::StringType::new(
&String::from_utf8(self.bytes.eject_value().into_iter().map(|byte| *byte).collect())
.unwrap_or_else(|error| E::halt(format!("Failed to eject a string value: {error}"))),
),
false => E::halt(format!("Attempted to eject a string of size {num_bytes}")),
}
}
}
#[cfg(console)]
impl<E: Environment> Parser for StringType<E> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
let (string, content) = console::StringType::parse(string)?;
let (string, mode) = opt(pair(tag("."), Mode::parse))(string)?;
match mode {
Some((_, mode)) => Ok((string, StringType::new(mode, content))),
None => Ok((string, StringType::new(Mode::Constant, content))),
}
}
}
#[cfg(console)]
impl<E: Environment> FromStr for StringType<E> {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
#[cfg(console)]
impl<E: Environment> TypeName for StringType<E> {
#[inline]
fn type_name() -> &'static str {
console::StringType::<E::Network>::type_name()
}
}
#[cfg(console)]
impl<E: Environment> Debug for StringType<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
#[cfg(console)]
impl<E: Environment> Display for StringType<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}", self.eject_value(), self.eject_mode())
}
}