Skip to content

Commit e58ef0f

Browse files
Support to proof creation/verification with no bwt
1 parent 78ecbb1 commit e58ef0f

File tree

3 files changed

+169
-66
lines changed

3 files changed

+169
-66
lines changed

api/src/ginger_calls.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use primitives::{
2020
BoweHopwoodPedersenCRH, BoweHopwoodPedersenParameters
2121
},
2222
},
23-
merkle_tree::field_based_mht::{FieldBasedMerkleHashTree, FieldBasedMerkleTreeConfig, FieldBasedMerkleTreePath},
23+
merkle_tree::field_based_mht::{
24+
FieldBasedMerkleHashTree, FieldBasedMerkleTreeConfig,
25+
FieldBasedMerkleTreePath, MNT4753_PHANTOM_MERKLE_ROOT,
26+
},
2427
signature::{
2528
FieldBasedSignatureScheme, schnorr::field_based_schnorr::{
2629
FieldBasedSchnorrSignatureScheme, FieldBasedSchnorrSignature
@@ -178,20 +181,19 @@ pub fn compute_msg_to_sign(
178181
bt_list: &[BackwardTransfer],
179182
) -> Result<(FieldElement, FieldElement), Error> {
180183

181-
let mut bt_field_list = vec![];
182-
for bt in bt_list.iter() {
183-
let bt_f = bt.to_field_element()?;
184-
bt_field_list.push(bt_f);
185-
}
186-
187-
//Compute bt_list merkle_root
188-
let bt_mt = new_ginger_merkle_tree(bt_field_list.as_slice())?;
189-
190-
drop(bt_field_list);
191-
192-
let mr_bt = get_ginger_merkle_root(&bt_mt);
184+
let mr_bt = if bt_list.is_empty() {
185+
MNT4753_PHANTOM_MERKLE_ROOT
186+
} else {
187+
let mut bt_field_list = vec![];
188+
for bt in bt_list.iter() {
189+
let bt_f = bt.to_field_element()?;
190+
bt_field_list.push(bt_f);
191+
}
193192

194-
drop(bt_mt);
193+
//Compute bt_list merkle_root
194+
let bt_mt = new_ginger_merkle_tree(bt_field_list.as_slice())?;
195+
get_ginger_merkle_root(&bt_mt)
196+
};
195197

196198
//Compute message to be verified
197199
let msg = compute_poseidon_hash(&[mr_bt, *prev_end_epoch_mc_b_hash, *end_epoch_mc_b_hash])?;
@@ -416,8 +418,7 @@ mod test {
416418
unsafe { Vec::from_raw_parts(p as *mut i8, len, cap) }
417419
}
418420

419-
#[test]
420-
fn create_sample_naive_threshold_sig_circuit() {
421+
fn create_sample_naive_threshold_sig_circuit(bt_num: usize) {
421422
//assume to have 3 pks, threshold = 2
422423
let mut rng = OsRng;
423424

@@ -429,7 +430,6 @@ mod test {
429430
let end_epoch_mc_b_hash_f = read_field_element_from_buffer_with_padding(&end_epoch_mc_b_hash[..]).unwrap();
430431
let prev_end_epoch_mc_b_hash_f = read_field_element_from_buffer_with_padding(&prev_end_epoch_mc_b_hash[..]).unwrap();
431432

432-
let bt_num = 10;
433433
let mut bt_list = vec![];
434434
for _ in 0..bt_num {
435435
bt_list.push(BackwardTransfer::default());
@@ -504,6 +504,12 @@ mod test {
504504
).unwrap());
505505
}
506506

507+
#[test]
508+
fn naive_threshold_sig_circuit_test() {
509+
create_sample_naive_threshold_sig_circuit(10);
510+
create_sample_naive_threshold_sig_circuit(0);
511+
}
512+
507513
#[test]
508514
fn sample_schnorr_sig_prove_verify(){
509515
let mut rng = OsRng;

api/src/lib.rs

+55-49
Original file line numberDiff line numberDiff line change
@@ -1078,29 +1078,31 @@ pub extern "system" fn Java_com_horizen_sigproofnative_NaiveThresholdSigProof_na
10781078
let bt_list_size = _env.get_array_length(_bt_list)
10791079
.expect("Should be able to get bt_list size");
10801080

1081-
for i in 0..bt_list_size {
1082-
let o = _env.get_object_array_element(_bt_list, i)
1083-
.expect(format!("Should be able to get elem {} of bt_list array", i).as_str());
1081+
if bt_list_size > 0
1082+
{
1083+
for i in 0..bt_list_size {
1084+
let o = _env.get_object_array_element(_bt_list, i)
1085+
.expect(format!("Should be able to get elem {} of bt_list array", i).as_str());
10841086

1087+
let pk: [u8; 20] = {
1088+
let p = _env.call_method(o, "getPublicKeyHash", "()[B", &[])
1089+
.expect("Should be able to call getPublicKeyHash method").l().unwrap().cast();
10851090

1086-
let pk: [u8; 20] = {
1087-
let p = _env.call_method(o, "getPublicKeyHash", "()[B", &[])
1088-
.expect("Should be able to call getPublicKeyHash method").l().unwrap().cast();
1091+
let mut pk_bytes = [0u8; 20];
10891092

1090-
let mut pk_bytes = [0u8; 20];
1093+
_env.convert_byte_array(p)
1094+
.expect("Should be able to convert to Rust byte array")
1095+
.write(&mut pk_bytes[..])
1096+
.expect("Should be able to write into byte array of fixed size");
10911097

1092-
_env.convert_byte_array(p)
1093-
.expect("Should be able to convert to Rust byte array")
1094-
.write(&mut pk_bytes[..])
1095-
.expect("Should be able to write into byte array of fixed size");
1098+
pk_bytes
1099+
};
10961100

1097-
pk_bytes
1098-
};
1099-
1100-
let a = _env.call_method(o, "getAmount", "()J", &[])
1101-
.expect("Should be able to call getAmount method").j().unwrap() as u64;
1101+
let a = _env.call_method(o, "getAmount", "()J", &[])
1102+
.expect("Should be able to call getAmount method").j().unwrap() as u64;
11021103

1103-
bt_list.push(BackwardTransfer::new(pk, a));
1104+
bt_list.push(BackwardTransfer::new(pk, a));
1105+
}
11041106
}
11051107

11061108
//Extract block hashes
@@ -1176,29 +1178,31 @@ pub extern "system" fn Java_com_horizen_sigproofnative_NaiveThresholdSigProof_na
11761178
let bt_list_size = _env.get_array_length(_bt_list)
11771179
.expect("Should be able to get bt_list size");
11781180

1179-
for i in 0..bt_list_size {
1180-
let o = _env.get_object_array_element(_bt_list, i)
1181-
.expect(format!("Should be able to get elem {} of bt_list array", i).as_str());
1181+
if bt_list_size > 0 {
1182+
for i in 0..bt_list_size {
1183+
let o = _env.get_object_array_element(_bt_list, i)
1184+
.expect(format!("Should be able to get elem {} of bt_list array", i).as_str());
11821185

11831186

1184-
let pk: [u8; 20] = {
1185-
let p = _env.call_method(o, "getPublicKeyHash", "()[B", &[])
1186-
.expect("Should be able to call getPublicKeyHash method").l().unwrap().cast();
1187+
let pk: [u8; 20] = {
1188+
let p = _env.call_method(o, "getPublicKeyHash", "()[B", &[])
1189+
.expect("Should be able to call getPublicKeyHash method").l().unwrap().cast();
11871190

1188-
let mut pk_bytes = [0u8; 20];
1191+
let mut pk_bytes = [0u8; 20];
11891192

1190-
_env.convert_byte_array(p)
1191-
.expect("Should be able to convert to Rust byte array")
1192-
.write(&mut pk_bytes[..])
1193-
.expect("Should be able to write into byte array of fixed size");
1193+
_env.convert_byte_array(p)
1194+
.expect("Should be able to convert to Rust byte array")
1195+
.write(&mut pk_bytes[..])
1196+
.expect("Should be able to write into byte array of fixed size");
11941197

1195-
pk_bytes
1196-
};
1198+
pk_bytes
1199+
};
11971200

1198-
let a = _env.call_method(o, "getAmount", "()J", &[])
1199-
.expect("Should be able to call getAmount method").j().unwrap() as u64;
1201+
let a = _env.call_method(o, "getAmount", "()J", &[])
1202+
.expect("Should be able to call getAmount method").j().unwrap() as u64;
12001203

1201-
bt_list.push(BackwardTransfer::new(pk, a));
1204+
bt_list.push(BackwardTransfer::new(pk, a));
1205+
}
12021206
}
12031207

12041208
//Extract Schnorr signatures and the corresponding Schnorr pks
@@ -1335,29 +1339,31 @@ pub extern "system" fn Java_com_horizen_sigproofnative_NaiveThresholdSigProof_na
13351339
let bt_list_size = _env.get_array_length(_bt_list)
13361340
.expect("Should be able to get bt_list size");
13371341

1338-
for i in 0..bt_list_size {
1339-
let o = _env.get_object_array_element(_bt_list, i)
1340-
.expect(format!("Should be able to get elem {} of bt_list array", i).as_str());
1342+
if bt_list_size > 0 {
1343+
for i in 0..bt_list_size {
1344+
let o = _env.get_object_array_element(_bt_list, i)
1345+
.expect(format!("Should be able to get elem {} of bt_list array", i).as_str());
13411346

13421347

1343-
let pk: [u8; 20] = {
1344-
let p = _env.call_method(o, "getPublicKeyHash", "()[B", &[])
1345-
.expect("Should be able to call getPublicKeyHash method").l().unwrap().cast();
1348+
let pk: [u8; 20] = {
1349+
let p = _env.call_method(o, "getPublicKeyHash", "()[B", &[])
1350+
.expect("Should be able to call getPublicKeyHash method").l().unwrap().cast();
13461351

1347-
let mut pk_bytes = [0u8; 20];
1352+
let mut pk_bytes = [0u8; 20];
13481353

1349-
_env.convert_byte_array(p)
1350-
.expect("Should be able to convert to Rust byte array")
1351-
.write(&mut pk_bytes[..])
1352-
.expect("Should be able to write into byte array of fixed size");
1354+
_env.convert_byte_array(p)
1355+
.expect("Should be able to convert to Rust byte array")
1356+
.write(&mut pk_bytes[..])
1357+
.expect("Should be able to write into byte array of fixed size");
13531358

1354-
pk_bytes
1355-
};
1359+
pk_bytes
1360+
};
13561361

1357-
let a = _env.call_method(o, "getAmount", "()J", &[])
1358-
.expect("Should be able to call getAmount method").j().unwrap() as u64;
1362+
let a = _env.call_method(o, "getAmount", "()J", &[])
1363+
.expect("Should be able to call getAmount method").j().unwrap() as u64;
13591364

1360-
bt_list.push(BackwardTransfer::new(pk, a));
1365+
bt_list.push(BackwardTransfer::new(pk, a));
1366+
}
13611367
}
13621368

13631369
//Extract block hashes

jni/src/test/java/com/horizen/sigproofnative/NaiveThresholdSigProofTest.java

+91
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,96 @@ public void testcreateProof() {
132132
createAndVerifyProof();
133133
}
134134

135+
@Test
136+
public void testcreateProofWithoutBWT() {
137+
138+
endEpochBlockHash = new byte[] {
139+
61, -127, 80, -103, 117, -119, -44, -90, 52, -56, 79, -18, -64, -92, -42, -61, -89, 8, -107, 114, -6, -58,
140+
87, 123, 54, 3, 100, 121, -26, -80, -122, -90
141+
};
142+
143+
prevEndEpochBlockHash = new byte[] {
144+
95, 99, 89, 78, -113, 46, 99, -61, -11, -11, -24, 104, -51, -109, -48, 11, 119, 94, 104, -104, 38, -84,
145+
126, 22, -119, -96, -57, -67, 38, 109, 73, -22
146+
};
147+
148+
byte[][] secretKeyList = {
149+
{
150+
-63, 75, 3, -102, -107, 40, -92, 126, -93, 78, -33, -110, 98, -23, 115, -111, 39, -37, -88, 56, -25, 44,
151+
-59, 15, 106, -95, 105, 73, 14, 38, 81, -94, -36, -41, 57, -7, -104, 96, -38, -30, 15, -61, 36, -109,
152+
-74, -38, 70, -97, 67, -19, 74, -122, 98, -95, 27, -33, -44, 83, -20, 12, -44, -107, -81, -7, 24, -73,
153+
118, 70, 5, -41, 13, 109, -106, -8, 39, 79, 24, 94, -5, -61, 47, 124, -107, -99, -25, -81, -3, -104,
154+
-78, -76, 62, -45, -66, 74, 0, 0
155+
},
156+
{
157+
4, 80, -48, 29, 28, 54, -89, 82, 40, -76, -78, -111, 30, 51, 82, -64, -97, -33, 46, 91, 25, -20, 72,
158+
117, 84, 38, -53, 40, 26, 125, 77, -22, -16, -83, -23, 0, 52, -27, 57, -17, 83, -60, 59, 125, 97, 94,
159+
-118, -10, 33, -5, -79, 15, 105, -119, -99, 125, 107, -123, -27, 89, -56, -99, -114, 62, 31, 82, -80,
160+
108, 104, -114, -60, -70, 34, 118, 97, -90, 15, -92, 2, -65, -82, 78, 119, -119, 107, 103, 3, 115, 15,
161+
-76, -30, -98, -91, 14, 1, 0
162+
},
163+
{
164+
16, -108, 42, -111, 35, -85, 34, -91, -83, -68, -36, 89, -39, 2, -67, -15, -48, -64, -109, 2, -22, 87,
165+
127, 16, 72, -116, -39, -13, 76, -100, 83, -103, -32, 78, 6, -13, -127, -2, -10, -33, -9, 32, 67, 17,
166+
12, -77, 70, 112, 101, -28, 76, 42, -93, 31, -108, -42, -26, -45, 96, -45, 119, -56, 118, 68, -61, -27,
167+
94, 22, -30, -120, -111, -115, -99, -86, 97, -98, -49, 23, 8, -9, 88, 50, -46, 48, -83, 65, -88, 120,
168+
-25, -92, 76, -112, -76, 82, 1, 0
169+
}
170+
};
171+
172+
byte[][] serializedSignatureList = {
173+
{
174+
25, -17, 9, 92, -67, -25, 113, 99, -75, 110, -63, -63, 23, -107, -107, 8, 83, -94, 43, 55, 96, 71,
175+
-7, 37, -103, -28, -74, 125, -115, 125, 66, 33, -62, 54, 34, -6, 38, 26, -86, 62, 37, 26, -90, -46, -21,
176+
-47, 101, -105, 48, 62, 23, -64, -49, 20, -45, 41, -116, 21, -112, -79, -5, -56, -4, -33, -65, 76, -31,
177+
89, 15, 3, -41, -88, 48, -105, -98, -115, -49, -23, 98, -69, 105, 2, 13, 76, -107, 88, -61, 15, -122,
178+
73, 19, -117, -124, -16, 0, 0, 124, -66, 28, -41, -12, -12, 108, 19, 88, 107, 106, -5, 52, 122, 101,
179+
-59, -73, -99, 24, 43, -81, -93, 72, -83, -126, 24, 102, -125, -115, -27, 75, 92, 17, -61, -14, -58, 46,
180+
-104, 101, 94, -124, 117, 1, -66, 48, -127, -103, 32, -3, -81, 115, -21, 67, 126, 36, -74, 56, 113, 31,
181+
-123, 30, 8, -82, -115, 100, -89, 93, -105, -35, -82, 98, 34, 58, 77, 79, 56, 94, -111, -124, 17, 4,
182+
-13, -108, -121, 30, -89, 96, 43, 67, -55, 38, -61, -123, -119, 0, 0
183+
},
184+
{
185+
-72, 57, 95, -103, 95, 18, -39, -31, 116, 33, -76, -46, -121, 79, -123, 45, 119, 104, 1, -50, -95, 14,
186+
-93, 41, -63, 82, -61, 22, 65, -103, 115, -106, 69, -4, -40, -80, 45, -66, 105, 30, -55, 9, -86, -60,
187+
48, 100, -81, -1, 45, 111, -55, -93, 5, 33, -101, -56, 56, 10, 110, 22, -66, -57, 21, -102, -44, 54,
188+
-119, 121, -32, 51, -4, 70, -74, -26, 91, -20, 8, -43, -5, 75, -73, 12, -43, 46, -4, 98, 49, -20, 97, 8,
189+
87, 53, -65, 119, 0, 0, -16, 35, -123, 124, -6, 10, 61, -27, 123, 4, -66, 71, -92, -125, 108, -76, 104,
190+
79, -74, 42, 125, -59, 89, 126, 124, -21, 67, -56, 22, -109, -39, -30, 4, 53, 5, 111, -96, 82, 123, 77,
191+
86, 103, -75, 28, -79, -98, 108, -73, 55, 117, -22, -126, -7, 103, -10, 28, -73, 14, -37, -47, 56, -49,
192+
-34, 52, -117, 18, -99, -49, -73, -61, -114, -59, 43, -28, -30, 39, 29, -111, -98, -119, -40, 15, -75,
193+
24, 35, 118, 38, 85, -53, 8, 70, 80, 74, -117, 0, 0
194+
}
195+
};
196+
197+
// Deserialize secret keys and get the corresponding public keys
198+
for (int i = 0; i<keyCount; i++) {
199+
200+
SchnorrSecretKey sk = SchnorrSecretKey.deserialize(secretKeyList[i]);
201+
assertNotNull("sk" + i + "deserialization must not fail", sk);
202+
203+
SchnorrPublicKey pk = new SchnorrKeyPair(sk).getPublicKey();
204+
assertTrue("Public key verification failed.", pk.verifyKey());
205+
206+
publicKeyList.add(pk);
207+
sk.freeSecretKey();
208+
}
209+
210+
// Deserialize Schnorr Signatures
211+
for (int i = 0; i<keyCount; i++) {
212+
if (i < threshold) {
213+
SchnorrSignature sig = SchnorrSignature.deserialize(serializedSignatureList[i]);
214+
assertNotNull("sig" + i + "deserialization must not fail", sig);
215+
signatureList.add(sig);
216+
} else {
217+
SchnorrSignature sig = new SchnorrSignature();
218+
signatureList.add(sig);
219+
}
220+
}
221+
222+
createAndVerifyProof();
223+
}
224+
135225
@Test
136226
public void testCreateRandomProof(){
137227
Random r = new Random();
@@ -140,6 +230,7 @@ public void testCreateRandomProof(){
140230

141231
r.nextBytes(prevEndEpochBlockHash);
142232

233+
backwardTransferCout = r.nextInt(backwardTransferCout + 1);
143234
// Create dummy Backward Transfers
144235
for(int i = 0; i < backwardTransferCout; i++) {
145236

0 commit comments

Comments
 (0)