taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

base32.rs (1934B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024, 2025, 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
     18 use rand::RngExt as _;
     19 use taler_common::{
     20     encoding::base32::{decode_static, encode_static},
     21     types::base32::Base32,
     22 };
     23 
     24 fn parser(c: &mut Criterion) {
     25     let mut buf = [0u8; 255];
     26     c.bench_function("base32_encode_random", |b| {
     27         b.iter_batched(
     28             rand::random::<[u8; 64]>,
     29             |case| {
     30                 encode_static(&case, &mut buf);
     31             },
     32             BatchSize::SmallInput,
     33         )
     34     });
     35     c.bench_function("base32_decode_valid", |b| {
     36         b.iter_batched(
     37             || Base32::<64>::rand().to_string(),
     38             |case| decode_static::<64>(case.as_bytes()).unwrap(),
     39             BatchSize::SmallInput,
     40         )
     41     });
     42     c.bench_function("base32_decode_random", |b| {
     43         b.iter_batched(
     44             || {
     45                 rand::rng()
     46                     .sample_iter::<char, _>(&rand::distr::StandardUniform)
     47                     .take(56)
     48                     .collect::<String>()
     49             },
     50             |case| decode_static::<64>(case.as_bytes()).ok(),
     51             BatchSize::SmallInput,
     52         )
     53     });
     54 }
     55 
     56 criterion_group!(benches, parser);
     57 criterion_main!(benches);