Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active July 24, 2020 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuoZijun/cf1e56799d1a076e50ad3167ae6bc655 to your computer and use it in GitHub Desktop.
Save LuoZijun/cf1e56799d1a076e50ad3167ae6bc655 to your computer and use it in GitHub Desktop.
#![feature(test)]
extern crate test;
#[macro_use]
extern crate lazy_static;
extern crate generic_array;
extern crate fff;
extern crate paired;
extern crate neptune;
#[allow(unused_imports)]
use fff::{Field, PrimeField, PrimeFieldRepr, ScalarEngine};
use paired::bls12_381::{Bls12, Fr, FrRepr};
use generic_array::typenum::{U1, U11, U16, U2, U24, U36, U4, U8};
use neptune::poseidon::PoseidonConstants;
pub type PoseidonMDArity = U36;
lazy_static! {
// pub static ref POSEIDON_CONSTANTS_1: PoseidonConstants::<Bls12, U1> = PoseidonConstants::new();
// pub static ref POSEIDON_CONSTANTS_2: PoseidonConstants::<Bls12, U2> = PoseidonConstants::new();
// pub static ref POSEIDON_CONSTANTS_4: PoseidonConstants::<Bls12, U4> = PoseidonConstants::new();
// pub static ref POSEIDON_CONSTANTS_8: PoseidonConstants::<Bls12, U8> = PoseidonConstants::new();
// pub static ref POSEIDON_CONSTANTS_16: PoseidonConstants::<Bls12, U16> = PoseidonConstants::new();
// pub static ref POSEIDON_CONSTANTS_24: PoseidonConstants::<Bls12, U24> = PoseidonConstants::new();
// pub static ref POSEIDON_CONSTANTS_36: PoseidonConstants::<Bls12, U36> = PoseidonConstants::new();
pub static ref POSEIDON_CONSTANTS_11: PoseidonConstants::<Bls12, U11> = PoseidonConstants::new();
// pub static ref POSEIDON_MD_CONSTANTS: PoseidonConstants::<Bls12, PoseidonMDArity> = PoseidonConstants::new();
}
fn init_lazy_static() {
// lazy_static::initialize(&POSEIDON_CONSTANTS_1);
// lazy_static::initialize(&POSEIDON_CONSTANTS_2);
// lazy_static::initialize(&POSEIDON_CONSTANTS_4);
// lazy_static::initialize(&POSEIDON_CONSTANTS_8);
// lazy_static::initialize(&POSEIDON_CONSTANTS_16);
// lazy_static::initialize(&POSEIDON_CONSTANTS_24);
// lazy_static::initialize(&POSEIDON_CONSTANTS_36);
lazy_static::initialize(&POSEIDON_CONSTANTS_11);
// lazy_static::initialize(&POSEIDON_MD_CONSTANTS);
}
#[bench]
fn fr_from_le_bytes(b: &mut test::Bencher) {
let data = [0u8; 32];
b.bytes = 32;
b.iter(|| {
let mut repr = FrRepr::default();
repr.read_le(&data[..]).unwrap();
Fr::from_repr(repr).unwrap()
})
}
#[inline]
fn poseidon_hash(preimage: &[Fr]) -> Fr {
use neptune::poseidon::Poseidon;
match preimage.len() {
// 1 => {
// let mut p = Poseidon::new_with_preimage(&preimage, &*POSEIDON_CONSTANTS_1);
// p.hash()
// }
// 2 => {
// let mut p = Poseidon::new_with_preimage(&preimage, &POSEIDON_CONSTANTS_2);
// p.hash()
// }
// 4 => {
// let mut p = Poseidon::new_with_preimage(&preimage, &POSEIDON_CONSTANTS_4);
// p.hash()
// }
// 8 => {
// let mut p = Poseidon::new_with_preimage(&preimage, &POSEIDON_CONSTANTS_8);
// p.hash()
// }
11 => {
let mut hasher = Poseidon::new_with_preimage(&preimage, &*POSEIDON_CONSTANTS_11);
hasher.hash()
}
// 16 => {
// let mut p = Poseidon::new_with_preimage(&preimage, &POSEIDON_CONSTANTS_16);
// p.hash()
// }
// 24 => {
// let mut hasher = Poseidon::new_with_preimage(&preimage, &*POSEIDON_CONSTANTS_24);
// hasher.hash()
// }
// 36 => {
// let mut hasher = Poseidon::new_with_preimage(&preimage, &*POSEIDON_CONSTANTS_36);
// hasher.hash()
// }
_ => unreachable!(),
}
}
// #[bench]
// fn bench_poseidon_hash_32_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 1];
// b.bytes = 32;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
// #[bench]
// fn bench_poseidon_hash_64_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 2];
// b.bytes = 64;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
// #[bench]
// fn bench_poseidon_hash_128_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 4];
// b.bytes = 128;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
// #[bench]
// fn bench_poseidon_hash_256_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 8];
// b.bytes = 256;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
#[bench]
fn bench_poseidon_hash_352_bytes(b: &mut test::Bencher) {
init_lazy_static();
let data = [ Fr::zero(); 11];
b.bytes = 352;
b.iter(|| {
poseidon_hash(&data)
})
}
// #[bench]
// fn bench_poseidon_hash_512_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 16];
// b.bytes = 512;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
// #[bench]
// fn bench_poseidon_hash_768_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 24];
// b.bytes = 768;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
// #[bench]
// fn bench_poseidon_hash_1152_bytes(b: &mut test::Bencher) {
// init_lazy_static();
// let data = [ Fr::zero(); 36];
// b.bytes = 1152;
// b.iter(|| {
// poseidon_hash(&data)
// })
// }
[package]
name = "bench_neptune"
version = "0.1.0"
authors = ["luozijun <luozijun.assistant@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.4"
generic-array = "0.14"
fff = "0.2"
paired = "0.20"
neptune = "1.2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment