-
Notifications
You must be signed in to change notification settings - Fork 3
feat(ffi): Add FFI types for Bls12381 #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6bdd13
feat(ffi): Add FFI types for Bls12381
DaughterOfMars dcdc457
Merge branch 'sdk-bindings' into feat/bls12381-ffi
DaughterOfMars 76a3d3a
remove clone
DaughterOfMars b9404c4
add bitmap getter
DaughterOfMars cb2ef66
update bindings
DaughterOfMars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Copyright (c) 2025 IOTA Stiftung | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use std::sync::RwLock; | ||
|
|
||
| use iota_types::SignatureScheme; | ||
| use rand::rngs::OsRng; | ||
|
|
||
| use crate::{ | ||
| error::{Result, SdkFfiError}, | ||
| types::{ | ||
| checkpoint::CheckpointSummary, | ||
| crypto::{ | ||
| Bls12381PublicKey, Bls12381Signature, | ||
| validator::{ValidatorAggregatedSignature, ValidatorCommittee, ValidatorSignature}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| #[derive(derive_more::From, uniffi::Object)] | ||
| pub struct Bls12381PrivateKey(pub iota_crypto::bls12381::Bls12381PrivateKey); | ||
|
|
||
| #[uniffi::export] | ||
| impl Bls12381PrivateKey { | ||
| #[uniffi::constructor] | ||
| pub fn new(bytes: Vec<u8>) -> Result<Self> { | ||
| Ok(Self(iota_crypto::bls12381::Bls12381PrivateKey::new( | ||
| bytes.try_into().map_err(|v: Vec<u8>| { | ||
| SdkFfiError::custom(format!("expected bytes of length 32, found {}", v.len())) | ||
| })?, | ||
| )?)) | ||
| } | ||
|
|
||
| pub fn scheme(&self) -> SignatureScheme { | ||
| self.0.scheme() | ||
| } | ||
|
|
||
| pub fn verifying_key(&self) -> Bls12381VerifyingKey { | ||
| self.0.verifying_key().into() | ||
| } | ||
|
|
||
| pub fn public_key(&self) -> Bls12381PublicKey { | ||
| self.0.public_key().into() | ||
| } | ||
|
|
||
| #[uniffi::constructor] | ||
| pub fn generate() -> Self { | ||
| Self(iota_crypto::bls12381::Bls12381PrivateKey::generate(OsRng)) | ||
| } | ||
|
|
||
| pub fn sign_checkpoint_summary(&self, summary: &CheckpointSummary) -> ValidatorSignature { | ||
| self.0.sign_checkpoint_summary(&summary.0).into() | ||
| } | ||
|
|
||
| pub fn try_sign(&self, message: &[u8]) -> Result<Bls12381Signature> { | ||
| Ok( | ||
| iota_crypto::Signer::<iota_types::Bls12381Signature>::try_sign(&self.0, message)? | ||
| .into(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #[derive(derive_more::From, uniffi::Object)] | ||
| pub struct Bls12381VerifyingKey(pub iota_crypto::bls12381::Bls12381VerifyingKey); | ||
|
|
||
| #[uniffi::export] | ||
| impl Bls12381VerifyingKey { | ||
| #[uniffi::constructor] | ||
| pub fn new(public_key: &Bls12381PublicKey) -> Result<Self> { | ||
| Ok(iota_crypto::bls12381::Bls12381VerifyingKey::new(&public_key.0).map(Self)?) | ||
| } | ||
|
|
||
| pub fn public_key(&self) -> Bls12381PublicKey { | ||
| self.0.public_key().into() | ||
| } | ||
|
|
||
| pub fn verify(&self, message: &[u8], signature: &Bls12381Signature) -> Result<()> { | ||
| Ok( | ||
| iota_crypto::Verifier::<iota_types::Bls12381Signature>::verify( | ||
| &self.0, | ||
| message, | ||
| &signature.0, | ||
| )?, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #[derive(derive_more::From, uniffi::Object)] | ||
| pub struct ValidatorCommitteeSignatureVerifier( | ||
| pub iota_crypto::bls12381::ValidatorCommitteeSignatureVerifier, | ||
| ); | ||
|
|
||
| #[uniffi::export] | ||
| impl ValidatorCommitteeSignatureVerifier { | ||
| #[uniffi::constructor] | ||
| pub fn new(committee: ValidatorCommittee) -> Result<Self> { | ||
| Ok(Self( | ||
| iota_crypto::bls12381::ValidatorCommitteeSignatureVerifier::new(committee.into())?, | ||
| )) | ||
| } | ||
|
|
||
| pub fn committee(&self) -> ValidatorCommittee { | ||
| self.0.committee().clone().into() | ||
| } | ||
|
|
||
| pub fn verify_checkpoint_summary( | ||
| &self, | ||
| summary: &CheckpointSummary, | ||
| signature: &ValidatorAggregatedSignature, | ||
| ) -> Result<()> { | ||
| Ok(self.0.verify_checkpoint_summary(&summary.0, &signature.0)?) | ||
| } | ||
|
|
||
| pub fn verify(&self, message: &[u8], signature: &ValidatorSignature) -> Result<()> { | ||
| Ok( | ||
| iota_crypto::Verifier::<iota_types::ValidatorSignature>::verify( | ||
| &self.0, | ||
| message, | ||
| &signature.0, | ||
| )?, | ||
| ) | ||
| } | ||
|
|
||
| pub fn verify_aggregated( | ||
| &self, | ||
| message: &[u8], | ||
| signature: &ValidatorAggregatedSignature, | ||
| ) -> Result<()> { | ||
| Ok(iota_crypto::Verifier::< | ||
| iota_types::ValidatorAggregatedSignature, | ||
| >::verify(&self.0, message, &signature.0)?) | ||
| } | ||
| } | ||
|
|
||
| #[derive(derive_more::From, uniffi::Object)] | ||
| pub struct ValidatorCommitteeSignatureAggregator( | ||
| pub RwLock<iota_crypto::bls12381::ValidatorCommitteeSignatureAggregator>, | ||
| ); | ||
|
|
||
| #[uniffi::export] | ||
| impl ValidatorCommitteeSignatureAggregator { | ||
| #[uniffi::constructor] | ||
| pub fn new_checkpoint_summary( | ||
| committee: ValidatorCommittee, | ||
| summary: &CheckpointSummary, | ||
| ) -> Result<Self> { | ||
| Ok(Self( | ||
| iota_crypto::bls12381::ValidatorCommitteeSignatureAggregator::new_checkpoint_summary( | ||
| committee.into(), | ||
| &summary.0, | ||
| )? | ||
| .into(), | ||
| )) | ||
| } | ||
|
|
||
| pub fn committee(&self) -> ValidatorCommittee { | ||
| self.0 | ||
| .read() | ||
| .expect("failed to read validator committee signature aggregator") | ||
| .committee() | ||
| .clone() | ||
| .into() | ||
| } | ||
|
|
||
| pub fn add_signature(&self, signature: &ValidatorSignature) -> Result<()> { | ||
| Ok(self | ||
| .0 | ||
| .write() | ||
| .expect("failed to read validator committee signature aggregator") | ||
| .add_signature(signature.0.clone())?) | ||
| } | ||
|
|
||
| pub fn finish(&self) -> Result<ValidatorAggregatedSignature> { | ||
| Ok(self | ||
| .0 | ||
| .read() | ||
| .expect("failed to read validator committee signature aggregator") | ||
| .finish()? | ||
| .into()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for a bitmap getter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't really sure how to handle it. I could just serialize the bytes for the getter but I can't just return the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kind of lacking the context if it's useful to get it or not, in doubt I'd serialise it yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done