1. 安装交差编译的targets
rustup target list
两个环境,真机与模拟器
x86_64-apple-ios
aarch64-apple-ios
2.安装lipo
cargo install cargo-lipo
3.编写代码
use num::{BigInt, ToPrimitive};
#[no_mangle]
pub extern fn get_rand_key(p: u32) -> u32 {
//随机一个小于P的数最为私钥
return rand::random::<u32>() % (p - 1);
}
#[no_mangle]
pub extern fn get_public_key(g: u32,p: u32,pub_key: u32) -> i32 {
return match BigInt::from(g).modpow(&BigInt::from(pub_key), &BigInt::from(p)).to_i32() {
Some(n) =>n,
None => -1
};
}
#[no_mangle]
pub extern fn get_private_key(p: u32,pub_key:i32,rand: u32)->i32{
return match BigInt::from(pub_key).modpow(&BigInt::from(rand), &BigInt::from(p)).to_i32() {
Some(n) => n,
None => -1
};
}
4.cargo.toml配置
[package]
name = "dh-lib"
version = "0.1.0"
authors = ["sorata"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.7.3"
num = "0.2.1"
[lib]
name = "ff"
crate-type = ["staticlib", "cdylib"]
5.编译
cargo lipo --release
6.合并静态库
lipo -create /Users/cocos/software/github/dh-lib/target/x86_64-apple-ios/release/libff.a /Users/cocos/software/github/dh-lib/target/aarch64-apple-ios/release/libff.a -o cc.a
7.头文件
//
// Header.h
// dh
//
// Created by sorata on 2020/4/21.
// Copyright © 2020 sorata. All rights reserved.
//
#ifndef Header_h
#define Header_h
//创建私钥
int get_rand_key(int p);
//创建公钥
int get_public_key(int g,int p,int pub_key);
//交换得到共享密钥
int get_private_key(int p,int pub_key,int rand);
#endif /* Header_h */
来源:oschina
链接:https://my.oschina.net/swiftloop/blog/3301177