Skip to content

Rename one to one_wire for clarity in sxy, sx and sy evaluation #52

@ConstanceBeguier

Description

@ConstanceBeguier

Crate

ragu_circuits

Severity

low

Repo commit

c9649de

Description

In the eval function from ragu_circuits/s/sx.rs, the variable one refers to a wire returned by mul, while one is already conceptually associated with the constant value F::ONE. This naming overlap can reduce readability and make the code harder to follow.

Renaming one to one_wire would make it immediately clear that this variable represents a wire rather than a field constant.

Code

File name: ragu_circuits/s/sx.rs

pub fn eval<F: Field, C: Circuit<F>, R: Rank>(
    circuit: &C,
    x: F,
    key: F,
) -> Result<unstructured::Polynomial<F, R>> {
    if x == F::ZERO {
        // The polynomial is zero if x is zero.
        return Ok(Polynomial::new());
    }

    let multiplication_constraints = 0;
    let linear_constraints = 0;
    let x_inv = x.invert().expect("x is not zero");
    let xn = x.pow_vartime([R::n() as u64]);
    let xn2 = xn.square();
    let current_u_x = xn2 * x_inv;
    let current_v_x = xn2;
    let xn4 = xn2.square();
    let current_w_x = xn4 * x_inv;

    let mut collector = Collector::<F, R> {
        result: unstructured::Polynomial::new(),
        multiplication_constraints,
        linear_constraints,
        x,
        x_inv,
        current_u_x,
        current_v_x,
        current_w_x,
        one: current_w_x, // one constant value
        available_b: None,
        _marker: core::marker::PhantomData,
    };
    let (key_wire, _, one) = collector.mul(|| unreachable!())?;

    // Enforce linear constraint key_wire = key to randomize non-trivial
    // evaluations of this circuit polynomial.
    collector.enforce_zero(|lc| {
        lc.add(&key_wire)
            .add_term(&one, Coeff::NegativeArbitrary(key))
    })?;

    let mut outputs = vec![];
    let (io, _) = circuit.witness(&mut collector, Empty)?;
    io.write(&mut collector, &mut outputs)?;
    for output in outputs {
        collector.enforce_zero(|lc| lc.add(output.wire()))?;
    }
    collector.enforce_zero(|lc| lc.add(&one))?;

    collector.result[0..collector.linear_constraints].reverse();
    assert_eq!(collector.result[0], collector.one);

    Ok(collector.result)
}

Recommendations

Renaming one to one_wire.

pub fn eval<F: Field, C: Circuit<F>, R: Rank>(
    circuit: &C,
    x: F,
    key: F,
) -> Result<unstructured::Polynomial<F, R>> {
    if x == F::ZERO {
        // The polynomial is zero if x is zero.
        return Ok(Polynomial::new());
    }

    let multiplication_constraints = 0;
    let linear_constraints = 0;
    let x_inv = x.invert().expect("x is not zero");
    let xn = x.pow_vartime([R::n() as u64]);
    let xn2 = xn.square();
    let current_u_x = xn2 * x_inv;
    let current_v_x = xn2;
    let xn4 = xn2.square();
    let current_w_x = xn4 * x_inv;

    let mut collector = Collector::<F, R> {
        result: unstructured::Polynomial::new(),
        multiplication_constraints,
        linear_constraints,
        x,
        x_inv,
        current_u_x,
        current_v_x,
        current_w_x,
        one: current_w_x,
        available_b: None,
        _marker: core::marker::PhantomData,
    };
    let (key_wire, _, one_wire) = collector.mul(|| unreachable!())?;

    // Enforce linear constraint key_wire = key to randomize non-trivial
    // evaluations of this circuit polynomial.
    collector.enforce_zero(|lc| {
        lc.add(&key_wire)
            .add_term(&one_wire, Coeff::NegativeArbitrary(key))
    })?;

    let mut outputs = vec![];
    let (io, _) = circuit.witness(&mut collector, Empty)?;
    io.write(&mut collector, &mut outputs)?;
    for output in outputs {
        collector.enforce_zero(|lc| lc.add(output.wire()))?;
    }
    collector.enforce_zero(|lc| lc.add(&one_wire))?;

    collector.result[0..collector.linear_constraints].reverse();
    assert_eq!(collector.result[0], collector.one);

    Ok(collector.result)
}

Also affected

The eval functions from sxy.rs and sx.rs are subject to the same issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions