ZK-SecreC Documentation

2024.09

Module FastFixedPointVec

Function add_fixed_fv_unchecked

pub fn add_fixed_fv_unchecked [ N : Nat ] ( x : Fixed[N, $post, @prover] $pre @public, xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Adding a scalar to all elements of a fixed-point vector.

Function add_fv

pub fn add_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Adding two fixed-point vectors elementwise, checked, very slow.

Function add_fv_exact

pub fn add_fv_exact [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Adding two fixed-point vectors elementwise, increases the len by 1 to guarantee fitting, very fast.

Function add_fv_unchecked

pub fn add_fv_unchecked [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Adding two fixed-point vectors elementwise, unchecked, very fast.

Function add_prefixed_fv_unchecked

pub fn add_prefixed_fv_unchecked [ N : Nat ] ( x : Fixed[N, $pre, @public] $pre @public, xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Adding a constant to all elements of a fixed-point vector.

Function check_fv

pub fn check_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> () $pre @public
where
  Field[N],
  Vectorization

Checking that each element of a fixed-point vector fits in len bits. The same as checked_fv but without returning the input vector.

Function checked_fv

pub fn checked_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Returning the same fixed-point vector after checking that each element fits in len bits. Can be applied to the result of functions with the suffix _unchecked to make them safe. Sometimes it is possible to make several _unchecked operations in a row before calling the expensive checked_fv, as long as the intermediate results do not overflow the modulus (but they do not have to fit into len bits). For example, three fixed-point vectors can be added together by checked_fv(add_fv_unchecked(xs, add_fv_unchecked(ys, zs))), calling checked_fv only once rather than twice. This is safe if 3*(2^len - 1) < N.

Function decrease_len_fv_unchecked

pub fn decrease_len_fv_unchecked [ N : Nat ] ( xv : FixedVec[N] $pre @public, new_len : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public

Decreasing the len of each element of a fixed-point vector without checking for fitting into the new len bits.

Function div_fixed_fv

pub fn div_fixed_fv [ N : Nat ] ( x : Fixed[N, $post, @prover] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Dividing a scalar by each element of a fixed-point vector.

Function div_fv

pub fn div_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Dividing two fixed-point vectors elementwise.

Function empty_fv

pub fn empty_fv [ N : Nat ] () -> FixedVec[N] $pre @public
where
  Vectorization

An empty fixed-point vector.

Function fv_checked

pub fn fv_checked [ N : Nat ] ( coefs : arr[uint[N] $post @prover] $pre @public, len : uint[18446744073709551616] $pre @public, pplen : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Creating a fixed-point vector, checking for fitting in len bits.

Function fv_unchecked

pub fn fv_unchecked [ N : Nat ] ( coefs : arr[uint[N] $post @prover] $pre @public, len : uint[18446744073709551616] $pre @public, pplen : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public

Creating a fixed-point vector without checking for fitting in len bits.

Function ge_fv_fixed

pub fn ge_fv_fixed [ N : Nat ] ( xv : FixedVec[N] $pre @public, y : Fixed[N, $post, @prover] $pre @public ) -> arr[uint[N] $post @prover] $pre @public
where
  Field[N],
  Vectorization

Comparing each element of the input fixed-point vector xv with a scalar y. Returns a fixed-point vector with the same number of elements as xv, with the ith element being 1 if xv[i] >= y and 0 otherwise.

Function increase_pplen_fv

pub fn increase_pplen_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, new_pplen : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Increasing pplen of each element of a fixed-point vector to a given value and increasing the len to keep len - pplen the same as before.

Function le_fv_fixed

pub fn le_fv_fixed [ N : Nat ] ( xv : FixedVec[N] $pre @public, y : Fixed[N, $post, @prover] $pre @public ) -> arr[uint[N] $post @prover] $pre @public
where
  Field[N],
  Vectorization

Comparing each element of the input fixed-point vector xv with a scalar y. Returns a fixed-point vector with the same number of elements as xv, with the ith element being 1 if xv[i] <= y and 0 otherwise.

Function make_unknown_fv

pub fn make_unknown_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public

Removing values from the wire range corresponding to the fixed-point vector, to make circuit generation faster if the values are no longer needed in $pre.

Function max_fixed_fv

pub fn max_fixed_fv [ N : Nat ] ( x : Fixed[N, $post, @prover] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

The ith element of the output is the maximum of the scalar and the ith element of the input fixed-point vector.

Function max_fv

pub fn max_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Elementwise maximum of two fixed-point vectors.

Function min_fixed_fv

pub fn min_fixed_fv [ N : Nat ] ( x : Fixed[N, $post, @prover] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

The ith element of the output is the minimum of the scalar and the ith element of the input fixed-point vector.

Function min_fv

pub fn min_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Elementwise minimum of two fixed-point vectors.

Function mul_fixed_fv_exact

pub fn mul_fixed_fv_exact [ N : Nat ] ( x : Fixed[N, $post, @prover] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Multiplying all elements of a fixed-point vector by a scalar, no rounding, fast, the len and pplen of the result are the sum of the len and pplen of the arguments, respectively.

Function mul_fixed_uv_unchecked

pub fn mul_fixed_uv_unchecked [ N : Nat ] ( x : Fixed[N, $post, @prover] $pre @public, xs : arr[uint[N] $post @prover] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Multiplying all elements of an integer vector by a fixed-point scalar, unchecked, fast.

Function mul_fv

pub fn mul_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Multiplying two fixed-point vectors elementwise, rounding, slow.

Function mul_fv_exact

pub fn mul_fv_exact [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Multiplying two fixed-point vectors elementwise, no rounding, fast, the len and pplen of the result are the sum of the len and pplen of the arguments, respectively.

Function mul_prefixed_fv_exact

pub fn mul_prefixed_fv_exact [ N : Nat ] ( x : Fixed[N, $pre, @public] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Multiplying all elements of a fixed-point vector by a constant, no rounding, fast, the len and pplen of the result are the sum of the len and pplen of the arguments, respectively.

Function mul_uint_fv_unchecked

pub fn mul_uint_fv_unchecked [ N : Nat ] ( x : uint[N] $post @prover, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Multiplying all elements of a fixed-point vector by an integer scalar, unchecked, fast.

Function mul_uv_fv_unchecked

pub fn mul_uv_fv_unchecked [ N : Nat ] ( xs : arr[uint[N] $post @prover] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Vectorization

Multiplying an integer vector and a fixed-point vector elementwise, unchecked, fast.

Function negate_fv

pub fn negate_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Negating (multiplying by -1) the elements of a fixed-point vector.

Function round_down_fv

pub fn round_down_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, new_len : uint[18446744073709551616] $pre @public, new_pplen : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Decreasing the pplen of each element of a fixed-point vector by rounding down (towards minus infinity) some of the final bits.

Function round_down_fv_to_half

pub fn round_down_fv_to_half [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Rounding down half of the bits after the point of each element of a fixed-point vector, can be used after mul_fv_exact to get back to the original len and pplen.

Function round_down_fv_to_int

pub fn round_down_fv_to_int [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> arr[uint[N] $post @prover] $pre @public
where
  Field[N],
  Vectorization

Rounding each element of a fixed-point vector down to an integer (taking the floor of each element).

Function round_down_fv_unchecked

pub fn round_down_fv_unchecked [ N : Nat ] ( xv : FixedVec[N] $pre @public, new_len : uint[18446744073709551616] $pre @public, new_pplen : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Unchecked version of round_down_fv, about twice as fast.

Function scalar_prod_uv_fv_unchecked

pub fn scalar_prod_uv_fv_unchecked [ N : Nat ] ( xs : arr[uint[N] $post @prover] $pre @public, yv : FixedVec[N] $pre @public ) -> Fixed[N, $post, @prover] $pre @public
where
  Vectorization

The scalar product (dot product) of an integer vector and a fixed-point vector, unchecked.

Function slice_fv

pub fn slice_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, i1 : uint[18446744073709551616] $pre @public, i2 : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public

A slice of another fixed-point vector, including elements with indices from i1 (inclusive) to i2 (exclusive).

Function sqrt_fv

pub fn sqrt_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Taking a square root of all elements of a fixed-point vector.

Function sub_fv

pub fn sub_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Subtracting two fixed-point vectors elementwise, checked, very slow.

Function sub_fv_unchecked

pub fn sub_fv_unchecked [ N : Nat ] ( xv : FixedVec[N] $pre @public, yv : FixedVec[N] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Subtracting two fixed-point vectors elementwise, unchecked, very fast.

Function sum_fv_unchecked

pub fn sum_fv_unchecked [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> Fixed[N, $post, @prover] $pre @public
where
  Vectorization

The sum of the elements of a fixed-point vector, unchecked.

Function thaw_fv

pub fn thaw_fv [ N : Nat ] ( xv : FixedVec[N] $pre @public ) -> list[Fixed[N, $post, @prover] $pre @public] $pre @public

Converting a fixed-point vector to a list of fixed-point scalars.

Function uv_to_fv_unchecked

pub fn uv_to_fv_unchecked [ N : Nat ] ( xs : arr[uint[N] $post @prover] $pre @public, len : uint[18446744073709551616] $pre @public, pplen : uint[18446744073709551616] $pre @public ) -> FixedVec[N] $pre @public
where
  Field[N],
  Vectorization

Converting an integer vector into a fixed-point vector, unchecked.