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()
};
cbindgen::generate_with_config(&crate_dir, config)
cbindgen::generate_with_config(crate_dir, config)
.unwrap()
.write_to_file(&output_file);
.write_to_file(output_file);
}
/// Find the location of the `target/` directory. Note that this may be

View file

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

View file

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

View file

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