Skip to content

Commit

Permalink
fix: keep the correct type for bitshift (noir-lang#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic authored Sep 18, 2023
1 parent 13cc23b commit 04fc2ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ impl<'a> FunctionContext<'a> {
fn insert_shift_left(&mut self, lhs: ValueId, rhs: ValueId) -> ValueId {
let base = self.builder.field_constant(FieldElement::from(2_u128));
let pow = self.pow(base, rhs);
let typ = self.builder.current_function.dfg.type_of_value(lhs);
let pow = self.builder.insert_cast(pow, typ);
self.builder.insert_binary(lhs, BinaryOp::Mul, pow)
}

Expand Down
30 changes: 30 additions & 0 deletions tooling/nargo_cli/tests/execution_success/regression/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ fn enc<N>(value: [u8; N], value_length: Field) -> ([u8; 32], Field)
}
}

fn bitshift_literal_0() -> u64 {
let mut bits: u64 = 0;
bits |= 1 << 0;

bits
}
fn bitshift_literal_4() -> u64 {
let mut bits: u64 = 0;
bits |= 1 << 4;

bits
}
fn bitshift_variable(idx: u64) -> u64 {
let mut bits: u64 = 0;
bits |= 1 << idx;

bits
}


fn main(x: [u8; 5], z: Field)
{
//Issue 1144
Expand All @@ -87,4 +107,14 @@ fn main(x: [u8; 5], z: Field)
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]);
assert(enc_val1.1 == 21);

// Issue 2399
let result_0 = bitshift_literal_0();
assert(result_0 == 1);
let result_4 = bitshift_literal_4();
assert(result_4 == 16);
let result_0 = bitshift_variable(0);
assert(result_0 == 1);
let result_4 = bitshift_variable(4);
assert(result_4 == 16);

}

0 comments on commit 04fc2ea

Please sign in to comment.