This commit is contained in:
mariari 2023-12-20 11:16:43 -07:00 committed by GitHub
commit 1124f25589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 5 deletions

View File

@ -44,6 +44,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 }
@ -72,3 +73,4 @@ sqrt-table = ["alloc", "lazy_static"]
repr-c = []
uninline-portable = []
serde = ["hex", "serde_crate"]
repr-erlang=["rustler"]

View File

@ -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")]
@ -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,
@ -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,
@ -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),
@ -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)),
@ -984,7 +997,9 @@ new_curve_impl!(
0x18354a2eb0ea8c9c,
],
[1265, 0, 0, 0],
general
general,
"IsoEp",
"IsoEpAffine"
);
new_curve_impl!(
(pub(crate)),
@ -1001,7 +1016,9 @@ new_curve_impl!(
0x267f9b2ee592271a,
],
[1265, 0, 0, 0],
general
general,
"IsoEq",
"IsoEqAffine"
);
impl Ep {

View File

@ -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;
@ -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;

View File

@ -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;
@ -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([