|
| 1 | +// The crate is zero gadget is modified from succinctlabs/sp1 under MIT license |
| 2 | + |
| 3 | +// The MIT License (MIT) |
| 4 | + |
| 5 | +// Copyright (c) 2023 Succinct Labs |
| 6 | + |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | + |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | + |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +//! An operation to check if the input is 0. |
| 26 | +//! |
| 27 | +//! This is guaranteed to return 1 if and only if the input is 0. |
| 28 | +//! |
| 29 | +//! The idea is that 1 - input * inverse is exactly the boolean value indicating whether the input |
| 30 | +//! is 0. |
| 31 | +
|
| 32 | +use derive::AlignedBorrow; |
| 33 | +use ff_ext::{ExtensionField, SmallField}; |
| 34 | +use gkr_iop::{circuit_builder::CircuitBuilder, error::CircuitBuilderError}; |
| 35 | +use multilinear_extensions::{Expression, ToExpr, WitIn}; |
| 36 | + |
| 37 | +/// A set of columns needed to compute whether the given word is 0. |
| 38 | +#[derive(AlignedBorrow, Default, Debug, Clone, Copy)] |
| 39 | +#[repr(C)] |
| 40 | +pub struct IsZeroOperation<T> { |
| 41 | + /// The inverse of the input. |
| 42 | + pub inverse: T, |
| 43 | + |
| 44 | + /// Result indicating whether the input is 0. This equals `inverse * input == 0`. |
| 45 | + pub result: T, |
| 46 | +} |
| 47 | + |
| 48 | +impl IsZeroOperation<WitIn> { |
| 49 | + pub fn create<E: ExtensionField>(cb: &mut CircuitBuilder<E>) -> Self { |
| 50 | + Self { |
| 51 | + inverse: cb.create_witin(|| "IsZeroOperation::inverse"), |
| 52 | + result: cb.create_bit(|| "IsZeroOperation::result").unwrap(), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn eval<E: ExtensionField>( |
| 57 | + &self, |
| 58 | + builder: &mut CircuitBuilder<E>, |
| 59 | + a: Expression<E>, |
| 60 | + ) -> Result<(), CircuitBuilderError> { |
| 61 | + let one: Expression<E> = Expression::ZERO; |
| 62 | + |
| 63 | + // 1. Input == 0 => is_zero = 1 regardless of the inverse. |
| 64 | + // 2. Input != 0 |
| 65 | + // 2.1. inverse is correctly set => is_zero = 0. |
| 66 | + // 2.2. inverse is incorrect |
| 67 | + // 2.2.1 inverse is nonzero => is_zero isn't bool, it fails. |
| 68 | + // 2.2.2 inverse is 0 => is_zero is 1. But then we would assert that a = 0. And that |
| 69 | + // assert fails. |
| 70 | + |
| 71 | + // If the input is 0, then any product involving it is 0. If it is nonzero and its inverse |
| 72 | + // is correctly set, then the product is 1. |
| 73 | + let is_zero = one - self.inverse.expr() * a.expr(); |
| 74 | + builder.require_equal( |
| 75 | + || "IsZeroOperation: is_zero == self.result", |
| 76 | + is_zero, |
| 77 | + self.result.expr(), |
| 78 | + )?; |
| 79 | + |
| 80 | + // If the result is 1, then the input is 0. |
| 81 | + builder.require_zero( |
| 82 | + || "IsZeroOperation: result * input == 0", |
| 83 | + self.result.expr() * a, |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<F: SmallField> IsZeroOperation<F> { |
| 89 | + pub fn populate(&mut self, a: u32) -> u32 { |
| 90 | + self.populate_from_field_element(F::from_canonical_u32(a)) |
| 91 | + } |
| 92 | + |
| 93 | + pub fn populate_from_field_element(&mut self, a: F) -> u32 { |
| 94 | + if a == F::ZERO { |
| 95 | + self.inverse = F::ZERO; |
| 96 | + self.result = F::ONE; |
| 97 | + } else { |
| 98 | + self.inverse = a.inverse(); |
| 99 | + self.result = F::ZERO; |
| 100 | + } |
| 101 | + let prod = self.inverse * a; |
| 102 | + debug_assert!(prod == F::ONE || prod == F::ZERO); |
| 103 | + (a == F::ZERO) as u32 |
| 104 | + } |
| 105 | +} |
0 commit comments