Exemplo n.º 1
0
func (l *LogTreeNode) ComputeHash() sha.Hash {
	var buffer [68]byte
	binary.LittleEndian.PutUint32(buffer[0:4], uint32(l.Num))
	copy(buffer[4:36], ads.Hash(l.Left).Bytes())
	copy(buffer[36:68], ads.Hash(l.Right).Bytes())
	return sha.Sum(buffer[:])
}
Exemplo n.º 2
0
func (n *BitrieNode) ComputeHash() sha.Hash {
	var buffer [96]byte
	n.Bits.Canonicalize(buffer[0:32])
	copy(buffer[32:64], ads.Hash(n.Left).Bytes())
	copy(buffer[64:96], ads.Hash(n.Right).Bytes())
	return sha.Sum(buffer[:])
}
Exemplo n.º 3
0
func (c *PagingC) Store(info *ads.Info) int64 {
	if info.Token != 0 {
		return info.Token
	}

	buffer := ads.GetFromPool()
	defer ads.ReturnToPool(buffer)

	ads.Hash(info.Value)

	buffer.Reset()
	e := ads.Encoder{
		Writer:      buffer,
		Transparent: map[ads.ADS]bool{info.Value: true},
	}
	e.Encode(&info.Value)

	for _, root := range ads.CollectChildren(info.Value) {
		info := ads.GetInfo(root)
		c.Store(info)

		var buffer [8]byte
		binary.LittleEndian.PutUint64(buffer[:], uint64(info.Token))
		if n, err := e.Write(buffer[:]); n != 8 || err != nil {
			log.Panic(err)
		}
	}

	info.Token = c.DB.Write(buffer.Bytes())
	return info.Token
}
Exemplo n.º 4
0
func ProcessTransactionImpl(transaction *Transaction, balances bitrie.Bitrie, c comp.C) bitrie.Bitrie {
	c.Use(transaction, balances)

	for _, input := range transaction.MsgTx.TxIn {
		if (input.PreviousOutpoint.Hash == btcwire.ShaHash{}) {
			continue
		}

		balances = ProcessOutpoint(input.PreviousOutpoint, balances, c)
	}

	loc := bitrie.MakeBits(ads.Hash(transaction))
	x, found := balances.Get(loc, c)
	var oi *OutpointInfo
	if found {
		oi = x.(*OutpointInfo)
	} else {
		oi = &OutpointInfo{}
	}

	c.Use(oi)

	oi = oi.Add(len(transaction.MsgTx.TxOut))
	return balances.Set(loc, oi, c)
}
Exemplo n.º 5
0
func (l *BitrieLeaf) ComputeHash() sha.Hash {
	var buffer [64]byte
	l.Bits.Canonicalize(buffer[0:32])
	if l.Value == nil {
		spew.Dump(l)
	}
	copy(buffer[32:64], ads.Hash(l.Value).Bytes())
	return sha.Sum(buffer[:])
}
Exemplo n.º 6
0
func doRound(elems []Hashable, volatileLeft, volatileRight bool, c comp.C) round {
	N := len(elems)
	kind := make([]int, N)

	left := 0
	right := N - 1

	hashes := make([]sha.Hash, N)
	for i := 0; i < N; i++ {
		hashes[i] = ads.Hash(elems[i])
	}

	for idx := uint(0); ; idx++ {
		if idx > 0 && idx%sha.Bits == 0 {
			for i := 0; i < N; i++ {
				hashes[i] = sha.Sum(hashes[i].Bytes())
			}
		}

		done := true

		if volatileLeft {
			if left < N && kind[left] == unknown && hashes[left].Bit(idx%sha.Bits) == 0 {
				kind[left] = leftFringe
				left++
			}

			if left < N && kind[left] == unknown {
				done = false
			}
		}

		if volatileRight {
			if right >= 0 && kind[right] == unknown && hashes[right].Bit(idx%sha.Bits) == 1 {
				kind[right] = rightFringe
				right--
			}

			if right >= 0 && kind[right] == unknown {
				done = false
			}
		}

		for j := 0; j < N-1; j++ {
			if kind[j] == unknown && kind[j+1] == unknown {
				if hashes[j].Bit(idx%sha.Bits) == 1 && hashes[j+1].Bit(idx%sha.Bits) == 0 {
					kind[j] = mergeLeft
					kind[j+1] = mergeRight
				} else {
					done = false
				}
			}
		}

		if done {
			break
		}
	}

	var r round
	for i := 0; i < N; i++ {
		switch kind[i] {
		case unknown:
			r.center = append(r.center, elems[i])
		case mergeLeft:
			r.center = append(r.center, elems[i].CombineWith(elems[i+1], c))
			i++
		case leftFringe:
			r.leftFringe = append(r.leftFringe, elems[i])
		case rightFringe:
			r.rightFringe = append(r.rightFringe, elems[i])
		}
	}
	return r
}
Exemplo n.º 7
0
func verifiedMain() {
	ads.RegisterType(0, &LogTreap{})
	ads.RegisterType(1, &LogEntry{})
	ads.RegisterType(2, &Tmp{})
	ads.RegisterType(4, int64(0))
	ads.RegisterType(5, &LogTreeNode{})
	ads.RegisterType(6, &seqhash.Hash{})

	ads.RegisterFunc(0, fib)

	c := NewProofC()
	fmt.Printf("fib(5) = %d\n", Fib(5, c))

	/*
		log := c.stack[0]

		seqHash := log.Slice(0, 10000)
		c = NewProofC()
		next, err := Resolve(seqHash.Finish(c).(LogTree), c)
		spew.Dump(next, err)

		buffer := new(bytes.Buffer)
		encoder := ads.Encoder{
			Writer:      buffer,
			Transparent: c.used,
		}
		encoder.Encode(&seqHash)

		fmt.Printf("proof length: %v\n", buffer.Len())

		var newSeqHash *seqhash.Hash
		decoder := ads.Decoder{
			Reader: buffer,
		}
		decoder.Decode(&newSeqHash)

		next, err = Resolve(newSeqHash.Finish(comp.NilC).(LogTree), comp.NilC)
		spew.Dump(next, err)
		// spew.Dump(newSeqHash)

		fmt.Printf("%s\n%s\n", ads.Hash(seqHash), ads.Hash(newSeqHash))
	*/

	for i := int32(1); i <= c.Stack[0].Count(comp.NilC); i++ {
		seqHash := c.Stack[0].Slice(0, i, comp.NilC)

		fmt.Printf("commitment %v\n", ads.Hash(seqHash))
		fmt.Printf("printing prefix of length %d\n", i)

		logTree := seqHash.Finish(comp.NilC).(LogTree)

		for j, entry := range logTree.Flatten() {
			switch entry.Type {
			case FunctionEntry:
				fmt.Printf("%d: enter fib(%d)\n", j, entry.ArgsOrResults[0])
			case FunctionExit:
				idx := int32(j) - entry.Length
				entryEntry := logTree.Index(idx, comp.NilC)
				fmt.Printf("%d: exit fib(%d)@%d -> %d\n", j, entryEntry.ArgsOrResults[0], idx, entry.ArgsOrResults[0])
			}
		}

		//spew.Dump("last:", logTree.Flatten()[i-1])

		next, err := Resolve(logTree, comp.NilC)

		if err != nil {
			panic(err)
		} else {
			if next != nil {
				expected := seqhash.Merge(seqHash, seqhash.New(next), comp.NilC)
				fmt.Printf("predicted  %v\n", ads.Hash(expected))
			}

			//spew.Dump("next:", next)
		}
	}

	/*
		var t *Treap

		for i := 0; i < 10000; i++ {
			c := NewProofC()
			t = t.Insert(KeyType(i), ValueType(i*3), c)
		}

		c := NewProofC()
		fmt.Printf("found %v\n", *t.Find(20, c))
		t.CachingCount(c)

		tmp := &Tmp{Log: c.log}

		fmt.Printf("%v\n", Hash(tmp))

		buffer := new(bytes.Buffer)
		encoder := Encoder{
			Writer:      buffer,
			transparent: map[Value]bool{tmp: true},
		}

		encoder.Encode(&tmp)

		var newTmp *Tmp
		decoder := Decoder{
			Reader: buffer,
		}
		decoder.Decode(&newTmp)

		spew.Dump(newTmp)
	*/

	/*
		buffer := new(bytes.Buffer)
		encoder := Encoder{
			Writer:      buffer,
			transparent: c.used,
		}

		encoder.Encode(&t)

		fmt.Printf("proof size: %d\n", buffer.Len())

		var newT *Treap
		decoder := Decoder{
			Reader: buffer,
		}
		decoder.Decode(&newT)

		// spew.Dump(t)
		// spew.Dump(newT)

		c = NewC()
		fmt.Printf("found %v\n", *newT.Find(20, c))

		fmt.Printf("%+v\n", Hash(t))
		fmt.Printf("%+v\n", Hash(newT))
	*/

	/*
		le := new(LogEntry)

		le.Func = (*Treap).cachingCount
		le.ArgsOrResults = []interface{}{t}

		buffer := new(bytes.Buffer)
		encoder := Encoder{
			Writer:      buffer,
			transparent: map[Value]bool{le: true},
		}

		encoder.Encode(&le)

		fmt.Printf("proof size: %d\n", buffer.Len())

		var newLE *LogEntry
		decoder := Decoder{
			Reader: buffer,
		}
		decoder.Decode(&newLE)

		fmt.Printf("hash of     le: %v\n", Hash(le))
		fmt.Printf("hash of new le: %v\n", Hash(newLE))
	*/
}