| 1 | package edwards25519 |
| 2 | |
| 3 | import ( |
| 4 | // Requires for FieldElement.[Set]BigInt(). Obviously not used for actual |
| 5 | // implementation, as operations on big.Ints are not constant-time. |
| 6 | "math/big" |
| 7 | |
| 8 | "encoding/binary" |
| 9 | ) |
| 10 | |
| 11 | // Set fe to i, the root of -1. Returns fe. |
| 12 | func (fe *FieldElement) SetI() *FieldElement { |
| 13 | copy(fe[:], feI[:]) |
| 14 | return fe |
| 15 | } |
| 16 | |
| 17 | // Set fe to 0. Returns fe. |
| 18 | func (fe *FieldElement) SetZero() *FieldElement { |
| 19 | copy(fe[:], feZero[:]) |
| 20 | return fe |
| 21 | } |
| 22 | |
| 23 | // Set fe to 1. Returns fe. |
| 24 | func (fe *FieldElement) SetOne() *FieldElement { |
| 25 | copy(fe[:], feOne[:]) |
| 26 | return fe |
| 27 | } |
| 28 | |
| 29 | // Sets fe to 2*a without normalizing. Returns fe. |
| 30 | func (fe *FieldElement) double(a *FieldElement) *FieldElement { |
| 31 | return fe.add(a, a) |
| 32 | } |
| 33 | |
| 34 | // Sets fe to a + b. Returns fe. |
| 35 | func (fe *FieldElement) Add(a, b *FieldElement) *FieldElement { |
| 36 | return fe.add(a, b).normalize() |
| 37 | } |
| 38 | |
| 39 | // Sets fe to a - b. Returns fe. |
| 40 | func (fe *FieldElement) Sub(a, b *FieldElement) *FieldElement { |
| 41 | return fe.sub(a, b).normalize() |
| 42 | } |
| 43 | |
| 44 | // Sets fe to a. Returns fe. |
| 45 | func (fe *FieldElement) Set(a *FieldElement) *FieldElement { |
| 46 | copy(fe[:], a[:]) |
| 47 | return fe |
| 48 | } |
| 49 | |
| 50 | // Returns little endian representation of fe. |
| 51 | func (fe *FieldElement) Bytes() [32]byte { |
| 52 | var ret [32]byte |
| 53 | fe.BytesInto(&ret) |
| 54 | return ret |
| 55 | } |
| 56 | |
| 57 | // Set fe to the inverse of a. Return fe. |
| 58 | func (fe *FieldElement) Inverse(a *FieldElement) *FieldElement { |
| 59 | var t0, t1, t2, t3 FieldElement |
| 60 | var i int |
| 61 | |
| 62 | t0.Square(a) |
| 63 | t1.Square(&t0) |
| 64 | t1.Square(&t1) |
| 65 | t1.Mul(a, &t1) |
| 66 | t0.Mul(&t0, &t1) |
| 67 | t2.Square(&t0) |
| 68 | t1.Mul(&t1, &t2) |
| 69 | t2.Square(&t1) |
| 70 | for i = 1; i < 5; i++ { |
| 71 | t2.Square(&t2) |
| 72 | } |
| 73 | t1.Mul(&t2, &t1) |
| 74 | t2.Square(&t1) |
| 75 | for i = 1; i < 10; i++ { |
| 76 | t2.Square(&t2) |
| 77 | } |
| 78 | t2.Mul(&t2, &t1) |
| 79 | t3.Square(&t2) |
| 80 | for i = 1; i < 20; i++ { |
| 81 | t3.Square(&t3) |
| 82 | } |
| 83 | t2.Mul(&t3, &t2) |
| 84 | t2.Square(&t2) |
| 85 | for i = 1; i < 10; i++ { |
| 86 | t2.Square(&t2) |
| 87 | } |
| 88 | t1.Mul(&t2, &t1) |
| 89 | t2.Square(&t1) |
| 90 | for i = 1; i < 50; i++ { |
| 91 | t2.Square(&t2) |
| 92 | } |
| 93 | t2.Mul(&t2, &t1) |
| 94 | t3.Square(&t2) |
| 95 | for i = 1; i < 100; i++ { |
| 96 | t3.Square(&t3) |
| 97 | } |
| 98 | t2.Mul(&t3, &t2) |
| 99 | t2.Square(&t2) |
| 100 | for i = 1; i < 50; i++ { |
| 101 | t2.Square(&t2) |
| 102 | } |
| 103 | t1.Mul(&t2, &t1) |
| 104 | t1.Square(&t1) |
| 105 | for i = 1; i < 5; i++ { |
| 106 | t1.Square(&t1) |
| 107 | } |
| 108 | return fe.Mul(&t1, &t0) |
| 109 | } |
| 110 | |
| 111 | // Set fe to -x if x is negative and x otherwise. Returns fe. |
| 112 | func (fe *FieldElement) Abs(x *FieldElement) *FieldElement { |
| 113 | var xNeg FieldElement |
| 114 | xNeg.Neg(x) |
| 115 | fe.Set(x) |
| 116 | fe.ConditionalSet(&xNeg, x.IsNegativeI()) |
| 117 | return fe |
| 118 | } |
| 119 | |
| 120 | // Returns 1 if fe is negative, otherwise 0. |
| 121 | func (fe *FieldElement) IsNegativeI() int32 { |
| 122 | var buf [32]byte |
| 123 | fe.BytesInto(&buf) |
| 124 | return int32(buf[0] & 1) |
| 125 | } |
| 126 | |
| 127 | // Returns 1 if fe is non-zero, otherwise 0. |
| 128 | func (fe *FieldElement) IsNonZeroI() int32 { |
| 129 | var buf [32]byte |
| 130 | fe.BytesInto(&buf) |
| 131 | ret := (binary.LittleEndian.Uint64(buf[0:8]) | |
| 132 | binary.LittleEndian.Uint64(buf[8:16]) | |
| 133 | binary.LittleEndian.Uint64(buf[16:24]) | |
| 134 | binary.LittleEndian.Uint64(buf[24:32])) |
| 135 | ret |= ret >> 32 |
| 136 | ret |= ret >> 16 |
| 137 | ret |= ret >> 8 |
| 138 | ret |= ret >> 4 |
| 139 | ret |= ret >> 2 |
| 140 | ret |= ret >> 1 |
| 141 | return int32(ret & 1) |
| 142 | } |
| 143 | |
| 144 | // Returns 1 if fe is equal to one, otherwise 0. |
| 145 | func (fe *FieldElement) IsOneI() int32 { |
| 146 | var b FieldElement |
| 147 | return 1 - b.sub(fe, &feOne).IsNonZeroI() |
| 148 | } |
| 149 | |
| 150 | // Returns 1 if fe is equal to a, otherwise 0. |
| 151 | func (fe *FieldElement) EqualsI(a *FieldElement) int32 { |
| 152 | var b FieldElement |
| 153 | return 1 - b.sub(fe, a).IsNonZeroI() |
| 154 | } |
| 155 | |
| 156 | // Returns whether fe equals a. |
| 157 | func (fe *FieldElement) Equals(a *FieldElement) bool { |
| 158 | var b FieldElement |
| 159 | return b.sub(fe, a).IsNonZeroI() == 0 |
| 160 | } |
| 161 | |
| 162 | // Returns fe as a big.Int. |
| 163 | // |
| 164 | // WARNING Operations on big.Ints are not constant-time: do not use them |
| 165 | // |
| 166 | // for cryptography unless you're sure this is not an issue. |
| 167 | func (fe *FieldElement) BigInt() *big.Int { |
| 168 | var ret big.Int |
| 169 | var buf, rBuf [32]byte |
| 170 | fe.BytesInto(&buf) |
| 171 | for i := 0; i < 32; i++ { |
| 172 | rBuf[i] = buf[31-i] |
| 173 | } |
| 174 | return ret.SetBytes(rBuf[:]) |
| 175 | } |
| 176 | |
| 177 | // Writes fe as a string for debugging. |
| 178 | // |
| 179 | // WARNING This operation is not constant-time. Do not use for cryptography |
| 180 | // |
| 181 | // unless you're sure this is not an issue. |
| 182 | func (fe FieldElement) String() string { |
| 183 | return fe.BigInt().String() |
| 184 | } |
| 185 | |
| 186 | // Sets fe to x modulo 2^255-19. |
| 187 | // |
| 188 | // WARNING Operations on big.Ints are not constant-time: do not use them |
| 189 | // |
| 190 | // for cryptography unless you're sure this is not an issue. |
| 191 | func (fe *FieldElement) SetBigInt(x *big.Int) *FieldElement { |
| 192 | var v, bi25519 big.Int |
| 193 | bi25519.SetString( |
| 194 | "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed", 16) |
| 195 | buf := v.Mod(x, &bi25519).Bytes() |
| 196 | var rBuf [32]byte |
| 197 | for i := 0; i < len(buf) && i < 32; i++ { |
| 198 | rBuf[i] = buf[len(buf)-i-1] |
| 199 | } |
| 200 | return fe.SetBytes(&rBuf) |
| 201 | } |
| 202 | |
| 203 | // Sets fe to a^( ((2^255 - 19) - 5) / 8 ) = a^(2^252 - 3). Returns fe. |
| 204 | // |
| 205 | // This method is useful to compute (inverse) square-roots |
| 206 | // with the method of Lagrange. |
| 207 | func (fe *FieldElement) Exp22523(a *FieldElement) *FieldElement { |
| 208 | var t0, t1, t2 FieldElement |
| 209 | var i int |
| 210 | |
| 211 | t0.Square(a) |
| 212 | for i = 1; i < 1; i++ { |
| 213 | t0.Square(&t0) |
| 214 | } |
| 215 | t1.Square(&t0) |
| 216 | for i = 1; i < 2; i++ { |
| 217 | t1.Square(&t1) |
| 218 | } |
| 219 | t1.Mul(a, &t1) |
| 220 | t0.Mul(&t0, &t1) |
| 221 | t0.Square(&t0) |
| 222 | for i = 1; i < 1; i++ { |
| 223 | t0.Square(&t0) |
| 224 | } |
| 225 | t0.Mul(&t1, &t0) |
| 226 | t1.Square(&t0) |
| 227 | for i = 1; i < 5; i++ { |
| 228 | t1.Square(&t1) |
| 229 | } |
| 230 | t0.Mul(&t1, &t0) |
| 231 | t1.Square(&t0) |
| 232 | for i = 1; i < 10; i++ { |
| 233 | t1.Square(&t1) |
| 234 | } |
| 235 | t1.Mul(&t1, &t0) |
| 236 | t2.Square(&t1) |
| 237 | for i = 1; i < 20; i++ { |
| 238 | t2.Square(&t2) |
| 239 | } |
| 240 | t1.Mul(&t2, &t1) |
| 241 | t1.Square(&t1) |
| 242 | for i = 1; i < 10; i++ { |
| 243 | t1.Square(&t1) |
| 244 | } |
| 245 | t0.Mul(&t1, &t0) |
| 246 | t1.Square(&t0) |
| 247 | for i = 1; i < 50; i++ { |
| 248 | t1.Square(&t1) |
| 249 | } |
| 250 | t1.Mul(&t1, &t0) |
| 251 | t2.Square(&t1) |
| 252 | for i = 1; i < 100; i++ { |
| 253 | t2.Square(&t2) |
| 254 | } |
| 255 | t1.Mul(&t2, &t1) |
| 256 | t1.Square(&t1) |
| 257 | for i = 1; i < 50; i++ { |
| 258 | t1.Square(&t1) |
| 259 | } |
| 260 | t0.Mul(&t1, &t0) |
| 261 | t0.Square(&t0) |
| 262 | for i = 1; i < 2; i++ { |
| 263 | t0.Square(&t0) |
| 264 | } |
| 265 | return fe.Mul(&t0, a) |
| 266 | } |
| 267 | |
| 268 | // Sets fe to 1/sqrt(a). Requires a to be a square. Returns fe. |
| 269 | func (fe *FieldElement) InvSqrt(a *FieldElement) *FieldElement { |
| 270 | var den2, den3, den4, den6, chk, t, t2 FieldElement |
| 271 | den2.Square(a) |
| 272 | den3.Mul(&den2, a) |
| 273 | den4.Square(&den2) |
| 274 | den6.Mul(&den2, &den4) |
| 275 | t.Mul(&den6, a) |
| 276 | t.Exp22523(&t) |
| 277 | t.Mul(&t, &den3) |
| 278 | t2.Mul(&t, &feI) |
| 279 | |
| 280 | chk.Square(&t) |
| 281 | chk.Mul(&chk, a) |
| 282 | |
| 283 | fe.Set(&t) |
| 284 | fe.ConditionalSet(&t2, 1-chk.IsOneI()) |
| 285 | return fe |
| 286 | } |
| 287 | |
| 288 | // Sets fe to sqrt(a). Requires a to be a square. Returns fe. |
| 289 | func (fe *FieldElement) Sqrt(a *FieldElement) *FieldElement { |
| 290 | var aCopy FieldElement |
| 291 | aCopy.Set(a) |
| 292 | fe.InvSqrt(a) |
| 293 | fe.Mul(fe, &aCopy) |
| 294 | |
| 295 | var feNeg FieldElement |
| 296 | feNeg.Neg(fe) |
| 297 | fe.ConditionalSet(&feNeg, fe.IsNegativeI()) |
| 298 | return fe |
| 299 | } |
| 300 | |
| 301 | // Sets fe to either 1/sqrt(a) or 1/sqrt(i*a). Returns 1 in the former case |
| 302 | // and 0 in the latter. |
| 303 | func (fe *FieldElement) InvSqrtI(a *FieldElement) int32 { |
| 304 | var inCaseA, inCaseB, inCaseD int32 |
| 305 | var den2, den3, den4, den6, chk, t, corr FieldElement |
| 306 | den2.Square(a) |
| 307 | den3.Mul(&den2, a) |
| 308 | den4.Square(&den2) |
| 309 | den6.Mul(&den2, &den4) |
| 310 | t.Mul(&den6, a) |
| 311 | t.Exp22523(&t) |
| 312 | t.Mul(&t, &den3) |
| 313 | |
| 314 | // case A B C D |
| 315 | // --------------------------------------------------------------- |
| 316 | // t 1/sqrt(a) -i/sqrt(a) 1/sqrt(i*a) -i/sqrt(i*a) |
| 317 | // chk 1 -1 -i i |
| 318 | // corr 1 i 1 i |
| 319 | // ret 1 1 0 0 |
| 320 | |
| 321 | chk.Square(&t) |
| 322 | chk.Mul(&chk, a) |
| 323 | |
| 324 | inCaseA = chk.IsOneI() |
| 325 | inCaseD = chk.EqualsI(&feI) |
| 326 | chk.Neg(&chk) |
| 327 | inCaseB = chk.IsOneI() |
| 328 | |
| 329 | corr.SetOne() |
| 330 | corr.ConditionalSet(&feI, inCaseB+inCaseD) |
| 331 | t.Mul(&t, &corr) |
| 332 | fe.Set(&t) |
| 333 | |
| 334 | return inCaseA + inCaseB |
| 335 | } |
| 336 | |
| 337 | // Returns 1 if b == c and 0 otherwise. Assumes 0 <= b, c < 2^30. |
| 338 | func equal30(b, c int32) int32 { |
| 339 | x := uint32(b ^ c) |
| 340 | x-- |
| 341 | return int32(x >> 31) |
| 342 | } |
| 343 | |
| 344 | // Returns 1 if b == c and 0 otherwise. Assumes 2^15 <= b, c < 2^30. |
| 345 | func equal15(b, c int32) int32 { |
| 346 | ub := uint16(b) |
| 347 | uc := uint16(c) |
| 348 | x := uint32(ub ^ uc) |
| 349 | x-- |
| 350 | return int32(x >> 31) |
| 351 | } |
| 352 | |
| 353 | // Returns 1 if b < 0 and 0 otherwise. |
| 354 | func negative(b int32) int32 { |
| 355 | return (b >> 31) & 1 |
| 356 | } |