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
use super::*;
impl<E: Environment> Equal<Self> for StringType<E> {
type Output = Boolean<E>;
fn is_equal(&self, other: &Self) -> Self::Output {
let this = self.to_fields();
let that = other.to_fields();
if this.len() != that.len() {
return Boolean::constant(false);
}
this.iter().zip_eq(&that).fold(Boolean::constant(true), |acc, (a, b)| acc & a.is_equal(b))
}
fn is_not_equal(&self, other: &Self) -> Self::Output {
let this = self.to_fields();
let that = other.to_fields();
if this.len() != that.len() {
return Boolean::constant(true);
}
this.iter().zip_eq(&that).fold(Boolean::constant(false), |acc, (a, b)| acc | a.is_not_equal(b))
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
use rand::Rng;
fn sample_string(mode: Mode) -> StringType<Circuit> {
let rng = &mut test_rng();
let given: String = (0..Circuit::MAX_STRING_BYTES / 4).map(|_| rng.gen::<char>()).collect();
StringType::<Circuit>::new(mode, console::StringType::new(&given))
}
fn check_is_equal(
mode: Mode,
num_constants: u64,
num_public: u64,
num_private: u64,
num_constraints: u64,
) -> Result<()> {
let string_a = sample_string(mode);
let string_b = sample_string(mode);
Circuit::scope(&format!("{}", mode), || {
let candidate = string_a.is_equal(&string_a);
assert!(candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::scope(&format!("{}", mode), || {
let candidate = string_a.is_equal(&string_b);
assert!(!candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
Ok(())
}
fn check_is_not_equal(
mode: Mode,
num_constants: u64,
num_public: u64,
num_private: u64,
num_constraints: u64,
) -> Result<()> {
let string_a = sample_string(mode);
let string_b = sample_string(mode);
Circuit::scope(&format!("{}", mode), || {
let candidate = string_a.is_not_equal(&string_b);
assert!(candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::scope(&format!("{}", mode), || {
let candidate = string_a.is_not_equal(&string_a);
assert!(!candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
Ok(())
}
#[test]
fn test_is_equal_constant() -> Result<()> {
check_is_equal(Mode::Constant, 8, 0, 0, 0)
}
#[test]
fn test_is_equal_public() -> Result<()> {
check_is_equal(Mode::Public, 0, 0, 23, 31)
}
#[test]
fn test_is_equal_private() -> Result<()> {
check_is_equal(Mode::Private, 0, 0, 23, 31)
}
#[test]
fn test_is_not_equal_constant() -> Result<()> {
check_is_not_equal(Mode::Constant, 8, 0, 0, 0)
}
#[test]
fn test_is_not_equal_public() -> Result<()> {
check_is_not_equal(Mode::Public, 0, 0, 23, 31)
}
#[test]
fn test_is_not_equal_private() -> Result<()> {
check_is_not_equal(Mode::Private, 0, 0, 23, 31)
}
}