Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::wnaf Namespace Reference

Functions

constexpr size_t get_optimal_bucket_width (const size_t num_points)
 
constexpr size_t get_num_buckets (const size_t num_points)
 
constexpr size_t get_num_rounds (const size_t num_points)
 
template<size_t bits, size_t bit_position>
uint64_t get_wnaf_bits_const (const uint64_t *scalar) noexcept
 
uint64_t get_wnaf_bits (const uint64_t *scalar, const uint64_t bits, const uint64_t bit_position) noexcept
 
void fixed_wnaf_packed (const uint64_t *scalar, uint64_t *wnaf, bool &skew_map, const uint64_t point_index, const size_t wnaf_bits) noexcept
 
void fixed_wnaf (const uint64_t *scalar, uint64_t *wnaf, bool &skew_map, const uint64_t point_index, const uint64_t num_points, const size_t wnaf_bits) noexcept
 Performs fixed-window non-adjacent form (WNAF) computation for scalar multiplication.
 
uint64_t get_num_scalar_bits (const uint64_t *scalar)
 
void fixed_wnaf_with_counts (const uint64_t *scalar, uint64_t *wnaf, bool &skew_map, uint64_t *wnaf_round_counts, const uint64_t point_index, const uint64_t num_points, const size_t wnaf_bits) noexcept
 
template<size_t num_points, size_t wnaf_bits, size_t round_i>
void wnaf_round (uint64_t *scalar, uint64_t *wnaf, const uint64_t point_index, const uint64_t previous) noexcept
 
template<size_t scalar_bits, size_t num_points, size_t wnaf_bits, size_t round_i>
void wnaf_round (uint64_t *scalar, uint64_t *wnaf, const uint64_t point_index, const uint64_t previous) noexcept
 
template<size_t wnaf_bits, size_t round_i>
void wnaf_round_packed (const uint64_t *scalar, uint64_t *wnaf, const uint64_t point_index, const uint64_t previous) noexcept
 
template<size_t num_points, size_t wnaf_bits>
void fixed_wnaf (uint64_t *scalar, uint64_t *wnaf, bool &skew_map, const size_t point_index) noexcept
 
template<size_t num_bits, size_t num_points, size_t wnaf_bits>
void fixed_wnaf (uint64_t *scalar, uint64_t *wnaf, bool &skew_map, const size_t point_index) noexcept
 
template<size_t scalar_bits, size_t num_points, size_t wnaf_bits, size_t round_i>
void wnaf_round_with_restricted_first_slice (uint64_t *scalar, uint64_t *wnaf, const uint64_t point_index, const uint64_t previous) noexcept
 
template<size_t num_bits, size_t num_points, size_t wnaf_bits>
void fixed_wnaf_with_restricted_first_slice (uint64_t *scalar, uint64_t *wnaf, bool &skew_map, const size_t point_index) noexcept
 

Variables

constexpr size_t SCALAR_BITS = 127
 

Function Documentation

◆ fixed_wnaf() [1/3]

void bb::wnaf::fixed_wnaf ( const uint64_t *  scalar,
uint64_t *  wnaf,
bool &  skew_map,
const uint64_t  point_index,
const uint64_t  num_points,
const size_t  wnaf_bits 
)
inlinenoexcept

Performs fixed-window non-adjacent form (WNAF) computation for scalar multiplication.

WNAF is a method for representing integers which optimizes the number of non-zero terms, which in turn optimizes the number of point doublings in scalar multiplication, in turn aiding efficiency.

Parameters
scalarPointer to 128-bit scalar for which WNAF is to be computed.
wnafPointer to num_points+1 size array where the computed WNAF will be stored.
skew_mapReference to a boolean variable which will be set based on the least significant bit of the scalar.
point_indexThe index of the point being computed in the context of multiple point multiplication.
num_pointsThe number of points being computed in parallel.
wnaf_bitsThe number of bits to use in each window of the WNAF representation.

Definition at line 178 of file wnaf.hpp.

◆ fixed_wnaf() [2/3]

template<size_t num_points, size_t wnaf_bits>
void bb::wnaf::fixed_wnaf ( uint64_t *  scalar,
uint64_t *  wnaf,
bool &  skew_map,
const size_t  point_index 
)
inlinenoexcept

Definition at line 428 of file wnaf.hpp.

◆ fixed_wnaf() [3/3]

template<size_t num_bits, size_t num_points, size_t wnaf_bits>
void bb::wnaf::fixed_wnaf ( uint64_t *  scalar,
uint64_t *  wnaf,
bool &  skew_map,
const size_t  point_index 
)
inlinenoexcept

Definition at line 436 of file wnaf.hpp.

◆ fixed_wnaf_packed()

void bb::wnaf::fixed_wnaf_packed ( const uint64_t *  scalar,
uint64_t *  wnaf,
bool &  skew_map,
const uint64_t  point_index,
const size_t  wnaf_bits 
)
inlinenoexcept

Definition at line 141 of file wnaf.hpp.

◆ fixed_wnaf_with_counts()

void bb::wnaf::fixed_wnaf_with_counts ( const uint64_t *  scalar,
uint64_t *  wnaf,
bool &  skew_map,
uint64_t *  wnaf_round_counts,
const uint64_t  point_index,
const uint64_t  num_points,
const size_t  wnaf_bits 
)
inlinenoexcept

How to compute an x-bit wnaf slice?

Iterate over number of slices in scalar. For each slice, if slice is even, ADD +1 to current slice and SUBTRACT 2^x from previous slice. (for 1st slice we instead add +1 and set the scalar's 'skew' value to 'true' (i.e. need to subtract 1 from it at the end of our scalar mul algo))

In *wnaf we store the following:

  1. bits 0-30: ABSOLUTE value of wnaf (i.e. -3 goes to 3)
  2. bit 31: 'predicate' bool (i.e. does the wnaf value need to be negated?)
  3. bits 32-63: position in a point array that describes the elliptic curve point this wnaf slice is referencing

N.B. IN OUR STDLIB ALGORITHMS THE SKEW VALUE REPRESENTS AN ADDITION NOT A SUBTRACTION (i.e. we add +1 at the end of the scalar mul algo we don't sub 1) (this is to eliminate situations which could produce the point at infinity as an output as our circuit logic cannot accommodate this edge case).

Credits: Zac W.

Parameters
scalarPointer to the 128-bit non-montgomery scalar that is supposed to be transformed into wnaf
wnafPointer to output array that needs to accommodate enough 64-bit WNAF entries
skew_mapReference to output skew value, which if true shows that the point should be added once at the end of computation
wnaf_round_countsPointer to output array specifying the number of points participating in each round
point_indexThe index of the point that should be multiplied by this scalar in the point array
num_pointsTotal points in the MSM (2*num_initial_points)

Definition at line 274 of file wnaf.hpp.

◆ fixed_wnaf_with_restricted_first_slice()

template<size_t num_bits, size_t num_points, size_t wnaf_bits>
void bb::wnaf::fixed_wnaf_with_restricted_first_slice ( uint64_t *  scalar,
uint64_t *  wnaf,
bool &  skew_map,
const size_t  point_index 
)
inlinenoexcept

Definition at line 486 of file wnaf.hpp.

◆ get_num_buckets()

constexpr size_t bb::wnaf::get_num_buckets ( const size_t  num_points)
constexpr

Definition at line 67 of file wnaf.hpp.

◆ get_num_rounds()

constexpr size_t bb::wnaf::get_num_rounds ( const size_t  num_points)
constexpr

Definition at line 73 of file wnaf.hpp.

◆ get_num_scalar_bits()

uint64_t bb::wnaf::get_num_scalar_bits ( const uint64_t *  scalar)
inline

Current flow...

If a wnaf entry is even, we add +1 to it, and subtract 32 from the previous entry. This works if the previous entry is odd. If we recursively apply this process, starting at the least significant window, this will always be the case.

However, we want to skip over windows that are 0, which poses a problem.

Scenario 1: even window followed by 0 window followed by any window 'x'

We can't add 1 to the even window and subtract 32 from the 0 window, as we don't have a bucket that maps to -32 This means that we have to identify whether we are going to borrow 32 from 'x', requiring us to look at least 2 steps ahead

Scenario 2: <even> <0> <0> <x>

This problem proceeds indefinitely - if we have adjacent 0 windows, we do not know whether we need to track a borrow flag until we identify the next non-zero window

Scenario 3: <odd> <0>

This one works...

Ok, so we should be a bit more limited with when we don't include window entries. The goal here is to identify short scalars, so we want to identify the most significant non-zero window

Definition at line 234 of file wnaf.hpp.

◆ get_optimal_bucket_width()

constexpr size_t bb::wnaf::get_optimal_bucket_width ( const size_t  num_points)
constexpr

Definition at line 18 of file wnaf.hpp.

◆ get_wnaf_bits()

uint64_t bb::wnaf::get_wnaf_bits ( const uint64_t *  scalar,
const uint64_t  bits,
const uint64_t  bit_position 
)
inlinenoexcept

we want to take a 128 bit scalar and shift it down by (bit_position). We then wish to mask out bits number of bits. Low limb contains first 64 bits, so we wish to shift this limb by (bit_position mod 64), which is also (bit_position & 63) If we require bits from the high limb, these need to be shifted left, not right. Actual bit position of bit in high limb = b. Desired position = 64 - (amount we shifted low limb by) = 64 - (bit_position & 63)

So, step 1: get low limb and shift right by (bit_position & 63) get high limb and shift left by (64 - (bit_position & 63))

Definition at line 113 of file wnaf.hpp.

◆ get_wnaf_bits_const()

template<size_t bits, size_t bit_position>
uint64_t bb::wnaf::get_wnaf_bits_const ( const uint64_t *  scalar)
inlinenoexcept

we want to take a 128 bit scalar and shift it down by (bit_position). We then wish to mask out bits number of bits. Low limb contains first 64 bits, so we wish to shift this limb by (bit_position mod 64), which is also (bit_position & 63) If we require bits from the high limb, these need to be shifted left, not right. Actual bit position of bit in high limb = b. Desired position = 64 - (amount we shifted low limb by) = 64 - (bit_position & 63)

So, step 1: get low limb and shift right by (bit_position & 63) get high limb and shift left by (64 - (bit_position & 63))

Definition at line 79 of file wnaf.hpp.

◆ wnaf_round() [1/2]

template<size_t num_points, size_t wnaf_bits, size_t round_i>
void bb::wnaf::wnaf_round ( uint64_t *  scalar,
uint64_t *  wnaf,
const uint64_t  point_index,
const uint64_t  previous 
)
inlinenoexcept

Definition at line 348 of file wnaf.hpp.

◆ wnaf_round() [2/2]

template<size_t scalar_bits, size_t num_points, size_t wnaf_bits, size_t round_i>
void bb::wnaf::wnaf_round ( uint64_t *  scalar,
uint64_t *  wnaf,
const uint64_t  point_index,
const uint64_t  previous 
)
inlinenoexcept

Definition at line 373 of file wnaf.hpp.

◆ wnaf_round_packed()

template<size_t wnaf_bits, size_t round_i>
void bb::wnaf::wnaf_round_packed ( const uint64_t *  scalar,
uint64_t *  wnaf,
const uint64_t  point_index,
const uint64_t  previous 
)
inlinenoexcept

Definition at line 399 of file wnaf.hpp.

◆ wnaf_round_with_restricted_first_slice()

template<size_t scalar_bits, size_t num_points, size_t wnaf_bits, size_t round_i>
void bb::wnaf::wnaf_round_with_restricted_first_slice ( uint64_t *  scalar,
uint64_t *  wnaf,
const uint64_t  point_index,
const uint64_t  previous 
)
inlinenoexcept

Definition at line 444 of file wnaf.hpp.

Variable Documentation

◆ SCALAR_BITS

constexpr size_t bb::wnaf::SCALAR_BITS = 127
constexpr

Definition at line 14 of file wnaf.hpp.