structstd.crypto.pcurves.p256.scalar.Scalar[src]

A scalar in unpacked representation.

Fields

fe: Fe

Values

Constantzero[src]

Zero.

Source Code

Source code
pub const zero = Scalar{ .fe = Fe.zero }

Constantone[src]

One.

Source Code

Source code
pub const one = Scalar{ .fe = Fe.one }

Functions

FunctionfromBytes[src]

Unpack a serialized representation of a scalar.

Parameters

Source Code

Source code
pub fn fromBytes(s: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!Scalar {
    return Scalar{ .fe = try Fe.fromBytes(s, endian) };
}

FunctionfromBytes48[src]

pub fn fromBytes48(s: [48]u8, endian: std.builtin.Endian) Scalar

Reduce a 384 bit input to the field size.

Parameters

s: [48]u8

Source Code

Source code
pub fn fromBytes48(s: [48]u8, endian: std.builtin.Endian) Scalar {
    const t = ScalarDouble.fromBytes(384, s, endian);
    return t.reduce(384);
}

FunctionfromBytes64[src]

pub fn fromBytes64(s: [64]u8, endian: std.builtin.Endian) Scalar

Reduce a 512 bit input to the field size.

Parameters

s: [64]u8

Source Code

Source code
pub fn fromBytes64(s: [64]u8, endian: std.builtin.Endian) Scalar {
    const t = ScalarDouble.fromBytes(512, s, endian);
    return t.reduce(512);
}

FunctiontoBytes[src]

pub fn toBytes(n: Scalar, endian: std.builtin.Endian) CompressedScalar

Pack a scalar into bytes.

Parameters

Source Code

Source code
pub fn toBytes(n: Scalar, endian: std.builtin.Endian) CompressedScalar {
    return n.fe.toBytes(endian);
}

FunctionisZero[src]

pub fn isZero(n: Scalar) bool

Return true if the scalar is zero..

Parameters

Source Code

Source code
pub fn isZero(n: Scalar) bool {
    return n.fe.isZero();
}

FunctionisOdd[src]

pub fn isOdd(n: Scalar) bool

Return true if the scalar is odd.

Parameters

Source Code

Source code
pub fn isOdd(n: Scalar) bool {
    return n.fe.isOdd();
}

Functionequivalent[src]

pub fn equivalent(a: Scalar, b: Scalar) bool

Return true if a and b are equivalent.

Parameters

Source Code

Source code
pub fn equivalent(a: Scalar, b: Scalar) bool {
    return a.fe.equivalent(b.fe);
}

Functionadd[src]

pub fn add(x: Scalar, y: Scalar) Scalar

Compute x+y (mod L)

Parameters

Source Code

Source code
pub fn add(x: Scalar, y: Scalar) Scalar {
    return Scalar{ .fe = x.fe.add(y.fe) };
}

Functionsub[src]

pub fn sub(x: Scalar, y: Scalar) Scalar

Compute x-y (mod L)

Parameters

Source Code

Source code
pub fn sub(x: Scalar, y: Scalar) Scalar {
    return Scalar{ .fe = x.fe.sub(y.fe) };
}

Functiondbl[src]

pub fn dbl(n: Scalar) Scalar

Compute 2n (mod L)

Parameters

Source Code

Source code
pub fn dbl(n: Scalar) Scalar {
    return Scalar{ .fe = n.fe.dbl() };
}

Functionmul[src]

pub fn mul(x: Scalar, y: Scalar) Scalar

Compute x*y (mod L)

Parameters

Source Code

Source code
pub fn mul(x: Scalar, y: Scalar) Scalar {
    return Scalar{ .fe = x.fe.mul(y.fe) };
}

Functionsq[src]

pub fn sq(n: Scalar) Scalar

Compute x^2 (mod L)

Parameters

Source Code

Source code
pub fn sq(n: Scalar) Scalar {
    return Scalar{ .fe = n.fe.sq() };
}

Functionpow[src]

pub fn pow(a: Scalar, comptime T: type, comptime n: T) Scalar

Compute x^n (mod L)

Parameters

T: type
n: T

Source Code

Source code
pub fn pow(a: Scalar, comptime T: type, comptime n: T) Scalar {
    return Scalar{ .fe = a.fe.pow(n) };
}

Functionneg[src]

pub fn neg(n: Scalar) Scalar

Compute -x (mod L)

Parameters

Source Code

Source code
pub fn neg(n: Scalar) Scalar {
    return Scalar{ .fe = n.fe.neg() };
}

Functioninvert[src]

pub fn invert(n: Scalar) Scalar

Compute x^-1 (mod L)

Parameters

Source Code

Source code
pub fn invert(n: Scalar) Scalar {
    return Scalar{ .fe = n.fe.invert() };
}

FunctionisSquare[src]

pub fn isSquare(n: Scalar) bool

Return true if n is a quadratic residue mod L.

Parameters

Source Code

Source code
pub fn isSquare(n: Scalar) bool {
    return n.fe.isSquare();
}

Functionsqrt[src]

pub fn sqrt(n: Scalar) NotSquareError!Scalar

Return the square root of L, or NotSquare if there isn't any solutions.

Parameters

Source Code

Source code
pub fn sqrt(n: Scalar) NotSquareError!Scalar {
    return Scalar{ .fe = try n.fe.sqrt() };
}

Functionrandom[src]

pub fn random() Scalar

Return a random scalar < L.

Source Code

Source code
pub fn random() Scalar {
    var s: [48]u8 = undefined;
    while (true) {
        crypto.random.bytes(&s);
        const n = Scalar.fromBytes48(s, .little);
        if (!n.isZero()) {
            return n;
        }
    }
}

Source Code

Source code
pub const Scalar = struct {
    fe: Fe,

    /// Zero.
    pub const zero = Scalar{ .fe = Fe.zero };

    /// One.
    pub const one = Scalar{ .fe = Fe.one };

    /// Unpack a serialized representation of a scalar.
    pub fn fromBytes(s: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!Scalar {
        return Scalar{ .fe = try Fe.fromBytes(s, endian) };
    }

    /// Reduce a 384 bit input to the field size.
    pub fn fromBytes48(s: [48]u8, endian: std.builtin.Endian) Scalar {
        const t = ScalarDouble.fromBytes(384, s, endian);
        return t.reduce(384);
    }

    /// Reduce a 512 bit input to the field size.
    pub fn fromBytes64(s: [64]u8, endian: std.builtin.Endian) Scalar {
        const t = ScalarDouble.fromBytes(512, s, endian);
        return t.reduce(512);
    }

    /// Pack a scalar into bytes.
    pub fn toBytes(n: Scalar, endian: std.builtin.Endian) CompressedScalar {
        return n.fe.toBytes(endian);
    }

    /// Return true if the scalar is zero..
    pub fn isZero(n: Scalar) bool {
        return n.fe.isZero();
    }

    /// Return true if the scalar is odd.
    pub fn isOdd(n: Scalar) bool {
        return n.fe.isOdd();
    }

    /// Return true if a and b are equivalent.
    pub fn equivalent(a: Scalar, b: Scalar) bool {
        return a.fe.equivalent(b.fe);
    }

    /// Compute x+y (mod L)
    pub fn add(x: Scalar, y: Scalar) Scalar {
        return Scalar{ .fe = x.fe.add(y.fe) };
    }

    /// Compute x-y (mod L)
    pub fn sub(x: Scalar, y: Scalar) Scalar {
        return Scalar{ .fe = x.fe.sub(y.fe) };
    }

    /// Compute 2n (mod L)
    pub fn dbl(n: Scalar) Scalar {
        return Scalar{ .fe = n.fe.dbl() };
    }

    /// Compute x*y (mod L)
    pub fn mul(x: Scalar, y: Scalar) Scalar {
        return Scalar{ .fe = x.fe.mul(y.fe) };
    }

    /// Compute x^2 (mod L)
    pub fn sq(n: Scalar) Scalar {
        return Scalar{ .fe = n.fe.sq() };
    }

    /// Compute x^n (mod L)
    pub fn pow(a: Scalar, comptime T: type, comptime n: T) Scalar {
        return Scalar{ .fe = a.fe.pow(n) };
    }

    /// Compute -x (mod L)
    pub fn neg(n: Scalar) Scalar {
        return Scalar{ .fe = n.fe.neg() };
    }

    /// Compute x^-1 (mod L)
    pub fn invert(n: Scalar) Scalar {
        return Scalar{ .fe = n.fe.invert() };
    }

    /// Return true if n is a quadratic residue mod L.
    pub fn isSquare(n: Scalar) bool {
        return n.fe.isSquare();
    }

    /// Return the square root of L, or NotSquare if there isn't any solutions.
    pub fn sqrt(n: Scalar) NotSquareError!Scalar {
        return Scalar{ .fe = try n.fe.sqrt() };
    }

    /// Return a random scalar < L.
    pub fn random() Scalar {
        var s: [48]u8 = undefined;
        while (true) {
            crypto.random.bytes(&s);
            const n = Scalar.fromBytes48(s, .little);
            if (!n.isZero()) {
                return n;
            }
        }
    }
}