Esempio n. 1
0
func (p *point) Mul(cb abstract.Point, cs abstract.Scalar) abstract.Point {
	s := cs.(*scalar)
	if cb == nil { // multiply standard generator
		if C.EC_POINT_mul(p.c.g, p.p, s.bignum.bn, nil, nil,
			p.c.ctx) == 0 {
			panic("EC_POINT_mul: " + getErrString())
		}
	} else { // multiply arbitrary point b
		b := cb.(*point)
		if C.EC_POINT_mul(p.c.g, p.p, nil, b.p, s.bignum.bn,
			p.c.ctx) == 0 {
			panic("EC_POINT_mul: " + getErrString())
		}
	}
	return p
}
Esempio n. 2
0
func (curve CurveParams) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) {
	bnx1 := C.BN_new()
	if bnx1 == nil {
		return nil, nil
	}
	defer C.BN_free(bnx1)
	bny1 := C.BN_new()
	if bny1 == nil {
		return nil, nil
	}
	defer C.BN_free(bny1)

	x1s := C.CString(x1.String())
	defer C.free(unsafe.Pointer(x1s))
	if C.BN_hex2bn(&bnx1, x1s) == 0 {
		return nil, nil
	}
	y1s := C.CString(y1.String())
	defer C.free(unsafe.Pointer(y1s))
	if C.BN_hex2bn(&bny1, y1s) == 0 {
		return nil, nil
	}

	pointR := C.EC_POINT_new(curve.curve)
	if pointR == nil {
		return nil, nil
	}
	defer C.EC_POINT_free(pointR)

	pointQ := C.EC_POINT_new(curve.curve)
	if pointQ == nil {
		return nil, nil
	}
	defer C.EC_POINT_free(pointQ)
	if C.EC_POINT_set_affine_coordinates_GFp(curve.curve, pointQ, bnx1, bny1, nil) != 1 {
		return nil, nil
	}

	n := C.BN_new()
	if n == nil {
		return nil, nil
	}
	defer C.BN_free(n)

	bn := C.CString(string(k))
	defer C.free(unsafe.Pointer(bn))
	if C.BN_hex2bn(&n, bn) == 0 {
		return nil, nil
	}

	if C.EC_POINT_mul(curve.curve, pointR, n, pointQ, nil, nil) != 1 {
		return nil, nil
	}

	rx := C.BN_new()
	if rx == nil {
		return nil, nil
	}
	defer C.BN_free(rx)
	ry := C.BN_new()
	if ry == nil {
		return nil, nil
	}
	defer C.BN_free(ry)
	if C.EC_POINT_get_affine_coordinates_GFp(curve.curve, pointR, rx, ry, nil) != 1 {
		return nil, nil
	}
	x, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(rx)), 16)
	y, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(ry)), 16)

	return
}