From: Chris Duncan Date: Mon, 9 Nov 2026 17:03:34 +0000 (-0800) Subject: Draft paired donna verification. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=b1a0c8ac130958a66cc5fde49bb0b0a7d7d40863;p=nano25519.git Draft paired donna verification. --- diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index 09af8ef..5c7afd2 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -3,7 +3,7 @@ import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants' import { Blake2b } from './ed25519/blake2b' -import { ge_accept_small_order, ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_bytes, ge_double_scalarmult_vartime_to_cached, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ed25519/ge' +import { ge_accept_small_order, ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_bytes, ge_double_scalarmult_vartime_to_bytes_pair, ge_double_scalarmult_vartime_to_cached, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ed25519/ge' import { p3, p3_0, p3_dbl_to_p3, p3_sub_cached_to_p3, p_cached } from './ed25519/p' import { sc_is_canonical, sc_reduce } from './ed25519/sc' import { equalbytes } from './ed25519/utils' @@ -26,11 +26,14 @@ const Ai: StaticArray = StaticArray.fromArray([ new p_cached() ]) const S = new StaticArray(32) +const S_pair = new StaticArray(32) const expected_r = new p3() const h = new StaticArray(64) +const h_pair = new StaticArray(64) const sb_ah = new p_cached() const check = new p3() const check_r = new StaticArray(32) +const check_r_pair = new StaticArray(32) /** * Verify public key `pub` can be decoded to point `A`. If so, builds a cache @@ -80,6 +83,49 @@ export function crypto_verify_donna (sig: StaticArray, M: StaticArray, A return equalbytes(sig, check_r, KEY_BYTELENGTH) } +/** + * For two signatures, verify both were made by signing their respective block + * hashes using a single public key `pub` (see crypto_verify_donna). + * + * The return value is a bitwise flag indicating success or failure of each + * signature's verification: + * + * - 0: both failed + * - 1: first OK, second failed + * - 2: first failed, second OK + * - 3: both OK + */ +export function crypto_verify_donna_pair (sig_0: StaticArray, M_0: StaticArray, sig_1: StaticArray, M_1: StaticArray, Ai: StaticArray, pub: StaticArray): i32 { + + // if private scalar `S` is out of range (`2²⁵³ ≤ S`), fail that test and fall + // back to single signature verification using bitwise flag format + if ((sig_0[63] & 224) != 0) { + return crypto_verify_donna(sig_1, M_1, Ai, pub) << 1 // OK = 10 + } + if ((sig_1[63] & 224) != 0) { + return crypto_verify_donna(sig_0, M_0, Ai, pub) // OK = 01 + } + + memory.copy(changetype(S), changetype(sig_0) + 32, 32) + memory.copy(changetype(S_pair), changetype(sig_1) + 32, 32) + + // signature is nonce point R and scalar S (R || S) + // data to hash is nonce point R, public key A, and message M + // from parameter arguments: R = s[0,32], A = pk, M = m + // R, S, A, and M are all 32-byte values in this implementation + blake2b.init().update(sig_0, 32).update(pub, KEY_BYTELENGTH).update(M_0, BLOCKHASH_BYTELENGTH).digest(h) + sc_reduce(h) + blake2b.init().update(sig_1, 32).update(pub, KEY_BYTELENGTH).update(M_1, BLOCKHASH_BYTELENGTH).digest(h_pair) + sc_reduce(h_pair) + + ge_double_scalarmult_vartime_to_bytes_pair(check_r, h, S, check_r_pair, h_pair, S_pair, Ai) + + let result: i32 = 0 + result ^= equalbytes(sig_0, check_r, KEY_BYTELENGTH) + result ^= equalbytes(sig_1, check_r_pair, KEY_BYTELENGTH) << 1 + return result +} + /** * Verify signature `sig` was made by signing message `M` using public key * `pub`.