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

sign: Adding SLH-DSA signature #512

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Alternatively, look at the [Cloudflare Go](https://github.com/cloudflare/go/tree
[RFC-9496]: https://doi.org/10.17487/RFC9496
[RFC-9497]: https://doi.org/10.17487/RFC9497
[FIPS 202]: https://doi.org/10.6028/NIST.FIPS.202
[FIPS 205]: https://doi.org/10.6028/NIST.FIPS.205
[FIPS 186-5]: https://doi.org/10.6028/NIST.FIPS.186-5
[BLS12-381]: https://electriccoin.co/blog/new-snark-curve/
[ia.cr/2015/267]: https://ia.cr/2015/267
Expand Down Expand Up @@ -91,6 +92,7 @@ Alternatively, look at the [Cloudflare Go](https://github.com/cloudflare/go/tree
|:---:|

- [Dilithium](./sign/dilithium): modes 2, 3, 5 ([Dilithium](https://pq-crystals.org/dilithium/)).
- [SLH-DSA](./sign/slhdsa): twelve parameter sets, pure and pre-hash signing ([FIPS 205]).

### Zero-knowledge Proofs

Expand Down
25 changes: 25 additions & 0 deletions internal/conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/big"
"strings"

"golang.org/x/crypto/cryptobyte"
)

// BytesLe2Hex returns an hexadecimal string of a number stored in a
Expand Down Expand Up @@ -138,3 +140,26 @@ func BigInt2Uint64Le(z []uint64, x *big.Int) {
z[i] = 0
}
}

// MarshalBinary encodes a value into a byte array in a format readable by UnmarshalBinary.
func MarshalBinary(v cryptobyte.MarshalingValue) ([]byte, error) {
var b cryptobyte.Builder
b.AddValue(v)
return b.Bytes()
}

// A UnmarshalingValue decodes itself from a cryptobyte.String and advances the pointer.
// It reports whether the read was successful.
type UnmarshalingValue interface {
Unmarshal(*cryptobyte.String) bool
}

// UnmarshalBinary recovers a value from a byte array.
// It returns an error if the read was unsuccessful.
func UnmarshalBinary(v UnmarshalingValue, data []byte) (err error) {
s := cryptobyte.String(data)
if !v.Unmarshal(&s) || !s.Empty() {
err = fmt.Errorf("cannot read %T from input string", v)
}
return
}
25 changes: 25 additions & 0 deletions internal/test/test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package test

import (
"bytes"
"encoding"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -58,3 +60,26 @@ func CheckPanic(f func()) error {
f()
return hasPanicked
}

func CheckMarshal(
t *testing.T,
x, y interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
},
) {
t.Helper()

want, err := x.MarshalBinary()
CheckNoErr(t, err, fmt.Sprintf("cannot marshal %T = %v", x, x))

err = y.UnmarshalBinary(want)
CheckNoErr(t, err, fmt.Sprintf("cannot unmarshal %T from %x", y, want))

got, err := y.MarshalBinary()
CheckNoErr(t, err, fmt.Sprintf("cannot marshal %T = %v", y, y))

if !bytes.Equal(got, want) {
ReportError(t, got, want, x, y)
}
}
14 changes: 14 additions & 0 deletions sign/schemes/schemes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Ed448
// Ed25519-Dilithium2
// Ed448-Dilithium3
// SLH-DSA
package schemes

import (
Expand All @@ -16,13 +17,26 @@ import (
"github.com/cloudflare/circl/sign/ed448"
"github.com/cloudflare/circl/sign/eddilithium2"
"github.com/cloudflare/circl/sign/eddilithium3"
"github.com/cloudflare/circl/sign/slhdsa"
)

var allSchemes = [...]sign.Scheme{
ed25519.Scheme(),
ed448.Scheme(),
eddilithium2.Scheme(),
eddilithium3.Scheme(),
slhdsa.ParamIDSHA2Small128,
slhdsa.ParamIDSHAKESmall128,
slhdsa.ParamIDSHA2Fast128,
slhdsa.ParamIDSHAKEFast128,
slhdsa.ParamIDSHA2Small192,
slhdsa.ParamIDSHAKESmall192,
slhdsa.ParamIDSHA2Fast192,
slhdsa.ParamIDSHAKEFast192,
slhdsa.ParamIDSHA2Small256,
slhdsa.ParamIDSHAKESmall256,
slhdsa.ParamIDSHA2Fast256,
slhdsa.ParamIDSHAKEFast256,
}

var allSchemeNames map[string]sign.Scheme
Expand Down
12 changes: 12 additions & 0 deletions sign/schemes/schemes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ func Example() {
// Ed448
// Ed25519-Dilithium2
// Ed448-Dilithium3
// SLH-DSA-SHA2-128s
// SLH-DSA-SHAKE-128s
// SLH-DSA-SHA2-128f
// SLH-DSA-SHAKE-128f
// SLH-DSA-SHA2-192s
// SLH-DSA-SHAKE-192s
// SLH-DSA-SHA2-192f
// SLH-DSA-SHAKE-192f
// SLH-DSA-SHA2-256s
// SLH-DSA-SHAKE-256s
// SLH-DSA-SHA2-256f
// SLH-DSA-SHAKE-256f
}

func BenchmarkGenerateKeyPair(b *testing.B) {
Expand Down
Loading
Loading