Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/ed25519-dalek/src/batch.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / ed25519-dalek / src / batch.rs` 241 lines · 8916 bytes Raw Blame History 1 // -*- mode: rust; -*- 2 // 3 // This file is part of `ed25519`-dalek. 4 // Copyright (c) 2017-2019 isis lovecruft 5 // See LICENSE for licensing information. 6 // 7 // Authors: 8 // - isis agora lovecruft 9 10 //! Batch signature verification. 11 12 use alloc::vec::Vec; 13 14 use core::iter::once; 15 16 use curve25519_dalek::constants; 17 use curve25519_dalek::edwards::EdwardsPoint; 18 use curve25519_dalek::scalar::Scalar; 19 use curve25519_dalek::traits::IsIdentity; 20 use curve25519_dalek::traits::VartimeMultiscalarMul; 21 22 pub use curve25519_dalek::digest::Digest; 23 24 use merlin::Transcript; 25 26 use rand_core::RngCore; 27 28 use sha2::Sha512; 29 30 use crate::errors::InternalError; 31 use crate::errors::SignatureError; 32 use crate::signature::InternalSignature; 33 use crate::VerifyingKey; 34 35 /// An implementation of `rand_core::RngCore` which does nothing. This is necessary because merlin 36 /// demands an `Rng` as input to `TranscriptRngBuilder::finalize()`. Using this with `finalize()` 37 /// yields a PRG whose input is the hashed transcript. 38 struct ZeroRng; 39 40 impl rand_core::RngCore for ZeroRng { 41 fn next_u32(&mut self) -> u32 { 42 rand_core::impls::next_u32_via_fill(self) 43 } 44 45 fn next_u64(&mut self) -> u64 { 46 rand_core::impls::next_u64_via_fill(self) 47 } 48 49 /// A no-op function which leaves the destination bytes for randomness unchanged. 50 /// 51 /// In this case, the internal merlin code is initialising the destination 52 /// by doing `[0u8; …]`, which means that when we call 53 /// `merlin::TranscriptRngBuilder.finalize()`, rather than rekeying the 54 /// STROBE state based on external randomness, we're doing an 55 /// `ENC_{state}(`00000000000000000000000000000000`)` operation, which is 56 /// identical to the STROBE `MAC` operation. 57 fn fill_bytes(&mut self, _dest: &mut [u8]) {} 58 59 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { 60 self.fill_bytes(dest); 61 Ok(()) 62 } 63 } 64 65 // `TranscriptRngBuilder::finalize()` requires a `CryptoRng` 66 impl rand_core::CryptoRng for ZeroRng {} 67 68 // We write our own gen() function so we don't need to pull in the rand crate 69 fn gen_u128(rng: &mut R) -> u128 { 70 let mut buf = [0u8; 16]; 71 rng.fill_bytes(&mut buf); 72 u128::from_le_bytes(buf) 73 } 74 75 /// Verify a batch of `signatures` on `messages` with their respective `verifying_keys`. 76 /// 77 /// # Inputs 78 /// 79 /// * `messages` is a slice of byte slices, one per signed message. 80 /// * `signatures` is a slice of `Signature`s. 81 /// * `verifying_keys` is a slice of `VerifyingKey`s. 82 /// 83 /// # Returns 84 /// 85 /// * A `Result` whose `Ok` value is an empty tuple and whose `Err` value is a 86 /// `SignatureError` containing a description of the internal error which 87 /// occurred. 88 /// 89 /// ## On Deterministic Nonces 90 /// 91 /// The nonces for batch signature verification are derived purely from the inputs to this function 92 /// themselves. 93 /// 94 /// In any sigma protocol it is wise to include as much context pertaining 95 /// to the public state in the protocol as possible, to avoid malleability 96 /// attacks where an adversary alters publics in an algebraic manner that 97 /// manages to satisfy the equations for the protocol in question. 98 /// 99 /// For `ed25519` batch verification we include the following as scalars in the protocol transcript: 100 /// 101 /// * All of the computed `H(R||A||M)`s to the protocol transcript, and 102 /// * All of the `s` components of each signature. 103 /// 104 /// The former, while not quite as elegant as adding the `R`s, `A`s, and 105 /// `M`s separately, saves us a bit of context hashing since the 106 /// `H(R||A||M)`s need to be computed for the verification equation anyway. 107 /// 108 /// The latter prevents a malleability attack wherein an adversary, without access 109 /// to the signing key(s), can take any valid signature, `(s,R)`, and swap 110 /// `s` with `s' = -z1`. This doesn't constitute a signature forgery, merely 111 /// a vulnerability, as the resulting signature will not pass single 112 /// signature verification. (Thanks to Github users @real_or_random and 113 /// @jonasnick for pointing out this malleability issue.) 114 /// 115 /// # Examples 116 /// 117 /// ``` 118 /// use `ed25519`_dalek::{ 119 /// verify_batch, SigningKey, VerifyingKey, Signer, Signature, 120 /// }; 121 /// use rand::rngs::OsRng; 122 /// 123 /// # fn main() { 124 /// let mut csprng = OsRng; 125 /// let signing_keys: Vec<_> = (0..64).map(|_| SigningKey::generate(&mut csprng)).collect(); 126 /// let msg: &[u8] = b"They're good dogs Brant"; 127 /// let messages: Vec<_> = (0..64).map(|_| msg).collect(); 128 /// let signatures: Vec<_> = signing_keys.iter().map(|key| key.sign(&msg)).collect(); 129 /// let verifying_keys: Vec<_> = signing_keys.iter().map(|key| key.verifying_key()).collect(); 130 /// 131 /// let result = verify_batch(&messages, &signatures, &verifying_keys); 132 /// assert!(result.is_ok()); 133 /// # } 134 /// ``` 135 #[allow(non_snake_case)] 136 pub fn verify_batch( 137 messages: &[&[u8]], 138 signatures: &[`ed25519`::Signature], 139 verifying_keys: &[VerifyingKey], 140 ) -> Result<(), SignatureError> { 141 // Return an Error if any of the vectors were not the same size as the others. 142 if signatures.len() != messages.len() 143 || signatures.len() != verifying_keys.len() 144 || verifying_keys.len() != messages.len() 145 { 146 return Err(InternalError::ArrayLength { 147 name_a: "signatures", 148 length_a: signatures.len(), 149 name_b: "messages", 150 length_b: messages.len(), 151 name_c: "verifying_keys", 152 length_c: verifying_keys.len(), 153 } 154 .into()); 155 } 156 157 // Make a transcript which logs all inputs to this function 158 let mut transcript: Transcript = Transcript::new(b"`ed25519` batch verification"); 159 160 // We make one optimization in the transcript: since we will end up computing H(R || A || M) 161 // for each (R, A, M) triplet, we will feed _that_ into our transcript rather than each R, A, M 162 // individually. Since R and A are fixed-length, this modification is secure so long as SHA-512 163 // is collision-resistant. 164 // It suffices to take `verifying_keys[i].as_bytes()` even though a `VerifyingKey` has two 165 // fields, and `as_bytes()` only returns the bytes of the first. This is because of an 166 // invariant guaranteed by `VerifyingKey`: the second field is always the (unique) 167 // decompression of the first. Thus, the serialized first field is a unique representation of 168 // the entire `VerifyingKey`. 169 let hrams: Vec<[u8; 64]> = (0..signatures.len()) 170 .map(|i| { 171 // Compute H(R || A || M), where 172 // R = sig.R 173 // A = verifying key 174 // M = msg 175 let mut h: Sha512 = Sha512::default(); 176 h.update(signatures[i].r_bytes()); 177 h.update(verifying_keys[i].as_bytes()); 178 h.update(messages[i]); 179 *h.finalize().as_ref() 180 }) 181 .collect(); 182 183 // Update transcript with the hashes above. This covers verifying_keys, messages, and the R 184 // half of signatures 185 for hram in hrams.iter() { 186 transcript.append_message(b"hram", hram); 187 } 188 // Update transcript with the rest of the data. This covers the s half of the signatures 189 for sig in signatures { 190 transcript.append_message(b"sig.s", sig.s_bytes()); 191 } 192 193 // All function inputs have now been hashed into the transcript. Finalize it and use it as 194 // randomness for the batch verification. 195 let mut rng = transcript.build_rng().finalize(&mut ZeroRng); 196 197 // Convert all signatures to `InternalSignature` 198 let signatures = signatures 199 .iter() 200 .map(InternalSignature::try_from) 201 .collect::, _>>()?; 202 // Convert the H(R || A || M) values into scalars 203 let hrams: Vec = hrams 204 .iter() 205 .map(Scalar::from_bytes_mod_order_wide) 206 .collect(); 207 208 // Select a random 128-bit scalar for each signature. 209 let zs: Vec = signatures 210 .iter() 211 .map(|_| Scalar::from(gen_u128(&mut rng))) 212 .collect(); 213 214 // Compute the basepoint coefficient, ∑ s[i]z[i] (mod l) 215 let B_coefficient: Scalar = signatures 216 .iter() 217 .map(|sig| sig.s) 218 .zip(zs.iter()) 219 .map(|(s, z)| z * s) 220 .sum(); 221 222 // Multiply each H(R || A || M) by the random value 223 let zhrams = hrams.iter().zip(zs.iter()).map(|(hram, z)| hram * z); 224 225 let Rs = signatures.iter().map(|sig| sig.R.decompress()); 226 let As = verifying_keys.iter().map(|pk| Some(pk.point)); 227 let B = once(Some(constants::ED25519_BASEPOINT_POINT)); 228 229 // Compute (-∑ z[i]s[i] (mod l)) B + ∑ z[i]R[i] + ∑ (z[i]H(R||A||M)[i] (mod l)) A[i] = 0 230 let id = EdwardsPoint::optional_multiscalar_mul( 231 once(-B_coefficient).chain(zs.iter().cloned()).chain(zhrams), 232 B.chain(Rs).chain(As), 233 ) 234 .ok_or(InternalError::Verify)?; 235 236 if id.is_identity() { 237 Ok(()) 238 } else { 239 Err(InternalError::Verify.into()) 240 } 241 }