Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add to_radix and to_bits support to brillig gen #2012

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ edition = "2021"
rust-version = "1.66"

[workspace.dependencies]
acvm = "0.19.1"
acvm = "0.20.0"
arena = { path = "crates/arena" }
fm = { path = "crates/fm" }
iter-extended = { path = "crates/iter-extended" }
Expand Down Expand Up @@ -57,4 +57,4 @@ wasm-bindgen-test = "0.3.33"
base64 = "0.21.2"

[patch.crates-io]
async-lsp = { git = "https://github.com/oxalica/async-lsp", rev = "09dbcc11046f7a188a80137f8d36484d86c78c78" }
async-lsp = { git = "https://github.com/oxalica/async-lsp", rev = "09dbcc11046f7a188a80137f8d36484d86c78c78" }
2 changes: 1 addition & 1 deletion crates/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ color-eyre = "0.6.2"
tokio = { version = "1.0", features = ["io-std"] }

# Backends
acvm-backend-barretenberg = { version = "0.9.0", default-features = false }
acvm-backend-barretenberg = { version = "0.9.1", default-features = false }

[dev-dependencies]
tempdir = "0.3.7"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "2040124"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use dep::std;

unconstrained fn main(x : Field) -> pub [u8; 31] {
// The result of this byte array will be big-endian
let byte_array = x.to_be_bytes(31);
let mut bytes = [0; 31];
for i in 0..31 {
bytes[i] = byte_array[i];
}
assert(bytes[30] == 60);
assert(bytes[29] == 33);
assert(bytes[28] == 31);
bytes
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.7.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use dep::std;

unconstrained fn main() {
let field = 1000;
let be_bits = field.to_be_bits(16);
let le_bits = field.to_le_bits(16);

for i in 0..16 {
let x = be_bits[i];
let y = le_bits[15-i];
assert(x == y);
}

let x = 3;
let be_bits_x = x.to_be_bits(4);
let le_bits_x = x.to_le_bits(4);

for i in 0..4 {
let be_bit = be_bits_x[i];
let le_bit = le_bits_x[3-i];
assert(be_bit == le_bit);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "2040124"
_y = "0x2000000000000000000000000000000000000000000000000000000000000000"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use dep::std;

unconstrained fn main(x : Field, _y: Field) {
// The result of this byte array will be big-endian
let y: Field = 2040124;
let be_byte_array = y.to_be_bytes(31);
// The result of this byte array will be little-endian
let le_byte_array = x.to_le_bytes(31);

assert(le_byte_array[0] == 60);
assert(le_byte_array[0] == be_byte_array[30]);
assert(le_byte_array[1] == be_byte_array[29]);
assert(le_byte_array[2] == be_byte_array[28]);

let z = 0 - 1;
let p_bytes = std::field::modulus_le_bytes();
let z_bytes = z.to_le_bytes(32);
assert(p_bytes[10] == z_bytes[10]);
assert(p_bytes[0] == z_bytes[0] as u8 + 1 as u8);

let p_bits = std::field::modulus_le_bits();
let z_bits = z.to_le_bits(std::field::modulus_num_bits() as u32);
assert(z_bits[0] == 0);
assert(p_bits[100] == z_bits[100]);

_y.to_le_bits(std::field::modulus_num_bits() as u32);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "2040124"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use dep::std;

unconstrained fn main(x : Field) -> pub [u8; 4] {
// The result of this byte array will be little-endian
let byte_array = x.to_le_bytes(31);
let mut first_four_bytes = [0; 4];
for i in 0..4 {
first_four_bytes[i] = byte_array[i];
}
// Issue #617 fix
// We were incorrectly mapping our output array from bit decomposition functions during acir generation
first_four_bytes[3] = byte_array[31];
first_four_bytes
}
41 changes: 40 additions & 1 deletion crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::brillig::brillig_ir::{
BrilligBinaryOp, BrilligContext, BRILLIG_INTEGER_ARITHMETIC_BIT_SIZE,
};
use crate::ssa_refactor::ir::function::FunctionId;
use crate::ssa_refactor::ir::instruction::Intrinsic;
use crate::ssa_refactor::ir::instruction::{Endian, Intrinsic};
use crate::ssa_refactor::ir::{
basic_block::{BasicBlock, BasicBlockId},
dfg::DataFlowGraph,
Expand Down Expand Up @@ -327,6 +327,45 @@ impl<'block> BrilligBlock<'block> {
arguments,
);
}
Value::Intrinsic(Intrinsic::ToRadix(endianness)) => {
let source = self.convert_ssa_register_value(arguments[0], dfg);
let radix = self.convert_ssa_register_value(arguments[1], dfg);
let limb_count = self.convert_ssa_register_value(arguments[2], dfg);
let target_slice = self.function_context.create_variable(
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
dfg,
);

self.brillig_context.radix_instruction(
source,
self.function_context.extract_heap_vector(target_slice),
radix,
limb_count,
matches!(endianness, Endian::Big),
);
}
Value::Intrinsic(Intrinsic::ToBits(endianness)) => {
let source = self.convert_ssa_register_value(arguments[0], dfg);
let limb_count = self.convert_ssa_register_value(arguments[1], dfg);
let target_slice = self.function_context.create_variable(
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
dfg,
);

let radix = self.brillig_context.make_constant(2_usize.into());

self.brillig_context.radix_instruction(
source,
self.function_context.extract_heap_vector(target_slice),
radix,
limb_count,
matches!(endianness, Endian::Big),
);

self.brillig_context.deallocate_register(radix);
}
_ => {
unreachable!("unsupported function call type {:?}", dfg[*func])
}
Expand Down
Loading