| 1 | #cxx . . $ `<file>/src/pub_conversion.c`/include <fe.h>cxx . . $ `<file>/ref10/crypto_sign/fe.h`/ |
| 2 | #include <sodium.h> |
| 3 | |
| 4 | #include <xeddsa.h>cxx . . $ `<file>/include/xeddsa.h`/ |
| 5 | |
| 6 | /** |
| 7 | * Convert a Curve25519 public key into an Ed25519 public key. |
| 8 | * |
| 9 | * @param ed25519_pub (out): The Ed25519 public key corresponding to the Curve25519 public key. |
| 10 | * @param curve25519_pub (in): The Curve25519 public key to convert into its Ed25519 equivalent. |
| 11 | * @param set_sign_bit: Whether to set the sign bit of the output Ed25519 public key. |
| 12 | */ |
| 13 | void curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(uint8_tcxx . . $ uint8_t#* ed25519_pub, |
| 14 | const uint8_tcxx . . $ uint8_t#* curve25519_pub, |
| 15 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! set_sign_bit) { |
| 16 | // Apply the birational map defined in RFC 7748, section 4.1 "Curve25519" to calculate an Ed25519 public |
| 17 | // key from a Curve25519 public key. Because the serialization format of Curve25519 public keys only |
| 18 | // contains the u coordinate, the x coordinate of the corresponding Ed25519 public key can't be uniquely |
| 19 | // calculated as defined by the birational map. The x coordinate is represented in the serialization |
| 20 | // format of Ed25519 public keys only in a single sign bit. This function assumes that the sign bit is |
| 21 | // known to the user and is passed accordingly. |
| 22 | fecxx . . $ fe# u, y; |
| 23 | fecxx . . $ fe# one; |
| 24 | fecxx . . $ fe# u_minus_one, u_plus_one, u_plus_one_inv; |
| 25 | |
| 26 | // Parse the Curve25519 public key input as a field element containing the u coordinate. RFC 7748, |
| 27 | // section 5 "The X25519 and X448 Functions", mandates that the most significant bit of the Curve25519 |
| 28 | // public key has to be zeroized. This is handled by fe_frombytes internally. |
| 29 | fe_frombytescxx . . $ `ref10/crypto_sign/fe.h:18:9`!(u, curve25519_pub); |
| 30 | |
| 31 | // Calculate the parameters (u - 1) and (u + 1) |
| 32 | fe_1cxx . . $ `ref10/crypto_sign/fe.h:24:9`!(one); |
| 33 | fe_subcxx . . $ `ref10/crypto_sign/fe.h:27:9`!(u_minus_one, u, one); |
| 34 | fe_addcxx . . $ `ref10/crypto_sign/fe.h:26:9`!(u_plus_one, u, one); |
| 35 | |
| 36 | // Invert u + 1 |
| 37 | fe_invertcxx . . $ `ref10/crypto_sign/fe.h:32:9`!(u_plus_one_inv, u_plus_one); |
| 38 | |
| 39 | // Calculate y = (u - 1) * inv(u + 1) (mod p) |
| 40 | fe_mulcxx . . $ `ref10/crypto_sign/fe.h:29:9`!(y, u_minus_one, u_plus_one_inv); |
| 41 | |
| 42 | // Serialize the field element containing the y coordinate to the Ed25519 public key output |
| 43 | fe_tobytescxx . . $ `ref10/crypto_sign/fe.h:19:9`!(ed25519_pub, y); |
| 44 | |
| 45 | // Set the sign bit to zero |
| 46 | ed25519_pub[31] &= 0x7f; |
| 47 | |
| 48 | // Set the sign bit to the requested value |
| 49 | ed25519_pub[31] |= !!set_sign_bit << 7; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Convert an Ed25519 public key into a Curve25519 public key. Re-export of libsodiums/ref10s |
| 54 | * `crypto_sign_ed25519_pk_to_curve25519` function for convenience. |
| 55 | * |
| 56 | * @param curve25519_pub (out): The Curve25519 public key corresponding to the Ed25519 public key. |
| 57 | * @param ed25519_pub (in): The Ed25519 public key to convert into its Curve25519 equivalent. |
| 58 | * @return 0 on success, -1 if the public key was rejected due to suboptimal security propierties. |
| 59 | */ |
| 60 | int ed25519_pub_to_curve25519_pubcxx . . $ ed25519_pub_to_curve25519_pub(1ff552b5f61cf5ec).(uint8_tcxx . . $ uint8_t#* curve25519_pub, const uint8_tcxx . . $ uint8_t#* ed25519_pub) { |
| 61 | return crypto_sign_ed25519_pk_to_curve25519cxx . . $ crypto_sign_ed25519_pk_to_curve25519(1ff552b5f61cf5ec).(curve25519_pub, ed25519_pub); |
| 62 | } |