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