Skip to content

Commit

Permalink
update API: is_xxx to check_xxx when self is mutated (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenfeizhang authored Apr 14, 2022
1 parent 5eb3059 commit e487c12
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 57 deletions.
8 changes: 4 additions & 4 deletions plonk/src/circuit/customized/ecc/glv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ where
let right_coeff = [lambda_1, F::one(), F::zero(), F::zero()];
let right_var = circuit.lc(&right_wire, &right_coeff)?;

circuit.is_equal(left_var, right_var)?
circuit.check_equal(left_var, right_var)?
};

let k2_is_neg_sat = {
Expand All @@ -517,7 +517,7 @@ where
let right_wire = [*s_var, t_var, circuit.zero(), circuit.zero()];
let right_coeff = [F::one(), r1, F::zero(), F::zero()];
let right_var = circuit.lc(&right_wire, &right_coeff)?;
circuit.is_equal(left_var, right_var)?
circuit.check_equal(left_var, right_var)?
};

// (f.3) either f.1 or f.2 is satisfied
Expand All @@ -535,7 +535,7 @@ where

let right_var = circuit.mul_constant(t_var, &r2)?;

circuit.is_equal(left_var, right_var)?
circuit.check_equal(left_var, right_var)?
};

let k2_is_neg_sat = {
Expand All @@ -547,7 +547,7 @@ where

let right_var = circuit.mul_constant(k2_var, &lambda_2)?;

circuit.is_equal(left_var, right_var)?
circuit.check_equal(left_var, right_var)?
};

// (g.3) either g.1 or g.2 is satisfied
Expand Down
20 changes: 10 additions & 10 deletions plonk/src/circuit/customized/ecc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ where

/// Obtain a bool variable representing whether two point variables are
/// equal. Return error if point variables are invalid.
pub fn is_equal_point(
pub fn check_equal_point(
&mut self,
point0: &PointVariable,
point1: &PointVariable,
) -> Result<Variable, PlonkError> {
self.check_point_var_bound(point0)?;
self.check_point_var_bound(point1)?;
let x_eq = self.is_equal(point0.0, point1.0)?;
let y_eq = self.is_equal(point0.1, point1.1)?;
let x_eq = self.check_equal(point0.0, point1.0)?;
let y_eq = self.check_equal(point0.1, point1.1)?;
self.mul(x_eq, y_eq)
}
}
Expand Down Expand Up @@ -305,9 +305,9 @@ where
self.check_var_bound(expected_neutral)?;

// constraint 1: b_x = is_equal(x, 0);
let b_x = self.is_equal(point_var.0, self.zero())?;
let b_x = self.check_equal(point_var.0, self.zero())?;
// constraint 2: b_y = is_equal(y, 1);
let b_y = self.is_equal(point_var.1, self.one())?;
let b_y = self.check_equal(point_var.1, self.one())?;
// constraint 3: b = b_x * b_y;
self.mul_gate(b_x, b_y, expected_neutral)?;
Ok(())
Expand Down Expand Up @@ -1019,8 +1019,8 @@ mod test {
let p1_var = circuit.create_point_variable(Point::from(p1))?;
let p2_var = circuit.create_point_variable(Point::from(p2))?;
let p3_var = circuit.create_point_variable(Point::from(p3))?;
let p1_p2_eq = circuit.is_equal_point(&p1_var, &p2_var)?;
let p1_p3_eq = circuit.is_equal_point(&p1_var, &p3_var)?;
let p1_p2_eq = circuit.check_equal_point(&p1_var, &p2_var)?;
let p1_p3_eq = circuit.check_equal_point(&p1_var, &p3_var)?;

assert_eq!(circuit.witness(p1_p2_eq)?, F::one());
assert_eq!(circuit.witness(p1_p3_eq)?, F::zero());
Expand All @@ -1029,7 +1029,7 @@ mod test {
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
// Check variable out of bound error.
assert!(circuit
.is_equal_point(&PointVariable(0, 0), &PointVariable(1, circuit.num_vars()))
.check_equal_point(&PointVariable(0, 0), &PointVariable(1, circuit.num_vars()))
.is_err());

let circuit_1 =
Expand All @@ -1050,8 +1050,8 @@ mod test {
let p1_var = circuit.create_point_variable(Point::from(p1))?;
let p2_var = circuit.create_point_variable(Point::from(p2))?;
let p3_var = circuit.create_point_variable(Point::from(p3))?;
circuit.is_equal_point(&p1_var, &p2_var)?;
circuit.is_equal_point(&p1_var, &p3_var)?;
circuit.check_equal_point(&p1_var, &p2_var)?;
circuit.check_equal_point(&p1_var, &p3_var)?;
circuit.finalize_for_arithmetization()?;
Ok(circuit)
}
Expand Down
76 changes: 38 additions & 38 deletions plonk/src/circuit/customized/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,16 @@ where

/// Obtain a bool variable representing whether two input variables are
/// equal. Return error if variables are invalid.
pub fn is_equal(&mut self, a: Variable, b: Variable) -> Result<Variable, PlonkError> {
pub fn check_equal(&mut self, a: Variable, b: Variable) -> Result<Variable, PlonkError> {
self.check_var_bound(a)?;
self.check_var_bound(b)?;
let delta = self.sub(a, b)?;
self.is_zero(delta)
self.check_is_zero(delta)
}

/// Obtain a bool variable representing whether input variable is zero.
/// Return error if the input variable is invalid.
pub fn is_zero(&mut self, a: Variable) -> Result<Variable, PlonkError> {
pub fn check_is_zero(&mut self, a: Variable) -> Result<Variable, PlonkError> {
self.check_var_bound(a)?;

// y is the bit indicating if a == zero
Expand Down Expand Up @@ -374,7 +374,7 @@ where
/// variable representing the result of a logic negation gate. Return the
/// index of the variable. Return error if the input variable is invalid.
pub fn logic_neg(&mut self, a: Variable) -> Result<Variable, PlonkError> {
self.is_zero(a)
self.check_is_zero(a)
}

/// Assuming values represented by `a` and `b` are boolean, obtain a
Expand Down Expand Up @@ -654,12 +654,12 @@ impl<F: PrimeField> PlonkCircuit<F> {
/// Return a boolean variable indicating whether variable `a` is in the
/// range [0, 2^`bit_len`). Return error if the variable is invalid.
/// TODO: optimize the gate for UltraPlonk.
pub fn is_in_range(&mut self, a: Variable, bit_len: usize) -> Result<Variable, PlonkError> {
pub fn check_in_range(&mut self, a: Variable, bit_len: usize) -> Result<Variable, PlonkError> {
let a_bit_le = self.unpack(a, F::size_in_bits())?;
// a is in range if and only if the bits in `a_bit_le[bit_len..]` are all
// zeroes.
let higher_bit_sum = self.sum(&a_bit_le[bit_len..])?;
self.is_zero(higher_bit_sum)
self.check_is_zero(higher_bit_sum)
}

/// Obtain the `bit_len`-long binary representation of variable `a`
Expand Down Expand Up @@ -900,8 +900,8 @@ pub(crate) mod test {
let val = F::from(31415u32);
let a = circuit.create_variable(val)?;
let b = circuit.create_variable(val)?;
let a_b_eq = circuit.is_equal(a, b)?;
let a_zero_eq = circuit.is_equal(a, circuit.zero())?;
let a_b_eq = circuit.check_equal(a, b)?;
let a_zero_eq = circuit.check_equal(a, circuit.zero())?;

// check circuit
assert_eq!(circuit.witness(a_b_eq)?, F::one());
Expand All @@ -910,7 +910,7 @@ pub(crate) mod test {
*circuit.witness_mut(b) = val + F::one();
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
// Check variable out of bound error.
assert!(circuit.is_equal(circuit.num_vars(), a).is_err());
assert!(circuit.check_equal(circuit.num_vars(), a).is_err());

let circuit_1 = build_is_equal_circuit(F::one(), F::one())?;
let circuit_2 = build_is_equal_circuit(F::zero(), F::one())?;
Expand All @@ -923,24 +923,24 @@ pub(crate) mod test {
let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_turbo_plonk();
let a = circuit.create_variable(a)?;
let b = circuit.create_variable(b)?;
circuit.is_equal(a, b)?;
circuit.check_equal(a, b)?;
circuit.finalize_for_arithmetization()?;
Ok(circuit)
}

#[test]
fn test_is_zero() -> Result<(), PlonkError> {
test_is_zero_helper::<FqEd254>()?;
test_is_zero_helper::<FqEd377>()?;
test_is_zero_helper::<FqEd381>()?;
test_is_zero_helper::<Fq377>()
fn test_check_is_zero() -> Result<(), PlonkError> {
test_check_is_zero_helper::<FqEd254>()?;
test_check_is_zero_helper::<FqEd377>()?;
test_check_is_zero_helper::<FqEd381>()?;
test_check_is_zero_helper::<Fq377>()
}
fn test_is_zero_helper<F: PrimeField>() -> Result<(), PlonkError> {
fn test_check_is_zero_helper<F: PrimeField>() -> Result<(), PlonkError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let val = F::from(31415u32);
let a = circuit.create_variable(val)?;
let a_zero_eq = circuit.is_zero(a)?;
let zero_zero_eq = circuit.is_zero(circuit.zero())?;
let a_zero_eq = circuit.check_is_zero(a)?;
let zero_zero_eq = circuit.check_is_zero(circuit.zero())?;

// check circuit
assert_eq!(circuit.witness(a_zero_eq)?, F::zero());
Expand All @@ -952,19 +952,19 @@ pub(crate) mod test {
*circuit.witness_mut(a) = F::zero();
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
// Check variable out of bound error.
assert!(circuit.is_zero(circuit.num_vars()).is_err());
assert!(circuit.check_is_zero(circuit.num_vars()).is_err());

let circuit_1 = build_is_zero_circuit(F::one())?;
let circuit_2 = build_is_zero_circuit(F::zero())?;
let circuit_1 = build_check_is_zero_circuit(F::one())?;
let circuit_2 = build_check_is_zero_circuit(F::zero())?;
test_variable_independence_for_circuit(circuit_1, circuit_2)?;

Ok(())
}

fn build_is_zero_circuit<F: PrimeField>(a: F) -> Result<PlonkCircuit<F>, PlonkError> {
fn build_check_is_zero_circuit<F: PrimeField>(a: F) -> Result<PlonkCircuit<F>, PlonkError> {
let mut circuit = PlonkCircuit::new_turbo_plonk();
let a = circuit.create_variable(a)?;
circuit.is_zero(a)?;
circuit.check_is_zero(a)?;
circuit.finalize_for_arithmetization()?;
Ok(circuit)
}
Expand Down Expand Up @@ -1357,19 +1357,19 @@ pub(crate) mod test {
}

#[test]
fn test_is_in_range() -> Result<(), PlonkError> {
test_is_in_range_helper::<FqEd254>()?;
test_is_in_range_helper::<FqEd377>()?;
test_is_in_range_helper::<FqEd381>()?;
test_is_in_range_helper::<Fq377>()
fn test_check_in_range() -> Result<(), PlonkError> {
test_check_in_range_helper::<FqEd254>()?;
test_check_in_range_helper::<FqEd377>()?;
test_check_in_range_helper::<FqEd381>()?;
test_check_in_range_helper::<Fq377>()
}
fn test_is_in_range_helper<F: PrimeField>() -> Result<(), PlonkError> {
fn test_check_in_range_helper<F: PrimeField>() -> Result<(), PlonkError> {
let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_turbo_plonk();
let a = circuit.create_variable(F::from(1023u32))?;

let b1 = circuit.is_in_range(a, 5)?;
let b2 = circuit.is_in_range(a, 10)?;
let b3 = circuit.is_in_range(a, 0)?;
let b1 = circuit.check_in_range(a, 5)?;
let b2 = circuit.check_in_range(a, 10)?;
let b3 = circuit.check_in_range(a, 0)?;
assert_eq!(circuit.witness(b1)?, F::zero());
assert_eq!(circuit.witness(b2)?, F::one());
assert_eq!(circuit.witness(b3)?, F::zero());
Expand All @@ -1379,22 +1379,22 @@ pub(crate) mod test {
*circuit.witness_mut(a) = F::from(1024u32);
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
// Check variable out of bound error.
assert!(circuit.is_in_range(circuit.num_vars(), 10).is_err());
assert!(circuit.check_in_range(circuit.num_vars(), 10).is_err());

// build two fixed circuits with different variable assignments, checking that
// the arithmetized extended permutation polynomial is variable
// independent
let circuit_1 = build_is_in_range_circuit(F::from(314u32))?;
let circuit_2 = build_is_in_range_circuit(F::from(1489u32))?;
let circuit_1 = build_check_in_range_circuit(F::from(314u32))?;
let circuit_2 = build_check_in_range_circuit(F::from(1489u32))?;
test_variable_independence_for_circuit(circuit_1, circuit_2)?;

Ok(())
}

fn build_is_in_range_circuit<F: PrimeField>(a: F) -> Result<PlonkCircuit<F>, PlonkError> {
fn build_check_in_range_circuit<F: PrimeField>(a: F) -> Result<PlonkCircuit<F>, PlonkError> {
let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_turbo_plonk();
let a_var = circuit.create_variable(a)?;
circuit.is_in_range(a_var, 10)?;
circuit.check_in_range(a_var, 10)?;
circuit.finalize_for_arithmetization()?;
Ok(circuit)
}
Expand All @@ -1414,7 +1414,7 @@ pub(crate) mod test {
let val = F::from(31415u32);
let a = circuit.create_variable(val)?;
let b = circuit.create_variable(val)?;
circuit.is_equal(a, b)?;
circuit.check_equal(a, b)?;

// lc gate
let wire_in: Vec<_> = [
Expand Down
14 changes: 9 additions & 5 deletions primitives/src/circuit/schnorr_dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
/// * `msg` - message variables that have been signed.
/// * `sig` - signature variable.
/// * `returns` - a bool variable indicating whether the signature is valid.
fn is_valid_signature(
fn check_valid_signature(
&mut self,
vk: &VerKeyVar,
msg: &[Variable],
Expand Down Expand Up @@ -105,14 +105,14 @@ where
Ok(())
}

fn is_valid_signature(
fn check_valid_signature(
&mut self,
vk: &VerKeyVar,
msg: &[Variable],
sig: &SignatureVar,
) -> Result<Variable, PlonkError> {
let (p1, p2) = <Self as SignatureGadget<F, P>>::verify_sig_core(self, vk, msg, sig)?;
self.is_equal_point(&p1, &p2)
self.check_equal_point(&p1, &p2)
}

fn create_signature_variable(
Expand Down Expand Up @@ -311,8 +311,12 @@ mod tests {
.iter()
.map(|m| circuit.create_variable(*m))
.collect::<Result<Vec<_>, PlonkError>>()?;
let bit =
SignatureGadget::<_, P>::is_valid_signature(&mut circuit, &vk_var, &msg_var, &sig_var)?;
let bit = SignatureGadget::<_, P>::check_valid_signature(
&mut circuit,
&vk_var,
&msg_var,
&sig_var,
)?;
Ok((circuit, bit))
}
}

0 comments on commit e487c12

Please sign in to comment.