import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants'
import { Blake2b } from './ed25519/blake2b'
-import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_is_small_order_point, ge_is_canonical } from './ed25519/ge'
+import { ge_accept_small_order, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_is_canonical, ge_reject_small_order } from './ed25519/ge'
import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p'
import { sc_is_canonical, sc_reduce } from './ed25519/sc'
import { clear_t, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t'
ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
- // after cofactoring, `check` should be small order to pass, unlike
- // `verify_pubkey()` and `verify_signature()` below
- const b = ge_is_small_order_point(check, t_fe_0, t_u8x32)
+ // after cofactoring, `check` should be small order and not malformed in order
+ // to pass, unlike `verify_pubkey()` and `verify_signature()` below
+ const b = ge_accept_small_order(check, t_fe_0, t_u8x32)
clear_t()
return b
}
/**
* Verify public key `k` is canonical, decodeable, and not small order. Used by
- * `crypto_verify_sodium` and `crypto_verify_strict`.
+ * `crypto_verify_sodium` and `crypto_verify_strict`. Exported only so that
+ * `test/arithmetic.mjs` can test it directly; `index.ts` does not re-export it,
+ * so it is not a wasm export.
* @returns 0 if pubkey fails checks, else return 1 if pubkey is good
*/
-function verify_pubkey (pub: StaticArray<u8>): boolean {
+export function verify_pubkey (pub: StaticArray<u8>): boolean {
// fail if public key `k` is non-canonical (`p = 2²⁵⁵-19 ≤ k`)
if (ge_is_canonical(pub) != 0) {
}
// fail if public key `k` is small order
- if (ge_is_small_order_point(A, t_fe_0, t_u8x32) != 0) {
+ if (ge_reject_small_order(A, t_fe_0, t_u8x32) != 0) {
clear_t()
return false
}
/**
* Verify signature `sig` is canonical, decodeable, and not small order. Used by
- * `crypto_verify_sodium` and `crypto_verify_strict`.
+ * `crypto_verify_sodium` and `crypto_verify_strict`. Exported only so that
+ * `test/arithmetic.mjs` can test it directly; `index.ts` does not re-export it,
+ * so it is not a wasm export.
* @returns 0 if signature fails checks, else return 1 if signature is good
*/
-function verify_signature (sig: StaticArray<u8>): boolean {
+export function verify_signature (sig: StaticArray<u8>): boolean {
// fail if scalar `S` is non-canonical (`L ≤ S`)
memory.copy(changetype<usize>(S), changetype<usize>(sig) + 32, 32)
}
// fail if nonce `R` is small order
- if (ge_is_small_order_point(expected_r, t_fe_0, t_u8x32) != 0) {
+ if (ge_reject_small_order(expected_r, t_fe_0, t_u8x32) != 0) {
clear_t()
return false
}
return i32(has_m_root | has_p_root) - 1
}
-const ge_is_small_order_point_y_sqrtm1: FieldElement = fe()
-const ge_is_small_order_point_c: FieldElement = fe()
/**
* @param {ge_p3} p
- * @returns 1 if `p` is a valid projective point with small order, else 0
+ * @returns 1 if `p` is malformed (`Z = 0`, or `X = Y = 0`), else 0
*/
-export function ge_is_small_order_point (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
- const y_sqrtm1 = ge_is_small_order_point_y_sqrtm1
- const c = ge_is_small_order_point_c
-
+function ge_is_malformed (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
const x_iszero = fe_iszero(p.X, t_fe, t_u8x32)
const y_iszero = fe_iszero(p.Y, t_fe, t_u8x32)
const z_iszero = fe_iszero(p.Z, t_fe, t_u8x32)
+ return ((x_iszero & y_iszero) | z_iszero)
+}
+
+const ge_has_small_order_y_sqrtm1: FieldElement = fe()
+const ge_has_small_order_c: FieldElement = fe()
+/**
+ * @param {ge_p3} p
+ * @returns 1 if `p` has small order, else 0
+ */
+function ge_has_small_order (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+ const y_sqrtm1 = ge_has_small_order_y_sqrtm1
+ const c = ge_has_small_order_c
+
+ const x_iszero = fe_iszero(p.X, t_fe, t_u8x32)
+ const y_iszero = fe_iszero(p.Y, t_fe, t_u8x32)
fe_mul(y_sqrtm1, p.Y, fe_sqrtm1)
fe_sub(c, y_sqrtm1, p.X)
- let c_iszero = fe_iszero(c, t_fe, t_u8x32)
+ const y_sqrtm1_minusx = fe_iszero(c, t_fe, t_u8x32)
fe_add(c, y_sqrtm1, p.X)
- c_iszero |= fe_iszero(c, t_fe, t_u8x32)
+ const y_sqrtm1_plusx = fe_iszero(c, t_fe, t_u8x32)
+
+ return x_iszero | y_iszero | y_sqrtm1_minusx | y_sqrtm1_plusx
+}
+
+/**
+ * For the cofactored equation in `crypto_verify_sodium`, where small order means
+ * the signature is valid: a malformed point must not pass.
+ * @param {ge_p3} p
+ * @returns 1 if `p` is small order and not malformed, else 0
+ */
+export function ge_accept_small_order (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+ const small_order = ge_has_small_order(p, t_fe, t_u8x32)
+ const malformed = ge_is_malformed(p, t_fe, t_u8x32)
- const torsion = x_iszero | y_iszero | c_iszero
- const valid = ((x_iszero & y_iszero) | z_iszero) ^ 1
+ return small_order & (malformed ^ 1)
+}
+
+/**
+ * For the checks on the key and `R`, where small order means reject: a
+ * malformed point must not pass either.
+ * @param {ge_p3} p
+ * @returns 0 if `p` is not small order and not malformed, else -1
+ */
+export function ge_reject_small_order (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+ const small_order = ge_has_small_order(p, t_fe, t_u8x32)
+ const malformed = ge_is_malformed(p, t_fe, t_u8x32)
- return torsion & valid
+ return 0 - (small_order | malformed)
}
/**