Fix clippy

This commit is contained in:
Martin Ondejka 2023-06-28 15:12:43 +02:00
parent d3627307ea
commit 0e84fb487d
4 changed files with 46 additions and 40 deletions

View file

@ -18,9 +18,9 @@ fn main() {
..Default::default() ..Default::default()
}; };
cbindgen::generate_with_config(&crate_dir, config) cbindgen::generate_with_config(crate_dir, config)
.unwrap() .unwrap()
.write_to_file(&output_file); .write_to_file(output_file);
} }
/// Find the location of the `target/` directory. Note that this may be /// Find the location of the `target/` directory. Note that this may be

View file

@ -5,11 +5,17 @@
#define FIELD_SIZE 32 #define FIELD_SIZE 32
/**
* * # Safety * this functions accepts raw pointer from golang
*/
bool poseidon(uint8_t network_id, bool poseidon(uint8_t network_id,
const uint8_t *field_ptr, const uint8_t *field_ptr,
uintptr_t field_len, uintptr_t field_len,
uint8_t *output_ptr); uint8_t *output_ptr);
/**
* * # Safety * this functions accepts raw pointer from golang
*/
bool verify(uint8_t network_id, bool verify(uint8_t network_id,
const uint8_t *pubkey_x, const uint8_t *pubkey_x,
const uint8_t *pubkey_y, const uint8_t *pubkey_y,

View file

@ -8,8 +8,12 @@ use o1_utils::FieldHelpers;
pub const FIELD_SIZE: usize = 32; pub const FIELD_SIZE: usize = 32;
/**
* # Safety
* this functions accepts raw pointer from golang
*/
#[no_mangle] #[no_mangle]
pub extern "C" fn poseidon( pub unsafe extern "C" fn poseidon(
network_id: u8, network_id: u8,
field_ptr: *const u8, field_ptr: *const u8,
field_len: usize, field_len: usize,
@ -20,9 +24,9 @@ pub extern "C" fn poseidon(
} }
let network_id = match network_id { let network_id = match network_id {
0x00 => NetworkId::MAINNET, 0x00 => NetworkId::Mainnet,
0x01 => NetworkId::TESTNET, 0x01 => NetworkId::Testnet,
0x02 => NetworkId::NULLNET, 0x02 => NetworkId::Nullnet,
_ => return false, _ => return false,
}; };
@ -51,8 +55,12 @@ pub extern "C" fn poseidon(
true true
} }
/**
* # Safety
* this functions accepts raw pointer from golang
*/
#[no_mangle] #[no_mangle]
pub extern "C" fn verify( pub unsafe extern "C" fn verify(
network_id: u8, network_id: u8,
pubkey_x: *const u8, pubkey_x: *const u8,
pubkey_y: *const u8, pubkey_y: *const u8,
@ -73,9 +81,9 @@ pub extern "C" fn verify(
} }
let network_id = match network_id { let network_id = match network_id {
0x00 => NetworkId::MAINNET, 0x00 => NetworkId::Mainnet,
0x01 => NetworkId::TESTNET, 0x01 => NetworkId::Testnet,
0x02 => NetworkId::NULLNET, 0x02 => NetworkId::Nullnet,
_ => return false, _ => return false,
}; };
@ -161,19 +169,17 @@ mod tests {
let input = test_vector let input = test_vector
.input .input
.iter() .iter()
.map(|input| BaseField::from_hex(input).unwrap().to_bytes()) .flat_map(|input| BaseField::from_hex(input).unwrap().to_bytes())
.flatten()
.collect::<Vec<u8>>(); .collect::<Vec<u8>>();
assert_eq!( unsafe {
poseidon( assert!(poseidon(
0x02, 0x02,
input.as_ptr(), input.as_ptr(),
test_vector.input.len(), test_vector.input.len(),
output.as_mut_ptr() output.as_mut_ptr()
), ))
true };
);
assert_eq!( assert_eq!(
BaseField::from_bytes(&output).unwrap().to_hex(), BaseField::from_bytes(&output).unwrap().to_hex(),
@ -222,16 +228,15 @@ mod tests {
let fields = test_vector let fields = test_vector
.fields .fields
.iter() .iter()
.map(|input| { .flat_map(|input| {
BaseField::from_biguint(&BigUint::from_str(&input).unwrap()) BaseField::from_biguint(&BigUint::from_str(input).unwrap())
.unwrap() .unwrap()
.to_bytes() .to_bytes()
}) })
.flatten()
.collect::<Vec<u8>>(); .collect::<Vec<u8>>();
assert_eq!( unsafe {
verify( assert!(verify(
0x01, 0x01,
pub_key_x.as_ptr(), pub_key_x.as_ptr(),
pub_key_y.as_ptr(), pub_key_y.as_ptr(),
@ -240,9 +245,8 @@ mod tests {
fields.as_ptr(), fields.as_ptr(),
test_vector.fields.len(), test_vector.fields.len(),
&mut output &mut output
), ))
true };
);
assert_eq!(output, test_vector.output); assert_eq!(output, test_vector.output);
} }
@ -250,12 +254,9 @@ mod tests {
#[test] #[test]
fn null_pointer() { fn null_pointer() {
assert_eq!( unsafe {
poseidon(0x00, std::ptr::null(), 1, std::ptr::null_mut()), assert!(!poseidon(0x00, std::ptr::null(), 1, std::ptr::null_mut()));
false assert!(!verify(
);
assert_eq!(
verify(
0x00, 0x00,
std::ptr::null(), std::ptr::null(),
std::ptr::null(), std::ptr::null(),
@ -264,8 +265,7 @@ mod tests {
std::ptr::null(), std::ptr::null(),
0, 0,
std::ptr::null_mut() std::ptr::null_mut()
), ));
false }
);
} }
} }

View file

@ -5,9 +5,9 @@ use o1_utils::{field_helpers::FieldHelpersError, FieldHelpers};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[repr(C)] #[repr(C)]
pub enum NetworkId { pub enum NetworkId {
MAINNET = 0x00, Mainnet = 0x00,
TESTNET = 0x01, Testnet = 0x01,
NULLNET = 0x02, Nullnet = 0x02,
} }
impl From<NetworkId> for u8 { impl From<NetworkId> for u8 {
@ -49,9 +49,9 @@ impl Hashable for Message {
fn domain_string(network_id: NetworkId) -> Option<String> { fn domain_string(network_id: NetworkId) -> Option<String> {
match network_id { match network_id {
NetworkId::MAINNET => "MinaSignatureMainnet".to_string().into(), NetworkId::Mainnet => "MinaSignatureMainnet".to_string().into(),
NetworkId::TESTNET => "CodaSignature".to_string().into(), NetworkId::Testnet => "CodaSignature".to_string().into(),
NetworkId::NULLNET => None, NetworkId::Nullnet => None,
} }
} }
} }