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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::evm_circuit::util::{
    constraint_builder::EVMConstraintBuilder, math_gadget::*, sum, CachedRegion,
};
use eth_types::Field;
use halo2_proofs::plonk::{Error, Expression};

/// Returns (lt, eq):
/// - `lt` is `1` when `lhs < rhs`, `0` otherwise.
/// - `eq` is `1` when `lhs == rhs`, `0` otherwise.
/// lhs and rhs `< 256**N_BYTES`
/// `N_BYTES` is required to be `<= MAX_N_BYTES_INTEGER`.
#[derive(Clone, Debug)]
pub struct ComparisonGadget<F, const N_BYTES: usize> {
    lt: LtGadget<F, N_BYTES>,
    eq: IsZeroGadget<F>,
}

impl<F: Field, const N_BYTES: usize> ComparisonGadget<F, N_BYTES> {
    pub(crate) fn construct(
        cb: &mut EVMConstraintBuilder<F>,
        lhs: Expression<F>,
        rhs: Expression<F>,
    ) -> Self {
        let lt = cb.is_lt(lhs, rhs);
        let eq = cb.is_zero(sum::expr(&lt.diff_bytes()));

        Self { lt, eq }
    }

    pub(crate) fn expr(&self) -> (Expression<F>, Expression<F>) {
        (self.lt.expr(), self.eq.expr())
    }

    pub(crate) fn assign(
        &self,
        region: &mut CachedRegion<'_, '_, F>,
        offset: usize,
        lhs: F,
        rhs: F,
    ) -> Result<(F, F), Error> {
        // lt
        let (lt, diff) = self.lt.assign(region, offset, lhs, rhs)?;

        // eq
        let eq = self.eq.assign(region, offset, sum::value(&diff))?;

        Ok((lt, eq))
    }
}

#[cfg(test)]
mod tests {
    use super::{test_util::*, *};
    use crate::evm_circuit::util::{constraint_builder::ConstrainBuilderCommon, Cell};
    use eth_types::*;
    use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error};

    #[derive(Clone)]
    /// ComparisonTestContainer: require(a == b if CHECK_EQ else a < b)
    struct ComparisonTestContainer<F, const N: usize, const CHECK_EQ: bool> {
        cmp_gadget: ComparisonGadget<F, N>,
        a: Cell<F>,
        b: Cell<F>,
    }

    impl<F: Field, const N: usize, const CHECK_EQ: bool> MathGadgetContainer<F>
        for ComparisonTestContainer<F, N, CHECK_EQ>
    {
        fn configure_gadget_container(cb: &mut EVMConstraintBuilder<F>) -> Self {
            let a = cb.query_cell();
            let b = cb.query_cell();
            let cmp_gadget = ComparisonGadget::<F, N>::construct(cb, a.expr(), b.expr());
            cb.require_equal(
                "(a < b) * (a == b) == 0",
                cmp_gadget.expr().0 * cmp_gadget.expr().1,
                0.expr(),
            );

            if CHECK_EQ {
                cb.require_equal("a == b", cmp_gadget.expr().1, 1.expr());
            } else {
                cb.require_equal("a < b", cmp_gadget.expr().0, 1.expr());
            }

            ComparisonTestContainer { cmp_gadget, a, b }
        }

        fn assign_gadget_container(
            &self,
            witnesses: &[Word],
            region: &mut CachedRegion<'_, '_, F>,
        ) -> Result<(), Error> {
            let a = witnesses[0].to_scalar().unwrap();
            let b = witnesses[1].to_scalar().unwrap();
            let offset = 0;

            self.a.assign(region, offset, Value::known(a))?;
            self.b.assign(region, offset, Value::known(b))?;
            self.cmp_gadget.assign(region, offset, a, b)?;

            Ok(())
        }
    }

    #[test]
    fn test_comparison_0_eq() {
        // a == b check
        try_test!(
            ComparisonTestContainer<Fr, 4, true>,
            [Word::from(0), Word::from(0)],
            true,
        );
    }

    #[test]
    fn test_comparison_1_eq() {
        try_test!(
            ComparisonTestContainer<Fr, 4, true>,
            [Word::from(1), Word::from(1)],
            true,
        );
    }

    #[test]
    fn test_comparison_max_eq() {
        try_test!(
            ComparisonTestContainer<Fr, 4, true>,
            [Word::from(1 << 4), Word::from(1 << 4)],
            true,
        );
    }

    #[test]
    fn test_comparison_0_neq_max() {
        try_test!(
            ComparisonTestContainer<Fr, 4, true>,
            [Word::from(0), Word::from(1 << 4)],
            false,
        );
    }

    // a < b check
    #[test]
    fn test_comparison_0_lt_1() {
        try_test!(
            ComparisonTestContainer<Fr, 4, false>,
            [Word::from(0), Word::from(1)],
            true,
        );
    }

    #[test]
    fn test_comparison_1_lt_max() {
        try_test!(
            ComparisonTestContainer<Fr, 4, false>,
            [Word::from(1), Word::from(1 << 4)],
            true,
        );
    }

    #[test]
    fn test_comparison_1_lt_0() {
        try_test!(
            ComparisonTestContainer<Fr, 4, false>,
            [Word::from(1), Word::from(0)],
            false,
        );
    }

    #[test]
    fn test_comparison_8bytes_lowmax_lt_highmax() {
        const N_BYTES: usize = 16;
        let half_max_lo = U256([u64::MAX, 0, 0, 0]);
        let half_max_hi = U256([0, u64::MAX, 0, 0]);
        try_test!(
            ComparisonTestContainer<Fr, N_BYTES, false>,
            [half_max_lo, half_max_hi],
            true,
        );

        try_test!(
            ComparisonTestContainer<Fr, N_BYTES, false>,
            [half_max_hi, half_max_lo],
            false,
        );
    }

    #[test]
    fn test_comparison_overflow() {
        try_test!(
            ComparisonTestContainer<Fr, 4, false>,
            [Word::from(10000), Word::from(1 << (4 + 1))],
            false,
        );
    }
}