Skip to content
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
88 changes: 44 additions & 44 deletions noir_stdlib/src/field/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,9 @@ impl Field {
BIT_SIZE < modulus_num_bits() as u32,
"BIT_SIZE must be less than modulus_num_bits",
);
self.__assert_max_bit_size(BIT_SIZE);
__assert_max_bit_size(self, BIT_SIZE);
}

#[builtin(apply_range_constraint)]
fn __assert_max_bit_size(self, bit_size: u32) {}

/// Decomposes `self` into its little endian bit decomposition as a `[u1; N]` array.
/// This slice will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_le_bits)]
fn _to_le_bits<let N: u32>(self: Self) -> [u1; N] {}

/// Decomposes `self` into its big endian bit decomposition as a `[u1; N]` array.
/// This array will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_be_bits)]
fn _to_be_bits<let N: u32>(self: Self) -> [u1; N] {}

/// Decomposes `self` into its little endian bit decomposition as a `[u1; N]` array.
/// This slice will be zero padded should not all bits be necessary to represent `self`.
///
Expand All @@ -60,7 +29,7 @@ impl Field {
// docs:start:to_le_bits
pub fn to_le_bits<let N: u32>(self: Self) -> [u1; N] {
// docs:end:to_le_bits
let bits = self._to_le_bits();
let bits = __to_le_bits(self);

if !is_unconstrained() {
// Ensure that the byte decomposition does not overflow the modulus
Expand Down Expand Up @@ -92,7 +61,7 @@ impl Field {
// docs:start:to_be_bits
pub fn to_be_bits<let N: u32>(self: Self) -> [u1; N] {
// docs:end:to_be_bits
let bits = self._to_be_bits();
let bits = __to_be_bits(self);

if !is_unconstrained() {
// Ensure that the decomposition does not overflow the modulus
Expand Down Expand Up @@ -193,7 +162,7 @@ impl Field {
static_assert(radix <= 256, "radix must be less than or equal to 256");
static_assert(radix & (radix - 1) == 0, "radix must be a power of 2");
}
self.__to_le_radix(radix)
__to_le_radix(self, radix)
}

fn to_be_radix<let N: u32>(self: Self, radix: u32) -> [u8; N] {
Expand All @@ -203,17 +172,9 @@ impl Field {
static_assert(radix <= 256, "radix must be less than or equal to 256");
static_assert(radix & (radix - 1) == 0, "radix must be a power of 2");
}
self.__to_be_radix(radix)
__to_be_radix(self, radix)
}

// `_radix` must be less than 256
#[builtin(to_le_radix)]
fn __to_le_radix<let N: u32>(self, radix: u32) -> [u8; N] {}

// `_radix` must be less than 256
#[builtin(to_be_radix)]
fn __to_be_radix<let N: u32>(self, radix: u32) -> [u8; N] {}

// Returns self to the power of the given exponent value.
// Caution: we assume the exponent fits into 32 bits
// using a bigger bit size impacts negatively the performance and should be done only if the exponent does not fit in 32 bits
Expand Down Expand Up @@ -272,6 +233,45 @@ impl Field {
}
}

#[builtin(apply_range_constraint)]
fn __assert_max_bit_size(value: Field, bit_size: u32) {}

// `_radix` must be less than 256
#[builtin(to_le_radix)]
fn __to_le_radix<let N: u32>(value: Field, radix: u32) -> [u8; N] {}

// `_radix` must be less than 256
#[builtin(to_be_radix)]
fn __to_be_radix<let N: u32>(value: Field, radix: u32) -> [u8; N] {}

/// Decomposes `self` into its little endian bit decomposition as a `[u1; N]` array.
/// This slice will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_le_bits)]
fn __to_le_bits<let N: u32>(value: Field) -> [u1; N] {}

/// Decomposes `self` into its big endian bit decomposition as a `[u1; N]` array.
/// This array will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_be_bits)]
fn __to_be_bits<let N: u32>(value: Field) -> [u1; N] {}

#[builtin(modulus_num_bits)]
pub comptime fn modulus_num_bits() -> u64 {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: failed to solve blackbox function: range, reason: value exceeds range check bounds
┌─ std/field/mod.nr:17:9
17 │ self.__assert_max_bit_size(BIT_SIZE);
│ ------------------------------------
17 │ __assert_max_bit_size(self, BIT_SIZE);
│ -------------------------------------

Aborting due to 1 previous error
Aborting due to 1 previous error

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading
Loading