Zig 0.17.0-dev (Split by item)

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 library itself.

NistCurveGroup

hybrid_kem.NistCurveGroup
fn NistCurveGroup(comptime Curve: type) type

File

lib/std/crypto/hybrid_kem.zig:318

Code

fn NistCurveGroup(comptime Curve: type) type {
    return struct {
        pub const scalar_length = Curve.scalar.encoded_length;
        pub const seed_length = scalar_length * 4;
        pub const element_length = scalar_length + scalar_length + 1;

        pub fn randomScalar(seed: []const u8) ![scalar_length]u8 {
            var offset: usize = 0;
            while (offset + scalar_length <= seed.len) : (offset += scalar_length) {
                const bytes = seed[offset..][0..scalar_length].*;
                Curve.scalar.rejectNonCanonical(bytes, .big) catch continue;
                return bytes;
            }
            return error.RejectionSamplingFailed;
        }

        pub fn mul(p: Curve, scalar: [scalar_length]u8) !Curve {
            return p.mul(scalar, .big);
        }

        pub fn mulBase(scalar: [scalar_length]u8) !Curve {
            return Curve.basePoint.mul(scalar, .big);
        }

        pub fn elementToSharedSecret(p: Curve) [scalar_length]u8 {
            const affine = p.affineCoordinates();
            return affine.x.toBytes(.big);
        }

        pub fn encodePoint(p: Curve) [element_length]u8 {
            return p.toUncompressedSec1();
        }

        pub fn decodePoint(bytes: *const [element_length]u8) !Curve {
            return Curve.fromSec1(bytes);
        }
    };
}