sui_sdk_types/crypto/
zklogin.rs

1use super::SimpleSignature;
2use crate::checkpoint::EpochId;
3use crate::u256::U256;
4
5/// A zklogin authenticator
6///
7/// # BCS
8///
9/// The BCS serialized form for this type is defined by the following ABNF:
10///
11/// ```text
12/// zklogin-bcs = bytes             ; contents are defined by <zklogin-authenticator>
13/// zklogin     = zklogin-flag
14///               zklogin-inputs
15///               u64               ; max epoch
16///               simple-signature    
17/// ```
18///
19/// Note: Due to historical reasons, signatures are serialized slightly different from the majority
20/// of the types in Sui. In particular if a signature is ever embedded in another structure it
21/// generally is serialized as `bytes` meaning it has a length prefix that defines the length of
22/// the completely serialized signature.
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
25pub struct ZkLoginAuthenticator {
26    /// Zklogin proof and inputs required to perform proof verification.
27    pub inputs: ZkLoginInputs,
28
29    /// Maximum epoch for which the proof is valid.
30    pub max_epoch: EpochId,
31
32    /// User signature with the pubkey attested to by the provided proof.
33    pub signature: SimpleSignature,
34}
35
36/// A zklogin groth16 proof and the required inputs to perform proof verification.
37///
38/// # BCS
39///
40/// The BCS serialized form for this type is defined by the following ABNF:
41///
42/// ```text
43/// zklogin-inputs = zklogin-proof
44///                  zklogin-claim
45///                  string              ; base64url-unpadded encoded JwtHeader
46///                  bn254-field-element ; address_seed
47/// ```
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct ZkLoginInputs {
50    proof_points: ZkLoginProof,
51    iss_base64_details: ZkLoginClaim,
52    header_base64: String,
53
54    jwt_header: JwtHeader,
55    jwk_id: JwkId,
56    public_identifier: ZkLoginPublicIdentifier,
57}
58
59impl ZkLoginInputs {
60    #[cfg(feature = "serde")]
61    #[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
62    pub fn new(
63        proof_points: ZkLoginProof,
64        iss_base64_details: ZkLoginClaim,
65        header_base64: String,
66        address_seed: Bn254FieldElement,
67    ) -> Result<Self, InvalidZkLoginAuthenticatorError> {
68        let iss = {
69            const ISS: &str = "iss";
70
71            let iss = iss_base64_details.verify_extended_claim(ISS)?;
72
73            if iss.len() > 255 {
74                return Err(InvalidZkLoginAuthenticatorError::new(
75                    "invalid iss: too long",
76                ));
77            }
78            iss
79        };
80
81        let jwt_header = JwtHeader::from_base64(&header_base64)?;
82        let jwk_id = JwkId {
83            iss: iss.clone(),
84            kid: jwt_header.kid.clone(),
85        };
86
87        let public_identifier = ZkLoginPublicIdentifier { iss, address_seed };
88
89        Ok(Self {
90            proof_points,
91            iss_base64_details,
92            header_base64,
93            jwt_header,
94            jwk_id,
95            public_identifier,
96        })
97    }
98
99    pub fn proof_points(&self) -> &ZkLoginProof {
100        &self.proof_points
101    }
102
103    pub fn iss_base64_details(&self) -> &ZkLoginClaim {
104        &self.iss_base64_details
105    }
106
107    pub fn header_base64(&self) -> &str {
108        &self.header_base64
109    }
110
111    pub fn address_seed(&self) -> &Bn254FieldElement {
112        &self.public_identifier.address_seed
113    }
114
115    pub fn jwk_id(&self) -> &JwkId {
116        &self.jwk_id
117    }
118
119    pub fn iss(&self) -> &str {
120        &self.public_identifier.iss
121    }
122
123    pub fn public_identifier(&self) -> &ZkLoginPublicIdentifier {
124        &self.public_identifier
125    }
126}
127
128#[cfg(feature = "proptest")]
129impl proptest::arbitrary::Arbitrary for ZkLoginInputs {
130    type Parameters = ();
131    type Strategy = proptest::strategy::BoxedStrategy<Self>;
132
133    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
134        use proptest::prelude::*;
135
136        (any::<ZkLoginProof>(), any::<Bn254FieldElement>())
137            .prop_map(|(proof_points, address_seed)| {
138                //TODO implement Arbitrary for real for ZkLoginClaim and header_base64 values
139                let iss_base64_details = ZkLoginClaim {
140                    value: "wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw".to_owned(),
141                    index_mod_4: 2,
142                };
143                let header_base64 = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ".to_owned();
144                Self::new(
145                    proof_points,
146                    iss_base64_details,
147                    header_base64,
148                    address_seed,
149                )
150                .unwrap()
151            })
152            .boxed()
153    }
154}
155
156/// A claim of the iss in a zklogin proof
157///
158/// # BCS
159///
160/// The BCS serialized form for this type is defined by the following ABNF:
161///
162/// ```text
163/// zklogin-claim = string u8
164/// ```
165#[derive(Debug, Clone, PartialEq, Eq)]
166#[cfg_attr(
167    feature = "serde",
168    derive(serde_derive::Serialize, serde_derive::Deserialize)
169)]
170#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
171pub struct ZkLoginClaim {
172    pub value: String,
173    pub index_mod_4: u8,
174}
175
176#[derive(Debug)]
177pub struct InvalidZkLoginAuthenticatorError(String);
178
179#[cfg(feature = "serde")]
180#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
181impl InvalidZkLoginAuthenticatorError {
182    fn new<T: Into<String>>(err: T) -> Self {
183        Self(err.into())
184    }
185}
186
187impl std::fmt::Display for InvalidZkLoginAuthenticatorError {
188    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189        write!(f, "invalid zklogin claim: {}", self.0)
190    }
191}
192
193impl std::error::Error for InvalidZkLoginAuthenticatorError {}
194
195#[cfg(feature = "serde")]
196#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
197impl ZkLoginClaim {
198    fn verify_extended_claim(
199        &self,
200        expected_key: &str,
201    ) -> Result<String, InvalidZkLoginAuthenticatorError> {
202        /// Map a base64 string to a bit array by taking each char's index and convert it to binary form with one bit per u8
203        /// element in the output. Returns InvalidZkLoginClaimError if one of the characters is not in the base64 charset.
204        fn base64_to_bitarray(input: &str) -> Result<Vec<u8>, InvalidZkLoginAuthenticatorError> {
205            use itertools::Itertools;
206
207            const BASE64_URL_CHARSET: &str =
208                "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
209
210            input
211                .chars()
212                .map(|c| {
213                    BASE64_URL_CHARSET
214                        .find(c)
215                        .map(|index| index as u8)
216                        .map(|index| (0..6).rev().map(move |i| (index >> i) & 1))
217                        .ok_or_else(|| {
218                            InvalidZkLoginAuthenticatorError::new("base64_to_bitarry invalid input")
219                        })
220                })
221                .flatten_ok()
222                .collect()
223        }
224
225        /// Convert a bitarray (each bit is represented by a u8) to a byte array by taking each 8 bits as a
226        /// byte in big-endian format.
227        fn bitarray_to_bytearray(bits: &[u8]) -> Result<Vec<u8>, InvalidZkLoginAuthenticatorError> {
228            #[expect(clippy::manual_is_multiple_of)]
229            if bits.len() % 8 != 0 {
230                return Err(InvalidZkLoginAuthenticatorError::new(
231                    "bitarray_to_bytearray invalid input",
232                ));
233            }
234            Ok(bits
235                .chunks(8)
236                .map(|chunk| {
237                    let mut byte = 0u8;
238                    for (i, bit) in chunk.iter().rev().enumerate() {
239                        byte |= bit << i;
240                    }
241                    byte
242                })
243                .collect())
244        }
245
246        /// Parse the base64 string, add paddings based on offset, and convert to a bytearray.
247        fn decode_base64_url(
248            s: &str,
249            index_mod_4: &u8,
250        ) -> Result<String, InvalidZkLoginAuthenticatorError> {
251            if s.len() < 2 {
252                return Err(InvalidZkLoginAuthenticatorError::new(
253                    "Base64 string smaller than 2",
254                ));
255            }
256            let mut bits = base64_to_bitarray(s)?;
257            match index_mod_4 {
258                0 => {}
259                1 => {
260                    bits.drain(..2);
261                }
262                2 => {
263                    bits.drain(..4);
264                }
265                _ => {
266                    return Err(InvalidZkLoginAuthenticatorError::new(
267                        "Invalid first_char_offset",
268                    ));
269                }
270            }
271
272            let last_char_offset = (index_mod_4 + s.len() as u8 - 1) % 4;
273            match last_char_offset {
274                3 => {}
275                2 => {
276                    bits.drain(bits.len() - 2..);
277                }
278                1 => {
279                    bits.drain(bits.len() - 4..);
280                }
281                _ => {
282                    return Err(InvalidZkLoginAuthenticatorError::new(
283                        "Invalid last_char_offset",
284                    ));
285                }
286            }
287
288            if bits.len() % 8 != 0 {
289                return Err(InvalidZkLoginAuthenticatorError::new("Invalid bits length"));
290            }
291
292            Ok(std::str::from_utf8(&bitarray_to_bytearray(&bits)?)
293                .map_err(|_| InvalidZkLoginAuthenticatorError::new("Invalid UTF8 string"))?
294                .to_owned())
295        }
296
297        let extended_claim = decode_base64_url(&self.value, &self.index_mod_4)?;
298
299        // Last character of each extracted_claim must be '}' or ','
300        if !(extended_claim.ends_with('}') || extended_claim.ends_with(',')) {
301            return Err(InvalidZkLoginAuthenticatorError::new(
302                "Invalid extended claim",
303            ));
304        }
305
306        let json_str = format!("{{{}}}", &extended_claim[..extended_claim.len() - 1]);
307
308        serde_json::from_str::<serde_json::Value>(&json_str)
309            .map_err(|e| InvalidZkLoginAuthenticatorError::new(e.to_string()))?
310            .as_object_mut()
311            .and_then(|o| o.get_mut(expected_key))
312            .map(serde_json::Value::take)
313            .and_then(|v| match v {
314                serde_json::Value::String(s) => Some(s),
315                _ => None,
316            })
317            .ok_or_else(|| InvalidZkLoginAuthenticatorError::new("invalid extended claim"))
318    }
319}
320
321/// Struct that represents a standard JWT header according to
322/// https://openid.net/specs/openid-connect-core-1_0.html
323#[derive(Debug, Clone, PartialEq, Eq)]
324struct JwtHeader {
325    alg: String,
326    kid: String,
327    typ: Option<String>,
328}
329
330impl JwtHeader {
331    #[cfg(feature = "serde")]
332    fn from_base64(s: &str) -> Result<Self, InvalidZkLoginAuthenticatorError> {
333        use base64ct::Base64UrlUnpadded;
334        use base64ct::Encoding;
335
336        #[derive(serde_derive::Serialize, serde_derive::Deserialize)]
337        struct Header {
338            alg: String,
339            kid: String,
340            #[serde(skip_serializing_if = "Option::is_none")]
341            typ: Option<String>,
342        }
343
344        let header_bytes = Base64UrlUnpadded::decode_vec(s)
345            .map_err(|e| InvalidZkLoginAuthenticatorError::new(format!("invalid base64: {e}")))?;
346        let Header { alg, kid, typ } = serde_json::from_slice(&header_bytes)
347            .map_err(|e| InvalidZkLoginAuthenticatorError::new(format!("invalid json: {e}")))?;
348        if alg != "RS256" {
349            return Err(InvalidZkLoginAuthenticatorError::new(
350                "jwt alg must be RS256",
351            ));
352        }
353        Ok(Self { alg, kid, typ })
354    }
355}
356
357/// A zklogin groth16 proof
358///
359/// # BCS
360///
361/// The BCS serialized form for this type is defined by the following ABNF:
362///
363/// ```text
364/// zklogin-proof = circom-g1 circom-g2 circom-g1
365/// ```
366#[derive(Debug, Clone, PartialEq, Eq)]
367#[cfg_attr(
368    feature = "serde",
369    derive(serde_derive::Serialize, serde_derive::Deserialize)
370)]
371#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
372pub struct ZkLoginProof {
373    pub a: CircomG1,
374    pub b: CircomG2,
375    pub c: CircomG1,
376}
377
378/// A G1 point
379///
380/// This represents the canonical decimal representation of the projective coordinates in Fq.
381///
382/// # BCS
383///
384/// The BCS serialized form for this type is defined by the following ABNF:
385///
386/// ```text
387/// circom-g1 = %x03 3(bn254-field-element)
388/// ```
389#[derive(Clone, Debug, PartialEq, Eq)]
390#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
391pub struct CircomG1(pub [Bn254FieldElement; 3]);
392
393/// A G2 point
394///
395/// This represents the canonical decimal representation of the coefficients of the projective
396/// coordinates in Fq2.
397///
398/// # BCS
399///
400/// The BCS serialized form for this type is defined by the following ABNF:
401///
402/// ```text
403/// circom-g2 = %x03 3(%x02 2(bn254-field-element))
404/// ```
405#[derive(Clone, Debug, PartialEq, Eq)]
406#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
407pub struct CircomG2(pub [[Bn254FieldElement; 2]; 3]);
408
409/// Public Key equivalent for Zklogin authenticators
410///
411/// A `ZkLoginPublicIdentifier` is the equivalent of a public key for other account authenticators,
412/// and contains the information required to derive the onchain account [`Address`] for a Zklogin
413/// authenticator.
414///
415/// ## Note
416///
417/// Due to a historical bug that was introduced in the Sui Typescript SDK when the zklogin
418/// authenticator was first introduced, there are now possibly two "valid" addresses for each
419/// zklogin authenticator depending on the bit-pattern of the `address_seed` value.
420///
421/// The original bug incorrectly derived a zklogin's address by stripping any leading
422/// zero-bytes that could have been present in the 32-byte length `address_seed` value prior to
423/// hashing, leading to a different derived address. This incorrectly derived address was
424/// presented to users of various wallets, leading them to sending funds to these addresses
425/// that they couldn't access. Instead of letting these users lose any assets that were sent to
426/// these addresses, the Sui network decided to change the protocol to allow for a zklogin
427/// authenticator who's `address_seed` value had leading zero-bytes be authorized to sign for
428/// both the addresses derived from both the unpadded and padded `address_seed` value.
429///
430/// # BCS
431///
432/// The BCS serialized form for this type is defined by the following ABNF:
433///
434/// ```text
435/// zklogin-public-identifier-bcs = bytes ; where the contents are defined by
436///                                       ; <zklogin-public-identifier>
437///
438/// zklogin-public-identifier = zklogin-public-identifier-iss
439///                             address-seed
440///
441/// zklogin-public-identifier-unpadded = zklogin-public-identifier-iss
442///                                      address-seed-unpadded
443///
444/// ; The iss, or issuer, is a utf8 string that is less than 255 bytes long
445/// ; and is serialized with the iss's length in bytes as a u8 followed by
446/// ; the bytes of the iss
447/// zklogin-public-identifier-iss = u8 *255(OCTET)
448///
449/// ; A Bn254FieldElement serialized as a 32-byte big-endian value
450/// address-seed = 32(OCTET)
451///
452/// ; A Bn254FieldElement serialized as a 32-byte big-endian value
453/// ; with any leading zero bytes stripped
454/// address-seed-unpadded = %x00 / %x01-ff *31(OCTET)
455/// ```
456///
457/// [`Address`]: crate::Address
458#[derive(Clone, Debug, PartialEq, Eq)]
459#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
460pub struct ZkLoginPublicIdentifier {
461    iss: String,
462    address_seed: Bn254FieldElement,
463}
464
465impl ZkLoginPublicIdentifier {
466    pub fn new(iss: String, address_seed: Bn254FieldElement) -> Option<Self> {
467        if iss.len() > 255 {
468            None
469        } else {
470            Some(Self { iss, address_seed })
471        }
472    }
473
474    pub fn iss(&self) -> &str {
475        &self.iss
476    }
477
478    pub fn address_seed(&self) -> &Bn254FieldElement {
479        &self.address_seed
480    }
481}
482
483/// A JSON Web Key
484///
485/// Struct that contains info for a JWK. A list of them for different kids can
486/// be retrieved from the JWK endpoint (e.g. <https://www.googleapis.com/oauth2/v3/certs>).
487/// The JWK is used to verify the JWT token.
488///
489/// # BCS
490///
491/// The BCS serialized form for this type is defined by the following ABNF:
492///
493/// ```text
494/// jwk = string string string string
495/// ```
496#[derive(Clone, Debug, PartialEq, Eq, Hash)]
497#[cfg_attr(
498    feature = "serde",
499    derive(serde_derive::Serialize, serde_derive::Deserialize)
500)]
501#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
502pub struct Jwk {
503    /// Key type parameter, <https://datatracker.ietf.org/doc/html/rfc7517#section-4.1>
504    pub kty: String,
505
506    /// RSA public exponent, <https://datatracker.ietf.org/doc/html/rfc7517#section-9.3>
507    pub e: String,
508
509    /// RSA modulus, <https://datatracker.ietf.org/doc/html/rfc7517#section-9.3>
510    pub n: String,
511
512    /// Algorithm parameter, <https://datatracker.ietf.org/doc/html/rfc7517#section-4.4>
513    pub alg: String,
514}
515
516/// Key to uniquely identify a JWK
517///
518/// # BCS
519///
520/// The BCS serialized form for this type is defined by the following ABNF:
521///
522/// ```text
523/// jwk-id = string string
524/// ```
525#[derive(Clone, Debug, PartialEq, Eq, Hash)]
526#[cfg_attr(
527    feature = "serde",
528    derive(serde_derive::Serialize, serde_derive::Deserialize)
529)]
530#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
531pub struct JwkId {
532    /// The issuer or identity of the OIDC provider.
533    pub iss: String,
534
535    /// A key id use to uniquely identify a key from an OIDC provider.
536    pub kid: String,
537}
538
539/// A point on the BN254 elliptic curve.
540///
541/// This is a 32-byte, or 256-bit, value that is generally represented as radix10 when a
542/// human-readable display format is needed, and is represented as a 32-byte big-endian value while
543/// in memory.
544///
545/// # BCS
546///
547/// The BCS serialized form for this type is defined by the following ABNF:
548///
549/// ```text
550/// bn254-field-element = *DIGIT ; which is then interpreted as a radix10 encoded 32-byte value
551/// ```
552#[derive(Clone, Debug, Default, PartialEq, Eq)]
553#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
554pub struct Bn254FieldElement([u8; 32]);
555
556impl Bn254FieldElement {
557    pub const fn new(bytes: [u8; 32]) -> Self {
558        Self(bytes)
559    }
560
561    pub const fn from_str_radix_10(s: &str) -> Result<Self, Bn254FieldElementParseError> {
562        let u256 = match U256::from_str_radix(s, 10) {
563            Ok(u256) => u256,
564            Err(e) => return Err(Bn254FieldElementParseError(e)),
565        };
566        let be = u256.to_be();
567        Ok(Self(*be.digits()))
568    }
569
570    pub fn unpadded(&self) -> &[u8] {
571        let mut buf = self.0.as_slice();
572
573        while !buf.is_empty() && buf[0] == 0 {
574            buf = &buf[1..];
575        }
576
577        // If the value is '0' then just return a slice of length 1 of the final byte
578        if buf.is_empty() { &self.0[31..] } else { buf }
579    }
580
581    pub fn padded(&self) -> &[u8] {
582        &self.0
583    }
584}
585
586impl std::fmt::Display for Bn254FieldElement {
587    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
588        let u256 = U256::from_be(U256::from_digits(self.0));
589        let radix10 = u256.to_str_radix(10);
590        f.write_str(&radix10)
591    }
592}
593
594#[derive(Debug)]
595pub struct Bn254FieldElementParseError(bnum::errors::ParseIntError);
596
597impl std::fmt::Display for Bn254FieldElementParseError {
598    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
599        write!(f, "unable to parse radix10 encoded value {}", self.0)
600    }
601}
602
603impl std::error::Error for Bn254FieldElementParseError {}
604
605impl std::str::FromStr for Bn254FieldElement {
606    type Err = Bn254FieldElementParseError;
607
608    fn from_str(s: &str) -> Result<Self, Self::Err> {
609        let u256 = U256::from_str_radix(s, 10).map_err(Bn254FieldElementParseError)?;
610        let be = u256.to_be();
611        Ok(Self(*be.digits()))
612    }
613}
614
615#[cfg(test)]
616mod test {
617    use super::Bn254FieldElement;
618    use num_bigint::BigUint;
619    use proptest::prelude::*;
620    use std::str::FromStr;
621    use test_strategy::proptest;
622
623    #[cfg(target_arch = "wasm32")]
624    use wasm_bindgen_test::wasm_bindgen_test as test;
625
626    #[test]
627    fn unpadded_slice() {
628        let seed = Bn254FieldElement([0; 32]);
629        let zero: [u8; 1] = [0];
630        assert_eq!(seed.unpadded(), zero.as_slice());
631
632        let mut seed = Bn254FieldElement([1; 32]);
633        seed.0[0] = 0;
634        assert_eq!(seed.unpadded(), [1; 31].as_slice());
635    }
636
637    #[proptest]
638    fn dont_crash_on_large_inputs(
639        #[strategy(proptest::collection::vec(any::<u8>(), 33..1024))] bytes: Vec<u8>,
640    ) {
641        let big_int = BigUint::from_bytes_be(&bytes);
642        let radix10 = big_int.to_str_radix(10);
643
644        // doesn't crash
645        let _ = Bn254FieldElement::from_str(&radix10);
646    }
647
648    #[proptest]
649    fn valid_address_seeds(
650        #[strategy(proptest::collection::vec(any::<u8>(), 1..=32))] bytes: Vec<u8>,
651    ) {
652        let big_int = BigUint::from_bytes_be(&bytes);
653        let radix10 = big_int.to_str_radix(10);
654
655        let seed = Bn254FieldElement::from_str(&radix10).unwrap();
656        assert_eq!(radix10, seed.to_string());
657        // Ensure unpadded doesn't crash
658        seed.unpadded();
659    }
660}
661
662#[cfg(feature = "serde")]
663#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
664mod serialization {
665    use crate::SignatureScheme;
666
667    use super::*;
668    use serde::Deserialize;
669    use serde::Deserializer;
670    use serde::Serialize;
671    use serde::Serializer;
672    use serde_with::Bytes;
673    use serde_with::DeserializeAs;
674    use serde_with::SerializeAs;
675    use std::borrow::Cow;
676
677    // Serialized format is: iss_bytes_len || iss_bytes || padded_32_byte_address_seed.
678    impl Serialize for ZkLoginPublicIdentifier {
679        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
680        where
681            S: Serializer,
682        {
683            if serializer.is_human_readable() {
684                #[derive(serde_derive::Serialize)]
685                struct Readable<'a> {
686                    iss: &'a str,
687                    address_seed: &'a Bn254FieldElement,
688                }
689                let readable = Readable {
690                    iss: &self.iss,
691                    address_seed: &self.address_seed,
692                };
693                readable.serialize(serializer)
694            } else {
695                let mut buf = Vec::new();
696                let iss_bytes = self.iss.as_bytes();
697                buf.push(iss_bytes.len() as u8);
698                buf.extend(iss_bytes);
699
700                buf.extend(&self.address_seed.0);
701
702                serializer.serialize_bytes(&buf)
703            }
704        }
705    }
706
707    impl<'de> Deserialize<'de> for ZkLoginPublicIdentifier {
708        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
709        where
710            D: Deserializer<'de>,
711        {
712            if deserializer.is_human_readable() {
713                #[derive(serde_derive::Deserialize)]
714                struct Readable {
715                    iss: String,
716                    address_seed: Bn254FieldElement,
717                }
718
719                let Readable { iss, address_seed } = Deserialize::deserialize(deserializer)?;
720                Self::new(iss, address_seed)
721                    .ok_or_else(|| serde::de::Error::custom("invalid zklogin public identifier"))
722            } else {
723                let bytes: Cow<'de, [u8]> = Bytes::deserialize_as(deserializer)?;
724                let iss_len = *bytes
725                    .first()
726                    .ok_or_else(|| serde::de::Error::custom("invalid zklogin public identifier"))?;
727                let iss_bytes = bytes
728                    .get(1..(1 + iss_len as usize))
729                    .ok_or_else(|| serde::de::Error::custom("invalid zklogin public identifier"))?;
730                let iss = std::str::from_utf8(iss_bytes).map_err(serde::de::Error::custom)?;
731                let address_seed_bytes = bytes
732                    .get((1 + iss_len as usize)..)
733                    .ok_or_else(|| serde::de::Error::custom("invalid zklogin public identifier"))?;
734
735                let address_seed = <[u8; 32]>::try_from(address_seed_bytes)
736                    .map_err(serde::de::Error::custom)
737                    .map(Bn254FieldElement)?;
738
739                Self::new(iss.into(), address_seed)
740                    .ok_or_else(|| serde::de::Error::custom("invalid zklogin public identifier"))
741            }
742        }
743    }
744
745    #[derive(serde_derive::Serialize)]
746    struct AuthenticatorRef<'a> {
747        inputs: &'a ZkLoginInputs,
748        max_epoch: EpochId,
749        signature: &'a SimpleSignature,
750    }
751
752    #[derive(serde_derive::Deserialize)]
753    struct Authenticator {
754        inputs: ZkLoginInputs,
755        max_epoch: EpochId,
756        signature: SimpleSignature,
757    }
758
759    impl Serialize for ZkLoginAuthenticator {
760        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
761        where
762            S: Serializer,
763        {
764            if serializer.is_human_readable() {
765                let authenticator_ref = AuthenticatorRef {
766                    inputs: &self.inputs,
767                    max_epoch: self.max_epoch,
768                    signature: &self.signature,
769                };
770
771                authenticator_ref.serialize(serializer)
772            } else {
773                let bytes = self.to_bytes();
774                serializer.serialize_bytes(&bytes)
775            }
776        }
777    }
778
779    impl<'de> Deserialize<'de> for ZkLoginAuthenticator {
780        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
781        where
782            D: Deserializer<'de>,
783        {
784            if deserializer.is_human_readable() {
785                let Authenticator {
786                    inputs,
787                    max_epoch,
788                    signature,
789                } = Authenticator::deserialize(deserializer)?;
790                Ok(Self {
791                    inputs,
792                    max_epoch,
793                    signature,
794                })
795            } else {
796                let bytes: Cow<'de, [u8]> = Bytes::deserialize_as(deserializer)?;
797                Self::from_serialized_bytes(bytes)
798            }
799        }
800    }
801
802    impl ZkLoginAuthenticator {
803        pub(crate) fn to_bytes(&self) -> Vec<u8> {
804            let authenticator_ref = AuthenticatorRef {
805                inputs: &self.inputs,
806                max_epoch: self.max_epoch,
807                signature: &self.signature,
808            };
809
810            let mut buf = Vec::new();
811            buf.push(SignatureScheme::ZkLogin as u8);
812
813            bcs::serialize_into(&mut buf, &authenticator_ref).expect("serialization cannot fail");
814            buf
815        }
816
817        pub(crate) fn from_serialized_bytes<T: AsRef<[u8]>, E: serde::de::Error>(
818            bytes: T,
819        ) -> Result<Self, E> {
820            let bytes = bytes.as_ref();
821            let flag = SignatureScheme::from_byte(
822                *bytes
823                    .first()
824                    .ok_or_else(|| serde::de::Error::custom("missing signature scheme flag"))?,
825            )
826            .map_err(serde::de::Error::custom)?;
827            if flag != SignatureScheme::ZkLogin {
828                return Err(serde::de::Error::custom("invalid zklogin flag"));
829            }
830            let bcs_bytes = &bytes[1..];
831
832            let Authenticator {
833                inputs,
834                max_epoch,
835                signature,
836            } = bcs::from_bytes(bcs_bytes).map_err(serde::de::Error::custom)?;
837            Ok(Self {
838                inputs,
839                max_epoch,
840                signature,
841            })
842        }
843    }
844
845    impl Serialize for ZkLoginInputs {
846        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
847        where
848            S: Serializer,
849        {
850            #[derive(serde_derive::Serialize)]
851            struct Inputs<'a> {
852                proof_points: &'a ZkLoginProof,
853                iss_base64_details: &'a ZkLoginClaim,
854                header_base64: &'a str,
855                address_seed: &'a Bn254FieldElement,
856            }
857
858            Inputs {
859                proof_points: self.proof_points(),
860                iss_base64_details: self.iss_base64_details(),
861                header_base64: self.header_base64(),
862                address_seed: self.address_seed(),
863            }
864            .serialize(serializer)
865        }
866    }
867
868    impl<'de> Deserialize<'de> for ZkLoginInputs {
869        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
870        where
871            D: Deserializer<'de>,
872        {
873            #[derive(serde_derive::Deserialize)]
874            struct Inputs {
875                proof_points: ZkLoginProof,
876                iss_base64_details: ZkLoginClaim,
877                header_base64: String,
878                address_seed: Bn254FieldElement,
879            }
880
881            let Inputs {
882                proof_points,
883                iss_base64_details,
884                header_base64,
885                address_seed,
886            } = Inputs::deserialize(deserializer)?;
887            Self::new(
888                proof_points,
889                iss_base64_details,
890                header_base64,
891                address_seed,
892            )
893            .map_err(serde::de::Error::custom)
894        }
895    }
896
897    // AddressSeed's serialized format is as a radix10 encoded string
898    impl Serialize for Bn254FieldElement {
899        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
900        where
901            S: serde::Serializer,
902        {
903            serde_with::DisplayFromStr::serialize_as(self, serializer)
904        }
905    }
906
907    impl<'de> Deserialize<'de> for Bn254FieldElement {
908        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
909        where
910            D: Deserializer<'de>,
911        {
912            serde_with::DisplayFromStr::deserialize_as(deserializer)
913        }
914    }
915
916    impl Serialize for CircomG1 {
917        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
918        where
919            S: serde::Serializer,
920        {
921            use serde::ser::SerializeSeq;
922            let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
923            for element in &self.0 {
924                seq.serialize_element(element)?;
925            }
926            seq.end()
927        }
928    }
929
930    impl<'de> Deserialize<'de> for CircomG1 {
931        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
932        where
933            D: Deserializer<'de>,
934        {
935            let inner = <Vec<_>>::deserialize(deserializer)?;
936            Ok(Self(inner.try_into().map_err(|_| {
937                serde::de::Error::custom("expected array of length 3")
938            })?))
939        }
940    }
941
942    impl Serialize for CircomG2 {
943        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
944        where
945            S: serde::Serializer,
946        {
947            use serde::ser::SerializeSeq;
948
949            struct Inner<'a>(&'a [Bn254FieldElement; 2]);
950
951            impl Serialize for Inner<'_> {
952                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
953                where
954                    S: serde::Serializer,
955                {
956                    let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
957                    for element in self.0 {
958                        seq.serialize_element(element)?;
959                    }
960                    seq.end()
961                }
962            }
963
964            let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
965            for element in &self.0 {
966                seq.serialize_element(&Inner(element))?;
967            }
968            seq.end()
969        }
970    }
971
972    impl<'de> Deserialize<'de> for CircomG2 {
973        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
974        where
975            D: Deserializer<'de>,
976        {
977            let vecs = <Vec<Vec<Bn254FieldElement>>>::deserialize(deserializer)?;
978            let mut inner: [[Bn254FieldElement; 2]; 3] = Default::default();
979
980            if vecs.len() != 3 {
981                return Err(serde::de::Error::custom(
982                    "vector of three vectors each being a vector of two strings",
983                ));
984            }
985
986            for (i, v) in vecs.into_iter().enumerate() {
987                if v.len() != 2 {
988                    return Err(serde::de::Error::custom(
989                        "vector of three vectors each being a vector of two strings",
990                    ));
991                }
992
993                for (j, point) in v.into_iter().enumerate() {
994                    inner[i][j] = point;
995                }
996            }
997
998            Ok(Self(inner))
999        }
1000    }
1001}