Permalink
Browse files

Initial import.

  • Loading branch information...
ignopeverell committed Oct 21, 2016
0 parents commit f73a308bf3c9c3a621f8b08d5dfd2e8d87c8a840
Showing with 29,870 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +40 −0 README.md
  3. +16 −0 TODO.md
  4. +15 −0 chain/Cargo.toml
  5. +3 −0 chain/rustfmt.toml
  6. +26 −0 chain/src/lib.rs
  7. +118 −0 chain/src/pipe.rs
  8. +69 −0 chain/src/store.rs
  9. +133 −0 chain/src/types.rs
  10. +50 −0 chain/tests/mine_simple_chain.rs
  11. +13 −0 core/Cargo.toml
  12. +3 −0 core/rustfmt.toml
  13. +1,028 −0 core/src/core/mod.rs
  14. +308 −0 core/src/core/ser.rs
  15. +38 −0 core/src/genesis.rs
  16. +24 −0 core/src/lib.rs
  17. +59 −0 core/src/macros.rs
  18. +444 −0 core/src/pow/.cuckoo.rs.rustfmt
  19. +374 −0 core/src/pow/cuckoo.rs
  20. +178 −0 core/src/pow/mod.rs
  21. +68 −0 core/src/pow/siphash.rs
  22. +158 −0 core/src/ser.rs
  23. +8 −0 grin/Cargo.toml
  24. +3 −0 grin/rustfmt.toml
  25. +23 −0 grin/src/main.rs
  26. +2 −0 secp256k1zkp/.gitignore
  27. +23 −0 secp256k1zkp/.travis.yml
  28. +34 −0 secp256k1zkp/Cargo.toml
  29. +122 −0 secp256k1zkp/LICENSE
  30. +6 −0 secp256k1zkp/Makefile
  31. +15 −0 secp256k1zkp/README.md
  32. +50 −0 secp256k1zkp/build.rs
  33. +48 −0 secp256k1zkp/depend/secp256k1-zkp/.gitignore
  34. +70 −0 secp256k1zkp/depend/secp256k1-zkp/.travis.yml
  35. +19 −0 secp256k1zkp/depend/secp256k1-zkp/COPYING
  36. +167 −0 secp256k1zkp/depend/secp256k1-zkp/Makefile.am
  37. +61 −0 secp256k1zkp/depend/secp256k1-zkp/README.md
  38. +3 −0 secp256k1zkp/depend/secp256k1-zkp/TODO
  39. +3 −0 secp256k1zkp/depend/secp256k1-zkp/autogen.sh
  40. +140 −0 secp256k1zkp/depend/secp256k1-zkp/build-aux/m4/ax_jni_include_dir.m4
  41. +125 −0 secp256k1zkp/depend/secp256k1-zkp/build-aux/m4/ax_prog_cc_for_build.m4
  42. +65 −0 secp256k1zkp/depend/secp256k1-zkp/build-aux/m4/bitcoin_secp.m4
  43. +510 −0 secp256k1zkp/depend/secp256k1-zkp/configure.ac
  44. +150 −0 secp256k1zkp/depend/secp256k1-zkp/contrib/lax_der_parsing.c
  45. +91 −0 secp256k1zkp/depend/secp256k1-zkp/contrib/lax_der_parsing.h
  46. +113 −0 secp256k1zkp/depend/secp256k1-zkp/contrib/lax_der_privatekey_parsing.c
  47. +90 −0 secp256k1zkp/depend/secp256k1-zkp/contrib/lax_der_privatekey_parsing.h
  48. +583 −0 secp256k1zkp/depend/secp256k1-zkp/include/secp256k1.h
  49. +31 −0 secp256k1zkp/depend/secp256k1-zkp/include/secp256k1_ecdh.h
  50. +204 −0 secp256k1zkp/depend/secp256k1-zkp/include/secp256k1_rangeproof.h
  51. +110 −0 secp256k1zkp/depend/secp256k1-zkp/include/secp256k1_recovery.h
  52. +173 −0 secp256k1zkp/depend/secp256k1-zkp/include/secp256k1_schnorr.h
  53. +13 −0 secp256k1zkp/depend/secp256k1-zkp/libsecp256k1.pc.in
  54. 0 secp256k1zkp/depend/secp256k1-zkp/obj/.gitignore
  55. +322 −0 secp256k1zkp/depend/secp256k1-zkp/sage/group_prover.sage
  56. +306 −0 secp256k1zkp/depend/secp256k1-zkp/sage/secp256k1.sage
  57. +264 −0 secp256k1zkp/depend/secp256k1-zkp/sage/weierstrass_prover.sage
  58. +919 −0 secp256k1zkp/depend/secp256k1-zkp/src/asm/field_10x26_arm.s
  59. +32 −0 secp256k1zkp/depend/secp256k1-zkp/src/basic-config.h
  60. +66 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench.h
  61. +54 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_ecdh.c
  62. +382 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_internal.c
  63. +65 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_rangeproof.c
  64. +60 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_recover.c
  65. +73 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_schnorr_verify.c
  66. +56 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_sign.c
  67. +112 −0 secp256k1zkp/depend/secp256k1-zkp/src/bench_verify.c
  68. +21 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecdsa.h
  69. +303 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecdsa_impl.h
  70. +25 −0 secp256k1zkp/depend/secp256k1-zkp/src/eckey.h
  71. +99 −0 secp256k1zkp/depend/secp256k1-zkp/src/eckey_impl.h
  72. +31 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecmult.h
  73. +15 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecmult_const.h
  74. +239 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecmult_const_impl.h
  75. +43 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecmult_gen.h
  76. +210 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecmult_gen_impl.h
  77. +389 −0 secp256k1zkp/depend/secp256k1-zkp/src/ecmult_impl.h
  78. +127 −0 secp256k1zkp/depend/secp256k1-zkp/src/field.h
  79. +47 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_10x26.h
  80. +1,144 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_10x26_impl.h
  81. +47 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_5x52.h
  82. +502 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_5x52_asm_impl.h
  83. +455 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_5x52_impl.h
  84. +277 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_5x52_int128_impl.h
  85. +315 −0 secp256k1zkp/depend/secp256k1-zkp/src/field_impl.h
  86. +74 −0 secp256k1zkp/depend/secp256k1-zkp/src/gen_context.c
  87. +144 −0 secp256k1zkp/depend/secp256k1-zkp/src/group.h
  88. +650 −0 secp256k1zkp/depend/secp256k1-zkp/src/group_impl.h
  89. +41 −0 secp256k1zkp/depend/secp256k1-zkp/src/hash.h
  90. +281 −0 secp256k1zkp/depend/secp256k1-zkp/src/hash_impl.h
  91. +478 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org/bitcoin/NativeSecp256k1.java
  92. +247 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org/bitcoin/NativeSecp256k1Test.java
  93. +45 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org/bitcoin/NativeSecp256k1Util.java
  94. +51 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org/bitcoin/Secp256k1Context.java
  95. +411 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org_bitcoin_NativeSecp256k1.c
  96. +127 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org_bitcoin_NativeSecp256k1.h
  97. +15 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org_bitcoin_Secp256k1Context.c
  98. +22 −0 secp256k1zkp/depend/secp256k1-zkp/src/java/org_bitcoin_Secp256k1Context.h
  99. +8 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/ecdh/Makefile.am.include
  100. +54 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/ecdh/main_impl.h
  101. +75 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/ecdh/tests_impl.h
  102. +15 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/Makefile.am.include
  103. +24 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/borromean.h
  104. +201 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/borromean_impl.h
  105. +212 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/main_impl.h
  106. +34 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/pedersen.h
  107. +139 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/pedersen_impl.h
  108. +31 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/rangeproof.h
  109. +736 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/rangeproof_impl.h
  110. +281 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/rangeproof/tests_impl.h
  111. +8 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/recovery/Makefile.am.include
  112. +193 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/recovery/main_impl.h
  113. +250 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/recovery/tests_impl.h
  114. +10 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/schnorr/Makefile.am.include
  115. +164 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/schnorr/main_impl.h
  116. +20 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/schnorr/schnorr.h
  117. +207 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/schnorr/schnorr_impl.h
  118. +175 −0 secp256k1zkp/depend/secp256k1-zkp/src/modules/schnorr/tests_impl.h
  119. +74 −0 secp256k1zkp/depend/secp256k1-zkp/src/num.h
  120. +20 −0 secp256k1zkp/depend/secp256k1-zkp/src/num_gmp.h
  121. +288 −0 secp256k1zkp/depend/secp256k1-zkp/src/num_gmp_impl.h
  122. +24 −0 secp256k1zkp/depend/secp256k1-zkp/src/num_impl.h
  123. +104 −0 secp256k1zkp/depend/secp256k1-zkp/src/scalar.h
  124. +19 −0 secp256k1zkp/depend/secp256k1-zkp/src/scalar_4x64.h
  125. +949 −0 secp256k1zkp/depend/secp256k1-zkp/src/scalar_4x64_impl.h
  126. +19 −0 secp256k1zkp/depend/secp256k1-zkp/src/scalar_8x32.h
  127. +721 −0 secp256k1zkp/depend/secp256k1-zkp/src/scalar_8x32_impl.h
  128. +335 −0 secp256k1zkp/depend/secp256k1-zkp/src/scalar_impl.h
  129. +586 −0 secp256k1zkp/depend/secp256k1-zkp/src/secp256k1.c
  130. +41 −0 secp256k1zkp/depend/secp256k1-zkp/src/testrand.h
  131. +127 −0 secp256k1zkp/depend/secp256k1-zkp/src/testrand_impl.h
  132. +4,580 −0 secp256k1zkp/depend/secp256k1-zkp/src/tests.c
  133. +137 −0 secp256k1zkp/depend/secp256k1-zkp/src/util.h
  134. +72 −0 secp256k1zkp/src/constants.rs
  135. +132 −0 secp256k1zkp/src/ecdh.rs
  136. +381 −0 secp256k1zkp/src/ffi.rs
  137. +719 −0 secp256k1zkp/src/key.rs
  138. +1,002 −0 secp256k1zkp/src/lib.rs
  139. +227 −0 secp256k1zkp/src/macros.rs
  140. +367 −0 secp256k1zkp/src/pedersen.rs
  141. +197 −0 secp256k1zkp/src/schnorr.rs
  142. +10 −0 store/Cargo.toml
  143. +3 −0 store/rustfmt.toml
  144. +91 −0 store/src/lib.rs
@@ -0,0 +1,2 @@
*.swp
target
@@ -0,0 +1,40 @@
# Grin

Grin is an in-progress implementation of the MimbleWimble protocol. Many characteristics are still undefined but the following constitutes a first set of choices:

* Clean and minimal implementation, aiming to stay as such.
* Follows the MimbleWimble protocol, which provides great anonymity and scaling characteristics.
* Cuckoo Cycle proof of work (at least to start with).
* Relatively fast block time (a minute or less, possibly decreasing as networks improve).
* Fixed block reward, both over time and in blocks (fees are not additive).
* Transaction fees are based on the number of UTXO created/destroyed and total transaction size.
* Smooth curve for difficulty adjustments.

## Status

Grin is still an infant, much is left to be done and contributions are welcome (see below). Our [status file](TODO.md) may help.

## Contributing

Find an area you can help with and do it. Open source is about collaboration and open participation. Try to make your code look like what already exists and submit a pull request. If you're looking for additional ideas, the code includes TODO comments for minor to major improvements. Grep is your friend.

Additional tests are rewarded with an immense amount of positive karma. So is documentation.

Find us on Gitter: https://gitter.im/grin_community

## Philosophy

Grin likes itself small and easy on the eyes. It wants to be inclusive and welcoming for all walks of life, without judgement. Grin is terribly ambitious, but not at the detriment of others, rather to further us all. It may have strong opinions to stay in line with its objectives, which doesn't mean disrepect of others' ideas.

We believe in pull requests, data and scientific research. We do not believe in unfounded beliefs.

## Credits

Tom Elvis Jedusor for the first formulation of MimbleWimble.
Andrew Poelstra for his related work and improvements.
John Tromp for the Cuckoo Cycles proof of work.
J.K. Rowling for making it despite extraordinary adversity.

## License

Apache License v2.0.
16 TODO.md
@@ -0,0 +1,16 @@
# Rolling up our sleeves

Grin is still an infant, much is left to be done and contributions are welcome. Here's a (non exhaustive) list of what's left to implement:

* Transaction signatures aggregation.
* Proper API to author transactions.
* Protocol layer (handshake, send blocks, ask for blocks, send txs, ask for them, sync, etc.).
* Maintenance of the UTXO set and its Merkle tree.
* Transaction pool and related validation.
* Chain logic and related validation.
* Efficient miner (as a distinct project).
* User-friendly wallet (as a distinct project).
* Figure out if the rangeproofs can be eliminated under some cicrumstances while keeping security guarantes.
* Website, logo design and all the cool stuff.

Don't worry, we'll get there, with your help.
@@ -0,0 +1,15 @@
[package]
name = "grin_chain"
version = "0.1.0"
authors = ["Ignotus Peverell <igno.peverell@protonmail.com>"]

[dependencies]
bitflags = "^0.7.0"
byteorder = "^0.5"

grin_core = { path = "../core" }
grin_store = { path = "../store" }
secp256k1zkp = { path = "../secp256k1zkp" }

[dev-dependencies]
rand = "^0.3"
@@ -0,0 +1,3 @@
hard_tabs = true
wrap_comments = true
write_mode = "Overwrite"
@@ -0,0 +1,26 @@
//! The block chain itself, validates and accepts new blocks, handles reorgs.

#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]

#[macro_use]
extern crate bitflags;
extern crate byteorder;

#[macro_use(try_m)]
extern crate grin_core as core;
extern crate grin_store;
extern crate secp256k1zkp as secp;

pub mod pipe;
pub mod store;
pub mod types;

// Re-export the base interface

pub use types::ChainStore;
pub use chain::Options;
pub use chain::process_block;
@@ -0,0 +1,118 @@
//! Implementation of the chain block acceptance (or refusal) pipeline.

use secp;

use core::core::{Hash, BlockHeader, Block, Proof};
use core::pow;
use types;
use types::{Tip, ChainStore};
use store;

bitflags! {
/// Options for block validation
pub flags Options: u32 {
/// Runs with the easier version of the Proof of Work, mostly to make testing easier.
const EASY_POW = 0b00000001,
}
}

/// Contextual information required to process a new block and either reject or
/// accept it.
pub struct BlockContext<'a> {
opts: Options,
store: &'a ChainStore,
head: Tip,
tip: Option<Tip>,
}

#[derive(Debug)]
pub enum Error {
/// The block doesn't fit anywhere in our chain
Unfit(String),
/// The proof of work is invalid
InvalidPow,
/// The block doesn't sum correctly or a tx signature is invalid
InvalidBlockProof(secp::Error),
/// Internal issue when trying to save the block
StoreErr(types::Error),
}

pub fn process_block(b: &Block, store: &ChainStore, opts: Options) -> Option<Error> {
// TODO should just take a promise for a block with a full header so we don't
// spend resources reading the full block when its header is invalid

let head = match store.head() {
Ok(head) => head,
Err(err) => return Some(Error::StoreErr(err)),
};
let mut ctx = BlockContext {
opts: opts,
store: store,
head: head,
tip: None,
};

try_m!(validate_header(&b, &mut ctx));
try_m!(set_tip(&b.header, &mut ctx));
try_m!(validate_block(b, &mut ctx));
try_m!(add_block(b, &mut ctx));
try_m!(update_tips(&mut ctx));
None
}

// block processing pipeline
// 1. is the header valid (time, PoW, etc.)
// 2. is it the next head, a new fork, or extension of a fork (not a too old
// fork tho)
// 3. ok fine, is all of it valid (txs, merkle, utxo merkle, etc.)
// 4. add the sucker to the head/fork
// 5. did we increase a fork difficulty over the head?
// 6. ok fine, swap them up (can be tricky, think addresses invalidation)

/// First level of black validation that only needs to act on the block header
/// to make it as cheap as possible. The different validations are also
/// arranged by order of cost to have as little DoS surface as possible.
/// TODO actually require only the block header (with length information)
fn validate_header(b: &Block, ctx: &mut BlockContext) -> Option<Error> {
let header = &b.header;
println!("{} {}", header.height, ctx.head.height);
if header.height > ctx.head.height + 1 {
// TODO actually handle orphans and add them to a size-limited set
return Some(Error::Unfit("orphan".to_string()));
}
// TODO check time wrt to chain time, refuse older than 100 blocks or too far
// in future

// TODO maintain current difficulty
let diff_target = Proof(pow::MAX_TARGET);

if ctx.opts.intersects(EASY_POW) {
if !pow::verify20(b, diff_target) {
return Some(Error::InvalidPow);
}
} else if !pow::verify(b, diff_target) {
return Some(Error::InvalidPow);
}
None
}

fn set_tip(h: &BlockHeader, ctx: &mut BlockContext) -> Option<Error> {
ctx.tip = Some(ctx.head.clone());
None
}

fn validate_block(b: &Block, ctx: &mut BlockContext) -> Option<Error> {
// TODO check tx merkle tree
let curve = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
try_m!(b.verify(&curve).err().map(&Error::InvalidBlockProof));
None
}

fn add_block(b: &Block, ctx: &mut BlockContext) -> Option<Error> {
ctx.tip = ctx.tip.as_ref().map(|t| t.append(b.hash()));
ctx.store.save_block(b).map(&Error::StoreErr)
}

fn update_tips(ctx: &mut BlockContext) -> Option<Error> {
ctx.store.save_head(ctx.tip.as_ref().unwrap()).map(&Error::StoreErr)
}
@@ -0,0 +1,69 @@
//! Implements storage primitives required by the chain

use byteorder::{WriteBytesExt, BigEndian};

use types::*;
use core::core::Block;
use grin_store;

const STORE_PATH: &'static str = ".grin/chain";

const SEP: u8 = ':' as u8;

const BLOCK_PREFIX: u8 = 'B' as u8;
const TIP_PREFIX: u8 = 'T' as u8;
const HEAD_PREFIX: u8 = 'H' as u8;

/// An implementation of the ChainStore trait backed by a simple key-value
/// store.
pub struct ChainKVStore {
db: grin_store::Store,
}

impl ChainKVStore {
pub fn new() -> Result<ChainKVStore, Error> {
let db = try!(grin_store::Store::open(STORE_PATH).map_err(to_store_err));
Ok(ChainKVStore { db: db })
}
}

impl ChainStore for ChainKVStore {
fn head(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&vec![HEAD_PREFIX]))
}

fn save_block(&self, b: &Block) -> Option<Error> {
self.db.put_ser(&to_key(BLOCK_PREFIX, &mut b.hash().to_vec())[..], b).map(&to_store_err)
}

fn save_head(&self, t: &Tip) -> Option<Error> {
try_m!(self.save_tip(t));
self.db.put_ser(&vec![HEAD_PREFIX], t).map(&to_store_err)
}

fn save_tip(&self, t: &Tip) -> Option<Error> {
let last_branch = t.lineage.last_branch();
let mut k = vec![TIP_PREFIX, SEP];
k.write_u32::<BigEndian>(last_branch);
self.db.put_ser(&mut k, t).map(&to_store_err)
}
}

fn to_key(prefix: u8, val: &mut Vec<u8>) -> &mut Vec<u8> {
val.insert(0, SEP);
val.insert(0, prefix);
val
}

fn to_store_err(e: grin_store::Error) -> Error {
Error::StorageErr(format!("{:?}", e))
}

/// unwraps the inner option by converting the none case to a not found error
fn option_to_not_found<T>(res: Result<Option<T>, grin_store::Error>) -> Result<T, Error> {
match res {
Ok(None) => Err(Error::NotFoundErr),
Ok(Some(o)) => Ok(o),
Err(e) => Err(to_store_err(e)),
}
}
Oops, something went wrong.

0 comments on commit f73a308

Please sign in to comment.