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 FieldElement::from<usize> implementation #3647

Merged
merged 2 commits into from
Nov 30, 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
6 changes: 6 additions & 0 deletions acvm-repo/acir_field/src/generic_ark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// Check if the negative version is smaller to represent
//
let minus_number = BigUint::from_bytes_be(&(self.neg()).to_be_bytes());
let (smaller_repr, is_negative) =

Check warning on line 22 in acvm-repo/acir_field/src/generic_ark.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (repr)
if minus_number.to_string().len() < number.to_string().len() {
(minus_number, true)
} else {
Expand All @@ -30,13 +30,13 @@
}

// Number of bits needed to represent the smaller representation
let num_bits = smaller_repr.bits();

Check warning on line 33 in acvm-repo/acir_field/src/generic_ark.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (repr)

// Check if the number represents a power of 2
if smaller_repr.count_ones() == 1 {

Check warning on line 36 in acvm-repo/acir_field/src/generic_ark.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (repr)
let mut bit_index = 0;
for i in 0..num_bits {
if smaller_repr.bit(i) {

Check warning on line 39 in acvm-repo/acir_field/src/generic_ark.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (repr)
bit_index = i;
break;
}
Expand All @@ -59,7 +59,7 @@
let mul_sign = "×";
for power in [64, 32, 16, 8, 4] {
let power_of_two = BigUint::from(2_u128).pow(power);
if &smaller_repr % &power_of_two == BigUint::zero() {

Check warning on line 62 in acvm-repo/acir_field/src/generic_ark.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (repr)
return write!(
f,
"2{}{}{}",
Expand Down Expand Up @@ -143,6 +143,12 @@
}
}

impl<F: PrimeField> From<usize> for FieldElement<F> {
fn from(a: usize) -> FieldElement<F> {
FieldElement::from(a as u128)
}
}

impl<F: PrimeField> From<bool> for FieldElement<F> {
fn from(boolean: bool) -> FieldElement<F> {
if boolean {
Expand Down
25 changes: 11 additions & 14 deletions compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl AcirContext {
}

/// Adds a constant to the context and assigns a Variable to represent it
pub(crate) fn add_constant(&mut self, constant: FieldElement) -> AcirVar {
let constant_data = AcirVarData::Const(constant);
pub(crate) fn add_constant(&mut self, constant: impl Into<FieldElement>) -> AcirVar {
let constant_data = AcirVarData::Const(constant.into());
self.add_data(constant_data)
}

Expand Down Expand Up @@ -353,7 +353,7 @@ impl AcirContext {

// Check to see if equality can be determined at compile-time.
if diff_expr.is_const() {
return Ok(self.add_constant(diff_expr.is_zero().into()));
return Ok(self.add_constant(diff_expr.is_zero()));
}

let is_equal_witness = self.acir_ir.is_equal(&lhs_expr, &rhs_expr);
Expand Down Expand Up @@ -410,7 +410,7 @@ impl AcirContext {
// max - ((max - a) AND (max -b))
// Subtracting from max flips the bits, so this is effectively:
// (NOT a) NAND (NOT b)
let max = self.add_constant(FieldElement::from((1_u128 << bit_size) - 1));
let max = self.add_constant((1_u128 << bit_size) - 1);
let a = self.sub_var(max, lhs)?;
let b = self.sub_var(max, rhs)?;
let inputs = vec![AcirValue::Var(a, typ.clone()), AcirValue::Var(b, typ)];
Expand Down Expand Up @@ -553,7 +553,7 @@ impl AcirContext {
pub(crate) fn not_var(&mut self, x: AcirVar, typ: AcirType) -> Result<AcirVar, RuntimeError> {
let bit_size = typ.bit_size();
// Subtracting from max flips the bits
let max = self.add_constant(FieldElement::from((1_u128 << bit_size) - 1));
let max = self.add_constant((1_u128 << bit_size) - 1);
self.sub_var(max, x)
}

Expand All @@ -580,8 +580,8 @@ impl AcirContext {
let quotient = lhs_const.to_u128() / rhs_const.to_u128();
let remainder = lhs_const.to_u128() - quotient * rhs_const.to_u128();

let quotient_var = self.add_constant(FieldElement::from(quotient));
let remainder_var = self.add_constant(FieldElement::from(remainder));
let quotient_var = self.add_constant(quotient);
let remainder_var = self.add_constant(remainder);
return Ok((quotient_var, remainder_var));
}

Expand Down Expand Up @@ -778,7 +778,7 @@ impl AcirContext {
// witness = lhs_offset + r
assert!(bits + r_bit_size < FieldElement::max_num_bits()); //we need to ensure lhs_offset + r does not overflow

let r_var = self.add_constant(r.into());
let r_var = self.add_constant(r);
let aor = self.add_var(lhs_offset, r_var)?;
// lhs_offset<=rhs_offset <=> lhs_offset + r < rhs_offset + r = 2^bit_size <=> witness < 2^bit_size
self.range_constrain_var(aor, &NumericType::Unsigned { bit_size }, None)?;
Expand Down Expand Up @@ -1150,10 +1150,7 @@ impl AcirContext {
// `Intrinsic::ToRadix` returns slices which are represented
// by tuples with the structure (length, slice contents)
Ok(vec![
AcirValue::Var(
self.add_constant(FieldElement::from(limb_vars.len() as u128)),
AcirType::field(),
),
AcirValue::Var(self.add_constant(limb_vars.len()), AcirType::field()),
AcirValue::Array(limb_vars.into()),
])
}
Expand All @@ -1166,7 +1163,7 @@ impl AcirContext {
limb_count_var: AcirVar,
result_element_type: AcirType,
) -> Result<Vec<AcirValue>, RuntimeError> {
let two_var = self.add_constant(FieldElement::from(2_u128));
let two_var = self.add_constant(2_u128);
self.radix_decompose(endian, input_var, two_var, limb_count_var, result_element_type)
}

Expand Down Expand Up @@ -1274,7 +1271,7 @@ impl AcirContext {
AcirValue::DynamicArray(AcirDynamicArray { block_id, len, .. }) => {
for i in 0..len {
// We generate witnesses corresponding to the array values
let index_var = self.add_constant(FieldElement::from(i as u128));
let index_var = self.add_constant(i);

let value_read_var = self.read_from_memory(block_id, &index_var)?;
let value_read = AcirValue::Var(value_read_var, AcirType::field());
Expand Down
17 changes: 8 additions & 9 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,7 @@

let mut read_dynamic_array_index =
|block_id: BlockId, array_index: usize| -> Result<AcirVar, InternalError> {
let index_var =
self.acir_context.add_constant(FieldElement::from(array_index as u128));
let index_var = self.acir_context.add_constant(array_index);

self.acir_context.read_from_memory(block_id, &index_var)
};
Expand Down Expand Up @@ -852,7 +851,7 @@
);

let values = try_vecmap(0..*len, |i| {
let index_var = self.acir_context.add_constant(FieldElement::from(i as u128));
let index_var = self.acir_context.add_constant(i);

let read = self.acir_context.read_from_memory(*block_id, &index_var)?;
Ok::<AcirValue, RuntimeError>(AcirValue::Var(read, AcirType::field()))
Expand Down Expand Up @@ -897,7 +896,7 @@

// The first max size is going to be the length of the parent slice
// As we are fetching from the parent slice we just want its internal
// slize sizes.

Check warning on line 899 in compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (slize)
let slice_sizes = slice_sizes[1..].to_vec();

let value = self.array_get_value(&res_typ, block_id, &mut var_index, &slice_sizes)?;
Expand Down Expand Up @@ -1066,7 +1065,7 @@
AcirValue::Var(store_var, _) => {
// Write the new value into the new array at the specified index
self.acir_context.write_to_memory(block_id, var_index, &store_var)?;
// Incremement the var_index in case of a nested array

Check warning on line 1068 in compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Incremement)
*var_index = self.acir_context.add_var(*var_index, one)?;
}
AcirValue::Array(values) => {
Expand All @@ -1076,7 +1075,7 @@
}
AcirValue::DynamicArray(AcirDynamicArray { block_id: inner_block_id, len, .. }) => {
let values = try_vecmap(0..len, |i| {
let index_var = self.acir_context.add_constant(FieldElement::from(i as u128));
let index_var = self.acir_context.add_constant(i);

let read = self.acir_context.read_from_memory(inner_block_id, &index_var)?;
Ok::<AcirValue, RuntimeError>(AcirValue::Var(read, AcirType::field()))
Expand Down Expand Up @@ -1235,7 +1234,7 @@

// The final array should will the flattened index at each outer array index
let init_values = vecmap(flat_elem_type_sizes, |type_size| {
let var = self.acir_context.add_constant(FieldElement::from(type_size as u128));
let var = self.acir_context.add_constant(type_size);
AcirValue::Var(var, AcirType::field())
});
let element_type_sizes_len = init_values.len();
Expand Down Expand Up @@ -1292,7 +1291,7 @@
array_len: usize,
) -> Result<(), RuntimeError> {
let init_values = try_vecmap(0..array_len, |i| {
let index_var = self.acir_context.add_constant(FieldElement::from(i as u128));
let index_var = self.acir_context.add_constant(i);

let read = self.acir_context.read_from_memory(source, &index_var)?;
Ok::<AcirValue, RuntimeError>(AcirValue::Var(read, AcirType::field()))
Expand Down Expand Up @@ -1760,8 +1759,8 @@
Intrinsic::ArrayLen => {
let len = match self.convert_value(arguments[0], dfg) {
AcirValue::Var(_, _) => unreachable!("Non-array passed to array.len() method"),
AcirValue::Array(values) => (values.len() as u128).into(),
AcirValue::DynamicArray(array) => (array.len as u128).into(),
AcirValue::Array(values) => values.len(),
AcirValue::DynamicArray(array) => array.len,
};
Ok(vec![AcirValue::Var(self.acir_context.add_constant(len), AcirType::field())])
}
Expand Down Expand Up @@ -1978,7 +1977,7 @@
AcirValue::DynamicArray(AcirDynamicArray { block_id, len, .. }) => {
for i in 0..len {
// We generate witnesses corresponding to the array values
let index_var = self.acir_context.add_constant(FieldElement::from(i as u128));
let index_var = self.acir_context.add_constant(i);

let value_read_var =
self.acir_context.read_from_memory(block_id, &index_var)?;
Expand Down
Loading