DoxigAlpha

reduce

Reduces an arbitrary Uint, converting it to a field element.

Function parameters

Parameters

#
x:anytype

An unsigned big integer with a fixed maximum size (`max_bits`), suitable for cryptographic operations.

Functions

#
Uint
An unsigned big integer with a fixed maximum size (`max_bits`), suitable for cryptographic operations.
Modulus
A modulus, defining a finite field.

Error sets in this namespace

Error Sets

#
OverflowError
Value is too large for the destination.
InvalidModulusError
Invalid modulus.
NullExponentError
Exponentiation with a null exponent.
FieldElementError
Invalid field element for the given modulus.
RepresentationError
Invalid representation (Montgomery vs non-Montgomery domain.)
Error
The set of all possible errors `std.crypto.ff` functions can return.

Source

Implementation

#
pub fn reduce(self: Self, x: anytype) Fe {
    var out = self.zero;
    var i = x.limbs_len - 1;
    if (self.limbs_count() >= 2) {
        const start = @min(i, self.limbs_count() - 2);
        var j = start;
        while (true) : (j -= 1) {
            out.v.limbs()[j] = x.limbsConst()[i];
            i -= 1;
            if (j == 0) break;
        }
    }
    while (true) : (i -= 1) {
        self.shiftIn(&out, x.limbsConst()[i]);
        if (i == 0) break;
    }
    return out;
}