fn Fe_(comptime bits: comptime_int) type The maximum number of bytes required to encode a field element.
fn limbs_count(self: Self) usizeThe number of active limbs to represent the field element.
pub fn fromPrimitive(comptime T: type, m: Modulus(bits), x: T) (OverflowError || FieldElementError)!SelfCreates a field element from a primitive. This function may not run in constant time.
pub fn toPrimitive(self: Self, comptime T: type) (OverflowError || RepresentationError)!TConverts 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)!SelfCreates a field element from a byte string.
pub fn toBytes(self: Self, bytes: []u8, comptime endian: Endian) (OverflowError || RepresentationError)!voidConverts the field element to a byte string. Returns an error if the element is in Montgomery form.
pub fn eql(x: Self, y: Self) boolReturns true if the field elements are equal, in constant time.
pub fn compare(x: Self, y: Self) math.OrderCompares two field elements in constant time.
pub fn isZero(self: Self) boolReturns true if the element is zero.
pub fn isOdd(self: Self) boolReturns true is the element is odd.
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
377fn 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();
}
};
}
A field element.