Skip to content

Commit d956e5d

Browse files
committed
Experiment with Rustler Serde feature.
1 parent 91e17d7 commit d956e5d

6 files changed

Lines changed: 95 additions & 134 deletions

File tree

Cargo.lock

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/arm/arm.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@ defmodule AnomaSDK.Arm do
33
I define a few functions to test the ARM repo NIF interface.
44
"""
55

6-
mix_config = Mix.Project.config()
7-
version = mix_config[:version]
8-
github_url = mix_config[:package][:links]["GitHub"]
9-
10-
use RustlerPrecompiled,
6+
use Rustler,
117
otp_app: :anoma_sdk,
12-
crate: :arm_bindings,
13-
base_url: "#{github_url}/releases/download/v#{version}",
14-
version: version,
15-
force_build: System.get_env("BUILD_NATIVE") in ["1", "true"]
8+
crate: :arm_bindings
169

1710
alias AnomaSDK.Arm.ComplianceInstance
1811
alias AnomaSDK.Arm.ComplianceUnit

native/arm_bindings/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ name = "arm_bindings"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
evm_protocol_adapter_bindings = { git = "https://github.com/anoma/evm-protocol-adapter", branch = "feature/sepolia-demo" }
13-
arm = { git = "https://github.com/anoma/arm-risc0", branch = "m1dnight/sdk-integration", default-features = true, features = [
12+
evm_protocol_adapter_bindings = { git = "https://github.com/anoma/evm-protocol-adapter", branch = "murisi/sepolia-demo" }
13+
arm = { git = "https://github.com/anoma/arm-risc0", branch = "murisi/sdk-integration", default-features = true, features = [
1414
"nif",
1515
"transaction",
1616
"groth16_prover",
@@ -23,7 +23,7 @@ risc0-zkvm = { version = "3.0.3", features = [
2323
"unstable",
2424
], default-features = false }
2525

26-
rustler = "0.36.2"
26+
rustler = { version = "0.36.2", features = ["serde"] }
2727
serde = { version = "1.0.197", default-features = false }
2828
serde_json = "1.0"
2929
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

native/arm_bindings/src/lib.rs

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,113 +6,80 @@ use arm::delta_proof::DeltaWitness;
66
use arm::encryption::{random_keypair, Ciphertext, SecretKey};
77
use arm::logic_proof::LogicVerifier;
88
use arm::logic_proof::LogicVerifierInputs;
9-
use arm::rustler_util::{at_struct, RustlerDecoder, RustlerEncoder};
109
use arm::transaction::Transaction;
1110
use k256::AffinePoint;
12-
use rustler::types::map::map_new;
13-
use rustler::{atoms, nif, Decoder, Encoder, Env, NifResult, Term};
11+
use rustler::{nif, SerdeTerm};
12+
use serde::{Deserialize, Serialize};
1413

1514
/// A Keypair is a struct that holds a SecretKey and Affinepoint.
1615
/// It is used here to implement the Encoder and RusterEncoder trait.
1716
/// The encoded value is a tuple.
17+
#[derive(Deserialize, Serialize)]
1818
pub struct Keypair {
1919
pub secret: SecretKey,
2020
pub public: AffinePoint,
2121
}
2222

23-
atoms! {
24-
at_secret_key = "secret_key",
25-
at_public_key = "public_key",
26-
at_keypair_key = "Elixir.AnomaSDK.Arm.Keypair"
27-
}
28-
29-
impl Encoder for Keypair {
30-
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
31-
let secret_key = self.secret.rustler_encode(env).unwrap();
32-
let public_key = self.public.rustler_encode(env).unwrap();
33-
34-
let map = map_new(env)
35-
.map_put(at_struct().encode(env), at_keypair_key())
36-
.unwrap()
37-
.map_put(at_secret_key(), secret_key)
38-
.unwrap()
39-
.map_put(at_public_key(), public_key);
40-
41-
map.expect("failed to encode Keypair")
42-
}
43-
}
44-
45-
impl<'a> Decoder<'a> for Keypair {
46-
fn decode(term: Term<'a>) -> NifResult<Self> {
47-
let secret_term = term.map_get(at_secret_key().encode(term.get_env()))?;
48-
let secret: SecretKey = RustlerDecoder::rustler_decode(secret_term)?;
49-
50-
let public_term = term.map_get(at_public_key().encode(term.get_env()))?;
51-
let public: AffinePoint = RustlerDecoder::rustler_decode(public_term)?;
52-
Ok(Keypair { secret, public })
53-
}
54-
}
55-
5623
#[nif]
5724
/// Generates a random pair of SecretKey and AffinePoint.
58-
pub fn random_key_pair() -> Keypair {
25+
pub fn random_key_pair() -> SerdeTerm<Keypair> {
5926
let (secret, public) = random_keypair();
60-
Keypair { secret, public }
27+
SerdeTerm(Keypair { secret, public })
6128
}
6229

6330
/// Returns the ComplianceInstance from within a ComplianceUnit
6431
#[nif]
65-
fn compliance_unit_instance(unit: ComplianceUnit) -> ComplianceInstance {
66-
unit.get_instance()
32+
fn compliance_unit_instance(SerdeTerm(unit): SerdeTerm<ComplianceUnit>) -> SerdeTerm<ComplianceInstance> {
33+
SerdeTerm(unit.get_instance())
6734
}
6835

6936
#[nif]
70-
fn encrypt_cipher(cipher: Vec<u8>, keypair: Keypair, nonce: Vec<u8>) -> Ciphertext {
71-
Ciphertext::encrypt(
37+
fn encrypt_cipher(cipher: Vec<u8>, SerdeTerm(keypair): SerdeTerm<Keypair>, nonce: Vec<u8>) -> SerdeTerm<Ciphertext> {
38+
SerdeTerm(Ciphertext::encrypt(
7239
cipher.as_ref(),
7340
&keypair.public,
7441
&keypair.secret,
7542
nonce.try_into().expect("REASON"),
76-
)
43+
))
7744
}
7845

7946
#[nif]
80-
pub fn decrypt_cipher(cipher_bytes: Vec<u8>, keypair: Keypair) -> Option<Vec<u8>> {
47+
pub fn decrypt_cipher(cipher_bytes: Vec<u8>, SerdeTerm(keypair): SerdeTerm<Keypair>) -> Option<Vec<u8>> {
8148
let cipher_text = Ciphertext::from_bytes(cipher_bytes);
8249
let decipher_result = cipher_text.decrypt(&keypair.secret);
8350
decipher_result.ok()
8451
}
8552

8653
#[nif]
8754
/// Generate a compliance unit from a compliance witness.
88-
fn prove_compliance_witness(compliance_witness: ComplianceWitness) -> ComplianceUnit {
89-
ComplianceUnit::create(&compliance_witness)
55+
fn prove_compliance_witness(SerdeTerm(compliance_witness): SerdeTerm<ComplianceWitness>) -> SerdeTerm<ComplianceUnit> {
56+
SerdeTerm(ComplianceUnit::create(&compliance_witness))
9057
}
9158

9259
#[nif]
9360
/// Converts a logic verifier to a logic verifier inputs.
9461
/// this nif is here because this conversion relies on the Journal
9562
/// implementation of Risc0 ZKVM.
96-
fn convert(logic_verifier: LogicVerifier) -> LogicVerifierInputs {
97-
logic_verifier.into()
63+
fn convert(SerdeTerm(logic_verifier): SerdeTerm<LogicVerifier>) -> SerdeTerm<LogicVerifierInputs> {
64+
SerdeTerm(logic_verifier.into())
9865
}
9966

10067
#[nif]
10168
/// Generate a proof for a delta witness.
102-
fn prove_delta_witness(witness: DeltaWitness, message: Vec<u8>) -> DeltaProof {
103-
DeltaProof::prove(&message, &witness)
69+
fn prove_delta_witness(SerdeTerm(witness): SerdeTerm<DeltaWitness>, message: Vec<u8>) -> SerdeTerm<DeltaProof> {
70+
SerdeTerm(DeltaProof::prove(&message, &witness))
10471
}
10572

10673
#[nif]
10774
/// Given a transaction, puts in the delta proof.
108-
pub fn generate_delta_proof(transaction: Transaction) -> Transaction {
75+
pub fn generate_delta_proof(SerdeTerm(transaction): SerdeTerm<Transaction>) -> SerdeTerm<Transaction> {
10976
let mut tx = transaction.clone();
11077
tx.generate_delta_proof();
111-
tx
78+
SerdeTerm(tx)
11279
}
11380

11481
#[nif]
115-
pub fn verify_transaction(transaction: Transaction) -> bool {
82+
pub fn verify_transaction(SerdeTerm(transaction): SerdeTerm<Transaction>) -> bool {
11683
transaction.verify()
11784
}
11885

native/arm_bindings_test/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "arm_bindings_test"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
arm = { git = "https://github.com/anoma/arm-risc0", branch = "m1dnight/sdk-integration", default-features = true, features = ["nif"] }
12+
arm = { git = "https://github.com/anoma/arm-risc0", branch = "murisi/sdk-integration", default-features = true, features = ["nif"] }
1313

1414

1515

@@ -18,7 +18,7 @@ risc0-zkvm = { version = "3.0.3", features = [
1818
"unstable",
1919
], default-features = false }
2020

21-
rustler = "0.36.2"
21+
rustler = { version = "0.36.2", features = ["serde"] }
2222
serde = { version = "1.0.197", default-features = false }
2323
serde_json = "1.0"
2424
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

0 commit comments

Comments
 (0)