Skip to content

Commit

Permalink
Fix naming for std.endian read/write methods
Browse files Browse the repository at this point in the history
The methods write_u32 and read_u32 don't actually care about the sign
and produce values as an Int, so they should be called write_i32 and
read_i32 respectively.

Changelog: changed
  • Loading branch information
yorickpeterse committed Jul 25, 2024
1 parent 9d7e5fe commit c6a18e2
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 114 deletions.
84 changes: 42 additions & 42 deletions std/src/std/crypto/chacha.inko
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,30 @@ fn pub hchacha20(key: ref ByteArray, nonce: ref ByteArray) -> ByteArray {
0x3320646E,
0x79622D32,
0x6B206574,
little.read_u32(from: key, at: 0),
little.read_u32(from: key, at: 4),
little.read_u32(from: key, at: 8),
little.read_u32(from: key, at: 12),
little.read_u32(from: key, at: 16),
little.read_u32(from: key, at: 20),
little.read_u32(from: key, at: 24),
little.read_u32(from: key, at: 28),
little.read_u32(from: nonce, at: 0),
little.read_u32(from: nonce, at: 4),
little.read_u32(from: nonce, at: 8),
little.read_u32(from: nonce, at: 12),
little.read_i32(from: key, at: 0),
little.read_i32(from: key, at: 4),
little.read_i32(from: key, at: 8),
little.read_i32(from: key, at: 12),
little.read_i32(from: key, at: 16),
little.read_i32(from: key, at: 20),
little.read_i32(from: key, at: 24),
little.read_i32(from: key, at: 28),
little.read_i32(from: nonce, at: 0),
little.read_i32(from: nonce, at: 4),
little.read_i32(from: nonce, at: 8),
little.read_i32(from: nonce, at: 12),
],
)

matrix.perform_rounds
little.write_u32(matrix.words.get(0), into: out, at: 0)
little.write_u32(matrix.words.get(1), into: out, at: 4)
little.write_u32(matrix.words.get(2), into: out, at: 8)
little.write_u32(matrix.words.get(3), into: out, at: 12)
little.write_u32(matrix.words.get(12), into: out, at: 16)
little.write_u32(matrix.words.get(13), into: out, at: 20)
little.write_u32(matrix.words.get(14), into: out, at: 24)
little.write_u32(matrix.words.get(15), into: out, at: 28)
little.write_i32(matrix.words.get(0), into: out, at: 0)
little.write_i32(matrix.words.get(1), into: out, at: 4)
little.write_i32(matrix.words.get(2), into: out, at: 8)
little.write_i32(matrix.words.get(3), into: out, at: 12)
little.write_i32(matrix.words.get(12), into: out, at: 16)
little.write_i32(matrix.words.get(13), into: out, at: 20)
little.write_i32(matrix.words.get(14), into: out, at: 24)
little.write_i32(matrix.words.get(15), into: out, at: 28)
out
}

Expand Down Expand Up @@ -254,18 +254,18 @@ class pub ChaCha20 {
0x3320646E,
0x79622D32,
0x6B206574,
little.read_u32(from: key, at: 0),
little.read_u32(from: key, at: 4),
little.read_u32(from: key, at: 8),
little.read_u32(from: key, at: 12),
little.read_u32(from: key, at: 16),
little.read_u32(from: key, at: 20),
little.read_u32(from: key, at: 24),
little.read_u32(from: key, at: 28),
little.read_i32(from: key, at: 0),
little.read_i32(from: key, at: 4),
little.read_i32(from: key, at: 8),
little.read_i32(from: key, at: 12),
little.read_i32(from: key, at: 16),
little.read_i32(from: key, at: 20),
little.read_i32(from: key, at: 24),
little.read_i32(from: key, at: 28),
DEFAULT_COUNTER,
little.read_u32(from: nonce, at: 0),
little.read_u32(from: nonce, at: 4),
little.read_u32(from: nonce, at: 8),
little.read_i32(from: nonce, at: 0),
little.read_i32(from: nonce, at: 4),
little.read_i32(from: nonce, at: 8),
],
),
)
Expand Down Expand Up @@ -297,7 +297,7 @@ class pub ChaCha20 {
tmp.perform_rounds
MATRIX_SIZE.times(fn (i) {
tmp.set(i, to_u32(tmp.get(i).wrapping_add(@matrix.get(i))))
little.write_u32(tmp.get(i), into: buf, at: i * 4)
little.write_i32(tmp.get(i), into: buf, at: i * 4)
})

# This in itself can't overflow, as the Int type is a 64-bits signed
Expand Down Expand Up @@ -404,18 +404,18 @@ class pub XChaCha20 {
0x3320646E,
0x79622D32,
0x6B206574,
little.read_u32(from: sub_key, at: 0),
little.read_u32(from: sub_key, at: 4),
little.read_u32(from: sub_key, at: 8),
little.read_u32(from: sub_key, at: 12),
little.read_u32(from: sub_key, at: 16),
little.read_u32(from: sub_key, at: 20),
little.read_u32(from: sub_key, at: 24),
little.read_u32(from: sub_key, at: 28),
little.read_i32(from: sub_key, at: 0),
little.read_i32(from: sub_key, at: 4),
little.read_i32(from: sub_key, at: 8),
little.read_i32(from: sub_key, at: 12),
little.read_i32(from: sub_key, at: 16),
little.read_i32(from: sub_key, at: 20),
little.read_i32(from: sub_key, at: 24),
little.read_i32(from: sub_key, at: 28),
DEFAULT_COUNTER,
0,
little.read_u32(from: nonce, at: 16),
little.read_u32(from: nonce, at: 20),
little.read_i32(from: nonce, at: 16),
little.read_i32(from: nonce, at: 20),
],
),
),
Expand Down
8 changes: 4 additions & 4 deletions std/src/std/crypto/hash.inko
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class pub Block {
}

# Reads an unsigned 32-bits little-endian integer from the given index.
fn pub read_u32_le(at: Int) -> Int {
little.read_u32(@bytes, at)
fn pub read_i32_le(at: Int) -> Int {
little.read_i32(@bytes, at)
}

# Reads an unsigned 32-bits big-endian integer from the given index.
fn pub read_u32_be(at: Int) -> Int {
big.read_u32(@bytes, at)
fn pub read_i32_be(at: Int) -> Int {
big.read_i32(@bytes, at)
}

# Reads a signed 64-bits big-endian integer from the given index.
Expand Down
10 changes: 5 additions & 5 deletions std/src/std/crypto/md5.inko
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class pub Md5 {
fn mut compress {
let words = @words

16.times(fn (i) { words.set(i, @block.read_u32_le(i * 4)) })
16.times(fn (i) { words.set(i, @block.read_i32_le(i * 4)) })

let mut a = @a
let mut b = @b
Expand Down Expand Up @@ -174,10 +174,10 @@ impl Hasher for Md5 {

let out = ByteArray.filled(with: 0, times: 16)

little.write_u32(@a, into: out, at: 0)
little.write_u32(@b, into: out, at: 4)
little.write_u32(@c, into: out, at: 8)
little.write_u32(@d, into: out, at: 12)
little.write_i32(@a, into: out, at: 0)
little.write_i32(@b, into: out, at: 4)
little.write_i32(@c, into: out, at: 8)
little.write_i32(@d, into: out, at: 12)
Hash.new(out)
}
}
32 changes: 16 additions & 16 deletions std/src/std/crypto/poly1305.inko
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ class pub Poly1305 {

Poly1305(
block: Block.new(BLOCK_SIZE),
r0: little.read_u32(from: key, at: 0) & 0x0FFFFFFF,
r1: little.read_u32(from: key, at: 4) & 0x0FFFFFFC,
r2: little.read_u32(from: key, at: 8) & 0x0FFFFFFC,
r3: little.read_u32(from: key, at: 12) & 0x0FFFFFFC,
pad0: little.read_u32(from: key, at: 16),
pad1: little.read_u32(from: key, at: 20),
pad2: little.read_u32(from: key, at: 24),
pad3: little.read_u32(from: key, at: 28),
r0: little.read_i32(from: key, at: 0) & 0x0FFFFFFF,
r1: little.read_i32(from: key, at: 4) & 0x0FFFFFFC,
r2: little.read_i32(from: key, at: 8) & 0x0FFFFFFC,
r3: little.read_i32(from: key, at: 12) & 0x0FFFFFFC,
pad0: little.read_i32(from: key, at: 16),
pad1: little.read_i32(from: key, at: 20),
pad2: little.read_i32(from: key, at: 24),
pad3: little.read_i32(from: key, at: 28),
h0: 0,
h1: 0,
h2: 0,
Expand All @@ -106,10 +106,10 @@ class pub Poly1305 {
}

fn mut compress(final: Bool) {
let mut s0 = @h0.wrapping_add(@block.read_u32_le(at: 0))
let mut s1 = @h1.wrapping_add(@block.read_u32_le(at: 4))
let mut s2 = @h2.wrapping_add(@block.read_u32_le(at: 8))
let mut s3 = @h3.wrapping_add(@block.read_u32_le(at: 12))
let mut s0 = @h0.wrapping_add(@block.read_i32_le(at: 0))
let mut s1 = @h1.wrapping_add(@block.read_i32_le(at: 4))
let mut s2 = @h2.wrapping_add(@block.read_i32_le(at: 8))
let mut s3 = @h3.wrapping_add(@block.read_i32_le(at: 12))
let mut s4 = @h4.wrapping_add(if final { 0 } else { 1 })

let r0 = @r0
Expand Down Expand Up @@ -193,19 +193,19 @@ impl Hasher for Poly1305 {
let out = ByteArray.filled(with: 0, times: TAG_SIZE)

c = c.wrapping_add(@h0) + @pad0
little.write_u32(c, into: out, at: 0)
little.write_i32(c, into: out, at: 0)
c = c >>> 32

c = c.wrapping_add(@h1) + @pad1
little.write_u32(c, into: out, at: 4)
little.write_i32(c, into: out, at: 4)
c = c >>> 32

c = c.wrapping_add(@h2) + @pad2
little.write_u32(c, into: out, at: 8)
little.write_i32(c, into: out, at: 8)
c = c >>> 32

c = c.wrapping_add(@h3) + @pad3
little.write_u32(c, into: out, at: 12)
little.write_i32(c, into: out, at: 12)
c = c >>> 32

Hash.new(out)
Expand Down
12 changes: 6 additions & 6 deletions std/src/std/crypto/sha1.inko
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class pub Sha1 {
fn mut compress {
let words = @words

0.until(16).iter.each(fn (i) { words.set(i, @block.read_u32_be(i * 4)) })
0.until(16).iter.each(fn (i) { words.set(i, @block.read_i32_be(i * 4)) })

16.until(80).iter.each(fn (i) {
words.set(
Expand Down Expand Up @@ -187,11 +187,11 @@ impl Hasher for Sha1 {

let out = ByteArray.filled(with: 0, times: 20)

big.write_u32(@a, into: out, at: 0)
big.write_u32(@b, into: out, at: 4)
big.write_u32(@c, into: out, at: 8)
big.write_u32(@d, into: out, at: 12)
big.write_u32(@e, into: out, at: 16)
big.write_i32(@a, into: out, at: 0)
big.write_i32(@b, into: out, at: 4)
big.write_i32(@c, into: out, at: 8)
big.write_i32(@d, into: out, at: 12)
big.write_i32(@e, into: out, at: 16)
Hash.new(out)
}
}
18 changes: 9 additions & 9 deletions std/src/std/crypto/sha2.inko
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class pub Sha256 {
fn mut compress {
let words = @words

0.until(16).iter.each(fn (i) { words.set(i, @block.read_u32_be(i * 4)) })
0.until(16).iter.each(fn (i) { words.set(i, @block.read_i32_be(i * 4)) })

16.until(64).iter.each(fn (i) {
let w15 = words.get(i - 15)
Expand Down Expand Up @@ -222,14 +222,14 @@ impl Hasher for Sha256 {

let out = ByteArray.filled(with: 0, times: 32)

big.write_u32(@a, into: out, at: 0)
big.write_u32(@b, into: out, at: 4)
big.write_u32(@c, into: out, at: 8)
big.write_u32(@d, into: out, at: 12)
big.write_u32(@e, into: out, at: 16)
big.write_u32(@f, into: out, at: 20)
big.write_u32(@g, into: out, at: 24)
big.write_u32(@h, into: out, at: 28)
big.write_i32(@a, into: out, at: 0)
big.write_i32(@b, into: out, at: 4)
big.write_i32(@c, into: out, at: 8)
big.write_i32(@d, into: out, at: 12)
big.write_i32(@e, into: out, at: 16)
big.write_i32(@f, into: out, at: 20)
big.write_i32(@g, into: out, at: 24)
big.write_i32(@h, into: out, at: 28)
Hash.new(out)
}
}
Expand Down
10 changes: 5 additions & 5 deletions std/src/std/endian/big.inko
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#
# let bytes = ByteArray.filled(with: 0, times: 4)
#
# big.write_u32(123456789, into: bytes, at: 0)
# big.write_i32(123456789, into: bytes, at: 0)
# bytes # => ByteArray.from_array([7, 91, 205, 21])
# ```
fn pub write_u32(value: Int, into: mut ByteArray, at: Int) {
fn pub write_i32(value: Int, into: mut ByteArray, at: Int) {
into.set(at, value >> 24)
into.set(at + 1, value >> 16)
into.set(at + 2, value >> 8)
Expand Down Expand Up @@ -61,10 +61,10 @@ fn pub write_i64(value: Int, into: mut ByteArray, at: Int) {
#
# let bytes = ByteArray.filled(with: 0, times: 4)
#
# big.write_u32(123456789, into: bytes, at: 0)
# big.read_u32(from: bytes, at: 0) # => 123456789
# big.write_i32(123456789, into: bytes, at: 0)
# big.read_i32(from: bytes, at: 0) # => 123456789
# ```
fn pub read_u32(from: ref ByteArray, at: Int) -> Int {
fn pub read_i32(from: ref ByteArray, at: Int) -> Int {
(from.get(at) << 24)
| (from.get(at + 1) << 16)
| (from.get(at + 2) << 8)
Expand Down
10 changes: 5 additions & 5 deletions std/src/std/endian/little.inko
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#
# let bytes = ByteArray.filled(with: 0, times: 4)
#
# little.write_u32(123456789, into: bytes, at: 0)
# little.write_i32(123456789, into: bytes, at: 0)
# bytes # => ByteArray.from_array([21, 205, 91, 7])
# ```
fn pub write_u32(value: Int, into: mut ByteArray, at: Int) {
fn pub write_i32(value: Int, into: mut ByteArray, at: Int) {
into.set(at, value)
into.set(at + 1, value >> 8)
into.set(at + 2, value >> 16)
Expand Down Expand Up @@ -61,10 +61,10 @@ fn pub write_i64(value: Int, into: mut ByteArray, at: Int) {
#
# let bytes = ByteArray.filled(with: 0, times: 4)
#
# little.write_u32(123456789, into: bytes, at: 0)
# little.read_u32(from: bytes, at: 0) # => 123456789
# little.write_i32(123456789, into: bytes, at: 0)
# little.read_i32(from: bytes, at: 0) # => 123456789
# ```
fn pub read_u32(from: ref ByteArray, at: Int) -> Int {
fn pub read_i32(from: ref ByteArray, at: Int) -> Int {
(from.get(at + 3) << 24)
| (from.get(at + 2) << 16)
| (from.get(at + 1) << 8)
Expand Down
12 changes: 6 additions & 6 deletions std/test/std/crypto/test_hash.inko
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import std.fmt (fmt)
import std.test (Tests)

fn pub tests(t: mut Tests) {
t.test('Block.read_u32_le', fn (t) {
t.test('Block.read_i32_le', fn (t) {
let block = Block.new(4)
let bytes = ByteArray.filled(with: 0, times: 4)

little.write_u32(123, into: bytes, at: 0)
little.write_i32(123, into: bytes, at: 0)
block.write_bytes(bytes, fn {})

t.equal(block.read_u32_le(0), 123)
t.equal(block.read_i32_le(0), 123)
})

t.test('Block.read_u32_be', fn (t) {
t.test('Block.read_i32_be', fn (t) {
let block = Block.new(4)
let bytes = ByteArray.filled(with: 0, times: 4)

big.write_u32(123, into: bytes, at: 0)
big.write_i32(123, into: bytes, at: 0)
block.write_bytes(bytes, fn {})

t.equal(block.read_u32_be(0), 123)
t.equal(block.read_i32_be(0), 123)
})

t.test('Block.write_bytes', fn (t) {
Expand Down
Loading

0 comments on commit c6a18e2

Please sign in to comment.