1#![forbid(unsafe_code)]
18#![allow(missing_docs)] use napi::bindgen_prelude::Buffer;
21use napi::bindgen_prelude::Result as NapiResult;
22use napi_derive::napi;
23
24fn map_err<E: std::fmt::Display>(e: E) -> napi::Error {
25 napi::Error::new(napi::Status::GenericFailure, e.to_string())
26}
27
28#[napi]
33pub struct Cmp20;
34
35#[napi]
36impl Cmp20 {
37 #[napi]
41 pub fn keygen(threshold: u32, party_count: u32) -> NapiResult<Cmp20Keygen> {
42 let kg = confium_tc_cmp20::inprocess::keygen(threshold, party_count as usize)
43 .map_err(map_err)?;
44 Ok(Cmp20Keygen {
45 shares: kg.shares.into_iter().map(Into::into).collect(),
46 public_key: kg.public_key.into(),
47 })
48 }
49
50 #[napi]
53 pub fn sign(shares: Vec<Buffer>, threshold: u32, message: Buffer) -> NapiResult<Buffer> {
54 let share_blobs: Vec<Vec<u8>> = shares.into_iter().map(|b| b.to_vec()).collect();
55 let msg = message.to_vec();
56 let sig =
57 confium_tc_cmp20::inprocess::sign(&share_blobs, threshold, &msg).map_err(map_err)?;
58 Ok(sig.into())
59 }
60
61 #[napi]
64 pub fn sign_batch(
65 shares: Vec<Buffer>,
66 threshold: u32,
67 messages: Vec<Buffer>,
68 ) -> NapiResult<Vec<Buffer>> {
69 let share_blobs: Vec<Vec<u8>> = shares.into_iter().map(|b| b.to_vec()).collect();
70 let msg_refs: Vec<&[u8]> = messages.iter().map(|b| b.as_ref()).collect();
71 let sigs = confium_tc_cmp20::inprocess::sign_batch(&share_blobs, threshold, &msg_refs)
72 .map_err(map_err)?;
73 Ok(sigs.into_iter().map(Into::into).collect())
74 }
75}
76
77#[napi(object)]
79pub struct Cmp20Keygen {
80 pub shares: Vec<Buffer>,
82 #[napi(js_name = "publicKey")]
84 pub public_key: Buffer,
85}
86
87#[napi]
91pub struct Gg18;
92
93#[napi]
94impl Gg18 {
95 #[napi]
97 pub fn keygen(threshold: u32, party_count: u32) -> NapiResult<Cmp20Keygen> {
98 let kg =
99 confium_tc_gg18::inprocess::keygen(threshold, party_count as usize).map_err(map_err)?;
100 Ok(Cmp20Keygen {
101 shares: kg.shares.into_iter().map(Into::into).collect(),
102 public_key: kg.public_key.into(),
103 })
104 }
105
106 #[napi]
108 pub fn sign(shares: Vec<Buffer>, threshold: u32, message: Buffer) -> NapiResult<Buffer> {
109 let share_blobs: Vec<Vec<u8>> = shares.into_iter().map(|b| b.to_vec()).collect();
110 let msg = message.to_vec();
111 let sig =
112 confium_tc_gg18::inprocess::sign(&share_blobs, threshold, &msg).map_err(map_err)?;
113 Ok(sig.into())
114 }
115}
116
117#[napi]
121pub struct FrostP256;
122
123#[napi]
124impl FrostP256 {
125 #[napi]
128 pub fn generate_keypair() -> NapiResult<FrostKeypair> {
129 let kp = confium_tc_frost_p256::generate_keypair();
130 let sk: [u8; 32] = kp.to_signing_key().to_bytes().into();
131 let pk = kp.to_verifying_key().to_sec1_bytes();
132 Ok(FrostKeypair {
133 private_key: sk.to_vec().into(),
134 public_key: pk.to_vec().into(),
135 })
136 }
137}
138
139#[napi(object)]
141pub struct FrostKeypair {
142 #[napi(js_name = "privateKey")]
144 pub private_key: Buffer,
145 #[napi(js_name = "publicKey")]
147 pub public_key: Buffer,
148}
149
150#[napi]
154pub fn version() -> String {
155 env!("CARGO_PKG_VERSION").to_string()
156}