Skip to content

Commit

Permalink
fix: Consistent bit size for truncate (#4370)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Work towards #4369

## Summary\*

Uses consistent bit sizes for truncate and starts a refactor where we
start to track bit sizes for values in brillig IR

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
sirasistant authored Feb 14, 2024
1 parent e0ad0b2 commit dcd7a1e
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 252 deletions.
102 changes: 51 additions & 51 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::Keccak256 => {
if let (
[message, BrilligVariable::Simple(array_size)],
[message, BrilligVariable::SingleAddr(array_size)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
let mut message_vector = convert_array_or_vector(brillig_context, message, bb_func);
message_vector.size = *array_size;
message_vector.size = array_size.address;

brillig_context.black_box_op_instruction(BlackBoxOp::Keccak256 {
message: message_vector.to_heap_vector(),
Expand All @@ -88,7 +88,7 @@ pub(crate) fn convert_black_box_call(
BlackBoxFunc::EcdsaSecp256k1 => {
if let (
[BrilligVariable::BrilligArray(public_key_x), BrilligVariable::BrilligArray(public_key_y), BrilligVariable::BrilligArray(signature), message],
[BrilligVariable::Simple(result_register)],
[BrilligVariable::SingleAddr(result_register)],
) = (function_arguments, function_results)
{
let message_hash_vector =
Expand All @@ -98,7 +98,7 @@ pub(crate) fn convert_black_box_call(
public_key_x: public_key_x.to_heap_array(),
public_key_y: public_key_y.to_heap_array(),
signature: signature.to_heap_array(),
result: *result_register,
result: result_register.address,
});
} else {
unreachable!(
Expand All @@ -109,7 +109,7 @@ pub(crate) fn convert_black_box_call(
BlackBoxFunc::EcdsaSecp256r1 => {
if let (
[BrilligVariable::BrilligArray(public_key_x), BrilligVariable::BrilligArray(public_key_y), BrilligVariable::BrilligArray(signature), message],
[BrilligVariable::Simple(result_register)],
[BrilligVariable::SingleAddr(result_register)],
) = (function_arguments, function_results)
{
let message_hash_vector =
Expand All @@ -119,7 +119,7 @@ pub(crate) fn convert_black_box_call(
public_key_x: public_key_x.to_heap_array(),
public_key_y: public_key_y.to_heap_array(),
signature: signature.to_heap_array(),
result: *result_register,
result: result_register.address,
});
} else {
unreachable!(
Expand All @@ -130,14 +130,14 @@ pub(crate) fn convert_black_box_call(

BlackBoxFunc::PedersenCommitment => {
if let (
[message, BrilligVariable::Simple(domain_separator)],
[message, BrilligVariable::SingleAddr(domain_separator)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
let message_vector = convert_array_or_vector(brillig_context, message, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::PedersenCommitment {
inputs: message_vector.to_heap_vector(),
domain_separator: *domain_separator,
domain_separator: domain_separator.address,
output: result_array.to_heap_array(),
});
} else {
Expand All @@ -146,48 +146,48 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::PedersenHash => {
if let (
[message, BrilligVariable::Simple(domain_separator)],
[BrilligVariable::Simple(result)],
[message, BrilligVariable::SingleAddr(domain_separator)],
[BrilligVariable::SingleAddr(result)],
) = (function_arguments, function_results)
{
let message_vector = convert_array_or_vector(brillig_context, message, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::PedersenHash {
inputs: message_vector.to_heap_vector(),
domain_separator: *domain_separator,
output: *result,
domain_separator: domain_separator.address,
output: result.address,
});
} else {
unreachable!("ICE: Pedersen hash expects one array argument, a register for the domain separator, and one register result")
}
}
BlackBoxFunc::SchnorrVerify => {
if let (
[BrilligVariable::Simple(public_key_x), BrilligVariable::Simple(public_key_y), BrilligVariable::BrilligArray(signature), message],
[BrilligVariable::Simple(result_register)],
[BrilligVariable::SingleAddr(public_key_x), BrilligVariable::SingleAddr(public_key_y), BrilligVariable::BrilligArray(signature), message],
[BrilligVariable::SingleAddr(result_register)],
) = (function_arguments, function_results)
{
let message_hash = convert_array_or_vector(brillig_context, message, bb_func);
let signature = brillig_context.array_to_vector(signature);
brillig_context.black_box_op_instruction(BlackBoxOp::SchnorrVerify {
public_key_x: *public_key_x,
public_key_y: *public_key_y,
public_key_x: public_key_x.address,
public_key_y: public_key_y.address,
message: message_hash.to_heap_vector(),
signature: signature.to_heap_vector(),
result: *result_register,
result: result_register.address,
});
} else {
unreachable!("ICE: Schnorr verify expects two registers for the public key, an array for signature, an array for the message hash and one result register")
}
}
BlackBoxFunc::FixedBaseScalarMul => {
if let (
[BrilligVariable::Simple(low), BrilligVariable::Simple(high)],
[BrilligVariable::SingleAddr(low), BrilligVariable::SingleAddr(high)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::FixedBaseScalarMul {
low: *low,
high: *high,
low: low.address,
high: high.address,
result: result_array.to_heap_array(),
});
} else {
Expand All @@ -198,15 +198,15 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::EmbeddedCurveAdd => {
if let (
[BrilligVariable::Simple(input1_x), BrilligVariable::Simple(input1_y), BrilligVariable::Simple(input2_x), BrilligVariable::Simple(input2_y)],
[BrilligVariable::SingleAddr(input1_x), BrilligVariable::SingleAddr(input1_y), BrilligVariable::SingleAddr(input2_x), BrilligVariable::SingleAddr(input2_y)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::EmbeddedCurveAdd {
input1_x: *input1_x,
input1_y: *input1_y,
input2_x: *input2_x,
input2_y: *input2_y,
input1_x: input1_x.address,
input1_y: input1_y.address,
input2_x: input2_x.address,
input2_y: input2_y.address,
result: result_array.to_heap_array(),
});
} else {
Expand All @@ -229,14 +229,14 @@ pub(crate) fn convert_black_box_call(
),
BlackBoxFunc::BigIntAdd => {
if let (
[BrilligVariable::Simple(lhs), BrilligVariable::Simple(rhs)],
[BrilligVariable::Simple(output)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(rhs)],
[BrilligVariable::SingleAddr(output)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntAdd {
lhs: *lhs,
rhs: *rhs,
output: *output,
lhs: lhs.address,
rhs: rhs.address,
output: output.address,
});
} else {
unreachable!(
Expand All @@ -246,14 +246,14 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::BigIntSub => {
if let (
[BrilligVariable::Simple(lhs), BrilligVariable::Simple(rhs)],
[BrilligVariable::Simple(output)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(rhs)],
[BrilligVariable::SingleAddr(output)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntSub {
lhs: *lhs,
rhs: *rhs,
output: *output,
lhs: lhs.address,
rhs: rhs.address,
output: output.address,
});
} else {
unreachable!(
Expand All @@ -263,14 +263,14 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::BigIntMul => {
if let (
[BrilligVariable::Simple(lhs), BrilligVariable::Simple(rhs)],
[BrilligVariable::Simple(output)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(rhs)],
[BrilligVariable::SingleAddr(output)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntMul {
lhs: *lhs,
rhs: *rhs,
output: *output,
lhs: lhs.address,
rhs: rhs.address,
output: output.address,
});
} else {
unreachable!(
Expand All @@ -280,14 +280,14 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::BigIntDiv => {
if let (
[BrilligVariable::Simple(lhs), BrilligVariable::Simple(rhs)],
[BrilligVariable::Simple(output)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(rhs)],
[BrilligVariable::SingleAddr(output)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntDiv {
lhs: *lhs,
rhs: *rhs,
output: *output,
lhs: lhs.address,
rhs: rhs.address,
output: output.address,
});
} else {
unreachable!(
Expand All @@ -296,15 +296,15 @@ pub(crate) fn convert_black_box_call(
}
}
BlackBoxFunc::BigIntFromLeBytes => {
if let ([inputs, modulus], [BrilligVariable::Simple(output)]) =
if let ([inputs, modulus], [BrilligVariable::SingleAddr(output)]) =
(function_arguments, function_results)
{
let inputs_vector = convert_array_or_vector(brillig_context, inputs, bb_func);
let modulus_vector = convert_array_or_vector(brillig_context, modulus, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntFromLeBytes {
inputs: inputs_vector.to_heap_vector(),
modulus: modulus_vector.to_heap_vector(),
output: *output,
output: output.address,
});
} else {
unreachable!(
Expand All @@ -314,12 +314,12 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::BigIntToLeBytes => {
if let (
[BrilligVariable::Simple(input)],
[BrilligVariable::SingleAddr(input)],
[BrilligVariable::BrilligVector(result_vector)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntToLeBytes {
input: *input,
input: input.address,
output: result_vector.to_heap_vector(),
});
} else {
Expand All @@ -330,15 +330,15 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::Poseidon2Permutation => {
if let (
[message, BrilligVariable::Simple(state_len)],
[message, BrilligVariable::SingleAddr(state_len)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
let message_vector = convert_array_or_vector(brillig_context, message, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::Poseidon2Permutation {
message: message_vector.to_heap_vector(),
output: result_array.to_heap_array(),
len: *state_len,
len: state_len.address,
});
} else {
unreachable!("ICE: Poseidon2Permutation expects one array argument, a length and one array result")
Expand Down
Loading

0 comments on commit dcd7a1e

Please sign in to comment.