Debian Codegraph @

internal/montgomery/montgomery.go

raw symbols SCIP
1 // Copyright (c) 2021 Oasis Labs Inc. All rights reserved.
2 // Copyright (c) 2021 Yawning Angel. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Package montgomery provides helper routines for dealing with curve25519
32 // montgomery points.
33 package montgomery
34
35 import (
36 "encoding/binary"
37
38 "filippo.io/edwards25519"
39 "filippo.io/edwards25519/field"
40 )
41
42 var (
43 ZERO = new(field.Element).Zero()
44 ONE = new(field.Element).One()
45 TWO = new(field.Element).Add(ONE, ONE)
46
47 A = mustFeFromUint64(486662)
48 A_SQUARED = mustFeFromUint64(486662 * 486662)
49
50 SQRT_M1 = mustFeFromBytes([]byte{
51 0xb0, 0xa0, 0x0e, 0x4a, 0x27, 0x1b, 0xee, 0xc4, 0x78, 0xe4, 0x2f, 0xad, 0x06, 0x18, 0x43, 0x2f,
52 0xa7, 0xd7, 0xfb, 0x3d, 0x99, 0x00, 0x4d, 0x2b, 0x0b, 0xdf, 0xc1, 0x4f, 0x80, 0x24, 0x83, 0x2b,
53 })
54
55 NEG_A = mustFeFromBytes([]byte{
56 0xe7, 0x92, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
58 })
59
60 SQRT_NEG_A_PLUS_TWO = mustFeFromBytes([]byte{
61 0x06, 0x7e, 0x45, 0xff, 0xaa, 0x04, 0x6e, 0xcc, 0x82, 0x1a, 0x7d, 0x4b, 0xd1, 0xd3, 0xa1, 0xc5,
62 0x7e, 0x4f, 0xfc, 0x03, 0xdc, 0x08, 0x7b, 0xd2, 0xbb, 0x06, 0xa0, 0x60, 0xf4, 0xed, 0x26, 0x0f,
63 })
64
65 U_FACTOR = mustFeFromBytes([]byte{
66 0x8d, 0xbe, 0xe2, 0x6b, 0xb1, 0xc9, 0x23, 0x76, 0x0e, 0x37, 0xa0, 0xa5, 0xf2, 0xcf, 0x79, 0xa1,
67 0xb1, 0x50, 0x08, 0x84, 0xcd, 0xfe, 0x65, 0xa9, 0xe9, 0x41, 0x7c, 0x60, 0xff, 0xb6, 0xf9, 0x28,
68 })
69
70 V_FACTOR = mustFeFromBytes([]byte{
71 0x3e, 0x5f, 0xf1, 0xb5, 0xd8, 0xe4, 0x11, 0x3b, 0x87, 0x1b, 0xd0, 0x52, 0xf9, 0xe7, 0xbc, 0xd0,
72 0x58, 0x28, 0x04, 0xc2, 0x66, 0xff, 0xb2, 0xd4, 0xf4, 0x20, 0x3e, 0xb0, 0x7f, 0xdb, 0x7c, 0x54,
73 })
74 )
75
76 func mustFeFromBytes(b []byte) *field.Element {
77 fe, err := new(field.Element).SetBytes(b)
78 if err != nil {
79 panic("h2c: failed to deserialize constant: " + err.Error())
80 }
81 return fe
82 }
83
84 func mustFeFromUint64(x uint64) *field.Element {
85 var b [32]byte
86 binary.LittleEndian.PutUint64(b[:], x)
87 return mustFeFromBytes(b[:])
88 }
89
90 func feIsZero(fe *field.Element) int {
91 return fe.Equal(ZERO)
92 }
93
94 func FromEdwardsPoint(p *edwards25519.Point) (*field.Element, *field.Element) {
95 xExt, yExt, zExt, _ := p.ExtendedCoordinates()
96
97 // Convert from extended (x, y, z, t) coordiantes to x, y.
98 zInv := new(field.Element).Invert(zExt)
99 x := new(field.Element).Multiply(xExt, zInv)
100 y := new(field.Element).Multiply(yExt, zInv)
101
102 // Per RFC 7748: (u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)
103
104 onePlusY := new(field.Element).Add(ONE, y)
105 oneMinusY := new(field.Element).Subtract(ONE, y)
106 u := new(field.Element).Invert(oneMinusY)
107 u.Multiply(onePlusY, u)
108
109 v := new(field.Element).Invert(x)
110 v.Multiply(v, SQRT_NEG_A_PLUS_TWO)
111 v.Multiply(v, u)
112
113 // If y == 1, 1/(1-y) = 0, (u, v) = (0, 0) (No adjustment needed)
114 // If x == 0, sqrt(-486664)*u/x = 0, (u, v) = (u, 0)
115 u.Select(ZERO, u, feIsZero(x))
116
117 return u, v
118 }
119
120 func ToEdwardsPoint(u, v *field.Element) *edwards25519.Point {
121 // Per RFC 7748: (x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))
122
123 x := new(field.Element).Invert(v)
124 x.Multiply(x, u)
125 x.Multiply(x, SQRT_NEG_A_PLUS_TWO)
126
127 uMinusOne := new(field.Element).Subtract(u, ONE)
128 uPlusOne := new(field.Element).Add(u, ONE)
129 uPlusOneIsZero := feIsZero(uPlusOne)
130
131 uPlusOne.Invert(uPlusOne)
132 y := new(field.Element).Multiply(uMinusOne, uPlusOne)
133
134 // This mapping is undefined when t == 0 or s == -1, i.e., when the
135 // denominator of either of the above rational functions is zero.
136 // Implementations MUST detect exceptional cases and return the value
137 // (v, w) = (0, 1), which is the identity point on all twisted Edwards
138 // curves.
139 resultUndefined := feIsZero(v) | uPlusOneIsZero
140 x.Select(ZERO, x, resultUndefined)
141 y.Select(ONE, y, resultUndefined)
142
143 // Convert from Edwards (x, y) to extended (x, y, z, t) coordinates.
144 return NewEdwardsFromXY(x, y)
145 }
146
147 func NewEdwardsFromXY(x, y *field.Element) *edwards25519.Point {
148 // Yeah yeah yeah, no where better to put this. :(
149 Z := new(field.Element).One()
150 T := new(field.Element).Multiply(x, y)
151
152 p, err := new(edwards25519.Point).SetExtendedCoordinates(x, y, Z, T)
153 if err != nil {
154 panic("h2c: failed to create edwards point from x, y: " + err.Error())
155 }
156 return p
157 }