From 3d2e9efb1ed856f68a42662875e4e9df97908958 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 27 Mar 2017 18:39:21 +0300 Subject: [PATCH 01/13] P1, P2 definition --- Cargo.lock | 2 +- ethcore/src/builtin.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1a25c8553..3b5eded0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -180,7 +180,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "bn" version = "0.4.3" -source = "git+https://github.com/paritytech/bn#59d848e642ad1ff0d60e39348576a6f11ee123b8" +source = "git+https://github.com/paritytech/bn#5df8d83c19f3fb97ae3fdefa5bad5279babe795e" dependencies = [ "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index bdedd5739..c240250df 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -191,6 +191,9 @@ struct Bn128AddImpl; #[derive(Debug)] struct Bn128MulImpl; +#[derive(Debug)] +struct Bn128ParingImpl; + impl Impl for Identity { fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { output.write(0, input); @@ -393,6 +396,31 @@ impl Impl for Bn128MulImpl { } } +impl Impl for Bn128ParingImpl { + // Can fail if any of the 2 points does not belong the bn128 curve + fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { + use bn::{Fq, Fq2, AffineG1, AffineG2}; + + let p1 = AffineG1::new( + Fq::from_str("1").expect("1 is a valid field element"), + Fq::from_str("2").expect("2 is a valid field element"), + ).expect("Generator P1(1, 2) is a valid curve point"); + + let p2 = AffineG2::new( + Fq2::new( + Fq::from_str("1").expect("1 is a valid field element"), + Fq::from_str("2").expect("2 is a valid field element"), + ), + Fq2::new( + Fq::from_str("1").expect("1 is a valid field element"), + Fq::from_str("2").expect("2 is a valid field element"), + ), + ).expect("Generator P2(i+2b, i+2b) is a valid curve point"); + + Ok(()) + } +} + #[cfg(test)] mod tests { use super::{Builtin, Linear, ethereum_builtin, Pricer, Modexp}; From 9271dd0cc7f6d2185d720378494f6872ff616b75 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 27 Mar 2017 19:44:35 +0300 Subject: [PATCH 02/13] wip --- ethcore/src/builtin.rs | 93 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 8 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index c240250df..f5e500154 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -396,17 +396,18 @@ impl Impl for Bn128MulImpl { } } -impl Impl for Bn128ParingImpl { - // Can fail if any of the 2 points does not belong the bn128 curve - fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { - use bn::{Fq, Fq2, AffineG1, AffineG2}; +mod bn128_gen { + use bn::{AffineG1, AffineG2, Fq, Fq2, G1, G2, Gt, pairing}; - let p1 = AffineG1::new( + lazy_static! { + pub static ref P1: G1 = G1::from(AffineG1::new( Fq::from_str("1").expect("1 is a valid field element"), Fq::from_str("2").expect("2 is a valid field element"), - ).expect("Generator P1(1, 2) is a valid curve point"); + ).expect("Generator P1(1, 2) is a valid curve point")); + } - let p2 = AffineG2::new( + lazy_static! { + pub static ref P2: G2 = G2::from(AffineG2::new( Fq2::new( Fq::from_str("1").expect("1 is a valid field element"), Fq::from_str("2").expect("2 is a valid field element"), @@ -415,7 +416,83 @@ impl Impl for Bn128ParingImpl { Fq::from_str("1").expect("1 is a valid field element"), Fq::from_str("2").expect("2 is a valid field element"), ), - ).expect("Generator P2(i+2b, i+2b) is a valid curve point"); + ).expect("Generator P2(i+2b, i+2b) is a valid curve point")); + } + + + lazy_static! { + pub static ref P1xP2: Gt = pairing(P1.clone(), P2.clone()); + } +} + +impl Impl for Bn128ParingImpl { + // Can fail if any of the 2 points does not belong the bn128 curve + fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { + use bn::{AffineG1, AffineG2, Fq, Fq2, pairing, G1, G2}; + + let elements = input.len() / 192; // (a, b_a, b_b - each 64-byte affine coordinates) + if input.len() % 192 != 0 { + return Err("Invalid input length, must be multiple of 192 (3 * (32*2))".into()) + } + let ret_val = if input.len() == 0 { + U256::one() + } else { + let mut vals = Vec::new(); + for idx in 0..elements { + let a_x = Fq::from_slice(&input[idx*192..idx*192+32]) + .map_err(|_| Error::from("Invalid a argument x coordinate"))?; + + let a_y = Fq::from_slice(&input[idx*192+32..idx*192+64]) + .map_err(|_| Error::from("Invalid a argument y coordinate"))?; + + let b_b_x = Fq::from_slice(&input[idx*192+64..idx*192+96]) + .map_err(|_| Error::from("Invalid b argument imaginary coeff x coordinate"))?; + + let b_b_y = Fq::from_slice(&input[idx*192+96..idx*192+128]) + .map_err(|_| Error::from("Invalid b argument imaginary coeff y coordinate"))?; + + let b_a_x = Fq::from_slice(&input[idx*192+128..idx*192+160]) + .map_err(|_| Error::from("Invalid b argument real coeff x coordinate"))?; + + let b_a_y = Fq::from_slice(&input[idx*192+160..idx*192+192]) + .map_err(|_| Error::from("Invalid b argument real coeff y coordinate"))?; + + vals.push(( + G1::from( + AffineG1::new(a_x, a_y).map_err(|_| Error::from("Invalid a argument - not on curve"))? + ), + G2::from( + AffineG2::new( + Fq2::new(b_a_x, b_a_y), + Fq2::new(b_b_x, b_b_y), + ).map_err(|_| Error::from("Invalid b argument - not on curve"))? + ), + )); + }; + let mul = if elements == 1 { + let (a, b) = vals[0]; + pairing(a, b) + } else { + let mut drain = vals.drain(..); + let mut mul = { + let (a, b) = drain.next() + .expect("Checked above that elements > 1, so 0th element should exist; qed"); + pairing(a, b) + }; + for _ in 1..elements { + let (a, b) = drain.next() + .expect("idx-th element should exist, because we do next() no more than elements-1 times; qed"); + mul = mul * pairing(a, b); + } + mul + }; + + if mul == *bn128_gen::P1xP2 { + U256::one() + } else { + U256::zero() + } + }; Ok(()) } From d34aec29ed412792db18ac20aaf5090bdd8a3ffa Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 27 Mar 2017 21:34:02 +0300 Subject: [PATCH 03/13] finalize --- ethcore/res/ethereum/foundation.json | 1 + ethcore/src/builtin.rs | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/ethcore/res/ethereum/foundation.json b/ethcore/res/ethereum/foundation.json index 54f10b70c..68b74a0f5 100644 --- a/ethcore/res/ethereum/foundation.json +++ b/ethcore/res/ethereum/foundation.json @@ -192,6 +192,7 @@ "0000000000000000000000000000000000000005": { "builtin": { "name": "modexp", "activate_at": "0x7fffffffffffff", "pricing": { "modexp": { "divisor": 20 } } } }, "0000000000000000000000000000000000000006": { "builtin": { "name": "bn128_add", "activate_at": "0x7fffffffffffff", "pricing": { "linear": { "base": 999999, "word": 0 } } } }, "0000000000000000000000000000000000000007": { "builtin": { "name": "bn128_mul", "activate_at": "0x7fffffffffffff", "pricing": { "linear": { "base": 999999, "word": 0 } } } }, + "0000000000000000000000000000000000000008": { "builtin": { "name": "bn128_pairing", "activate_at": "0x7fffffffffffff", "pricing": { "linear": { "base": 999999, "word": 0 } } } }, "3282791d6fd713f1e94f4bfd565eaa78b3a0599d": { "balance": "1337000000000000000000" }, diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index f5e500154..cc214f2a3 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -158,6 +158,7 @@ fn ethereum_builtin(name: &str) -> Box { "modexp" => Box::new(ModexpImpl) as Box, "bn128_add" => Box::new(Bn128AddImpl) as Box, "bn128_mul" => Box::new(Bn128MulImpl) as Box, + "bn128_pairing" => Box::new(Bn128ParingImpl) as Box, _ => panic!("invalid builtin name: {}", name), } } @@ -409,19 +410,18 @@ mod bn128_gen { lazy_static! { pub static ref P2: G2 = G2::from(AffineG2::new( Fq2::new( - Fq::from_str("1").expect("1 is a valid field element"), - Fq::from_str("2").expect("2 is a valid field element"), + Fq::from_str("10857046999023057135944570762232829481370756359578518086990519993285655852781").expect("1 is a valid field element"), + Fq::from_str("11559732032986387107991004021392285783925812861821192530917403151452391805634").expect("2 is a valid field element"), ), Fq2::new( - Fq::from_str("1").expect("1 is a valid field element"), - Fq::from_str("2").expect("2 is a valid field element"), + Fq::from_str("8495653923123431417604973247489272438418190587263600148770280649306958101930").expect("1 is a valid field element"), + Fq::from_str("4082367875863433681332203403145435568316851327593401208105741076214120093531").expect("2 is a valid field element"), ), - ).expect("Generator P2(i+2b, i+2b) is a valid curve point")); + ).expect("Generator P2(10857046999023057135944570762232829481370756359578518086990519993285655852781 + 11559732032986387107991004021392285783925812861821192530917403151452391805634i, 8495653923123431417604973247489272438418190587263600148770280649306958101930 + 4082367875863433681332203403145435568316851327593401208105741076214120093531i) is a valid curve point")); } - lazy_static! { - pub static ref P1xP2: Gt = pairing(P1.clone(), P2.clone()); + pub static ref P1_P2_PAIRING: Gt = pairing(P1.clone(), P2.clone()); } } @@ -481,19 +481,23 @@ impl Impl for Bn128ParingImpl { }; for _ in 1..elements { let (a, b) = drain.next() - .expect("idx-th element should exist, because we do next() no more than elements-1 times; qed"); + .expect("this element should exist, because we do next() no more than elements-1 times; qed"); mul = mul * pairing(a, b); } mul }; - if mul == *bn128_gen::P1xP2 { + if mul == *bn128_gen::P1_P2_PAIRING { U256::one() } else { U256::zero() } }; + let mut buf = [0u8; 32]; + ret_val.to_big_endian(&mut buf); + output.write(0, &buf); + Ok(()) } } From 2f30b030605ec17c0c6feecb926c9a34d78404b1 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 28 Mar 2017 01:38:04 +0300 Subject: [PATCH 04/13] some tests --- ethcore/src/builtin.rs | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index cc214f2a3..0113e94f2 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -507,6 +507,7 @@ mod tests { use super::{Builtin, Linear, ethereum_builtin, Pricer, Modexp}; use ethjson; use util::{U256, BytesRef}; + use rustc_serialize::hex::FromHex; #[test] fn identity() { @@ -822,7 +823,82 @@ mod tests { assert!(res.is_err(), "There should be built-in error here"); } } + + fn builitin_pairing() -> Builtin { + Builtin { + pricer: Box::new(Linear { base: 0, word: 0 }), + native: ethereum_builtin("bn128_pairing"), + activate_at: 0, + } + } + + fn empty_test(f: Builtin, expected: Vec) { + let mut empty = [0u8; 0]; + let input = BytesRef::Fixed(&mut empty); + + let mut output = vec![0u8; expected.len()]; + + f.execute(&input[..], &mut BytesRef::Fixed(&mut output[..])).expect("Builtin should not fail"); + assert_eq!(output, expected); + } + + fn error_test(f: Builtin, input: &[u8], msg_contains: Option<&str>) { + let mut output = vec![0u8; 64]; + let res = f.execute(input, &mut BytesRef::Fixed(&mut output[..])); + if let Some(msg) = msg_contains { + if let Err(e) = res { + if !e.0.contains(msg) { + panic!("There should be error containing '{}' here, but got: '{}'", msg, e.0); + } + } + } else { + assert!(res.is_err(), "There should be built-in error here"); + } + } + + fn bytes(s: &'static str) -> Vec { + FromHex::from_hex(s).expect("static str should contain valid hex bytes") + } + #[test] + fn bn128_pairing_empty() { + // should not fail, because empty input is a valid input of 0 elements + empty_test( + builitin_pairing(), + bytes("0000000000000000000000000000000000000000000000000000000000000001"), + ); + } + + #[test] + fn bn128_pairing_notcurve() { + // should fail - point not on curve + error_test( + builitin_pairing(), + &bytes("\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 1111111111111111111111111111111111111111111111111111111111111111" + ), + Some("not on curve"), + ); + } + + #[test] + fn bn128_pairing_fragmented() { + // should fail - input length is invalid + error_test( + builitin_pairing(), + &bytes("\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 1111111111111111111111111111111111111111111111111111111111111111\ + 111111111111111111111111111111" + ), + Some("Invalid input length"), + ); + } #[test] #[should_panic] From a529427bc935303890dbec53868858bf8715c20a Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 28 Mar 2017 01:42:37 +0300 Subject: [PATCH 05/13] bump bn lib --- Cargo.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b5eded0a..26fec8a73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,8 +179,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "bn" -version = "0.4.3" -source = "git+https://github.com/paritytech/bn#5df8d83c19f3fb97ae3fdefa5bad5279babe795e" +version = "0.4.4" +source = "git+https://github.com/paritytech/bn#b97e95a45f4484a41a515338c4f0e093bf6675e0" dependencies = [ "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -387,7 +387,7 @@ version = "1.7.0" dependencies = [ "bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "bloomchain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "bn 0.4.3 (git+https://github.com/paritytech/bn)", + "bn 0.4.4 (git+https://github.com/paritytech/bn)", "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.103 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2642,7 +2642,7 @@ dependencies = [ "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" "checksum blastfig 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "09640e0509d97d5cdff03a9f5daf087a8e04c735c3b113a75139634a19cfc7b2" "checksum bloomchain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f421095d2a76fc24cd3fb3f912b90df06be7689912b1bdb423caefae59c258d" -"checksum bn 0.4.3 (git+https://github.com/paritytech/bn)" = "" +"checksum bn 0.4.4 (git+https://github.com/paritytech/bn)" = "" "checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8" "checksum bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c129aff112dcc562970abb69e2508b40850dd24c274761bb50fb8a0067ba6c27" "checksum bytes 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "46112a0060ae15e3a3f9a445428a53e082b91215b744fa27a1948842f4a64b96" From 569d63d0c99db7abe6215bb9ec2394dfdf1f2c82 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 31 Mar 2017 17:46:44 +0300 Subject: [PATCH 06/13] fix assertions --- ethcore/src/builtin.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index 0113e94f2..a2677abc7 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -410,14 +410,18 @@ mod bn128_gen { lazy_static! { pub static ref P2: G2 = G2::from(AffineG2::new( Fq2::new( - Fq::from_str("10857046999023057135944570762232829481370756359578518086990519993285655852781").expect("1 is a valid field element"), - Fq::from_str("11559732032986387107991004021392285783925812861821192530917403151452391805634").expect("2 is a valid field element"), + Fq::from_str("10857046999023057135944570762232829481370756359578518086990519993285655852781") + .expect("a valid field element"), + Fq::from_str("11559732032986387107991004021392285783925812861821192530917403151452391805634") + .expect("a valid field element"), ), Fq2::new( - Fq::from_str("8495653923123431417604973247489272438418190587263600148770280649306958101930").expect("1 is a valid field element"), - Fq::from_str("4082367875863433681332203403145435568316851327593401208105741076214120093531").expect("2 is a valid field element"), + Fq::from_str("8495653923123431417604973247489272438418190587263600148770280649306958101930") + .expect("a valid field element"), + Fq::from_str("4082367875863433681332203403145435568316851327593401208105741076214120093531") + .expect("a valid field element"), ), - ).expect("Generator P2(10857046999023057135944570762232829481370756359578518086990519993285655852781 + 11559732032986387107991004021392285783925812861821192530917403151452391805634i, 8495653923123431417604973247489272438418190587263600148770280649306958101930 + 4082367875863433681332203403145435568316851327593401208105741076214120093531i) is a valid curve point")); + ).expect("the generator P2(10857046999023057135944570762232829481370756359578518086990519993285655852781 + 11559732032986387107991004021392285783925812861821192530917403151452391805634i, 8495653923123431417604973247489272438418190587263600148770280649306958101930 + 4082367875863433681332203403145435568316851327593401208105741076214120093531i) is a valid curve point")); } lazy_static! { From d6baadbaf2b24660066c30d41b8915e769ad7682 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 31 Mar 2017 17:53:26 +0300 Subject: [PATCH 07/13] fix doc comment about failing --- ethcore/src/builtin.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index a2677abc7..efe59d43f 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -430,11 +430,14 @@ mod bn128_gen { } impl Impl for Bn128ParingImpl { - // Can fail if any of the 2 points does not belong the bn128 curve + /// Can fail if: + /// - input length is not a multiple of 192 + /// - any of odd points does not belong to bn128 curve + /// - any of even points does not belong to the twisted bn128 curve over the field F_p^2 = F_p[i] / (i^2 + 1) fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { use bn::{AffineG1, AffineG2, Fq, Fq2, pairing, G1, G2}; - let elements = input.len() / 192; // (a, b_a, b_b - each 64-byte affine coordinates) + let elements = input.len() / 192; // (a, b_a, b_b - each 64-byte affine coordinate) if input.len() % 192 != 0 { return Err("Invalid input length, must be multiple of 192 (3 * (32*2))".into()) } From df72c9c6cc77ad32a97b85afc1722e3ea35e5eba Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 3 Apr 2017 12:57:18 +0300 Subject: [PATCH 08/13] fold multiplication --- ethcore/src/builtin.rs | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index efe59d43f..88cf357a3 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -435,9 +435,9 @@ impl Impl for Bn128ParingImpl { /// - any of odd points does not belong to bn128 curve /// - any of even points does not belong to the twisted bn128 curve over the field F_p^2 = F_p[i] / (i^2 + 1) fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { - use bn::{AffineG1, AffineG2, Fq, Fq2, pairing, G1, G2}; + use bn::{AffineG1, AffineG2, Fq, Fq2, pairing, G1, G2, Gt}; - let elements = input.len() / 192; // (a, b_a, b_b - each 64-byte affine coordinate) + let elements = input.len() / 192; // (a, b_a, b_b - each 64-byte affine coordinates) if input.len() % 192 != 0 { return Err("Invalid input length, must be multiple of 192 (3 * (32*2))".into()) } @@ -476,23 +476,8 @@ impl Impl for Bn128ParingImpl { ), )); }; - let mul = if elements == 1 { - let (a, b) = vals[0]; - pairing(a, b) - } else { - let mut drain = vals.drain(..); - let mut mul = { - let (a, b) = drain.next() - .expect("Checked above that elements > 1, so 0th element should exist; qed"); - pairing(a, b) - }; - for _ in 1..elements { - let (a, b) = drain.next() - .expect("this element should exist, because we do next() no more than elements-1 times; qed"); - mul = mul * pairing(a, b); - } - mul - }; + + let mul = vals.into_iter().fold(Gt::one(), |s, (a, b)| s * pairing(a, b)); if mul == *bn128_gen::P1_P2_PAIRING { U256::one() From 9bcd1245d9d669c436a0a397adc6c0f1d4b89282 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 6 Apr 2017 11:04:28 +0300 Subject: [PATCH 09/13] fix typo --- ethcore/src/builtin.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index 88cf357a3..32087be96 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -158,7 +158,7 @@ fn ethereum_builtin(name: &str) -> Box { "modexp" => Box::new(ModexpImpl) as Box, "bn128_add" => Box::new(Bn128AddImpl) as Box, "bn128_mul" => Box::new(Bn128MulImpl) as Box, - "bn128_pairing" => Box::new(Bn128ParingImpl) as Box, + "bn128_pairing" => Box::new(Bn128PairingImpl) as Box, _ => panic!("invalid builtin name: {}", name), } } @@ -193,7 +193,7 @@ struct Bn128AddImpl; struct Bn128MulImpl; #[derive(Debug)] -struct Bn128ParingImpl; +struct Bn128PairingImpl; impl Impl for Identity { fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { @@ -429,7 +429,7 @@ mod bn128_gen { } } -impl Impl for Bn128ParingImpl { +impl Impl for Bn128PairingImpl { /// Can fail if: /// - input length is not a multiple of 192 /// - any of odd points does not belong to bn128 curve From 167d06d727079450c5812924091eb955a350a251 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 20 Apr 2017 16:21:53 +0200 Subject: [PATCH 10/13] fix panickers in tree-route --- ethcore/src/blockchain/blockchain.rs | 48 ++++++++++++++++------------ ethcore/src/client/client.rs | 2 +- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index 39c1039ca..feaf52a03 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -650,12 +650,19 @@ impl BlockChain { /// ```json /// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 } /// ``` - pub fn tree_route(&self, from: H256, to: H256) -> TreeRoute { + /// + /// If the tree route verges into pruned or unknown blocks, + /// `None` is returned. + pub fn tree_route(&self, from: H256, to: H256) -> Option { + macro_rules! otry { + ($e:expr) => { match $e { Some(x) => x, None => return None } } + } + let mut from_branch = vec![]; let mut to_branch = vec![]; - let mut from_details = self.block_details(&from).unwrap_or_else(|| panic!("0. Expected to find details for block {:?}", from)); - let mut to_details = self.block_details(&to).unwrap_or_else(|| panic!("1. Expected to find details for block {:?}", to)); + let mut from_details = otry!(self.block_details(&from)); + let mut to_details = otry!(self.block_details(&to)); let mut current_from = from; let mut current_to = to; @@ -663,13 +670,13 @@ impl BlockChain { while from_details.number > to_details.number { from_branch.push(current_from); current_from = from_details.parent.clone(); - from_details = self.block_details(&from_details.parent).unwrap_or_else(|| panic!("2. Expected to find details for block {:?}", from_details.parent)); + from_details = otry!(self.block_details(&from_details.parent)); } while to_details.number > from_details.number { to_branch.push(current_to); current_to = to_details.parent.clone(); - to_details = self.block_details(&to_details.parent).unwrap_or_else(|| panic!("3. Expected to find details for block {:?}", to_details.parent)); + to_details = otry!(self.block_details(&to_details.parent)); } assert_eq!(from_details.number, to_details.number); @@ -678,22 +685,22 @@ impl BlockChain { while current_from != current_to { from_branch.push(current_from); current_from = from_details.parent.clone(); - from_details = self.block_details(&from_details.parent).unwrap_or_else(|| panic!("4. Expected to find details for block {:?}", from_details.parent)); + from_details = otry!(self.block_details(&from_details.parent)); to_branch.push(current_to); current_to = to_details.parent.clone(); - to_details = self.block_details(&to_details.parent).unwrap_or_else(|| panic!("5. Expected to find details for block {:?}", from_details.parent)); + to_details = otry!(self.block_details(&to_details.parent)); } let index = from_branch.len(); from_branch.extend(to_branch.into_iter().rev()); - TreeRoute { + Some(TreeRoute { blocks: from_branch, ancestor: current_from, index: index - } + }) } /// Inserts a verified, known block from the canonical chain. @@ -879,7 +886,8 @@ impl BlockChain { // are moved to "canon chain" // find the route between old best block and the new one let best_hash = self.best_block_hash(); - let route = self.tree_route(best_hash, parent_hash); + let route = self.tree_route(best_hash, parent_hash) + .expect("blocks being imported always within recent history; qed"); assert_eq!(number, parent_details.number + 1); @@ -1711,52 +1719,52 @@ mod tests { assert_eq!(bc.block_hash(3).unwrap(), b3a_hash); // test trie route - let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone()); + let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone()).unwrap(); assert_eq!(r0_1.ancestor, genesis_hash); assert_eq!(r0_1.blocks, [b1_hash.clone()]); assert_eq!(r0_1.index, 0); - let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone()); + let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone()).unwrap(); assert_eq!(r0_2.ancestor, genesis_hash); assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]); assert_eq!(r0_2.index, 0); - let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone()); + let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone()).unwrap(); assert_eq!(r1_3a.ancestor, b1_hash); assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]); assert_eq!(r1_3a.index, 0); - let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone()); + let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone()).unwrap(); assert_eq!(r1_3b.ancestor, b1_hash); assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]); assert_eq!(r1_3b.index, 0); - let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone()); + let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone()).unwrap(); assert_eq!(r3a_3b.ancestor, b2_hash); assert_eq!(r3a_3b.blocks, [b3a_hash.clone(), b3b_hash.clone()]); assert_eq!(r3a_3b.index, 1); - let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone()); + let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone()).unwrap(); assert_eq!(r1_0.ancestor, genesis_hash); assert_eq!(r1_0.blocks, [b1_hash.clone()]); assert_eq!(r1_0.index, 1); - let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone()); + let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone()).unwrap(); assert_eq!(r2_0.ancestor, genesis_hash); assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]); assert_eq!(r2_0.index, 2); - let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone()); + let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone()).unwrap(); assert_eq!(r3a_1.ancestor, b1_hash); assert_eq!(r3a_1.blocks, [b3a_hash.clone(), b2_hash.clone()]); assert_eq!(r3a_1.index, 2); - let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone()); + let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone()).unwrap(); assert_eq!(r3b_1.ancestor, b1_hash); assert_eq!(r3b_1.blocks, [b3b_hash.clone(), b2_hash.clone()]); assert_eq!(r3b_1.index, 2); - let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone()); + let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone()).unwrap(); assert_eq!(r3b_3a.ancestor, b2_hash); assert_eq!(r3b_3a.blocks, [b3b_hash.clone(), b3a_hash.clone()]); assert_eq!(r3b_3a.index, 1); diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index e7755e4d0..0656480f6 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -1270,7 +1270,7 @@ impl BlockChainClient for Client { fn tree_route(&self, from: &H256, to: &H256) -> Option { let chain = self.chain.read(); match chain.is_known(from) && chain.is_known(to) { - true => Some(chain.tree_route(from.clone(), to.clone())), + true => chain.tree_route(from.clone(), to.clone()), false => None } } From 6c33b781c349f81dc83d8da06a1295c4a3a07b08 Mon Sep 17 00:00:00 2001 From: Dmitriy Startsev Date: Thu, 20 Apr 2017 19:03:43 +0300 Subject: [PATCH 11/13] Update links to etherscan.io (#5455) --- js/src/3rdparty/etherscan/call.js | 23 +--------- js/src/3rdparty/etherscan/links.js | 47 +++++++++++--------- js/src/3rdparty/etherscan/links.spec.js | 57 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 js/src/3rdparty/etherscan/links.spec.js diff --git a/js/src/3rdparty/etherscan/call.js b/js/src/3rdparty/etherscan/call.js index 6b72e1bea..069627d78 100644 --- a/js/src/3rdparty/etherscan/call.js +++ b/js/src/3rdparty/etherscan/call.js @@ -15,6 +15,7 @@ // along with Parity. If not, see . import { stringify } from 'qs'; +import { apiLink } from './links'; const options = { method: 'GET', @@ -24,31 +25,11 @@ const options = { }; export function call (module, action, _params, test, netVersion) { - let prefix = 'api.'; - - switch (netVersion) { - case '2': - case '3': - prefix = 'testnet.'; - break; - - case '42': - prefix = 'kovan.'; - break; - - case '0': - default: - if (test) { - prefix = 'testnet.'; - } - break; - } - const query = stringify(Object.assign({ module, action }, _params || {})); - return fetch(`https://${prefix}etherscan.io/api?${query}`, options) + return fetch(apiLink(query, test, netVersion), options) .then((response) => { if (!response.ok) { throw { code: response.status, message: response.statusText }; // eslint-disable-line diff --git a/js/src/3rdparty/etherscan/links.js b/js/src/3rdparty/etherscan/links.js index 8c9101268..3b08d60e8 100644 --- a/js/src/3rdparty/etherscan/links.js +++ b/js/src/3rdparty/etherscan/links.js @@ -15,28 +15,31 @@ // along with Parity. If not, see . // NOTE: Keep 'isTestnet' for backwards library compatibility -export const url = (isTestnet = false, netVersion = '0') => { - let prefix = ''; - - switch (netVersion) { - case '2': - case '3': - prefix = 'testnet.'; - break; - - case '42': - prefix = 'kovan.'; - break; - - case '0': - default: - if (isTestnet) { - prefix = 'testnet.'; - } - break; +const getUrlPrefix = (isTestnet = false, netVersion = '0', defaultPrefix = '') => { + if (isTestnet) { + return 'ropsten.'; } - return `https://${prefix}etherscan.io`; + switch (netVersion) { + case '1': + return defaultPrefix; + + case '3': + return 'ropsten.'; + + case '4': + return 'rinkeby.'; + + case '42': + return 'kovan.'; + + default: + return 'testnet.'; + } +}; + +export const url = (isTestnet = false, netVersion = '0', defaultPrefix = '') => { + return `https://${getUrlPrefix(isTestnet, netVersion, defaultPrefix)}etherscan.io`; }; export const txLink = (hash, isTestnet = false, netVersion = '0') => { @@ -46,3 +49,7 @@ export const txLink = (hash, isTestnet = false, netVersion = '0') => { export const addressLink = (address, isTestnet = false, netVersion = '0') => { return `${url(isTestnet, netVersion)}/address/${address}`; }; + +export const apiLink = (query, isTestnet = false, netVersion = '0') => { + return `${url(isTestnet, netVersion, 'api.')}/api?${query}`; +}; diff --git a/js/src/3rdparty/etherscan/links.spec.js b/js/src/3rdparty/etherscan/links.spec.js new file mode 100644 index 000000000..c906940cb --- /dev/null +++ b/js/src/3rdparty/etherscan/links.spec.js @@ -0,0 +1,57 @@ +// Copyright 2015-2017 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { url, txLink, addressLink, apiLink } = require('./links'); + +describe('etherscan/links', function () { + it('builds link with a prefix', () => { + expect(url(false, '1', 'api.')).to.be.equal('https://api.etherscan.io'); + }); + + it('builds link to main network', () => { + expect(url(false, '1')).to.be.equal('https://etherscan.io'); + }); + + it('builds link to ropsten', () => { + expect(url(false, '3')).to.be.equal('https://ropsten.etherscan.io'); + expect(url(true)).to.be.equal('https://ropsten.etherscan.io'); + }); + + it('builds link to kovan', () => { + expect(url(false, '42')).to.be.equal('https://kovan.etherscan.io'); + }); + + it('builds link to rinkeby', () => { + expect(url(false, '4')).to.be.equal('https://rinkeby.etherscan.io'); + }); + + it('builds link to the testnet selector for unknown networks', () => { + expect(url(false, '10042')).to.be.equal('https://testnet.etherscan.io'); + expect(url(false, '51224')).to.be.equal('https://testnet.etherscan.io'); + }); + + it('builds transaction link', () => { + expect(txLink('aTxHash', false, '1')).to.be.equal('https://etherscan.io/tx/aTxHash'); + }); + + it('builds address link', () => { + expect(addressLink('anAddress', false, '1')).to.be.equal('https://etherscan.io/address/anAddress'); + }); + + it('builds api link', () => { + expect(apiLink('answer=42', false, '1')).to.be.equal('https://api.etherscan.io/api?answer=42'); + }); +}); From 70c82c0f61c6f4215fd8a83727ceb420f3b30330 Mon Sep 17 00:00:00 2001 From: GitLab Build Bot Date: Thu, 20 Apr 2017 19:28:34 +0000 Subject: [PATCH 12/13] [ci skip] js-precompiled 20170420-192450 --- Cargo.lock | 2 +- js/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf6d3bb6d..bb457df59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1773,7 +1773,7 @@ dependencies = [ [[package]] name = "parity-ui-precompiled" version = "1.4.0" -source = "git+https://github.com/paritytech/js-precompiled.git#bceb0caa1eae7d78ad3386338a76b7ca92bf033a" +source = "git+https://github.com/paritytech/js-precompiled.git#8645ef0d41c9c325556fad8241f2cedaf4ce2d79" dependencies = [ "parity-dapps-glue 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/js/package.json b/js/package.json index 07314f84d..d6c83f075 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "parity.js", - "version": "1.7.57", + "version": "1.7.58", "main": "release/index.js", "jsnext:main": "src/index.js", "author": "Parity Team ", From 7a1db0852b78d724c06acab535ced70beb5ad762 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 21 Apr 2017 12:10:41 +0300 Subject: [PATCH 13/13] fix typo --- ethcore/src/builtin.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index 32087be96..12d4d9ac1 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -816,7 +816,7 @@ mod tests { } } - fn builitin_pairing() -> Builtin { + fn builtin_pairing() -> Builtin { Builtin { pricer: Box::new(Linear { base: 0, word: 0 }), native: ethereum_builtin("bn128_pairing"), @@ -856,7 +856,7 @@ mod tests { fn bn128_pairing_empty() { // should not fail, because empty input is a valid input of 0 elements empty_test( - builitin_pairing(), + builtin_pairing(), bytes("0000000000000000000000000000000000000000000000000000000000000001"), ); } @@ -865,7 +865,7 @@ mod tests { fn bn128_pairing_notcurve() { // should fail - point not on curve error_test( - builitin_pairing(), + builtin_pairing(), &bytes("\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ @@ -882,7 +882,7 @@ mod tests { fn bn128_pairing_fragmented() { // should fail - input length is invalid error_test( - builitin_pairing(), + builtin_pairing(), &bytes("\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\