Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c84fbe
commit 10ed946
Showing
6 changed files
with
223 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
package snowblossom.miner; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.util.Set; | ||
import java.util.List; | ||
import java.util.TreeSet; | ||
|
||
import java.nio.ByteBuffer; | ||
import snowblossom.lib.Globals; | ||
import snowblossom.lib.SnowMerkle; | ||
import java.util.logging.Logger; | ||
|
||
public class FieldSourceComposit extends FieldSource | ||
{ | ||
private List<FieldSource> sources; | ||
|
||
public FieldSourceComposit(List<FieldSource> sources) | ||
{ | ||
this.sources = sources; | ||
TreeSet<Integer> total=new TreeSet<>(); | ||
|
||
for(FieldSource fs : sources) | ||
{ | ||
total.addAll(fs.getHoldingSet()); | ||
} | ||
|
||
holding_set = ImmutableSet.copyOf(total); | ||
} | ||
|
||
public void bulkRead(long word_index, ByteBuffer bb) throws java.io.IOException | ||
{ | ||
int chunk = (int)(word_index / words_per_chunk); | ||
for(FieldSource fs : sources) | ||
{ | ||
if (fs.hasChunk(chunk)) | ||
{ | ||
fs.bulkRead(word_index, bb); | ||
return; | ||
} | ||
} | ||
throw new RuntimeException("oh noes"); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package snowblossom.miner; | ||
|
||
import java.security.MessageDigest; | ||
|
||
import duckutil.TimeRecord; | ||
import duckutil.TimeRecordAuto; | ||
import java.nio.ByteBuffer; | ||
|
||
import com.google.protobuf.ByteString; | ||
|
||
import org.junit.Assert; | ||
|
||
import snowblossom.lib.*; | ||
import snowblossom.proto.*; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
|
||
public class ProofGen | ||
{ | ||
public static SnowPowProof getProof(FieldSource field_source, FieldSource deck_source, long word_index, long total_words) throws java.io.IOException | ||
{ | ||
try (TimeRecordAuto tra = TimeRecord.openAuto("SnowMerkleProof.getProof")) | ||
{ | ||
LinkedList<ByteString> partners = new LinkedList<ByteString>(); | ||
|
||
MessageDigest md; | ||
try | ||
{ | ||
md = MessageDigest.getInstance(Globals.SNOW_MERKLE_HASH_ALGO); | ||
} | ||
catch (java.security.NoSuchAlgorithmException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
getInnerProof(field_source, deck_source, md, partners, word_index, 0, total_words); | ||
|
||
SnowPowProof.Builder builder = SnowPowProof.newBuilder(); | ||
builder.setWordIdx(word_index); | ||
builder.addAllMerkleComponent(partners); | ||
|
||
return builder.build(); | ||
} | ||
} | ||
|
||
/** | ||
* If the target is not in specified subtree, return hash of subtree | ||
* If the target is in the specified subtree, return null and add hash partner from | ||
* opposite subtree to partners | ||
*/ | ||
private static ByteString getInnerProof( | ||
FieldSource field_source, FieldSource deck_source, | ||
MessageDigest md, List<ByteString> partners, | ||
long target_word_index, long start, long end) throws java.io.IOException | ||
{ | ||
boolean inside = false; | ||
if ((start <= target_word_index) && (target_word_index < end)) | ||
{ | ||
inside = true; | ||
} | ||
|
||
long dist = end - start; | ||
if (!inside) | ||
{ | ||
if (deck_source.getDeckFiles().containsKey(dist)) | ||
{ | ||
long deck_pos = SnowMerkle.HASH_LEN_LONG * (start / dist); | ||
byte[] buff = new byte[SnowMerkle.HASH_LEN]; | ||
ByteBuffer bb = ByteBuffer.wrap(buff); | ||
|
||
ChannelUtil.readFully(deck_source.getDeckFiles().get(dist), bb, deck_pos); | ||
|
||
return ByteString.copyFrom(buff); | ||
} | ||
|
||
if (dist == 1) | ||
{ | ||
byte[] buff = new byte[SnowMerkle.HASH_LEN]; | ||
ByteBuffer bb = ByteBuffer.wrap(buff); | ||
field_source.bulkRead(start, bb); | ||
|
||
return ByteString.copyFrom(buff); | ||
|
||
} | ||
long mid = (start + end) / 2; | ||
|
||
ByteString left = getInnerProof(field_source, deck_source, md, partners, target_word_index, start, mid); | ||
ByteString right = getInnerProof(field_source, deck_source, md, partners, target_word_index, mid, end); | ||
|
||
md.update(left.toByteArray()); | ||
md.update(right.toByteArray()); | ||
|
||
byte[] hash = md.digest(); | ||
|
||
return ByteString.copyFrom(hash); | ||
|
||
} | ||
else | ||
{ // we are inside | ||
|
||
if (dist == 1) | ||
{ | ||
byte[] buff = new byte[SnowMerkle.HASH_LEN]; | ||
ByteBuffer bb = ByteBuffer.wrap(buff); | ||
field_source.bulkRead(start, bb); | ||
|
||
partners.add(ByteString.copyFrom(buff)); | ||
return null; | ||
} | ||
|
||
long mid = (start + end) / 2; | ||
|
||
ByteString left = getInnerProof(field_source, deck_source, md, partners, target_word_index, start, mid); | ||
ByteString right = getInnerProof(field_source, deck_source, md, partners, target_word_index, mid, end); | ||
|
||
if (target_word_index < mid) | ||
{ | ||
partners.add(right); | ||
Assert.assertNull(left); | ||
} | ||
else | ||
{ | ||
partners.add(left); | ||
Assert.assertNull(right); | ||
} | ||
return null; | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |