@@ -6,113 +6,80 @@ use arm::delta_proof::DeltaWitness;
66use arm:: encryption:: { random_keypair, Ciphertext , SecretKey } ;
77use arm:: logic_proof:: LogicVerifier ;
88use arm:: logic_proof:: LogicVerifierInputs ;
9- use arm:: rustler_util:: { at_struct, RustlerDecoder , RustlerEncoder } ;
109use arm:: transaction:: Transaction ;
1110use 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 ) ]
1818pub 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
0 commit comments