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'
new p_cached()
])
const S = new StaticArray<u8>(32)
+const S_pair = new StaticArray<u8>(32)
const expected_r = new p3()
const h = new StaticArray<u8>(64)
+const h_pair = new StaticArray<u8>(64)
const sb_ah = new p_cached()
const check = new p3()
const check_r = new StaticArray<u8>(32)
+const check_r_pair = new StaticArray<u8>(32)
/**
* Verify public key `pub` can be decoded to point `A`. If so, builds a cache
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<u8>, M_0: StaticArray<u8>, sig_1: StaticArray<u8>, M_1: StaticArray<u8>, Ai: StaticArray<p_cached>, pub: StaticArray<u8>): 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<usize>(S), changetype<usize>(sig_0) + 32, 32)
+ memory.copy(changetype<usize>(S_pair), changetype<usize>(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`.