Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add K12 as XOF #437

Merged
merged 1 commit into from May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -74,6 +74,7 @@ go get -u github.com/cloudflare/circl
#### XOF: eXtendable Output Functions
- [FIPS 202](https://doi.org/10.6028/NIST.FIPS.202): SHAKE128 and SHAKE256
- [BLAKE2X](https://www.blake2.net/blake2x.pdf): BLAKE2XB and BLAKE2XS
- [KangarooTwelve](https://keccak.team/kangarootwelve.html): KangarooTwelve

#### Zero-knowledge Proofs
- [Schnorr](./zk/dl): Prove knowledge of the Discrete Logarithm.
Expand Down
23 changes: 23 additions & 0 deletions xof/k12/k12.go
Expand Up @@ -79,6 +79,29 @@ func (s *State) Reset() {
s.chunk = 0
}

func (s *State) Clone() State {
stalk := s.stalk.Clone().(*sha3.State)
ret := State{
initialTodo: s.initialTodo,
stalk: *stalk,
context: s.context,
offset: s.offset,
chunk: s.chunk,
lanes: s.lanes,
}

if s.leaf != nil {
ret.leaf = s.leaf.Clone().(*sha3.State)
}

if s.buf != nil {
ret.buf = make([]byte, len(s.buf))
copy(ret.buf, s.buf)
}

return ret
}

func Draft10Sum(hash []byte, msg []byte, c []byte) {
// TODO Tweak number of lanes depending on the length of the message
s := NewDraft10(c)
Expand Down
13 changes: 13 additions & 0 deletions xof/xof.go
Expand Up @@ -10,6 +10,8 @@ import (
"io"

"github.com/cloudflare/circl/internal/sha3"
"github.com/cloudflare/circl/xof/k12"

"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
)
Expand Down Expand Up @@ -38,6 +40,7 @@ const (
SHAKE256
BLAKE2XB
BLAKE2XS
K12D10
)

func (x ID) New() XOF {
Expand All @@ -54,6 +57,9 @@ func (x ID) New() XOF {
case BLAKE2XS:
x, _ := blake2s.NewXOF(blake2s.OutputLengthUnknown, nil)
return blake2xs{x}
case K12D10:
x := k12.NewDraft10([]byte{})
return k12d10{&x}
default:
panic("crypto: requested unavailable XOF function")
}
Expand All @@ -70,3 +76,10 @@ func (s blake2xb) Clone() XOF { return blake2xb{s.XOF.Clone()} }
type blake2xs struct{ blake2s.XOF }

func (s blake2xs) Clone() XOF { return blake2xs{s.XOF.Clone()} }

type k12d10 struct{ *k12.State }

func (s k12d10) Clone() XOF {
x := s.State.Clone()
return k12d10{&x}
}
6 changes: 6 additions & 0 deletions xof/xof_test.go
Expand Up @@ -53,6 +53,12 @@ var allVectors = []vector{
out: "0650cde4df888a06eada0f0fecb3c17594304b4a03fdd678182f27db1238b174",
outLen: 32,
},
{
id: xof.K12D10,
in: "The quick brown fox jumps over the lazy dog",
out: "b4f249b4f77c58df170aa4d1723db1127d82f1d98d25ddda561ada459cd11a48",
outLen: 32,
},
}

func TestXof(t *testing.T) {
Expand Down