Skip to content

Instantly share code, notes, and snippets.

@crackcomm
Created November 10, 2021 09:28
Show Gist options
  • Save crackcomm/03d42fe4a30ca0951432c8f4c14d06a3 to your computer and use it in GitHub Desktop.
Save crackcomm/03d42fe4a30ca0951432c8f4c14d06a3 to your computer and use it in GitHub Desktop.
// Copyright © 2021 Łukasz Kurowski. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
#![feature(in_band_lifetimes)]
use ocaml::bigarray::Array1;
use zeroize::Zeroize;
struct Keypair(schnorrkel::Keypair);
ocaml::custom!(Keypair);
#[ocaml::func]
pub fn generate_keypair() -> ocaml::Pointer<Keypair> {
let keypair = schnorrkel::Keypair::generate();
ocaml::Pointer::alloc_custom(Keypair(keypair))
}
#[ocaml::func]
pub unsafe fn keypair_to_bytes(keypair: ocaml::Pointer<Keypair>) -> Array1<u8> {
let mut bytes = keypair.as_ref().0.to_bytes();
// copy bytes to bigarray, they are zeroized on ocaml side
let res = Array1::from_slice(bytes);
// zeroize the keypair bytes
bytes.zeroize();
res
}
struct PublicKey(schnorrkel::PublicKey);
ocaml::custom!(PublicKey);
#[ocaml::func]
pub unsafe fn public_key_bytes(keypair: ocaml::Pointer<Keypair>) -> Array1<u8> {
let mut bytes = keypair.as_ref().0.public.to_bytes();
// copy bytes to bigarray, they are zeroized on ocaml side
let res = Array1::from_slice(bytes);
// zeroize the public key bytes
bytes.zeroize();
res
}
struct Signature(schnorrkel::Signature);
ocaml::custom!(Signature);
#[ocaml::func]
pub unsafe fn keypair_sign(
keypair: ocaml::Raw,
//ctx: Vec<u8>,
//bytes: Vec<u8>,
ctx: &'a [u8],
bytes: &[u8],
) -> ocaml::Pointer<'a, Signature> {
let keypair = keypair.as_pointer::<Keypair>();
//let ctx = schnorrkel::signing_context(&ctx);
//let signature = keypair.as_ref().0.sign(ctx.bytes(&bytes));
let ctx = schnorrkel::signing_context(ctx);
let signature = keypair.as_ref().0.sign(ctx.bytes(bytes));
ocaml::Pointer::alloc_custom(Signature(signature))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment