-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
341718b
commit 45c8b85
Showing
6 changed files
with
267 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod native; | ||
mod u120_add; | ||
mod u120_mul; | ||
mod u120_comp; | ||
mod u2048_add; | ||
mod u2048_comp; | ||
mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::mem::transmute; | ||
use expander_compiler::frontend::*; | ||
use expander_compiler::{declare_circuit, frontend::{BN254Config, Define, Variable, API}}; | ||
use halo2curves::bn256::Fr; | ||
|
||
use crate::constants::MASK120; | ||
use crate::u120::is_less_than_u120; | ||
|
||
declare_circuit!(LessThanCircuit { | ||
x: Variable, | ||
y: Variable, | ||
result: Variable, | ||
}); | ||
|
||
impl Define<BN254Config> for LessThanCircuit<Variable> { | ||
fn define(&self, builder: &mut API<BN254Config>) { | ||
let res = is_less_than_u120(&self.x, &self.y, builder); | ||
builder.assert_is_equal(res, self.result); | ||
} | ||
} | ||
|
||
impl LessThanCircuit<Fr> { | ||
fn create_circuit(x: [u64; 2], y: [u64; 2], result: [u64; 2]) -> LessThanCircuit<Fr> { | ||
Self { | ||
x: Fr::from_raw([x[0], x[1], 0, 0]), | ||
y: Fr::from_raw([y[0], y[1], 0, 0]), | ||
result: Fr::from_raw([result[0], result[1], 0, 0]), | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_u120_less_than() { | ||
let compile_result = compile(&LessThanCircuit::default()).unwrap(); | ||
|
||
{ | ||
// Test case: Simple less than | ||
let x = [5, 0]; | ||
let y = [10, 0]; | ||
let result = [1, 0]; // true: 5 < 10 | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![true]); | ||
} | ||
{ | ||
// Test case: Equal values | ||
let x = [42, 0]; | ||
let y = [42, 0]; | ||
let result = [0, 0]; // false: 42 = 42 | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![true]); | ||
} | ||
{ | ||
// Test case: Greater than | ||
let x = [100, 0]; | ||
let y = [50, 0]; | ||
let result = [0, 0]; // false: 100 > 50 | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![true]); | ||
} | ||
{ | ||
// Test case: Using second limb | ||
let x = [0, 1]; // 2^64 | ||
let y = [u64::MAX, 0]; | ||
let result = [0, 0]; // false: 2^64 > u64::MAX | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![true]); | ||
} | ||
{ | ||
// Test case: Large numbers near 120-bit limit | ||
let x = unsafe { transmute(MASK120 - 1) }; // 2^120 - 2 | ||
let y = unsafe { transmute(MASK120) }; // 2^120 - 1 | ||
let result = [1, 0]; // true: (2^120 - 2) < (2^120 - 1) | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![true]); | ||
} | ||
{ | ||
// Test case: Equal large numbers | ||
let x = unsafe { transmute(MASK120) }; // 2^120 - 1 | ||
let y = unsafe { transmute(MASK120) }; // 2^120 - 1 | ||
let result = [0, 0]; // false: equal values | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![true]); | ||
} | ||
{ | ||
// Test case: Negative case (incorrect result) | ||
let x = [5, 0]; | ||
let y = [10, 0]; | ||
let result = [0, 0]; // incorrect: should be 1 since 5 < 10 | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![false]); | ||
} | ||
{ | ||
// Test case: Negative case (incorrect result) | ||
let x = [5, 0]; | ||
let y = [5, 0]; | ||
let result = [1, 0]; // incorrect: should be 0 since 5 = 5 | ||
|
||
let assignment = LessThanCircuit::<Fr>::create_circuit(x, y, result); | ||
let witness = compile_result.witness_solver.solve_witness(&assignment).unwrap(); | ||
let output = compile_result.layered_circuit.run(&witness); | ||
assert_eq!(output, vec![false]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.