| 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 elligator2 implements the Elligator2 mapping. |
| 32 | package elligator2 |
| 33 | |
| 34 | import ( |
| 35 | "filippo.io/edwards25519" |
| 36 | "filippo.io/edwards25519/field" |
| 37 | |
| 38 | "gitlab.com/yawning/edwards25519-extra/internal/montgomery" |
| 39 | ) |
| 40 | |
| 41 | // MontgomeryFlavor calculates the Montgomery point corresponding to the |
| 42 | // representative r, returning the u and v coordinates (Elligator2 |
| 43 | // direct map). |
| 44 | func MontgomeryFlavor(r *field.Element) (*field.Element, *field.Element) { |
| 45 | // This is based off the public domain python implementation by |
| 46 | // Loup Vaillant, taken from the Monocypher package |
| 47 | // (tests/gen/elligator.py). |
| 48 | // |
| 49 | // The choice of base implementation is primarily because it was |
| 50 | // convenient, and because they appear to be one of the people |
| 51 | // that have given the most thought regarding how to implement |
| 52 | // this correctly, with a readable implementation that I can |
| 53 | // wrap my brain around. |
| 54 | |
| 55 | // r1 |
| 56 | t1 := new(field.Element).Square(r) |
| 57 | t1.Multiply(t1, montgomery.TWO) |
| 58 | |
| 59 | // r2 |
| 60 | u := new(field.Element).Add(t1, montgomery.ONE) |
| 61 | |
| 62 | t2 := new(field.Element).Square(u) |
| 63 | |
| 64 | // numerator |
| 65 | t3 := new(field.Element).Multiply(montgomery.A_SQUARED, t1) |
| 66 | t3.Subtract(t3, t2) |
| 67 | t3.Multiply(t3, montgomery.A) |
| 68 | |
| 69 | // denominator |
| 70 | t1.Multiply(t2, u) |
| 71 | |
| 72 | t1.Multiply(t1, t3) |
| 73 | _, isSquare := t1.SqrtRatio(montgomery.ONE, t1) |
| 74 | |
| 75 | u.Square(r) |
| 76 | u.Multiply(u, montgomery.U_FACTOR) |
| 77 | |
| 78 | v := new(field.Element).Multiply(r, montgomery.V_FACTOR) |
| 79 | |
| 80 | u.Select(montgomery.ONE, u, isSquare) |
| 81 | v.Select(montgomery.ONE, v, isSquare) |
| 82 | |
| 83 | v.Multiply(v, t3) |
| 84 | v.Multiply(v, t1) |
| 85 | |
| 86 | t1.Square(t1) |
| 87 | |
| 88 | u.Multiply(u, montgomery.NEG_A) |
| 89 | u.Multiply(u, t3) |
| 90 | u.Multiply(u, t2) |
| 91 | u.Multiply(u, t1) |
| 92 | |
| 93 | negV := new(field.Element).Negate(v) |
| 94 | v.Select(negV, v, isSquare^v.IsNegative()) |
| 95 | |
| 96 | return u, v |
| 97 | } |
| 98 | |
| 99 | // EdwardsFlavor calculates and returns the Edwards point corresponding |
| 100 | // to the representative r (Elligator2 direct map). |
| 101 | func EdwardsFlavor(r *field.Element) *edwards25519.Point { |
| 102 | u, v := MontgomeryFlavor(r) |
| 103 | return montgomery.ToEdwardsPoint(u, v) |
| 104 | } |