| 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 |
| 32 | |
| 33 | import ( |
| 34 | "testing" |
| 35 | |
| 36 | "filippo.io/edwards25519/field" |
| 37 | ) |
| 38 | |
| 39 | func TestMontgomery(t *testing.T) { |
| 40 | t.Run("Elligator2Constants", testElligator2Constants) |
| 41 | } |
| 42 | |
| 43 | func testElligator2Constants(t *testing.T) { |
| 44 | t.Run("NegA", func(t *testing.T) { |
| 45 | expected := new(field.Element).Negate(NEG_A) |
| 46 | |
| 47 | if expected.Equal(A) != 1 { |
| 48 | t.Fatalf("invalid value for -A: %x", NEG_A.Bytes()) |
| 49 | } |
| 50 | }) |
| 51 | |
| 52 | t.Run("SqrtNegAPlusTwo", func(t *testing.T) { |
| 53 | expected := new(field.Element).Subtract(NEG_A, TWO) |
| 54 | expected.Invert(expected) |
| 55 | expected.SqrtRatio(ONE, expected) |
| 56 | |
| 57 | if expected.Equal(SQRT_NEG_A_PLUS_TWO) != 1 { |
| 58 | t.Fatalf("invalid value for sqrt(-(A+2): %x", SQRT_NEG_A_PLUS_TWO.Bytes()) |
| 59 | } |
| 60 | }) |
| 61 | |
| 62 | t.Run("UFactor", func(t *testing.T) { |
| 63 | expected := new(field.Element).Negate(TWO) |
| 64 | expected.Multiply(expected, SQRT_M1) |
| 65 | |
| 66 | if expected.Equal(U_FACTOR) != 1 { |
| 67 | t.Fatalf("invalid value for u_factor: %x", U_FACTOR.Bytes()) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | t.Run("VFactor", func(t *testing.T) { |
| 72 | expected := new(field.Element).Invert(U_FACTOR) |
| 73 | expected.SqrtRatio(ONE, expected) |
| 74 | |
| 75 | if expected.Equal(V_FACTOR) != 1 { |
| 76 | t.Fatalf("invalid value for v_factor: %x", V_FACTOR.Bytes()) |
| 77 | } |
| 78 | }) |
| 79 | } |