Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEAM Nif integration for Pasta Curves #79

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ harness = false
required-features = ["alloc"]

[dependencies]
rustler = { version = "0.29.1", optional = true}
ff = { version = "0.13", default-features = false }
group = { version = "0.13", default-features = false }
rand = { version = "0.8", default-features = false }
Expand Down Expand Up @@ -73,3 +74,4 @@ sqrt-table = ["alloc", "lazy_static"]
repr-c = []
uninline-portable = []
serde = ["hex", "serde_crate"]
repr-erlang=["rustler"]
27 changes: 22 additions & 5 deletions src/curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use group::{
Curve as _, Group as _, GroupEncoding,
};
use rand::RngCore;
#[cfg(feature = "repr-erlang")]
use rustler::NifRecord;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "alloc")]
Expand All @@ -26,12 +28,17 @@ use super::{Fp, Fq};
#[cfg(feature = "alloc")]
use crate::arithmetic::{Coordinates, CurveAffine, CurveExt};

#[cfg(feature = "repr-erlang")]
use alloc::format;

macro_rules! new_curve_impl {
(($($privacy:tt)*), $name:ident, $name_affine:ident, $iso:ident, $base:ident, $scalar:ident,
$curve_id:literal, $a_raw:expr, $b_raw:expr, $curve_type:ident) => {
$curve_id:literal, $a_raw:expr, $b_raw:expr, $curve_type:ident, $name_string:literal, $name_affine_string:literal) => {
/// Represents a point in the projective coordinate space.
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "repr-c", repr(C))]
#[cfg_attr(feature = "repr-erlang", derive(NifRecord))]
#[cfg_attr(feature = "repr-erlang", tag = $name_string)]
$($privacy)* struct $name {
x: $base,
y: $base,
Expand All @@ -51,6 +58,8 @@ macro_rules! new_curve_impl {
/// Represents a point in the affine coordinate space (or the point at
/// infinity).
#[derive(Copy, Clone)]
#[cfg_attr(feature = "repr-erlang", derive(NifRecord))]
#[cfg_attr(feature = "repr-erlang", tag = $name_affine_string)]
#[cfg_attr(feature = "repr-c", repr(C))]
$($privacy)* struct $name_affine {
x: $base,
Expand Down Expand Up @@ -955,7 +964,9 @@ new_curve_impl!(
"pallas",
[0, 0, 0, 0],
[5, 0, 0, 0],
special_a0_b5
special_a0_b5,
"Ep",
"EpAffine"
);
new_curve_impl!(
(pub),
Expand All @@ -967,7 +978,9 @@ new_curve_impl!(
"vesta",
[0, 0, 0, 0],
[5, 0, 0, 0],
special_a0_b5
special_a0_b5,
"Eq",
"EqAffine"
);
new_curve_impl!(
(pub(crate)),
Expand All @@ -984,7 +997,9 @@ new_curve_impl!(
0x18354a2eb0ea8c9c,
],
[1265, 0, 0, 0],
general
general,
"IsoEp",
"IsoEpAffine"
);
new_curve_impl!(
(pub(crate)),
Expand All @@ -1001,7 +1016,9 @@ new_curve_impl!(
0x267f9b2ee592271a,
],
[1265, 0, 0, 0],
general
general,
"IsoEq",
"IsoEqAffine"
);

impl Ep {
Expand Down
18 changes: 18 additions & 0 deletions src/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use ff::{Field, FromUniformBytes, PrimeField, WithSmallOrderMulGroup};
use rand::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "repr-erlang")]
use rustler::{Decoder, Encoder, Env, NifResult, Term};

#[cfg(feature = "sqrt-table")]
use lazy_static::lazy_static;

Expand Down Expand Up @@ -185,6 +188,21 @@ impl<T: ::core::borrow::Borrow<Fp>> ::core::iter::Product<T> for Fp {
}
}

#[cfg(feature = "repr-erlang")]
impl Encoder for Fp {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
(self.0[0], self.0[1], self.0[2], self.0[3]).encode(env)
}
}

#[cfg(feature = "repr-erlang")]
impl<'a> Decoder<'a> for Fp {
fn decode(term: Term<'a>) -> NifResult<Self> {
let tuple: NifResult<(u64, u64, u64, u64)> = Decoder::decode(term);
tuple.map(|res| Fp([res.0, res.1, res.2, res.3]))
}
}

/// INV = -(p^{-1} mod 2^64) mod 2^64
const INV: u64 = 0x992d30ecffffffff;

Expand Down
18 changes: 18 additions & 0 deletions src/fields/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use ff::{Field, FromUniformBytes, PrimeField, WithSmallOrderMulGroup};
use rand::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "repr-erlang")]
use rustler::{Decoder, Encoder, Env, NifResult, Term};

#[cfg(feature = "sqrt-table")]
use lazy_static::lazy_static;

Expand Down Expand Up @@ -103,6 +106,21 @@ impl ConditionallySelectable for Fq {
}
}

#[cfg(feature = "repr-erlang")]
impl Encoder for Fq {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
(self.0[0], self.0[1], self.0[2], self.0[3]).encode(env)
}
}

#[cfg(feature = "repr-erlang")]
impl<'a> Decoder<'a> for Fq {
fn decode(term: Term<'a>) -> NifResult<Self> {
let tuple: NifResult<(u64, u64, u64, u64)> = Decoder::decode(term);
tuple.map(|res| Fq([res.0, res.1, res.2, res.3]))
}
}

/// Constant representing the modulus
/// q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
const MODULUS: Fq = Fq([
Expand Down