[][src]Macro secp256kfun::g

macro_rules! g {
    ($($t:tt)+) => { ... };
}

Group operation expression macro.

The g! macro lets you express a set of scalar multiplications and group additions/substraction. This compiles down to operations from the op module. Apart from being far more readable, the idea is that g! will (or may in the future) compile to more efficient operations than if you were to manually call the functions from op yourself.

As a bonus, you don't need to put reference & makers on terms in g! this is done automatically if necessary.

Examples

Simple scalar multiplication by G but will work with any Point

use secp256kfun::{g, Scalar, G};
let x = Scalar::random(&mut rand::thread_rng());
let X = g!(x * G);

A more complicated set of expressions.

let x = Scalar::random(&mut rand::thread_rng());
let y = Scalar::random(&mut rand::thread_rng());
let H = Point::random(&mut rand::thread_rng());
let minus = g!(x * G - y * H);
let plus = g!(x * G + y * H);
// note the parenthesis around the scalar sub expression
assert_eq!(g!(plus + minus), g!((2 * x) * G));

You may access attributes:

struct DoMul {
    scalar: Scalar,
    point: Point,
}

let mul = DoMul {
    scalar: Scalar::random(&mut rand::thread_rng()),
    point: Point::random(&mut rand::thread_rng()),
};

let result = g!(mul.scalar * mul.point);

You can put an arbitrary expressions inside {...}

let x = Scalar::random(&mut rand::thread_rng());
let Xinv = g!({ x.invert() } * G);
assert_eq!(g!(x * Xinv), *G);