Skip to content

Commit

Permalink
feat: axiom xor (#51)
Browse files Browse the repository at this point in the history
* add: axiom xor constraint

* fix readme
  • Loading branch information
RajeshRk18 authored Jan 8, 2024
1 parent fe08987 commit b6d2c36
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ nargo codegen-verifier
| arithmetic gates | ✔️ | ✔️ |
| range proofs | ✔️ | ✔️ |
| and gates | ✔️ | ✔️ |
| xor | | |
| xor | | ✔️ |
| sha256 | | |
| blake2s | | |
| schnorr_verify | | |
Expand Down
10 changes: 7 additions & 3 deletions crates/noir_halo2_backend_axiom/src/circuit_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ impl Halo2PlonkCircuit<Fr> for NoirHalo2Translator<Fr> {
&config,
&mut witness_assignments,
),
BlackBoxFuncCall::XOR { .. } => {
panic!("xor has not yet been implemented")
}
BlackBoxFuncCall::XOR { .. } => self.add_xor_constrain(
lhs.witness,
rhs.witness,
*output,
&config,
&mut witness_assignments,
),
_ => unreachable!("expected either an AND or XOR opcode"),
}
}
Expand Down
43 changes: 43 additions & 0 deletions crates/noir_halo2_backend_axiom/src/constrains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,49 @@ impl NoirHalo2Translator<Fr> {
config.gate_chip.is_equal(&mut ctx, *output_v, and_out);
}

pub(crate) fn add_xor_constrain(
&self,
lhs: Witness,
rhs: Witness,
output: Witness,
config: &PlonkConfig,
witness_assignments: &mut AssignedMap<Fr>,
) {
let mut ctx = Context::<Fr>::new(false, 0);

// assign lhs, rhs, output or get existing assignnments
let lhs_v = &witness_assignments.get_or_assign(
&mut ctx,
&lhs,
noir_field_to_halo2_field(
*self.witness_values.get(&lhs).unwrap_or(&FieldElement::zero()),
),
);
let rhs_v = &witness_assignments.get_or_assign(
&mut ctx,
&rhs,
noir_field_to_halo2_field(
*self.witness_values.get(&rhs).unwrap_or(&FieldElement::zero()),
),
);
let output_v = &witness_assignments.get_or_assign(
&mut ctx,
&output,
noir_field_to_halo2_field(
*self.witness_values.get(&output).unwrap_or(&FieldElement::zero()),
),
);

// lhs + rhs - 2 * (lhs & rhs)
let two_val = QuantumCell::Constant(Fr::from(2));
let and_res = config.gate_chip.and(&mut ctx, *lhs_v, *rhs_v);
let and_res_with_two = config.gate_chip.mul(&mut ctx, and_res, two_val);
let add_res = config.gate_chip.add(&mut ctx, *lhs_v, *rhs_v);
let final_res = config.gate_chip.sub(&mut ctx, add_res, and_res_with_two);

config.gate_chip.is_equal(&mut ctx, final_res, *output_v);
}

pub(crate) fn add_ecdsa_secp256k1_constrain(
&self,
hashed_message: Vec<Witness>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ fn main(x : Field, y : Field) {
let x_as_u11 = x as u11;
let y_as_u11 = y as u11;
assert((x_as_u11 & y_as_u11) == x_as_u11);
}

}

0 comments on commit b6d2c36

Please sign in to comment.