Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
20 changes: 18 additions & 2 deletions skyscraper/bn254-multiplier/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {

// #[divan::bench_group]
mod mul {
use super::*;
use {super::*, bn254_multiplier::rne};

#[divan::bench]
fn scalar_mul(bencher: Bencher) {
Expand All @@ -31,13 +31,21 @@ mod mul {
.bench_local_values(|(a, b)| a * b);
}

#[divan::bench]
fn mono_mul_b51(bencher: Bencher) {
bencher
//.counter(ItemsCount::new(1usize))
.with_inputs(|| rng().random())
.bench_local_values(|(a, b)| rne::mono::mul(a, b));
}

#[divan::bench]
fn simd_mul_51b(bencher: Bencher) {
bencher
//.counter(ItemsCount::new(2usize))
.with_inputs(|| rng().random())
.bench_local_values(|(a, b, c, d)| {
bn254_multiplier::rne::portable_simd::simd_mul(a, b, c, d)
bn254_multiplier::rne::batched::simd_mul(a, b, c, d)
});
}

Expand Down Expand Up @@ -141,6 +149,14 @@ mod sqr {
.bench_local_values(|(a, b)| rne::simd_sqr(a, b));
}

#[divan::bench]
fn mono_sqr_b51(bencher: Bencher) {
bencher
//.counter(ItemsCount::new(1usize))
.with_inputs(|| rng().random())
.bench_local_values(|a| rne::mono::sqr(a));
}

#[divan::bench]
fn ark_ff(bencher: Bencher) {
use {ark_bn254::Fr, ark_ff::BigInt};
Expand Down
105 changes: 105 additions & 0 deletions skyscraper/bn254-multiplier/examples/count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//! Helper script to calculate the magic numbers to cancel out the anchors used
//! in FMA multiplication.
use bn254_multiplier::rne::{
mono::{mul, sqr},
simd_utils::make_initial,
};

const LO: usize = 1;
const HI: usize = 1 << 16;

fn main() {
let t = diagonal();
for (i, x) in t.iter().enumerate() {
println!("t[{i}]: lo: {}\t hi: {}", x & (HI - 1), x >> 16);
}

let a = [0, 0, 0, 0];
let res = sqr(a);
for (i, k) in res.iter().enumerate() {
println!("res[{i}]: {:x}", k)
}

println!("\nFULL");

let t = full();
for (i, x) in t.iter().enumerate() {
let lo = x & (HI - 1);
let hi = x >> 16;
println!(
"t[{i}]: lo: {}\t hi: {}\t init: {:x}",
lo,
hi,
make_initial(lo as u64, hi as u64)
);
}

let a = [0, 0, 0, 0];
let res = mul(a, a);
for (i, k) in res.iter().enumerate() {
println!("res[{i}]: {:x}", k)
}
}

fn diagonal() -> [usize; 12] {
let mut t = [0; 12];
for i in 0..5 {
for j in ((i + 1)..5).step_by(2) {
println!("i: {i} j: {} {}", j, j + 1);
t[i + j + 2] += HI;
t[i + j + 1] += LO + HI;
t[i + j] += LO;
}
println!();
}

// Since there is no data of the diagionals yet it is safe to double them. This
// means that t[8][1] = t[9] needs to be included.
// t[0][1] is not touched by the diagonalisation so no need to include t[0]

for i in 1..=9 {
t[i] += t[i];
}

for i in (0..4).step_by(2) {
t[2 * (i + 1) + 1] += HI;
t[2 * (i + 1)] += LO;
t[2 * i + 1] += HI;
t[2 * i] += LO;
}
t[2 * 4 + 1] += HI;
t[2 * 4] += LO;

let i = 4;
for _k in 0..5 {
for j in 0..3 {
t[i + 2 * j + 2] += HI;
t[i + 2 * j + 1] += LO + HI;
t[i + 2 * j] += LO;
}
}

t
}

fn full() -> [usize; 11] {
let mut t = [0; 11];
for i in 0..5 {
for j in 0..3 {
t[i + 2 * j + 2] += HI;
t[i + 2 * j + 1] += LO + HI;
t[i + 2 * j] += LO;
}
}

let i = 4;
for _k in 0..5 {
for j in 0..3 {
t[i + 2 * j + 2] += HI;
t[i + 2 * j + 1] += LO + HI;
t[i + 2 * j] += LO;
}
}

t
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ pub fn simd_mul(
mod tests {
use {
super::*,
crate::{rne::simd_utils::u255_to_u256_simd, test_utils::ark_ff_reference},
crate::{
rne::simd_utils::u255_to_u256_simd,
test_utils::{ark_ff_reference, limbs5_51},
},
ark_bn254::Fr,
ark_ff::{BigInt, PrimeField},
proptest::{
prelude::{prop, Strategy},
prop_assert_eq, proptest,
},
proptest::{prop_assert_eq, proptest},
};

#[test]
Expand Down Expand Up @@ -248,12 +248,4 @@ mod tests {
prop_assert_eq!(b2, b2s);
})
}

fn limb51() -> impl Strategy<Value = u64> {
0u64..(1u64 << 51)
}

fn limbs5_51() -> impl Strategy<Value = [u64; 5]> {
prop::array::uniform5(limb51())
}
}
5 changes: 3 additions & 2 deletions skyscraper/bn254-multiplier/src/rne/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
//! Point Arithmetic on the GPU, 2018 IEEE 25th Symposium on Computer Arithmetic
//! (ARITH) by Emmart, Zheng and Weems; which uses RTZ.

pub mod batched;
pub mod constants;
pub mod portable_simd;
pub mod mono;
pub mod simd_utils;

pub use {constants::*, portable_simd::*, simd_utils::*};
pub use {batched::*, constants::*, mono::*};
Loading
Loading