MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / Verify

Function Verify

cipher/dsa/dsa.go:157–181  ·  view source on GitHub ↗

Verify is signature verification for DSA 1. Compute w = s^-1 mod q 2. Compute u1 = (H(m) * w) mod q 3. Compute u2 = (r * w) mod q 4. Compute v = ((g^u1 * y^u2) mod p) mod q 5. If v == r, the signature is valid

(m []byte, r, s, p, q, g, y *big.Int)

Source from the content-addressed store, hash-verified

155// 4. Compute v = ((g^u1 * y^u2) mod p) mod q
156// 5. If v == r, the signature is valid
157func Verify(m []byte, r, s, p, q, g, y *big.Int) bool {
158 // 1. Compute w = s^-1 mod q
159 w := new(big.Int).ModInverse(s, q)
160
161 // 2. Compute u1 = (H(m) * w) mod q
162 h := new(big.Int).SetBytes(m) // This should be the hash of the message
163 u1 := new(big.Int).Mul(h, w)
164 u1.Mod(u1, q)
165
166 // 3. Compute u2 = (r * w) mod q
167 u2 := new(big.Int).Mul(r, w)
168 u2.Mod(u2, q)
169
170 // 4. Compute v = ((g^u1 * y^u2) mod p) mod q
171 v := new(big.Int).Exp(g, u1, p)
172 v.Mul(
173 v,
174 new(big.Int).Exp(y, u2, p),
175 )
176 v.Mod(v, p)
177 v.Mod(v, q)
178
179 // 5. If v == r, the signature is valid
180 return v.Cmp(r) == 0
181}
182
183// GetPublicKey returns the public key (y)
184func (dsa *dsa) GetPublicKey() *big.Int {

Callers 2

TestDSAFunction · 0.92
BenchmarkDSAVerifyFunction · 0.92

Calls

no outgoing calls

Tested by 2

TestDSAFunction · 0.74
BenchmarkDSAVerifyFunction · 0.74