// ScalarEncodeTo provides a generic implementation of Scalar.EncodeTo // based on Scalar.Encode. func ScalarMarshalTo(s abstract.Scalar, w io.Writer) (int, error) { buf, err := s.MarshalBinary() if err != nil { return 0, err } return w.Write(buf) }
// Simple helper to compute G^{ab-cd} for Theta vector computation. func thenc(grp abstract.Group, G abstract.Point, a, b, c, d abstract.Scalar) abstract.Point { var ab, cd abstract.Scalar if a != nil { ab = grp.Scalar().Mul(a, b) } else { ab = grp.Scalar().Zero() } if c != nil { if d != nil { cd = grp.Scalar().Mul(c, d) } else { cd = c } } else { cd = grp.Scalar().Zero() } return grp.Point().Mul(G, ab.Sub(ab, cd)) }
// ScalarDecodeFrom provides a generic implementation of Scalar.DecodeFrom, // based on Scalar.Decode, or Scalar.Pick if r is a Cipher or cipher.Stream. // The returned byte-count is valid only when decoding from a normal Reader, // not when picking from a pseudorandom source. func ScalarUnmarshalFrom(s abstract.Scalar, r io.Reader) (int, error) { if strm, ok := r.(cipher.Stream); ok { s.Pick(strm) return -1, nil // no byte-count when picking randomly } buf := make([]byte, s.MarshalSize()) n, err := io.ReadFull(r, buf) if err != nil { return n, err } return n, s.UnmarshalBinary(buf) }
// Simple helper to verify Theta elements, // by checking whether A^a*B^-b = T. // P,Q,s are simply "scratch" abstract.Point/Scalars reused for efficiency. func thver(A, B, T, P, Q abstract.Point, a, b, s abstract.Scalar) bool { P.Mul(A, a) Q.Mul(B, s.Neg(b)) P.Add(P, Q) return P.Equal(T) }
// ScalarHex encodes a scalar to hexadecimal func ScalarHex(suite abstract.Suite, scalar abstract.Scalar) (string, error) { sbuf, err := scalar.MarshalBinary() return hex.EncodeToString(sbuf), err }