Skip to content

Commit 59f92e3

Browse files
fix: regression end to end test (#1965)
* update to acvm 0.19.1 * add 9_conditional * use a smaller circuit * add predicate for division * reduce lhs and rhs constraints * pass predicate to inv_var * [DO NOT MERGE] Test removing unconditional assert * fix clippy * remove Option<Expression> * add `assert_eq` and `maybe_eq_predicate` * sort now takes a predicate * add back full 9_conditional * add regression example * [DO NOT MERGE] make predicate None * predicate the final equation instead of relying on predicate = None * Update crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> * change unwrap to expect * make mul_with_witness crate visible remove bad merge * move test into main --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> Co-authored-by: TomAFrench <tom@tomfren.ch>
1 parent c15f9aa commit 59f92e3

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
authors = [""]
3+
compiler_version = "0.1"
4+
5+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x = [0x3f, 0x1c, 0xb8, 0x99, 0xab]
2+
z = 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
global NIBBLE_LENGTH: comptime Field = 16;
2+
3+
fn compact_decode<N>(input: [u8; N], length: Field) -> ([u4; NIBBLE_LENGTH], Field)
4+
{
5+
assert(2*input.len() as u64 <= NIBBLE_LENGTH as u64);
6+
assert(length as u64 <= input.len() as u64);
7+
8+
let mut nibble = [0 as u4; NIBBLE_LENGTH];
9+
10+
let first_nibble = (input[0] >> 4) as u4;
11+
let parity = first_nibble as u1;
12+
13+
if parity == 1
14+
{
15+
nibble[0] = (input[0] & 0x0f) as u4;
16+
for i in 1..input.len()
17+
{
18+
if i as u64 < length as u64
19+
{
20+
let x = input[i];
21+
nibble[2*i - 1] = (x >> 4) as u4;
22+
nibble[2*i] = (x & 0x0f) as u4;
23+
}
24+
}
25+
}
26+
else
27+
{
28+
for i in 0..2
29+
{
30+
if (i as u64) < length as u64 - 1
31+
{
32+
let x = input[i + 1];
33+
nibble[2*i] = (x >> 4) as u4;
34+
nibble[2*i + 1] = (x & 0x0f) as u4;
35+
}
36+
}
37+
}
38+
39+
let out = (nibble, 2*length + (parity as Field) - 2);
40+
41+
out
42+
}
43+
44+
fn enc<N>(value: [u8; N], value_length: Field) -> ([u8; 32], Field)
45+
{
46+
assert(value.len() as u8 >= value_length as u8);
47+
let mut out_value = [0; 32];
48+
if value_length == 0
49+
{
50+
let out = (out_value, value_length);
51+
out
52+
}
53+
else { if value_length as u8 < 31
54+
{
55+
out_value[0] = 0x80 + value_length as u8;
56+
57+
for i in 1..value.len()
58+
{
59+
out_value[i] = value[i-1];
60+
}
61+
62+
let out = (out_value, value_length + 1);
63+
64+
out
65+
}
66+
else
67+
{
68+
let out = (out_value, 32);
69+
out
70+
}
71+
}
72+
}
73+
74+
fn main(x: [u8; 5], z: Field)
75+
{
76+
//Issue 1144
77+
let (nib, len) = compact_decode(x,z);
78+
assert(len == 5);
79+
assert([nib[0], nib[1], nib[2], nib[3], nib[4]] == [15, 1, 12, 11, 8]);
80+
81+
// Issue 1169
82+
let val1 = [0xb8,0x8f,0x61,0xe6,0xfb,0xda,0x83,0xfb,0xff,0xfa,0xbe,0x36,0x41,0x12,0x13,0x74,0x80,0x39,0x80,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00];
83+
let val1_length = 20;
84+
85+
let enc_val1 = enc(val1,val1_length);
86+
87+
assert(enc_val1.0 == [0x94,0xb8,0x8f,0x61,0xe6,0xfb,0xda,0x83,0xfb,0xff,0xfa,0xbe,0x36,0x41,0x12,0x13,0x74,0x80,0x39,0x80,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]);
88+
assert(enc_val1.1 == 21);
89+
90+
}

crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl GeneratedAcir {
748748
let (q_witness, r_witness) = self.quotient_directive(
749749
comparison_evaluation.clone(),
750750
two_max_bits.into(),
751-
Some(predicate),
751+
Some(predicate.clone()),
752752
q_max_bits,
753753
r_max_bits,
754754
)?;
@@ -773,10 +773,17 @@ impl GeneratedAcir {
773773
// - 2^{max_bits} - k == q * 2^{max_bits} + r
774774
// - This is only the case when q == 0 and r == 2^{max_bits} - k
775775
//
776+
// case: predicate is zero
777+
// The values for q and r will be zero for a honest prover and
778+
// can be garbage for a dishonest prover. The below constraint will
779+
// will be switched off.
776780
let mut expr = Expression::default();
777781
expr.push_addition_term(two_max_bits, q_witness);
778782
expr.push_addition_term(FieldElement::one(), r_witness);
779-
self.push_opcode(AcirOpcode::Arithmetic(&comparison_evaluation - &expr));
783+
784+
let equation = &comparison_evaluation - &expr;
785+
let predicated_equation = self.mul_with_witness(&equation, &predicate);
786+
self.push_opcode(AcirOpcode::Arithmetic(predicated_equation));
780787

781788
Ok(q_witness)
782789
}

crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/sort.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl GeneratedAcir {
7575
/// Returns an expression which represents a*b
7676
/// If one has multiplicative term and the other is of degree one or more,
7777
/// the function creates intermediate variables accordindly
78-
fn mul_with_witness(&mut self, a: &Expression, b: &Expression) -> Expression {
78+
pub(crate) fn mul_with_witness(&mut self, a: &Expression, b: &Expression) -> Expression {
7979
let a_arith;
8080
let a_arith = if !a.mul_terms.is_empty() && !b.is_const() {
8181
let a_witness = self.get_or_create_witness(a);

0 commit comments

Comments
 (0)