Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Type Function

Fe_

Private Type Function
std.crypto.ff.Fe_
fn Fe_(comptime bits: comptime_int) type

A field element.

Fields

v: FeUint

The element value as a Uint.

montgomery: bool = false

true if the element is in Montgomery form.

Types

Values

  • const FeUint
  • pub const encoded_bytes

    The maximum number of bytes required to encode a field element.

Functions

fn limbs_count(self: Self) usize

The number of active limbs to represent the field element.

pub fn fromPrimitive(comptime T: type, m: Modulus(bits), x: T) (OverflowError || FieldElementError)!Self

Creates a field element from a primitive. This function may not run in constant time.

pub fn toPrimitive(self: Self, comptime T: type) (OverflowError || RepresentationError)!T

Converts the field element to a primitive. This function may not run in constant time. Returns an error if the element is in Montgomery form.

pub fn fromBytes(m: Modulus(bits), bytes: []const u8, comptime endian: Endian) (OverflowError || FieldElementError)!Self

Creates a field element from a byte string.

pub fn toBytes(self: Self, bytes: []u8, comptime endian: Endian) (OverflowError || RepresentationError)!void

Converts the field element to a byte string. Returns an error if the element is in Montgomery form.

pub fn eql(x: Self, y: Self) bool

Returns true if the field elements are equal, in constant time.

pub fn compare(x: Self, y: Self) math.Order

Compares two field elements in constant time.

pub fn isZero(self: Self) bool

Returns true if the element is zero.

pub fn isOdd(self: Self) bool

Returns true is the element is odd.

File

crypto/ff.zig:298
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
fn Fe_(comptime bits: comptime_int) type {
    return struct {
        const Self = @This();

        const FeUint = Uint(bits);

        /// The element value as a `Uint`.
        v: FeUint,

        /// `true` if the element is in Montgomery form.
        montgomery: bool = false,

        /// The maximum number of bytes required to encode a field element.
        pub const encoded_bytes = FeUint.encoded_bytes;

        // The number of active limbs to represent the field element.
        fn limbs_count(self: Self) usize {
            return self.v.limbs_len;
        }

        /// Creates a field element from a primitive.
        /// This function may not run in constant time.
        pub fn fromPrimitive(comptime T: type, m: Modulus(bits), x: T) (OverflowError || FieldElementError)!Self {
            comptime assert(@bitSizeOf(T) <= bits); // Primitive type is larger than the modulus type.
            const v = try FeUint.fromPrimitive(T, x);
            var fe = Self{ .v = v };
            try m.shrink(&fe);
            try m.rejectNonCanonical(fe);
            return fe;
        }

        /// Converts the field element to a primitive.
        /// This function may not run in constant time.
        /// Returns an error if the element is in Montgomery form.
        pub fn toPrimitive(self: Self, comptime T: type) (OverflowError || RepresentationError)!T {
            if (self.montgomery) {
                return error.UnexpectedRepresentation;
            }
            return self.v.toPrimitive(T);
        }

        /// Creates a field element from a byte string.
        pub fn fromBytes(m: Modulus(bits), bytes: []const u8, comptime endian: Endian) (OverflowError || FieldElementError)!Self {
            const v = try FeUint.fromBytes(bytes, endian);
            var fe = Self{ .v = v };
            try m.shrink(&fe);
            try m.rejectNonCanonical(fe);
            return fe;
        }

        /// Converts the field element to a byte string.
        /// Returns an error if the element is in Montgomery form.
        pub fn toBytes(self: Self, bytes: []u8, comptime endian: Endian) (OverflowError || RepresentationError)!void {
            if (self.montgomery) {
                return error.UnexpectedRepresentation;
            }
            return self.v.toBytes(bytes, endian);
        }

        /// Returns `true` if the field elements are equal, in constant time.
        pub fn eql(x: Self, y: Self) bool {
            return x.v.eql(y.v);
        }

        /// Compares two field elements in constant time.
        pub fn compare(x: Self, y: Self) math.Order {
            return x.v.compare(y.v);
        }

        /// Returns `true` if the element is zero.
        pub fn isZero(self: Self) bool {
            return self.v.isZero();
        }

        /// Returns `true` is the element is odd.
        pub fn isOdd(self: Self) bool {
            return self.v.isOdd();
        }
    };
}