Skip to main content

sui_types/
zk_login_authenticator.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4use crate::crypto::PublicKey;
5use crate::signature_verification::VerifiedDigestCache;
6use crate::{
7    base_types::{EpochId, SuiAddress},
8    crypto::{DefaultHash, Signature, SignatureScheme, SuiSignature},
9    digests::ZKLoginInputsDigest,
10    error::{SuiError, SuiErrorKind, SuiResult},
11    signature::{AuthenticatorTrait, VerifyParams},
12};
13use fastcrypto::{error::FastCryptoError, traits::ToFromBytes};
14use fastcrypto_zkp::bn254::zk_login::JwkId;
15use fastcrypto_zkp::bn254::zk_login::{JWK, OIDCProvider};
16use fastcrypto_zkp::bn254::zk_login_api::{ZkLoginCircuitMode, ZkLoginEnv};
17use fastcrypto_zkp::bn254::{zk_login::ZkLoginInputs, zk_login_api::verify_zk_login};
18use once_cell::sync::OnceCell;
19use schemars::JsonSchema;
20use serde::{Deserialize, Serialize};
21use shared_crypto::intent::IntentMessage;
22use std::hash::Hash;
23use std::hash::Hasher;
24use std::sync::Arc;
25#[cfg(test)]
26#[path = "unit_tests/zk_login_authenticator_test.rs"]
27mod zk_login_authenticator_test;
28
29/// An zk login authenticator with all the necessary fields.
30#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct ZkLoginAuthenticator {
33    pub inputs: ZkLoginInputs,
34    max_epoch: EpochId,
35    pub user_signature: Signature,
36    #[serde(skip)]
37    pub bytes: OnceCell<Vec<u8>>,
38}
39
40/// A helper struct that contains the necessary fields to calculate caching key.
41/// If the verify_zk_login() api changes, additional fields must be added here
42/// so the cache is not skipped.
43#[derive(Serialize, Deserialize)]
44struct ZkLoginCachingParams {
45    inputs: ZkLoginInputs,
46    max_epoch: EpochId,
47    extended_pk_bytes: Vec<u8>,
48    zklogin_circuit_mode: u64,
49}
50
51impl ZkLoginCachingParams {
52    fn digest(&self) -> ZKLoginInputsDigest {
53        use fastcrypto::hash::HashFunction;
54        let mut hasher = DefaultHash::default();
55        bcs::serialize_into(&mut hasher, self).expect("serde should not fail");
56        ZKLoginInputsDigest::new(hasher.finalize().into())
57    }
58}
59
60/// Map the protocol config circuit mode flag to the fastcrypto circuit mode:
61/// 0 = v1 circuit only, 1 = v2 circuit with fallback to v1 (migration mode), 2 = v2 circuit only.
62/// The flag comes from protocol config, so any other value is a config bug.
63pub fn zklogin_circuit_mode_from_flag(flag: u64) -> ZkLoginCircuitMode {
64    match flag {
65        0 => ZkLoginCircuitMode::V1Only,
66        1 => ZkLoginCircuitMode::Both,
67        2 => ZkLoginCircuitMode::V2Only,
68        _ => panic!("invalid zklogin circuit mode flag: {flag}"),
69    }
70}
71
72impl ZkLoginAuthenticator {
73    /// The caching key for zklogin signature, it is the hash of bcs bytes of
74    /// ZkLoginInputs || max_epoch || flagged_pk_bytes || zklogin_circuit_mode. If any of these
75    /// fields change, zklogin signature is re-verified without using the caching result.
76    fn get_caching_params(&self, zklogin_circuit_mode: u64) -> ZkLoginCachingParams {
77        let mut extended_pk_bytes = vec![self.user_signature.scheme().flag()];
78        extended_pk_bytes.extend(self.user_signature.public_key_bytes());
79        ZkLoginCachingParams {
80            inputs: self.inputs.clone(),
81            max_epoch: self.max_epoch,
82            extended_pk_bytes,
83            zklogin_circuit_mode,
84        }
85    }
86
87    pub fn hash_inputs(&self, zklogin_circuit_mode: u64) -> ZKLoginInputsDigest {
88        self.get_caching_params(zklogin_circuit_mode).digest()
89    }
90
91    /// Create a new [struct ZkLoginAuthenticator] with necessary fields.
92    pub fn new(inputs: ZkLoginInputs, max_epoch: EpochId, user_signature: Signature) -> Self {
93        Self {
94            inputs,
95            max_epoch,
96            user_signature,
97            bytes: OnceCell::new(),
98        }
99    }
100
101    pub fn get_pk(&self) -> SuiResult<PublicKey> {
102        PublicKey::from_zklogin_inputs(&self.inputs)
103    }
104
105    pub fn get_iss(&self) -> &str {
106        self.inputs.get_iss()
107    }
108
109    pub fn get_max_epoch(&self) -> EpochId {
110        self.max_epoch
111    }
112
113    pub fn user_signature_mut_for_testing(&mut self) -> &mut Signature {
114        &mut self.user_signature
115    }
116    pub fn max_epoch_mut_for_testing(&mut self) -> &mut EpochId {
117        &mut self.max_epoch
118    }
119    pub fn zk_login_inputs_mut_for_testing(&mut self) -> &mut ZkLoginInputs {
120        &mut self.inputs
121    }
122}
123
124/// Necessary trait for [struct SenderSignedData].
125impl PartialEq for ZkLoginAuthenticator {
126    fn eq(&self, other: &Self) -> bool {
127        self.as_ref() == other.as_ref()
128    }
129}
130
131/// Necessary trait for [struct SenderSignedData].
132impl Eq for ZkLoginAuthenticator {}
133
134/// Necessary trait for [struct SenderSignedData].
135impl Hash for ZkLoginAuthenticator {
136    fn hash<H: Hasher>(&self, state: &mut H) {
137        self.as_ref().hash(state);
138    }
139}
140
141impl AuthenticatorTrait for ZkLoginAuthenticator {
142    fn verify_user_authenticator_epoch(
143        &self,
144        epoch: EpochId,
145        max_epoch_upper_bound_delta: Option<u64>,
146    ) -> SuiResult {
147        // the checks here ensure that `current_epoch + max_epoch_upper_bound_delta >= self.max_epoch >= current_epoch`.
148        // 1. if the config for upper bound is set, ensure that the max epoch in signature is not larger than epoch + upper_bound.
149        if let Some(delta) = max_epoch_upper_bound_delta {
150            let max_epoch_upper_bound = epoch + delta;
151            if self.get_max_epoch() > max_epoch_upper_bound {
152                return Err(SuiErrorKind::InvalidSignature {
153                    error: format!(
154                        "ZKLogin max epoch too large {}, current epoch {}, max accepted: {}",
155                        self.get_max_epoch(),
156                        epoch,
157                        max_epoch_upper_bound
158                    ),
159                }
160                .into());
161            }
162        }
163        // 2. ensure that max epoch in signature is greater than the current epoch.
164        if epoch > self.get_max_epoch() {
165            return Err(SuiErrorKind::InvalidSignature {
166                error: format!(
167                    "ZKLogin expired at epoch {}, current epoch {}",
168                    self.get_max_epoch(),
169                    epoch
170                ),
171            }
172            .into());
173        }
174        Ok(())
175    }
176
177    /// Verify an intent message of a transaction with an zk login authenticator.
178    fn verify_claims<T>(
179        &self,
180        intent_msg: &IntentMessage<T>,
181        author: SuiAddress,
182        aux_verify_data: &VerifyParams,
183        zklogin_inputs_cache: Arc<VerifiedDigestCache<ZKLoginInputsDigest>>,
184    ) -> SuiResult
185    where
186        T: Serialize,
187    {
188        // Always evaluate the unpadded address derivation.
189        if author != SuiAddress::try_from_unpadded(&self.inputs)? {
190            // If the verify_legacy_zklogin_address flag is set, also evaluate the padded address derivation.
191            if !aux_verify_data.verify_legacy_zklogin_address
192                || author != SuiAddress::try_from_padded(&self.inputs)?
193            {
194                return Err(SuiErrorKind::InvalidAddress.into());
195            }
196        }
197
198        // Only when supported_providers list is not empty, we check if the provider is supported. Otherwise,
199        // we just use the JWK map to check if its supported.
200        if !aux_verify_data.supported_providers.is_empty()
201            && !aux_verify_data.supported_providers.contains(
202                &OIDCProvider::from_iss(self.inputs.get_iss()).map_err(|_| {
203                    SuiErrorKind::InvalidSignature {
204                        error: "Unknown provider".to_string(),
205                    }
206                })?,
207            )
208        {
209            return Err(SuiErrorKind::InvalidSignature {
210                error: format!("OIDC provider not supported: {}", self.inputs.get_iss()),
211            }
212            .into());
213        }
214
215        // zkLogin is classical (Groth16 over BN254); a PQ ephemeral key adds nothing.
216        match self.user_signature.scheme() {
217            SignatureScheme::ED25519 | SignatureScheme::Secp256k1 | SignatureScheme::Secp256r1 => {}
218            s => {
219                return Err(SuiErrorKind::InvalidSignature {
220                    error: format!("zkLogin ephemeral signature scheme not supported: {s}"),
221                }
222                .into());
223            }
224        }
225
226        aux_verify_data.reject_compact_signature_key(&self.user_signature)?;
227
228        // Verify the ephemeral signature over the intent message of the transaction data.
229        self.user_signature.verify_secure(
230            intent_msg,
231            author,
232            SignatureScheme::ZkLoginAuthenticator,
233        )?;
234
235        let zklogin_circuit_mode = aux_verify_data.zklogin_circuit_mode;
236        let caching_params = self.get_caching_params(zklogin_circuit_mode);
237        let inputs_digest = caching_params.digest();
238        if zklogin_inputs_cache.is_cached(&inputs_digest) {
239            // If the zklogin inputs hits the cache, we don't need to verify the zklogin
240            // again that contains the heavy computation.
241            Ok(())
242        } else {
243            // if it is not cached, we verify the full zklogin inputs.
244            verify_zklogin_inputs_wrapper(
245                caching_params,
246                &aux_verify_data.oidc_provider_jwks,
247                &aux_verify_data.zk_login_env,
248            )
249            .map_err(|e| -> SuiError {
250                SuiErrorKind::InvalidSignature {
251                    error: e.to_string(),
252                }
253                .into()
254            })?;
255            // If it's verified ok, we cache the digest.
256            zklogin_inputs_cache.cache_digest(inputs_digest);
257            Ok(())
258        }
259    }
260}
261
262fn verify_zklogin_inputs_wrapper(
263    params: ZkLoginCachingParams,
264    all_jwk: &imbl::HashMap<JwkId, JWK>,
265    env: &ZkLoginEnv,
266) -> SuiResult<()> {
267    verify_zk_login(
268        &params.inputs,
269        params.max_epoch,
270        &params.extended_pk_bytes,
271        all_jwk,
272        env,
273        zklogin_circuit_mode_from_flag(params.zklogin_circuit_mode),
274    )
275    .map_err(|e| {
276        SuiErrorKind::InvalidSignature {
277            error: e.to_string(),
278        }
279        .into()
280    })
281}
282
283impl ToFromBytes for ZkLoginAuthenticator {
284    fn from_bytes(bytes: &[u8]) -> Result<Self, FastCryptoError> {
285        // The first byte matches the flag of MultiSig.
286        if bytes.first().ok_or(FastCryptoError::InvalidInput)?
287            != &SignatureScheme::ZkLoginAuthenticator.flag()
288        {
289            return Err(FastCryptoError::InvalidInput);
290        }
291        let mut zk_login: ZkLoginAuthenticator =
292            bcs::from_bytes(&bytes[1..]).map_err(|_| FastCryptoError::InvalidSignature)?;
293        zk_login.inputs.init()?;
294        Ok(zk_login)
295    }
296}
297
298impl AsRef<[u8]> for ZkLoginAuthenticator {
299    fn as_ref(&self) -> &[u8] {
300        self.bytes
301            .get_or_try_init::<_, eyre::Report>(|| {
302                let as_bytes = bcs::to_bytes(self).expect("BCS serialization should not fail");
303                let mut bytes = Vec::with_capacity(1 + as_bytes.len());
304                bytes.push(SignatureScheme::ZkLoginAuthenticator.flag());
305                bytes.extend_from_slice(as_bytes.as_slice());
306                Ok(bytes)
307            })
308            .expect("OnceCell invariant violated")
309    }
310}
311
312#[derive(Debug, Clone)]
313pub struct AddressSeed([u8; 32]);
314
315impl AddressSeed {
316    pub fn unpadded(&self) -> &[u8] {
317        let mut buf = self.0.as_slice();
318
319        while !buf.is_empty() && buf[0] == 0 {
320            buf = &buf[1..];
321        }
322
323        // If the value is '0' then just return a slice of length 1 of the final byte
324        if buf.is_empty() { &self.0[31..] } else { buf }
325    }
326
327    pub fn padded(&self) -> &[u8] {
328        &self.0
329    }
330}
331
332impl std::fmt::Display for AddressSeed {
333    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
334        let big_int = num_bigint::BigUint::from_bytes_be(&self.0);
335        let radix10 = big_int.to_str_radix(10);
336        f.write_str(&radix10)
337    }
338}
339
340#[derive(thiserror::Error, Debug)]
341pub enum AddressSeedParseError {
342    #[error("unable to parse radix10 encoded value `{0}`")]
343    Parse(#[from] num_bigint::ParseBigIntError),
344    #[error("larger than 32 bytes")]
345    TooBig,
346}
347
348impl std::str::FromStr for AddressSeed {
349    type Err = AddressSeedParseError;
350
351    fn from_str(s: &str) -> Result<Self, Self::Err> {
352        let big_int = <num_bigint::BigUint as num_traits::Num>::from_str_radix(s, 10)?;
353        let be_bytes = big_int.to_bytes_be();
354        let len = be_bytes.len();
355        let mut buf = [0; 32];
356
357        if len > 32 {
358            return Err(AddressSeedParseError::TooBig);
359        }
360
361        buf[32 - len..].copy_from_slice(&be_bytes);
362        Ok(Self(buf))
363    }
364}
365
366// AddressSeed's serialized format is as a radix10 encoded string
367impl Serialize for AddressSeed {
368    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
369    where
370        S: serde::Serializer,
371    {
372        self.to_string().serialize(serializer)
373    }
374}
375
376impl<'de> Deserialize<'de> for AddressSeed {
377    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
378    where
379        D: serde::Deserializer<'de>,
380    {
381        let s = std::borrow::Cow::<'de, str>::deserialize(deserializer)?;
382        std::str::FromStr::from_str(&s).map_err(serde::de::Error::custom)
383    }
384}
385
386#[cfg(test)]
387mod test {
388    use std::str::FromStr;
389
390    use super::AddressSeed;
391    use num_bigint::BigUint;
392    use proptest::prelude::*;
393
394    #[test]
395    fn unpadded_slice() {
396        let seed = AddressSeed([0; 32]);
397        let zero: [u8; 1] = [0];
398        assert_eq!(seed.unpadded(), zero.as_slice());
399
400        let mut seed = AddressSeed([1; 32]);
401        seed.0[0] = 0;
402        assert_eq!(seed.unpadded(), [1; 31].as_slice());
403    }
404
405    proptest! {
406        #[test]
407        fn dont_crash_on_large_inputs(
408            bytes in proptest::collection::vec(any::<u8>(), 33..1024)
409        ) {
410            let big_int = BigUint::from_bytes_be(&bytes);
411            let radix10 = big_int.to_str_radix(10);
412
413            // doesn't crash
414            let _ = AddressSeed::from_str(&radix10);
415        }
416
417        #[test]
418        fn valid_address_seeds(
419            bytes in proptest::collection::vec(any::<u8>(), 1..=32)
420        ) {
421            let big_int = BigUint::from_bytes_be(&bytes);
422            let radix10 = big_int.to_str_radix(10);
423
424            let seed = AddressSeed::from_str(&radix10).unwrap();
425            assert_eq!(radix10, seed.to_string());
426            // Ensure unpadded doesn't crash
427            seed.unpadded();
428        }
429    }
430}