Skip to content

Commit

Permalink
rename (#17)
Browse files Browse the repository at this point in the history
* rename

* u128
  • Loading branch information
sergey-shandar authored Oct 6, 2023
1 parent 66a9433 commit 5eaecd6
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions blockset/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
base32::{StrEx, ToBase32},
digest224::Digest224,
io::Io,
u224::U224,
};

pub fn run(io: &mut impl Io) -> Result<(), &str> {
Expand All @@ -11,7 +11,7 @@ pub fn run(io: &mut impl Io) -> Result<(), &str> {
match command.as_str() {
"validate" => {
let b32 = a.next().ok_or("missing address")?;
let d = b32.from_base32::<Digest224>().ok_or("invalid address")?;
let d = b32.from_base32::<U224>().ok_or("invalid address")?;
io.print("valid: ");
io.println(&d.to_base32());
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions blockset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ mod app;
mod ascii;
mod base32;
mod bit_vec;
mod digest224;
mod io;
mod overflow32;
mod sha224;
mod sigma32;
mod u32x16;
mod u32x4;
mod u32x8;
mod u128;
mod u224;
mod u256;
mod u512;

#[cfg(test)]
mod static_assert;
Expand Down
31 changes: 14 additions & 17 deletions blockset/src/sha224.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
overflow32::{add, add3, add4},
sigma32::{BIG0, BIG1, SMALL0, SMALL1},
u32x16::U512,
u32x4::{get_u32, to_u128, to_u32x4},
u32x8::{u32x8_add, U256},
u128::{from_u32x4, get_u32, to_u32x4},
u224::U224,
u256::{to_u32x8, u32x8_add, U256},
u512::U512,
};

const fn round([s0, s1]: U256, i: usize, w: u128, k: u128) -> U256 {
Expand Down Expand Up @@ -94,7 +95,7 @@ const fn w_round4(w: &U512, i: usize) -> u128 {
w0[1] = w_round(w0[1], w0[2], w22, w33);
w0[2] = w_round(w0[2], w0[3], w23, w0[0]);
w0[3] = w_round(w0[3], w10, w30, w0[1]);
to_u128(w0)
from_u32x4(w0)
}

const fn w_round16(mut w: U512) -> U512 {
Expand All @@ -110,7 +111,7 @@ pub const INIT: U256 = [
0xbefa4fa4_64f98fa7_68581511_ffc00b31,
];

pub const fn compress(mut w: U512) -> U256 {
pub const fn compress(mut w: U512) -> U224 {
let mut x: U256 = INIT;
x = round16(x, &w, 0);
w = w_round16(w);
Expand All @@ -120,35 +121,31 @@ pub const fn compress(mut w: U512) -> U256 {
w = w_round16(w);
x = round16(x, &w, 3);
x = u32x8_add(&x, &INIT);
x[1] |= 0xFFFF_FFFF << 96;
x
let [x0, x1, x2, x3, x4, x5, x6, _] = to_u32x8(&x);
[x0, x1, x2, x3, x4, x5, x6]
}

#[cfg(test)]
mod test {
use super::{compress, U256};
use crate::u224::U224;

const A: U256 = compress([0x8000_0000, 0, 0, 0]);
use super::compress;

const A: U224 = compress([0x8000_0000, 0, 0, 0]);

#[test]
fn test() {
assert_eq!(
A,
[
0x288234c4_476102bb_2a3a2bc9_d14a028c,
0xFFFFFFFF_c5b3e42f_828ea62a_15a2b01f
]
[0xd14a028c, 0x2a3a2bc9, 0x476102bb, 0x288234c4, 0x15a2b01f, 0x828ea62a, 0xc5b3e42f]
);
}

#[test]
fn runtime_test() {
assert_eq!(
compress([0x8000_0000, 0, 0, 0]),
[
0x288234c4_476102bb_2a3a2bc9_d14a028c,
0xFFFFFFFF_c5b3e42f_828ea62a_15a2b01f
]
[0xd14a028c, 0x2a3a2bc9, 0x476102bb, 0x288234c4, 0x15a2b01f, 0x828ea62a, 0xc5b3e42f]
);
}
}
4 changes: 2 additions & 2 deletions blockset/src/u32x4.rs → blockset/src/u128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const fn to_u32x4(v: u128) -> [u32; 4] {
}

#[inline(always)]
pub const fn to_u128([w0, w1, w2, w3]: [u32; 4]) -> u128 {
pub const fn from_u32x4([w0, w1, w2, w3]: [u32; 4]) -> u128 {
w0 as u128 | ((w1 as u128) << 32) | ((w2 as u128) << 64) | ((w3 as u128) << 96)
}

Expand All @@ -24,5 +24,5 @@ pub const fn get_u32(v: u128, i: usize) -> u32 {
pub const fn u32x4_add(a: u128, b: u128) -> u128 {
let [a0, a1, a2, a3] = to_u32x4(a);
let [b0, b1, b2, b3] = to_u32x4(b);
to_u128([add(a0, b0), add(a1, b1), add(a2, b2), add(a3, b3)])
from_u32x4([add(a0, b0), add(a1, b1), add(a2, b2), add(a3, b3)])
}
20 changes: 10 additions & 10 deletions blockset/src/digest224.rs → blockset/src/u224.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use crate::{
bit_vec::BitVec,
};

pub type Digest224 = [u32; 7];
pub type U224 = [u32; 7];

pub trait Digest224Ex {
pub trait U224Ex {
fn parity_bit(self) -> u8;
}

impl Digest224Ex for &Digest224 {
impl U224Ex for &U224 {
fn parity_bit(self) -> u8 {
self.iter().fold(0, |a, b| a ^ b.count_ones()) as u8 & 1
}
}

impl ToBase32 for &Digest224 {
impl ToBase32 for &U224 {
fn to_base32(self) -> String {
let (result, BitVec { value, len }) = self
.iter()
Expand All @@ -31,15 +31,15 @@ impl ToBase32 for &Digest224 {
}
}

impl FromBase32 for Digest224 {
impl FromBase32 for U224 {
fn from_base32(i: &str) -> Option<Self> {
let (vec, BitVec { value, len }) = i.from_base32()?;
if vec.len() != 7 {
return None;
}
assert_eq!(len, 1);
assert_eq!(value | 1, 1);
let mut result = Digest224::default();
let mut result = U224::default();
result.copy_from_slice(&vec);
if value != result.parity_bit() as u64 {
return None;
Expand All @@ -52,7 +52,7 @@ impl FromBase32 for Digest224 {
mod tests {
use crate::{
base32::{StrEx, ToBase32},
digest224::{Digest224, Digest224Ex},
u224::{U224Ex, U224},
};

#[test]
Expand Down Expand Up @@ -219,12 +219,12 @@ mod tests {

#[test]
fn invalid_str_test() {
assert_eq!("01".from_base32::<Digest224>(), None);
assert_eq!("01".from_base32::<U224>(), None);
assert!("3v1d4j94scaseqgcyzr0ha5dxa9rx6ppnfbndck971ac0"
.from_base32::<Digest224>()
.from_base32::<U224>()
.is_none());
assert!("1v1d4j94scaseqgcyzr0ha5dxa9rx6ppnfbndck971ac0"
.from_base32::<Digest224>()
.from_base32::<U224>()
.is_some());
}
}
14 changes: 14 additions & 0 deletions blockset/src/u256.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::u128::{to_u32x4, u32x4_add};

pub type U256 = [u128; 2];

#[inline(always)]
pub const fn u32x8_add(&[a0, a1]: &U256, &[b0, b1]: &U256) -> U256 {
[u32x4_add(a0, b0), u32x4_add(a1, b1)]
}

pub const fn to_u32x8([a, b]: &U256) -> [u32; 8] {
let [a0, a1, a2, a3] = to_u32x4(*a);
let [b0, b1, b2, b3] = to_u32x4(*b);
[a0, a1, a2, a3, b0, b1, b2, b3]
}
8 changes: 0 additions & 8 deletions blockset/src/u32x8.rs

This file was deleted.

File renamed without changes.

0 comments on commit 5eaecd6

Please sign in to comment.