Esempio n. 1
0
// parseBigInt treats the given bytes as a big-endian, signed integer and returns
// the result.
func parseBigInt(bytes []byte) *big.Int {
	ret := new(big.Int)
	if len(bytes) > 0 && bytes[0]&0x80 == 0x80 {
		// This is a negative number.
		notBytes := make([]byte, len(bytes))
		for i := range notBytes {
			notBytes[i] = ^bytes[i]
		}
		ret.SetBytes(notBytes)
		ret.Add(ret, bigOne)
		ret.Neg(ret)
		return ret
	}
	ret.SetBytes(bytes)
	return ret
}
Esempio n. 2
0
func (curve *Curve) Add(p1, p2 *Point) *Point {
	fmt.Printf("a")
	if p1 == nil {
		return p2
	}
	if p2 == nil {
		return p1
	}
	lambda_numerator := new(big.Int)
	lambda_denominator := new(big.Int)
	lambda := new(big.Int)
	lambda_numerator.Sub(p2.Y, p1.Y)
	lambda_denominator.Sub(p2.X, p1.X)
	if lambda_denominator.Cmp(BigZero) == -1 { //if Y is negative
		lambda_denominator.Neg(lambda_denominator)
		lambda_denominator.Sub(curve.P, lambda_denominator)
	}
	lambda_denominator, ok := modInverse(lambda_denominator, curve.P)
	if !ok {
		fmt.Printf("Add : Not ok\n")
		return nil
	}
	lambda.Mul(lambda_numerator, lambda_denominator)
	lambda = lambda.Mod(lambda, curve.P)

	p3 := NewPoint()
	p3.X.Exp(lambda, BigTwo, curve.P)
	p3.X.Sub(p3.X, p1.X)
	p3.X.Sub(p3.X, p2.X)
	p3.X = p3.X.Mod(p3.X, curve.P)

	p3.Y.Sub(p1.X, p3.X)
	p3.Y.Mul(lambda, p3.Y)
	p3.Y.Sub(p3.Y, p1.Y)
	p3.Y = p3.Y.Mod(p3.Y, curve.P)

	if p3.X.Cmp(BigZero) == -1 { //if X is negative
		p3.X.Neg(p3.X)
		p3.X.Sub(curve.P, p3.X)
	}
	if p3.Y.Cmp(BigZero) == -1 { //if Y is negative
		p3.Y.Neg(p3.Y)
		p3.Y.Sub(curve.P, p3.Y)
	}
	return p3
}
Esempio n. 3
0
func B64ToBigInt(in string, b *big.Int) (err os.Error) {
	bsize := base64.StdEncoding.DecodedLen(len(in))
	buff := make([]byte, bsize)
	n, err := base64.StdEncoding.Decode(buff, bytes.NewBufferString(in).Bytes())
	neg := false
	if err == nil {
		buff = buff[0:n]
		if buff[0]&0x80 == 0x80 {
			neg = true
			buff[0] &= 0x7f
		}
		b.SetBytes(buff)
		// In case the passed in big was negative...
		b.Abs(b)
		if neg {
			b.Neg(b)
		}
	}
	return
}