shasum: working for 0x0, 0x37, or 0x100 ROM bytes

This commit is contained in:
Zellyn Hunter 2016-09-28 22:43:49 -04:00
parent 049df4a55f
commit 37de1f5cac
2 changed files with 160 additions and 51 deletions

View File

@ -84,17 +84,25 @@ prbytes:
rts
main:
;; Test PRBYTES
;; lda #<kh0
;; sta SRC
;; lda #>kh0
;; sta SRC+1
;; lda #8
;; jsr prbytes
;; LDA #$8D
;; jsr COUT
;; Test shasum ""
lda #0
sta INPUT
lda #$ff
sta INPUT+1
lda #0
sta LENGTH+1
lda #0 ; da39a3ee5e6b4b0d3255bfef95601890afd80709
sta LENGTH
jsr shasum
;; Test shasum
; lda #$8d
; jsr COUT
+setSRC h0
lda #(h5-h0)
jsr prbytes
;; Test shasum ROM[:0x37]
lda #0
sta INPUT
lda #$ff
@ -102,12 +110,29 @@ main:
lda #0
sta LENGTH+1
lda #$37 ; 863ec5335a92b8289e4b1e5cbf646689fe81134b
lda #0 ; da39a3ee5e6b4b0d3255bfef95601890afd80709
sta LENGTH
jsr shasum
lda #$8d
jsr COUT
; lda #$8d
; jsr COUT
+setSRC h0
lda #(h5-h0)
jsr prbytes
;; Test shasum ROM[:0x100]
lda #0
sta INPUT
lda #$ff
sta INPUT+1
lda #1
sta LENGTH+1
lda #0 ; F0282F962EC20651431D385E813F6E0904F8F812
sta LENGTH
jsr shasum
; lda #$8d
; jsr COUT
+setSRC h0
lda #(h5-h0)
@ -298,6 +323,7 @@ kind1:
jsr and32
+setSRC d
jsr xor32
jmp common
kind2:
sty TMP1
@ -310,6 +336,7 @@ kind2:
jsr xor32
+setSRC b
jsr xor32
jmp common
kind3:
sty TMP1
@ -337,9 +364,11 @@ common:
+setSRC a
jsr cp32
jsr rol8
jsr ror1
jsr ror1
jsr ror1
+setSRC f
jsr add32
+setSRC e
@ -358,10 +387,6 @@ common:
sta SRC+1
jsr add32
;; Print out w[i]
lda #4
jsr prbytes
;; e = d
+setSRC d
+setDST e
@ -462,12 +487,16 @@ ror1:
lda (DST),y
ror
ldy #0
php
- lda (DST),y
plp
ror
php
sta (DST),y
iny
cpy #4
bne -
plp
rts
;; Xor SRC into DST. Burns a,y.
@ -522,17 +551,17 @@ or32:
;; Rotate DST right by 8 bits. Burns a,x,y.
rol8:
ldy #0
lda (SRC),y
lda (DST),y
tax
- iny
lda (SRC),y
lda (DST),y
dey
sta (SRC),y
sta (DST),y
iny
cpy #3
bne -
txa
sta (SRC),y
sta (DST),y
rts
!eof
@ -548,5 +577,5 @@ Needed arithmetic routines for sha1sum:
- [X] xor32
- [X] ROL1
- [X] ROR1
- [ ] ROL5 --> ROL8, (ROR1,ROR1,ROR1)
- [ ] ROL30 --> (ROR1,ROR1)
- [X] ROL5 --> ROL8, (ROR1,ROR1,ROR1)
- [X] ROL30 --> (ROR1,ROR1)

View File

@ -44,9 +44,13 @@ var rom = []byte{
}
func main() {
print40s(expand(pad(rom[:0x37])))
fmt.Printf("%x\n", sha1.Sum(rom[:0x37]))
fmt.Printf("%x\n", sha1.Sum(rom[:0]))
// print40s(tobytes(expand(pad(rom[:0x37]))))
fmt.Printf("%X\n", sha1.Sum(rom[:0]))
// fmt.Printf("%X\n", shasum(rom[:0]))
// fmt.Printf("%X\n", sha1.Sum(rom[:0x37]))
fmt.Printf("%X\n", shasum(rom[:0x37]))
fmt.Printf("%X\n", sha1.Sum(rom[:0x100]))
// fmt.Printf("%X\n", shasum(rom[:0x100]))
}
func print40s(in []byte) {
@ -74,7 +78,18 @@ func pad(in []byte) []byte {
return out
}
func expand(in []byte) []byte {
func tobytes(in []uint32) []byte {
out := make([]byte, len(in)*4)
for i, w := range in {
binary.BigEndian.PutUint32(out[i*4:], w)
}
return out
}
func expand(in []byte) []uint32 {
if len(in) != 64 {
panic(fmt.Sprintf("expand() expects 64-byte slices as input; got %d", len(in)))
}
var w [80]uint32
for i := 0; i < 16; i++ {
w[i] = binary.BigEndian.Uint32(in[i*4:])
@ -85,9 +100,74 @@ func expand(in []byte) []byte {
w[i] = w[i]<<1 + w[i]>>31
}
out := make([]byte, len(w)*4)
return w[:]
}
func shasum(in []byte) [20]byte {
h0 := uint32(0x67452301)
h1 := uint32(0xEFCDAB89)
h2 := uint32(0x98BADCFE)
h3 := uint32(0x10325476)
h4 := uint32(0xC3D2E1F0)
in2 := pad(in)
_ = in2
_ = h0
_ = h1
_ = h2
_ = h3
_ = h4
for j := 0; j < len(in2); j += 64 {
w := expand(in2[j : j+64])
if len(w) != 80 {
panic(fmt.Sprintf("expand should return 80 words; got %d", len(w)))
}
a := h0
b := h1
c := h2
d := h3
e := h4
var f, k uint32
for i, wi := range w {
binary.BigEndian.PutUint32(out[i*4:], wi)
switch {
case i <= 19:
f = d ^ (b & (c ^ d))
k = 0x5A827999
case i <= 39:
f = b ^ c ^ d
k = 0x6ED9EBA1
case i <= 59:
f = (b & c) | (d & (b | c))
k = 0x8F1BBCDC
default:
f = b ^ c ^ d
k = 0xCA62C1D6
}
return out
temp := ((a << 5) | (a >> 27)) + f + e + k + wi
e = d
d = c
c = (b << 30) | (b >> 2)
b = a
a = temp
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
}
var result [20]byte
binary.BigEndian.PutUint32(result[0:], h0)
binary.BigEndian.PutUint32(result[4:], h1)
binary.BigEndian.PutUint32(result[8:], h2)
binary.BigEndian.PutUint32(result[12:], h3)
binary.BigEndian.PutUint32(result[16:], h4)
return result
}