@@ -15,38 +15,44 @@ import (
1515 "filippo.io/edwards25519/field"
1616)
1717
18- const p25519 = "57896044618658097711785492504343953926634992332820282019728792003956564819949"
18+ const (
19+ p25519 = "57896044618658097711785492504343953926634992332820282019728792003956564819949" // 2^255 - 19
20+ p252 = "7237005577332262213973186563042994240857116359379907606001950938285454250989" // 2^252 + 27742317777372353535851937790883648493
21+ )
1922
20- var prime , _ = new (big.Int ).SetString (p25519 , 10 )
23+ var (
24+ prime , _ = new (big.Int ).SetString (p25519 , 10 )
25+ subPrime , _ = new (big.Int ).SetString (p252 , 10 )
26+ )
2127
22- // HashToScalarXMD hashes the input and dst to the field and returns a uniformly distributed byte array, that can
28+ // HashToField25519XMD hashes the input and dst to the field and returns a uniformly distributed byte array, that can
2329// be used as a scalar.
24- func HashToScalarXMD (id crypto.Hash , input , dst []byte , length int ) []byte {
30+ func HashToField25519XMD (id crypto.Hash , input , dst []byte , length int ) []byte {
2531 l := 48
2632 expLength := 1 * 1 * l // 1 element * ext * security length
2733 uniform := ExpandXMD (id , input , dst , expLength )
2834
29- return innerh2f (uniform , length )
35+ return reduce (uniform , length )
3036}
3137
32- // HashToFieldXMD hashes the input and dst to the field and returns two field elements destined to be mapped to
38+ // doubleHashToField25519XMD hashes the input and dst to the field and returns two field elements destined to be mapped to
3339// points on the destination curve.
34- func HashToFieldXMD (id crypto.Hash , input , dst []byte , length int ) (u , v * field.Element ) {
40+ func doubleHashToField25519XMD (id crypto.Hash , input , dst []byte , length int ) (u , v * field.Element ) {
3541 l := 48
3642 expLength := 2 * 1 * l // 2 elements * ext * security length
3743 uniform := ExpandXMD (id , input , dst , expLength )
38- u = innerh2fe ( uniform [:l ], length )
39- v = innerh2fe ( uniform [l :2 * l ], length )
44+ u = element ( reduce ( uniform [:l ], length ) )
45+ v = element ( reduce ( uniform [l :2 * l ], length ) )
4046
4147 return
4248}
4349
44- func innerh2f (input []byte , length int ) []byte {
50+ func reduce (input []byte , length int ) []byte {
4551 /*
4652 Interpret the input as an integer of the field, and reduce it modulo the prime.
4753 */
4854 i := new (big.Int ).SetBytes (input )
49- i .Mod (i , prime )
55+ i .Mod (i , subPrime )
5056
5157 // If necessary, build a buffer of right size so it gets correctly interpreted.
5258 b := i .Bytes ()
@@ -59,10 +65,8 @@ func innerh2f(input []byte, length int) []byte {
5965 return reverse (b )
6066}
6167
62- func innerh2fe (input []byte , length int ) * field.Element {
63- b := innerh2f (input , length )
64-
65- e , err := new (field.Element ).SetBytes (b )
68+ func element (input []byte ) * field.Element {
69+ e , err := new (field.Element ).SetBytes (input )
6670 if err != nil {
6771 panic (err )
6872 }
0 commit comments