sui_sdk_types/crypto/
mod.rs

1mod bls12381;
2mod ed25519;
3mod intent;
4mod multisig;
5mod passkey;
6mod secp256k1;
7mod secp256r1;
8mod signature;
9mod validator;
10mod zklogin;
11
12pub use bls12381::Bls12381PublicKey;
13pub use bls12381::Bls12381Signature;
14pub use ed25519::Ed25519PublicKey;
15pub use ed25519::Ed25519Signature;
16pub use intent::Intent;
17pub use intent::IntentAppId;
18pub use intent::IntentScope;
19pub use intent::IntentVersion;
20pub use multisig::MultisigAggregatedSignature;
21pub use multisig::MultisigCommittee;
22pub use multisig::MultisigMember;
23pub use multisig::MultisigMemberPublicKey;
24pub use multisig::MultisigMemberSignature;
25pub use passkey::PasskeyAuthenticator;
26pub use passkey::PasskeyPublicKey;
27pub use secp256k1::Secp256k1PublicKey;
28pub use secp256k1::Secp256k1Signature;
29pub use secp256r1::Secp256r1PublicKey;
30pub use secp256r1::Secp256r1Signature;
31pub use signature::SignatureScheme;
32pub use signature::SimpleSignature;
33pub use signature::UserSignature;
34pub use validator::ValidatorAggregatedSignature;
35pub use validator::ValidatorCommittee;
36pub use validator::ValidatorCommitteeMember;
37pub use validator::ValidatorSignature;
38pub use zklogin::Bn254FieldElement;
39pub use zklogin::CircomG1;
40pub use zklogin::CircomG2;
41pub use zklogin::Jwk;
42pub use zklogin::JwkId;
43pub use zklogin::ZkLoginAuthenticator;
44pub use zklogin::ZkLoginClaim;
45pub use zklogin::ZkLoginInputs;
46pub use zklogin::ZkLoginProof;
47pub use zklogin::ZkLoginPublicIdentifier;
48
49//
50// Implement various base64 fixed-size array helpers
51//
52
53/// Utility for calculating base64 encoding lenghths.
54///
55/// In the Base64 encoding each character is used to represent 6 bits (log2(64) = 6). This means
56/// that 4 characters are used to represnet 4*6 = 24 bits = 3 bytes. So you need 4*(`n`/3)
57/// characters in order to represent `n` bytes, and this needs to be rounded up to a multiple of 4.
58/// The number of unused padding characters resulting from the rounding will be 0, 1, 2, or 3.
59const fn base64_encoded_length(len: usize) -> usize {
60    ((4 * len / 3) + 3) & !3
61}
62
63macro_rules! impl_base64_helper {
64    ($base:ident, $display:ident, $fromstr:ident, $test_module:ident, $array_length:literal) => {
65        #[allow(unused)]
66        struct $base;
67
68        impl $base {
69            const LENGTH: usize = $array_length;
70            #[allow(unused)]
71            const ENCODED_LENGTH: usize = base64_encoded_length(Self::LENGTH);
72        }
73
74        #[allow(unused)]
75        struct $display<'a>(&'a [u8; $base::LENGTH]);
76
77        impl<'a> std::fmt::Display for $display<'a> {
78            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79                let mut buf = [0; $base::ENCODED_LENGTH];
80                let encoded =
81                    <base64ct::Base64 as base64ct::Encoding>::encode(self.0, &mut buf).unwrap();
82                f.write_str(encoded)
83            }
84        }
85
86        #[allow(unused)]
87        #[derive(Debug, PartialEq)]
88        #[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
89        struct $fromstr([u8; $base::LENGTH]);
90
91        impl std::str::FromStr for $fromstr {
92            type Err = base64ct::Error;
93
94            fn from_str(s: &str) -> Result<Self, Self::Err> {
95                let mut buf = [0; $base::LENGTH];
96                let decoded = <base64ct::Base64 as base64ct::Encoding>::decode(s, &mut buf)?;
97                assert_eq!(decoded.len(), $base::LENGTH);
98                Ok(Self(buf))
99            }
100        }
101
102        #[cfg(feature = "serde")]
103        #[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
104        impl serde_with::SerializeAs<[u8; Self::LENGTH]> for $base {
105            fn serialize_as<S>(
106                source: &[u8; Self::LENGTH],
107                serializer: S,
108            ) -> Result<S::Ok, S::Error>
109            where
110                S: serde::Serializer,
111            {
112                let display = $display(source);
113                serde_with::DisplayFromStr::serialize_as(&display, serializer)
114            }
115        }
116
117        #[cfg(feature = "serde")]
118        #[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
119        impl<'de> serde_with::DeserializeAs<'de, [u8; Self::LENGTH]> for $base {
120            fn deserialize_as<D>(deserializer: D) -> Result<[u8; Self::LENGTH], D::Error>
121            where
122                D: serde::Deserializer<'de>,
123            {
124                let array: $fromstr = serde_with::DisplayFromStr::deserialize_as(deserializer)?;
125                Ok(array.0)
126            }
127        }
128
129        #[cfg(test)]
130        mod $test_module {
131            use super::$display;
132            use super::$fromstr;
133            use test_strategy::proptest;
134
135            #[cfg(target_arch = "wasm32")]
136            use wasm_bindgen_test::wasm_bindgen_test as test;
137
138            #[proptest]
139            fn roundtrip_display_fromstr(array: $fromstr) {
140                let s = $display(&array.0).to_string();
141                let a = s.parse::<$fromstr>().unwrap();
142                assert_eq!(array, a);
143            }
144        }
145    };
146}
147
148impl_base64_helper!(Base64Array32, Base64Display32, Base64FromStr32, test32, 32);
149impl_base64_helper!(Base64Array33, Base64Display33, Base64FromStr33, test33, 33);
150impl_base64_helper!(Base64Array34, Base64Display34, Base64FromStr34, test34, 34);
151impl_base64_helper!(Base64Array48, Base64Display48, Base64FromStr48, test48, 48);
152impl_base64_helper!(Base64Array64, Base64Display64, Base64FromStr64, test64, 64);
153impl_base64_helper!(Base64Array96, Base64Display96, Base64FromStr96, test96, 96);