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
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();
self.size_in_bytes.is_equal(&other.size_in_bytes)
& this.iter().zip(&that).fold(Boolean::constant(true), |acc, (a, b)| acc & a.is_equal(b))
}
fn is_not_equal(&self, other: &Self) -> Self::Output {
!self.is_equal(other)
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
fn sample_string(mode: Mode, rng: &mut TestRng) -> StringType<Circuit> {
let given = rng.next_string(Circuit::MAX_STRING_BYTES / 4, true);
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 mut rng = TestRng::default();
let string_a = sample_string(mode, &mut rng);
let string_b = sample_string(mode, &mut rng);
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 mut rng = TestRng::default();
let string_a = sample_string(mode, &mut rng);
let string_b = sample_string(mode, &mut rng);
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, 9, 0, 0, 0)
}
#[test]
fn test_is_equal_public() -> Result<()> {
check_is_equal(Mode::Public, 0, 0, 26, 35)
}
#[test]
fn test_is_equal_private() -> Result<()> {
check_is_equal(Mode::Private, 0, 0, 26, 35)
}
#[test]
fn test_is_not_equal_constant() -> Result<()> {
check_is_not_equal(Mode::Constant, 9, 0, 0, 0)
}
#[test]
fn test_is_not_equal_public() -> Result<()> {
check_is_not_equal(Mode::Public, 0, 0, 26, 35)
}
#[test]
fn test_is_not_equal_private() -> Result<()> {
check_is_not_equal(Mode::Private, 0, 0, 26, 35)
}
}