1#![deny(unsafe_code)]
21#![allow(missing_docs)] use napi::bindgen_prelude::Buffer;
24use napi::bindgen_prelude::Result as NapiResult;
25use napi_derive::napi;
26
27fn map_err<E: std::fmt::Display>(e: E) -> napi::Error {
28 napi::Error::new(napi::Status::GenericFailure, e.to_string())
29}
30
31#[napi]
36pub struct Cmp20;
37
38#[napi]
39impl Cmp20 {
40 #[napi]
44 pub fn keygen(threshold: u32, party_count: u32) -> NapiResult<Cmp20Keygen> {
45 let kg = confium_tc_cmp20::inprocess::keygen(threshold, party_count as usize)
46 .map_err(map_err)?;
47 Ok(Cmp20Keygen {
48 shares: kg.shares.into_iter().map(Into::into).collect(),
49 public_key: kg.public_key.into(),
50 })
51 }
52
53 #[napi]
56 pub fn sign(shares: Vec<Buffer>, threshold: u32, message: Buffer) -> NapiResult<Buffer> {
57 let share_blobs: Vec<Vec<u8>> = shares.into_iter().map(|b| b.to_vec()).collect();
58 let msg = message.to_vec();
59 let sig =
60 confium_tc_cmp20::inprocess::sign(&share_blobs, threshold, &msg).map_err(map_err)?;
61 Ok(sig.into())
62 }
63
64 #[napi]
67 pub fn sign_batch(
68 shares: Vec<Buffer>,
69 threshold: u32,
70 messages: Vec<Buffer>,
71 ) -> NapiResult<Vec<Buffer>> {
72 let share_blobs: Vec<Vec<u8>> = shares.into_iter().map(|b| b.to_vec()).collect();
73 let msg_refs: Vec<&[u8]> = messages.iter().map(|b| b.as_ref()).collect();
74 let sigs = confium_tc_cmp20::inprocess::sign_batch(&share_blobs, threshold, &msg_refs)
75 .map_err(map_err)?;
76 Ok(sigs.into_iter().map(Into::into).collect())
77 }
78}
79
80#[napi(object)]
82pub struct Cmp20Keygen {
83 pub shares: Vec<Buffer>,
85 #[napi(js_name = "publicKey")]
87 pub public_key: Buffer,
88}
89
90#[napi]
94pub struct Gg18;
95
96#[napi]
97impl Gg18 {
98 #[napi]
100 pub fn keygen(threshold: u32, party_count: u32) -> NapiResult<Cmp20Keygen> {
101 let kg =
102 confium_tc_gg18::inprocess::keygen(threshold, party_count as usize).map_err(map_err)?;
103 Ok(Cmp20Keygen {
104 shares: kg.shares.into_iter().map(Into::into).collect(),
105 public_key: kg.public_key.into(),
106 })
107 }
108
109 #[napi]
111 pub fn sign(shares: Vec<Buffer>, threshold: u32, message: Buffer) -> NapiResult<Buffer> {
112 let share_blobs: Vec<Vec<u8>> = shares.into_iter().map(|b| b.to_vec()).collect();
113 let msg = message.to_vec();
114 let sig =
115 confium_tc_gg18::inprocess::sign(&share_blobs, threshold, &msg).map_err(map_err)?;
116 Ok(sig.into())
117 }
118}
119
120#[napi]
124pub struct FrostP256;
125
126#[napi]
127impl FrostP256 {
128 #[napi]
131 pub fn generate_keypair() -> NapiResult<FrostKeypair> {
132 let kp = confium_tc_frost_p256::generate_keypair();
133 let sk: [u8; 32] = kp.to_signing_key().to_bytes().into();
134 let pk = kp.to_verifying_key().to_sec1_bytes();
135 Ok(FrostKeypair {
136 private_key: sk.to_vec().into(),
137 public_key: pk.to_vec().into(),
138 })
139 }
140}
141
142#[napi(object)]
144pub struct FrostKeypair {
145 #[napi(js_name = "privateKey")]
147 pub private_key: Buffer,
148 #[napi(js_name = "publicKey")]
150 pub public_key: Buffer,
151}
152
153#[napi]
157pub fn version() -> String {
158 env!("CARGO_PKG_VERSION").to_string()
159}