Skip to content

Commit

Permalink
Merge pull request #6 from bytemare/revert-5-use-bigmod
Browse files Browse the repository at this point in the history
Revert "Use bigmod instead of math/big"
  • Loading branch information
bytemare committed Apr 3, 2023
2 parents 4aabe20 + 2131190 commit 6e7072e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module github.com/bytemare/hash2curve

go 1.20

require (
filippo.io/bigmod v0.0.1
github.com/bytemare/hash v0.1.5
)
require github.com/bytemare/hash v0.1.5

require (
golang.org/x/crypto v0.7.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
filippo.io/bigmod v0.0.1 h1:OaEqDr3gEbofpnHbGqZweSL/bLMhy1pb54puiCDeuOA=
filippo.io/bigmod v0.0.1/go.mod h1:KyzqAbH7bRH6MOuOF1TPfUjvLoi0mRF2bIyD2ouRNQI=
github.com/bytemare/hash v0.1.5 h1:VW+X1YQ2b3chjRFHkRUnO42uclsQjXimdBCPOgIobR4=
github.com/bytemare/hash v0.1.5/go.mod h1:+QmWXTky/2b63ngqM5IYezGydn9UTFDhpX7mLYwYxCA=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
Expand Down
28 changes: 8 additions & 20 deletions h2f.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ package hash2curve

import (
"crypto"
"math/big"

"filippo.io/bigmod"
"github.com/bytemare/hash"
)

// HashToFieldXOF hashes the input with the domain separation tag (dst) to an integer under modulo, using an
// extensible output function (e.g. SHAKE).
func HashToFieldXOF(
id hash.Extendable,
input, dst []byte,
count, ext, securityLength int,
modulo *bigmod.Modulus,
) []*bigmod.Nat {
func HashToFieldXOF(id hash.Extendable, input, dst []byte, count, ext, securityLength int, modulo *big.Int) []*big.Int {
expLength := count * ext * securityLength // elements * ext * security length
uniform := ExpandXOF(id, input, dst, expLength)

Expand All @@ -32,20 +27,15 @@ func HashToFieldXOF(

// HashToFieldXMD hashes the input with the domain separation tag (dst) to an integer under modulo, using an
// merkle-damgard based expander (e.g. SHA256).
func HashToFieldXMD(
id crypto.Hash,
input, dst []byte,
count, ext, securityLength int,
modulo *bigmod.Modulus,
) []*bigmod.Nat {
func HashToFieldXMD(id crypto.Hash, input, dst []byte, count, ext, securityLength int, modulo *big.Int) []*big.Int {
expLength := count * ext * securityLength // elements * ext * security length
uniform := ExpandXMD(id, input, dst, expLength)

return reduceUniform(uniform, count, securityLength, modulo)
}

func reduceUniform(uniform []byte, count, securityLength int, modulo *bigmod.Modulus) []*bigmod.Nat {
res := make([]*bigmod.Nat, count)
func reduceUniform(uniform []byte, count, securityLength int, modulo *big.Int) []*big.Int {
res := make([]*big.Int, count)

for i := 0; i < count; i++ {
offset := i * securityLength
Expand All @@ -55,14 +45,12 @@ func reduceUniform(uniform []byte, count, securityLength int, modulo *bigmod.Mod
return res
}

func reduce(input []byte, modulo *bigmod.Modulus) *bigmod.Nat {
func reduce(input []byte, modulo *big.Int) *big.Int {
/*
Interpret the input as a big-endian encoded unsigned integer of the field, and reduce it modulo the prime.
*/
i, err := bigmod.NewNat().SetBytes(input, modulo)
if err != nil {
panic(err)
}
i := new(big.Int).SetBytes(input)
i.Mod(i, modulo)

return i
}

0 comments on commit 6e7072e

Please sign in to comment.