Skip to content

Commit

Permalink
Merge pull request #149 from mmcloughlin/asm-module
Browse files Browse the repository at this point in the history
Move avo assembly generator to separate `asm` module
  • Loading branch information
pjbgf authored Jan 21, 2025
2 parents 6f02425 + 2a026ef commit 05a0266
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 48 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ build-nocgo:
cross-build: build-arm build-arm64 build-nocgo

generate:
go run sha1cdblock_amd64_asm.go -out sha1cdblock_amd64.s
sed -i 's;&\samd64;&\n// +build !noasm,gc,amd64;g' sha1cdblock_amd64.s

cd ubc && go run ubc_amd64_asm.go -out ubc_amd64.s
sed -i 's;&\samd64;&\n// +build !noasm,gc,amd64;g' ubc/ubc_amd64.s
go generate -x ./...

verify: generate
git diff --exit-code
Expand Down
29 changes: 17 additions & 12 deletions sha1cdblock_amd64_asm.go → asm/asm.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
//go:build ignore
// +build ignore

package main

import (
. "github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/buildtags"
. "github.com/mmcloughlin/avo/operand"
. "github.com/mmcloughlin/avo/reg"
shared "github.com/pjbgf/sha1cd/internal"
)

//go:generate go run sha1cdblock_amd64_asm.go -out sha1cdblock_amd64.s
const (
// Constants for the SHA-1 hash function.
RoundConst0 = 0x5A827999
RoundConst1 = 0x6ED9EBA1
RoundConst2 = 0x8F1BBCDC
RoundConst3 = 0xCA62C1D6

// SHA1 processes the input data in chunks. Each chunk contains 64 bytes.
Chunk = 64
)

func main() {
Constraint(buildtags.Not("noasm").ToConstraint())
Expand Down Expand Up @@ -61,7 +66,7 @@ func main() {
}

// Store message values on the stack.
w := AllocLocal(shared.Chunk)
w := AllocLocal(Chunk)
W := func(r int) Mem { return w.Offset((r % 16) * 4) }

Comment("len(p) >= chunk")
Expand Down Expand Up @@ -161,39 +166,39 @@ func main() {
Commentf("ROUND1(%d)", index)
LOAD(index)
FUNC1(a, b, c, d, e)
MIX(a, b, c, d, e, shared.K0)
MIX(a, b, c, d, e, RoundConst0)
LOADM1(index)
}

ROUND1x := func(a, b, c, d, e GPVirtual, index int) {
Commentf("ROUND1x(%d)", index)
SHUFFLE(index)
FUNC1(a, b, c, d, e)
MIX(a, b, c, d, e, shared.K0)
MIX(a, b, c, d, e, RoundConst0)
LOADM1(index)
}

ROUND2 := func(a, b, c, d, e GPVirtual, index int) {
Commentf("ROUND2(%d)", index)
SHUFFLE(index)
FUNC2(a, b, c, d, e)
MIX(a, b, c, d, e, shared.K1)
MIX(a, b, c, d, e, RoundConst1)
LOADM1(index)
}

ROUND3 := func(a, b, c, d, e GPVirtual, index int) {
Commentf("ROUND3(%d)", index)
SHUFFLE(index)
FUNC3(a, b, c, d, e)
MIX(a, b, c, d, e, shared.K2)
MIX(a, b, c, d, e, RoundConst2)
LOADM1(index)
}

ROUND4 := func(a, b, c, d, e GPVirtual, index int) {
Commentf("ROUND4(%d)", index)
SHUFFLE(index)
FUNC4(a, b, c, d, e)
MIX(a, b, c, d, e, shared.K3)
MIX(a, b, c, d, e, RoundConst3)
LOADM1(index)
}

Expand Down Expand Up @@ -297,7 +302,7 @@ func main() {
ADDL(r, hash[i])
}

ADDQ(I8(shared.Chunk), p_base)
ADDQ(I8(Chunk), p_base)
CMPQ(p_base, di64)
JB(LabelRef("loop"))

Expand Down
15 changes: 15 additions & 0 deletions asm/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/pjbgf/sha1cd/asm

go 1.21

require (
github.com/mmcloughlin/avo v0.6.0
github.com/pjbgf/sha1cd v0.0.0-local
)

require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
)

replace github.com/pjbgf/sha1cd => ..
8 changes: 8 additions & 0 deletions asm/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
module github.com/pjbgf/sha1cd

go 1.21

require github.com/mmcloughlin/avo v0.6.0

require (
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/tools v0.20.0 // indirect
)
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
3 changes: 2 additions & 1 deletion sha1cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (
"errors"
"hash"

_ "github.com/mmcloughlin/avo/build"
shared "github.com/pjbgf/sha1cd/internal"
)

//go:generate go run -C asm . -out ../sha1cdblock_amd64.s -pkg $GOPACKAGE

func init() {
crypto.RegisterHash(crypto.SHA1, New)
}
Expand Down
3 changes: 1 addition & 2 deletions sha1cdblock_amd64.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Code generated by command: go run sha1cdblock_amd64_asm.go -out sha1cdblock_amd64.s. DO NOT EDIT.
// Code generated by command: go run asm.go -out ../sha1cdblock_amd64.s -pkg sha1cd. DO NOT EDIT.

//go:build !noasm && gc && amd64
// +build !noasm,gc,amd64

#include "textflag.h"

Expand Down
5 changes: 0 additions & 5 deletions ubc/ubc_amd64_asm.go → ubc/asm/asm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build ignore
// +build ignore

package main

import (
Expand All @@ -10,8 +7,6 @@ import (
"github.com/pjbgf/sha1cd/ubc"
)

//go:generate go run ubc_amd64_asm.go -out ubc_amd64.s

const (
DvTypeOffset = 0
DvKOffset = 4
Expand Down
16 changes: 16 additions & 0 deletions ubc/asm/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/pjbgf/sha1cd/ubc/asm

go 1.21

require (
github.com/mmcloughlin/avo v0.6.0
github.com/pjbgf/sha1cd v0.0.0-local
)

require (
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/tools v0.20.0 // indirect
)

replace github.com/pjbgf/sha1cd => ../..
8 changes: 8 additions & 0 deletions ubc/asm/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
2 changes: 2 additions & 0 deletions ubc/doc.go → ubc/ubc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ubc package provides ways for SHA1 blocks to be checked for
// Unavoidable Bit Conditions that arise from crypto analysis attacks.
package ubc

//go:generate go run -C asm . -out ../ubc_amd64.s -pkg $GOPACKAGE
3 changes: 1 addition & 2 deletions ubc/ubc_amd64.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Code generated by command: go run ubc_amd64_asm.go -out ubc_amd64.s. DO NOT EDIT.
// Code generated by command: go run asm.go -out ../ubc_amd64.s -pkg ubc. DO NOT EDIT.

//go:build !noasm && gc && amd64
// +build !noasm,gc,amd64

#include "textflag.h"

Expand Down

0 comments on commit 05a0266

Please sign in to comment.