Add unit tests to the mina lib

This commit is contained in:
Martin Ondejka 2023-04-20 14:27:02 +02:00
parent 7fb7cda87d
commit 48ec3a52c1
7 changed files with 281 additions and 8 deletions

1
mina/.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
/lib/libmina.a

15
mina/Cargo.lock generated
View file

@ -567,7 +567,10 @@ dependencies = [
"cbindgen", "cbindgen",
"mina-hasher", "mina-hasher",
"mina-signer", "mina-signer",
"num-bigint",
"o1-utils", "o1-utils",
"serde",
"serde_json",
] ]
[[package]] [[package]]
@ -866,18 +869,18 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.159" version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.159" version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -886,9 +889,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.95" version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",

View file

@ -15,3 +15,8 @@ o1-utils = { git = "https://github.com/o1-labs/proof-systems", tag = "0.1.0", ve
[build-dependencies] [build-dependencies]
cbindgen = "0.24.3" cbindgen = "0.24.3"
[dev-dependencies]
num-bigint = "0.4.3"
serde = "1.0.160"
serde_json = "1.0.96"

Binary file not shown.

View file

@ -15,6 +15,10 @@ pub extern "C" fn poseidon(
field_len: usize, field_len: usize,
output_ptr: *mut u8, // 32 bytes output_ptr: *mut u8, // 32 bytes
) -> bool { ) -> bool {
if (field_ptr.is_null() && field_len != 0) || output_ptr.is_null() {
return false;
}
let network_id = match network_id { let network_id = match network_id {
0x00 => NetworkId::MAINNET, 0x00 => NetworkId::MAINNET,
0x01 => NetworkId::TESTNET, 0x01 => NetworkId::TESTNET,
@ -58,6 +62,16 @@ pub extern "C" fn verify(
field_len: usize, field_len: usize,
output_ptr: *mut bool, output_ptr: *mut bool,
) -> bool { ) -> bool {
if pubkey_x.is_null()
|| pubkey_y.is_null()
|| sig_rx.is_null()
|| sig_s.is_null()
|| (field_ptr.is_null() && field_len != 0)
|| output_ptr.is_null()
{
return false;
}
let network_id = match network_id { let network_id = match network_id {
0x00 => NetworkId::MAINNET, 0x00 => NetworkId::MAINNET,
0x01 => NetworkId::TESTNET, 0x01 => NetworkId::TESTNET,
@ -119,8 +133,139 @@ pub extern "C" fn verify(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// use super::*; use std::str::FromStr;
use super::*;
use num_bigint::BigUint;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct PoseidonTestVector {
input: Vec<String>,
output: String,
}
#[derive(Debug, Deserialize)]
struct PoseidonTestVectors {
test_vectors: Vec<PoseidonTestVector>,
}
#[test] #[test]
fn it_works() {} fn poseidon_test_vectors() {
let test_vectors: PoseidonTestVectors =
serde_json::from_str(include_str!("test/poseidon_test_vectors.json")).unwrap();
for test_vector in test_vectors.test_vectors {
let mut output = [0u8; 32];
let input = test_vector
.input
.iter()
.map(|input| BaseField::from_hex(input).unwrap().to_bytes())
.flatten()
.collect::<Vec<u8>>();
assert_eq!(
poseidon(
0x02,
input.as_ptr(),
test_vector.input.len(),
output.as_mut_ptr()
),
true
);
assert_eq!(
BaseField::from_bytes(&output).unwrap().to_hex(),
test_vector.output
);
}
}
#[derive(Debug, Deserialize)]
struct SignerTestVector {
pub_key_x: String,
pub_key_y: String,
sig_rx: String,
sig_s: String,
fields: Vec<String>,
output: bool,
}
#[derive(Debug, Deserialize)]
struct SignerTestVectors {
test_vectors: Vec<SignerTestVector>,
}
#[test]
fn test_signer() {
let test_vectors: SignerTestVectors =
serde_json::from_str(include_str!("test/signer_test_vectors.json")).unwrap();
for test_vector in test_vectors.test_vectors {
let mut output = false;
let pub_key_x =
BaseField::from_biguint(&BigUint::from_str(&test_vector.pub_key_x).unwrap())
.unwrap()
.to_bytes();
let pub_key_y =
BaseField::from_biguint(&BigUint::from_str(&test_vector.pub_key_y).unwrap())
.unwrap()
.to_bytes();
let sig_rx = BaseField::from_biguint(&BigUint::from_str(&test_vector.sig_rx).unwrap())
.unwrap()
.to_bytes();
let sig_s = ScalarField::from_biguint(&BigUint::from_str(&test_vector.sig_s).unwrap())
.unwrap()
.to_bytes();
let fields = test_vector
.fields
.iter()
.map(|input| {
BaseField::from_biguint(&BigUint::from_str(&input).unwrap())
.unwrap()
.to_bytes()
})
.flatten()
.collect::<Vec<u8>>();
assert_eq!(
verify(
0x01,
pub_key_x.as_ptr(),
pub_key_y.as_ptr(),
sig_rx.as_ptr(),
sig_s.as_ptr(),
fields.as_ptr(),
test_vector.fields.len(),
&mut output
),
true
);
assert_eq!(output, test_vector.output);
}
}
#[test]
fn null_pointer() {
assert_eq!(
poseidon(0x00, std::ptr::null(), 1, std::ptr::null_mut()),
false
);
assert_eq!(
verify(
0x00,
std::ptr::null(),
std::ptr::null(),
std::ptr::null(),
std::ptr::null(),
std::ptr::null(),
0,
std::ptr::null_mut()
),
false
);
}
} }

View file

@ -0,0 +1,47 @@
{
"name": "kimchi",
"test_vectors": [
{
"input": [],
"output": "a8eb9ee0f30046308abbfa5d20af73c81bbdabc25b459785024d045228bead2f"
},
{
"input": ["f2eee8d8f6e5fb182c610cae6c5393fce69dc4d900e7b4923b074e54ad00fb36"],
"output": "fb5992f65c07f9335995f43fd791d39012ad466717729e61045c297507054f3d"
},
{
"input": [
"bd3f1c8f183ceedea15080edbe79d30bd7d613b86bf2ba12007091c60ae39337",
"65e4f04ab87706bab06d13c7eee0a7807d0b8ce268b4ece6aab1e0508ec9c42f"
],
"output": "fe2436f2027620a11233318b55d0a117086f09674826d1b7ce08d48ad0736c33"
},
{
"input": [
"f5ea61ce47773495363dc4f6a41c3e2da14b13d6dd173acf87c9ca7357fb2400",
"f28573f49c658b4ba151e82ed0bd6aaab045311d1a72df58c21eed462bede018",
"73cf45c39285f17ccea99e0daeb547430cf7921218fe3726010f608e682a841a"
],
"output": "9b1b94444a54af49a7623d1fe1ca72649f0a098daf5704925f024eb6ab0e4b3f"
},
{
"input": [
"4c28b87198e0012207f93cdbdaa35355ec8213fa97a60e62701f62602d465920",
"0787a40fc046c4dd0ff3cad0e54006577fece871c774707494984f1c7d334727",
"1504ffe48e4e6dfcc4ded439edd386cf271b69d94afae83079f3ee3e7c04d52d",
"290b6506516fe7588b5100f8db2e871427c6d74e7a60ab656f43dd9bc687c312"
],
"output": "47ecd3bf2eed86dcf8d2cef3d7667104689dba4d9bcb54006e0c66f6ec8c5a16"
},
{
"input": [
"da99182b35f2cd9f8a137052c4262576377a16deb83652db459a74893a0cf73c",
"9805573990c4028292c9db171cd2b97902f9fc494983f6f7e0a0c184bc55df1b",
"90ff1001b9dab21358aad1f6b7906a56d0c039502c1590c3ef9921a8951e4409",
"88b56238a0eda34576db959fecd1c3790bb5311fdb231753243c5085974a5b37",
"896a7727e511a4c30d99082bf3542623fb702afab0b62ebbf301ed51e38f6812"
],
"output": "09a2d55277908b7c8214f745b3605f0f9055dcd4c9b594cdd759292c34c3a20c"
}
]
}

View file

@ -0,0 +1,72 @@
{
"test_vectors": [
{
"pub_key_x": "5749528645515407352164834780408435992604865487588794690727207227268864389887",
"pub_key_y": "961123686654787572379844019641929632619233203965678030693512890434590233589",
"sig_rx": "21936703768608309712712286678384212721667043165509057046476743727602317531457",
"sig_s": "700244403784750329046343352585969728434513601583367350022979202621496307135",
"fields": ["4312143217416119453149054021680295703515035791870532431847656061436158469703"],
"output": true
},
{
"pub_key_x": "7042208129527677077861348888492606461956846197792817644260213199747289065652",
"pub_key_y": "10558926836583110251042002010080745706359112066577139056638753279976447906000",
"sig_rx": "16079692606027161177880995922609222716277493656983184336617842262008786419582",
"sig_s": "26519020815623540666020017924931679489835794248279750040849654719218653085849",
"fields": [
"3081943907937102890466321349086512568672738229687175831440974884688704210203",
"13581532047510445467277570462258108561396477004644819735095763123534482739016",
"3048822856514015646580881400214404144660523787797871598232610379373617426012",
"21660741932862358360736636215811102042484175487489560833468895032615023667514"
],
"output": true
},
{
"pub_key_x": "13100040091688310466504835329210079551770129473582830476827447762924491321023",
"pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299",
"sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108",
"sig_s": "10007084982746413426116958072671171608413225743556423925802350318023121051002",
"fields": [
"26883907960994343248721672123600111927912732275633851101666934083556914846115",
"15530614225213312579256518680413347727953796927384451546410690799125628951482",
"14938482801295869243888529448769259929444998690971476813418451846255531696047"
],
"output": true
},
{
"pub_key_x": "13100040091688310466504835329210079551770129473582830476827447762924491321023",
"pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299",
"sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108",
"sig_s": "10007084982746413426116958072671171608413225743556423925802350318023121051002",
"fields": [
"26883907960994343248721672123600111927912732275633851101666934083556914846115",
"15530614225213312579256518680413347727953796927384451546410690799125628951482"
],
"output": false
},
{
"pub_key_x": "7042208129527677077861348888492606461956846197792817644260213199747289065652",
"pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299",
"sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108",
"sig_s": "10007084982746413426116958072671171608413225743556423925802350318023121051002",
"fields": [
"26883907960994343248721672123600111927912732275633851101666934083556914846115",
"15530614225213312579256518680413347727953796927384451546410690799125628951482",
"14938482801295869243888529448769259929444998690971476813418451846255531696047"
],
"output": false
},
{
"pub_key_x": "13100040091688310466504835329210079551770129473582830476827447762924491321023",
"pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299",
"sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108",
"sig_s": "700244403784750329046343352585969728434513601583367350022979202621496307135",
"fields": [
"26883907960994343248721672123600111927912732275633851101666934083556914846115",
"15530614225213312579256518680413347727953796927384451546410690799125628951482",
"14938482801295869243888529448769259929444998690971476813418451846255531696047"
],
"output": false
}
]
}