DoxigAlpha

fromUint

Creates a new modulus from a Uint value. The modulus must be odd and larger than 2.

Function parameters

Parameters

#

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 fromUint(v_: FeUint) InvalidModulusError!Self {
    if (!v_.isOdd()) return error.EvenModulus;

    var v = v_.normalize();
    const hi = v.limbsConst()[v.limbs_len - 1];
    const lo = v.limbsConst()[0];

    if (v.limbs_len < 2 and lo < 3) {
        return error.ModulusTooSmall;
    }

    const leading = @clz(hi) - carry_bits;

    var y = lo;

    inline for (0..comptime math.log2_int(usize, t_bits)) |_| {
        y = y *% (2 -% lo *% y);
    }
    const m0inv = (@as(Limb, 1) << t_bits) - (@as(TLimb, @truncate(y)));

    const zero = Fe{ .v = FeUint.zero };

    var m = Self{
        .zero = zero,
        .v = v,
        .leading = leading,
        .m0inv = m0inv,
        .rr = undefined, // will be computed right after
    };
    m.shrink(&m.zero) catch unreachable;
    computeRR(&m);

    return m;
}