NP-Eng / ligero

Rust implementation of (the non-interactive, non-ZK version of) the Ligero SNARK for arithmetic circuits.
Apache License 2.0
2 stars 1 forks source link

Better use of unchecked methods #9

Open Antonio95 opened 2 months ago

Antonio95 commented 2 months ago

Right now, some ArithmeticCircuit operations have unchecked variants, such as the method add and its counterpart add_unchecked. Unchecked methods simply don't check whether the operands one passes are already in the circuit. For instance, add(a, b) would panic if a and b are not (the indices of nodes) in the circuit.

This is a very cheap check, since it only compares the inputs (which are secretly usizes) to the length of the node vector. However, if one adds many gates (e.g. by calling existing_node.pow(something_big)), it is unnecessary to check that condition for each (multiplication, in this case) gate - hence why the unchecked methods were introduced.

There are two things to polish in relation to this: