Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

impl trait GpuEngine preparing for gpu accelerated FFT and multiexp #12

Closed
wants to merge 1 commit into from
Closed
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 @@ -26,11 +26,13 @@ group = "0.11"
static_assertions = "1.1.0"
rand = "0.8"
rand_core = { version = "0.6", default-features = false }
ec-gpu = { version = "0.1.0", optional = true }

[features]
default = []
asm = []
prefetch = []
gpu = ["ec-gpu"]

[profile.bench]
opt-level = 3
Expand Down
14 changes: 14 additions & 0 deletions src/bn256/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,20 @@ impl MultiMillerLoop for Bn256 {
}
}

#[cfg(feature = "gpu")]
impl ec_gpu::GpuEngine for Bn256 {
type Scalar = Fr;
type Fp = Fq;
}

#[cfg(feature = "gpu")]
pub fn u64_to_u32(limbs: &[u64]) -> Vec<u32> {
limbs
.iter()
.flat_map(|limb| vec![(limb & 0xFFFF_FFFF) as u32, (limb >> 32) as u32])
.collect()
}

#[cfg(test)]
use rand::SeedableRng;
#[cfg(test)]
Expand Down
17 changes: 17 additions & 0 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use super::assembly::assembly_field;
use super::common::common_field;
use super::LegendreSymbol;
#[cfg(feature = "gpu")]
use super::engine::u64_to_u32;
use crate::arithmetic::{adc, mac, sbb, BaseExt, FieldExt, Group};
use core::convert::TryInto;
use core::fmt;
Expand Down Expand Up @@ -274,6 +276,21 @@ impl ff::PrimeField for Fq {
}
}

#[cfg(feature = "gpu")]
impl ec_gpu::GpuField for Fq {
fn one() -> Vec<u32> {
u64_to_u32(&R.0[..])
}

fn r2() -> Vec<u32> {
u64_to_u32(&R2.0[..])
}

fn modulus() -> Vec<u32> {
u64_to_u32(&MODULUS.0[..])
}
}

#[cfg(test)]
use ff::Field;
#[cfg(test)]
Expand Down
17 changes: 17 additions & 0 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use super::assembly::assembly_field;
use super::common::common_field;
use super::LegendreSymbol;
#[cfg(feature = "gpu")]
use super::engine::u64_to_u32;
use crate::arithmetic::{adc, mac, sbb, BaseExt, FieldExt, Group};
use core::convert::TryInto;
use core::fmt;
Expand Down Expand Up @@ -236,6 +238,21 @@ impl ff::PrimeField for Fr {
}
}

#[cfg(feature = "gpu")]
impl ec_gpu::GpuField for Fr {
fn one() -> Vec<u32> {
u64_to_u32(&R.0[..])
}

fn r2() -> Vec<u32> {
u64_to_u32(&R2.0[..])
}

fn modulus() -> Vec<u32> {
u64_to_u32(&MODULUS.0[..])
}
}

#[cfg(test)]
use ff::Field;

Expand Down